blob: dd5e21ff81ab9cd79d9e87e4b2886bc68d69bbbe [file] [log] [blame]
Mark Lobodzinski317574e2016-08-29 14:21:14 -06001/*
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002 *
Mark Young39389872017-01-19 21:10:49 -07003 * Copyright (c) 2014-2017 The Khronos Group Inc.
4 * Copyright (c) 2014-2017 Valve Corporation
5 * Copyright (c) 2014-2017 LunarG, Inc.
Courtney Goeltzenleuchterf821dad2015-12-02 14:53:22 -07006 * Copyright (C) 2015 Google Inc.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08007 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06008 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080013 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19
Jon Ashburn23d36b12016-02-02 17:47:28 -070020 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060021 * Author: Jon Ashburn <jon@lunarg.com>
Jon Ashburn23d36b12016-02-02 17:47:28 -070022 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Mark Young39389872017-01-19 21:10:49 -070023 * Author: Mark Young <marky@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060024 *
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080025 */
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070026
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060027#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080028#include <stdio.h>
29#include <stdlib.h>
30#include <stdarg.h>
31#include <stdbool.h>
32#include <string.h>
Benjamin Saunders31a48012017-01-29 14:49:54 -080033#include <stddef.h>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080034
Chia-I Wu13a61a52014-08-04 11:18:20 +080035#include <sys/types.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050036#if defined(_WIN32)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070037#include "dirent_on_windows.h"
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070038#else // _WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080039#include <dirent.h>
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070040#endif // _WIN32
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060041#include "vk_loader_platform.h"
Chia-I Wu19300602014-08-04 08:03:57 +080042#include "loader.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060043#include "gpa_helper.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060044#include "debug_report.h"
Ian Elliott954fa342015-10-30 15:28:23 -060045#include "wsi.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070046#include "vulkan/vk_icd.h"
Jon Ashburn2077e382015-06-29 11:25:34 -060047#include "cJSON.h"
Jon Ashburnfc1031e2015-11-17 15:31:02 -070048#include "murmurhash.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080049
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +020050#if defined(_WIN32)
51#include <Cfgmgr32.h>
52#include <initguid.h>
53#include <Devpkey.h>
54#endif
55
Mark Youngba7ee022017-03-09 10:41:25 -070056// This is a CMake generated file with #defines for any functions/includes
57// that it found present. This is currently necessary to properly determine
58// if secure_getenv or __secure_getenv are present
Jamie Madille09268a2017-03-21 13:48:13 -040059#if !defined(VULKAN_NON_CMAKE_BUILD)
Mark Youngba7ee022017-03-09 10:41:25 -070060#include "loader_cmake_config.h"
Jamie Madille09268a2017-03-21 13:48:13 -040061#endif // !defined(VULKAN_NON_CMAKE_BUILD)
Mark Youngba7ee022017-03-09 10:41:25 -070062
Mark Young0f183a82017-02-28 09:58:04 -070063// Generated file containing all the extension data
64#include "vk_loader_extensions.c"
65
Jon Ashburn27cd5842015-05-12 17:26:48 -060066struct loader_struct loader = {0};
Jon Ashburn87d6aa92015-08-28 15:19:27 -060067// TLS for instance for alloc/free callbacks
68THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080069
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -070070static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap00b4aba2015-09-30 11:50:51 -050071
Jon Ashburn24cd4be2015-11-01 14:04:06 -070072struct loader_phys_dev_per_icd {
73 uint32_t count;
74 VkPhysicalDevice *phys_devs;
Mark Young0153e0b2016-11-03 14:27:13 -060075 struct loader_icd_term *this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -070076};
77
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060078enum loader_debug {
Jon Ashburn23d36b12016-02-02 17:47:28 -070079 LOADER_INFO_BIT = 0x01,
80 LOADER_WARN_BIT = 0x02,
81 LOADER_PERF_BIT = 0x04,
82 LOADER_ERROR_BIT = 0x08,
83 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060084};
85
86uint32_t g_loader_debug = 0;
87uint32_t g_loader_log_msgs = 0;
88
Jon Ashburn23d36b12016-02-02 17:47:28 -070089// thread safety lock for accessing global data structures such as "loader"
Jon Ashburn6301a0f2015-05-29 13:15:39 -060090// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburn2077e382015-06-29 11:25:34 -060091// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburn6301a0f2015-05-29 13:15:39 -060092loader_platform_thread_mutex loader_lock;
Jon Ashburn6461ef22015-09-22 13:11:00 -060093loader_platform_thread_mutex loader_json_lock;
Jon Ashburn6301a0f2015-05-29 13:15:39 -060094
Jon Ashburn8810c5f2015-08-18 18:04:47 -060095LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070096
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070097void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope alloc_scope) {
Mark Young0ad83132016-06-30 13:02:42 -060098 void *pMemory = NULL;
99#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
100 {
101#else
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800102 if (instance && instance->alloc_callbacks.pfnAllocation) {
Mark Young0f183a82017-02-28 09:58:04 -0700103 // These are internal structures, so it's best to align everything to
104 // the largest unit size which is the size of a uint64_t.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700105 pMemory = instance->alloc_callbacks.pfnAllocation(instance->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600106 } else {
107#endif
108 pMemory = malloc(size);
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600109 }
Mark Youngf2079b92017-05-02 10:49:46 -0600110
Mark Young0ad83132016-06-30 13:02:42 -0600111 return pMemory;
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600112}
113
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700114void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory) {
Mark Young0ad83132016-06-30 13:02:42 -0600115 if (pMemory != NULL) {
116#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
117 {
118#else
119 if (instance && instance->alloc_callbacks.pfnFree) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700120 instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData, pMemory);
Mark Young0ad83132016-06-30 13:02:42 -0600121 } else {
122#endif
123 free(pMemory);
Mark Youngd077f992016-06-30 11:03:59 -0600124 }
Mark Young4b0b9222016-06-29 18:33:53 -0600125 }
Mark Young4b0b9222016-06-29 18:33:53 -0600126}
127
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700128void *loader_instance_heap_realloc(const struct loader_instance *instance, void *pMemory, size_t orig_size, size_t size,
Mark Young0ad83132016-06-30 13:02:42 -0600129 VkSystemAllocationScope alloc_scope) {
130 void *pNewMem = NULL;
131 if (pMemory == NULL || orig_size == 0) {
132 pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
133 } else if (size == 0) {
134 loader_instance_heap_free(instance, pMemory);
135#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
136#else
137 } else if (instance && instance->alloc_callbacks.pfnReallocation) {
Mark Young0f183a82017-02-28 09:58:04 -0700138 // These are internal structures, so it's best to align everything to
139 // the largest unit size which is the size of a uint64_t.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700140 pNewMem = instance->alloc_callbacks.pfnReallocation(instance->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
141 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600142#endif
143 } else {
144 pNewMem = realloc(pMemory, size);
145 }
146 return pNewMem;
Mark Young4b0b9222016-06-29 18:33:53 -0600147}
148
Mark Young0ad83132016-06-30 13:02:42 -0600149void *loader_instance_tls_heap_alloc(size_t size) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700150 return loader_instance_heap_alloc(tls_instance, size, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young4b0b9222016-06-29 18:33:53 -0600151}
Mark Young4b0b9222016-06-29 18:33:53 -0600152
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700153void loader_instance_tls_heap_free(void *pMemory) { loader_instance_heap_free(tls_instance, pMemory); }
Mark Young0ad83132016-06-30 13:02:42 -0600154
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700155void *loader_device_heap_alloc(const struct loader_device *device, size_t size, VkSystemAllocationScope alloc_scope) {
Mark Young0ad83132016-06-30 13:02:42 -0600156 void *pMemory = NULL;
157#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
158 {
159#else
160 if (device && device->alloc_callbacks.pfnAllocation) {
Mark Young0f183a82017-02-28 09:58:04 -0700161 // These are internal structures, so it's best to align everything to
162 // the largest unit size which is the size of a uint64_t.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700163 pMemory = device->alloc_callbacks.pfnAllocation(device->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600164 } else {
165#endif
166 pMemory = malloc(size);
167 }
168 return pMemory;
169}
170
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700171void loader_device_heap_free(const struct loader_device *device, void *pMemory) {
Mark Young0ad83132016-06-30 13:02:42 -0600172 if (pMemory != NULL) {
173#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
174 {
175#else
176 if (device && device->alloc_callbacks.pfnFree) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700177 device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData, pMemory);
Mark Young0ad83132016-06-30 13:02:42 -0600178 } else {
179#endif
180 free(pMemory);
181 }
182 }
183}
184
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700185void *loader_device_heap_realloc(const struct loader_device *device, void *pMemory, size_t orig_size, size_t size,
Mark Young0ad83132016-06-30 13:02:42 -0600186 VkSystemAllocationScope alloc_scope) {
187 void *pNewMem = NULL;
188 if (pMemory == NULL || orig_size == 0) {
189 pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
190 } else if (size == 0) {
191 loader_device_heap_free(device, pMemory);
192#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
193#else
194 } else if (device && device->alloc_callbacks.pfnReallocation) {
Mark Young0f183a82017-02-28 09:58:04 -0700195 // These are internal structures, so it's best to align everything to
196 // the largest unit size which is the size of a uint64_t.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700197 pNewMem = device->alloc_callbacks.pfnReallocation(device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
198 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600199#endif
200 } else {
201 pNewMem = realloc(pMemory, size);
202 }
203 return pNewMem;
204}
205
206// Environment variables
207#if defined(__linux__)
208
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700209static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600210 // No allocation of memory necessary for Linux, but we should at least touch
211 // the inst pointer to get rid of compiler warnings.
212 (void)inst;
Mark Youngd8c6b692017-03-09 11:39:41 -0700213 return getenv(name);
214}
215
Mark Youngd8c6b692017-03-09 11:39:41 -0700216static inline char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
217 // No allocation of memory necessary for Linux, but we should at least touch
218 // the inst pointer to get rid of compiler warnings.
219 (void)inst;
Mark Youngf2f2a662017-03-08 10:11:52 -0700220
Mark Youngba7ee022017-03-09 10:41:25 -0700221#ifdef HAVE_SECURE_GETENV
222 return secure_getenv(name);
223#elif defined(HAVE___SECURE_GETENV)
Mark Youngf2f2a662017-03-08 10:11:52 -0700224 return __secure_getenv(name);
225#else
Mark Youngf2079b92017-05-02 10:49:46 -0600226#pragma message( \
227 "Warning: Falling back to non-secure getenv for environmental lookups! Consider" \
228 " updating to a different libc.")
Mark Youngd8c6b692017-03-09 11:39:41 -0700229 return loader_getenv(name, inst);
Mark Youngf2f2a662017-03-08 10:11:52 -0700230#endif
Mark Young0ad83132016-06-30 13:02:42 -0600231}
Mark Youngf2f2a662017-03-08 10:11:52 -0700232
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700233static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600234 // No freeing of memory necessary for Linux, but we should at least touch
235 // the val and inst pointers to get rid of compiler warnings.
236 (void)val;
237 (void)inst;
238}
239
240#elif defined(WIN32)
241
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700242static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600243 char *retVal;
244 DWORD valSize;
245
246 valSize = GetEnvironmentVariableA(name, NULL, 0);
247
248 // valSize DOES include the null terminator, so for any set variable
249 // will always be at least 1. If it's 0, the variable wasn't set.
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700250 if (valSize == 0) return NULL;
Mark Young0ad83132016-06-30 13:02:42 -0600251
252 // Allocate the space necessary for the registry entry
253 if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700254 retVal = (char *)inst->alloc_callbacks.pfnAllocation(inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
255 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -0600256 } else {
257 retVal = (char *)malloc(valSize);
258 }
259
260 if (NULL != retVal) {
261 GetEnvironmentVariableA(name, retVal, valSize);
262 }
263
264 return retVal;
265}
266
Mark Youngd8c6b692017-03-09 11:39:41 -0700267static inline char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
Mark Youngbb3a29c2017-05-19 12:29:43 -0600268 // No secure version for Windows as far as I know
Mark Youngd8c6b692017-03-09 11:39:41 -0700269 return loader_getenv(name, inst);
270}
271
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700272static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600273 if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
274 inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
275 } else {
276 free((void *)val);
277 }
278}
279
280#else
281
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700282static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600283 // stub func
284 (void)inst;
285 (void)name;
286 return NULL;
287}
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700288static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600289 // stub func
290 (void)val;
291 (void)inst;
292}
293
294#endif
295
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700296void loader_log(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code, const char *format, ...) {
Jon Ashburn86723b02015-07-31 15:47:59 -0600297 char msg[512];
Mark Young2c84c0c2017-01-13 10:27:03 -0700298 char cmd_line_msg[512];
Mark Young5de3af72017-04-25 08:08:29 -0600299 size_t cmd_line_size = sizeof(cmd_line_msg);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700300 va_list ap;
301 int ret;
302
Jon Ashburnffad94d2015-06-30 14:46:22 -0700303 va_start(ap, format);
304 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700305 if ((ret >= (int)sizeof(msg)) || ret < 0) {
306 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnffad94d2015-06-30 14:46:22 -0700307 }
308 va_end(ap);
309
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700310 if (inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700311 util_DebugReportMessage(inst, msg_type, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, (uint64_t)(uintptr_t)inst, 0, msg_code,
312 "loader", msg);
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700313 }
314
315 if (!(msg_type & g_loader_log_msgs)) {
316 return;
317 }
318
Mark Young2c84c0c2017-01-13 10:27:03 -0700319 cmd_line_msg[0] = '\0';
Mark Young5de3af72017-04-25 08:08:29 -0600320 cmd_line_size -= 1;
321 size_t original_size = cmd_line_size;
Mark Young2c84c0c2017-01-13 10:27:03 -0700322
323 va_start(ap, format);
324 if ((msg_type & LOADER_INFO_BIT) != 0) {
Mark Young6ef95f42017-02-17 09:02:23 -0700325 strncat(cmd_line_msg, "INFO", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700326 cmd_line_size -= 4;
327 }
328 if ((msg_type & LOADER_WARN_BIT) != 0) {
Mark Young5de3af72017-04-25 08:08:29 -0600329 if (cmd_line_size != original_size) {
Mark Young6ef95f42017-02-17 09:02:23 -0700330 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700331 cmd_line_size -= 3;
332 }
Mark Young6ef95f42017-02-17 09:02:23 -0700333 strncat(cmd_line_msg, "WARNING", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700334 cmd_line_size -= 7;
335 }
336 if ((msg_type & LOADER_PERF_BIT) != 0) {
Mark Young5de3af72017-04-25 08:08:29 -0600337 if (cmd_line_size != original_size) {
Mark Young6ef95f42017-02-17 09:02:23 -0700338 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700339 cmd_line_size -= 3;
340 }
Mark Young6ef95f42017-02-17 09:02:23 -0700341 strncat(cmd_line_msg, "PERF", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700342 cmd_line_size -= 4;
343 }
344 if ((msg_type & LOADER_ERROR_BIT) != 0) {
Mark Young5de3af72017-04-25 08:08:29 -0600345 if (cmd_line_size != original_size) {
Mark Young6ef95f42017-02-17 09:02:23 -0700346 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700347 cmd_line_size -= 3;
348 }
Mark Young6ef95f42017-02-17 09:02:23 -0700349 strncat(cmd_line_msg, "ERROR", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700350 cmd_line_size -= 5;
351 }
352 if ((msg_type & LOADER_DEBUG_BIT) != 0) {
Mark Young5de3af72017-04-25 08:08:29 -0600353 if (cmd_line_size != original_size) {
Mark Young6ef95f42017-02-17 09:02:23 -0700354 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700355 cmd_line_size -= 3;
356 }
Mark Young6ef95f42017-02-17 09:02:23 -0700357 strncat(cmd_line_msg, "DEBUG", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700358 cmd_line_size -= 5;
359 }
Mark Young5de3af72017-04-25 08:08:29 -0600360 if (cmd_line_size != original_size) {
Mark Young6ef95f42017-02-17 09:02:23 -0700361 strncat(cmd_line_msg, ": ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700362 cmd_line_size -= 2;
363 }
Mark Young5de3af72017-04-25 08:08:29 -0600364
365 if (0 < cmd_line_size) {
366 // If the message is too long, trim it down
367 if (strlen(msg) > cmd_line_size) {
368 msg[cmd_line_size - 1] = '\0';
369 }
370 strncat(cmd_line_msg, msg, cmd_line_size);
371 } else {
372 // Shouldn't get here, but check to make sure if we've already overrun
373 // the string boundary
374 assert(false);
375 }
Mark Young2c84c0c2017-01-13 10:27:03 -0700376
Ian Elliott4470a302015-02-17 10:33:47 -0700377#if defined(WIN32)
Mark Young2c84c0c2017-01-13 10:27:03 -0700378 OutputDebugString(cmd_line_msg);
mschottb9cdb782015-07-22 14:11:29 +0200379 OutputDebugString("\n");
Jon Ashburnffad94d2015-06-30 14:46:22 -0700380#endif
Mark Young5de3af72017-04-25 08:08:29 -0600381
Mark Young2c84c0c2017-01-13 10:27:03 -0700382 fputs(cmd_line_msg, stderr);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700383 fputc('\n', stderr);
384}
385
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700386VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceDispatch(VkInstance instance, void *object) {
Jon Ashburnc3c58772016-03-29 11:16:01 -0600387 struct loader_instance *inst = loader_get_instance(instance);
388 if (!inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700389 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
390 "vkSetInstanceDispatch: Can not retrieve Instance "
391 "dispatch table.");
Jon Ashburnc3c58772016-03-29 11:16:01 -0600392 return VK_ERROR_INITIALIZATION_FAILED;
393 }
394 loader_set_dispatch(object, inst->disp);
395 return VK_SUCCESS;
396}
397
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700398VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600399 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700400 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Jon Ashburned8f2312016-03-31 10:52:22 -0600401
Mark Young0153e0b2016-11-03 14:27:13 -0600402 if (NULL == icd_term) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600403 return VK_ERROR_INITIALIZATION_FAILED;
404 }
405 loader_set_dispatch(object, &dev->loader_dispatch);
406 return VK_SUCCESS;
407}
408
Lenny Komowf7c09382017-08-31 16:35:08 -0600409#if defined(_WIN32)
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200410// Find the list of registry files (names VulkanDriverName/VulkanDriverNameWow) in hkr.
411//
412// This function looks for filename in given device handle, filename is then added to return list
413// function return true if filename was appended to reg_data list
414// If error occures result is updated with failure reason
Lenny Komowf7c09382017-08-31 16:35:08 -0600415bool loaderGetDeviceRegistryEntry(const struct loader_instance *inst, char **reg_data, PDWORD total_size, DEVINST dev_id, LPCTSTR value_name, VkResult *result)
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200416{
417 HKEY hkrKey = INVALID_HANDLE_VALUE;
Lenny Komowf7c09382017-08-31 16:35:08 -0600418 DWORD requiredSize, data_type;
419 char *manifest_path = NULL;
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200420 bool found = false;
421
422 if (NULL == total_size || NULL == reg_data) {
423 *result = VK_ERROR_INITIALIZATION_FAILED;
424 return false;
425 }
426
Lenny Komowf7c09382017-08-31 16:35:08 -0600427 CONFIGRET status = CM_Open_DevNode_Key(dev_id, KEY_QUERY_VALUE, 0, RegDisposition_OpenExisting, &hkrKey, CM_REGISTRY_SOFTWARE);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200428 if (status != CR_SUCCESS) {
429 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Lenny Komowf7c09382017-08-31 16:35:08 -0600430 "loaderGetDeviceRegistryEntry: Failed to open registry key for DeviceID(%d)", dev_id);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200431 *result = VK_ERROR_INITIALIZATION_FAILED;
432 return false;
433 }
434
435 // query value
436 LSTATUS ret = RegQueryValueEx(
437 hkrKey,
Lenny Komowf7c09382017-08-31 16:35:08 -0600438 value_name,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200439 NULL,
440 NULL,
441 NULL,
442 &requiredSize);
443
444 if (ret != ERROR_SUCCESS) {
Lenny Komowf7c09382017-08-31 16:35:08 -0600445 if (ret == ERROR_FILE_NOT_FOUND) {
446 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
447 "loaderGetDeviceRegistryEntry: Device ID(%d) Does not contain a value for \"%s\"", dev_id, value_name);
448 } else {
449 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
450 "loaderGetDeviceRegistryEntry: DeviceID(%d) Failed to obtain %s size", dev_id, value_name);
451 }
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200452 goto out;
453 }
454
Lenny Komowf7c09382017-08-31 16:35:08 -0600455 manifest_path = loader_instance_heap_alloc(inst, requiredSize, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
456 if (manifest_path == NULL) {
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200457 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
458 "loaderGetDeviceRegistryEntry: Failed to allocate space for DriverName.");
459 *result = VK_ERROR_OUT_OF_HOST_MEMORY;
460 goto out;
461 }
462
463 ret = RegQueryValueEx(
464 hkrKey,
Lenny Komowf7c09382017-08-31 16:35:08 -0600465 value_name,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200466 NULL,
Lenny Komowf7c09382017-08-31 16:35:08 -0600467 &data_type,
Jamie Madilla11ae0b2017-11-08 14:48:32 -0500468 (BYTE *)manifest_path,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200469 &requiredSize
470 );
471
472 if (ret != ERROR_SUCCESS) {
473 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Lenny Komowf7c09382017-08-31 16:35:08 -0600474 "loaderGetDeviceRegistryEntry: DeviceID(%d) Failed to obtain %s", value_name);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200475
476 *result = VK_ERROR_INITIALIZATION_FAILED;
477 goto out;
478 }
479
Lenny Komowf7c09382017-08-31 16:35:08 -0600480 if (data_type != REG_SZ && data_type != REG_MULTI_SZ) {
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200481 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Lenny Komowf7c09382017-08-31 16:35:08 -0600482 "loaderGetDeviceRegistryEntry: Invalid %s data type. Expected REG_SZ or REG_MULTI_SZ.", value_name);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200483 *result = VK_ERROR_INITIALIZATION_FAILED;
484 goto out;
485 }
486
487 if (NULL == *reg_data) {
488 *reg_data = loader_instance_heap_alloc(inst, *total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
489 if (NULL == *reg_data) {
490 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Lenny Komowf7c09382017-08-31 16:35:08 -0600491 "loaderGetDeviceRegistryEntry: Failed to allocate space for registry data for key %s", manifest_path);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200492 *result = VK_ERROR_OUT_OF_HOST_MEMORY;
493 goto out;
494 }
495 *reg_data[0] = '\0';
496 } else if (strlen(*reg_data) + requiredSize + 1 > *total_size) {
497 void *new_ptr = loader_instance_heap_realloc(inst, *reg_data, *total_size, *total_size * 2,
498 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
499 if (NULL == new_ptr) {
500 loader_log(
501 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
502 "loaderGetDeviceRegistryEntry: Failed to reallocate space for registry value of size %d for key %s",
Lenny Komowf7c09382017-08-31 16:35:08 -0600503 *total_size * 2, manifest_path);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200504 *result = VK_ERROR_OUT_OF_HOST_MEMORY;
505 goto out;
506 }
507 *reg_data = new_ptr;
508 *total_size *= 2;
509 }
510
Lenny Komowf7c09382017-08-31 16:35:08 -0600511 for (char *curr_filename = manifest_path; curr_filename[0] != '\0'; curr_filename += strlen(curr_filename) + 1) {
512 if (strlen(*reg_data) == 0) {
513 (void)snprintf(*reg_data, requiredSize + 1, "%s", curr_filename);
514 } else {
515 (void)snprintf(*reg_data + strlen(*reg_data), requiredSize + 2, "%c%s", PATH_SEPARATOR, curr_filename);
516 }
517 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, __FUNCTION__ ": Located json file \"%s\" from PnP registry: %s", curr_filename, value_name);
518
519 if (data_type == REG_SZ) {
520 break;
521 }
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200522 }
523 found = true;
524
525out:
Lenny Komowf7c09382017-08-31 16:35:08 -0600526 if (manifest_path != NULL) {
527 loader_instance_heap_free(inst, manifest_path);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200528 }
529 RegCloseKey(hkrKey);
530 return found;
531}
532
533// Find the list of registry files (names VulkanDriverName/VulkanDriverNameWow) in hkr .
534//
535// This function looks for display devices and childish software components
536// for a list of files which are added to a returned list (function return
537// value).
538// Function return is a string with a ';' separated list of filenames.
539// Function return is NULL if no valid name/value pairs are found in the key,
540// or the key is not found.
541//
542// *reg_data contains a string list of filenames as pointer.
543// When done using the returned string list, the caller should free the pointer.
Lenny Komowf7c09382017-08-31 16:35:08 -0600544VkResult loaderGetDeviceRegistryFiles(const struct loader_instance *inst, char **reg_data, PDWORD reg_data_size, LPCTSTR value_name) {
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200545 static const char* softwareComponentGUID = "{5c4c3332-344d-483c-8739-259e934c9cc8}";
546 static const char* displayGUID = "{4d36e968-e325-11ce-bfc1-08002be10318}";
547 const ULONG flags = CM_GETIDLIST_FILTER_CLASS | CM_GETIDLIST_FILTER_PRESENT;
548
549 char childGuid[MAX_GUID_STRING_LEN + 2]; // +2 for brackets {}
550 ULONG childGuidSize = sizeof(childGuid);
551
552 DEVINST devID = 0, childID = 0;
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200553 char *pDeviceNames = NULL;
554 ULONG deviceNamesSize = 0;
555 VkResult result = VK_SUCCESS;
556 bool found = false;
557
558 if (NULL == reg_data) {
559 result = VK_ERROR_INITIALIZATION_FAILED;
560 return result;
561 }
562
563 // if after obtaining the DeviceNameSize, new device is added start over
564 do {
565 CM_Get_Device_ID_List_Size(&deviceNamesSize, displayGUID, flags);
566
567 if (pDeviceNames != NULL) {
568 loader_instance_heap_free(inst, pDeviceNames);
569 }
570
571 pDeviceNames = loader_instance_heap_alloc(inst, deviceNamesSize, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
572 if (pDeviceNames == NULL) {
573 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
574 "loaderGetDeviceRegistryFiles: Failed to allocate space for display device names.");
575 result = VK_ERROR_OUT_OF_HOST_MEMORY;
Slawomir Cygan018bfd52017-08-25 10:54:03 +0200576 return result;
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200577 }
578 } while (CM_Get_Device_ID_List(displayGUID, pDeviceNames, deviceNamesSize, flags) == CR_BUFFER_SMALL);
579
580 if (pDeviceNames) {
581
582 for (char *deviceName = pDeviceNames; *deviceName; deviceName += strlen(deviceName) + 1) {
583 CONFIGRET status = CM_Locate_DevNode(&devID, deviceName, CM_LOCATE_DEVNODE_NORMAL);
584 if (CR_SUCCESS != status) {
585 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
586 "loaderGetRegistryFiles: failed to open DevNode %s", deviceName);
587 continue;
588 }
589 ULONG ulStatus, ulProblem;
590 status = CM_Get_DevNode_Status(&ulStatus, &ulProblem, devID, 0);
591
592 if (CR_SUCCESS != status)
593 {
594 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
595 "loaderGetRegistryFiles: failed to probe device status %s", deviceName);
596 continue;
597 }
598 if ((ulStatus & DN_HAS_PROBLEM) && (ulProblem == CM_PROB_NEED_RESTART || ulProblem == DN_NEED_RESTART))
599 {
Lenny Komowe17a12a2017-08-10 09:25:49 -0600600 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200601 "loaderGetRegistryFiles: device %s is pending reboot, skipping ...", deviceName);
602 continue;
603 }
604
Lenny Komowe17a12a2017-08-10 09:25:49 -0600605 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200606 "loaderGetRegistryFiles: opening device %s", deviceName);
607
Lenny Komowf7c09382017-08-31 16:35:08 -0600608 if (loaderGetDeviceRegistryEntry(inst, reg_data, reg_data_size, devID, value_name, &result)) {
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200609 found = true;
610 continue;
611 }
Igor Ostrowski21532732017-10-06 18:26:23 +0200612 else if (result == VK_ERROR_OUT_OF_HOST_MEMORY) {
613 break;
614 }
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200615
616 status = CM_Get_Child(&childID, devID, 0);
617 if (status != CR_SUCCESS) {
Lenny Komowe17a12a2017-08-10 09:25:49 -0600618 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200619 "loaderGetRegistryFiles: unable to open child-device error:%d", status);
620 continue;
621 }
622
623 do {
624 char buffer[MAX_DEVICE_ID_LEN];
625 CM_Get_Device_ID(childID, buffer, MAX_DEVICE_ID_LEN, 0);
626
Lenny Komowe17a12a2017-08-10 09:25:49 -0600627 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200628 "loaderGetRegistryFiles: Opening child device %d - %s", childID, buffer);
629
630 status = CM_Get_DevNode_Registry_Property(childID, CM_DRP_CLASSGUID, NULL, &childGuid, &childGuidSize, 0);
631 if (status != CR_SUCCESS) {
632 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
633 "loaderGetRegistryFiles: unable to obtain GUID for:%d error:%d", childID, status);
634
635 result = VK_ERROR_INITIALIZATION_FAILED;
636 continue;
637 }
638
639 if (strcmp(childGuid, softwareComponentGUID) != 0) {
640 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
641 "loaderGetRegistryFiles: GUID for %d is not SoftwareComponent skipping", childID);
642 continue;
643 }
644
Lenny Komowf7c09382017-08-31 16:35:08 -0600645 if (loaderGetDeviceRegistryEntry(inst, reg_data, reg_data_size, childID, value_name, &result)) {
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200646 found = true;
647 break; // check next-display-device
648 }
649
650 } while (CM_Get_Sibling(&childID, childID, 0) == CR_SUCCESS);
651 }
652
653 loader_instance_heap_free(inst, pDeviceNames);
654 }
655
656 if (!found && result != VK_ERROR_OUT_OF_HOST_MEMORY) {
657 result = VK_ERROR_INITIALIZATION_FAILED;
658 }
659
660 return result;
661}
662
Tony Barbourea968902015-07-29 14:26:21 -0600663static char *loader_get_next_path(char *path);
Mark Young2c84c0c2017-01-13 10:27:03 -0700664
665// Find the list of registry files (names within a key) in key "location".
666//
667// This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
668// given in "location"
669// for a list or name/values which are added to a returned list (function return
670// value).
671// The DWORD values within the key must be 0 or they are skipped.
672// Function return is a string with a ';' separated list of filenames.
673// Function return is NULL if no valid name/value pairs are found in the key,
674// or the key is not found.
675//
676// *reg_data contains a string list of filenames as pointer.
677// When done using the returned string list, the caller should free the pointer.
Slawomir Cygan8f9f2552017-08-09 11:36:52 +0200678VkResult loaderGetRegistryFiles(const struct loader_instance *inst, char *location, bool use_secondary_hive, char **reg_data, PDWORD reg_data_size) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700679 LONG rtn_value;
Lenny Komowda849dc2017-03-01 17:22:29 -0700680 HKEY hive = DEFAULT_VK_REGISTRY_HIVE, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700681 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700682 char name[2048];
Tony Barbourea968902015-07-29 14:26:21 -0600683 char *loc = location;
684 char *next;
Lenny Komowda849dc2017-03-01 17:22:29 -0700685 DWORD idx;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700686 DWORD name_size = sizeof(name);
687 DWORD value;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700688 DWORD value_size = sizeof(value);
Mark Young2c84c0c2017-01-13 10:27:03 -0700689 VkResult result = VK_SUCCESS;
690 bool found = false;
691
692 if (NULL == reg_data) {
693 result = VK_ERROR_INITIALIZATION_FAILED;
694 goto out;
695 }
Tony Barbourea968902015-07-29 14:26:21 -0600696
Jon Ashburn23d36b12016-02-02 17:47:28 -0700697 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600698 next = loader_get_next_path(loc);
Piers Daniell524ec732015-11-05 16:58:26 -0700699 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600700 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
Lenny Komowda849dc2017-03-01 17:22:29 -0700701 if (ERROR_SUCCESS == rtn_value) {
702 idx = 0;
703 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL, NULL, (LPBYTE)&value, &value_size)) ==
704 ERROR_SUCCESS) {
705 if (value_size == sizeof(value) && value == 0) {
706 if (NULL == *reg_data) {
Slawomir Cygan8f9f2552017-08-09 11:36:52 +0200707 *reg_data = loader_instance_heap_alloc(inst, *reg_data_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Lenny Komowda849dc2017-03-01 17:22:29 -0700708 if (NULL == *reg_data) {
709 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngbb3a29c2017-05-19 12:29:43 -0600710 "loaderGetRegistryFiles: Failed to allocate space for registry data for key %s", name);
Lenny Komowda849dc2017-03-01 17:22:29 -0700711 result = VK_ERROR_OUT_OF_HOST_MEMORY;
712 goto out;
713 }
714 *reg_data[0] = '\0';
Slawomir Cygan8f9f2552017-08-09 11:36:52 +0200715 } else if (strlen(*reg_data) + name_size + 1 > *reg_data_size) {
716 void *new_ptr = loader_instance_heap_realloc(inst, *reg_data, *reg_data_size, *reg_data_size * 2,
Mark Youngbb3a29c2017-05-19 12:29:43 -0600717 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
718 if (NULL == new_ptr) {
719 loader_log(
720 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
721 "loaderGetRegistryFiles: Failed to reallocate space for registry value of size %d for key %s",
Slawomir Cygan8f9f2552017-08-09 11:36:52 +0200722 *reg_data_size * 2, name);
Lenny Komowda849dc2017-03-01 17:22:29 -0700723 result = VK_ERROR_OUT_OF_HOST_MEMORY;
724 goto out;
725 }
Mark Youngbb3a29c2017-05-19 12:29:43 -0600726 *reg_data = new_ptr;
Slawomir Cygan8f9f2552017-08-09 11:36:52 +0200727 *reg_data_size *= 2;
Lenny Komowda849dc2017-03-01 17:22:29 -0700728 }
Mark Youngf2079b92017-05-02 10:49:46 -0600729 loader_log(
730 inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Located json file \"%s\" from registry \"%s\\%s\"", name,
731 hive == DEFAULT_VK_REGISTRY_HIVE ? DEFAULT_VK_REGISTRY_HIVE_STR : SECONDARY_VK_REGISTRY_HIVE_STR, location);
Lenny Komowda849dc2017-03-01 17:22:29 -0700732 if (strlen(*reg_data) == 0) {
733 (void)snprintf(*reg_data, name_size + 1, "%s", name);
734 } else {
735 (void)snprintf(*reg_data + strlen(*reg_data), name_size + 2, "%c%s", PATH_SEPARATOR, name);
736 }
737 found = true;
738 }
739 name_size = 2048;
740 }
Jon Ashburnffad94d2015-06-30 14:46:22 -0700741 }
Tony Barbourea968902015-07-29 14:26:21 -0600742
Lenny Komowda849dc2017-03-01 17:22:29 -0700743 // Advance the location - if the next location is in the secondary hive, then reset the locations and advance the hive
744 if (use_secondary_hive && (hive == DEFAULT_VK_REGISTRY_HIVE) && (*next == '\0')) {
745 loc = location;
746 hive = SECONDARY_VK_REGISTRY_HIVE;
747 } else {
748 loc = next;
Tony Barbourea968902015-07-29 14:26:21 -0600749 }
Jon Ashburnffad94d2015-06-30 14:46:22 -0700750 }
Tony Barbourea968902015-07-29 14:26:21 -0600751
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +0200752 if (!found && result != VK_ERROR_OUT_OF_HOST_MEMORY) {
Mark Young2c84c0c2017-01-13 10:27:03 -0700753 result = VK_ERROR_INITIALIZATION_FAILED;
754 }
755
756out:
757
758 return result;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700759}
760
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700761#endif // WIN32
Ian Elliott4470a302015-02-17 10:33:47 -0700762
Mark Young0f183a82017-02-28 09:58:04 -0700763// Combine path elements, separating each element with the platform-specific
764// directory separator, and save the combined string to a destination buffer,
Mark Youngdee312c2017-03-08 13:38:35 -0700765// not exceeding the given length. Path elements are given as variable args,
Mark Young0f183a82017-02-28 09:58:04 -0700766// with a NULL element terminating the list.
767//
768// \returns the total length of the combined string, not including an ASCII
769// NUL termination character. This length may exceed the available storage:
770// in this case, the written string will be truncated to avoid a buffer
771// overrun, and the return value will greater than or equal to the storage
772// size. A NULL argument may be provided as the destination buffer in order
773// to determine the required string length without actually writing a string.
Jon Ashburn23d36b12016-02-02 17:47:28 -0700774static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700775 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500776 va_list ap;
777 const char *component;
778
779 va_start(ap, len);
780
Jon Ashburn23d36b12016-02-02 17:47:28 -0700781 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500782 if (required_len > 0) {
783 // This path element is not the first non-empty element; prepend
784 // a directory separator if space allows
785 if (dest && required_len + 1 < len) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700786 (void)snprintf(dest + required_len, len - required_len, "%c", DIRECTORY_SYMBOL);
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500787 }
788 required_len++;
789 }
790
791 if (dest && required_len < len) {
792 strncpy(dest + required_len, component, len - required_len);
793 }
794 required_len += strlen(component);
795 }
796
797 va_end(ap);
798
799 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
800 if (dest && required_len >= len) {
801 dest[len - 1] = '\0';
802 }
803
804 return required_len;
805}
806
Mark Young0f183a82017-02-28 09:58:04 -0700807// Given string of three part form "maj.min.pat" convert to a vulkan version number.
Mark Young60861ac2016-09-02 11:39:26 -0600808static uint32_t loader_make_version(char *vers_str) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700809 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
Mark Young60861ac2016-09-02 11:39:26 -0600810 char *vers_tok;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600811
Mark Young60861ac2016-09-02 11:39:26 -0600812 if (!vers_str) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600813 return vers;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600814 }
Mark Young60861ac2016-09-02 11:39:26 -0600815
816 vers_tok = strtok(vers_str, ".\"\n\r");
817 if (NULL != vers_tok) {
818 major = (uint16_t)atoi(vers_tok);
819 vers_tok = strtok(NULL, ".\"\n\r");
820 if (NULL != vers_tok) {
821 minor = (uint16_t)atoi(vers_tok);
822 vers_tok = strtok(NULL, ".\"\n\r");
823 if (NULL != vers_tok) {
824 patch = (uint16_t)atoi(vers_tok);
825 }
826 }
827 }
Jon Ashburnc7237a72015-08-03 09:08:46 -0600828
829 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600830}
831
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700832bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800833 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600834}
835
Mark Young0f183a82017-02-28 09:58:04 -0700836// Search the given ext_array for an extension matching the given vk_ext_prop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700837bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop, const uint32_t count,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700838 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600839 for (uint32_t i = 0; i < count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700840 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i])) return true;
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600841 }
842 return false;
843}
844
Mark Young0f183a82017-02-28 09:58:04 -0700845// Search the given ext_list for an extension matching the given vk_ext_prop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700846bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop, const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600847 for (uint32_t i = 0; i < ext_list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700848 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop)) return true;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600849 }
850 return false;
851}
852
Mark Young0f183a82017-02-28 09:58:04 -0700853// Search the given ext_list for a device extension matching the given ext_prop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700854bool has_vk_dev_ext_property(const VkExtensionProperties *ext_prop, const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600855 for (uint32_t i = 0; i < ext_list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700856 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop)) return true;
Jon Ashburnb8726962016-04-08 15:03:35 -0600857 }
858 return false;
859}
860
Mark Young0f183a82017-02-28 09:58:04 -0700861// Search the given layer list for a layer matching the given layer name
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700862static struct loader_layer_properties *loader_get_layer_property(const char *name, const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600863 for (uint32_t i = 0; i < layer_list->count; i++) {
864 const VkLayerProperties *item = &layer_list->list[i].info;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700865 if (strcmp(name, item->layerName) == 0) return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600866 }
867 return NULL;
868}
869
Mark Young0f183a82017-02-28 09:58:04 -0700870// Get the next unused layer property in the list. Init the property to zero.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700871static struct loader_layer_properties *loader_get_next_layer_property(const struct loader_instance *inst,
872 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600873 if (layer_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700874 layer_list->list =
875 loader_instance_heap_alloc(inst, sizeof(struct loader_layer_properties) * 64, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600876 if (layer_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700877 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
878 "loader_get_next_layer_property: Out of memory can "
879 "not add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600880 return NULL;
881 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700882 memset(layer_list->list, 0, sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600883 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
884 }
885
Mark Young0f183a82017-02-28 09:58:04 -0700886 // Ensure enough room to add an entry
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700887 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) > layer_list->capacity) {
Mark Youngbb3a29c2017-05-19 12:29:43 -0600888 void *new_ptr = loader_instance_heap_realloc(inst, layer_list->list, layer_list->capacity, layer_list->capacity * 2,
889 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
890 if (NULL == new_ptr) {
891 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_next_layer_property: realloc failed for layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600892 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600893 }
Mark Youngbb3a29c2017-05-19 12:29:43 -0600894 layer_list->list = new_ptr;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600895 layer_list->capacity *= 2;
896 }
897
898 layer_list->count++;
899 return &(layer_list->list[layer_list->count - 1]);
900}
901
Mark Youngdee312c2017-03-08 13:38:35 -0700902// Remove all layer properties entries from the list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700903void loader_delete_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_list) {
Cort Stratton6974a132017-11-28 12:11:05 -0800904 uint32_t i, j, k;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700905 struct loader_device_extension_list *dev_ext_list;
Cort Stratton6974a132017-11-28 12:11:05 -0800906 struct loader_dev_ext_props *ext_props;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700907 if (!layer_list) return;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600908
Jon Ashburne13ecc92015-08-03 17:19:30 -0600909 for (i = 0; i < layer_list->count; i++) {
Mark Youngf2079b92017-05-02 10:49:46 -0600910 if (NULL != layer_list->list[i].component_layer_names) {
911 loader_instance_heap_free(inst, layer_list->list[i].component_layer_names);
912 layer_list->list[i].component_layer_names = NULL;
913 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700914 loader_destroy_generic_list(inst, (struct loader_generic_list *)&layer_list->list[i].instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700915 dev_ext_list = &layer_list->list[i].device_extension_list;
Cort Stratton6974a132017-11-28 12:11:05 -0800916 if (dev_ext_list->capacity > 0 && NULL != dev_ext_list->list) {
917 for (j = 0; j < dev_ext_list->count; j++) {
918 ext_props = &dev_ext_list->list[j];
919 if (ext_props->entrypoint_count > 0) {
920 for (k = 0; k < ext_props->entrypoint_count; k++) {
921 loader_instance_heap_free(inst, ext_props->entrypoints[k]);
922 }
923 loader_instance_heap_free(inst, ext_props->entrypoints);
924 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700925 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700926 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700927 loader_destroy_generic_list(inst, (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600928 }
929 layer_list->count = 0;
930
Jon Ashburnb82c1852015-08-11 14:49:54 -0600931 if (layer_list->capacity > 0) {
932 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600933 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600934 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600935}
936
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700937static VkResult loader_add_instance_extensions(const struct loader_instance *inst,
938 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props, const char *lib_name,
939 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700940 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600941 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600942 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600943
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600944 if (!fp_get_props) {
Mark Young0f183a82017-02-28 09:58:04 -0700945 // No EnumerateInstanceExtensionProperties defined
Mark Young3a587792016-08-19 15:25:08 -0600946 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600947 }
948
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600949 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600950 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700951 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
952 "loader_add_instance_extensions: Error getting Instance "
953 "extension count from %s",
Mark Youngb6399312017-01-10 14:22:15 -0700954 lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600955 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600956 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600957
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600958 if (count == 0) {
Mark Young0f183a82017-02-28 09:58:04 -0700959 // No ExtensionProperties to report
Mark Young3a587792016-08-19 15:25:08 -0600960 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600961 }
962
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600963 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young39389872017-01-19 21:10:49 -0700964 if (NULL == ext_props) {
965 res = VK_ERROR_OUT_OF_HOST_MEMORY;
966 goto out;
967 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600968
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600969 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600970 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700971 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
972 "loader_add_instance_extensions: Error getting Instance "
973 "extensions from %s",
Mark Youngb6399312017-01-10 14:22:15 -0700974 lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600975 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600976 }
Tony Barbour59a47322015-06-24 16:06:58 -0600977
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600978 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600979 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600980
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700981 bool ext_unsupported = wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600982 if (!ext_unsupported) {
Mark Young02df1a82017-04-18 19:52:18 -0600983 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_VERSION_MAJOR(ext_props[i].specVersion),
984 VK_VERSION_MINOR(ext_props[i].specVersion), VK_VERSION_PATCH(ext_props[i].specVersion));
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700985 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Instance Extension: %s (%s) version %s", ext_props[i].extensionName,
986 lib_name, spec_version);
Mark Young6267ae62017-01-12 12:27:19 -0700987
Mark Young3a587792016-08-19 15:25:08 -0600988 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
989 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700990 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
991 "loader_add_instance_extensions: Failed to add %s "
992 "to Instance extension list",
Mark Young3a587792016-08-19 15:25:08 -0600993 lib_name);
994 goto out;
995 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600996 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600997 }
Mark Young6267ae62017-01-12 12:27:19 -0700998
Mark Young3a587792016-08-19 15:25:08 -0600999out:
1000 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001001}
1002
Mark Young0f183a82017-02-28 09:58:04 -07001003// Initialize ext_list with the physical device extensions.
1004// The extension properties are passed as inputs in count and ext_props.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001005static VkResult loader_init_device_extensions(const struct loader_instance *inst, struct loader_physical_device_term *phys_dev_term,
1006 uint32_t count, VkExtensionProperties *ext_props,
1007 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001008 VkResult res;
1009 uint32_t i;
1010
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001011 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06001012 if (VK_SUCCESS != res) {
1013 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001014 }
1015
1016 for (i = 0; i < count; i++) {
1017 char spec_version[64];
Mark Young02df1a82017-04-18 19:52:18 -06001018 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_VERSION_MAJOR(ext_props[i].specVersion),
1019 VK_VERSION_MINOR(ext_props[i].specVersion), VK_VERSION_PATCH(ext_props[i].specVersion));
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001020 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
1021 phys_dev_term->this_icd_term->scanned_icd->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001022 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001023 if (res != VK_SUCCESS) return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001024 }
1025
1026 return VK_SUCCESS;
1027}
1028
Jon Ashburn1530c342016-02-26 13:14:27 -07001029VkResult loader_add_device_extensions(const struct loader_instance *inst,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001030 PFN_vkEnumerateDeviceExtensionProperties fpEnumerateDeviceExtensionProperties,
1031 VkPhysicalDevice physical_device, const char *lib_name,
Jon Ashburn1530c342016-02-26 13:14:27 -07001032 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001033 uint32_t i, count;
1034 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001035 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001036
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001037 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001038 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001039 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young9a3ddd42016-10-21 16:25:47 -06001040 if (!ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001041 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1042 "loader_add_device_extensions: Failed to allocate space"
1043 " for device extension properties.");
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001044 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young9a3ddd42016-10-21 16:25:47 -06001045 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001046 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, ext_props);
Mark Young9a3ddd42016-10-21 16:25:47 -06001047 if (res != VK_SUCCESS) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07001048 return res;
Mark Young9a3ddd42016-10-21 16:25:47 -06001049 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001050 for (i = 0; i < count; i++) {
1051 char spec_version[64];
Mark Young02df1a82017-04-18 19:52:18 -06001052 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_VERSION_MAJOR(ext_props[i].specVersion),
1053 VK_VERSION_MINOR(ext_props[i].specVersion), VK_VERSION_PATCH(ext_props[i].specVersion));
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001054 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
1055 lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001056 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
Mark Youngb6399312017-01-10 14:22:15 -07001057 if (res != VK_SUCCESS) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001058 return res;
Mark Youngb6399312017-01-10 14:22:15 -07001059 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001060 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001061 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001062 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1063 "loader_add_device_extensions: Error getting physical "
1064 "device extension info count from library %s",
Jon Ashburn23d36b12016-02-02 17:47:28 -07001065 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -07001066 return res;
1067 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001068
Jon Ashburn24cd4be2015-11-01 14:04:06 -07001069 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001070}
1071
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001072VkResult loader_init_generic_list(const struct loader_instance *inst, struct loader_generic_list *list_info, size_t element_size) {
Mark Young84ba0482016-09-02 11:45:00 -06001073 size_t capacity = 32 * element_size;
1074 list_info->count = 0;
1075 list_info->capacity = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001076 list_info->list = loader_instance_heap_alloc(inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -07001077 if (list_info->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001078 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1079 "loader_init_generic_list: Failed to allocate space "
1080 "for generic list");
Mark Young3a587792016-08-19 15:25:08 -06001081 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001082 }
Mark Young84ba0482016-09-02 11:45:00 -06001083 memset(list_info->list, 0, capacity);
1084 list_info->capacity = capacity;
Mark Young3a587792016-08-19 15:25:08 -06001085 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001086}
1087
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001088void loader_destroy_generic_list(const struct loader_instance *inst, struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -06001089 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -07001090 list->count = 0;
1091 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001092}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001093
Mark Young0f183a82017-02-28 09:58:04 -07001094// Append non-duplicate extension properties defined in props to the given ext_list.
1095// Return - Vk_SUCCESS on success
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001096VkResult loader_add_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list,
1097 uint32_t prop_list_count, const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001098 uint32_t i;
1099 const VkExtensionProperties *cur_ext;
1100
1101 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001102 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06001103 if (VK_SUCCESS != res) {
1104 return res;
1105 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001106 }
1107
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001108 for (i = 0; i < prop_list_count; i++) {
1109 cur_ext = &props[i];
1110
1111 // look for duplicates
1112 if (has_vk_extension_property(cur_ext, ext_list)) {
1113 continue;
1114 }
1115
1116 // add to list at end
1117 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001118 if (ext_list->count * sizeof(VkExtensionProperties) >= ext_list->capacity) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06001119 void *new_ptr = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
1120 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1121 if (new_ptr == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001122 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1123 "loader_add_to_ext_list: Failed to reallocate "
1124 "space for extension list");
Jon Ashburn24cd4be2015-11-01 14:04:06 -07001125 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Youngb6399312017-01-10 14:22:15 -07001126 }
Mark Youngbb3a29c2017-05-19 12:29:43 -06001127 ext_list->list = new_ptr;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07001128
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001129 // double capacity
1130 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001131 }
1132
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001133 memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001134 ext_list->count++;
1135 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -07001136 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06001137}
1138
Mark Youngdee312c2017-03-08 13:38:35 -07001139// Append one extension property defined in props with entrypoints defined in entries to the given
Mark Young0f183a82017-02-28 09:58:04 -07001140// ext_list. Do not append if a duplicate.
1141// Return - Vk_SUCCESS on success
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001142VkResult loader_add_to_dev_ext_list(const struct loader_instance *inst, struct loader_device_extension_list *ext_list,
1143 const VkExtensionProperties *props, uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001144 uint32_t idx;
1145 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001146 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(struct loader_dev_ext_props));
Mark Young3a587792016-08-19 15:25:08 -06001147 if (VK_SUCCESS != res) {
1148 return res;
1149 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001150 }
1151
Jon Ashburnb8726962016-04-08 15:03:35 -06001152 // look for duplicates
1153 if (has_vk_dev_ext_property(props, ext_list)) {
1154 return VK_SUCCESS;
1155 }
1156
Jon Ashburn23d36b12016-02-02 17:47:28 -07001157 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001158 // add to list at end
1159 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001160 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06001161 void *new_ptr = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
1162 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001163
Mark Youngbb3a29c2017-05-19 12:29:43 -06001164 if (NULL == new_ptr) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001165 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngbb3a29c2017-05-19 12:29:43 -06001166 "loader_add_to_dev_ext_list: Failed to reallocate space for device extension list");
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001167 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Youngb6399312017-01-10 14:22:15 -07001168 }
Mark Youngbb3a29c2017-05-19 12:29:43 -06001169 ext_list->list = new_ptr;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001170
1171 // double capacity
1172 ext_list->capacity *= 2;
1173 }
1174
Gabríel Arthúr Pétursson71510142017-06-03 01:38:49 +00001175 memcpy(&ext_list->list[idx].props, props, sizeof(*props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001176 ext_list->list[idx].entrypoint_count = entry_count;
Mark Young3f7a3b32017-06-26 14:03:08 -06001177 if (entry_count == 0) {
1178 ext_list->list[idx].entrypoints = NULL;
1179 } else {
1180 ext_list->list[idx].entrypoints =
1181 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1182 if (ext_list->list[idx].entrypoints == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001183 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1184 "loader_add_to_dev_ext_list: Failed to allocate space "
Mark Young3f7a3b32017-06-26 14:03:08 -06001185 "for device extension entrypoint list in list %d",
1186 idx);
1187 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001188 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001189 }
Mark Young3f7a3b32017-06-26 14:03:08 -06001190 for (uint32_t i = 0; i < entry_count; i++) {
1191 ext_list->list[idx].entrypoints[i] =
1192 loader_instance_heap_alloc(inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1193 if (ext_list->list[idx].entrypoints[i] == NULL) {
1194 for (uint32_t j = 0; j < i; j++) {
1195 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints[j]);
1196 }
1197 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
1198 ext_list->list[idx].entrypoint_count = 0;
1199 ext_list->list[idx].entrypoints = NULL;
1200 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1201 "loader_add_to_dev_ext_list: Failed to allocate space "
1202 "for device extension entrypoint %d name",
1203 i);
1204 return VK_ERROR_OUT_OF_HOST_MEMORY;
1205 }
1206 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
1207 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001208 }
1209 ext_list->count++;
1210
1211 return VK_SUCCESS;
1212}
1213
Mark Youngf2079b92017-05-02 10:49:46 -06001214// Prototype of loader_add_meta_layer function since we use it in the loader_add_implicit_layer, but can also
1215// call loader_add_implicit_layer from loader_add_meta_layer.
1216bool loader_add_meta_layer(const struct loader_instance *inst, const struct loader_layer_properties *prop,
Mark Young283fe1c2017-05-04 12:16:35 -06001217 struct loader_layer_list *target_list, struct loader_layer_list *expanded_target_list,
1218 const struct loader_layer_list *source_list);
1219
1220// Search the given layer list for a list matching the given VkLayerProperties
1221bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop, const struct loader_layer_list *list) {
1222 for (uint32_t i = 0; i < list->count; i++) {
1223 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0) return true;
1224 }
1225 return false;
1226}
1227
1228// Search the given layer list for a layer matching the given name
1229bool has_layer_name(const char *name, const struct loader_layer_list *list) {
1230 for (uint32_t i = 0; i < list->count; i++) {
1231 if (strcmp(name, list->list[i].info.layerName) == 0) return true;
1232 }
1233 return false;
1234}
Mark Youngf2079b92017-05-02 10:49:46 -06001235
Mark Young0f183a82017-02-28 09:58:04 -07001236// Search the given search_list for any layers in the props list. Add these to the
1237// output layer_list. Don't add duplicates to the output layer_list.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001238static VkResult loader_add_layer_names_to_list(const struct loader_instance *inst, struct loader_layer_list *output_list,
Mark Young283fe1c2017-05-04 12:16:35 -06001239 struct loader_layer_list *expanded_output_list, uint32_t name_count,
1240 const char *const *names, const struct loader_layer_list *source_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001241 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001242 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001243
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001244 for (uint32_t i = 0; i < name_count; i++) {
Mark Young283fe1c2017-05-04 12:16:35 -06001245 const char *source_name = names[i];
1246 layer_prop = loader_get_layer_property(source_name, source_list);
Mark Youngf2079b92017-05-02 10:49:46 -06001247 if (NULL == layer_prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001248 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1249 "loader_add_layer_names_to_list: Unable to find layer"
1250 " %s",
Mark Young283fe1c2017-05-04 12:16:35 -06001251 source_name);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001252 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001253 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001254 }
1255
Mark Youngf2079b92017-05-02 10:49:46 -06001256 // If not a meta-layer, simply add it.
1257 if (0 == (layer_prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
Mark Young283fe1c2017-05-04 12:16:35 -06001258 if (!has_vk_layer_property(&layer_prop->info, output_list)) {
1259 loader_add_to_layer_list(inst, output_list, 1, layer_prop);
1260 }
1261 if (!has_vk_layer_property(&layer_prop->info, expanded_output_list)) {
1262 loader_add_to_layer_list(inst, expanded_output_list, 1, layer_prop);
1263 }
Mark Youngf2079b92017-05-02 10:49:46 -06001264 } else {
Mark Young283fe1c2017-05-04 12:16:35 -06001265 if (!has_vk_layer_property(&layer_prop->info, output_list) ||
1266 !has_vk_layer_property(&layer_prop->info, expanded_output_list)) {
1267 loader_add_meta_layer(inst, layer_prop, output_list, expanded_output_list, source_list);
Mark Youngf2079b92017-05-02 10:49:46 -06001268 }
1269 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001270 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001271
1272 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001273}
1274
Mark Young0f183a82017-02-28 09:58:04 -07001275// Manage lists of VkLayerProperties
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001276static bool loader_init_layer_list(const struct loader_instance *inst, struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001277 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001278 list->list = loader_instance_heap_alloc(inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001279 if (list->list == NULL) {
1280 return false;
1281 }
1282 memset(list->list, 0, list->capacity);
1283 list->count = 0;
1284 return true;
1285}
1286
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001287void loader_destroy_layer_list(const struct loader_instance *inst, struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001288 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001289 if (device) {
1290 loader_device_heap_free(device, layer_list->list);
1291 } else {
1292 loader_instance_heap_free(inst, layer_list->list);
1293 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001294 layer_list->count = 0;
1295 layer_list->capacity = 0;
1296}
1297
Mark Young0f183a82017-02-28 09:58:04 -07001298// Append non-duplicate layer properties defined in prop_list to the given layer_info list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001299VkResult loader_add_to_layer_list(const struct loader_instance *inst, struct loader_layer_list *list, uint32_t prop_list_count,
Mark Young0ad83132016-06-30 13:02:42 -06001300 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001301 uint32_t i;
1302 struct loader_layer_properties *layer;
1303
1304 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001305 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001306 }
1307
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001308 if (list->list == NULL) return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001309
1310 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001311 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001312
Mark Youngf2079b92017-05-02 10:49:46 -06001313 // Look for duplicates, and skip
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001314 if (has_vk_layer_property(&layer->info, list)) {
1315 continue;
1316 }
1317
Mark Youngf2079b92017-05-02 10:49:46 -06001318 // Check for enough capacity
1319 if (((list->count + 1) * sizeof(struct loader_layer_properties)) >= list->capacity) {
1320 size_t new_capacity = list->capacity * 2;
Mark Youngbb3a29c2017-05-19 12:29:43 -06001321 void *new_ptr =
Mark Youngf2079b92017-05-02 10:49:46 -06001322 loader_instance_heap_realloc(inst, list->list, list->capacity, new_capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngbb3a29c2017-05-19 12:29:43 -06001323 if (NULL == new_ptr) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001324 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngbb3a29c2017-05-19 12:29:43 -06001325 "loader_add_to_layer_list: Realloc failed for when attempting to add new layer");
Mark Young0ad83132016-06-30 13:02:42 -06001326 return VK_ERROR_OUT_OF_HOST_MEMORY;
1327 }
Mark Youngbb3a29c2017-05-19 12:29:43 -06001328 list->list = new_ptr;
Mark Youngf2079b92017-05-02 10:49:46 -06001329 list->capacity = new_capacity;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001330 }
1331
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001332 memcpy(&list->list[list->count], layer, sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001333 list->count++;
1334 }
Mark Young0ad83132016-06-30 13:02:42 -06001335
1336 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001337}
1338
Mark Youngf2079b92017-05-02 10:49:46 -06001339// Check the individual implicit layer for the enable/disable environment variable settings. Only add it after
1340// every check has passed indicating it should be used.
1341static void loader_add_implicit_layer(const struct loader_instance *inst, const struct loader_layer_properties *prop,
Mark Young283fe1c2017-05-04 12:16:35 -06001342 struct loader_layer_list *target_list, struct loader_layer_list *expanded_target_list,
1343 const struct loader_layer_list *source_list) {
Jean-Francois Roybd7ceab2017-07-06 14:10:13 -07001344 bool enable = loader_is_implicit_layer_enabled(inst, prop);
Mark Youngf2079b92017-05-02 10:49:46 -06001345 if (enable) {
Mark Youngf2079b92017-05-02 10:49:46 -06001346 if (0 == (prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
Mark Youngc8b807a2017-07-14 17:11:31 -06001347 if (NULL != target_list && !has_vk_layer_property(&prop->info, target_list)) {
Mark Young283fe1c2017-05-04 12:16:35 -06001348 loader_add_to_layer_list(inst, target_list, 1, prop);
1349 }
1350 if (NULL != expanded_target_list && !has_vk_layer_property(&prop->info, expanded_target_list)) {
1351 loader_add_to_layer_list(inst, expanded_target_list, 1, prop);
1352 }
Mark Youngf2079b92017-05-02 10:49:46 -06001353 } else {
Mark Young283fe1c2017-05-04 12:16:35 -06001354 if (!has_vk_layer_property(&prop->info, target_list) ||
1355 (NULL != expanded_target_list && !has_vk_layer_property(&prop->info, expanded_target_list))) {
1356 loader_add_meta_layer(inst, prop, target_list, expanded_target_list, source_list);
1357 }
Mark Youngf2079b92017-05-02 10:49:46 -06001358 }
1359 }
1360}
1361
1362// Add the component layers of a meta-layer to the active list of layers
1363bool loader_add_meta_layer(const struct loader_instance *inst, const struct loader_layer_properties *prop,
Mark Young283fe1c2017-05-04 12:16:35 -06001364 struct loader_layer_list *target_list, struct loader_layer_list *expanded_target_list,
1365 const struct loader_layer_list *source_list) {
Mark Youngf2079b92017-05-02 10:49:46 -06001366 bool found = true;
1367
1368 // We need to add all the individual component layers
1369 for (uint32_t comp_layer = 0; comp_layer < prop->num_component_layers; comp_layer++) {
1370 bool found_comp = false;
1371 const struct loader_layer_properties *search_prop =
1372 loader_get_layer_property(prop->component_layer_names[comp_layer], source_list);
1373 if (search_prop != NULL) {
1374 found_comp = true;
Mark Young283fe1c2017-05-04 12:16:35 -06001375
1376 // If the component layer is itself an implicit layer, we need to do the implicit layer enable
1377 // checks
Mark Youngf2079b92017-05-02 10:49:46 -06001378 if (0 == (search_prop->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER)) {
Mark Young283fe1c2017-05-04 12:16:35 -06001379 loader_add_implicit_layer(inst, search_prop, target_list, expanded_target_list, source_list);
Mark Youngf2079b92017-05-02 10:49:46 -06001380 } else {
Mark Young283fe1c2017-05-04 12:16:35 -06001381 if (NULL != expanded_target_list && !has_vk_layer_property(&search_prop->info, expanded_target_list)) {
1382 loader_add_to_layer_list(inst, expanded_target_list, 1, search_prop);
1383 }
Mark Youngf2079b92017-05-02 10:49:46 -06001384 }
1385 }
1386 if (!found_comp) {
1387 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
1388 "loader_add_meta_layer: Failed to find layer name %s component layer "
1389 "%s to activate",
1390 search_prop->info.layerName, prop->component_layer_names[comp_layer]);
1391 found = false;
1392 }
1393 }
Mark Youngc8b807a2017-07-14 17:11:31 -06001394
1395 // Add this layer to the overall target list (not the expanded one)
1396 if (found && !has_vk_layer_property(&prop->info, target_list)) {
1397 loader_add_to_layer_list(inst, target_list, 1, prop);
1398 }
1399
Mark Youngf2079b92017-05-02 10:49:46 -06001400 return found;
1401}
1402
1403// Search the source_list for any layer with a name that matches the given name and a type
1404// that matches the given type. Add all matching layers to the target_list.
1405// Do not add if found loader_layer_properties is already on the target_list.
1406void loader_find_layer_name_add_list(const struct loader_instance *inst, const char *name, const enum layer_type_flags type_flags,
Mark Young283fe1c2017-05-04 12:16:35 -06001407 const struct loader_layer_list *source_list, struct loader_layer_list *target_list,
1408 struct loader_layer_list *expanded_target_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001409 bool found = false;
Mark Youngf2079b92017-05-02 10:49:46 -06001410 for (uint32_t i = 0; i < source_list->count; i++) {
1411 struct loader_layer_properties *source_prop = &source_list->list[i];
1412 if (0 == strcmp(source_prop->info.layerName, name) && (source_prop->type_flags & type_flags) == type_flags) {
Mark Youngf2079b92017-05-02 10:49:46 -06001413 // If not a meta-layer, simply add it.
1414 if (0 == (source_prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
Mark Youngc8b807a2017-07-14 17:11:31 -06001415 if (NULL != target_list && !has_vk_layer_property(&source_prop->info, target_list) &&
Mark Young283fe1c2017-05-04 12:16:35 -06001416 VK_SUCCESS == loader_add_to_layer_list(inst, target_list, 1, source_prop)) {
1417 found = true;
1418 }
Mark Youngc8b807a2017-07-14 17:11:31 -06001419 if (NULL != expanded_target_list && !has_vk_layer_property(&source_prop->info, expanded_target_list) &&
Mark Young283fe1c2017-05-04 12:16:35 -06001420 VK_SUCCESS == loader_add_to_layer_list(inst, expanded_target_list, 1, source_prop)) {
Mark Youngf2079b92017-05-02 10:49:46 -06001421 found = true;
1422 }
1423 } else {
Mark Young283fe1c2017-05-04 12:16:35 -06001424 found = loader_add_meta_layer(inst, source_prop, target_list, expanded_target_list, source_list);
Mark Young0ad83132016-06-30 13:02:42 -06001425 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001426 }
1427 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001428 if (!found) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001429 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Youngf2079b92017-05-02 10:49:46 -06001430 "loader_find_layer_name_add_list: Failed to find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001431 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001432}
1433
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001434static VkExtensionProperties *get_extension_property(const char *name, const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001435 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001436 if (strcmp(name, list->list[i].extensionName) == 0) return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001437 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001438 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001439}
1440
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001441static VkExtensionProperties *get_dev_extension_property(const char *name, const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001442 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001443 if (strcmp(name, list->list[i].props.extensionName) == 0) return &list->list[i].props;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001444 }
1445 return NULL;
1446}
1447
Mark Young0f183a82017-02-28 09:58:04 -07001448// For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
1449// the extension must provide two entry points for the loader to use:
1450// - "trampoline" entry point - this is the address returned by GetProcAddr
1451// and will always do what's necessary to support a
1452// global call.
1453// - "terminator" function - this function will be put at the end of the
1454// instance chain and will contain the necessary logic
1455// to call / process the extension for the appropriate
1456// ICDs that are available.
1457// There is no generic mechanism for including these functions, the references
1458// must be placed into the appropriate loader entry points.
1459// GetInstanceProcAddr: call extension GetInstanceProcAddr to check for GetProcAddr
1460// requests
1461// loader_coalesce_extensions(void) - add extension records to the list of global
1462// extension available to the app.
1463// instance_disp - add function pointer for terminator function
1464// to this array.
1465// The extension itself should be in a separate file that will be linked directly
1466// with the loader.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001467VkResult loader_get_icd_loader_instance_extensions(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
1468 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001469 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001470 VkResult res = VK_SUCCESS;
Mark Young2b2ece72017-02-10 11:19:02 -07001471 char *env_value;
1472 bool filter_extensions = true;
Mark Young3a587792016-08-19 15:25:08 -06001473
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001474 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001475
Mark Young2b2ece72017-02-10 11:19:02 -07001476 // Check if a user wants to disable the instance extension filtering behavior
1477 env_value = loader_getenv("VK_LOADER_DISABLE_INST_EXT_FILTER", inst);
1478 if (NULL != env_value && atoi(env_value) != 0) {
1479 filter_extensions = false;
1480 }
1481 loader_free_getenv(env_value, inst);
1482
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001483 // traverse scanned icd list adding non-duplicate extensions to the list
Mark Young0153e0b2016-11-03 14:27:13 -06001484 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001485 res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06001486 if (VK_SUCCESS != res) {
1487 goto out;
1488 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001489 res = loader_add_instance_extensions(inst, icd_tramp_list->scanned_list[i].EnumerateInstanceExtensionProperties,
1490 icd_tramp_list->scanned_list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001491 if (VK_SUCCESS == res) {
Mark Young2b2ece72017-02-10 11:19:02 -07001492 if (filter_extensions) {
1493 // Remove any extensions not recognized by the loader
1494 for (int32_t j = 0; j < (int32_t)icd_exts.count; j++) {
1495 // See if the extension is in the list of supported extensions
1496 bool found = false;
1497 for (uint32_t k = 0; LOADER_INSTANCE_EXTENSIONS[k] != NULL; k++) {
1498 if (strcmp(icd_exts.list[j].extensionName, LOADER_INSTANCE_EXTENSIONS[k]) == 0) {
1499 found = true;
1500 break;
1501 }
Lenny Komow4053b812016-12-29 16:27:28 -07001502 }
Lenny Komow4053b812016-12-29 16:27:28 -07001503
Mark Young2b2ece72017-02-10 11:19:02 -07001504 // If it isn't in the list, remove it
1505 if (!found) {
1506 for (uint32_t k = j + 1; k < icd_exts.count; k++) {
1507 icd_exts.list[k - 1] = icd_exts.list[k];
1508 }
1509 --icd_exts.count;
1510 --j;
Lenny Komow4053b812016-12-29 16:27:28 -07001511 }
Lenny Komow4053b812016-12-29 16:27:28 -07001512 }
1513 }
1514
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001515 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count, icd_exts.list);
Mark Young3a587792016-08-19 15:25:08 -06001516 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001517 loader_destroy_generic_list(inst, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001518 if (VK_SUCCESS != res) {
1519 goto out;
1520 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001521 };
1522
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001523 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001524 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001525
1526out:
1527 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001528}
1529
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001530struct loader_icd_term *loader_get_icd_and_device(const VkDevice device, struct loader_device **found_dev, uint32_t *icd_index) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001531 *found_dev = NULL;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001532 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
Lenny Komow27167312017-03-31 13:43:35 -06001533 uint32_t index = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001534 for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
1535 for (struct loader_device *dev = icd_term->logical_device_list; dev; dev = dev->next)
Mark Young65cb3662016-11-07 13:27:02 -07001536 // Value comparison of device prevents object wrapping by layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001537 if (loader_get_dispatch(dev->icd_device) == loader_get_dispatch(device) ||
1538 loader_get_dispatch(dev->chain_device) == loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001539 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001540 if (NULL != icd_index) {
1541 *icd_index = index;
1542 }
Mark Young0153e0b2016-11-03 14:27:13 -06001543 return icd_term;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001544 }
Mark Young16573c72016-06-28 10:52:43 -06001545 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001546 }
1547 }
1548 return NULL;
1549}
1550
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001551void loader_destroy_logical_device(const struct loader_instance *inst, struct loader_device *dev,
Mark Young0ad83132016-06-30 13:02:42 -06001552 const VkAllocationCallbacks *pAllocator) {
1553 if (pAllocator) {
1554 dev->alloc_callbacks = *pAllocator;
1555 }
Mark Young283fe1c2017-05-04 12:16:35 -06001556 if (NULL != dev->expanded_activated_layer_list.list) {
1557 loader_deactivate_layers(inst, dev, &dev->expanded_activated_layer_list);
1558 }
1559 if (NULL != dev->app_activated_layer_list.list) {
1560 loader_destroy_layer_list(inst, dev, &dev->app_activated_layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06001561 }
1562 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001563}
1564
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001565struct loader_device *loader_create_logical_device(const struct loader_instance *inst, const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001566 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001567#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1568 {
1569#else
1570 if (pAllocator) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001571 new_dev = (struct loader_device *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(struct loader_device),
1572 sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Mark Young0ad83132016-06-30 13:02:42 -06001573 } else {
1574#endif
1575 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1576 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001577
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001578 if (!new_dev) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001579 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1580 "loader_create_logical_device: Failed to alloc struct "
1581 "loader_device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001582 return NULL;
1583 }
1584
1585 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001586 if (pAllocator) {
1587 new_dev->alloc_callbacks = *pAllocator;
1588 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001589
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001590 return new_dev;
1591}
1592
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001593void loader_add_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term, struct loader_device *dev) {
Mark Young0153e0b2016-11-03 14:27:13 -06001594 dev->next = icd_term->logical_device_list;
1595 icd_term->logical_device_list = dev;
Piers Daniell295fe402016-03-29 11:51:11 -06001596}
1597
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001598void loader_remove_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term,
1599 struct loader_device *found_dev, const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001600 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001601
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001602 if (!icd_term || !found_dev) return;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001603
1604 prev_dev = NULL;
Mark Young0153e0b2016-11-03 14:27:13 -06001605 dev = icd_term->logical_device_list;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001606 while (dev && dev != found_dev) {
1607 prev_dev = dev;
1608 dev = dev->next;
1609 }
1610
1611 if (prev_dev)
1612 prev_dev->next = found_dev->next;
1613 else
Mark Young0153e0b2016-11-03 14:27:13 -06001614 icd_term->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001615 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001616}
1617
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001618static void loader_icd_destroy(struct loader_instance *ptr_inst, struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001619 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001620 ptr_inst->total_icd_count--;
Mark Young0153e0b2016-11-03 14:27:13 -06001621 for (struct loader_device *dev = icd_term->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001622 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001623 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001624 dev = next_dev;
1625 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001626
Mark Young0153e0b2016-11-03 14:27:13 -06001627 loader_instance_heap_free(ptr_inst, icd_term);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001628}
1629
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001630static struct loader_icd_term *loader_icd_create(const struct loader_instance *inst) {
Mark Young0153e0b2016-11-03 14:27:13 -06001631 struct loader_icd_term *icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001632
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001633 icd_term = loader_instance_heap_alloc(inst, sizeof(struct loader_icd_term), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001634 if (!icd_term) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001635 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001636 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001637
Mark Young0153e0b2016-11-03 14:27:13 -06001638 memset(icd_term, 0, sizeof(struct loader_icd_term));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001639
Mark Young0153e0b2016-11-03 14:27:13 -06001640 return icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001641}
1642
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001643static struct loader_icd_term *loader_icd_add(struct loader_instance *ptr_inst, const struct loader_scanned_icd *scanned_icd) {
Mark Young0153e0b2016-11-03 14:27:13 -06001644 struct loader_icd_term *icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001645
Mark Young0153e0b2016-11-03 14:27:13 -06001646 icd_term = loader_icd_create(ptr_inst);
1647 if (!icd_term) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001648 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001649 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001650
Mark Young0153e0b2016-11-03 14:27:13 -06001651 icd_term->scanned_icd = scanned_icd;
1652 icd_term->this_instance = ptr_inst;
Jon Ashburn3d002332015-08-20 16:35:30 -06001653
Mark Young0f183a82017-02-28 09:58:04 -07001654 // Prepend to the list
Mark Young0153e0b2016-11-03 14:27:13 -06001655 icd_term->next = ptr_inst->icd_terms;
1656 ptr_inst->icd_terms = icd_term;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001657 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001658
Mark Young0153e0b2016-11-03 14:27:13 -06001659 return icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001660}
Mark Young0153e0b2016-11-03 14:27:13 -06001661
Mark Young0f183a82017-02-28 09:58:04 -07001662// Determine the ICD interface version to use.
1663// @param icd
1664// @param pVersion Output parameter indicating which version to use or 0 if
1665// the negotiation API is not supported by the ICD
1666// @return bool indicating true if the selected interface version is supported
1667// by the loader, false indicates the version is not supported
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001668bool loader_get_icd_interface_version(PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version, uint32_t *pVersion) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001669 if (fp_negotiate_icd_version == NULL) {
1670 // ICD does not support the negotiation API, it supports version 0 or 1
1671 // calling code must determine if it is version 0 or 1
1672 *pVersion = 0;
1673 } else {
1674 // ICD supports the negotiation API, so call it with the loader's
1675 // latest version supported
1676 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1677 VkResult result = fp_negotiate_icd_version(pVersion);
1678
1679 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1680 // ICD no longer supports the loader's latest interface version so
1681 // fail loading the ICD
1682 return false;
1683 }
1684 }
1685
1686#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1687 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1688 // Loader no longer supports the ICD's latest interface version so fail
1689 // loading the ICD
1690 return false;
1691 }
1692#endif
1693 return true;
1694}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001695
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001696void loader_scanned_icd_clear(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Mark Youngf2079b92017-05-02 10:49:46 -06001697 if (0 != icd_tramp_list->capacity) {
1698 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
1699 loader_platform_close_library(icd_tramp_list->scanned_list[i].handle);
1700 loader_instance_heap_free(inst, icd_tramp_list->scanned_list[i].lib_name);
1701 }
1702 loader_instance_heap_free(inst, icd_tramp_list->scanned_list);
1703 icd_tramp_list->capacity = 0;
1704 icd_tramp_list->count = 0;
1705 icd_tramp_list->scanned_list = NULL;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001706 }
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001707}
1708
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001709static VkResult loader_scanned_icd_init(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001710 VkResult err = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06001711 loader_scanned_icd_clear(inst, icd_tramp_list);
1712 icd_tramp_list->capacity = 8 * sizeof(struct loader_scanned_icd);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001713 icd_tramp_list->scanned_list = loader_instance_heap_alloc(inst, icd_tramp_list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001714 if (NULL == icd_tramp_list->scanned_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001715 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1716 "loader_scanned_icd_init: Realloc failed for layer list when "
1717 "attempting to add new layer");
Mark Young0ad83132016-06-30 13:02:42 -06001718 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1719 }
1720 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001721}
1722
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001723static VkResult loader_scanned_icd_add(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
1724 const char *filename, uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001725 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001726 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001727 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001728 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Mark Young39389872017-01-19 21:10:49 -07001729 PFN_GetPhysicalDeviceProcAddr fp_get_phys_dev_proc_addr = NULL;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001730 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Mark Young0153e0b2016-11-03 14:27:13 -06001731 struct loader_scanned_icd *new_scanned_icd;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001732 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001733 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001734
Mark Young0f183a82017-02-28 09:58:04 -07001735 // TODO implement smarter opening/closing of libraries. For now this
1736 // function leaves libraries open and the scanned_icd_clear closes them
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001737 handle = loader_platform_open_library(filename);
Mark Youngb6399312017-01-10 14:22:15 -07001738 if (NULL == handle) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001739 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001740 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001741 }
1742
Jon Ashburn17b4c862016-04-25 11:09:37 -06001743 // Get and settle on an ICD interface version
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001744 fp_negotiate_icd_version = loader_platform_get_proc_address(handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001745
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001746 if (!loader_get_icd_interface_version(fp_negotiate_icd_version, &interface_vers)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001747 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1748 "loader_scanned_icd_add: ICD %s doesn't support interface"
1749 " version compatible with loader, skip this ICD.",
Mark Young0ad83132016-06-30 13:02:42 -06001750 filename);
Mark Young3a587792016-08-19 15:25:08 -06001751 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001752 }
1753
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001754 fp_get_proc_addr = loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Mark Youngb6399312017-01-10 14:22:15 -07001755 if (NULL == fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001756 assert(interface_vers == 0);
1757 // Use deprecated interface from version 0
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001758 fp_get_proc_addr = loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Mark Youngb6399312017-01-10 14:22:15 -07001759 if (NULL == fp_get_proc_addr) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001760 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngdee312c2017-03-08 13:38:35 -07001761 "loader_scanned_icd_add: Attempt to retrieve either "
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001762 "\'vkGetInstanceProcAddr\' or "
1763 "\'vk_icdGetInstanceProcAddr\' from ICD %s failed.",
Mark Youngb6399312017-01-10 14:22:15 -07001764 filename);
Mark Young3a587792016-08-19 15:25:08 -06001765 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001766 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001767 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
1768 "loader_scanned_icd_add: Using deprecated ICD "
1769 "interface of \'vkGetInstanceProcAddr\' instead of "
1770 "\'vk_icdGetInstanceProcAddr\' for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001771 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001772 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001773 fp_create_inst = loader_platform_get_proc_address(handle, "vkCreateInstance");
Mark Youngb6399312017-01-10 14:22:15 -07001774 if (NULL == fp_create_inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001775 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1776 "loader_scanned_icd_add: Failed querying "
1777 "\'vkCreateInstance\' via dlsym/loadlibrary for "
1778 "ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001779 filename);
Mark Young3a587792016-08-19 15:25:08 -06001780 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001781 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001782 fp_get_inst_ext_props = loader_platform_get_proc_address(handle, "vkEnumerateInstanceExtensionProperties");
Mark Youngb6399312017-01-10 14:22:15 -07001783 if (NULL == fp_get_inst_ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001784 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1785 "loader_scanned_icd_add: Could not get \'vkEnumerate"
1786 "InstanceExtensionProperties\' via dlsym/loadlibrary "
1787 "for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001788 filename);
Mark Young3a587792016-08-19 15:25:08 -06001789 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001790 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001791 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001792 // Use newer interface version 1 or later
Mark Young39389872017-01-19 21:10:49 -07001793 if (interface_vers == 0) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001794 interface_vers = 1;
Mark Young39389872017-01-19 21:10:49 -07001795 }
Jon Ashburn17b4c862016-04-25 11:09:37 -06001796
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001797 fp_create_inst = (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Mark Youngb6399312017-01-10 14:22:15 -07001798 if (NULL == fp_create_inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001799 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1800 "loader_scanned_icd_add: Could not get "
1801 "\'vkCreateInstance\' via \'vk_icdGetInstanceProcAddr\'"
1802 " for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001803 filename);
Mark Young3a587792016-08-19 15:25:08 -06001804 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001805 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001806 fp_get_inst_ext_props =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001807 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(NULL, "vkEnumerateInstanceExtensionProperties");
Mark Youngb6399312017-01-10 14:22:15 -07001808 if (NULL == fp_get_inst_ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001809 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1810 "loader_scanned_icd_add: Could not get \'vkEnumerate"
1811 "InstanceExtensionProperties\' via "
1812 "\'vk_icdGetInstanceProcAddr\' for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001813 filename);
Mark Young3a587792016-08-19 15:25:08 -06001814 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001815 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001816 fp_get_phys_dev_proc_addr = loader_platform_get_proc_address(handle, "vk_icdGetPhysicalDeviceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001817 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001818
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001819 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001820 if ((icd_tramp_list->count * sizeof(struct loader_scanned_icd)) >= icd_tramp_list->capacity) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06001821 void *new_ptr = loader_instance_heap_realloc(inst, icd_tramp_list->scanned_list, icd_tramp_list->capacity,
1822 icd_tramp_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1823 if (NULL == new_ptr) {
Mark Young3a587792016-08-19 15:25:08 -06001824 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001825 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngbb3a29c2017-05-19 12:29:43 -06001826 "loader_scanned_icd_add: Realloc failed on icd library list for ICD %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001827 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001828 }
Mark Youngbb3a29c2017-05-19 12:29:43 -06001829 icd_tramp_list->scanned_list = new_ptr;
1830
Jon Ashburn23d36b12016-02-02 17:47:28 -07001831 // double capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001832 icd_tramp_list->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001833 }
1834
Mark Young39389872017-01-19 21:10:49 -07001835 new_scanned_icd = &(icd_tramp_list->scanned_list[icd_tramp_list->count]);
Mark Young0153e0b2016-11-03 14:27:13 -06001836 new_scanned_icd->handle = handle;
1837 new_scanned_icd->api_version = api_version;
1838 new_scanned_icd->GetInstanceProcAddr = fp_get_proc_addr;
Mark Young39389872017-01-19 21:10:49 -07001839 new_scanned_icd->GetPhysicalDeviceProcAddr = fp_get_phys_dev_proc_addr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001840 new_scanned_icd->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Mark Young0153e0b2016-11-03 14:27:13 -06001841 new_scanned_icd->CreateInstance = fp_create_inst;
1842 new_scanned_icd->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001843
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001844 new_scanned_icd->lib_name = (char *)loader_instance_heap_alloc(inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001845 if (NULL == new_scanned_icd->lib_name) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001846 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_scanned_icd_add: Out of memory can't add ICD %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001847 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001848 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001849 }
Mark Young0153e0b2016-11-03 14:27:13 -06001850 strcpy(new_scanned_icd->lib_name, filename);
1851 icd_tramp_list->count++;
Mark Young3a587792016-08-19 15:25:08 -06001852
1853out:
1854
1855 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001856}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001857
Jon Ashburn23d36b12016-02-02 17:47:28 -07001858static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001859 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001860
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001861 if (g_loader_debug > 0) return;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001862
1863 g_loader_debug = 0;
1864
Mark Young0f183a82017-02-28 09:58:04 -07001865 // Parse comma-separated debug options
Mark Young0ad83132016-06-30 13:02:42 -06001866 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001867 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001868 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001869 size_t len;
1870
1871 if (p)
1872 len = p - env;
1873 else
1874 len = strlen(env);
1875
1876 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001877 if (strncmp(env, "all", len) == 0) {
1878 g_loader_debug = ~0u;
1879 g_loader_log_msgs = ~0u;
1880 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001881 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001882 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001883 } else if (strncmp(env, "info", len) == 0) {
1884 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001885 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001886 } else if (strncmp(env, "perf", len) == 0) {
1887 g_loader_debug |= LOADER_PERF_BIT;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001888 g_loader_log_msgs |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001889 } else if (strncmp(env, "error", len) == 0) {
1890 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001891 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001892 } else if (strncmp(env, "debug", len) == 0) {
1893 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001894 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001895 }
1896 }
1897
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001898 if (!p) break;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001899
1900 env = p + 1;
1901 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001902
Mark Young0ad83132016-06-30 13:02:42 -06001903 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001904}
1905
Jon Ashburn23d36b12016-02-02 17:47:28 -07001906void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001907 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001908 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001909 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001910
1911 // initialize logging
1912 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001913
1914 // initial cJSON to use alloc callbacks
1915 cJSON_Hooks alloc_fns = {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001916 .malloc_fn = loader_instance_tls_heap_alloc, .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001917 };
1918 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001919}
1920
Jon Ashburn2077e382015-06-29 11:25:34 -06001921struct loader_manifest_files {
1922 uint32_t count;
1923 char **filename_list;
1924};
1925
Mark Young0f183a82017-02-28 09:58:04 -07001926// Get next file or dirname given a string list or registry key path
1927//
1928// \returns
1929// A pointer to first char in the next path.
1930// The next path (or NULL) in the list is returned in next_path.
1931// Note: input string is modified in some cases. PASS IN A COPY!
Jon Ashburn23d36b12016-02-02 17:47:28 -07001932static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001933 uint32_t len;
1934 char *next;
1935
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001936 if (path == NULL) return NULL;
Frank Henigman57173102016-11-24 22:15:20 -05001937 next = strchr(path, PATH_SEPARATOR);
Jon Ashburn2077e382015-06-29 11:25:34 -06001938 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001939 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001940 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001941 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001942 *next = '\0';
1943 next++;
1944 }
1945
1946 return next;
1947}
1948
Mark Young0f183a82017-02-28 09:58:04 -07001949// Given a path which is absolute or relative, expand the path if relative or
1950// leave the path unmodified if absolute. The base path to prepend to relative
1951// paths is given in rel_base.
1952//
1953// @return - A string in out_fullpath of the full absolute path
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001954static void loader_expand_path(const char *path, const char *rel_base, size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001955 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001956 // do not prepend a base to an absolute path
1957 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001958 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001959
1960 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001961}
1962
Mark Young0f183a82017-02-28 09:58:04 -07001963// Given a filename (file) and a list of paths (dir), try to find an existing
1964// file in the paths. If filename already is a path then no searching in the given paths.
1965//
1966// @return - A string in out_fullpath of either the full path or file.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001967static void loader_get_fullpath(const char *file, const char *dirs, size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001968 if (!loader_platform_is_path(file) && *dirs) {
1969 char *dirs_copy, *dir, *next_dir;
1970
1971 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1972 strcpy(dirs_copy, dirs);
1973
Jon Ashburn23d36b12016-02-02 17:47:28 -07001974 // find if file exists after prepending paths in given list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001975 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir)); dir = next_dir) {
1976 loader_platform_combine_path(out_fullpath, out_size, dir, file, NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001977 if (loader_platform_file_exists(out_fullpath)) {
1978 return;
1979 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001980 }
1981 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001982
Karl Schultze2ef9e62017-01-13 14:01:35 -07001983 (void)snprintf(out_fullpath, out_size, "%s", file);
Jon Ashburn2077e382015-06-29 11:25:34 -06001984}
1985
Mark Young0f183a82017-02-28 09:58:04 -07001986// Read a JSON file into a buffer.
1987//
1988// @return - A pointer to a cJSON object representing the JSON parse tree.
1989// This returned buffer should be freed by caller.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001990static VkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json) {
Mark Young3a587792016-08-19 15:25:08 -06001991 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001992 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001993 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001994 VkResult res = VK_SUCCESS;
1995
1996 if (NULL == json) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001997 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Received invalid JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001998 res = VK_ERROR_INITIALIZATION_FAILED;
1999 goto out;
2000 }
2001
2002 *json = NULL;
2003
Jon Ashburn23d36b12016-02-02 17:47:28 -07002004 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002005 if (!file) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002006 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Failed to open JSON file %s", filename);
Mark Youngb6399312017-01-10 14:22:15 -07002007 res = VK_ERROR_INITIALIZATION_FAILED;
Mark Young3a587792016-08-19 15:25:08 -06002008 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002009 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002010 fseek(file, 0, SEEK_END);
2011 len = ftell(file);
2012 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002013 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002014 if (json_buf == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002015 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2016 "loader_get_json: Failed to allocate space for "
2017 "JSON file %s buffer of length %d",
Mark Youngb6399312017-01-10 14:22:15 -07002018 filename, len);
2019 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06002020 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002021 }
2022 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002023 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Failed to read JSON file %s.", filename);
Mark Youngb6399312017-01-10 14:22:15 -07002024 res = VK_ERROR_INITIALIZATION_FAILED;
Mark Young3a587792016-08-19 15:25:08 -06002025 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002026 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002027 json_buf[len] = '\0';
2028
Mark Young0f183a82017-02-28 09:58:04 -07002029 // Parse text from file
Mark Young3a587792016-08-19 15:25:08 -06002030 *json = cJSON_Parse(json_buf);
2031 if (*json == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002032 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2033 "loader_get_json: Failed to parse JSON file %s, "
2034 "this is usually because something ran out of "
2035 "memory.",
Mark Youngb6399312017-01-10 14:22:15 -07002036 filename);
2037 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06002038 goto out;
2039 }
2040
2041out:
2042 if (NULL != file) {
2043 fclose(file);
2044 }
2045
2046 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002047}
2048
Mark Young0f183a82017-02-28 09:58:04 -07002049// Do a deep copy of the loader_layer_properties structure.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002050VkResult loader_copy_layer_properties(const struct loader_instance *inst, struct loader_layer_properties *dst,
Mark Young0ad83132016-06-30 13:02:42 -06002051 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07002052 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002053 memcpy(dst, src, sizeof(*src));
Mark Youngb6399312017-01-10 14:22:15 -07002054 dst->instance_extension_list.list = loader_instance_heap_alloc(
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002055 inst, sizeof(VkExtensionProperties) * src->instance_extension_list.count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06002056 if (NULL == dst->instance_extension_list.list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002057 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2058 "loader_copy_layer_properties: Failed to allocate space "
2059 "for instance extension list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07002060 src->instance_extension_list.count);
Mark Young0ad83132016-06-30 13:02:42 -06002061 return VK_ERROR_OUT_OF_HOST_MEMORY;
2062 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002063 dst->instance_extension_list.capacity = sizeof(VkExtensionProperties) * src->instance_extension_list.count;
2064 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list, dst->instance_extension_list.capacity);
Mark Youngb6399312017-01-10 14:22:15 -07002065 dst->device_extension_list.list = loader_instance_heap_alloc(
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002066 inst, sizeof(struct loader_dev_ext_props) * src->device_extension_list.count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06002067 if (NULL == dst->device_extension_list.list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002068 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2069 "loader_copy_layer_properties: Failed to allocate space "
2070 "for device extension list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07002071 src->device_extension_list.count);
Mark Young0ad83132016-06-30 13:02:42 -06002072 return VK_ERROR_OUT_OF_HOST_MEMORY;
2073 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002074 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) * src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07002075
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002076 dst->device_extension_list.capacity = sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
2077 memcpy(dst->device_extension_list.list, src->device_extension_list.list, dst->device_extension_list.capacity);
2078 if (src->device_extension_list.count > 0 && src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07002079 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06002080 dst->device_extension_list.list->entrypoints =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002081 loader_instance_heap_alloc(inst, sizeof(char *) * cnt, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06002082 if (NULL == dst->device_extension_list.list->entrypoints) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002083 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2084 "loader_copy_layer_properties: Failed to allocate space "
2085 "for device extension entrypoint list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07002086 cnt);
Mark Young0ad83132016-06-30 13:02:42 -06002087 return VK_ERROR_OUT_OF_HOST_MEMORY;
2088 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002089 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
Mark Young0ad83132016-06-30 13:02:42 -06002090
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07002091 for (i = 0; i < cnt; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002092 dst->device_extension_list.list->entrypoints[i] = loader_instance_heap_alloc(
2093 inst, strlen(src->device_extension_list.list->entrypoints[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06002094 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002095 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2096 "loader_copy_layer_properties: Failed to "
2097 "allocate space for device extension entrypoint "
2098 "%d name of length",
Mark Youngb6399312017-01-10 14:22:15 -07002099 i);
Mark Young0ad83132016-06-30 13:02:42 -06002100 return VK_ERROR_OUT_OF_HOST_MEMORY;
2101 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002102 strcpy(dst->device_extension_list.list->entrypoints[i], src->device_extension_list.list->entrypoints[i]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07002103 }
2104 }
Mark Young0ad83132016-06-30 13:02:42 -06002105
2106 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06002107}
2108
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002109static bool loader_find_layer_name_list(const char *name, const struct loader_layer_list *layer_list) {
Mark Youngf2079b92017-05-02 10:49:46 -06002110 if (NULL == layer_list) {
2111 return false;
2112 }
2113 for (uint32_t j = 0; j < layer_list->count; j++) {
2114 if (!strcmp(name, layer_list->list[j].info.layerName)) {
2115 return true;
2116 }
2117 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07002118 return false;
2119}
2120
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002121bool loader_find_layer_name_array(const char *name, uint32_t layer_count, const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002122 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002123 for (uint32_t j = 0; j < layer_count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002124 if (!strcmp(name, layer_list[j])) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002125 return false;
2126}
2127
Mark Youngf2079b92017-05-02 10:49:46 -06002128const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
Jon Ashburn86a527a2016-02-10 20:59:26 -07002129
Mark Youngf2079b92017-05-02 10:49:46 -06002130// Adds the legacy VK_LAYER_LUNARG_standard_validation as a meta-layer if it
2131// fails to find it in the list already. This is usually an indication that a
2132// newer loader is being used with an older layer set.
2133static bool loader_add_legacy_std_val_layer(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list) {
2134 uint32_t i;
2135 bool success = true;
2136 struct loader_layer_properties *props = loader_get_next_layer_property(inst, layer_instance_list);
2137 const char std_validation_names[6][VK_MAX_EXTENSION_NAME_SIZE] = {
Mark Youngc8b807a2017-07-14 17:11:31 -06002138 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker",
2139 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects"};
Mark Youngf2079b92017-05-02 10:49:46 -06002140 uint32_t layer_count = sizeof(std_validation_names) / sizeof(std_validation_names[0]);
2141
2142 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2143 "Adding VK_LAYER_LUNARG_standard_validation using the loader legacy path. This is"
2144 " not an error.");
2145
2146 if (NULL == props) {
2147 goto out;
Jon Ashburn491cd042016-05-16 14:01:18 -06002148 }
Jon Ashburn71483442016-02-11 18:59:43 -07002149
Jon Ashburn491cd042016-05-16 14:01:18 -06002150 memset(props, 0, sizeof(struct loader_layer_properties));
Mark Youngf2079b92017-05-02 10:49:46 -06002151 props->type_flags = VK_LAYER_TYPE_FLAG_INSTANCE_LAYER | VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER | VK_LAYER_TYPE_FLAG_META_LAYER;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002152 strncpy(props->info.description, "LunarG Standard Validation Layer", sizeof(props->info.description));
Jon Ashburn491cd042016-05-16 14:01:18 -06002153 props->info.implementationVersion = 1;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002154 strncpy(props->info.layerName, std_validation_str, sizeof(props->info.layerName));
Jon Ashburn491cd042016-05-16 14:01:18 -06002155 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
Jon Ashburn491cd042016-05-16 14:01:18 -06002156
Mark Youngf2079b92017-05-02 10:49:46 -06002157 props->component_layer_names =
2158 loader_instance_heap_alloc(inst, sizeof(char[MAX_STRING_SIZE]) * layer_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
2159 if (NULL == props->component_layer_names) {
2160 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2161 "Failed to allocate space for legacy VK_LAYER_LUNARG_standard_validation"
2162 " meta-layer component_layers information.");
2163 success = false;
2164 goto out;
2165 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002166 for (i = 0; i < layer_count; i++) {
Mark Youngf2079b92017-05-02 10:49:46 -06002167 strncpy(props->component_layer_names[i], std_validation_names[i], MAX_STRING_SIZE - 1);
2168 props->component_layer_names[i][MAX_STRING_SIZE - 1] = '\0';
Jon Ashburn86a527a2016-02-10 20:59:26 -07002169 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002170
Mark Youngf2079b92017-05-02 10:49:46 -06002171out:
2172
2173 if (!success && NULL != props && NULL != props->component_layer_names) {
2174 loader_instance_heap_free(inst, props->component_layer_names);
2175 props->component_layer_names = NULL;
2176 }
2177
2178 return success;
2179}
2180
2181// Verify that all component layers in a meta-layer are valid.
2182static bool verify_meta_layer_comp_layers(const struct loader_instance *inst, struct loader_layer_properties *prop,
2183 struct loader_layer_list *instance_layers) {
2184 bool success = true;
Mark Youngc82a0622017-05-05 11:17:17 -06002185 const uint32_t expected_major = VK_VERSION_MAJOR(prop->info.specVersion);
2186 const uint32_t expected_minor = VK_VERSION_MINOR(prop->info.specVersion);
Mark Youngf2079b92017-05-02 10:49:46 -06002187
2188 for (uint32_t comp_layer = 0; comp_layer < prop->num_component_layers; comp_layer++) {
2189 if (!loader_find_layer_name_list(prop->component_layer_names[comp_layer], instance_layers)) {
2190 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2191 "Meta-layer %s can't find component layer %s at index %d."
2192 " Skipping this layer.",
2193 prop->info.layerName, prop->component_layer_names[comp_layer], comp_layer);
2194 success = false;
2195 break;
2196 } else {
2197 struct loader_layer_properties *comp_prop =
2198 loader_get_layer_property(prop->component_layer_names[comp_layer], instance_layers);
2199 if (comp_prop == NULL) {
2200 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2201 "Meta-layer %s can't find property for component layer %s at index %d."
2202 " Skipping this layer.",
2203 prop->info.layerName, prop->component_layer_names[comp_layer], comp_layer);
2204 success = false;
2205 break;
2206 }
2207
2208 // Check the version of each layer, they need to at least match MAJOR and MINOR
Mark Youngc82a0622017-05-05 11:17:17 -06002209 uint32_t cur_major = VK_VERSION_MAJOR(comp_prop->info.specVersion);
2210 uint32_t cur_minor = VK_VERSION_MINOR(comp_prop->info.specVersion);
2211 if (cur_major != expected_major || cur_minor != expected_minor) {
2212 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2213 "Meta-layer uses API version %d.%d, but component layer %d uses API "
2214 "version %d.%d. Skipping this layer.",
2215 expected_major, expected_minor, comp_layer, cur_major, cur_minor);
2216 success = false;
2217 break;
Mark Youngf2079b92017-05-02 10:49:46 -06002218 }
2219
2220 // Make sure the layer isn't using it's own name
2221 if (!strcmp(prop->info.layerName, prop->component_layer_names[comp_layer])) {
2222 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2223 "Meta-layer %s lists itself in its component layer list at index %d."
2224 " Skipping this layer.",
2225 prop->info.layerName, comp_layer);
2226 success = false;
2227 break;
2228 }
2229 if (comp_prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) {
2230 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2231 "verify_meta_layer_comp_layers: Adding meta-layer %s which also contains meta-layer %s",
2232 prop->info.layerName, comp_prop->info.layerName);
2233
2234 // Make sure if the layer is using a meta-layer in its component list that we also verify that.
2235 if (!verify_meta_layer_comp_layers(inst, comp_prop, instance_layers)) {
2236 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2237 "Meta-layer %s component layer %s can not find all component layers."
2238 " Skipping this layer.",
2239 prop->info.layerName, prop->component_layer_names[comp_layer]);
2240 success = false;
2241 break;
2242 }
2243 }
2244
2245 // Add any instance and device extensions from component layers to this layer
2246 // list, so that anyone querying extensions will only need to look at the meta-layer
2247 for (uint32_t ext = 0; ext < comp_prop->instance_extension_list.count; ext++) {
2248 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Meta-layer %s component layer %s adding instance extension %s",
2249 prop->info.layerName, prop->component_layer_names[comp_layer],
2250 comp_prop->instance_extension_list.list[ext].extensionName);
2251 if (!has_vk_extension_property(&comp_prop->instance_extension_list.list[ext], &prop->instance_extension_list)) {
2252 loader_add_to_ext_list(inst, &prop->instance_extension_list, 1, &comp_prop->instance_extension_list.list[ext]);
2253 }
2254 }
2255
2256 for (uint32_t ext = 0; ext < comp_prop->device_extension_list.count; ext++) {
2257 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Meta-layer %s component layer %s adding device extension %s",
2258 prop->info.layerName, prop->component_layer_names[comp_layer],
2259 comp_prop->device_extension_list.list[ext].props.extensionName);
2260 if (!has_vk_dev_ext_property(&comp_prop->device_extension_list.list[ext].props, &prop->device_extension_list)) {
2261 loader_add_to_dev_ext_list(inst, &prop->device_extension_list,
2262 &comp_prop->device_extension_list.list[ext].props, 0, NULL);
2263 }
2264 }
Mark Young0ad83132016-06-30 13:02:42 -06002265 }
Mark Youngf2079b92017-05-02 10:49:46 -06002266 }
2267 if (success) {
2268 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Meta-layer %s all %d component layers appear to be valid.",
2269 prop->info.layerName, prop->num_component_layers);
2270 }
2271 return success;
2272}
2273
2274// Verify that all meta-layers in a layer list are valid.
2275static void verify_all_meta_layers(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Mark Young697317f2017-05-09 10:31:12 -06002276 for (int32_t i = 0; i < (int32_t)instance_layers->count; i++) {
Mark Youngf2079b92017-05-02 10:49:46 -06002277 struct loader_layer_properties *prop = &instance_layers->list[i];
2278
2279 // If this is a meta-layer, make sure it is valid
2280 if ((prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) && !verify_meta_layer_comp_layers(inst, prop, instance_layers)) {
2281 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2282 "Removing meta-layer %s from instance layer list since it appears invalid.", prop->info.layerName);
2283
2284 // Delete the component layers
Mark Youngf2079b92017-05-02 10:49:46 -06002285 loader_instance_heap_free(inst, prop->component_layer_names);
Mark Youngf2079b92017-05-02 10:49:46 -06002286
Mark Young70ede4c2017-05-09 15:23:33 -06002287 // Remove the current invalid meta-layer from the layer list. Use memmove since we are
2288 // overlapping the source and destination addresses.
2289 memmove(&instance_layers->list[i], &instance_layers->list[i + 1],
2290 sizeof(struct loader_layer_properties) * (instance_layers->count - 1 - i));
Mark Youngf2079b92017-05-02 10:49:46 -06002291
Mark Young70ede4c2017-05-09 15:23:33 -06002292 // Decrement the count (because we now have one less) and decrement the loop index since we need to
2293 // re-check this index.
2294 instance_layers->count--;
Mark Young697317f2017-05-09 10:31:12 -06002295 i--;
Mark Youngf2079b92017-05-02 10:49:46 -06002296 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002297 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07002298}
2299
Mark Young39389872017-01-19 21:10:49 -07002300// This structure is used to store the json file version
Mark Youngdee312c2017-03-08 13:38:35 -07002301// in a more manageable way.
Mark Young39389872017-01-19 21:10:49 -07002302typedef struct {
2303 uint16_t major;
2304 uint16_t minor;
2305 uint16_t patch;
2306} layer_json_version;
2307
Mark Youngf2079b92017-05-02 10:49:46 -06002308static VkResult loader_read_json_layer(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list,
2309 cJSON *layer_node, layer_json_version version, cJSON *item, cJSON *disable_environment,
2310 bool is_implicit, char *filename) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002311 char *temp;
Mark Youngf2079b92017-05-02 10:49:46 -06002312 char *name, *type, *library_path_str, *api_version;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002313 char *implementation_version, *description;
Mark Youngf2079b92017-05-02 10:49:46 -06002314 cJSON *ext_item, *library_path, *component_layers;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002315 VkExtensionProperties ext_prop;
Mark Youngf2079b92017-05-02 10:49:46 -06002316 VkResult result = VK_ERROR_INITIALIZATION_FAILED;
2317 struct loader_layer_properties *props = NULL;
2318 int i, j;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002319
Mark Young0f183a82017-02-28 09:58:04 -07002320// The following are required in the "layer" object:
2321// (required) "name"
2322// (required) "type"
Nekotekinad431e132017-06-11 13:13:07 +03002323// (required) "library_path"
2324// (required) "api_version"
2325// (required) "implementation_version"
2326// (required) "description"
2327// (required for implicit layers) "disable_environment"
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002328#define GET_JSON_OBJECT(node, var) \
2329 { \
2330 var = cJSON_GetObjectItem(node, #var); \
2331 if (var == NULL) { \
2332 layer_node = layer_node->next; \
2333 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2334 "Didn't find required layer object %s in manifest " \
2335 "JSON file, skipping this layer", \
2336 #var); \
Mark Youngf2079b92017-05-02 10:49:46 -06002337 goto out; \
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002338 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002339 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002340#define GET_JSON_ITEM(node, var) \
2341 { \
2342 item = cJSON_GetObjectItem(node, #var); \
2343 if (item == NULL) { \
2344 layer_node = layer_node->next; \
2345 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2346 "Didn't find required layer value %s in manifest JSON " \
2347 "file, skipping this layer", \
2348 #var); \
Mark Youngf2079b92017-05-02 10:49:46 -06002349 goto out; \
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002350 } \
2351 temp = cJSON_Print(item); \
2352 if (temp == NULL) { \
2353 layer_node = layer_node->next; \
2354 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2355 "Problem accessing layer value %s in manifest JSON " \
2356 "file, skipping this layer", \
2357 #var); \
Mark Youngf2079b92017-05-02 10:49:46 -06002358 result = VK_ERROR_OUT_OF_HOST_MEMORY; \
2359 goto out; \
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002360 } \
2361 temp[strlen(temp) - 1] = '\0'; \
2362 var = loader_stack_alloc(strlen(temp) + 1); \
2363 strcpy(var, &temp[1]); \
2364 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002365 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002366 GET_JSON_ITEM(layer_node, name)
2367 GET_JSON_ITEM(layer_node, type)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002368 GET_JSON_ITEM(layer_node, api_version)
2369 GET_JSON_ITEM(layer_node, implementation_version)
2370 GET_JSON_ITEM(layer_node, description)
Mark Youngf2079b92017-05-02 10:49:46 -06002371
2372 // Add list entry
2373 if (!strcmp(type, "DEVICE")) {
2374 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "Device layers are deprecated skipping this layer");
2375 layer_node = layer_node->next;
2376 goto out;
2377 }
2378
2379 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2380 // layers that must work with older loaders
2381 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2382 if (layer_instance_list == NULL) {
2383 layer_node = layer_node->next;
2384 goto out;
2385 }
2386 props = loader_get_next_layer_property(inst, layer_instance_list);
2387 if (NULL == props) {
2388 // Error already triggered in loader_get_next_layer_property.
2389 result = VK_ERROR_OUT_OF_HOST_MEMORY;
2390 goto out;
2391 }
2392 props->type_flags = VK_LAYER_TYPE_FLAG_INSTANCE_LAYER;
2393 if (!is_implicit) {
2394 props->type_flags |= VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER;
2395 }
2396 } else {
2397 layer_node = layer_node->next;
2398 goto out;
2399 }
2400
2401 // Library path no longer required unless component_layers is also not defined
2402 library_path = cJSON_GetObjectItem(layer_node, "library_path");
2403 component_layers = cJSON_GetObjectItem(layer_node, "component_layers");
2404 if (NULL != library_path) {
2405 if (NULL != component_layers) {
2406 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2407 "Indicating meta-layer-specific component_layers, but also "
2408 "defining layer library path. Both are not compatible, so "
2409 "skipping this layer");
2410 goto out;
2411 }
2412 props->num_component_layers = 0;
2413 props->component_layer_names = NULL;
2414
2415 temp = cJSON_Print(library_path);
2416 if (NULL == temp) {
2417 layer_node = layer_node->next;
2418 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2419 "Problem accessing layer value library_path in manifest JSON "
2420 "file, skipping this layer");
2421 result = VK_ERROR_OUT_OF_HOST_MEMORY;
2422 goto out;
2423 }
2424 temp[strlen(temp) - 1] = '\0';
2425 library_path_str = loader_stack_alloc(strlen(temp) + 1);
2426 strcpy(library_path_str, &temp[1]);
2427 cJSON_Free(temp);
2428
2429 char *fullpath = props->lib_name;
2430 char *rel_base;
2431 if (NULL != library_path_str) {
2432 if (loader_platform_is_path(library_path_str)) {
2433 // A relative or absolute path
2434 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2435 strcpy(name_copy, filename);
2436 rel_base = loader_platform_dirname(name_copy);
2437 loader_expand_path(library_path_str, rel_base, MAX_STRING_SIZE, fullpath);
2438 } else {
2439 // A filename which is assumed in a system directory
2440 loader_get_fullpath(library_path_str, DEFAULT_VK_LAYERS_PATH, MAX_STRING_SIZE, fullpath);
2441 }
2442 }
2443 } else if (NULL != component_layers) {
2444 if (version.major == 1 && (version.minor < 1 || version.patch < 1)) {
2445 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2446 "Indicating meta-layer-specific component_layers, but using older "
2447 "JSON file version.");
2448 }
2449 int count = cJSON_GetArraySize(component_layers);
2450 props->num_component_layers = count;
2451
2452 // Allocate buffer for layer names
2453 props->component_layer_names =
2454 loader_instance_heap_alloc(inst, sizeof(char[MAX_STRING_SIZE]) * count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
2455 if (NULL == props->component_layer_names) {
2456 result = VK_ERROR_OUT_OF_HOST_MEMORY;
2457 goto out;
2458 }
2459
2460 // Copy the component layers into the array
2461 for (i = 0; i < count; i++) {
2462 cJSON *comp_layer = cJSON_GetArrayItem(component_layers, i);
2463 if (NULL != comp_layer) {
2464 temp = cJSON_Print(comp_layer);
2465 if (NULL == temp) {
2466 result = VK_ERROR_OUT_OF_HOST_MEMORY;
2467 goto out;
2468 }
2469 temp[strlen(temp) - 1] = '\0';
2470 strncpy(props->component_layer_names[i], temp + 1, MAX_STRING_SIZE - 1);
2471 props->component_layer_names[i][MAX_STRING_SIZE - 1] = '\0';
2472 cJSON_Free(temp);
2473 }
2474 }
2475
2476 // This is now, officially, a meta-layer
2477 props->type_flags |= VK_LAYER_TYPE_FLAG_META_LAYER;
2478 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Encountered meta-layer %s", name);
2479
2480 // Make sure we set up other things so we head down the correct branches below
2481 library_path_str = NULL;
2482 } else {
2483 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2484 "Layer missing both library_path and component_layers fields. One or the "
2485 "other MUST be defined. Skipping this layer");
2486 goto out;
2487 }
2488
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002489 if (is_implicit) {
2490 GET_JSON_OBJECT(layer_node, disable_environment)
2491 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002492#undef GET_JSON_ITEM
2493#undef GET_JSON_OBJECT
2494
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002495 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2496 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002497 props->info.specVersion = loader_make_version(api_version);
2498 props->info.implementationVersion = atoi(implementation_version);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002499 strncpy((char *)props->info.description, description, sizeof(props->info.description));
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002500 props->info.description[sizeof(props->info.description) - 1] = '\0';
2501 if (is_implicit) {
2502 if (!disable_environment || !disable_environment->child) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002503 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2504 "Didn't find required layer child value disable_environment"
2505 "in manifest JSON file, skipping this layer");
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002506 layer_node = layer_node->next;
Mark Youngf2079b92017-05-02 10:49:46 -06002507 goto out;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002508 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002509 strncpy(props->disable_env_var.name, disable_environment->child->string, sizeof(props->disable_env_var.name));
2510 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] = '\0';
2511 strncpy(props->disable_env_var.value, disable_environment->child->valuestring, sizeof(props->disable_env_var.value));
2512 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002513 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002514
Mark Young0f183a82017-02-28 09:58:04 -07002515// Now get all optional items and objects and put in list:
2516// functions
2517// instance_extensions
2518// device_extensions
2519// enable_environment (implicit layers only)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002520#define GET_JSON_OBJECT(node, var) \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002521 { var = cJSON_GetObjectItem(node, #var); }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002522#define GET_JSON_ITEM(node, var) \
2523 { \
2524 item = cJSON_GetObjectItem(node, #var); \
2525 if (item != NULL) { \
2526 temp = cJSON_Print(item); \
2527 if (temp != NULL) { \
2528 temp[strlen(temp) - 1] = '\0'; \
2529 var = loader_stack_alloc(strlen(temp) + 1); \
2530 strcpy(var, &temp[1]); \
2531 cJSON_Free(temp); \
Mark Youngf2079b92017-05-02 10:49:46 -06002532 } else { \
2533 result = VK_ERROR_OUT_OF_HOST_MEMORY; \
2534 goto out; \
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002535 } \
2536 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002537 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002538
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002539 cJSON *instance_extensions, *device_extensions, *functions, *enable_environment;
Mark Young39389872017-01-19 21:10:49 -07002540 cJSON *entrypoints = NULL;
2541 char *vkGetInstanceProcAddr = NULL;
2542 char *vkGetDeviceProcAddr = NULL;
2543 char *vkNegotiateLoaderLayerInterfaceVersion = NULL;
2544 char *spec_version = NULL;
2545 char **entry_array = NULL;
Jon Ashburn075ce432015-12-17 17:38:24 -07002546
Mark Young39389872017-01-19 21:10:49 -07002547 // Layer interface functions
2548 // vkGetInstanceProcAddr
2549 // vkGetDeviceProcAddr
2550 // vkNegotiateLoaderLayerInterfaceVersion (starting with JSON file 1.1.0)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002551 GET_JSON_OBJECT(layer_node, functions)
2552 if (functions != NULL) {
Mark Young39389872017-01-19 21:10:49 -07002553 if (version.major > 1 || version.minor >= 1) {
2554 GET_JSON_ITEM(functions, vkNegotiateLoaderLayerInterfaceVersion)
2555 if (vkNegotiateLoaderLayerInterfaceVersion != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002556 strncpy(props->functions.str_negotiate_interface, vkNegotiateLoaderLayerInterfaceVersion,
Mark Young39389872017-01-19 21:10:49 -07002557 sizeof(props->functions.str_negotiate_interface));
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002558 props->functions.str_negotiate_interface[sizeof(props->functions.str_negotiate_interface) - 1] = '\0';
Mark Young39389872017-01-19 21:10:49 -07002559 } else {
2560 props->functions.str_negotiate_interface[0] = '\0';
2561 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002562 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2563 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
Mark Young39389872017-01-19 21:10:49 -07002564 if (vkGetInstanceProcAddr != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002565 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr, sizeof(props->functions.str_gipa));
Mark Young39389872017-01-19 21:10:49 -07002566 if (version.major > 1 || version.minor >= 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002567 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2568 "Indicating layer-specific vkGetInstanceProcAddr "
2569 "function is deprecated starting with JSON file "
2570 "version 1.1.0. Instead, use the new "
2571 "vkNegotiateLayerInterfaceVersion function to "
2572 "return the GetInstanceProcAddr function for this"
2573 "layer");
Mark Young39389872017-01-19 21:10:49 -07002574 }
2575 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002576 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
Mark Young39389872017-01-19 21:10:49 -07002577 if (vkGetDeviceProcAddr != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002578 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr, sizeof(props->functions.str_gdpa));
Mark Young39389872017-01-19 21:10:49 -07002579 if (version.major > 1 || version.minor >= 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002580 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2581 "Indicating layer-specific vkGetDeviceProcAddr "
2582 "function is deprecated starting with JSON file "
2583 "version 1.1.0. Instead, use the new "
2584 "vkNegotiateLayerInterfaceVersion function to "
2585 "return the GetDeviceProcAddr function for this"
2586 "layer");
Mark Young39389872017-01-19 21:10:49 -07002587 }
2588 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002589 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2590 }
Mark Young39389872017-01-19 21:10:49 -07002591
2592 // instance_extensions
2593 // array of {
2594 // name
2595 // spec_version
2596 // }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002597 GET_JSON_OBJECT(layer_node, instance_extensions)
2598 if (instance_extensions != NULL) {
2599 int count = cJSON_GetArraySize(instance_extensions);
2600 for (i = 0; i < count; i++) {
2601 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2602 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002603 if (name != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002604 strncpy(ext_prop.extensionName, name, sizeof(ext_prop.extensionName));
2605 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002606 }
Mark Young0ad83132016-06-30 13:02:42 -06002607 GET_JSON_ITEM(ext_item, spec_version)
2608 if (NULL != spec_version) {
2609 ext_prop.specVersion = atoi(spec_version);
2610 } else {
2611 ext_prop.specVersion = 0;
2612 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002613 bool ext_unsupported = wsi_unsupported_instance_extension(&ext_prop);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002614 if (!ext_unsupported) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002615 loader_add_to_ext_list(inst, &props->instance_extension_list, 1, &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002616 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002617 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002618 }
Mark Young39389872017-01-19 21:10:49 -07002619
2620 // device_extensions
2621 // array of {
2622 // name
2623 // spec_version
2624 // entrypoints
2625 // }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002626 GET_JSON_OBJECT(layer_node, device_extensions)
2627 if (device_extensions != NULL) {
2628 int count = cJSON_GetArraySize(device_extensions);
2629 for (i = 0; i < count; i++) {
2630 ext_item = cJSON_GetArrayItem(device_extensions, i);
2631 GET_JSON_ITEM(ext_item, name)
2632 GET_JSON_ITEM(ext_item, spec_version)
2633 if (name != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002634 strncpy(ext_prop.extensionName, name, sizeof(ext_prop.extensionName));
2635 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002636 }
Mark Young0ad83132016-06-30 13:02:42 -06002637 if (NULL != spec_version) {
2638 ext_prop.specVersion = atoi(spec_version);
2639 } else {
2640 ext_prop.specVersion = 0;
2641 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002642 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2643 GET_JSON_OBJECT(ext_item, entrypoints)
2644 int entry_count;
2645 if (entrypoints == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002646 loader_add_to_dev_ext_list(inst, &props->device_extension_list, &ext_prop, 0, NULL);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002647 continue;
2648 }
2649 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002650 if (entry_count) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002651 entry_array = (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002652 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002653 for (j = 0; j < entry_count; j++) {
2654 ext_item = cJSON_GetArrayItem(entrypoints, j);
2655 if (ext_item != NULL) {
2656 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002657 if (NULL == temp) {
2658 entry_array[j] = NULL;
Mark Youngf2079b92017-05-02 10:49:46 -06002659 result = VK_ERROR_OUT_OF_HOST_MEMORY;
2660 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06002661 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002662 temp[strlen(temp) - 1] = '\0';
2663 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2664 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002665 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002666 }
2667 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002668 loader_add_to_dev_ext_list(inst, &props->device_extension_list, &ext_prop, entry_count, entry_array);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002669 }
2670 }
2671 if (is_implicit) {
2672 GET_JSON_OBJECT(layer_node, enable_environment)
2673
2674 // enable_environment is optional
2675 if (enable_environment) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002676 strncpy(props->enable_env_var.name, enable_environment->child->string, sizeof(props->enable_env_var.name));
2677 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] = '\0';
2678 strncpy(props->enable_env_var.value, enable_environment->child->valuestring, sizeof(props->enable_env_var.value));
2679 props->enable_env_var.value[sizeof(props->enable_env_var.value) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002680 }
2681 }
Mark Youngf2079b92017-05-02 10:49:46 -06002682
2683 result = VK_SUCCESS;
2684
2685out:
2686
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002687#undef GET_JSON_ITEM
2688#undef GET_JSON_OBJECT
Mark Youngf2079b92017-05-02 10:49:46 -06002689
2690 if (VK_SUCCESS != result && NULL != props) {
2691 props->num_component_layers = 0;
2692 if (NULL != props->component_layer_names) {
2693 loader_instance_heap_free(inst, props->component_layer_names);
2694 }
2695 props->component_layer_names = NULL;
2696 }
2697
2698 return result;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002699}
2700
Mark Young39389872017-01-19 21:10:49 -07002701static inline bool is_valid_layer_json_version(const layer_json_version *layer_json) {
2702 // Supported versions are: 1.0.0, 1.0.1, and 1.1.0.
Mark Youngf2079b92017-05-02 10:49:46 -06002703 if ((layer_json->major == 1 && layer_json->minor == 1 && layer_json->patch < 2) ||
Mark Young39389872017-01-19 21:10:49 -07002704 (layer_json->major == 1 && layer_json->minor == 0 && layer_json->patch < 2)) {
2705 return true;
2706 }
2707 return false;
2708}
2709
2710static inline bool layer_json_supports_layers_tag(const layer_json_version *layer_json) {
2711 // Supported versions started in 1.0.1, so anything newer
2712 if ((layer_json->major > 1 || layer_json->minor > 0 || layer_json->patch > 1)) {
2713 return true;
2714 }
2715 return false;
2716}
2717
Mark Young0f183a82017-02-28 09:58:04 -07002718// Given a cJSON struct (json) of the top level JSON object from layer manifest
2719// file, add entry to the layer_list. Fill out the layer_properties in this list
2720// entry from the input cJSON object.
2721//
2722// \returns
2723// void
2724// layer_list has a new entry and initialized accordingly.
2725// If the json input object does not have all the required fields no entry
2726// is added to the list.
Mark Youngf2079b92017-05-02 10:49:46 -06002727static VkResult loader_add_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list,
2728 cJSON *json, bool is_implicit, char *filename) {
Mark Young39389872017-01-19 21:10:49 -07002729 // The following Fields in layer manifest file that are required:
Nekotekinad431e132017-06-11 13:13:07 +03002730 // - "file_format_version"
Mark Young39389872017-01-19 21:10:49 -07002731 // - If more than one "layer" object are used, then the "layers" array is
Mark Youngdee312c2017-03-08 13:38:35 -07002732 // required
Mark Youngf2079b92017-05-02 10:49:46 -06002733 VkResult result = VK_ERROR_INITIALIZATION_FAILED;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002734 cJSON *item, *layers_node, *layer_node;
Mike Stroyan81908212017-03-14 13:09:12 -06002735 layer_json_version json_version = {0, 0, 0};
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002736 char *vers_tok;
2737 cJSON *disable_environment = NULL;
2738 item = cJSON_GetObjectItem(json, "file_format_version");
2739 if (item == NULL) {
Mark Youngf2079b92017-05-02 10:49:46 -06002740 goto out;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002741 }
2742 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002743 if (NULL == file_vers) {
Mark Youngf2079b92017-05-02 10:49:46 -06002744 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06002745 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002746 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Found manifest file %s, version %s", filename, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002747 // Get the major/minor/and patch as integers for easier comparison
2748 vers_tok = strtok(file_vers, ".\"\n\r");
2749 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002750 json_version.major = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002751 vers_tok = strtok(NULL, ".\"\n\r");
2752 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002753 json_version.minor = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002754 vers_tok = strtok(NULL, ".\"\n\r");
2755 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002756 json_version.patch = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002757 }
2758 }
2759 }
Mark Young39389872017-01-19 21:10:49 -07002760
2761 if (!is_valid_layer_json_version(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002762 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2763 "loader_add_layer_properties: %s invalid layer "
Mark Youngf2079b92017-05-02 10:49:46 -06002764 "manifest file version %d.%d.%d. May cause errors.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002765 filename, json_version.major, json_version.minor, json_version.patch);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002766 }
Mark Young0ad83132016-06-30 13:02:42 -06002767 cJSON_Free(file_vers);
Mark Young39389872017-01-19 21:10:49 -07002768
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002769 // If "layers" is present, read in the array of layer objects
2770 layers_node = cJSON_GetObjectItem(json, "layers");
2771 if (layers_node != NULL) {
2772 int numItems = cJSON_GetArraySize(layers_node);
Mark Young39389872017-01-19 21:10:49 -07002773 if (!layer_json_supports_layers_tag(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002774 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2775 "loader_add_layer_properties: \'layers\' tag not "
2776 "supported until file version 1.0.1, but %s is "
2777 "reporting version %s",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002778 filename, file_vers);
2779 }
2780 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2781 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2782 if (layer_node == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002783 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2784 "loader_add_layer_properties: Can not find "
2785 "\'layers\' array element %d object in manifest "
2786 "JSON file %s. Skipping this file",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002787 curLayer, filename);
Mark Youngf2079b92017-05-02 10:49:46 -06002788 goto out;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002789 }
Mark Youngf2079b92017-05-02 10:49:46 -06002790 result = loader_read_json_layer(inst, layer_instance_list, layer_node, json_version, item, disable_environment,
2791 is_implicit, filename);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002792 }
2793 } else {
2794 // Otherwise, try to read in individual layers
2795 layer_node = cJSON_GetObjectItem(json, "layer");
2796 if (layer_node == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002797 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2798 "loader_add_layer_properties: Can not find \'layer\' "
2799 "object in manifest JSON file %s. Skipping this file.",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002800 filename);
Mark Youngf2079b92017-05-02 10:49:46 -06002801 goto out;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002802 }
2803 // Loop through all "layer" objects in the file to get a count of them
2804 // first.
2805 uint16_t layer_count = 0;
2806 cJSON *tempNode = layer_node;
2807 do {
2808 tempNode = tempNode->next;
2809 layer_count++;
2810 } while (tempNode != NULL);
Mark Young39389872017-01-19 21:10:49 -07002811
2812 // Throw a warning if we encounter multiple "layer" objects in file
2813 // versions newer than 1.0.0. Having multiple objects with the same
2814 // name at the same level is actually a JSON standard violation.
2815 if (layer_count > 1 && layer_json_supports_layers_tag(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002816 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2817 "loader_add_layer_properties: Multiple \'layer\' nodes"
2818 " are deprecated starting in file version \"1.0.1\". "
2819 "Please use \'layers\' : [] array instead in %s.",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002820 filename);
2821 } else {
2822 do {
Mark Youngf2079b92017-05-02 10:49:46 -06002823 result = loader_read_json_layer(inst, layer_instance_list, layer_node, json_version, item, disable_environment,
2824 is_implicit, filename);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002825 layer_node = layer_node->next;
2826 } while (layer_node != NULL);
2827 }
2828 }
Mark Youngf2079b92017-05-02 10:49:46 -06002829
2830out:
2831
2832 return result;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002833}
2834
Mark Young0f183a82017-02-28 09:58:04 -07002835// Find the Vulkan library manifest files.
2836//
2837// This function scans the "location" or "env_override" directories/files
2838// for a list of JSON manifest files. If env_override is non-NULL
2839// and has a valid value. Then the location is ignored. Otherwise
2840// location is used to look for manifest files. The location
2841// is interpreted as Registry path on Windows and a directory path(s)
2842// on Linux. "home_location" is an additional directory in the users home
2843// directory to look at. It is expanded into the dir path
2844// $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2845// on environment variables. This "home_location" is only used on Linux.
2846//
2847// \returns
2848// VKResult
2849// A string list of manifest files to be opened in out_files param.
2850// List has a pointer to string for each manifest filename.
2851// When done using the list in out_files, pointers should be freed.
2852// Location or override string lists can be either files or directories as
Mark Youngf2079b92017-05-02 10:49:46 -06002853// follows:
Mark Young0f183a82017-02-28 09:58:04 -07002854// | location | override
2855// --------------------------------
2856// Win ICD | files | files
2857// Win Layer | files | dirs
2858// Linux ICD | dirs | files
2859// Linux Layer| dirs | dirs
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002860static VkResult loader_get_manifest_files(const struct loader_instance *inst, const char *env_override, const char *source_override,
Mark Youngf2079b92017-05-02 10:49:46 -06002861 bool is_layer, bool warn_if_not_present, const char *location,
2862 const char *relative_location, struct loader_manifest_files *out_files) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06002863 const char *override = NULL;
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002864 char *override_getenv = NULL;
Mark Young0ad83132016-06-30 13:02:42 -06002865 char *loc, *orig_loc = NULL;
2866 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002867 char *file, *next_file, *name;
2868 size_t alloced_count = 64;
2869 char full_path[2048];
2870 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002871 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002872 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002873 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002874
2875 out_files->count = 0;
2876 out_files->filename_list = NULL;
2877
Jamie Madill00c3c912016-04-06 18:26:46 -04002878 if (source_override != NULL) {
2879 override = source_override;
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002880 } else if (env_override != NULL) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002881#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002882 if (geteuid() != getuid() || getegid() != getgid()) {
Mark Young0f183a82017-02-28 09:58:04 -07002883 // Don't allow setuid apps to use the env var:
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002884 env_override = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002885 }
2886#endif
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002887 if (env_override != NULL) {
Mark Youngd8c6b692017-03-09 11:39:41 -07002888 override = override_getenv = loader_secure_getenv(env_override, inst);
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002889 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002890 }
2891
Jon Ashburnb6822212016-02-16 15:34:16 -07002892#if !defined(_WIN32)
Benjamin Saunders31a48012017-01-29 14:49:54 -08002893 if (relative_location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002894#else
Benjamin Saunders31a48012017-01-29 14:49:54 -08002895 relative_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002896 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002897#endif
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002898 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2899 "loader_get_manifest_files: Can not get manifest files with "
2900 "NULL location, env_override=%s",
Mark Youngb6399312017-01-10 14:22:15 -07002901 (env_override != NULL) ? env_override : "");
Mark Young0ad83132016-06-30 13:02:42 -06002902 res = VK_ERROR_INITIALIZATION_FAILED;
2903 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002904 }
2905
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002906#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002907 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002908#else
2909 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002910#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002911 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002912 // Also handle getting the location(s) from registry on Windows
2913 if (override == NULL) {
Benjamin Saunders31a48012017-01-29 14:49:54 -08002914 size_t loc_size = 0;
2915#if !defined(_WIN32)
Mark Youngd8c6b692017-03-09 11:39:41 -07002916 const char *xdgconfdirs = loader_secure_getenv("XDG_CONFIG_DIRS", inst);
2917 const char *xdgdatadirs = loader_secure_getenv("XDG_DATA_DIRS", inst);
Mark Youngf2079b92017-05-02 10:49:46 -06002918 if (xdgconfdirs == NULL || xdgconfdirs[0] == '\0') xdgconfdirs = FALLBACK_CONFIG_DIRS;
2919 if (xdgdatadirs == NULL || xdgdatadirs[0] == '\0') xdgdatadirs = FALLBACK_DATA_DIRS;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002920 const size_t rel_size = strlen(relative_location);
2921 // Leave space for trailing separators
Mark Youngf2079b92017-05-02 10:49:46 -06002922 loc_size += strlen(xdgconfdirs) + strlen(xdgdatadirs) + 2 * rel_size + 2;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002923 for (const char *x = xdgconfdirs; *x; ++x)
2924 if (*x == PATH_SEPARATOR) loc_size += rel_size;
2925 for (const char *x = xdgdatadirs; *x; ++x)
2926 if (*x == PATH_SEPARATOR) loc_size += rel_size;
2927 loc_size += strlen(SYSCONFDIR) + rel_size + 1;
2928#if defined(EXTRASYSCONFDIR)
2929 loc_size += strlen(EXTRASYSCONFDIR) + rel_size + 1;
2930#endif
2931#else
2932 loc_size += strlen(location) + 1;
2933#endif
2934 loc = loader_stack_alloc(loc_size);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002935 if (loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002936 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2937 "loader_get_manifest_files: Failed to allocate "
2938 "%d bytes for manifest file location.",
Benjamin Saunders31a48012017-01-29 14:49:54 -08002939 loc_size);
Mark Young0ad83132016-06-30 13:02:42 -06002940 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2941 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002942 }
Benjamin Saunders31a48012017-01-29 14:49:54 -08002943 char *loc_write = loc;
2944#if !defined(_WIN32)
2945 const char *loc_read;
Karl Schultz52f5daa2017-02-12 12:34:03 -07002946 size_t start, stop;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002947
2948 loc_read = &xdgconfdirs[0];
Karl Schultz52f5daa2017-02-12 12:34:03 -07002949 start = 0;
2950 while (loc_read[start] != '\0') {
2951 while (loc_read[start] == PATH_SEPARATOR) {
2952 start++;
2953 }
2954 stop = start;
2955 while (loc_read[stop] != PATH_SEPARATOR && loc_read[stop] != '\0') {
2956 stop++;
2957 }
2958 const size_t s = stop - start;
2959 if (s) {
2960 memcpy(loc_write, &loc_read[start], s);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002961 loc_write += s;
2962 memcpy(loc_write, relative_location, rel_size);
2963 loc_write += rel_size;
2964 *loc_write++ = PATH_SEPARATOR;
Karl Schultz52f5daa2017-02-12 12:34:03 -07002965 start = stop;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002966 }
2967 }
2968
2969 memcpy(loc_write, SYSCONFDIR, strlen(SYSCONFDIR));
2970 loc_write += strlen(SYSCONFDIR);
2971 memcpy(loc_write, relative_location, rel_size);
2972 loc_write += rel_size;
2973 *loc_write++ = PATH_SEPARATOR;
2974
2975#if defined(EXTRASYSCONFDIR)
2976 memcpy(loc_write, EXTRASYSCONFDIR, strlen(EXTRASYSCONFDIR));
2977 loc_write += strlen(EXTRASYSCONFDIR);
2978 memcpy(loc_write, relative_location, rel_size);
2979 loc_write += rel_size;
2980 *loc_write++ = PATH_SEPARATOR;
2981#endif
2982
2983 loc_read = &xdgdatadirs[0];
Karl Schultz52f5daa2017-02-12 12:34:03 -07002984 start = 0;
2985 while (loc_read[start] != '\0') {
2986 while (loc_read[start] == PATH_SEPARATOR) {
2987 start++;
2988 }
2989 stop = start;
2990 while (loc_read[stop] != PATH_SEPARATOR && loc_read[stop] != '\0') {
2991 stop++;
2992 }
2993 const size_t s = stop - start;
2994 if (s) {
2995 memcpy(loc_write, &loc_read[start], s);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002996 loc_write += s;
2997 memcpy(loc_write, relative_location, rel_size);
2998 loc_write += rel_size;
2999 *loc_write++ = PATH_SEPARATOR;
Karl Schultz52f5daa2017-02-12 12:34:03 -07003000 start = stop;
Benjamin Saunders31a48012017-01-29 14:49:54 -08003001 }
3002 }
Karl Schultz52f5daa2017-02-12 12:34:03 -07003003
Benjamin Saunders31a48012017-01-29 14:49:54 -08003004 --loc_write;
3005#else
3006 memcpy(loc_write, location, strlen(location));
3007 loc_write += strlen(location);
3008#endif
3009 assert(loc_write - loc < (ptrdiff_t)loc_size);
3010 *loc_write = '\0';
3011
Johannes van Waveren9bd805012015-10-28 11:45:00 -05003012#if defined(_WIN32)
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +02003013 VkResult regHKR_result = VK_SUCCESS;
3014
Slawomir Cygan8f9f2552017-08-09 11:36:52 +02003015 DWORD reg_size = 4096;
3016
Lenny Komowf7c09382017-08-31 16:35:08 -06003017 if (!strncmp(loc, DEFAULT_VK_DRIVERS_INFO, sizeof(DEFAULT_VK_DRIVERS_INFO))) {
3018 regHKR_result = loaderGetDeviceRegistryFiles(inst, &reg, &reg_size, LoaderPnpDriverRegistry());
3019 } else if (!strncmp(loc, DEFAULT_VK_ELAYERS_INFO, sizeof(DEFAULT_VK_ELAYERS_INFO))) {
3020 regHKR_result = loaderGetDeviceRegistryFiles(inst, &reg, &reg_size, LoaderPnpELayerRegistry());
3021 } else if (!strncmp(loc, DEFAULT_VK_ILAYERS_INFO, sizeof(DEFAULT_VK_ILAYERS_INFO))) {
3022 regHKR_result = loaderGetDeviceRegistryFiles(inst, &reg, &reg_size, LoaderPnpILayerRegistry());
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +02003023 }
3024
Slawomir Cygan8f9f2552017-08-09 11:36:52 +02003025 VkResult reg_result = loaderGetRegistryFiles(inst, loc, is_layer, &reg, &reg_size);
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +02003026
3027 if ((VK_SUCCESS != reg_result && VK_SUCCESS != regHKR_result) || NULL == reg) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003028 if (!is_layer) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003029 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3030 "loader_get_manifest_files: Registry lookup failed "
3031 "to get ICD manifest files. Possibly missing Vulkan"
3032 " driver?");
Slawomir Cygan9a2fa0e2017-07-03 16:47:52 +02003033 if (VK_SUCCESS == regHKR_result || VK_ERROR_OUT_OF_HOST_MEMORY == regHKR_result) {
3034 res = regHKR_result;
3035 } else if (VK_SUCCESS == reg_result || VK_ERROR_OUT_OF_HOST_MEMORY == reg_result) {
Mark Young2c84c0c2017-01-13 10:27:03 -07003036 res = reg_result;
3037 } else {
3038 res = VK_ERROR_INCOMPATIBLE_DRIVER;
3039 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003040 } else {
Mark Youngf8c20102016-11-07 16:26:17 -07003041 if (warn_if_not_present) {
Mark Young2c84c0c2017-01-13 10:27:03 -07003042 // This is only a warning for layers
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003043 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3044 "loader_get_manifest_files: Registry lookup failed "
3045 "to get layer manifest files.");
Mark Youngf8c20102016-11-07 16:26:17 -07003046 }
Mark Young2c84c0c2017-01-13 10:27:03 -07003047 if (reg_result == VK_ERROR_OUT_OF_HOST_MEMORY) {
3048 res = reg_result;
3049 } else {
3050 // Return success for now since it's not critical for layers
3051 res = VK_SUCCESS;
3052 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003053 }
Mark Young0ad83132016-06-30 13:02:42 -06003054 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06003055 }
Mark Young0ad83132016-06-30 13:02:42 -06003056 orig_loc = loc;
3057 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07003058#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07003059 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003060 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07003061 if (loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003062 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3063 "loader_get_manifest_files: Failed to allocate space for "
3064 "override environment variable of length %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003065 strlen(override) + 1);
Mark Young0ad83132016-06-30 13:02:42 -06003066 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3067 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07003068 }
3069 strcpy(loc, override);
3070 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003071
Liam Middlebrook9b14e892015-07-23 18:32:20 -07003072 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003073 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07003074
Jon Ashburn2077e382015-06-29 11:25:34 -06003075 file = loc;
3076 while (*file) {
3077 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07003078 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003079 sysdir = opendir(file);
3080 name = NULL;
3081 if (sysdir) {
3082 dent = readdir(sysdir);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003083 if (dent == NULL) break;
Jon Ashburn2077e382015-06-29 11:25:34 -06003084 name = &(dent->d_name[0]);
3085 loader_get_fullpath(name, file, sizeof(full_path), full_path);
3086 name = full_path;
3087 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003088 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05003089#if defined(_WIN32)
3090 name = file;
3091#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07003092 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06003093 char *dir;
3094 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07003095 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06003096 if (dir == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003097 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3098 "loader_get_manifest_files: Failed to allocate "
3099 "space for relative location path length %d",
Mark Youngb6399312017-01-10 14:22:15 -07003100 strlen(loc) + 1);
Mark Young0ad83132016-06-30 13:02:42 -06003101 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06003102 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07003103 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06003104
3105 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
3106
3107 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07003108#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06003109 }
3110 while (name) {
Mark Young0f183a82017-02-28 09:58:04 -07003111 // Look for files ending with ".json" suffix
Jon Ashburn23d36b12016-02-02 17:47:28 -07003112 uint32_t nlen = (uint32_t)strlen(name);
3113 const char *suf = name + nlen - 5;
Lenny Komow408e0bd2017-08-09 16:01:59 -06003114
3115 // Check if the file is already present
3116 bool file_already_loaded = false;
3117 for (uint32_t i = 0; i < out_files->count; ++i) {
3118 if (!strcmp(out_files->filename_list[i], name)) {
3119 file_already_loaded = true;
3120 }
3121 }
3122
3123 if (!file_already_loaded && (nlen > 5) && !strncmp(suf, ".json", 5)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003124 if (out_files->count == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003125 out_files->filename_list =
3126 loader_instance_heap_alloc(inst, alloced_count * sizeof(char *), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Youngbb3a29c2017-05-19 12:29:43 -06003127 if (NULL == out_files->filename_list) {
3128 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3129 "loader_get_manifest_files: Failed to allocate space for manifest file name list");
3130 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3131 goto out;
3132 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003133 } else if (out_files->count == alloced_count) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06003134 void *new_ptr =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003135 loader_instance_heap_realloc(inst, out_files->filename_list, alloced_count * sizeof(char *),
3136 alloced_count * sizeof(char *) * 2, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Youngbb3a29c2017-05-19 12:29:43 -06003137 if (NULL == new_ptr) {
3138 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3139 "loader_get_manifest_files: Failed to reallocate space for manifest file name list");
3140 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3141 goto out;
3142 }
3143 out_files->filename_list = new_ptr;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003144 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06003145 }
Mark Young0ad83132016-06-30 13:02:42 -06003146 out_files->filename_list[out_files->count] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003147 loader_instance_heap_alloc(inst, strlen(name) + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003148 if (out_files->filename_list[out_files->count] == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003149 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3150 "loader_get_manifest_files: Failed to allocate "
3151 "space for manifest file %d list",
Mark Youngb6399312017-01-10 14:22:15 -07003152 out_files->count);
Mark Young0ad83132016-06-30 13:02:42 -06003153 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3154 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003155 }
3156 strcpy(out_files->filename_list[out_files->count], name);
3157 out_files->count++;
Lenny Komow408e0bd2017-08-09 16:01:59 -06003158 } else if(file_already_loaded) {
3159 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3160 "Skipping manifest file %s - The file has already been read once", name);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003161 } else if (!list_is_dirs) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003162 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "Skipping manifest file %s, file name must end in .json",
3163 name);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003164 }
3165 if (list_is_dirs) {
3166 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06003167 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003168 break;
Mark Young0ad83132016-06-30 13:02:42 -06003169 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003170 name = &(dent->d_name[0]);
3171 loader_get_fullpath(name, file, sizeof(full_path), full_path);
3172 name = full_path;
3173 } else {
3174 break;
3175 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003176 }
Mark Young0ad83132016-06-30 13:02:42 -06003177 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003178 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06003179 sysdir = NULL;
3180 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003181 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07003182#if !defined(_WIN32)
Benjamin Saunders31a48012017-01-29 14:49:54 -08003183 if (relative_location != NULL && (next_file == NULL || *next_file == '\0') && override == NULL) {
Mark Youngd8c6b692017-03-09 11:39:41 -07003184 char *xdgdatahome = loader_secure_getenv("XDG_DATA_HOME", inst);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003185 size_t len;
3186 if (xdgdatahome != NULL) {
Mark Young6ef95f42017-02-17 09:02:23 -07003187 size_t alloc_len = strlen(xdgdatahome) + 2 + strlen(relative_location);
3188 char *home_loc = loader_stack_alloc(alloc_len);
Jon Ashburn67e262e2016-02-18 12:45:39 -07003189 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003190 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3191 "loader_get_manifest_files: Failed to allocate "
3192 "space for manifest file XDG Home location");
Mark Young0ad83132016-06-30 13:02:42 -06003193 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3194 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07003195 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003196 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07003197 // Add directory separator if needed
Benjamin Saunders31a48012017-01-29 14:49:54 -08003198 if (relative_location[0] != DIRECTORY_SYMBOL) {
Jon Ashburn67e262e2016-02-18 12:45:39 -07003199 len = strlen(home_loc);
3200 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07003201 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07003202 }
Mark Young6ef95f42017-02-17 09:02:23 -07003203 strncat(home_loc, relative_location, alloc_len);
Jon Ashburn67e262e2016-02-18 12:45:39 -07003204 file = home_loc;
3205 next_file = loader_get_next_path(file);
Benjamin Saunders31a48012017-01-29 14:49:54 -08003206 relative_location = NULL;
Jon Ashburn67e262e2016-02-18 12:45:39 -07003207
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003208 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
3209 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07003210 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003211
3212 } else {
Mark Youngd8c6b692017-03-09 11:39:41 -07003213 char *home = loader_secure_getenv("HOME", inst);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003214 if (home != NULL) {
Mark Young6ef95f42017-02-17 09:02:23 -07003215 size_t alloc_len = strlen(home) + 16 + strlen(relative_location);
3216 char *home_loc = loader_stack_alloc(alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003217 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003218 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3219 "loader_get_manifest_files: Failed to allocate "
3220 "space for manifest file Home location");
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003221 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3222 goto out;
3223 }
Mark Young6ef95f42017-02-17 09:02:23 -07003224 strncpy(home_loc, home, alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003225
3226 len = strlen(home);
3227 if (home[len] != DIRECTORY_SYMBOL) {
3228 home_loc[len] = DIRECTORY_SYMBOL;
3229 home_loc[len + 1] = '\0';
3230 }
Mark Young6ef95f42017-02-17 09:02:23 -07003231 strncat(home_loc, ".local/share", alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003232
Benjamin Saunders31a48012017-01-29 14:49:54 -08003233 if (relative_location[0] != DIRECTORY_SYMBOL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003234 len = strlen(home_loc);
3235 home_loc[len] = DIRECTORY_SYMBOL;
3236 home_loc[len + 1] = '\0';
3237 }
Mark Young6ef95f42017-02-17 09:02:23 -07003238 strncat(home_loc, relative_location, alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003239 file = home_loc;
3240 next_file = loader_get_next_path(file);
Benjamin Saunders31a48012017-01-29 14:49:54 -08003241 relative_location = NULL;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003242
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003243 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
3244 home_loc);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01003245 list_is_dirs = true;
3246 } else {
3247 // without knowing HOME, we just.. give up
3248 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07003249 }
3250 }
3251#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06003252 }
Mark Young0ad83132016-06-30 13:02:42 -06003253
3254out:
3255 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
3256 for (uint32_t remove = 0; remove < out_files->count; remove++) {
3257 loader_instance_heap_free(inst, out_files->filename_list[remove]);
3258 }
3259 loader_instance_heap_free(inst, out_files->filename_list);
3260 out_files->count = 0;
3261 out_files->filename_list = NULL;
3262 }
3263
3264 if (NULL != sysdir) {
3265 closedir(sysdir);
3266 }
3267
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05003268 if (override_getenv != NULL) {
3269 loader_free_getenv(override_getenv, inst);
3270 }
3271
Mark Young0ad83132016-06-30 13:02:42 -06003272 if (NULL != reg && reg != orig_loc) {
3273 loader_instance_heap_free(inst, reg);
3274 }
3275 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06003276}
3277
Jon Ashburn23d36b12016-02-02 17:47:28 -07003278void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06003279
Jon Ashburn23d36b12016-02-02 17:47:28 -07003280void loader_destroy_icd_lib_list() {}
Mark Young0f183a82017-02-28 09:58:04 -07003281
3282// Try to find the Vulkan ICD driver(s).
3283//
3284// This function scans the default system loader path(s) or path
3285// specified by the \c VK_ICD_FILENAMES environment variable in
3286// order to find loadable VK ICDs manifest files. From these
3287// manifest files it finds the ICD libraries.
3288//
3289// \returns
3290// Vulkan result
3291// (on result == VK_SUCCESS) a list of icds that were discovered
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003292VkResult loader_icd_scan(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003293 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003294 uint16_t file_major_vers = 0;
3295 uint16_t file_minor_vers = 0;
3296 uint16_t file_patch_vers = 0;
3297 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06003298 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06003299 VkResult res = VK_SUCCESS;
3300 bool lockedMutex = false;
3301 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003302 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06003303
Mark Young0ad83132016-06-30 13:02:42 -06003304 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
3305
Mark Young0153e0b2016-11-03 14:27:13 -06003306 res = loader_scanned_icd_init(inst, icd_tramp_list);
Mark Young0ad83132016-06-30 13:02:42 -06003307 if (VK_SUCCESS != res) {
3308 goto out;
3309 }
3310
Jon Ashburn2077e382015-06-29 11:25:34 -06003311 // Get a list of manifest files for ICDs
Benjamin Saunders31a48012017-01-29 14:49:54 -08003312 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false, true, DEFAULT_VK_DRIVERS_INFO, RELATIVE_VK_DRIVERS_INFO,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003313 &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003314 if (VK_SUCCESS != res || manifest_files.count == 0) {
3315 goto out;
3316 }
Mark Youngf2079b92017-05-02 10:49:46 -06003317
Jon Ashburn6461ef22015-09-22 13:11:00 -06003318 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003319 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06003320 for (uint32_t i = 0; i < manifest_files.count; i++) {
3321 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06003322 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003323 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003324 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003325
Mark Youngdcd5f7e2017-04-25 10:35:54 -06003326 VkResult temp_res = loader_get_json(inst, file_str, &json);
3327 if (NULL == json || temp_res != VK_SUCCESS) {
Mark Youngd66edd52017-03-10 17:31:18 -07003328 if (NULL != json) {
3329 cJSON_Delete(json);
3330 json = NULL;
3331 }
Mark Youngdcd5f7e2017-04-25 10:35:54 -06003332 // If we haven't already found an ICD, copy this result to
3333 // the returned result.
3334 if (num_good_icds == 0) {
3335 res = temp_res;
3336 }
3337 if (temp_res == VK_ERROR_OUT_OF_HOST_MEMORY) {
Mark Youngd66edd52017-03-10 17:31:18 -07003338 break;
3339 } else {
3340 continue;
3341 }
Mark Young0ad83132016-06-30 13:02:42 -06003342 }
Mark Youngdcd5f7e2017-04-25 10:35:54 -06003343 res = temp_res;
Mark Young3a587792016-08-19 15:25:08 -06003344
Jon Ashburn005617f2015-11-17 17:35:40 -07003345 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003346 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06003347 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06003348 if (num_good_icds == 0) {
3349 res = VK_ERROR_INITIALIZATION_FAILED;
3350 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003351 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3352 "loader_icd_scan: ICD JSON %s does not have a"
3353 " \'file_format_version\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003354 file_str);
Derrick Owens62e16ef2016-09-09 15:49:07 -04003355 cJSON_Delete(json);
3356 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003357 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06003358 }
Mark Youngd66edd52017-03-10 17:31:18 -07003359
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003360 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003361 if (NULL == file_vers) {
Mark Youngd66edd52017-03-10 17:31:18 -07003362 // Only reason the print can fail is if there was an allocation issue
Mark Young3a587792016-08-19 15:25:08 -06003363 if (num_good_icds == 0) {
3364 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3365 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003366 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3367 "loader_icd_scan: Failed retrieving ICD JSON %s"
3368 " \'file_format_version\' field. Skipping ICD JSON",
Mark Youngb6399312017-01-10 14:22:15 -07003369 file_str);
Derrick Owenscd92b8b2016-09-09 15:45:13 -04003370 cJSON_Delete(json);
3371 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003372 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003373 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003374 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Found ICD manifest file %s, version %s", file_str, file_vers);
Mark Youngd66edd52017-03-10 17:31:18 -07003375
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003376 // Get the major/minor/and patch as integers for easier comparison
3377 vers_tok = strtok(file_vers, ".\"\n\r");
3378 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003379 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003380 vers_tok = strtok(NULL, ".\"\n\r");
3381 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003382 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003383 vers_tok = strtok(NULL, ".\"\n\r");
3384 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003385 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003386 }
3387 }
3388 }
Mark Youngd66edd52017-03-10 17:31:18 -07003389
3390 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003391 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3392 "loader_icd_scan: Unexpected manifest file version "
3393 "(expected 1.0.0 or 1.0.1), may cause errors");
Mark Youngd66edd52017-03-10 17:31:18 -07003394 }
Mark Young0ad83132016-06-30 13:02:42 -06003395 cJSON_Free(file_vers);
Mark Youngd66edd52017-03-10 17:31:18 -07003396
Jon Ashburn005617f2015-11-17 17:35:40 -07003397 itemICD = cJSON_GetObjectItem(json, "ICD");
3398 if (itemICD != NULL) {
3399 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003400 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003401 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06003402 if (!temp || strlen(temp) == 0) {
Mark Young3a587792016-08-19 15:25:08 -06003403 if (num_good_icds == 0) {
3404 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3405 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003406 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3407 "loader_icd_scan: Failed retrieving ICD JSON %s"
3408 " \'library_path\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003409 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003410 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06003411 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003412 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003413 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06003414 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003415 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06003416 temp[strlen(temp) - 1] = '\0';
3417 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06003418 if (NULL == library_path) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003419 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3420 "loader_icd_scan: Failed to allocate space for "
3421 "ICD JSON %s \'library_path\' value. Skipping "
3422 "ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003423 file_str);
Mark Young3a587792016-08-19 15:25:08 -06003424 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3425 cJSON_Free(temp);
3426 cJSON_Delete(json);
3427 json = NULL;
3428 goto out;
3429 }
Jon Ashburn86251302015-08-25 16:48:24 -06003430 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003431 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003432 if (strlen(library_path) == 0) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003433 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3434 "loader_icd_scan: ICD JSON %s \'library_path\'"
3435 " field is empty. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003436 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003437 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003438 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003439 continue;
3440 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003441 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003442 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003443 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching for ICD drivers named %s, using default dir %s",
3444 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003445 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003446 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003447 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3448 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003449 strcpy(name_copy, file_str);
3450 rel_base = loader_platform_dirname(name_copy);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003451 loader_expand_path(library_path, rel_base, sizeof(fullpath), fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003452 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003453 // a filename which is assumed in a system directory
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003454 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH, sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003455 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003456
3457 uint32_t vers = 0;
3458 item = cJSON_GetObjectItem(itemICD, "api_version");
3459 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003460 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003461 if (NULL == temp) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003462 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3463 "loader_icd_scan: Failed retrieving ICD JSON %s"
3464 " \'api_version\' field. Skipping ICD JSON.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003465 file_str);
Mark Youngb6399312017-01-10 14:22:15 -07003466
Mark Young0ad83132016-06-30 13:02:42 -06003467 // Only reason the print can fail is if there was an
3468 // allocation issue
Mark Youngb6399312017-01-10 14:22:15 -07003469 if (num_good_icds == 0) {
3470 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3471 }
3472
3473 cJSON_Free(temp);
3474 cJSON_Delete(json);
3475 json = NULL;
3476 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003477 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003478 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003479 cJSON_Free(temp);
Mark Youngb6399312017-01-10 14:22:15 -07003480 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003481 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3482 "loader_icd_scan: ICD JSON %s does not have an"
3483 " \'api_version\' field.",
Mark Youngb6399312017-01-10 14:22:15 -07003484 file_str);
Jon Ashburn005617f2015-11-17 17:35:40 -07003485 }
Mark Youngb6399312017-01-10 14:22:15 -07003486
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003487 res = loader_scanned_icd_add(inst, icd_tramp_list, fullpath, vers);
Mark Young3a587792016-08-19 15:25:08 -06003488 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003489 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3490 "loader_icd_scan: Failed to add ICD JSON %s. "
3491 " Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003492 fullpath);
Mark Youngd66edd52017-03-10 17:31:18 -07003493 cJSON_Delete(json);
3494 json = NULL;
Mark Youngb6399312017-01-10 14:22:15 -07003495 continue;
Mark Young3a587792016-08-19 15:25:08 -06003496 }
3497 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003498 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003499 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3500 "loader_icd_scan: Failed to find \'library_path\' "
3501 "object in ICD JSON file %s. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003502 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003503 }
3504 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003505 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3506 "loader_icd_scan: Can not find \'ICD\' object in ICD JSON "
3507 "file %s. Skipping ICD JSON",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003508 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003509 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003510
Mark Young0ad83132016-06-30 13:02:42 -06003511 cJSON_Delete(json);
3512 json = NULL;
3513 }
3514
3515out:
Mark Youngb6399312017-01-10 14:22:15 -07003516
Mark Young0ad83132016-06-30 13:02:42 -06003517 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003518 cJSON_Delete(json);
3519 }
Mark Youngd66edd52017-03-10 17:31:18 -07003520
Mark Young0ad83132016-06-30 13:02:42 -06003521 if (NULL != manifest_files.filename_list) {
3522 for (uint32_t i = 0; i < manifest_files.count; i++) {
3523 if (NULL != manifest_files.filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003524 loader_instance_heap_free(inst, manifest_files.filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003525 }
3526 }
3527 loader_instance_heap_free(inst, manifest_files.filename_list);
3528 }
3529 if (lockedMutex) {
3530 loader_platform_thread_unlock_mutex(&loader_json_lock);
3531 }
Mark Youngd66edd52017-03-10 17:31:18 -07003532
Mark Young0ad83132016-06-30 13:02:42 -06003533 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003534}
3535
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003536void loader_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003537 char *file_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003538 struct loader_manifest_files manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003539 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003540 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003541 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003542
Mark Young0ad83132016-06-30 13:02:42 -06003543 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3544
3545 // Get a list of manifest files for explicit layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003546 if (VK_SUCCESS != loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH, true, true, DEFAULT_VK_ELAYERS_INFO,
Benjamin Saunders31a48012017-01-29 14:49:54 -08003547 RELATIVE_VK_ELAYERS_INFO, &manifest_files[0])) {
Mark Young0ad83132016-06-30 13:02:42 -06003548 goto out;
3549 }
3550
3551 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003552 // Pass NULL for environment variable override - implicit layers are not
3553 // overridden by LAYERS_PATH_ENV
Benjamin Saunders31a48012017-01-29 14:49:54 -08003554 if (VK_SUCCESS != loader_get_manifest_files(inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO, RELATIVE_VK_ILAYERS_INFO,
Mark Youngf8c20102016-11-07 16:26:17 -07003555 &manifest_files[1])) {
Mark Young0ad83132016-06-30 13:02:42 -06003556 goto out;
3557 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003558
Mark Young0ad83132016-06-30 13:02:42 -06003559 // Make sure we have at least one layer, if not, go ahead and return
3560 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3561 goto out;
3562 }
3563
3564 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003565 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003566
Jon Ashburn6461ef22015-09-22 13:11:00 -06003567 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003568 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003569 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003570 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003571 file_str = manifest_files[implicit].filename_list[i];
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003572 if (file_str == NULL) continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003573
Jon Ashburn075ce432015-12-17 17:38:24 -07003574 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003575 VkResult res = loader_get_json(inst, file_str, &json);
3576 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3577 break;
3578 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003579 continue;
3580 }
3581
Mark Youngf2079b92017-05-02 10:49:46 -06003582 VkResult local_res = loader_add_layer_properties(inst, instance_layers, json, (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003583 cJSON_Delete(json);
Mark Youngf2079b92017-05-02 10:49:46 -06003584
3585 if (VK_SUCCESS != local_res) {
3586 goto out;
3587 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003588 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003589 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003590
Mark Youngf2079b92017-05-02 10:49:46 -06003591 // See if "VK_LAYER_LUNARG_standard_validation" already in list.
3592 bool found_std_val = false;
3593 for (uint32_t i = 0; i < instance_layers->count; i++) {
3594 struct loader_layer_properties *props = &instance_layers->list[i];
3595 if (strcmp(props->info.layerName, std_validation_str) == 0) {
3596 found_std_val = true;
3597 break;
3598 }
3599 }
3600
3601 // If we didn't find the VK_LAYER_LUNARG_standard_validation meta-layer in
3602 // the list, then we need to add it manually. This is likely because we're
3603 // dealing with a new loader, but an old layer folder.
3604 if (!found_std_val && !loader_add_legacy_std_val_layer(inst, instance_layers)) {
3605 goto out;
3606 }
3607
3608 // Verify any meta-layers in the list are valid and all the component layers are
3609 // actually present in the available layer list
3610 verify_all_meta_layers(inst, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003611
Mark Young0ad83132016-06-30 13:02:42 -06003612out:
3613
3614 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3615 if (NULL != manifest_files[manFile].filename_list) {
3616 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3617 if (NULL != manifest_files[manFile].filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003618 loader_instance_heap_free(inst, manifest_files[manFile].filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003619 }
3620 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003621 loader_instance_heap_free(inst, manifest_files[manFile].filename_list);
Mark Young0ad83132016-06-30 13:02:42 -06003622 }
3623 }
3624 if (lockedMutex) {
3625 loader_platform_thread_unlock_mutex(&loader_json_lock);
3626 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003627}
3628
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003629void loader_implicit_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003630 char *file_str;
3631 struct loader_manifest_files manifest_files;
3632 cJSON *json;
3633 uint32_t i;
3634
3635 // Pass NULL for environment variable override - implicit layers are not
3636 // overridden by LAYERS_PATH_ENV
Mark Youngf2079b92017-05-02 10:49:46 -06003637 VkResult res = loader_get_manifest_files(inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO, RELATIVE_VK_ILAYERS_INFO,
3638 &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003639 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003640 return;
3641 }
3642
Mark Young0f183a82017-02-28 09:58:04 -07003643 // Cleanup any previously scanned libraries
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003644 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003645
3646 loader_platform_thread_lock_mutex(&loader_json_lock);
3647
3648 for (i = 0; i < manifest_files.count; i++) {
3649 file_str = manifest_files.filename_list[i];
3650 if (file_str == NULL) {
3651 continue;
3652 }
3653
3654 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003655 res = loader_get_json(inst, file_str, &json);
3656 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3657 break;
3658 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003659 continue;
3660 }
3661
Mark Youngf2079b92017-05-02 10:49:46 -06003662 res = loader_add_layer_properties(inst, instance_layers, json, true, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003663
Mark Young0ad83132016-06-30 13:02:42 -06003664 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003665 cJSON_Delete(json);
Mark Youngf2079b92017-05-02 10:49:46 -06003666
3667 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3668 break;
3669 }
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003670 }
Mark Young0ad83132016-06-30 13:02:42 -06003671 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003672 loader_platform_thread_unlock_mutex(&loader_json_lock);
3673}
3674
Jean-Francois Roybd7ceab2017-07-06 14:10:13 -07003675// Check if an implicit layer should be enabled.
3676bool loader_is_implicit_layer_enabled(const struct loader_instance *inst, const struct loader_layer_properties *prop) {
3677 bool enable = false;
3678 char *env_value = NULL;
3679
3680 // if no enable_environment variable is specified, this implicit layer
3681 // should always be enabled. Otherwise check if the variable is set
3682 if (prop->enable_env_var.name[0] == 0) {
3683 enable = true;
3684 } else {
3685 env_value = loader_secure_getenv(prop->enable_env_var.name, inst);
3686 if (env_value && !strcmp(prop->enable_env_var.value, env_value)) enable = true;
3687 loader_free_getenv(env_value, inst);
3688 }
3689
3690 // disable_environment has priority, i.e. if both enable and disable
3691 // environment variables are set, the layer is disabled. Implicit
3692 // layers are required to have a disable_environment variables
3693 env_value = loader_secure_getenv(prop->disable_env_var.name, inst);
3694 if (env_value && !strcmp(prop->disable_env_var.value, env_value)) enable = false;
3695 loader_free_getenv(env_value, inst);
3696
3697 return enable;
3698}
3699
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003700static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003701 // inst is not wrapped
3702 if (inst == VK_NULL_HANDLE) {
3703 return NULL;
3704 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003705 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003706 void *addr;
3707
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003708 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003709
3710 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003711 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003712 if (found_name) {
3713 return addr;
3714 }
3715
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003716 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, true, NULL, &addr)) return addr;
Mark Young39389872017-01-19 21:10:49 -07003717
3718 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003719 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "loader_gpdpa_instance_internal() unrecognized name %s", pName);
Mark Young39389872017-01-19 21:10:49 -07003720 return NULL;
3721}
3722
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003723static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_terminator(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003724 // inst is not wrapped
3725 if (inst == VK_NULL_HANDLE) {
3726 return NULL;
3727 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003728 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003729 void *addr;
3730
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003731 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003732
3733 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003734 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003735 if (found_name) {
3736 return addr;
3737 }
3738
3739 // Get the terminator, but don't perform checking since it should already
3740 // have been setup if we get here.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003741 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, false, NULL, &addr)) {
Mark Young39389872017-01-19 21:10:49 -07003742 return addr;
3743 }
3744
3745 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003746 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "loader_gpdpa_instance_terminator() unrecognized name %s", pName);
Mark Young39389872017-01-19 21:10:49 -07003747 return NULL;
3748}
3749
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003750static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003751 if (!strcmp(pName, "vkGetInstanceProcAddr")) {
3752 return (PFN_vkVoidFunction)loader_gpa_instance_internal;
3753 }
3754 if (!strcmp(pName, "vk_layerGetPhysicalDeviceProcAddr")) {
3755 return (PFN_vkVoidFunction)loader_gpdpa_instance_terminator;
3756 }
3757 if (!strcmp(pName, "vkCreateInstance")) {
3758 return (PFN_vkVoidFunction)terminator_CreateInstance;
3759 }
3760 if (!strcmp(pName, "vkCreateDevice")) {
3761 return (PFN_vkVoidFunction)terminator_CreateDevice;
3762 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003763
Jon Ashburn27cd5842015-05-12 17:26:48 -06003764 // inst is not wrapped
3765 if (inst == VK_NULL_HANDLE) {
3766 return NULL;
3767 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003768 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003769 void *addr;
3770
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003771 if (disp_table == NULL) return NULL;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003772
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003773 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003774 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003775 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003776 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003777 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003778
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003779 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003780 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003781 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003782}
3783
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003784VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_device_internal(VkDevice device, const char *pName) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003785 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003786 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Mark Young16573c72016-06-28 10:52:43 -06003787
Mark Young0f183a82017-02-28 09:58:04 -07003788 // Return this function if a layer above here is asking for the vkGetDeviceProcAddr.
3789 // This is so we can properly intercept any device commands needing a terminator.
3790 if (!strcmp(pName, "vkGetDeviceProcAddr")) {
3791 return (PFN_vkVoidFunction)loader_gpa_device_internal;
3792 }
3793
Mark Young65cb3662016-11-07 13:27:02 -07003794 // NOTE: Device Funcs needing Trampoline/Terminator.
3795 // Overrides for device functions needing a trampoline and
3796 // a terminator because certain device entry-points still need to go
3797 // through a terminator before hitting the ICD. This could be for
3798 // several reasons, but the main one is currently unwrapping an
3799 // object before passing the appropriate info along to the ICD.
3800 // This is why we also have to override the direct ICD call to
3801 // vkGetDeviceProcAddr to intercept those calls.
Mark Young0f183a82017-02-28 09:58:04 -07003802 PFN_vkVoidFunction addr = get_extension_device_proc_terminator(pName);
3803 if (NULL != addr) {
3804 return addr;
Mark Young16573c72016-06-28 10:52:43 -06003805 }
3806
Mark Young0f183a82017-02-28 09:58:04 -07003807 return icd_term->dispatch.GetDeviceProcAddr(device, pName);
Piers Daniell295fe402016-03-29 11:51:11 -06003808}
3809
Mark Young0f183a82017-02-28 09:58:04 -07003810// Initialize device_ext dispatch table entry as follows:
3811// If dev == NULL find all logical devices created within this instance and
3812// init the entry (given by idx) in the ext dispatch table.
3813// If dev != NULL only initialize the entry in the given dev's dispatch table.
3814// The initialization value is gotten by calling down the device chain with
3815// GDPA.
3816// If GDPA returns NULL then don't initialize the dispatch table entry.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003817static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst, struct loader_device *dev, uint32_t idx,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003818 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003819
Jon Ashburn23d36b12016-02-02 17:47:28 -07003820{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003821 void *gdpa_value;
3822 if (dev != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003823 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(dev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003824 if (gdpa_value != NULL) dev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003825 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003826 for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
Mark Young0153e0b2016-11-03 14:27:13 -06003827 struct loader_device *ldev = icd_term->logical_device_list;
Karl Schultz2558bd32016-02-24 14:39:39 -07003828 while (ldev) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003829 gdpa_value = ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(ldev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003830 if (gdpa_value != NULL) ldev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003831 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003832 }
3833 }
3834 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003835}
3836
Mark Young0f183a82017-02-28 09:58:04 -07003837// Find all dev extension in the hash table and initialize the dispatch table
3838// for dev for each of those extension entrypoints found in hash table.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003839void loader_init_dispatch_dev_ext(struct loader_instance *inst, struct loader_device *dev) {
Mark Young39389872017-01-19 21:10:49 -07003840 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3841 if (inst->dev_ext_disp_hash[i].func_name != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003842 loader_init_dispatch_dev_ext_entry(inst, dev, i, inst->dev_ext_disp_hash[i].func_name);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003843 }
3844}
3845
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003846static bool loader_check_icds_for_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young0153e0b2016-11-03 14:27:13 -06003847 struct loader_icd_term *icd_term;
3848 icd_term = inst->icd_terms;
3849 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003850 if (icd_term->scanned_icd->GetInstanceProcAddr(icd_term->instance, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003851 // this icd supports funcName
3852 return true;
Mark Young0153e0b2016-11-03 14:27:13 -06003853 icd_term = icd_term->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003854 }
3855
3856 return false;
3857}
3858
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003859static bool loader_check_layer_list_for_dev_ext_address(const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003860 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003861 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003862 // Iterate over the extensions.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003863 const struct loader_device_extension_list *const extensions = &(layers->list[layer].device_extension_list);
3864 for (uint32_t extension = 0; extension < extensions->count; ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003865 // Iterate over the entry points.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003866 const struct loader_dev_ext_props *const property = &(extensions->list[extension]);
3867 for (uint32_t entry = 0; entry < property->entrypoint_count; ++entry) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003868 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003869 return true;
3870 }
3871 }
3872 }
3873 }
3874
3875 return false;
3876}
3877
Jon Ashburn23d36b12016-02-02 17:47:28 -07003878static void loader_free_dev_ext_table(struct loader_instance *inst) {
Mark Young39389872017-01-19 21:10:49 -07003879 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3880 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].func_name);
3881 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003882 }
Mark Young39389872017-01-19 21:10:49 -07003883 memset(inst->dev_ext_disp_hash, 0, sizeof(inst->dev_ext_disp_hash));
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003884}
3885
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003886static bool loader_add_dev_ext_table(struct loader_instance *inst, uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003887 uint32_t i;
3888 uint32_t idx = *ptr_idx;
Mark Young39389872017-01-19 21:10:49 -07003889 struct loader_dispatch_hash_list *list = &inst->dev_ext_disp_hash[idx].list;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003890
Mark Young39389872017-01-19 21:10:49 -07003891 if (!inst->dev_ext_disp_hash[idx].func_name) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003892 // no entry here at this idx, so use it
3893 assert(list->capacity == 0);
Mark Young39389872017-01-19 21:10:49 -07003894 inst->dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003895 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003896 if (inst->dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003897 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3898 "loader_add_dev_ext_table: Failed to allocate memory "
3899 "for func_name %s",
Mark Youngb6399312017-01-10 14:22:15 -07003900 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003901 return false;
3902 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003903 strncpy(inst->dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003904 return true;
3905 }
3906
3907 // check for enough capacity
3908 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003909 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003910 if (list->index == NULL) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06003911 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_add_dev_ext_table: Failed to allocate memory for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003912 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003913 return false;
3914 }
3915 list->capacity = 8 * sizeof(*(list->index));
3916 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06003917 void *new_ptr = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3918 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3919 if (NULL == new_ptr) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003920 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngbb3a29c2017-05-19 12:29:43 -06003921 "loader_add_dev_ext_table: Failed to reallocate memory for list index", funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003922 return false;
3923 }
Mark Youngbb3a29c2017-05-19 12:29:43 -06003924 list->index = new_ptr;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003925 list->capacity *= 2;
3926 }
3927
Jon Ashburn23d36b12016-02-02 17:47:28 -07003928 // find an unused index in the hash table and use it
Mark Young39389872017-01-19 21:10:49 -07003929 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003930 do {
Mark Young39389872017-01-19 21:10:49 -07003931 if (!inst->dev_ext_disp_hash[i].func_name) {
3932 assert(inst->dev_ext_disp_hash[i].list.capacity == 0);
3933 inst->dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003934 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003935 if (inst->dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003936 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3937 "loader_add_dev_ext_table: Failed to allocate memory "
3938 "for func_name %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003939 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003940 return false;
3941 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003942 strncpy(inst->dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003943 list->index[list->count] = i;
3944 list->count++;
3945 *ptr_idx = i;
3946 return true;
3947 }
Mark Young39389872017-01-19 21:10:49 -07003948 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003949 } while (i != idx);
3950
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003951 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3952 "loader_add_dev_ext_table: Could not insert into hash table; is "
3953 "it full?");
Mark Youngb6399312017-01-10 14:22:15 -07003954
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003955 return false;
3956}
3957
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003958static bool loader_name_in_dev_ext_table(struct loader_instance *inst, uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003959 uint32_t alt_idx;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003960 if (inst->dev_ext_disp_hash[*idx].func_name && !strcmp(inst->dev_ext_disp_hash[*idx].func_name, funcName)) return true;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003961
3962 // funcName wasn't at the primary spot in the hash table
3963 // search the list of secondary locations (shallow search, not deep search)
Mark Young39389872017-01-19 21:10:49 -07003964 for (uint32_t i = 0; i < inst->dev_ext_disp_hash[*idx].list.count; i++) {
3965 alt_idx = inst->dev_ext_disp_hash[*idx].list.index[i];
3966 if (!strcmp(inst->dev_ext_disp_hash[*idx].func_name, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003967 *idx = alt_idx;
3968 return true;
3969 }
3970 }
3971
3972 return false;
3973}
3974
Mark Young0f183a82017-02-28 09:58:04 -07003975// This function returns generic trampoline code address for unknown entry
3976// points.
3977// Presumably, these unknown entry points (as given by funcName) are device
3978// extension entrypoints. A hash table is used to keep a list of unknown entry
3979// points and their mapping to the device extension dispatch table
3980// (struct loader_dev_ext_dispatch_table).
3981// \returns
3982// For a given entry point string (funcName), if an existing mapping is found
3983// the
3984// trampoline address for that mapping is returned. Otherwise, this unknown
3985// entry point
3986// has not been seen yet. Next check if a layer or ICD supports it. If so then
3987// a
3988// new entry in the hash table is initialized and that trampoline address for
3989// the new entry is returned. Null is returned if the hash table is full or
3990// if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003991void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003992 uint32_t idx;
3993 uint32_t seed = 0;
3994
Mark Young39389872017-01-19 21:10:49 -07003995 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003996
3997 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3998 // found funcName already in hash
3999 return loader_get_dev_ext_trampoline(idx);
4000
4001 // Check if funcName is supported in either ICDs or a layer library
Mark Young39389872017-01-19 21:10:49 -07004002 if (!loader_check_icds_for_dev_ext_address(inst, funcName) &&
Lenny Komowb1685e02017-08-29 16:08:39 -06004003 !loader_check_layer_list_for_dev_ext_address(&inst->app_activated_layer_list, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004004 // if support found in layers continue on
4005 return NULL;
4006 }
4007
4008 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
4009 // successfully added new table entry
Mark Youngdee312c2017-03-08 13:38:35 -07004010 // init any dev dispatch table entries as needed
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004011 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
4012 return loader_get_dev_ext_trampoline(idx);
4013 }
4014
4015 return NULL;
4016}
4017
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004018static bool loader_check_icds_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07004019 struct loader_icd_term *icd_term;
4020 icd_term = inst->icd_terms;
4021 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004022 if (icd_term->scanned_icd->interface_version >= MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION &&
4023 icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName))
Mark Young39389872017-01-19 21:10:49 -07004024 // this icd supports funcName
4025 return true;
4026 icd_term = icd_term->next;
4027 }
4028
4029 return false;
4030}
4031
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004032static bool loader_check_layer_list_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young283fe1c2017-05-04 12:16:35 -06004033 struct loader_layer_properties *layer_prop_list = inst->expanded_activated_layer_list.list;
4034 for (uint32_t layer = 0; layer < inst->expanded_activated_layer_list.count; ++layer) {
Mark Young39389872017-01-19 21:10:49 -07004035 // If this layer supports the vk_layerGetPhysicalDeviceProcAddr, then call
4036 // it and see if it returns a valid pointer for this function name.
4037 if (layer_prop_list[layer].interface_version > 1) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004038 const struct loader_layer_functions *const functions = &(layer_prop_list[layer].functions);
Mark Young39389872017-01-19 21:10:49 -07004039 if (NULL != functions->get_physical_device_proc_addr &&
Tony Barbour55bd5392017-05-17 12:17:18 -06004040 NULL != functions->get_physical_device_proc_addr((VkInstance)inst->instance, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07004041 return true;
4042 }
4043 }
4044 }
4045
4046 return false;
4047}
4048
Mark Young39389872017-01-19 21:10:49 -07004049static void loader_free_phys_dev_ext_table(struct loader_instance *inst) {
4050 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004051 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].func_name);
4052 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].list.index);
Mark Young39389872017-01-19 21:10:49 -07004053 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004054 memset(inst->phys_dev_ext_disp_hash, 0, sizeof(inst->phys_dev_ext_disp_hash));
Mark Young39389872017-01-19 21:10:49 -07004055}
4056
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004057static bool loader_add_phys_dev_ext_table(struct loader_instance *inst, uint32_t *ptr_idx, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07004058 uint32_t i;
4059 uint32_t idx = *ptr_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004060 struct loader_dispatch_hash_list *list = &inst->phys_dev_ext_disp_hash[idx].list;
Mark Young39389872017-01-19 21:10:49 -07004061
4062 if (!inst->phys_dev_ext_disp_hash[idx].func_name) {
4063 // no entry here at this idx, so use it
4064 assert(list->capacity == 0);
4065 inst->phys_dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004066 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07004067 if (inst->phys_dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004068 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4069 "loader_add_phys_dev_ext_table() can't allocate memory for "
4070 "func_name");
Mark Young39389872017-01-19 21:10:49 -07004071 return false;
4072 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004073 strncpy(inst->phys_dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07004074 return true;
4075 }
4076
4077 // check for enough capacity
4078 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004079 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07004080 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004081 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_add_phys_dev_ext_table() can't allocate list memory");
Mark Young39389872017-01-19 21:10:49 -07004082 return false;
4083 }
4084 list->capacity = 8 * sizeof(*(list->index));
4085 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Youngbb3a29c2017-05-19 12:29:43 -06004086 void *new_ptr = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
4087 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4088 if (NULL == new_ptr) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004089 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_add_phys_dev_ext_table() can't reallocate list memory");
Mark Young39389872017-01-19 21:10:49 -07004090 return false;
4091 }
Mark Youngbb3a29c2017-05-19 12:29:43 -06004092 list->index = new_ptr;
Mark Young39389872017-01-19 21:10:49 -07004093 list->capacity *= 2;
4094 }
4095
4096 // find an unused index in the hash table and use it
4097 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
4098 do {
4099 if (!inst->phys_dev_ext_disp_hash[i].func_name) {
4100 assert(inst->phys_dev_ext_disp_hash[i].list.capacity == 0);
4101 inst->phys_dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004102 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07004103 if (inst->phys_dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004104 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngdee312c2017-03-08 13:38:35 -07004105 "loader_add_dev_ext_table() can't reallocate "
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004106 "func_name memory");
Mark Young39389872017-01-19 21:10:49 -07004107 return false;
4108 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004109 strncpy(inst->phys_dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07004110 list->index[list->count] = i;
4111 list->count++;
4112 *ptr_idx = i;
4113 return true;
4114 }
4115 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
4116 } while (i != idx);
4117
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004118 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4119 "loader_add_phys_dev_ext_table() couldn't insert into hash table; is "
4120 "it full?");
Mark Young39389872017-01-19 21:10:49 -07004121 return false;
4122}
4123
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004124static bool loader_name_in_phys_dev_ext_table(struct loader_instance *inst, uint32_t *idx, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07004125 uint32_t alt_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004126 if (inst->phys_dev_ext_disp_hash[*idx].func_name && !strcmp(inst->phys_dev_ext_disp_hash[*idx].func_name, funcName))
Mark Young39389872017-01-19 21:10:49 -07004127 return true;
4128
4129 // funcName wasn't at the primary spot in the hash table
4130 // search the list of secondary locations (shallow search, not deep search)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004131 for (uint32_t i = 0; i < inst->phys_dev_ext_disp_hash[*idx].list.count; i++) {
Mark Young39389872017-01-19 21:10:49 -07004132 alt_idx = inst->phys_dev_ext_disp_hash[*idx].list.index[i];
4133 if (!strcmp(inst->phys_dev_ext_disp_hash[*idx].func_name, funcName)) {
4134 *idx = alt_idx;
4135 return true;
4136 }
4137 }
4138
4139 return false;
4140}
4141
4142// This function returns a generic trampoline and/or terminator function
4143// address for any unknown physical device extension commands. A hash
4144// table is used to keep a list of unknown entry points and their
4145// mapping to the physical device extension dispatch table (struct
4146// loader_phys_dev_ext_dispatch_table).
4147// For a given entry point string (funcName), if an existing mapping is
4148// found, then the trampoline address for that mapping is returned in
4149// tramp_addr (if it is not NULL) and the terminator address for that
4150// mapping is returned in term_addr (if it is not NULL). Otherwise,
4151// this unknown entry point has not been seen yet.
4152// If it has not been seen before, and perform_checking is 'true',
4153// check if a layer or and ICD supports it. If so then a new entry in
4154// the hash table is initialized and the trampoline and/or terminator
4155// addresses are returned.
4156// Null is returned if the hash table is full or if no discovered layer or
4157// ICD returns a non-NULL GetProcAddr for it.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004158bool loader_phys_dev_ext_gpa(struct loader_instance *inst, const char *funcName, bool perform_checking, void **tramp_addr,
Mark Young39389872017-01-19 21:10:49 -07004159 void **term_addr) {
4160 uint32_t idx;
4161 uint32_t seed = 0;
4162 bool success = false;
4163
4164 if (inst == NULL) {
4165 goto out;
4166 }
4167
4168 if (NULL != tramp_addr) {
4169 *tramp_addr = NULL;
4170 }
4171 if (NULL != term_addr) {
4172 *term_addr = NULL;
4173 }
4174
4175 // We should always check to see if any ICD supports it.
4176 if (!loader_check_icds_for_phys_dev_ext_address(inst, funcName)) {
4177 // If we're not checking layers, or we are and it's not in a layer, just
4178 // return
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004179 if (!perform_checking || !loader_check_layer_list_for_phys_dev_ext_address(inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07004180 goto out;
4181 }
4182 }
4183
4184 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004185 if (perform_checking && !loader_name_in_phys_dev_ext_table(inst, &idx, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07004186 uint32_t i;
4187 bool added = false;
4188
4189 // Only need to add first one to get index in Instance. Others will use
4190 // the same index.
4191 if (!added && loader_add_phys_dev_ext_table(inst, &idx, funcName)) {
4192 added = true;
4193 }
4194
4195 // Setup the ICD function pointers
4196 struct loader_icd_term *icd_term = inst->icd_terms;
4197 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004198 if (MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION <= icd_term->scanned_icd->interface_version &&
Mark Young39389872017-01-19 21:10:49 -07004199 NULL != icd_term->scanned_icd->GetPhysicalDeviceProcAddr) {
4200 icd_term->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004201 (PFN_PhysDevExt)icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName);
Mark Young39389872017-01-19 21:10:49 -07004202
4203 // Make sure we set the instance dispatch to point to the
4204 // loader's terminator now since we can at least handle it
4205 // in one ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004206 inst->disp->phys_dev_ext[idx] = loader_get_phys_dev_ext_termin(idx);
Mark Young39389872017-01-19 21:10:49 -07004207 } else {
4208 icd_term->phys_dev_ext[idx] = NULL;
4209 }
4210
4211 icd_term = icd_term->next;
4212 }
4213
4214 // Now, search for the first layer attached and query using it to get
4215 // the first entry point.
Mark Young283fe1c2017-05-04 12:16:35 -06004216 for (i = 0; i < inst->expanded_activated_layer_list.count; i++) {
4217 struct loader_layer_properties *layer_prop = &inst->expanded_activated_layer_list.list[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004218 if (layer_prop->interface_version > 1 && NULL != layer_prop->functions.get_physical_device_proc_addr) {
Mark Young39389872017-01-19 21:10:49 -07004219 inst->disp->phys_dev_ext[idx] =
Tony Barbour55bd5392017-05-17 12:17:18 -06004220 (PFN_PhysDevExt)layer_prop->functions.get_physical_device_proc_addr((VkInstance)inst->instance, funcName);
Mark Young39389872017-01-19 21:10:49 -07004221 if (NULL != inst->disp->phys_dev_ext[idx]) {
4222 break;
4223 }
4224 }
4225 }
4226 }
4227
4228 if (NULL != tramp_addr) {
4229 *tramp_addr = loader_get_phys_dev_ext_tramp(idx);
4230 }
4231
4232 if (NULL != term_addr) {
4233 *term_addr = loader_get_phys_dev_ext_termin(idx);
4234 }
4235
4236 success = true;
4237
4238out:
4239 return success;
4240}
4241
Jon Ashburn23d36b12016-02-02 17:47:28 -07004242struct loader_instance *loader_get_instance(const VkInstance instance) {
Mark Young0f183a82017-02-28 09:58:04 -07004243 // look up the loader_instance in our list by comparing dispatch tables, as
4244 // there is no guarantee the instance is still a loader_instance* after any
4245 // layers which wrap the instance object.
Jon Ashburne0e64572015-09-30 12:56:42 -06004246 const VkLayerInstanceDispatchTable *disp;
4247 struct loader_instance *ptr_instance = NULL;
Mark Young39389872017-01-19 21:10:49 -07004248 disp = loader_get_instance_layer_dispatch(instance);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004249 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
Mark Young39389872017-01-19 21:10:49 -07004250 if (&inst->disp->layer_inst_disp == disp) {
Jon Ashburne0e64572015-09-30 12:56:42 -06004251 ptr_instance = inst;
4252 break;
4253 }
4254 }
4255 return ptr_instance;
4256}
4257
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004258static loader_platform_dl_handle loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
4259 struct loader_layer_properties *prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004260 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) == NULL) {
Mark Youngb986b5f2017-05-16 09:48:55 -06004261 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004262 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004263 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Loading layer library %s", prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004264 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004265
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004266 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004267}
4268
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004269static void loader_close_layer_lib(const struct loader_instance *inst, struct loader_layer_properties *prop) {
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004270 if (prop->lib_handle) {
4271 loader_platform_close_library(prop->lib_handle);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004272 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Unloading layer library %s", prop->lib_name);
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004273 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004274 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004275}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004276
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004277void loader_deactivate_layers(const struct loader_instance *instance, struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004278 struct loader_layer_list *list) {
Mark Young0f183a82017-02-28 09:58:04 -07004279 // Delete instance list of enabled layers and close any layer libraries
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004280 for (uint32_t i = 0; i < list->count; i++) {
4281 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07004282
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004283 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004284 }
Mark Young0ad83132016-06-30 13:02:42 -06004285 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07004286}
4287
Mark Young0f183a82017-02-28 09:58:04 -07004288// Go through the search_list and find any layers which match type. If layer
4289// type match is found in then add it to ext_list.
Mark Youngf2079b92017-05-02 10:49:46 -06004290static void loader_add_implicit_layers(const struct loader_instance *inst, struct loader_layer_list *target_list,
Mark Young283fe1c2017-05-04 12:16:35 -06004291 struct loader_layer_list *expanded_target_list,
Mark Youngf2079b92017-05-02 10:49:46 -06004292 const struct loader_layer_list *source_list) {
4293 for (uint32_t src_layer = 0; src_layer < source_list->count; src_layer++) {
4294 const struct loader_layer_properties *prop = &source_list->list[src_layer];
4295 if (0 == (prop->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER)) {
Mark Young283fe1c2017-05-04 12:16:35 -06004296 loader_add_implicit_layer(inst, prop, target_list, expanded_target_list, source_list);
Jon Ashburn0c26e712015-07-02 16:10:32 -06004297 }
4298 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06004299}
4300
Mark Youngf2079b92017-05-02 10:49:46 -06004301// Get the layer name(s) from the env_name environment variable. If layer is found in
4302// search_list then add it to layer_list. But only add it to layer_list if type_flags matches.
4303static void loader_add_env_layers(struct loader_instance *inst, const enum layer_type_flags type_flags, const char *env_name,
Mark Young283fe1c2017-05-04 12:16:35 -06004304 struct loader_layer_list *target_list, struct loader_layer_list *expanded_target_list,
4305 const struct loader_layer_list *source_list) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06004306 char *next, *name;
Mark Youngf2079b92017-05-02 10:49:46 -06004307 char *layer_env = loader_secure_getenv(env_name, inst);
4308 if (layer_env == NULL) {
4309 goto out;
Ian Elliott4470a302015-02-17 10:33:47 -07004310 }
Mark Youngf2079b92017-05-02 10:49:46 -06004311 name = loader_stack_alloc(strlen(layer_env) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06004312 if (name == NULL) {
Mark Youngf2079b92017-05-02 10:49:46 -06004313 goto out;
Ian Elliott4470a302015-02-17 10:33:47 -07004314 }
Mark Youngf2079b92017-05-02 10:49:46 -06004315 strcpy(name, layer_env);
Jon Ashburn38a497f2016-01-04 14:01:38 -07004316
Jon Ashburn23d36b12016-02-02 17:47:28 -07004317 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06004318 next = loader_get_next_path(name);
Mark Young283fe1c2017-05-04 12:16:35 -06004319 loader_find_layer_name_add_list(inst, name, type_flags, source_list, target_list, expanded_target_list);
Jon Ashburneb6d5682015-07-02 14:10:53 -06004320 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004321 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004322
Mark Youngf2079b92017-05-02 10:49:46 -06004323out:
4324
4325 if (layer_env != NULL) {
4326 loader_free_getenv(layer_env, inst);
4327 }
4328
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004329 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004330}
4331
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004332VkResult loader_enable_instance_layers(struct loader_instance *inst, const VkInstanceCreateInfo *pCreateInfo,
4333 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004334 VkResult err;
4335
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004336 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004337
Mark Young283fe1c2017-05-04 12:16:35 -06004338 if (!loader_init_layer_list(inst, &inst->app_activated_layer_list)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004339 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4340 "loader_enable_instance_layers: Failed to initialize"
Mark Young283fe1c2017-05-04 12:16:35 -06004341 " application version of the layer list");
4342 return VK_ERROR_OUT_OF_HOST_MEMORY;
4343 }
4344
4345 if (!loader_init_layer_list(inst, &inst->expanded_activated_layer_list)) {
4346 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4347 "loader_enable_instance_layers: Failed to initialize"
4348 " expanded version of the layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004349 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06004350 }
4351
Mark Young0f183a82017-02-28 09:58:04 -07004352 // Add any implicit layers first
Mark Young283fe1c2017-05-04 12:16:35 -06004353 loader_add_implicit_layers(inst, &inst->app_activated_layer_list, &inst->expanded_activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06004354
Mark Young0f183a82017-02-28 09:58:04 -07004355 // Add any layers specified via environment variable next
Mark Young283fe1c2017-05-04 12:16:35 -06004356 loader_add_env_layers(inst, VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER, "VK_INSTANCE_LAYERS", &inst->app_activated_layer_list,
4357 &inst->expanded_activated_layer_list, instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06004358
Mark Young0f183a82017-02-28 09:58:04 -07004359 // Add layers specified by the application
Mark Young283fe1c2017-05-04 12:16:35 -06004360 err = loader_add_layer_names_to_list(inst, &inst->app_activated_layer_list, &inst->expanded_activated_layer_list,
4361 pCreateInfo->enabledLayerCount, pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004362
4363 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004364}
4365
Mark Young39389872017-01-19 21:10:49 -07004366// Determine the layer interface version to use.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004367bool loader_get_layer_interface_version(PFN_vkNegotiateLoaderLayerInterfaceVersion fp_negotiate_layer_version,
4368 VkNegotiateLayerInterface *interface_struct) {
Mark Young39389872017-01-19 21:10:49 -07004369 memset(interface_struct, 0, sizeof(VkNegotiateLayerInterface));
Mark Young21aa6d62017-03-29 13:39:27 -06004370 interface_struct->sType = LAYER_NEGOTIATE_INTERFACE_STRUCT;
Mark Young39389872017-01-19 21:10:49 -07004371 interface_struct->loaderLayerInterfaceVersion = 1;
4372
4373 if (fp_negotiate_layer_version != NULL) {
4374 // Layer supports the negotiation API, so call it with the loader's
4375 // latest version supported
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004376 interface_struct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
Mark Young39389872017-01-19 21:10:49 -07004377 VkResult result = fp_negotiate_layer_version(interface_struct);
4378
4379 if (result != VK_SUCCESS) {
4380 // Layer no longer supports the loader's latest interface version so
4381 // fail loading the Layer
4382 return false;
4383 }
4384 }
4385
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004386 if (interface_struct->loaderLayerInterfaceVersion < MIN_SUPPORTED_LOADER_LAYER_INTERFACE_VERSION) {
Mark Young39389872017-01-19 21:10:49 -07004387 // Loader no longer supports the layer's latest interface version so
4388 // fail loading the layer
4389 return false;
4390 }
4391
4392 return true;
4393}
4394
Mark Young0f183a82017-02-28 09:58:04 -07004395// Given the list of layers to activate in the loader_instance
4396// structure. This function will add a VkLayerInstanceCreateInfo
4397// structure to the VkInstanceCreateInfo.pNext pointer.
4398// Each activated layer will have it's own VkLayerInstanceLink
4399// structure that tells the layer what Get*ProcAddr to call to
4400// get function pointers to the next layer down.
4401// Once the chain info has been created this function will
4402// execute the CreateInstance call chain. Each layer will
4403// then have an opportunity in it's CreateInstance function
4404// to setup it's dispatch table when the lower layer returns
4405// successfully.
4406// Each layer can wrap or not-wrap the returned VkInstance object
4407// as it sees fit.
4408// The instance chain is terminated by a loader function
4409// that will call CreateInstance on all available ICD's and
4410// cache those VkInstance objects for future use.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004411VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
4412 struct loader_instance *inst, VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004413 uint32_t activated_layers = 0;
4414 VkLayerInstanceCreateInfo chain_info;
4415 VkLayerInstanceLink *layer_instance_link_info = NULL;
4416 VkInstanceCreateInfo loader_create_info;
4417 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004418
Mark Young39389872017-01-19 21:10:49 -07004419 PFN_vkGetInstanceProcAddr next_gipa = loader_gpa_instance_internal;
4420 PFN_vkGetInstanceProcAddr cur_gipa = loader_gpa_instance_internal;
4421 PFN_GetPhysicalDeviceProcAddr next_gpdpa = loader_gpdpa_instance_internal;
4422 PFN_GetPhysicalDeviceProcAddr cur_gpdpa = loader_gpdpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004423
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004424 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06004425
Mark Young283fe1c2017-05-04 12:16:35 -06004426 if (inst->expanded_activated_layer_list.count > 0) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004427 chain_info.u.pLayerInfo = NULL;
4428 chain_info.pNext = pCreateInfo->pNext;
4429 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
4430 chain_info.function = VK_LAYER_LINK_INFO;
4431 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004432
Mark Young283fe1c2017-05-04 12:16:35 -06004433 layer_instance_link_info = loader_stack_alloc(sizeof(VkLayerInstanceLink) * inst->expanded_activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004434 if (!layer_instance_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004435 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4436 "loader_create_instance_chain: Failed to alloc Instance"
4437 " objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004438 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004439 }
4440
Mark Young39389872017-01-19 21:10:49 -07004441 // Create instance chain of enabled layers
Mark Young283fe1c2017-05-04 12:16:35 -06004442 for (int32_t i = inst->expanded_activated_layer_list.count - 1; i >= 0; i--) {
4443 struct loader_layer_properties *layer_prop = &inst->expanded_activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004444 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004445
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004446 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004447 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004448 continue;
Mark Young39389872017-01-19 21:10:49 -07004449 }
4450
4451 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004452 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
Mark Young39389872017-01-19 21:10:49 -07004453 bool functions_in_interface = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004454 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4455 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4456 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004457 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004458 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4459 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004460 }
4461
4462 // If we can negotiate an interface version, then we can also
4463 // get everything we need from the one function call, so try
4464 // that first, and see if we can get all the function pointers
4465 // necessary from that one call.
4466 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004467 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004468
4469 VkNegotiateLayerInterface interface_struct;
4470
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004471 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Youngdee312c2017-03-08 13:38:35 -07004472 // Go ahead and set the properties version to the
Mark Young39389872017-01-19 21:10:49 -07004473 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004474 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004475
4476 // If the interface is 2 or newer, we have access to the
4477 // new GetPhysicalDeviceProcAddr function, so grab it,
4478 // and the other necessary functions, from the
4479 // structure.
4480 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4481 cur_gipa = interface_struct.pfnGetInstanceProcAddr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004482 cur_gpdpa = interface_struct.pfnGetPhysicalDeviceProcAddr;
Mark Young39389872017-01-19 21:10:49 -07004483 if (cur_gipa != NULL) {
4484 // We've set the functions, so make sure we
4485 // don't do the unnecessary calls later.
4486 functions_in_interface = true;
4487 }
4488 }
4489 }
4490 }
4491
4492 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004493 if ((cur_gipa = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004494 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004495 cur_gipa =
4496 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
4497 layer_prop->functions.get_instance_proc_addr = cur_gipa;
Mark Young39389872017-01-19 21:10:49 -07004498 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004499 cur_gipa = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle,
4500 layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004501 }
4502
4503 if (NULL == cur_gipa) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004504 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4505 "loader_create_instance_chain: Failed to"
4506 " find \'vkGetInstanceProcAddr\' in "
4507 "layer %s",
Mark Young39389872017-01-19 21:10:49 -07004508 layer_prop->lib_name);
4509 continue;
4510 }
4511 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004512 }
4513 }
4514
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004515 layer_instance_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4516 layer_instance_link_info[activated_layers].pfnNextGetInstanceProcAddr = next_gipa;
4517 layer_instance_link_info[activated_layers].pfnNextGetPhysicalDeviceProcAddr = next_gpdpa;
Mark Young39389872017-01-19 21:10:49 -07004518 next_gipa = cur_gipa;
4519 if (layer_prop->interface_version > 1 && cur_gpdpa != NULL) {
4520 layer_prop->functions.get_physical_device_proc_addr = cur_gpdpa;
4521 next_gpdpa = cur_gpdpa;
4522 }
4523
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004524 chain_info.u.pLayerInfo = &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004525
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004526 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert instance layer %s (%s)", layer_prop->info.layerName,
4527 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004528
4529 activated_layers++;
4530 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06004531 }
4532
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004533 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)next_gipa(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004534 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06004535 VkLayerInstanceCreateInfo create_info_disp;
4536
Jon Ashburncc407a22016-04-15 09:25:03 -06004537 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004538 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06004539
4540 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
4541
4542 create_info_disp.pNext = loader_create_info.pNext;
4543 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004544 res = fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004545 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004546 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4547 "loader_create_instance_chain: Failed to find "
4548 "\'vkCreateInstance\'");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004549 // Couldn't find CreateInstance function!
4550 res = VK_ERROR_INITIALIZATION_FAILED;
4551 }
4552
Mark Young39389872017-01-19 21:10:49 -07004553 if (res == VK_SUCCESS) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004554 loader_init_instance_core_dispatch_table(&inst->disp->layer_inst_disp, next_gipa, *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07004555 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004556 }
4557
4558 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004559}
4560
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004561void loader_activate_instance_layer_extensions(struct loader_instance *inst, VkInstance created_inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004562 loader_init_instance_extension_dispatch_table(&inst->disp->layer_inst_disp, inst->disp->layer_inst_disp.GetInstanceProcAddr,
4563 created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004564}
4565
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004566VkResult loader_create_device_chain(const struct loader_physical_device_tramp *pd, const VkDeviceCreateInfo *pCreateInfo,
4567 const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst,
4568 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004569 uint32_t activated_layers = 0;
4570 VkLayerDeviceLink *layer_device_link_info;
4571 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004572 VkDeviceCreateInfo loader_create_info;
4573 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004574
Jamie Madill35127872017-03-15 16:17:46 -04004575 PFN_vkGetDeviceProcAddr fpGDPA = NULL, nextGDPA = loader_gpa_device_internal;
4576 PFN_vkGetInstanceProcAddr fpGIPA = NULL, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004577
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004578 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
4579
Mark Young0f183a82017-02-28 09:58:04 -07004580 // Before we continue, we need to find out if the KHX_device_group extension is in the enabled list. If it is, we then
4581 // need to look for the corresponding VkDeviceGroupDeviceCreateInfoKHX struct in the device list. This is because we
4582 // need to replace all the incoming physical device values (which are really loader trampoline physical device values)
4583 // with the layer/ICD version.
4584 if (inst->enabled_known_extensions.khx_device_group_creation == 1) {
4585 struct VkStructureHeader *pNext = (struct VkStructureHeader *)loader_create_info.pNext;
4586 struct VkStructureHeader *pPrev = (struct VkStructureHeader *)&loader_create_info;
4587 while (NULL != pNext) {
4588 if (VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO_KHX == pNext->sType) {
4589 VkDeviceGroupDeviceCreateInfoKHX *cur_struct = (VkDeviceGroupDeviceCreateInfoKHX *)pNext;
4590 if (0 < cur_struct->physicalDeviceCount && NULL != cur_struct->pPhysicalDevices) {
4591 VkDeviceGroupDeviceCreateInfoKHX *temp_struct = loader_stack_alloc(sizeof(VkDeviceGroupDeviceCreateInfoKHX));
4592 VkPhysicalDevice *phys_dev_array = NULL;
4593 if (NULL == temp_struct) {
4594 return VK_ERROR_OUT_OF_HOST_MEMORY;
4595 }
4596 memcpy(temp_struct, cur_struct, sizeof(VkDeviceGroupDeviceCreateInfoKHX));
4597 phys_dev_array = loader_stack_alloc(sizeof(VkPhysicalDevice) * cur_struct->physicalDeviceCount);
4598 if (NULL == phys_dev_array) {
4599 return VK_ERROR_OUT_OF_HOST_MEMORY;
4600 }
4601
4602 // Before calling down, replace the incoming physical device values (which are really loader trampoline
4603 // physical devices) with the next layer (or possibly even the terminator) physical device values.
4604 struct loader_physical_device_tramp *cur_tramp;
4605 for (uint32_t phys_dev = 0; phys_dev < cur_struct->physicalDeviceCount; phys_dev++) {
4606 cur_tramp = (struct loader_physical_device_tramp *)cur_struct->pPhysicalDevices[phys_dev];
4607 phys_dev_array[phys_dev] = cur_tramp->phys_dev;
4608 }
4609 temp_struct->pPhysicalDevices = phys_dev_array;
4610
4611 // Replace the old struct in the pNext chain with this one.
4612 pPrev->pNext = (const void *)temp_struct;
4613 pNext = (struct VkStructureHeader *)(temp_struct);
4614 }
4615 break;
4616 }
4617
4618 pPrev = pNext;
4619 pNext = (struct VkStructureHeader *)(pPrev->pNext);
4620 }
4621 }
4622
Mark Young283fe1c2017-05-04 12:16:35 -06004623 layer_device_link_info = loader_stack_alloc(sizeof(VkLayerDeviceLink) * dev->expanded_activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004624 if (!layer_device_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004625 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4626 "loader_create_device_chain: Failed to alloc Device objects"
4627 " for layer. Skipping Layer.");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004628 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06004629 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004630
Mark Young283fe1c2017-05-04 12:16:35 -06004631 if (dev->expanded_activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06004632 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
4633 chain_info.function = VK_LAYER_LINK_INFO;
4634 chain_info.u.pLayerInfo = NULL;
Mark Young39389872017-01-19 21:10:49 -07004635 chain_info.pNext = loader_create_info.pNext;
Jon Ashburn72690f22016-03-29 12:52:13 -06004636 loader_create_info.pNext = &chain_info;
4637
Mark Young39389872017-01-19 21:10:49 -07004638 // Create instance chain of enabled layers
Mark Young283fe1c2017-05-04 12:16:35 -06004639 for (int32_t i = dev->expanded_activated_layer_list.count - 1; i >= 0; i--) {
4640 struct loader_layer_properties *layer_prop = &dev->expanded_activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004641 loader_platform_dl_handle lib_handle;
Mark Young39389872017-01-19 21:10:49 -07004642 bool functions_in_interface = false;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004643
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004644 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004645 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004646 continue;
Jon Ashburn21c21ee2015-09-09 11:29:24 -06004647 }
Mark Young39389872017-01-19 21:10:49 -07004648
Mark Young0f183a82017-02-28 09:58:04 -07004649 // If we can negotiate an interface version, then we can also get everything we need from the one function
4650 // call, so try that first, and see if we can get all the function pointers necessary from that one call.
Mark Young39389872017-01-19 21:10:49 -07004651 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004652 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
4653 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4654 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4655 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004656 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004657 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4658 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004659 }
4660
4661 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004662 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004663
4664 VkNegotiateLayerInterface interface_struct;
4665
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004666 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Youngdee312c2017-03-08 13:38:35 -07004667 // Go ahead and set the properties version to the correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004668 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004669
Mark Young0f183a82017-02-28 09:58:04 -07004670 // If the interface is 2 or newer, we have access to the new GetPhysicalDeviceProcAddr
4671 // function, so grab it, and the other necessary functions, from the structure.
Mark Young39389872017-01-19 21:10:49 -07004672 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4673 fpGIPA = interface_struct.pfnGetInstanceProcAddr;
4674 fpGDPA = interface_struct.pfnGetDeviceProcAddr;
4675 if (fpGIPA != NULL && fpGDPA) {
4676 // We've set the functions, so make sure we
4677 // don't do the unnecessary calls later.
4678 functions_in_interface = true;
4679 }
4680 }
4681 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004682 }
4683 }
4684
Mark Young39389872017-01-19 21:10:49 -07004685 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004686 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004687 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004688 fpGIPA = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004689 layer_prop->functions.get_instance_proc_addr = fpGIPA;
4690 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004691 fpGIPA =
4692 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004693 if (!fpGIPA) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004694 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4695 "loader_create_device_chain: Failed to find "
4696 "\'vkGetInstanceProcAddr\' in layer %s. Skipping"
4697 " layer.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004698 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004699 continue;
4700 }
4701 }
4702 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
4703 if (strlen(layer_prop->functions.str_gdpa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004704 fpGDPA = (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004705 layer_prop->functions.get_device_proc_addr = fpGDPA;
4706 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004707 fpGDPA =
4708 (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gdpa);
Mark Young39389872017-01-19 21:10:49 -07004709 if (!fpGDPA) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004710 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Failed to find vkGetDeviceProcAddr in layer %s",
4711 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004712 continue;
4713 }
4714 }
4715 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004716 layer_device_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4717 layer_device_link_info[activated_layers].pfnNextGetInstanceProcAddr = nextGIPA;
4718 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr = nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004719 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
4720 nextGIPA = fpGIPA;
4721 nextGDPA = fpGDPA;
4722
Lenny Komow7ddd4322017-10-16 15:03:37 -06004723 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Inserted device layer %s (%s)", layer_prop->info.layerName,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004724 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004725
4726 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06004727 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004728 }
4729
Jon Ashburncc407a22016-04-15 09:25:03 -06004730 VkDevice created_device = (VkDevice)dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004731 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004732 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06004733 VkLayerDeviceCreateInfo create_info_disp;
4734
Jon Ashburncc407a22016-04-15 09:25:03 -06004735 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004736 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
4737
4738 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
4739
4740 create_info_disp.pNext = loader_create_info.pNext;
4741 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004742 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator, &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06004743 if (res != VK_SUCCESS) {
4744 return res;
4745 }
Mark Young65cb3662016-11-07 13:27:02 -07004746 dev->chain_device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004747 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004748 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4749 "loader_create_device_chain: Failed to find \'vkCreateDevice\' "
4750 "in layer %s");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004751 // Couldn't find CreateDevice function!
4752 return VK_ERROR_INITIALIZATION_FAILED;
4753 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004754
Mark Young65cb3662016-11-07 13:27:02 -07004755 // Initialize device dispatch table
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004756 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA, dev->chain_device);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004757
4758 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06004759}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004760
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004761VkResult loader_validate_layers(const struct loader_instance *inst, const uint32_t layer_count,
4762 const char *const *ppEnabledLayerNames, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004763 struct loader_layer_properties *prop;
4764
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004765 for (uint32_t i = 0; i < layer_count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004766 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004767 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004768 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4769 "loader_validate_layers: Device ppEnabledLayerNames "
4770 "contains string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004771 return VK_ERROR_LAYER_NOT_PRESENT;
4772 }
4773
Jon Ashburn23d36b12016-02-02 17:47:28 -07004774 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Mark Youngf2079b92017-05-02 10:49:46 -06004775 if (NULL == prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004776 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngc8b807a2017-07-14 17:11:31 -06004777 "loader_validate_layers: Layer %d does not exist in the list of available layers", i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004778 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004779 }
4780 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004781 return VK_SUCCESS;
4782}
4783
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004784VkResult loader_validate_instance_extensions(const struct loader_instance *inst, const struct loader_extension_list *icd_exts,
4785 const struct loader_layer_list *instance_layers,
4786 const VkInstanceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004787 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004788 struct loader_layer_properties *layer_prop;
Mark Young48cea0c2017-05-26 14:39:54 -06004789 char *env_value;
4790 bool check_if_known = true;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004791
Jon Ashburnf19916e2016-01-11 13:12:43 -07004792 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004793 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004794 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004795 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young48cea0c2017-05-26 14:39:54 -06004796 "loader_validate_instance_extensions: Instance ppEnabledExtensionNames contains "
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004797 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004798 return VK_ERROR_EXTENSION_NOT_PRESENT;
4799 }
4800
Mark Young48cea0c2017-05-26 14:39:54 -06004801 // Check if a user wants to disable the instance extension filtering behavior
4802 env_value = loader_getenv("VK_LOADER_DISABLE_INST_EXT_FILTER", inst);
4803 if (NULL != env_value && atoi(env_value) != 0) {
4804 check_if_known = false;
Lenny Komow4053b812016-12-29 16:27:28 -07004805 }
Mark Young48cea0c2017-05-26 14:39:54 -06004806 loader_free_getenv(env_value, inst);
Lenny Komow4053b812016-12-29 16:27:28 -07004807
Mark Young48cea0c2017-05-26 14:39:54 -06004808 if (check_if_known) {
4809 // See if the extension is in the list of supported extensions
4810 bool found = false;
4811 for (uint32_t j = 0; LOADER_INSTANCE_EXTENSIONS[j] != NULL; j++) {
4812 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], LOADER_INSTANCE_EXTENSIONS[j]) == 0) {
4813 found = true;
4814 break;
4815 }
4816 }
4817
4818 // If it isn't in the list, return an error
4819 if (!found) {
4820 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4821 "loader_validate_instance_extensions: Extension %s not found in list of known instance extensions.",
4822 pCreateInfo->ppEnabledExtensionNames[i]);
4823 return VK_ERROR_EXTENSION_NOT_PRESENT;
4824 }
Lenny Komow4053b812016-12-29 16:27:28 -07004825 }
4826
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004827 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004828
4829 if (extension_prop) {
4830 continue;
4831 }
4832
4833 extension_prop = NULL;
4834
Mark Young0f183a82017-02-28 09:58:04 -07004835 // Not in global list, search layer extension lists
Jon Ashburnf19916e2016-01-11 13:12:43 -07004836 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004837 layer_prop = loader_get_layer_property(pCreateInfo->ppEnabledLayerNames[j], instance_layers);
Mark Youngf2079b92017-05-02 10:49:46 -06004838 if (NULL == layer_prop) {
Mark Young0f183a82017-02-28 09:58:04 -07004839 // Should NOT get here, loader_validate_layers should have already filtered this case out.
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004840 continue;
4841 }
4842
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004843 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004844 if (extension_prop) {
Mark Young0f183a82017-02-28 09:58:04 -07004845 // Found the extension in one of the layers enabled by the app.
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004846 break;
4847 }
4848 }
4849
4850 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004851 // Didn't find extension name in any of the global layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004852 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young48cea0c2017-05-26 14:39:54 -06004853 "loader_validate_instance_extensions: Instance extension %s not supported by available ICDs or enabled "
4854 "layers.",
4855 pCreateInfo->ppEnabledExtensionNames[i]);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004856 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004857 }
4858 }
4859 return VK_SUCCESS;
4860}
4861
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004862VkResult loader_validate_device_extensions(struct loader_physical_device_tramp *phys_dev,
4863 const struct loader_layer_list *activated_device_layers,
4864 const struct loader_extension_list *icd_exts, const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004865 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004866 struct loader_layer_properties *layer_prop;
4867
Jon Ashburnf19916e2016-01-11 13:12:43 -07004868 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004869 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004870 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004871 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young48cea0c2017-05-26 14:39:54 -06004872 "loader_validate_device_extensions: Device ppEnabledExtensionNames contains "
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004873 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004874 return VK_ERROR_EXTENSION_NOT_PRESENT;
4875 }
4876
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004877 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004878 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004879
4880 if (extension_prop) {
4881 continue;
4882 }
4883
Mark Youngb6399312017-01-10 14:22:15 -07004884 // Not in global list, search activated layer extension lists
Jon Ashburn471f44c2016-01-13 12:51:43 -07004885 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4886 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004887
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004888 extension_prop = get_dev_extension_property(extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004889 if (extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004890 // Found the extension in one of the layers enabled by the app.
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004891 break;
4892 }
4893 }
4894
4895 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004896 // Didn't find extension name in any of the device layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004897 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young48cea0c2017-05-26 14:39:54 -06004898 "loader_validate_device_extensions: Device extension %s not supported by selected physical device "
4899 "or enabled layers.",
4900 pCreateInfo->ppEnabledExtensionNames[i]);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004901 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004902 }
4903 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004904 return VK_SUCCESS;
4905}
4906
Mark Youngb6399312017-01-10 14:22:15 -07004907// Terminator functions for the Instance chain
4908// All named terminator_<Vulakn API name>
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004909VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
4910 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Mark Young0153e0b2016-11-03 14:27:13 -06004911 struct loader_icd_term *icd_term;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004912 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004913 char **filtered_extension_names = NULL;
4914 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004915 VkResult res = VK_SUCCESS;
Mark Young8b4edb52016-11-11 09:31:55 -07004916 bool one_icd_successful = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004917
Jon Ashburncc407a22016-04-15 09:25:03 -06004918 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004919 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4920
Jon Ashburnf19916e2016-01-11 13:12:43 -07004921 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004922 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004923
Mark Youngb6399312017-01-10 14:22:15 -07004924 // NOTE: Need to filter the extensions to only those supported by the ICD.
4925 // No ICD will advertise support for layers. An ICD library could
4926 // support a layer, but it would be independent of the actual ICD,
4927 // just in the same library.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004928 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004929 if (!filtered_extension_names) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004930 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young48cea0c2017-05-26 14:39:54 -06004931 "terminator_CreateInstance: Failed create extension name array for %d extensions",
Mark Youngb6399312017-01-10 14:22:15 -07004932 pCreateInfo->enabledExtensionCount);
Mark Young3a587792016-08-19 15:25:08 -06004933 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4934 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004935 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004936 icd_create_info.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004937
Mark Young0153e0b2016-11-03 14:27:13 -06004938 for (uint32_t i = 0; i < ptr_instance->icd_tramp_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004939 icd_term = loader_icd_add(ptr_instance, &ptr_instance->icd_tramp_list.scanned_list[i]);
Mark Young0153e0b2016-11-03 14:27:13 -06004940 if (NULL == icd_term) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004941 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngc8b807a2017-07-14 17:11:31 -06004942 "terminator_CreateInstance: Failed to add ICD %d to ICD trampoline list.", i);
Mark Young3a587792016-08-19 15:25:08 -06004943 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4944 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004945 }
Mark Young6267ae62017-01-12 12:27:19 -07004946
Lenny Komowa44cc852017-09-12 22:54:21 -06004947 // If any error happens after here, we need to remove the ICD from the list,
4948 // because we've already added it, but haven't validated it
4949
Mark Young0ad83132016-06-30 13:02:42 -06004950 icd_create_info.enabledExtensionCount = 0;
4951 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004952
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004953 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young0f183a82017-02-28 09:58:04 -07004954 // traverse scanned icd list adding non-duplicate extensions to the list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004955 res = loader_init_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004956 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4957 // If out of memory, bail immediately.
4958 goto out;
4959 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004960 // Something bad happened with this ICD, so free it and try the
4961 // next.
Mark Young0153e0b2016-11-03 14:27:13 -06004962 ptr_instance->icd_terms = icd_term->next;
4963 icd_term->next = NULL;
4964 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004965 continue;
4966 }
4967
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004968 res = loader_add_instance_extensions(ptr_instance, icd_term->scanned_icd->EnumerateInstanceExtensionProperties,
4969 icd_term->scanned_icd->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004970 if (VK_SUCCESS != res) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004971 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004972 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4973 // If out of memory, bail immediately.
4974 goto out;
4975 } else {
Mark Young0f183a82017-02-28 09:58:04 -07004976 // Something bad happened with this ICD, so free it and try the next.
Mark Young0153e0b2016-11-03 14:27:13 -06004977 ptr_instance->icd_terms = icd_term->next;
4978 icd_term->next = NULL;
4979 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004980 continue;
4981 }
Mark Young3a587792016-08-19 15:25:08 -06004982 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004983
Mark Young0ad83132016-06-30 13:02:42 -06004984 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004985 prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004986 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004987 filtered_extension_names[icd_create_info.enabledExtensionCount] = (char *)pCreateInfo->ppEnabledExtensionNames[j];
Mark Young0ad83132016-06-30 13:02:42 -06004988 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004989 }
4990 }
Mark Young0ad83132016-06-30 13:02:42 -06004991
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004992 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004993
Mark Young8b4edb52016-11-11 09:31:55 -07004994 VkResult icd_result =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004995 ptr_instance->icd_tramp_list.scanned_list[i].CreateInstance(&icd_create_info, pAllocator, &(icd_term->instance));
Mark Young8b4edb52016-11-11 09:31:55 -07004996 if (VK_ERROR_OUT_OF_HOST_MEMORY == icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004997 // If out of memory, bail immediately.
Mark Young8b4edb52016-11-11 09:31:55 -07004998 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06004999 goto out;
Mark Young8b4edb52016-11-11 09:31:55 -07005000 } else if (VK_SUCCESS != icd_result) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005001 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
5002 "terminator_CreateInstance: Failed to CreateInstance in "
5003 "ICD %d. Skipping ICD.",
Mark Youngb6399312017-01-10 14:22:15 -07005004 i);
Mark Young0153e0b2016-11-03 14:27:13 -06005005 ptr_instance->icd_terms = icd_term->next;
5006 icd_term->next = NULL;
5007 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06005008 continue;
5009 }
Mark Young0ad83132016-06-30 13:02:42 -06005010
Mark Young0f183a82017-02-28 09:58:04 -07005011 if (!loader_icd_init_entries(icd_term, icd_term->instance,
5012 ptr_instance->icd_tramp_list.scanned_list[i].GetInstanceProcAddr)) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005013 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
5014 "terminator_CreateInstance: Failed to CreateInstance and find "
5015 "entrypoints with ICD. Skipping ICD.");
Lenny Komowa44cc852017-09-12 22:54:21 -06005016 ptr_instance->icd_terms = icd_term->next;
5017 icd_term->next = NULL;
5018 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06005019 continue;
Mark Young0ad83132016-06-30 13:02:42 -06005020 }
Mark Young8b4edb52016-11-11 09:31:55 -07005021
5022 // If we made it this far, at least one ICD was successful
5023 one_icd_successful = true;
Jon Ashburn46888392015-01-29 15:45:51 -07005024 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005025
Mark Young0f183a82017-02-28 09:58:04 -07005026 // If no ICDs were added to instance list and res is unchanged from it's initial value, the loader was unable to
5027 // find a suitable ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005028 if (VK_SUCCESS == res && (ptr_instance->icd_terms == NULL || !one_icd_successful)) {
Mark Young3a587792016-08-19 15:25:08 -06005029 res = VK_ERROR_INCOMPATIBLE_DRIVER;
5030 }
5031
5032out:
5033
5034 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06005035 while (NULL != ptr_instance->icd_terms) {
5036 icd_term = ptr_instance->icd_terms;
5037 ptr_instance->icd_terms = icd_term->next;
5038 if (NULL != icd_term->instance) {
Mark Young0f183a82017-02-28 09:58:04 -07005039 icd_term->dispatch.DestroyInstance(icd_term->instance, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06005040 }
Mark Young0153e0b2016-11-03 14:27:13 -06005041 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06005042 }
Ian Elliotteb450762015-02-05 15:19:15 -07005043 }
Jon Ashburn46888392015-01-29 15:45:51 -07005044
Mark Young3a587792016-08-19 15:25:08 -06005045 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005046}
5047
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005048VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06005049 struct loader_instance *ptr_instance = loader_instance(instance);
Karl Schultze2ef9e62017-01-13 14:01:35 -07005050 if (NULL == ptr_instance) {
5051 return;
5052 }
Mark Young0153e0b2016-11-03 14:27:13 -06005053 struct loader_icd_term *icd_terms = ptr_instance->icd_terms;
5054 struct loader_icd_term *next_icd_term;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005055
5056 // Remove this instance from the list of instances:
5057 struct loader_instance *prev = NULL;
5058 struct loader_instance *next = loader.instances;
5059 while (next != NULL) {
5060 if (next == ptr_instance) {
5061 // Remove this instance from the list:
5062 if (prev)
5063 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07005064 else
5065 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005066 break;
5067 }
5068 prev = next;
5069 next = next->next;
5070 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005071
Mark Young0153e0b2016-11-03 14:27:13 -06005072 while (NULL != icd_terms) {
5073 if (icd_terms->instance) {
Mark Young0f183a82017-02-28 09:58:04 -07005074 icd_terms->dispatch.DestroyInstance(icd_terms->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06005075 }
Mark Young0153e0b2016-11-03 14:27:13 -06005076 next_icd_term = icd_terms->next;
5077 icd_terms->instance = VK_NULL_HANDLE;
5078 loader_icd_destroy(ptr_instance, icd_terms, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06005079
Mark Young0153e0b2016-11-03 14:27:13 -06005080 icd_terms = next_icd_term;
Jon Ashburn46888392015-01-29 15:45:51 -07005081 }
Jon Ashburn491cd042016-05-16 14:01:18 -06005082
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005083 loader_delete_layer_properties(ptr_instance, &ptr_instance->instance_layer_list);
Mark Young0153e0b2016-11-03 14:27:13 -06005084 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_tramp_list);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005085 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Mark Young39389872017-01-19 21:10:49 -07005086 if (NULL != ptr_instance->phys_devs_term) {
Mark Young0193d652016-12-28 16:10:10 -07005087 for (uint32_t i = 0; i < ptr_instance->phys_dev_count_term; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005088 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term[i]);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07005089 }
Mark Young0ad83132016-06-30 13:02:42 -06005090 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07005091 }
Mark Youngd66edd52017-03-10 17:31:18 -07005092 if (NULL != ptr_instance->phys_dev_groups_term) {
5093 for (uint32_t i = 0; i < ptr_instance->phys_dev_group_count_term; i++) {
5094 loader_instance_heap_free(ptr_instance, ptr_instance->phys_dev_groups_term[i]);
5095 }
5096 loader_instance_heap_free(ptr_instance, ptr_instance->phys_dev_groups_term);
5097 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07005098 loader_free_dev_ext_table(ptr_instance);
Mark Young39389872017-01-19 21:10:49 -07005099 loader_free_phys_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005100}
5101
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005102VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
5103 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Mark Young0ad83132016-06-30 13:02:42 -06005104 VkResult res = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06005105 struct loader_physical_device_term *phys_dev_term;
5106 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
5107 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07005108
Jon Ashburncc407a22016-04-15 09:25:03 -06005109 struct loader_device *dev = (struct loader_device *)*pDevice;
Mark Young0f183a82017-02-28 09:58:04 -07005110 PFN_vkCreateDevice fpCreateDevice = icd_term->dispatch.CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06005111 struct loader_extension_list icd_exts;
5112
Mark Young65cb3662016-11-07 13:27:02 -07005113 dev->phys_dev_term = phys_dev_term;
5114
Mark Young0ad83132016-06-30 13:02:42 -06005115 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005116
Jon Ashburn1530c342016-02-26 13:14:27 -07005117 if (fpCreateDevice == NULL) {
Mark Young0153e0b2016-11-03 14:27:13 -06005118 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07005119 "terminator_CreateDevice: No vkCreateDevice command exposed "
5120 "by ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06005121 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06005122 res = VK_ERROR_INITIALIZATION_FAILED;
5123 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005124 }
5125
Jon Ashburn1530c342016-02-26 13:14:27 -07005126 VkDeviceCreateInfo localCreateInfo;
5127 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07005128
Mark Youngb6399312017-01-10 14:22:15 -07005129 // NOTE: Need to filter the extensions to only those supported by the ICD.
Mark Younge1ea9012017-03-09 14:17:40 -07005130 // No ICD will advertise support for layers. An ICD library could support a layer,
5131 // but it would be independent of the actual ICD, just in the same library.
Jon Ashburn1530c342016-02-26 13:14:27 -07005132 char **filtered_extension_names = NULL;
Mark Younge1ea9012017-03-09 14:17:40 -07005133 if (0 < pCreateInfo->enabledExtensionCount) {
5134 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
5135 if (NULL == filtered_extension_names) {
5136 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5137 "terminator_CreateDevice: Failed to create extension name "
5138 "storage for %d extensions %d",
5139 pCreateInfo->enabledExtensionCount);
5140 return VK_ERROR_OUT_OF_HOST_MEMORY;
5141 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005142 }
5143
Jon Ashburn1530c342016-02-26 13:14:27 -07005144 localCreateInfo.enabledLayerCount = 0;
5145 localCreateInfo.ppEnabledLayerNames = NULL;
5146
5147 localCreateInfo.enabledExtensionCount = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005148 localCreateInfo.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Jon Ashburn1530c342016-02-26 13:14:27 -07005149
Mark Youngb6399312017-01-10 14:22:15 -07005150 // Get the physical device (ICD) extensions
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005151 res = loader_init_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06005152 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06005153 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07005154 }
5155
Mark Young0f183a82017-02-28 09:58:04 -07005156 res = loader_add_device_extensions(icd_term->this_instance, icd_term->dispatch.EnumerateDeviceExtensionProperties,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005157 phys_dev_term->phys_dev, icd_term->scanned_icd->lib_name, &icd_exts);
Jon Ashburn014438f2016-03-01 19:51:07 -07005158 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06005159 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07005160 }
5161
Jon Ashburn1530c342016-02-26 13:14:27 -07005162 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
5163 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005164 VkExtensionProperties *prop = get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07005165 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005166 filtered_extension_names[localCreateInfo.enabledExtensionCount] = (char *)extension_name;
Jon Ashburn1530c342016-02-26 13:14:27 -07005167 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06005168 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005169 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
5170 "vkCreateDevice extension %s not available for "
5171 "devices associated with ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06005172 extension_name, icd_term->scanned_icd->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07005173 }
5174 }
5175
Mark Young0f183a82017-02-28 09:58:04 -07005176 // Before we continue, If KHX_device_group is the list of enabled and viable extensions, then we then need to look for the
5177 // corresponding VkDeviceGroupDeviceCreateInfoKHX struct in the device list and replace all the physical device values (which
5178 // are really loader physical device terminator values) with the ICD versions.
5179 if (icd_term->this_instance->enabled_known_extensions.khx_device_group_creation == 1) {
5180 struct VkStructureHeader *pNext = (struct VkStructureHeader *)localCreateInfo.pNext;
5181 struct VkStructureHeader *pPrev = (struct VkStructureHeader *)&localCreateInfo;
5182 while (NULL != pNext) {
5183 if (VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO_KHX == pNext->sType) {
5184 VkDeviceGroupDeviceCreateInfoKHX *cur_struct = (VkDeviceGroupDeviceCreateInfoKHX *)pNext;
5185 if (0 < cur_struct->physicalDeviceCount && NULL != cur_struct->pPhysicalDevices) {
5186 VkDeviceGroupDeviceCreateInfoKHX *temp_struct = loader_stack_alloc(sizeof(VkDeviceGroupDeviceCreateInfoKHX));
5187 VkPhysicalDevice *phys_dev_array = NULL;
5188 if (NULL == temp_struct) {
5189 return VK_ERROR_OUT_OF_HOST_MEMORY;
5190 }
5191 memcpy(temp_struct, cur_struct, sizeof(VkDeviceGroupDeviceCreateInfoKHX));
5192 phys_dev_array = loader_stack_alloc(sizeof(VkPhysicalDevice) * cur_struct->physicalDeviceCount);
5193 if (NULL == phys_dev_array) {
5194 return VK_ERROR_OUT_OF_HOST_MEMORY;
5195 }
5196
5197 // Before calling down, replace the incoming physical device values (which are really loader terminator
5198 // physical devices) with the ICDs physical device values.
5199 struct loader_physical_device_term *cur_term;
5200 for (uint32_t phys_dev = 0; phys_dev < cur_struct->physicalDeviceCount; phys_dev++) {
5201 cur_term = (struct loader_physical_device_term *)cur_struct->pPhysicalDevices[phys_dev];
5202 phys_dev_array[phys_dev] = cur_term->phys_dev;
5203 }
5204 temp_struct->pPhysicalDevices = phys_dev_array;
5205
5206 // Replace the old struct in the pNext chain with this one.
5207 pPrev->pNext = (const void *)temp_struct;
5208 pNext = (struct VkStructureHeader *)(temp_struct);
5209 }
5210 break;
5211 }
5212
5213 pPrev = pNext;
5214 pNext = (struct VkStructureHeader *)(pPrev->pNext);
5215 }
5216 }
5217
Lenny Komow34d78d22017-05-24 15:47:15 -06005218 // Handle loader emulation for structs that are not supported by the ICD:
5219 // Presently, the emulation leaves the pNext chain alone. This means that the ICD will receive items in the chain which
5220 // are not recognized by the ICD. If this causes the ICD to fail, then the items would have to be removed here. The current
5221 // implementation does not remove them because copying the pNext chain would be impossible if the loader does not recognize
5222 // the any of the struct types, as the loader would not know the size to allocate and copy.
5223 if (icd_term->dispatch.GetPhysicalDeviceFeatures2KHR == NULL) {
5224 const void *pNext = localCreateInfo.pNext;
5225 while (pNext != NULL) {
5226 switch (*(VkStructureType *)pNext) {
5227 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR: {
5228 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
5229 "vkCreateDevice: Emulating handling of VkPhysicalDeviceFeatures2KHR in pNext chain for ICD \"%s\"",
5230 icd_term->scanned_icd->lib_name);
5231 const VkPhysicalDeviceFeatures2KHR *features = pNext;
5232
5233 // Verify that VK_KHR_get_physical_device_properties2 is enabled
5234 if (icd_term->this_instance->enabled_known_extensions.khr_get_physical_device_properties2) {
5235 localCreateInfo.pEnabledFeatures = &features->features;
5236 }
5237
5238 // Leave this item in the pNext chain for now
5239
5240 pNext = features->pNext;
5241 break;
5242 }
5243
5244 case VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO_KHX: {
5245 loader_log(
5246 icd_term->this_instance, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
5247 "vkCreateDevice: Emulating handling of VkDeviceGroupDeviceCreateInfoKHX in pNext chain for ICD \"%s\"",
5248 icd_term->scanned_icd->lib_name);
5249 const VkDeviceGroupDeviceCreateInfoKHX *group_info = pNext;
5250
5251 // The group must contain only this one device, since physical device groups aren't actually supported
5252 if (group_info->physicalDeviceCount != 1 || group_info->pPhysicalDevices[0] != physicalDevice) {
5253 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5254 "vkCreateDevice: Emulation failed to create device from device group info");
5255 res = VK_ERROR_INITIALIZATION_FAILED;
5256 goto out;
5257 }
5258
5259 // Nothing needs to be done here because we're leaving the item in the pNext chain and because the spec states
5260 // that the physicalDevice argument must be included in the device group, and we've already checked that it is
5261
5262 pNext = group_info->pNext;
5263 break;
5264 }
5265
5266 // Multiview properties are also allowed, but since VK_KHX_multiview is a device extension, we'll just let the ICD
5267 // handle that error when the user enables the extension here
5268 default: {
5269 const struct VkStructureHeader *header = pNext;
5270 pNext = header->pNext;
5271 break;
5272 }
5273 }
5274 }
5275 }
5276
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005277 res = fpCreateDevice(phys_dev_term->phys_dev, &localCreateInfo, pAllocator, &dev->icd_device);
Jon Ashburn1530c342016-02-26 13:14:27 -07005278 if (res != VK_SUCCESS) {
Mark Young0153e0b2016-11-03 14:27:13 -06005279 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07005280 "terminator_CreateDevice: Failed in ICD %s vkCreateDevice"
5281 "call",
Mark Young0153e0b2016-11-03 14:27:13 -06005282 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06005283 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07005284 }
5285
Mark Young65cb3662016-11-07 13:27:02 -07005286 *pDevice = dev->icd_device;
Mark Young0153e0b2016-11-03 14:27:13 -06005287 loader_add_logical_device(icd_term->this_instance, icd_term, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07005288
Mark Young0f183a82017-02-28 09:58:04 -07005289 // Init dispatch pointer in new device object
Jon Ashburn1530c342016-02-26 13:14:27 -07005290 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
5291
Mark Young0ad83132016-06-30 13:02:42 -06005292out:
5293 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005294 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06005295 }
5296
Jon Ashburn1530c342016-02-26 13:14:27 -07005297 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005298}
5299
Mark Young6267ae62017-01-12 12:27:19 -07005300VkResult setupLoaderTrampPhysDevs(VkInstance instance) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07005301 VkResult res = VK_SUCCESS;
Mark Young6267ae62017-01-12 12:27:19 -07005302 VkPhysicalDevice *local_phys_devs = NULL;
5303 struct loader_instance *inst;
5304 uint32_t total_count = 0;
5305 struct loader_physical_device_tramp **new_phys_devs = NULL;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07005306
Mark Young6267ae62017-01-12 12:27:19 -07005307 inst = loader_get_instance(instance);
5308 if (NULL == inst) {
5309 res = VK_ERROR_INITIALIZATION_FAILED;
5310 goto out;
5311 }
Mark Youngd66edd52017-03-10 17:31:18 -07005312
Mark Youngbb3a29c2017-05-19 12:29:43 -06005313 // Query how many GPUs there
Mark Youngd66edd52017-03-10 17:31:18 -07005314 res = inst->disp->layer_inst_disp.EnumeratePhysicalDevices(instance, &total_count, NULL);
5315 if (res != VK_SUCCESS) {
5316 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5317 "setupLoaderTrampPhysDevs: Failed during dispatch call "
5318 "of \'vkEnumeratePhysicalDevices\' to lower layers or "
5319 "loader to get count.");
5320 goto out;
5321 }
5322
5323 // Really use what the total GPU count is since Optimus and other layers may mess
5324 // the count up.
Mark Young6267ae62017-01-12 12:27:19 -07005325 total_count = inst->total_gpu_count;
5326
5327 // Create an array for the new physical devices, which will be stored
5328 // in the instance for the trampoline code.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005329 new_phys_devs = (struct loader_physical_device_tramp **)loader_instance_heap_alloc(
5330 inst, total_count * sizeof(struct loader_physical_device_tramp *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005331 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005332 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5333 "setupLoaderTrampPhysDevs: Failed to allocate new physical device"
5334 " array of size %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005335 total_count);
Mark Youngd8382d72016-12-23 16:59:58 -07005336 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5337 goto out;
5338 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005339 memset(new_phys_devs, 0, total_count * sizeof(struct loader_physical_device_tramp *));
Jon Ashburn014438f2016-03-01 19:51:07 -07005340
Mark Young6267ae62017-01-12 12:27:19 -07005341 // Create a temporary array (on the stack) to keep track of the
5342 // returned VkPhysicalDevice values.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005343 local_phys_devs = loader_stack_alloc(sizeof(VkPhysicalDevice) * total_count);
Mark Young6267ae62017-01-12 12:27:19 -07005344 if (NULL == local_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005345 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5346 "setupLoaderTrampPhysDevs: Failed to allocate local "
5347 "physical device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07005348 total_count);
5349 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5350 goto out;
5351 }
5352 memset(local_phys_devs, 0, sizeof(VkPhysicalDevice) * total_count);
5353
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005354 res = inst->disp->layer_inst_disp.EnumeratePhysicalDevices(instance, &total_count, local_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005355 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005356 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5357 "setupLoaderTrampPhysDevs: Failed during dispatch call "
5358 "of \'vkEnumeratePhysicalDevices\' to lower layers or "
Mark Youngd66edd52017-03-10 17:31:18 -07005359 "loader to get content.");
Mark Young6267ae62017-01-12 12:27:19 -07005360 goto out;
5361 }
5362
5363 // Copy or create everything to fill the new array of physical devices
5364 for (uint32_t new_idx = 0; new_idx < total_count; new_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07005365 // Check if this physical device is already in the old buffer
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005366 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_tramp; old_idx++) {
5367 if (local_phys_devs[new_idx] == inst->phys_devs_tramp[old_idx]->phys_dev) {
Mark Young6267ae62017-01-12 12:27:19 -07005368 new_phys_devs[new_idx] = inst->phys_devs_tramp[old_idx];
5369 break;
5370 }
Mark Youngd8382d72016-12-23 16:59:58 -07005371 }
5372
Mark Young6267ae62017-01-12 12:27:19 -07005373 // If this physical device isn't in the old buffer, create it
5374 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005375 new_phys_devs[new_idx] = (struct loader_physical_device_tramp *)loader_instance_heap_alloc(
5376 inst, sizeof(struct loader_physical_device_tramp), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005377 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005378 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5379 "setupLoaderTrampPhysDevs: Failed to allocate "
5380 "physical device trampoline object %d",
Mark Young6267ae62017-01-12 12:27:19 -07005381 new_idx);
5382 total_count = new_idx;
Mark Youngd8382d72016-12-23 16:59:58 -07005383 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5384 goto out;
5385 }
5386
Mark Young6267ae62017-01-12 12:27:19 -07005387 // Initialize the new physicalDevice object
5388 loader_set_dispatch((void *)new_phys_devs[new_idx], inst->disp);
5389 new_phys_devs[new_idx]->this_instance = inst;
5390 new_phys_devs[new_idx]->phys_dev = local_phys_devs[new_idx];
Mark Young559d7502016-09-26 11:38:46 -06005391 }
Lenny Komowa5e01122016-12-22 15:29:43 -07005392 }
Mark Young559d7502016-09-26 11:38:46 -06005393
Lenny Komowa5e01122016-12-22 15:29:43 -07005394out:
Mark Youngd8382d72016-12-23 16:59:58 -07005395
Mark Young6267ae62017-01-12 12:27:19 -07005396 if (VK_SUCCESS != res) {
5397 if (NULL != new_phys_devs) {
5398 for (uint32_t i = 0; i < total_count; i++) {
5399 loader_instance_heap_free(inst, new_phys_devs[i]);
Lenny Komowa5e01122016-12-22 15:29:43 -07005400 }
5401 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005402 }
5403 total_count = 0;
5404 } else {
5405 // Free everything that didn't carry over to the new array of
5406 // physical devices
5407 if (NULL != inst->phys_devs_tramp) {
5408 for (uint32_t i = 0; i < inst->phys_dev_count_tramp; i++) {
5409 bool found = false;
5410 for (uint32_t j = 0; j < total_count; j++) {
5411 if (inst->phys_devs_tramp[i] == new_phys_devs[j]) {
5412 found = true;
5413 break;
5414 }
5415 }
5416 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005417 loader_instance_heap_free(inst, inst->phys_devs_tramp[i]);
Mark Young6267ae62017-01-12 12:27:19 -07005418 }
5419 }
5420 loader_instance_heap_free(inst, inst->phys_devs_tramp);
5421 }
Mark Youngd8382d72016-12-23 16:59:58 -07005422
Mark Young6267ae62017-01-12 12:27:19 -07005423 // Swap in the new physical device list
5424 inst->phys_dev_count_tramp = total_count;
5425 inst->phys_devs_tramp = new_phys_devs;
5426 }
5427
5428 return res;
5429}
5430
5431VkResult setupLoaderTermPhysDevs(struct loader_instance *inst) {
5432 VkResult res = VK_SUCCESS;
5433 struct loader_icd_term *icd_term;
5434 struct loader_phys_dev_per_icd *icd_phys_dev_array = NULL;
5435 struct loader_physical_device_term **new_phys_devs = NULL;
Mark Young6267ae62017-01-12 12:27:19 -07005436
5437 inst->total_gpu_count = 0;
5438
5439 // Allocate something to store the physical device characteristics
5440 // that we read from each ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005441 icd_phys_dev_array =
5442 (struct loader_phys_dev_per_icd *)loader_stack_alloc(sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
Mark Young6267ae62017-01-12 12:27:19 -07005443 if (NULL == icd_phys_dev_array) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005444 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5445 "setupLoaderTermPhysDevs: Failed to allocate temporary "
5446 "ICD Physical device info array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07005447 inst->total_gpu_count);
5448 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5449 goto out;
5450 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005451 memset(icd_phys_dev_array, 0, sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
Mark Young6267ae62017-01-12 12:27:19 -07005452 icd_term = inst->icd_terms;
5453
5454 // For each ICD, query the number of physical devices, and then get an
5455 // internal value for those physical devices.
Karl Schultz47dd59d2017-01-20 13:19:20 -07005456 for (uint32_t icd_idx = 0; NULL != icd_term; icd_term = icd_term->next, icd_idx++) {
Mark Young0f183a82017-02-28 09:58:04 -07005457 res = icd_term->dispatch.EnumeratePhysicalDevices(icd_term->instance, &icd_phys_dev_array[icd_idx].count, NULL);
Mark Young6267ae62017-01-12 12:27:19 -07005458 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005459 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5460 "setupLoaderTermPhysDevs: Call to "
5461 "ICD %d's \'vkEnumeratePhysicalDevices\' failed with"
5462 " error 0x%08x",
Karl Schultz47dd59d2017-01-20 13:19:20 -07005463 icd_idx, res);
Mark Young6267ae62017-01-12 12:27:19 -07005464 goto out;
5465 }
5466
Karl Schultz47dd59d2017-01-20 13:19:20 -07005467 icd_phys_dev_array[icd_idx].phys_devs =
5468 (VkPhysicalDevice *)loader_stack_alloc(icd_phys_dev_array[icd_idx].count * sizeof(VkPhysicalDevice));
5469 if (NULL == icd_phys_dev_array[icd_idx].phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005470 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5471 "setupLoaderTermPhysDevs: Failed to allocate temporary "
5472 "ICD Physical device array for ICD %d of size %d",
Karl Schultz47dd59d2017-01-20 13:19:20 -07005473 icd_idx, inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07005474 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5475 goto out;
5476 }
5477
Mark Young0f183a82017-02-28 09:58:04 -07005478 res = icd_term->dispatch.EnumeratePhysicalDevices(icd_term->instance, &(icd_phys_dev_array[icd_idx].count),
5479 icd_phys_dev_array[icd_idx].phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005480 if (VK_SUCCESS != res) {
5481 goto out;
5482 }
Karl Schultz47dd59d2017-01-20 13:19:20 -07005483 inst->total_gpu_count += icd_phys_dev_array[icd_idx].count;
5484 icd_phys_dev_array[icd_idx].this_icd_term = icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07005485 }
5486
5487 if (0 == inst->total_gpu_count) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005488 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5489 "setupLoaderTermPhysDevs: Failed to detect any valid"
5490 " GPUs in the current config");
Mark Young6267ae62017-01-12 12:27:19 -07005491 res = VK_ERROR_INITIALIZATION_FAILED;
5492 goto out;
5493 }
5494
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005495 new_phys_devs = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count,
5496 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005497 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005498 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5499 "setupLoaderTermPhysDevs: Failed to allocate new physical"
5500 " device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07005501 inst->total_gpu_count);
5502 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5503 goto out;
5504 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005505 memset(new_phys_devs, 0, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07005506
5507 // Copy or create everything to fill the new array of physical devices
5508 uint32_t idx = 0;
5509 for (uint32_t icd_idx = 0; icd_idx < inst->total_icd_count; icd_idx++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005510 for (uint32_t pd_idx = 0; pd_idx < icd_phys_dev_array[icd_idx].count; pd_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07005511 // Check if this physical device is already in the old buffer
5512 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005513 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_term; old_idx++) {
5514 if (icd_phys_dev_array[icd_idx].phys_devs[pd_idx] == inst->phys_devs_term[old_idx]->phys_dev) {
Mark Young6267ae62017-01-12 12:27:19 -07005515 new_phys_devs[idx] = inst->phys_devs_term[old_idx];
5516 break;
5517 }
5518 }
5519 }
5520 // If this physical device isn't in the old buffer, then we
5521 // need to create it.
5522 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005523 new_phys_devs[idx] = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term),
5524 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005525 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005526 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5527 "setupLoaderTermPhysDevs: Failed to allocate "
5528 "physical device terminator object %d",
Mark Young6267ae62017-01-12 12:27:19 -07005529 idx);
5530 inst->total_gpu_count = idx;
5531 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5532 goto out;
5533 }
5534
5535 loader_set_dispatch((void *)new_phys_devs[idx], inst->disp);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005536 new_phys_devs[idx]->this_icd_term = icd_phys_dev_array[icd_idx].this_icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07005537 new_phys_devs[idx]->icd_index = (uint8_t)(icd_idx);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005538 new_phys_devs[idx]->phys_dev = icd_phys_dev_array[icd_idx].phys_devs[pd_idx];
Mark Young6267ae62017-01-12 12:27:19 -07005539 }
5540 idx++;
5541 }
5542 }
5543
5544out:
5545
5546 if (VK_SUCCESS != res) {
Mark Young156e8552017-02-03 16:27:42 -07005547 if (NULL != new_phys_devs) {
5548 // We've encountered an error, so we should free the new buffers.
Mark Young6267ae62017-01-12 12:27:19 -07005549 for (uint32_t i = 0; i < inst->total_gpu_count; i++) {
5550 loader_instance_heap_free(inst, new_phys_devs[i]);
5551 }
Mark Young156e8552017-02-03 16:27:42 -07005552 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005553 }
Mark Young156e8552017-02-03 16:27:42 -07005554 inst->total_gpu_count = 0;
Mark Young6267ae62017-01-12 12:27:19 -07005555 } else {
5556 // Free everything that didn't carry over to the new array of
5557 // physical devices. Everything else will have been copied over
5558 // to the new array.
5559 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005560 for (uint32_t cur_pd = 0; cur_pd < inst->phys_dev_count_term; cur_pd++) {
Mark Young6267ae62017-01-12 12:27:19 -07005561 bool found = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005562 for (uint32_t new_pd_idx = 0; new_pd_idx < inst->total_gpu_count; new_pd_idx++) {
5563 if (inst->phys_devs_term[cur_pd] == new_phys_devs[new_pd_idx]) {
Mark Young6267ae62017-01-12 12:27:19 -07005564 found = true;
5565 break;
5566 }
5567 }
5568 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005569 loader_instance_heap_free(inst, inst->phys_devs_term[cur_pd]);
Mark Young6267ae62017-01-12 12:27:19 -07005570 }
5571 }
5572 loader_instance_heap_free(inst, inst->phys_devs_term);
5573 }
5574
5575 // Swap out old and new devices list
5576 inst->phys_dev_count_term = inst->total_gpu_count;
5577 inst->phys_devs_term = new_phys_devs;
5578 }
5579
5580 return res;
5581}
5582
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005583VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
5584 VkPhysicalDevice *pPhysicalDevices) {
Mark Young6267ae62017-01-12 12:27:19 -07005585 struct loader_instance *inst = (struct loader_instance *)instance;
5586 VkResult res = VK_SUCCESS;
5587
Mark Youngd66edd52017-03-10 17:31:18 -07005588 // Always call the setup loader terminator physical devices because they may
5589 // have changed at any point.
5590 res = setupLoaderTermPhysDevs(inst);
5591 if (VK_SUCCESS != res) {
5592 goto out;
Mark Young6267ae62017-01-12 12:27:19 -07005593 }
5594
5595 uint32_t copy_count = inst->total_gpu_count;
5596 if (NULL != pPhysicalDevices) {
5597 if (copy_count > *pPhysicalDeviceCount) {
5598 copy_count = *pPhysicalDeviceCount;
5599 res = VK_INCOMPLETE;
5600 }
5601
5602 for (uint32_t i = 0; i < copy_count; i++) {
5603 pPhysicalDevices[i] = (VkPhysicalDevice)inst->phys_devs_term[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07005604 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005605 }
Mark Young559d7502016-09-26 11:38:46 -06005606
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07005607 *pPhysicalDeviceCount = copy_count;
5608
Mark Young6267ae62017-01-12 12:27:19 -07005609out:
5610
Jon Ashburn24cd4be2015-11-01 14:04:06 -07005611 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005612}
5613
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005614VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
5615 VkPhysicalDeviceProperties *pProperties) {
5616 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005617 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005618 if (NULL != icd_term->dispatch.GetPhysicalDeviceProperties) {
5619 icd_term->dispatch.GetPhysicalDeviceProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005620 }
Tony Barbour59a47322015-06-24 16:06:58 -06005621}
5622
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005623VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
5624 uint32_t *pQueueFamilyPropertyCount,
5625 VkQueueFamilyProperties *pProperties) {
5626 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005627 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005628 if (NULL != icd_term->dispatch.GetPhysicalDeviceQueueFamilyProperties) {
5629 icd_term->dispatch.GetPhysicalDeviceQueueFamilyProperties(phys_dev_term->phys_dev, pQueueFamilyPropertyCount, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005630 }
Tony Barbour59a47322015-06-24 16:06:58 -06005631}
5632
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005633VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
5634 VkPhysicalDeviceMemoryProperties *pProperties) {
5635 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005636 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005637 if (NULL != icd_term->dispatch.GetPhysicalDeviceMemoryProperties) {
5638 icd_term->dispatch.GetPhysicalDeviceMemoryProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005639 }
Jon Ashburn3da71f22015-05-14 12:43:38 -06005640}
5641
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005642VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
5643 VkPhysicalDeviceFeatures *pFeatures) {
5644 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005645 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005646 if (NULL != icd_term->dispatch.GetPhysicalDeviceFeatures) {
5647 icd_term->dispatch.GetPhysicalDeviceFeatures(phys_dev_term->phys_dev, pFeatures);
Mark Youngb6399312017-01-10 14:22:15 -07005648 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005649}
5650
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005651VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5652 VkFormatProperties *pFormatInfo) {
5653 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005654 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005655 if (NULL != icd_term->dispatch.GetPhysicalDeviceFormatProperties) {
5656 icd_term->dispatch.GetPhysicalDeviceFormatProperties(phys_dev_term->phys_dev, format, pFormatInfo);
Mark Youngb6399312017-01-10 14:22:15 -07005657 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005658}
5659
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005660VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5661 VkImageType type, VkImageTiling tiling,
5662 VkImageUsageFlags usage, VkImageCreateFlags flags,
5663 VkImageFormatProperties *pImageFormatProperties) {
5664 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005665 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005666 if (NULL == icd_term->dispatch.GetPhysicalDeviceImageFormatProperties) {
Mark Youngb6399312017-01-10 14:22:15 -07005667 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5668 "Encountered the vkEnumerateDeviceLayerProperties "
5669 "terminator. This means a layer improperly continued.");
Chia-I Wu17241042015-10-31 00:31:16 +08005670 return VK_ERROR_INITIALIZATION_FAILED;
Mark Youngb6399312017-01-10 14:22:15 -07005671 }
Mark Young0f183a82017-02-28 09:58:04 -07005672 return icd_term->dispatch.GetPhysicalDeviceImageFormatProperties(phys_dev_term->phys_dev, format, type, tiling, usage, flags,
5673 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06005674}
5675
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005676VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5677 VkImageType type, VkSampleCountFlagBits samples,
5678 VkImageUsageFlags usage, VkImageTiling tiling,
5679 uint32_t *pNumProperties,
5680 VkSparseImageFormatProperties *pProperties) {
5681 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005682 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Young0f183a82017-02-28 09:58:04 -07005683 if (NULL != icd_term->dispatch.GetPhysicalDeviceSparseImageFormatProperties) {
5684 icd_term->dispatch.GetPhysicalDeviceSparseImageFormatProperties(phys_dev_term->phys_dev, format, type, samples, usage,
5685 tiling, pNumProperties, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005686 }
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06005687}
5688
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005689VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
5690 const char *pLayerName, uint32_t *pPropertyCount,
5691 VkExtensionProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06005692 struct loader_physical_device_term *phys_dev_term;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07005693
Mark Young3a587792016-08-19 15:25:08 -06005694 struct loader_layer_list implicit_layer_list = {0};
5695 struct loader_extension_list all_exts = {0};
5696 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07005697
Jon Ashburndc5d9202016-02-29 13:00:51 -07005698 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005699
Mark Young0f183a82017-02-28 09:58:04 -07005700 // Any layer or trampoline wrapping should be removed at this point in time can just cast to the expected
5701 // type for VkPhysicalDevice.
Mark Young0153e0b2016-11-03 14:27:13 -06005702 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005703
Mark Young0f183a82017-02-28 09:58:04 -07005704 // This case is during the call down the instance chain with pLayerName == NULL
Mark Young0153e0b2016-11-03 14:27:13 -06005705 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005706 uint32_t icd_ext_count = *pPropertyCount;
5707 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005708
Mark Young0f183a82017-02-28 09:58:04 -07005709 // Get the available device extensions
5710 res = icd_term->dispatch.EnumerateDeviceExtensionProperties(phys_dev_term->phys_dev, NULL, &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06005711 if (res != VK_SUCCESS) {
5712 goto out;
5713 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07005714
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005715 if (!loader_init_layer_list(icd_term->this_instance, &implicit_layer_list)) {
Mark Young3a587792016-08-19 15:25:08 -06005716 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5717 goto out;
5718 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005719
Mark Young283fe1c2017-05-04 12:16:35 -06005720 loader_add_implicit_layers(icd_term->this_instance, &implicit_layer_list, NULL, &icd_term->this_instance->instance_layer_list);
Mark Young0f183a82017-02-28 09:58:04 -07005721 // We need to determine which implicit layers are active, and then add their extensions. This can't be cached as
5722 // it depends on results of environment variables (which can change).
Jon Ashburndc5d9202016-02-29 13:00:51 -07005723 if (pProperties != NULL) {
Mark Young0f183a82017-02-28 09:58:04 -07005724 // Initialize dev_extension list within the physicalDevice object
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005725 res = loader_init_device_extensions(icd_term->this_instance, phys_dev_term, icd_ext_count, pProperties, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06005726 if (res != VK_SUCCESS) {
5727 goto out;
5728 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005729
Mark Young0f183a82017-02-28 09:58:04 -07005730 // We need to determine which implicit layers are active, and then add their extensions. This can't be cached as
5731 // it depends on results of environment variables (which can change).
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005732 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts, icd_exts.count, icd_exts.list);
Mark Young3a587792016-08-19 15:25:08 -06005733 if (res != VK_SUCCESS) {
5734 goto out;
5735 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005736
Mark Young283fe1c2017-05-04 12:16:35 -06005737 loader_add_implicit_layers(icd_term->this_instance, &implicit_layer_list, NULL,
5738 &icd_term->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07005739
Jon Ashburndc5d9202016-02-29 13:00:51 -07005740 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005741 for (uint32_t j = 0; j < implicit_layer_list.list[i].device_extension_list.count; j++) {
5742 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts, 1,
5743 &implicit_layer_list.list[i].device_extension_list.list[j].props);
Mark Young3a587792016-08-19 15:25:08 -06005744 if (res != VK_SUCCESS) {
5745 goto out;
5746 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005747 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005748 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005749 uint32_t capacity = *pPropertyCount;
5750 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005751
Jon Ashburndc5d9202016-02-29 13:00:51 -07005752 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
5753 props[i] = all_exts.list[i];
5754 }
Mark Young0f183a82017-02-28 09:58:04 -07005755
5756 // Wasn't enough space for the extensions, we did partial copy now return VK_INCOMPLETE
Jon Ashburndc5d9202016-02-29 13:00:51 -07005757 if (capacity < all_exts.count) {
5758 res = VK_INCOMPLETE;
5759 } else {
5760 *pPropertyCount = all_exts.count;
5761 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005762 } else {
Mark Young0f183a82017-02-28 09:58:04 -07005763 // Just return the count; need to add in the count of implicit layer extensions
5764 // don't worry about duplicates being added in the count
Jon Ashburndc5d9202016-02-29 13:00:51 -07005765 *pPropertyCount = icd_ext_count;
5766
5767 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005768 *pPropertyCount += implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005769 }
5770 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06005771 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005772
Mark Young3a587792016-08-19 15:25:08 -06005773out:
5774
5775 if (NULL != implicit_layer_list.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005776 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Mark Young3a587792016-08-19 15:25:08 -06005777 }
5778 if (NULL != all_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005779 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&all_exts);
Mark Young3a587792016-08-19 15:25:08 -06005780 }
5781 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005782 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06005783 }
5784
Jon Ashburndc5d9202016-02-29 13:00:51 -07005785 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005786}
5787
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005788VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
5789 VkLayerProperties *pProperties) {
5790 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Youngb6399312017-01-10 14:22:15 -07005791 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005792 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5793 "Encountered the vkEnumerateDeviceLayerProperties "
5794 "terminator. This means a layer improperly continued.");
Mark Youngb6399312017-01-10 14:22:15 -07005795 // Should never get here this call isn't dispatched down the chain
Jon Ashburndc5d9202016-02-29 13:00:51 -07005796 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06005797}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005798
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005799VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005800 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07005801 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005802 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005803
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005804 for (i = 0; i <= max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005805 if (utf8[i] == 0) {
5806 break;
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005807 } else if (i == max_length) {
5808 result |= VK_STRING_ERROR_LENGTH;
5809 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07005810 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005811 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005812 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005813 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005814 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005815 num_char_bytes = 2;
5816 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
5817 num_char_bytes = 3;
5818 } else {
5819 result = VK_STRING_ERROR_BAD_DATA;
5820 }
5821
5822 // Validate the following num_char_bytes of data
5823 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
5824 if (++i == max_length) {
5825 result |= VK_STRING_ERROR_LENGTH;
5826 break;
5827 }
5828 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
5829 result |= VK_STRING_ERROR_BAD_DATA;
5830 }
5831 }
5832 }
5833 return result;
5834}