blob: 1c6591fdea71218c535bddd8a539304a9096d3ad [file] [log] [blame]
Mark Lobodzinski317574e2016-08-29 14:21:14 -06001/*
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002 *
Jon Ashburn23d36b12016-02-02 17:47:28 -07003 * Copyright (c) 2014-2016 The Khronos Group Inc.
4 * Copyright (c) 2014-2016 Valve Corporation
5 * Copyright (c) 2014-2016 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>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060023 *
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080024 */
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070025
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060026#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080027#include <stdio.h>
28#include <stdlib.h>
29#include <stdarg.h>
30#include <stdbool.h>
31#include <string.h>
32
Chia-I Wu13a61a52014-08-04 11:18:20 +080033#include <sys/types.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050034#if defined(_WIN32)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070035#include "dirent_on_windows.h"
Johannes van Waveren9bd805012015-10-28 11:45:00 -050036#else // _WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080037#include <dirent.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050038#endif // _WIN32
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060039#include "vk_loader_platform.h"
Chia-I Wu19300602014-08-04 08:03:57 +080040#include "loader.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060041#include "gpa_helper.h"
42#include "table_ops.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060043#include "debug_report.h"
Ian Elliott954fa342015-10-30 15:28:23 -060044#include "wsi.h"
Mark Lobodzinski317574e2016-08-29 14:21:14 -060045#include "extensions.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
Jon Ashburnd8ed7992016-04-04 13:52:53 -060050#if defined(__GNUC__)
Jon Ashburn10b3f832016-05-09 11:31:40 -060051#if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 17))
Jon Ashburncc407a22016-04-15 09:25:03 -060052#define secure_getenv __secure_getenv
53#endif
Jon Ashburnd8ed7992016-04-04 13:52:53 -060054#endif
55
Jon Ashburn27cd5842015-05-12 17:26:48 -060056struct loader_struct loader = {0};
Jon Ashburn87d6aa92015-08-28 15:19:27 -060057// TLS for instance for alloc/free callbacks
58THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080059
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -070060static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap00b4aba2015-09-30 11:50:51 -050061
Jon Ashburn24cd4be2015-11-01 14:04:06 -070062struct loader_phys_dev_per_icd {
63 uint32_t count;
64 VkPhysicalDevice *phys_devs;
Jon Ashburn014438f2016-03-01 19:51:07 -070065 struct loader_icd *this_icd;
Jon Ashburn24cd4be2015-11-01 14:04:06 -070066};
67
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060068enum loader_debug {
Jon Ashburn23d36b12016-02-02 17:47:28 -070069 LOADER_INFO_BIT = 0x01,
70 LOADER_WARN_BIT = 0x02,
71 LOADER_PERF_BIT = 0x04,
72 LOADER_ERROR_BIT = 0x08,
73 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060074};
75
76uint32_t g_loader_debug = 0;
77uint32_t g_loader_log_msgs = 0;
78
Jon Ashburn23d36b12016-02-02 17:47:28 -070079// thread safety lock for accessing global data structures such as "loader"
Jon Ashburn6301a0f2015-05-29 13:15:39 -060080// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburn2077e382015-06-29 11:25:34 -060081// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburn6301a0f2015-05-29 13:15:39 -060082loader_platform_thread_mutex loader_lock;
Jon Ashburn6461ef22015-09-22 13:11:00 -060083loader_platform_thread_mutex loader_json_lock;
Jon Ashburn6301a0f2015-05-29 13:15:39 -060084
Jon Ashburn86a527a2016-02-10 20:59:26 -070085const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
86
Ian Elliottd3ef02f2015-07-06 14:36:13 -060087// This table contains the loader's instance dispatch table, which contains
88// default functions if no instance layers are activated. This contains
89// pointers to "terminator functions".
Jon Ashburn6301a0f2015-05-29 13:15:39 -060090const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn69e9ea22015-09-28 16:15:00 -060091 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn1530c342016-02-26 13:14:27 -070092 .DestroyInstance = terminator_DestroyInstance,
93 .EnumeratePhysicalDevices = terminator_EnumeratePhysicalDevices,
94 .GetPhysicalDeviceFeatures = terminator_GetPhysicalDeviceFeatures,
Jon Ashburn23d36b12016-02-02 17:47:28 -070095 .GetPhysicalDeviceFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -070096 terminator_GetPhysicalDeviceFormatProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -070097 .GetPhysicalDeviceImageFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -070098 terminator_GetPhysicalDeviceImageFormatProperties,
99 .GetPhysicalDeviceProperties = terminator_GetPhysicalDeviceProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700100 .GetPhysicalDeviceQueueFamilyProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700101 terminator_GetPhysicalDeviceQueueFamilyProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700102 .GetPhysicalDeviceMemoryProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700103 terminator_GetPhysicalDeviceMemoryProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700104 .EnumerateDeviceExtensionProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700105 terminator_EnumerateDeviceExtensionProperties,
106 .EnumerateDeviceLayerProperties = terminator_EnumerateDeviceLayerProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700107 .GetPhysicalDeviceSparseImageFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700108 terminator_GetPhysicalDeviceSparseImageFormatProperties,
109 .DestroySurfaceKHR = terminator_DestroySurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700110 .GetPhysicalDeviceSurfaceSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700111 terminator_GetPhysicalDeviceSurfaceSupportKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700112 .GetPhysicalDeviceSurfaceCapabilitiesKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700113 terminator_GetPhysicalDeviceSurfaceCapabilitiesKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700114 .GetPhysicalDeviceSurfaceFormatsKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700115 terminator_GetPhysicalDeviceSurfaceFormatsKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700116 .GetPhysicalDeviceSurfacePresentModesKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700117 terminator_GetPhysicalDeviceSurfacePresentModesKHR,
118 .CreateDebugReportCallbackEXT = terminator_CreateDebugReportCallback,
119 .DestroyDebugReportCallbackEXT = terminator_DestroyDebugReportCallback,
120 .DebugReportMessageEXT = terminator_DebugReportMessage,
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600121 .GetPhysicalDeviceExternalImageFormatPropertiesNV =
122 terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700123#ifdef VK_USE_PLATFORM_MIR_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700124 .CreateMirSurfaceKHR = terminator_CreateMirSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700125 .GetPhysicalDeviceMirPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700126 terminator_GetPhysicalDeviceMirPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700127#endif
128#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700129 .CreateWaylandSurfaceKHR = terminator_CreateWaylandSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700130 .GetPhysicalDeviceWaylandPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700131 terminator_GetPhysicalDeviceWaylandPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700132#endif
133#ifdef VK_USE_PLATFORM_WIN32_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700134 .CreateWin32SurfaceKHR = terminator_CreateWin32SurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700135 .GetPhysicalDeviceWin32PresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700136 terminator_GetPhysicalDeviceWin32PresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700137#endif
138#ifdef VK_USE_PLATFORM_XCB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700139 .CreateXcbSurfaceKHR = terminator_CreateXcbSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700140 .GetPhysicalDeviceXcbPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700141 terminator_GetPhysicalDeviceXcbPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700142#endif
143#ifdef VK_USE_PLATFORM_XLIB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700144 .CreateXlibSurfaceKHR = terminator_CreateXlibSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700145 .GetPhysicalDeviceXlibPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700146 terminator_GetPhysicalDeviceXlibPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700147#endif
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700148#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700149 .CreateAndroidSurfaceKHR = terminator_CreateAndroidSurfaceKHR,
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700150#endif
Jon Ashburnc7d3e732016-03-08 09:30:30 -0700151 .GetPhysicalDeviceDisplayPropertiesKHR =
152 terminator_GetPhysicalDeviceDisplayPropertiesKHR,
153 .GetPhysicalDeviceDisplayPlanePropertiesKHR =
154 terminator_GetPhysicalDeviceDisplayPlanePropertiesKHR,
155 .GetDisplayPlaneSupportedDisplaysKHR =
156 terminator_GetDisplayPlaneSupportedDisplaysKHR,
Jon Ashburncc407a22016-04-15 09:25:03 -0600157 .GetDisplayModePropertiesKHR = terminator_GetDisplayModePropertiesKHR,
158 .CreateDisplayModeKHR = terminator_CreateDisplayModeKHR,
159 .GetDisplayPlaneCapabilitiesKHR = terminator_GetDisplayPlaneCapabilitiesKHR,
160 .CreateDisplayPlaneSurfaceKHR = terminator_CreateDisplayPlaneSurfaceKHR,
Jon Ashburn27cd5842015-05-12 17:26:48 -0600161};
162
Jon Ashburn8810c5f2015-08-18 18:04:47 -0600163LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700164
Mark Young0ad83132016-06-30 13:02:42 -0600165void *loader_instance_heap_alloc(const struct loader_instance *instance,
166 size_t size,
167 VkSystemAllocationScope alloc_scope) {
168 void *pMemory = NULL;
169#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
170 {
171#else
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800172 if (instance && instance->alloc_callbacks.pfnAllocation) {
Mark Young0ad83132016-06-30 13:02:42 -0600173 /* These are internal structures, so it's best to align everything to
174 * the largest unit size which is the size of a uint64_t.
175 */
176 pMemory = instance->alloc_callbacks.pfnAllocation(
177 instance->alloc_callbacks.pUserData, size, sizeof(uint64_t),
Jon Ashburn23d36b12016-02-02 17:47:28 -0700178 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600179 } else {
180#endif
181 pMemory = malloc(size);
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600182 }
Mark Young0ad83132016-06-30 13:02:42 -0600183 return pMemory;
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600184}
185
Mark Young0ad83132016-06-30 13:02:42 -0600186void loader_instance_heap_free(const struct loader_instance *instance,
187 void *pMemory) {
188 if (pMemory != NULL) {
189#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
190 {
191#else
192 if (instance && instance->alloc_callbacks.pfnFree) {
193 instance->alloc_callbacks.pfnFree(
194 instance->alloc_callbacks.pUserData, pMemory);
195 } else {
196#endif
197 free(pMemory);
Mark Youngd077f992016-06-30 11:03:59 -0600198 }
Mark Young4b0b9222016-06-29 18:33:53 -0600199 }
Mark Young4b0b9222016-06-29 18:33:53 -0600200}
201
Mark Young0ad83132016-06-30 13:02:42 -0600202void *loader_instance_heap_realloc(const struct loader_instance *instance,
203 void *pMemory, size_t orig_size, size_t size,
204 VkSystemAllocationScope alloc_scope) {
205 void *pNewMem = NULL;
206 if (pMemory == NULL || orig_size == 0) {
207 pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
208 } else if (size == 0) {
209 loader_instance_heap_free(instance, pMemory);
210#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
211#else
212 } else if (instance && instance->alloc_callbacks.pfnReallocation) {
213 /* These are internal structures, so it's best to align everything to
214 * the largest unit size which is the size of a uint64_t.
215 */
216 pNewMem = instance->alloc_callbacks.pfnReallocation(
217 instance->alloc_callbacks.pUserData, pMemory, size,
218 sizeof(uint64_t), alloc_scope);
219#endif
220 } else {
221 pNewMem = realloc(pMemory, size);
222 }
223 return pNewMem;
Mark Young4b0b9222016-06-29 18:33:53 -0600224}
225
Mark Young0ad83132016-06-30 13:02:42 -0600226void *loader_instance_tls_heap_alloc(size_t size) {
227 return loader_instance_heap_alloc(tls_instance, size,
228 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young4b0b9222016-06-29 18:33:53 -0600229}
Mark Young4b0b9222016-06-29 18:33:53 -0600230
Mark Young0ad83132016-06-30 13:02:42 -0600231void loader_instance_tls_heap_free(void *pMemory) {
232 loader_instance_heap_free(tls_instance, pMemory);
233}
234
235void *loader_device_heap_alloc(const struct loader_device *device, size_t size,
236 VkSystemAllocationScope alloc_scope) {
237 void *pMemory = NULL;
238#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
239 {
240#else
241 if (device && device->alloc_callbacks.pfnAllocation) {
242 /* These are internal structures, so it's best to align everything to
243 * the largest unit size which is the size of a uint64_t.
244 */
245 pMemory = device->alloc_callbacks.pfnAllocation(
246 device->alloc_callbacks.pUserData, size, sizeof(uint64_t),
247 alloc_scope);
248 } else {
249#endif
250 pMemory = malloc(size);
251 }
252 return pMemory;
253}
254
255void loader_device_heap_free(const struct loader_device *device,
256 void *pMemory) {
257 if (pMemory != NULL) {
258#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
259 {
260#else
261 if (device && device->alloc_callbacks.pfnFree) {
262 device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData,
263 pMemory);
264 } else {
265#endif
266 free(pMemory);
267 }
268 }
269}
270
271void *loader_device_heap_realloc(const struct loader_device *device,
272 void *pMemory, size_t orig_size, size_t size,
273 VkSystemAllocationScope alloc_scope) {
274 void *pNewMem = NULL;
275 if (pMemory == NULL || orig_size == 0) {
276 pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
277 } else if (size == 0) {
278 loader_device_heap_free(device, pMemory);
279#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
280#else
281 } else if (device && device->alloc_callbacks.pfnReallocation) {
282 /* These are internal structures, so it's best to align everything to
283 * the largest unit size which is the size of a uint64_t.
284 */
285 pNewMem = device->alloc_callbacks.pfnReallocation(
286 device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
287 alloc_scope);
288#endif
289 } else {
290 pNewMem = realloc(pMemory, size);
291 }
292 return pNewMem;
293}
294
295// Environment variables
296#if defined(__linux__)
297
298static inline char *loader_getenv(const char *name,
299 const struct loader_instance *inst) {
300 // No allocation of memory necessary for Linux, but we should at least touch
301 // the inst pointer to get rid of compiler warnings.
302 (void)inst;
303 return getenv(name);
304}
305static inline void loader_free_getenv(const char *val,
306 const struct loader_instance *inst) {
307 // No freeing of memory necessary for Linux, but we should at least touch
308 // the val and inst pointers to get rid of compiler warnings.
309 (void)val;
310 (void)inst;
311}
312
313#elif defined(WIN32)
314
315static inline char *loader_getenv(const char *name,
316 const struct loader_instance *inst) {
317 char *retVal;
318 DWORD valSize;
319
320 valSize = GetEnvironmentVariableA(name, NULL, 0);
321
322 // valSize DOES include the null terminator, so for any set variable
323 // will always be at least 1. If it's 0, the variable wasn't set.
324 if (valSize == 0)
325 return NULL;
326
327 // Allocate the space necessary for the registry entry
328 if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
329 retVal = (char *)inst->alloc_callbacks.pfnAllocation(
330 inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
331 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
332 } else {
333 retVal = (char *)malloc(valSize);
334 }
335
336 if (NULL != retVal) {
337 GetEnvironmentVariableA(name, retVal, valSize);
338 }
339
340 return retVal;
341}
342
343static inline void loader_free_getenv(char *val,
344 const struct loader_instance *inst) {
345 if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
346 inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
347 } else {
348 free((void *)val);
349 }
350}
351
352#else
353
354static inline char *loader_getenv(const char *name,
355 const struct loader_instance *inst) {
356 // stub func
357 (void)inst;
358 (void)name;
359 return NULL;
360}
361static inline void loader_free_getenv(const char *val,
362 const struct loader_instance *inst) {
363 // stub func
364 (void)val;
365 (void)inst;
366}
367
368#endif
369
Jon Ashburn2e37d752016-02-12 08:20:06 -0700370void loader_log(const struct loader_instance *inst, VkFlags msg_type,
Jon Ashburn1530c342016-02-26 13:14:27 -0700371 int32_t msg_code, const char *format, ...) {
Jon Ashburn86723b02015-07-31 15:47:59 -0600372 char msg[512];
Jon Ashburnffad94d2015-06-30 14:46:22 -0700373 va_list ap;
374 int ret;
375
Jon Ashburnffad94d2015-06-30 14:46:22 -0700376 va_start(ap, format);
377 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700378 if ((ret >= (int)sizeof(msg)) || ret < 0) {
379 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnffad94d2015-06-30 14:46:22 -0700380 }
381 va_end(ap);
382
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700383 if (inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700384 util_DebugReportMessage(inst, msg_type,
385 VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
386 (uint64_t)inst, 0, msg_code, "loader", msg);
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700387 }
388
389 if (!(msg_type & g_loader_log_msgs)) {
390 return;
391 }
392
Ian Elliott4470a302015-02-17 10:33:47 -0700393#if defined(WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -0700394 OutputDebugString(msg);
mschottb9cdb782015-07-22 14:11:29 +0200395 OutputDebugString("\n");
Jon Ashburnffad94d2015-06-30 14:46:22 -0700396#endif
397 fputs(msg, stderr);
398 fputc('\n', stderr);
399}
400
Jon Ashburncc407a22016-04-15 09:25:03 -0600401VKAPI_ATTR VkResult VKAPI_CALL
402vkSetInstanceDispatch(VkInstance instance, void *object) {
Jon Ashburnc3c58772016-03-29 11:16:01 -0600403
404 struct loader_instance *inst = loader_get_instance(instance);
405 if (!inst) {
406 return VK_ERROR_INITIALIZATION_FAILED;
407 }
408 loader_set_dispatch(object, inst->disp);
409 return VK_SUCCESS;
410}
411
Jon Ashburncc407a22016-04-15 09:25:03 -0600412VKAPI_ATTR VkResult VKAPI_CALL
413vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600414 struct loader_device *dev;
Mark Young16573c72016-06-28 10:52:43 -0600415 struct loader_icd *icd = loader_get_icd_and_device(device, &dev, NULL);
Jon Ashburned8f2312016-03-31 10:52:22 -0600416
417 if (!icd) {
418 return VK_ERROR_INITIALIZATION_FAILED;
419 }
420 loader_set_dispatch(object, &dev->loader_dispatch);
421 return VK_SUCCESS;
422}
423
Jon Ashburnffad94d2015-06-30 14:46:22 -0700424#if defined(WIN32)
Tony Barbourea968902015-07-29 14:26:21 -0600425static char *loader_get_next_path(char *path);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700426/**
427* Find the list of registry files (names within a key) in key "location".
428*
Jon Ashburn23d36b12016-02-02 17:47:28 -0700429* This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
430*given in "location"
431* for a list or name/values which are added to a returned list (function return
432*value).
Jon Ashburnffad94d2015-06-30 14:46:22 -0700433* The DWORD values within the key must be 0 or they are skipped.
Jon Ashburne39a4f82015-08-28 13:38:21 -0600434* Function return is a string with a ';' separated list of filenames.
Jon Ashburnffad94d2015-06-30 14:46:22 -0700435* Function return is NULL if no valid name/value pairs are found in the key,
436* or the key is not found.
437*
438* \returns
439* A string list of filenames as pointer.
440* When done using the returned string list, pointer should be freed.
441*/
Jon Ashburn23d36b12016-02-02 17:47:28 -0700442static char *loader_get_registry_files(const struct loader_instance *inst,
443 char *location) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700444 LONG rtn_value;
445 HKEY hive, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700446 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700447 char name[2048];
448 char *out = NULL;
Tony Barbourea968902015-07-29 14:26:21 -0600449 char *loc = location;
450 char *next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700451 DWORD idx = 0;
452 DWORD name_size = sizeof(name);
453 DWORD value;
454 DWORD total_size = 4096;
455 DWORD value_size = sizeof(value);
Tony Barbourea968902015-07-29 14:26:21 -0600456
Jon Ashburn23d36b12016-02-02 17:47:28 -0700457 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600458 next = loader_get_next_path(loc);
459 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniell524ec732015-11-05 16:58:26 -0700460 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600461 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
462 if (rtn_value != ERROR_SUCCESS) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700463 // We still couldn't find the key, so give up:
464 loc = next;
465 continue;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700466 }
Tony Barbourea968902015-07-29 14:26:21 -0600467
Jon Ashburn23d36b12016-02-02 17:47:28 -0700468 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL,
469 NULL, (LPBYTE)&value, &value_size)) ==
470 ERROR_SUCCESS) {
Tony Barbourea968902015-07-29 14:26:21 -0600471 if (value_size == sizeof(value) && value == 0) {
472 if (out == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -0600473 out = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700474 inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600475 if (NULL == out) {
476 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
477 "Out of memory can't alloc space for registry data");
478 return NULL;
479 }
Tony Barbourea968902015-07-29 14:26:21 -0600480 out[0] = '\0';
Jon Ashburn23d36b12016-02-02 17:47:28 -0700481 } else if (strlen(out) + name_size + 1 > total_size) {
Mark Young0ad83132016-06-30 13:02:42 -0600482 out = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700483 inst, out, total_size, total_size * 2,
484 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600485 if (NULL == out) {
486 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
487 "Out of memory can't realloc space for registry data");
488 return NULL;
489 }
Tony Barbourea968902015-07-29 14:26:21 -0600490 total_size *= 2;
491 }
Tony Barbourea968902015-07-29 14:26:21 -0600492 if (strlen(out) == 0)
Jon Ashburn23d36b12016-02-02 17:47:28 -0700493 snprintf(out, name_size + 1, "%s", name);
Tony Barbourea968902015-07-29 14:26:21 -0600494 else
Jon Ashburn23d36b12016-02-02 17:47:28 -0700495 snprintf(out + strlen(out), name_size + 2, "%c%s",
496 PATH_SEPERATOR, name);
Tony Barbourea968902015-07-29 14:26:21 -0600497 }
498 name_size = 2048;
499 }
500 loc = next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700501 }
Tony Barbourea968902015-07-29 14:26:21 -0600502
Jon Ashburnffad94d2015-06-30 14:46:22 -0700503 return out;
504}
505
Ian Elliott4470a302015-02-17 10:33:47 -0700506#endif // WIN32
507
Jon Ashburnc7237a72015-08-03 09:08:46 -0600508/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500509 * Combine path elements, separating each element with the platform-specific
510 * directory separator, and save the combined string to a destination buffer,
511 * not exceeding the given length. Path elements are given as variadic args,
512 * with a NULL element terminating the list.
513 *
514 * \returns the total length of the combined string, not including an ASCII
515 * NUL termination character. This length may exceed the available storage:
516 * in this case, the written string will be truncated to avoid a buffer
517 * overrun, and the return value will greater than or equal to the storage
518 * size. A NULL argument may be provided as the destination buffer in order
519 * to determine the required string length without actually writing a string.
520 */
521
Jon Ashburn23d36b12016-02-02 17:47:28 -0700522static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700523 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500524 va_list ap;
525 const char *component;
526
527 va_start(ap, len);
528
Jon Ashburn23d36b12016-02-02 17:47:28 -0700529 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500530 if (required_len > 0) {
531 // This path element is not the first non-empty element; prepend
532 // a directory separator if space allows
533 if (dest && required_len + 1 < len) {
534 snprintf(dest + required_len, len - required_len, "%c",
535 DIRECTORY_SYMBOL);
536 }
537 required_len++;
538 }
539
540 if (dest && required_len < len) {
541 strncpy(dest + required_len, component, len - required_len);
542 }
543 required_len += strlen(component);
544 }
545
546 va_end(ap);
547
548 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
549 if (dest && required_len >= len) {
550 dest[len - 1] = '\0';
551 }
552
553 return required_len;
554}
555
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500556/**
Jon Ashburnc7237a72015-08-03 09:08:46 -0600557 * Given string of three part form "maj.min.pat" convert to a vulkan version
558 * number.
559 */
Mark Young60861ac2016-09-02 11:39:26 -0600560static uint32_t loader_make_version(char *vers_str) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700561 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
Mark Young60861ac2016-09-02 11:39:26 -0600562 char *vers_tok;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600563
Mark Young60861ac2016-09-02 11:39:26 -0600564 if (!vers_str) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600565 return vers;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600566 }
Mark Young60861ac2016-09-02 11:39:26 -0600567
568 vers_tok = strtok(vers_str, ".\"\n\r");
569 if (NULL != vers_tok) {
570 major = (uint16_t)atoi(vers_tok);
571 vers_tok = strtok(NULL, ".\"\n\r");
572 if (NULL != vers_tok) {
573 minor = (uint16_t)atoi(vers_tok);
574 vers_tok = strtok(NULL, ".\"\n\r");
575 if (NULL != vers_tok) {
576 patch = (uint16_t)atoi(vers_tok);
577 }
578 }
579 }
Jon Ashburnc7237a72015-08-03 09:08:46 -0600580
581 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600582}
583
Jon Ashburn23d36b12016-02-02 17:47:28 -0700584bool compare_vk_extension_properties(const VkExtensionProperties *op1,
585 const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800586 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600587}
588
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600589/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600590 * Search the given ext_array for an extension
591 * matching the given vk_ext_prop
592 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700593bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
594 const uint32_t count,
595 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600596 for (uint32_t i = 0; i < count; i++) {
597 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i]))
598 return true;
599 }
600 return false;
601}
602
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600603/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600604 * Search the given ext_list for an extension
605 * matching the given vk_ext_prop
606 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700607bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop,
608 const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600609 for (uint32_t i = 0; i < ext_list->count; i++) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600610 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop))
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600611 return true;
612 }
613 return false;
614}
615
Jon Ashburnb8726962016-04-08 15:03:35 -0600616/**
617 * Search the given ext_list for a device extension matching the given ext_prop
618 */
Jon Ashburncc407a22016-04-15 09:25:03 -0600619bool has_vk_dev_ext_property(
620 const VkExtensionProperties *ext_prop,
621 const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600622 for (uint32_t i = 0; i < ext_list->count; i++) {
623 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop))
624 return true;
625 }
626 return false;
627}
628
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600629/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600630 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600631 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700632static struct loader_layer_properties *
633loader_get_layer_property(const char *name,
634 const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600635 for (uint32_t i = 0; i < layer_list->count; i++) {
636 const VkLayerProperties *item = &layer_list->list[i].info;
637 if (strcmp(name, item->layerName) == 0)
638 return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600639 }
640 return NULL;
641}
642
Jon Ashburne13ecc92015-08-03 17:19:30 -0600643/**
644 * Get the next unused layer property in the list. Init the property to zero.
645 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700646static struct loader_layer_properties *
647loader_get_next_layer_property(const struct loader_instance *inst,
648 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600649 if (layer_list->capacity == 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700650 layer_list->list =
Mark Young0ad83132016-06-30 13:02:42 -0600651 loader_instance_heap_alloc(
652 inst, sizeof(struct loader_layer_properties) * 64,
653 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600654 if (layer_list->list == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700655 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
656 "Out of memory can't add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600657 return NULL;
658 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700659 memset(layer_list->list, 0,
660 sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600661 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
662 }
663
664 // ensure enough room to add an entry
Jon Ashburn23d36b12016-02-02 17:47:28 -0700665 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
666 layer_list->capacity) {
Mark Young0ad83132016-06-30 13:02:42 -0600667 layer_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700668 inst, layer_list->list, layer_list->capacity,
669 layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600670 if (layer_list->list == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700671 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700672 "realloc failed for layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600673 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600674 }
675 layer_list->capacity *= 2;
676 }
677
678 layer_list->count++;
679 return &(layer_list->list[layer_list->count - 1]);
680}
681
682/**
683 * Remove all layer properties entrys from the list
684 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700685void loader_delete_layer_properties(const struct loader_instance *inst,
686 struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700687 uint32_t i, j;
688 struct loader_device_extension_list *dev_ext_list;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600689 if (!layer_list)
690 return;
691
Jon Ashburne13ecc92015-08-03 17:19:30 -0600692 for (i = 0; i < layer_list->count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700693 loader_destroy_generic_list(
694 inst, (struct loader_generic_list *)&layer_list->list[i]
695 .instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700696 dev_ext_list = &layer_list->list[i].device_extension_list;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700697 if (dev_ext_list->capacity > 0 &&
Mark Young0ad83132016-06-30 13:02:42 -0600698 NULL != dev_ext_list->list &&
Jon Ashburn23d36b12016-02-02 17:47:28 -0700699 dev_ext_list->list->entrypoint_count > 0) {
700 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Young0ad83132016-06-30 13:02:42 -0600701 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700702 }
Mark Young0ad83132016-06-30 13:02:42 -0600703 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700704 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700705 loader_destroy_generic_list(inst,
706 (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600707 }
708 layer_list->count = 0;
709
Jon Ashburnb82c1852015-08-11 14:49:54 -0600710 if (layer_list->capacity > 0) {
711 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600712 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600713 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600714}
715
Mark Young3a587792016-08-19 15:25:08 -0600716static VkResult loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700717 const struct loader_instance *inst,
718 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props,
719 const char *lib_name, struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700720 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600721 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600722 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600723
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600724 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600725 /* No EnumerateInstanceExtensionProperties defined */
Mark Young3a587792016-08-19 15:25:08 -0600726 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600727 }
728
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600729 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600730 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700731 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700732 "Error getting Instance extension count from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600733 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600734 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600735
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600736 if (count == 0) {
737 /* No ExtensionProperties to report */
Mark Young3a587792016-08-19 15:25:08 -0600738 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600739 }
740
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600741 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600742
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600743 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600744 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700745 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700746 "Error getting Instance extensions from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600747 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600748 }
Tony Barbour59a47322015-06-24 16:06:58 -0600749
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600750 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600751 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600752
Jon Ashburncc407a22016-04-15 09:25:03 -0600753 bool ext_unsupported =
754 wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600755 if (!ext_unsupported) {
756 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
757 VK_MAJOR(ext_props[i].specVersion),
758 VK_MINOR(ext_props[i].specVersion),
759 VK_PATCH(ext_props[i].specVersion));
760 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
761 "Instance Extension: %s (%s) version %s",
762 ext_props[i].extensionName, lib_name, spec_version);
Mark Young3a587792016-08-19 15:25:08 -0600763 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
764 if (res != VK_SUCCESS) {
765 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
766 "Failed to add %s to Instance extension list",
767 lib_name);
768 goto out;
769 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600770 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600771 }
Mark Young3a587792016-08-19 15:25:08 -0600772out:
773 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600774}
775
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700776/*
777 * Initialize ext_list with the physical device extensions.
778 * The extension properties are passed as inputs in count and ext_props.
779 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700780static VkResult
781loader_init_device_extensions(const struct loader_instance *inst,
782 struct loader_physical_device *phys_dev,
783 uint32_t count, VkExtensionProperties *ext_props,
784 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700785 VkResult res;
786 uint32_t i;
787
Mark Young3a587792016-08-19 15:25:08 -0600788 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
789 sizeof(VkExtensionProperties));
790 if (VK_SUCCESS != res) {
791 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700792 }
793
794 for (i = 0; i < count; i++) {
795 char spec_version[64];
796
Jon Ashburn23d36b12016-02-02 17:47:28 -0700797 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
798 VK_MAJOR(ext_props[i].specVersion),
799 VK_MINOR(ext_props[i].specVersion),
800 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700801 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700802 "Device Extension: %s (%s) version %s",
803 ext_props[i].extensionName,
804 phys_dev->this_icd->this_icd_lib->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700805 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
806 if (res != VK_SUCCESS)
807 return res;
808 }
809
810 return VK_SUCCESS;
811}
812
Jon Ashburn1530c342016-02-26 13:14:27 -0700813VkResult loader_add_device_extensions(const struct loader_instance *inst,
Jon Ashburncc407a22016-04-15 09:25:03 -0600814 PFN_vkEnumerateDeviceExtensionProperties
815 fpEnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700816 VkPhysicalDevice physical_device,
817 const char *lib_name,
818 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600819 uint32_t i, count;
820 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600821 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600822
Piers Daniell295fe402016-03-29 11:51:11 -0600823 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count,
824 NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700825 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700826 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700827 if (!ext_props)
828 return VK_ERROR_OUT_OF_HOST_MEMORY;
Piers Daniell295fe402016-03-29 11:51:11 -0600829 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
830 &count, ext_props);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700831 if (res != VK_SUCCESS)
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700832 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700833 for (i = 0; i < count; i++) {
834 char spec_version[64];
835
Jon Ashburn23d36b12016-02-02 17:47:28 -0700836 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
837 VK_MAJOR(ext_props[i].specVersion),
838 VK_MINOR(ext_props[i].specVersion),
839 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700840 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700841 "Device Extension: %s (%s) version %s",
842 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700843 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
844 if (res != VK_SUCCESS)
845 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600846 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700847 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700848 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
849 "Error getting physical device extension info count from "
850 "library %s",
851 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700852 return res;
853 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600854
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700855 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600856}
857
Mark Young3a587792016-08-19 15:25:08 -0600858VkResult loader_init_generic_list(const struct loader_instance *inst,
Jon Ashburn1530c342016-02-26 13:14:27 -0700859 struct loader_generic_list *list_info,
860 size_t element_size) {
Mark Young84ba0482016-09-02 11:45:00 -0600861 size_t capacity = 32 * element_size;
862 list_info->count = 0;
863 list_info->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600864 list_info->list = loader_instance_heap_alloc(
Mark Young84ba0482016-09-02 11:45:00 -0600865 inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700866 if (list_info->list == NULL) {
Mark Young3a587792016-08-19 15:25:08 -0600867 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600868 }
Mark Young84ba0482016-09-02 11:45:00 -0600869 memset(list_info->list, 0, capacity);
870 list_info->capacity = capacity;
Mark Young3a587792016-08-19 15:25:08 -0600871 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600872}
873
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700874void loader_destroy_generic_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700875 struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600876 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700877 list->count = 0;
878 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600879}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600880
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600881/*
882 * Append non-duplicate extension properties defined in props
883 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700884 * Return
885 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600886 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700887VkResult loader_add_to_ext_list(const struct loader_instance *inst,
888 struct loader_extension_list *ext_list,
889 uint32_t prop_list_count,
890 const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600891 uint32_t i;
892 const VkExtensionProperties *cur_ext;
893
894 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600895 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700896 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600897 if (VK_SUCCESS != res) {
898 return res;
899 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600900 }
901
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600902 for (i = 0; i < prop_list_count; i++) {
903 cur_ext = &props[i];
904
905 // look for duplicates
906 if (has_vk_extension_property(cur_ext, ext_list)) {
907 continue;
908 }
909
910 // add to list at end
911 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700912 if (ext_list->count * sizeof(VkExtensionProperties) >=
913 ext_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -0600914
Mark Young0ad83132016-06-30 13:02:42 -0600915 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700916 inst, ext_list->list, ext_list->capacity,
917 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700918
919 if (ext_list->list == NULL)
920 return VK_ERROR_OUT_OF_HOST_MEMORY;
921
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600922 // double capacity
923 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600924 }
925
Jon Ashburn23d36b12016-02-02 17:47:28 -0700926 memcpy(&ext_list->list[ext_list->count], cur_ext,
927 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600928 ext_list->count++;
929 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700930 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600931}
932
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700933/*
934 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600935 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700936 * Return
937 * Vk_SUCCESS on success
938 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700939VkResult
940loader_add_to_dev_ext_list(const struct loader_instance *inst,
941 struct loader_device_extension_list *ext_list,
942 const VkExtensionProperties *props,
943 uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700944 uint32_t idx;
945 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600946 VkResult res = loader_init_generic_list(
947 inst, (struct loader_generic_list *)ext_list,
948 sizeof(struct loader_dev_ext_props));
949 if (VK_SUCCESS != res) {
950 return res;
951 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700952 }
953
Jon Ashburnb8726962016-04-08 15:03:35 -0600954 // look for duplicates
955 if (has_vk_dev_ext_property(props, ext_list)) {
956 return VK_SUCCESS;
957 }
958
Jon Ashburn23d36b12016-02-02 17:47:28 -0700959 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700960 // add to list at end
961 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700962 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700963
Mark Young0ad83132016-06-30 13:02:42 -0600964 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700965 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
966 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700967
968 if (ext_list->list == NULL)
969 return VK_ERROR_OUT_OF_HOST_MEMORY;
970
971 // double capacity
972 ext_list->capacity *= 2;
973 }
974
Jon Ashburn23d36b12016-02-02 17:47:28 -0700975 memcpy(&ext_list->list[idx].props, props,
976 sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700977 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700978 ext_list->list[idx].entrypoints =
Mark Young0ad83132016-06-30 13:02:42 -0600979 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
980 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
981 if (ext_list->list[idx].entrypoints == NULL) {
982 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700983 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600984 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700985 for (uint32_t i = 0; i < entry_count; i++) {
Mark Young0ad83132016-06-30 13:02:42 -0600986 ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700987 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600988 if (ext_list->list[idx].entrypoints[i] == NULL) {
989 for (uint32_t j = 0; j < i; j++) {
990 loader_instance_heap_free(inst,
991 ext_list->list[idx].entrypoints[j]);
992 }
993 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
994 ext_list->list[idx].entrypoint_count = 0;
995 ext_list->list[idx].entrypoints = NULL;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700996 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600997 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700998 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
999 }
1000 ext_list->count++;
1001
1002 return VK_SUCCESS;
1003}
1004
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001005/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001006 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001007 * Add these to the output layer_list. Don't add duplicates to the output
1008 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001009 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001010static VkResult
1011loader_add_layer_names_to_list(const struct loader_instance *inst,
1012 struct loader_layer_list *output_list,
1013 uint32_t name_count, const char *const *names,
1014 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001015 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001016 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001017
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001018 for (uint32_t i = 0; i < name_count; i++) {
1019 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001020 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001021 if (!layer_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001022 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1023 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001024 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001025 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001026 }
1027
Mark Young0ad83132016-06-30 13:02:42 -06001028 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001029 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001030
1031 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001032}
1033
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001034/*
1035 * Manage lists of VkLayerProperties
1036 */
Jon Ashburne39a4f82015-08-28 13:38:21 -06001037static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001038 struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001039 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Young0ad83132016-06-30 13:02:42 -06001040 list->list = loader_instance_heap_alloc(
1041 inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001042 if (list->list == NULL) {
1043 return false;
1044 }
1045 memset(list->list, 0, list->capacity);
1046 list->count = 0;
1047 return true;
1048}
1049
Jon Ashburne39a4f82015-08-28 13:38:21 -06001050void loader_destroy_layer_list(const struct loader_instance *inst,
Mark Young0ad83132016-06-30 13:02:42 -06001051 struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001052 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001053 if (device) {
1054 loader_device_heap_free(device, layer_list->list);
1055 } else {
1056 loader_instance_heap_free(inst, layer_list->list);
1057 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001058 layer_list->count = 0;
1059 layer_list->capacity = 0;
1060}
1061
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001062/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001063 * Search the given layer list for a list
1064 * matching the given VkLayerProperties
1065 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001066bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
1067 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001068 for (uint32_t i = 0; i < list->count; i++) {
1069 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
1070 return true;
1071 }
1072 return false;
1073}
1074
1075/*
1076 * Search the given layer list for a layer
1077 * matching the given name
1078 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001079bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001080 for (uint32_t i = 0; i < list->count; i++) {
1081 if (strcmp(name, list->list[i].info.layerName) == 0)
1082 return true;
1083 }
1084 return false;
1085}
1086
1087/*
1088 * Append non-duplicate layer properties defined in prop_list
1089 * to the given layer_info list
1090 */
Mark Young0ad83132016-06-30 13:02:42 -06001091VkResult loader_add_to_layer_list(const struct loader_instance *inst,
1092 struct loader_layer_list *list,
1093 uint32_t prop_list_count,
1094 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001095 uint32_t i;
1096 struct loader_layer_properties *layer;
1097
1098 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001099 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001100 }
1101
1102 if (list->list == NULL)
Mark Young0ad83132016-06-30 13:02:42 -06001103 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001104
1105 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001106 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001107
1108 // look for duplicates
1109 if (has_vk_layer_property(&layer->info, list)) {
1110 continue;
1111 }
1112
1113 // add to list at end
1114 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001115 if (list->count * sizeof(struct loader_layer_properties) >=
1116 list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001117
Mark Young0ad83132016-06-30 13:02:42 -06001118 list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001119 inst, list->list, list->capacity, list->capacity * 2,
1120 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001121 if (NULL == list->list) {
1122 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1123 "realloc failed for layer list when attempting to "
1124 "add new layer");
1125 return VK_ERROR_OUT_OF_HOST_MEMORY;
1126 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001127 // double capacity
1128 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001129 }
1130
Jon Ashburn23d36b12016-02-02 17:47:28 -07001131 memcpy(&list->list[list->count], layer,
1132 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001133 list->count++;
1134 }
Mark Young0ad83132016-06-30 13:02:42 -06001135
1136 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001137}
1138
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001139/**
1140 * Search the search_list for any layer with a name
1141 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001142 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001143 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001144 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001145 */
Jon Ashburncc407a22016-04-15 09:25:03 -06001146void loader_find_layer_name_add_list(
1147 const struct loader_instance *inst, const char *name,
1148 const enum layer_type type, const struct loader_layer_list *search_list,
1149 struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001150 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001151 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001152 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001153 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001154 (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001155 /* Found a layer with the same name, add to found_list */
Mark Young0ad83132016-06-30 13:02:42 -06001156 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
1157 found = true;
1158 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001159 }
1160 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001161 if (!found) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001162 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001163 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001164 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001165}
1166
Jon Ashburn23d36b12016-02-02 17:47:28 -07001167static VkExtensionProperties *
1168get_extension_property(const char *name,
1169 const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001170 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001171 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001172 return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001173 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001174 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001175}
1176
Jon Ashburn23d36b12016-02-02 17:47:28 -07001177static VkExtensionProperties *
1178get_dev_extension_property(const char *name,
1179 const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001180 for (uint32_t i = 0; i < list->count; i++) {
1181 if (strcmp(name, list->list[i].props.extensionName) == 0)
1182 return &list->list[i].props;
1183 }
1184 return NULL;
1185}
1186
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001187/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001188 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001189 * the extension must provide two entry points for the loader to use:
1190 * - "trampoline" entry point - this is the address returned by GetProcAddr
1191 * and will always do what's necessary to support a global call.
1192 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001193 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001194 * the extension for the appropriate ICDs that are available.
1195 * There is no generic mechanism for including these functions, the references
1196 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001197 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1198 * GetProcAddr requests
1199 * loader_coalesce_extensions(void) - add extension records to the list of
1200 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001201 * extension available to the app.
1202 * instance_disp - add function pointer for terminator function to this array.
1203 * The extension itself should be in a separate file that will be
1204 * linked directly with the loader.
1205 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001206
Mark Young3a587792016-08-19 15:25:08 -06001207VkResult loader_get_icd_loader_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001208 const struct loader_instance *inst, struct loader_icd_libs *icd_libs,
1209 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001210 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001211 VkResult res = VK_SUCCESS;
1212
Jon Ashburn23d36b12016-02-02 17:47:28 -07001213 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1214 "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001215
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001216 // traverse scanned icd list adding non-duplicate extensions to the list
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001217 for (uint32_t i = 0; i < icd_libs->count; i++) {
Mark Young3a587792016-08-19 15:25:08 -06001218 res = loader_init_generic_list(inst,
1219 (struct loader_generic_list *)&icd_exts,
1220 sizeof(VkExtensionProperties));
1221 if (VK_SUCCESS != res) {
1222 goto out;
1223 }
1224 res = loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001225 inst, icd_libs->list[i].EnumerateInstanceExtensionProperties,
1226 icd_libs->list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001227 if (VK_SUCCESS == res) {
1228 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count,
1229 icd_exts.list);
1230 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001231 loader_destroy_generic_list(inst,
1232 (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001233 if (VK_SUCCESS != res) {
1234 goto out;
1235 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001236 };
1237
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001238 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001239 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001240
1241out:
1242 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001243}
1244
Jon Ashburne39a4f82015-08-28 13:38:21 -06001245struct loader_icd *loader_get_icd_and_device(const VkDevice device,
Mark Young16573c72016-06-28 10:52:43 -06001246 struct loader_device **found_dev,
1247 uint32_t *icd_index) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001248 *found_dev = NULL;
Mark Young16573c72016-06-28 10:52:43 -06001249 uint32_t index = 0;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001250 for (struct loader_instance *inst = loader.instances; inst;
1251 inst = inst->next) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001252 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001253 for (struct loader_device *dev = icd->logical_device_list; dev;
1254 dev = dev->next)
1255 /* Value comparison of device prevents object wrapping by layers
1256 */
1257 if (loader_get_dispatch(dev->device) ==
1258 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001259 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001260 if (NULL != icd_index) {
1261 *icd_index = index;
1262 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001263 return icd;
1264 }
Mark Young16573c72016-06-28 10:52:43 -06001265 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001266 }
1267 }
1268 return NULL;
1269}
1270
Mark Young0ad83132016-06-30 13:02:42 -06001271void loader_destroy_logical_device(const struct loader_instance *inst,
1272 struct loader_device *dev,
1273 const VkAllocationCallbacks *pAllocator) {
1274 if (pAllocator) {
1275 dev->alloc_callbacks = *pAllocator;
1276 }
Mark Young0ad83132016-06-30 13:02:42 -06001277 if (NULL != dev->activated_layer_list.list) {
1278 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1279 }
1280 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001281}
1282
Jon Ashburn1530c342016-02-26 13:14:27 -07001283struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001284loader_create_logical_device(const struct loader_instance *inst,
1285 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001286 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001287#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1288 {
1289#else
1290 if (pAllocator) {
1291 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1292 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1293 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1294 } else {
1295#endif
1296 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1297 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001298
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001299 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001300 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1301 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001302 return NULL;
1303 }
1304
1305 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001306 if (pAllocator) {
1307 new_dev->alloc_callbacks = *pAllocator;
1308 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001309
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001310 return new_dev;
1311}
1312
Piers Daniell295fe402016-03-29 11:51:11 -06001313void loader_add_logical_device(const struct loader_instance *inst,
1314 struct loader_icd *icd,
1315 struct loader_device *dev) {
1316 dev->next = icd->logical_device_list;
1317 icd->logical_device_list = dev;
1318}
1319
Jon Ashburn23d36b12016-02-02 17:47:28 -07001320void loader_remove_logical_device(const struct loader_instance *inst,
1321 struct loader_icd *icd,
Mark Young0ad83132016-06-30 13:02:42 -06001322 struct loader_device *found_dev,
1323 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001324 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001325
1326 if (!icd || !found_dev)
1327 return;
1328
1329 prev_dev = NULL;
1330 dev = icd->logical_device_list;
1331 while (dev && dev != found_dev) {
1332 prev_dev = dev;
1333 dev = dev->next;
1334 }
1335
1336 if (prev_dev)
1337 prev_dev->next = found_dev->next;
1338 else
1339 icd->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001340 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001341}
1342
Jon Ashburn23d36b12016-02-02 17:47:28 -07001343static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0ad83132016-06-30 13:02:42 -06001344 struct loader_icd *icd,
1345 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001346 ptr_inst->total_icd_count--;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001347 for (struct loader_device *dev = icd->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001348 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001349 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001350 dev = next_dev;
1351 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001352
Mark Young0ad83132016-06-30 13:02:42 -06001353 loader_instance_heap_free(ptr_inst, icd);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001354}
1355
Jon Ashburn23d36b12016-02-02 17:47:28 -07001356static struct loader_icd *
1357loader_icd_create(const struct loader_instance *inst) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001358 struct loader_icd *icd;
1359
Mark Youngdb13a2a2016-09-06 13:53:03 -06001360 icd = loader_instance_heap_alloc(inst, sizeof(struct loader_icd),
Mark Young0ad83132016-06-30 13:02:42 -06001361 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngdb13a2a2016-09-06 13:53:03 -06001362 if (!icd) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001363 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001364 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001365
Mark Youngdb13a2a2016-09-06 13:53:03 -06001366 memset(icd, 0, sizeof(struct loader_icd));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001367
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001368 return icd;
1369}
1370
Jon Ashburn23d36b12016-02-02 17:47:28 -07001371static struct loader_icd *
1372loader_icd_add(struct loader_instance *ptr_inst,
1373 const struct loader_scanned_icds *icd_lib) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001374 struct loader_icd *icd;
1375
Jon Ashburne39a4f82015-08-28 13:38:21 -06001376 icd = loader_icd_create(ptr_inst);
Mark Youngdb13a2a2016-09-06 13:53:03 -06001377 if (!icd) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001378 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001379 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001380
Jon Ashburn3d002332015-08-20 16:35:30 -06001381 icd->this_icd_lib = icd_lib;
1382 icd->this_instance = ptr_inst;
1383
Chia-I Wu13a61a52014-08-04 11:18:20 +08001384 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -07001385 icd->next = ptr_inst->icds;
1386 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001387 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001388
1389 return icd;
1390}
Jon Ashburn17b4c862016-04-25 11:09:37 -06001391/**
1392 * Determine the ICD interface version to use.
1393 * @param icd
1394 * @param pVersion Output parameter indicating which version to use or 0 if
1395 * the negotiation API is not supported by the ICD
1396 * @return bool indicating true if the selected interface version is supported
1397 * by the loader, false indicates the version is not supported
1398 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1399 * vk_icdNegotiateLoaderICDInterfaceVersion
1400 * version 1 supports vk_icdGetInstanceProcAddr
1401 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1402 */
1403bool loader_get_icd_interface_version(
1404 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1405 uint32_t *pVersion) {
1406
1407 if (fp_negotiate_icd_version == NULL) {
1408 // ICD does not support the negotiation API, it supports version 0 or 1
1409 // calling code must determine if it is version 0 or 1
1410 *pVersion = 0;
1411 } else {
1412 // ICD supports the negotiation API, so call it with the loader's
1413 // latest version supported
1414 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1415 VkResult result = fp_negotiate_icd_version(pVersion);
1416
1417 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1418 // ICD no longer supports the loader's latest interface version so
1419 // fail loading the ICD
1420 return false;
1421 }
1422 }
1423
1424#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1425 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1426 // Loader no longer supports the ICD's latest interface version so fail
1427 // loading the ICD
1428 return false;
1429 }
1430#endif
1431 return true;
1432}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001433
Jon Ashburn23d36b12016-02-02 17:47:28 -07001434void loader_scanned_icd_clear(const struct loader_instance *inst,
1435 struct loader_icd_libs *icd_libs) {
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001436 if (icd_libs->capacity == 0)
1437 return;
1438 for (uint32_t i = 0; i < icd_libs->count; i++) {
1439 loader_platform_close_library(icd_libs->list[i].handle);
Mark Young0ad83132016-06-30 13:02:42 -06001440 loader_instance_heap_free(inst, icd_libs->list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001441 }
Mark Young0ad83132016-06-30 13:02:42 -06001442 loader_instance_heap_free(inst, icd_libs->list);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001443 icd_libs->capacity = 0;
1444 icd_libs->count = 0;
1445 icd_libs->list = NULL;
1446}
1447
Mark Young0ad83132016-06-30 13:02:42 -06001448static VkResult loader_scanned_icd_init(const struct loader_instance *inst,
1449 struct loader_icd_libs *icd_libs) {
1450 VkResult err = VK_SUCCESS;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001451 loader_scanned_icd_clear(inst, icd_libs);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001452 icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
Mark Young0ad83132016-06-30 13:02:42 -06001453 icd_libs->list = loader_instance_heap_alloc(
1454 inst, icd_libs->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1455 if (NULL == icd_libs->list) {
1456 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1457 "realloc failed for layer list when attempting to add new layer");
1458 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1459 }
1460 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001461}
1462
Mark Young3a587792016-08-19 15:25:08 -06001463static VkResult loader_scanned_icd_add(const struct loader_instance *inst,
1464 struct loader_icd_libs *icd_libs,
1465 const char *filename,
1466 uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001467 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001468 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001469 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001470 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001471 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001472 struct loader_scanned_icds *new_node;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001473 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001474 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001475
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001476 /* TODO implement smarter opening/closing of libraries. For now this
1477 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001478 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001479 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001480 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001481 loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001482 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001483 }
1484
Jon Ashburn17b4c862016-04-25 11:09:37 -06001485 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001486 fp_negotiate_icd_version = loader_platform_get_proc_address(
1487 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001488
1489 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001490 &interface_vers)) {
1491 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1492 "ICD (%s) doesn't support interface version compatible"
1493 "with loader, skip this ICD %s",
1494 filename);
Mark Young3a587792016-08-19 15:25:08 -06001495 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001496 }
1497
Jon Ashburn23d36b12016-02-02 17:47:28 -07001498 fp_get_proc_addr =
1499 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001500 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001501 assert(interface_vers == 0);
1502 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001503 fp_get_proc_addr =
1504 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001505 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001506 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1507 loader_platform_get_proc_address_error(
1508 "vk_icdGetInstanceProcAddr"));
Mark Young3a587792016-08-19 15:25:08 -06001509 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001510 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001511 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001512 "Using deprecated ICD interface of "
1513 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001514 "vk_icdGetInstanceProcAddr for ICD %s",
1515 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001516 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001517 fp_create_inst =
1518 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001519 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001520 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1521 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1522 "for ICD %s",
1523 filename);
Mark Young3a587792016-08-19 15:25:08 -06001524 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001525 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001526 fp_get_inst_ext_props = loader_platform_get_proc_address(
1527 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001528 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001529 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1530 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001531 "via dlsym/loadlibrary for ICD %s",
1532 filename);
Mark Young3a587792016-08-19 15:25:08 -06001533 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001534 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001535 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001536 // Use newer interface version 1 or later
1537 if (interface_vers == 0)
1538 interface_vers = 1;
1539
Jon Ashburn23d36b12016-02-02 17:47:28 -07001540 fp_create_inst =
1541 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001542 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001543 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1544 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001545 "vk_icdGetInstanceProcAddr for ICD %s",
1546 filename);
Mark Young3a587792016-08-19 15:25:08 -06001547 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001548 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001549 fp_get_inst_ext_props =
1550 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1551 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001552 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001553 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1554 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001555 "via vk_icdGetInstanceProcAddr for ICD %s",
1556 filename);
Mark Young3a587792016-08-19 15:25:08 -06001557 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001558 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001559 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001560
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001561 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001562 if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
1563 icd_libs->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001564
Mark Young0ad83132016-06-30 13:02:42 -06001565 icd_libs->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001566 inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
1567 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001568 if (NULL == icd_libs->list) {
Mark Young3a587792016-08-19 15:25:08 -06001569 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001570 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1571 "realloc failed on icd library list");
Mark Young3a587792016-08-19 15:25:08 -06001572 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001573 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001574 // double capacity
1575 icd_libs->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001576 }
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001577 new_node = &(icd_libs->list[icd_libs->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001578
1579 new_node->handle = handle;
Jon Ashburn005617f2015-11-17 17:35:40 -07001580 new_node->api_version = api_version;
Jon Ashburnc624c882015-07-16 10:17:29 -06001581 new_node->GetInstanceProcAddr = fp_get_proc_addr;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001582 new_node->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Jon Ashburn46888392015-01-29 15:45:51 -07001583 new_node->CreateInstance = fp_create_inst;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001584 new_node->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001585
Mark Young0ad83132016-06-30 13:02:42 -06001586 new_node->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001587 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001588 if (NULL == new_node->lib_name) {
Mark Young3a587792016-08-19 15:25:08 -06001589 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001590 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001591 "Out of memory can't add icd");
Mark Young3a587792016-08-19 15:25:08 -06001592 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001593 }
1594 strcpy(new_node->lib_name, filename);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001595 icd_libs->count++;
Mark Young3a587792016-08-19 15:25:08 -06001596
1597out:
1598
1599 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001600}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001601
Jon Ashburn23d36b12016-02-02 17:47:28 -07001602static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
1603 const PFN_vkGetInstanceProcAddr fp_gipa) {
1604/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001605
Jon Ashburn23d36b12016-02-02 17:47:28 -07001606#define LOOKUP_GIPA(func, required) \
1607 do { \
1608 icd->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1609 if (!icd->func && required) { \
1610 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001611 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001612 loader_platform_get_proc_address_error("vk" #func)); \
1613 return false; \
1614 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001615 } while (0)
1616
Jon Ashburnc624c882015-07-16 10:17:29 -06001617 LOOKUP_GIPA(GetDeviceProcAddr, true);
1618 LOOKUP_GIPA(DestroyInstance, true);
1619 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1620 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1621 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001622 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001623 LOOKUP_GIPA(CreateDevice, true);
1624 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1625 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001626 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001627 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001628 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001629 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1630 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001631 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001632 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1633 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1634 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001635 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1636 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1637 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1638 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1639 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1640 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1641 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1642 LOOKUP_GIPA(DestroySurfaceKHR, false);
Mark Young16573c72016-06-28 10:52:43 -06001643 LOOKUP_GIPA(CreateSwapchainKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001644#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Young16573c72016-06-28 10:52:43 -06001645 LOOKUP_GIPA(CreateWin32SurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001646 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1647#endif
1648#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001649 LOOKUP_GIPA(CreateXcbSurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001650 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1651#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001652#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001653 LOOKUP_GIPA(CreateXlibSurfaceKHR, false);
Karl Schultz65d20182016-03-08 07:55:27 -07001654 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1655#endif
Mark Younga7c51fd2016-09-16 10:18:42 -06001656#ifdef VK_USE_PLATFORM_MIR_KHR
1657 LOOKUP_GIPA(CreateMirSurfaceKHR, false);
1658 LOOKUP_GIPA(GetPhysicalDeviceMirPresentationSupportKHR, false);
1659#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001660#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Young16573c72016-06-28 10:52:43 -06001661 LOOKUP_GIPA(CreateWaylandSurfaceKHR, false);
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001662 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1663#endif
James Jones389dc0c2016-08-18 23:41:19 +01001664 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001665
Jon Ashburnc624c882015-07-16 10:17:29 -06001666#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001667
Jon Ashburnc624c882015-07-16 10:17:29 -06001668 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001669}
1670
Jon Ashburn23d36b12016-02-02 17:47:28 -07001671static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001672 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001673
1674 if (g_loader_debug > 0)
1675 return;
1676
1677 g_loader_debug = 0;
1678
1679 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001680 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001681 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001682 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001683 size_t len;
1684
1685 if (p)
1686 len = p - env;
1687 else
1688 len = strlen(env);
1689
1690 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001691 if (strncmp(env, "all", len) == 0) {
1692 g_loader_debug = ~0u;
1693 g_loader_log_msgs = ~0u;
1694 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001695 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001696 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001697 } else if (strncmp(env, "info", len) == 0) {
1698 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001699 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001700 } else if (strncmp(env, "perf", len) == 0) {
1701 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001702 g_loader_log_msgs |=
1703 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001704 } else if (strncmp(env, "error", len) == 0) {
1705 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001706 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001707 } else if (strncmp(env, "debug", len) == 0) {
1708 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001709 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001710 }
1711 }
1712
1713 if (!p)
1714 break;
1715
1716 env = p + 1;
1717 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001718
Mark Young0ad83132016-06-30 13:02:42 -06001719 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001720}
1721
Jon Ashburn23d36b12016-02-02 17:47:28 -07001722void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001723 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001724 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001725 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001726
1727 // initialize logging
1728 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001729
1730 // initial cJSON to use alloc callbacks
1731 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001732 .malloc_fn = loader_instance_tls_heap_alloc,
1733 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001734 };
1735 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001736}
1737
Jon Ashburn2077e382015-06-29 11:25:34 -06001738struct loader_manifest_files {
1739 uint32_t count;
1740 char **filename_list;
1741};
1742
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001743/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001744 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001745 *
1746 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001747 * A pointer to first char in the next path.
1748 * The next path (or NULL) in the list is returned in next_path.
1749 * Note: input string is modified in some cases. PASS IN A COPY!
1750 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001751static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001752 uint32_t len;
1753 char *next;
1754
1755 if (path == NULL)
1756 return NULL;
1757 next = strchr(path, PATH_SEPERATOR);
1758 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001759 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001760 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001761 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001762 *next = '\0';
1763 next++;
1764 }
1765
1766 return next;
1767}
1768
1769/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001770 * Given a path which is absolute or relative, expand the path if relative or
1771 * leave the path unmodified if absolute. The base path to prepend to relative
1772 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001773 *
1774 * \returns
1775 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001776 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001777static void loader_expand_path(const char *path, const char *rel_base,
1778 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001779 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001780 // do not prepend a base to an absolute path
1781 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001782 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001783
1784 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001785}
1786
1787/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001788 * Given a filename (file) and a list of paths (dir), try to find an existing
1789 * file in the paths. If filename already is a path then no
1790 * searching in the given paths.
1791 *
1792 * \returns
1793 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001794 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001795static void loader_get_fullpath(const char *file, const char *dirs,
1796 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001797 if (!loader_platform_is_path(file) && *dirs) {
1798 char *dirs_copy, *dir, *next_dir;
1799
1800 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1801 strcpy(dirs_copy, dirs);
1802
Jon Ashburn23d36b12016-02-02 17:47:28 -07001803 // find if file exists after prepending paths in given list
1804 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001805 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001806 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1807 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001808 if (loader_platform_file_exists(out_fullpath)) {
1809 return;
1810 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001811 }
1812 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001813
Jon Ashburn2077e382015-06-29 11:25:34 -06001814 snprintf(out_fullpath, out_size, "%s", file);
1815}
1816
1817/**
1818 * Read a JSON file into a buffer.
1819 *
1820 * \returns
1821 * A pointer to a cJSON object representing the JSON parse tree.
1822 * This returned buffer should be freed by caller.
1823 */
Mark Young3a587792016-08-19 15:25:08 -06001824static VkResult loader_get_json(const struct loader_instance *inst,
1825 const char *filename, cJSON **json) {
1826 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001827 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001828 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001829 VkResult res = VK_SUCCESS;
1830
1831 if (NULL == json) {
1832 res = VK_ERROR_INITIALIZATION_FAILED;
1833 goto out;
1834 }
1835
1836 *json = NULL;
1837
Jon Ashburn23d36b12016-02-02 17:47:28 -07001838 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001839 if (!file) {
Mark Young3a587792016-08-19 15:25:08 -06001840 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001841 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1842 "Couldn't open JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001843 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001844 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001845 fseek(file, 0, SEEK_END);
1846 len = ftell(file);
1847 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001848 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001849 if (json_buf == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06001850 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001851 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1852 "Out of memory can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001853 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001854 }
1855 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Young3a587792016-08-19 15:25:08 -06001856 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001857 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1858 "fread failed can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001859 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001860 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001861 json_buf[len] = '\0';
1862
Jon Ashburn23d36b12016-02-02 17:47:28 -07001863 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001864 *json = cJSON_Parse(json_buf);
1865 if (*json == NULL) {
1866 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001867 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1868 "Can't parse JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001869 goto out;
1870 }
1871
1872out:
1873 if (NULL != file) {
1874 fclose(file);
1875 }
1876
1877 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001878}
1879
1880/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001881 * Do a deep copy of the loader_layer_properties structure.
1882 */
Mark Young0ad83132016-06-30 13:02:42 -06001883VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1884 struct loader_layer_properties *dst,
1885 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001886 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001887 memcpy(dst, src, sizeof(*src));
1888 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001889 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1890 src->instance_extension_list.count,
1891 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1892 if (NULL == dst->instance_extension_list.list) {
1893 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1894 "alloc failed for instance extension list");
1895 return VK_ERROR_OUT_OF_HOST_MEMORY;
1896 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001897 dst->instance_extension_list.capacity =
1898 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001899 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001900 dst->instance_extension_list.capacity);
1901 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001902 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1903 src->device_extension_list.count,
1904 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1905 if (NULL == dst->device_extension_list.list) {
1906 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1907 "alloc failed for device extension list");
1908 return VK_ERROR_OUT_OF_HOST_MEMORY;
1909 }
1910 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) *
1911 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001912
Jon Ashburn23d36b12016-02-02 17:47:28 -07001913 dst->device_extension_list.capacity =
1914 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001915 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001916 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001917 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001918 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001919 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001920 dst->device_extension_list.list->entrypoints =
1921 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1922 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1923 if (NULL == dst->device_extension_list.list->entrypoints) {
1924 loader_log(
1925 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1926 "alloc failed for device extension list entrypoint array");
1927 return VK_ERROR_OUT_OF_HOST_MEMORY;
1928 }
1929 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
1930
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001931 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001932 dst->device_extension_list.list->entrypoints[i] =
1933 loader_instance_heap_alloc(
1934 inst,
1935 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1936 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1937 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1938 loader_log(
1939 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1940 "alloc failed for device extension list entrypoint %d", i);
1941 return VK_ERROR_OUT_OF_HOST_MEMORY;
1942 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001943 strcpy(dst->device_extension_list.list->entrypoints[i],
1944 src->device_extension_list.list->entrypoints[i]);
1945 }
1946 }
Mark Young0ad83132016-06-30 13:02:42 -06001947
1948 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001949}
1950
Jon Ashburn86a527a2016-02-10 20:59:26 -07001951static bool
1952loader_find_layer_name_list(const char *name,
1953 const struct loader_layer_list *layer_list) {
1954 if (!layer_list)
1955 return false;
1956 for (uint32_t j = 0; j < layer_list->count; j++)
1957 if (!strcmp(name, layer_list->list[j].info.layerName))
1958 return true;
1959 return false;
1960}
1961
1962static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1963 const char **layer_list) {
1964 if (!layer_list)
1965 return false;
1966 for (uint32_t j = 0; j < layer_count; j++)
1967 if (!strcmp(name, layer_list[j]))
1968 return true;
1969 return false;
1970}
1971
Jon Ashburn491cd042016-05-16 14:01:18 -06001972bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001973 const char *name, uint32_t layer_count,
1974 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1975 if (!layer_list)
1976 return false;
1977 for (uint32_t j = 0; j < layer_count; j++)
1978 if (!strcmp(name, layer_list[j]))
1979 return true;
1980 return false;
1981}
1982
1983/**
1984 * Searches through an array of layer names (ppp_layer_names) looking for a
1985 * layer key_name.
1986 * If not found then simply returns updating nothing.
1987 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12001988 * Any duplicate (pre-existing) expand_names in layer names are removed.
1989 * Order is otherwise preserved, with the layer key_name being replaced by the
1990 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07001991 * @param inst
1992 * @param layer_count
1993 * @param ppp_layer_names
1994 */
Mark Young0ad83132016-06-30 13:02:42 -06001995VkResult loader_expand_layer_names(
1996 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07001997 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06001998 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07001999
Jon Ashburncc407a22016-04-15 09:25:03 -06002000 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002001
Jon Ashburncc407a22016-04-15 09:25:03 -06002002 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06002003 (char const **)pp_src_layers)) {
2004 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06002005 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06002006 }
Jon Ashburn71483442016-02-11 18:59:43 -07002007
Jon Ashburn86a527a2016-02-10 20:59:26 -07002008 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2009 "Found meta layer %s, replacing with actual layer group",
2010 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12002011
Jon Ashburn491cd042016-05-16 14:01:18 -06002012 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06002013 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06002014 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2015 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002016 if (NULL == pp_dst_layers) {
2017 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2018 "alloc failed for dst layer array");
2019 return VK_ERROR_OUT_OF_HOST_MEMORY;
2020 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002021
2022 // copy layers from src to dst, stripping key_name and anything in
2023 // expand_names.
2024 uint32_t src_index, dst_index = 0;
2025 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002026 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
2027 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002028 continue;
2029 }
2030
2031 if (!strcmp(pp_src_layers[src_index], key_name)) {
2032 // insert all expand_names in place of key_name
2033 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06002034 for (expand_index = 0; expand_index < expand_count;
2035 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002036 pp_dst_layers[dst_index++] = expand_names[expand_index];
2037 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002038 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002039 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002040
2041 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002042 }
2043
Chris Forbesbd9de052016-04-06 20:49:02 +12002044 *ppp_layer_names = pp_dst_layers;
2045 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002046
2047 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002048}
2049
Chris Forbesbd9de052016-04-06 20:49:02 +12002050void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
2051 const VkInstanceCreateInfo *orig,
2052 VkInstanceCreateInfo *ours) {
2053 /* Free the layer names array iff we had to reallocate it */
2054 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002055 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002056 }
2057}
2058
Jon Ashburn491cd042016-05-16 14:01:18 -06002059void loader_init_std_validation_props(struct loader_layer_properties *props) {
2060 memset(props, 0, sizeof(struct loader_layer_properties));
2061 props->type = VK_LAYER_TYPE_META_EXPLICT;
2062 strncpy(props->info.description, "LunarG Standard Validation Layer",
2063 sizeof (props->info.description));
2064 props->info.implementationVersion = 1;
2065 strncpy(props->info.layerName, std_validation_str,
2066 sizeof (props->info.layerName));
2067 // TODO what about specVersion? for now insert loader's built version
2068 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2069}
2070
Jon Ashburn86a527a2016-02-10 20:59:26 -07002071/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002072 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002073 * the set of required layer names. If found then it adds a meta property to the
2074 * layer list.
2075 * Assumes the required layers are the same for both instance and device lists.
2076 * @param inst
2077 * @param layer_count number of layers in layer_names
2078 * @param layer_names array of required layer names
2079 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002080 */
2081static void loader_add_layer_property_meta(
2082 const struct loader_instance *inst, uint32_t layer_count,
2083 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002084 struct loader_layer_list *layer_instance_list) {
2085 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002086 bool found;
2087 struct loader_layer_list *layer_list;
2088
Jon Ashburn491cd042016-05-16 14:01:18 -06002089 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002090 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002091 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002092 return;
2093
Jon Ashburn491cd042016-05-16 14:01:18 -06002094
2095 layer_list = layer_instance_list;
2096
2097 found = true;
2098 if (layer_list == NULL)
2099 return;
2100 for (i = 0; i < layer_count; i++) {
2101 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002102 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002103 found = false;
2104 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002105 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002106
2107 struct loader_layer_properties *props;
2108 if (found) {
2109 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002110 if (NULL == props) {
2111 // Error already triggered in loader_get_next_layer_property.
2112 return;
2113 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002114 loader_init_std_validation_props(props);
2115
2116 }
2117
Jon Ashburn86a527a2016-02-10 20:59:26 -07002118}
2119
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002120static void loader_read_json_layer(
2121 const struct loader_instance *inst,
2122 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2123 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2124 char *temp;
2125 char *name, *type, *library_path, *api_version;
2126 char *implementation_version, *description;
2127 cJSON *ext_item;
2128 VkExtensionProperties ext_prop;
2129
Mark Young0ad83132016-06-30 13:02:42 -06002130/*
2131 * The following are required in the "layer" object:
2132 * (required) "name"
2133 * (required) "type"
2134 * (required) “library_path”
2135 * (required) “api_version”
2136 * (required) “implementation_version”
2137 * (required) “description”
2138 * (required for implicit layers) “disable_environment”
2139 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002140
Jon Ashburn23d36b12016-02-02 17:47:28 -07002141#define GET_JSON_OBJECT(node, var) \
2142 { \
2143 var = cJSON_GetObjectItem(node, #var); \
2144 if (var == NULL) { \
2145 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002146 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002147 "Didn't find required layer object %s in manifest " \
2148 "JSON file, skipping this layer", \
2149 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002150 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002151 } \
2152 }
2153#define GET_JSON_ITEM(node, var) \
2154 { \
2155 item = cJSON_GetObjectItem(node, #var); \
2156 if (item == NULL) { \
2157 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002158 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002159 "Didn't find required layer value %s in manifest JSON " \
2160 "file, skipping this layer", \
2161 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002162 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002163 } \
2164 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002165 if (temp == NULL) { \
2166 layer_node = layer_node->next; \
2167 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2168 "Problem accessing layer value %s in manifest JSON " \
2169 "file, skipping this layer", \
2170 #var); \
2171 return; \
2172 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002173 temp[strlen(temp) - 1] = '\0'; \
2174 var = loader_stack_alloc(strlen(temp) + 1); \
2175 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002176 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002177 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002178 GET_JSON_ITEM(layer_node, name)
2179 GET_JSON_ITEM(layer_node, type)
2180 GET_JSON_ITEM(layer_node, library_path)
2181 GET_JSON_ITEM(layer_node, api_version)
2182 GET_JSON_ITEM(layer_node, implementation_version)
2183 GET_JSON_ITEM(layer_node, description)
2184 if (is_implicit) {
2185 GET_JSON_OBJECT(layer_node, disable_environment)
2186 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002187#undef GET_JSON_ITEM
2188#undef GET_JSON_OBJECT
2189
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002190 // add list entry
2191 struct loader_layer_properties *props = NULL;
2192 if (!strcmp(type, "DEVICE")) {
2193 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2194 "Device layers are deprecated skipping this layer");
2195 layer_node = layer_node->next;
2196 return;
2197 }
2198 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2199 // layers that must work with older loaders
2200 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2201 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002202 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002203 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002204 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002205 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002206 if (NULL == props) {
2207 // Error already triggered in loader_get_next_layer_property.
2208 return;
2209 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002210 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2211 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2212 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002213
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002214 if (props == NULL) {
2215 layer_node = layer_node->next;
2216 return;
2217 }
Jon Ashburn15315172015-07-07 15:06:25 -06002218
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002219 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2220 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2221
2222 char *fullpath = props->lib_name;
2223 char *rel_base;
2224 if (loader_platform_is_path(library_path)) {
2225 // a relative or absolute path
2226 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2227 strcpy(name_copy, filename);
2228 rel_base = loader_platform_dirname(name_copy);
2229 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2230 } else {
2231 // a filename which is assumed in a system directory
2232 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2233 MAX_STRING_SIZE, fullpath);
2234 }
2235 props->info.specVersion = loader_make_version(api_version);
2236 props->info.implementationVersion = atoi(implementation_version);
2237 strncpy((char *)props->info.description, description,
2238 sizeof(props->info.description));
2239 props->info.description[sizeof(props->info.description) - 1] = '\0';
2240 if (is_implicit) {
2241 if (!disable_environment || !disable_environment->child) {
2242 loader_log(
2243 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2244 "Didn't find required layer child value disable_environment"
2245 "in manifest JSON file, skipping this layer");
2246 layer_node = layer_node->next;
2247 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002248 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002249 strncpy(props->disable_env_var.name, disable_environment->child->string,
2250 sizeof(props->disable_env_var.name));
2251 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2252 '\0';
2253 strncpy(props->disable_env_var.value,
2254 disable_environment->child->valuestring,
2255 sizeof(props->disable_env_var.value));
2256 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2257 '\0';
2258 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002259
Jon Ashburn23d36b12016-02-02 17:47:28 -07002260/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002261* Now get all optional items and objects and put in list:
2262* functions
2263* instance_extensions
2264* device_extensions
2265* enable_environment (implicit layers only)
2266*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002267#define GET_JSON_OBJECT(node, var) \
2268 { var = cJSON_GetObjectItem(node, #var); }
2269#define GET_JSON_ITEM(node, var) \
2270 { \
2271 item = cJSON_GetObjectItem(node, #var); \
2272 if (item != NULL) { \
2273 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002274 if (temp != NULL) { \
2275 temp[strlen(temp) - 1] = '\0'; \
2276 var = loader_stack_alloc(strlen(temp) + 1); \
2277 strcpy(var, &temp[1]); \
2278 cJSON_Free(temp); \
2279 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002280 } \
2281 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002282
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002283 cJSON *instance_extensions, *device_extensions, *functions,
2284 *enable_environment;
2285 cJSON *entrypoints;
2286 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2287 char **entry_array;
2288 vkGetInstanceProcAddr = NULL;
2289 vkGetDeviceProcAddr = NULL;
2290 spec_version = NULL;
2291 entrypoints = NULL;
2292 entry_array = NULL;
2293 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002294
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002295 /**
2296 * functions
2297 * vkGetInstanceProcAddr
2298 * vkGetDeviceProcAddr
2299 */
2300 GET_JSON_OBJECT(layer_node, functions)
2301 if (functions != NULL) {
2302 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2303 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2304 if (vkGetInstanceProcAddr != NULL)
2305 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2306 sizeof(props->functions.str_gipa));
2307 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2308 if (vkGetDeviceProcAddr != NULL)
2309 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2310 sizeof(props->functions.str_gdpa));
2311 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2312 }
2313 /**
2314 * instance_extensions
2315 * array of
2316 * name
2317 * spec_version
2318 */
2319 GET_JSON_OBJECT(layer_node, instance_extensions)
2320 if (instance_extensions != NULL) {
2321 int count = cJSON_GetArraySize(instance_extensions);
2322 for (i = 0; i < count; i++) {
2323 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2324 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002325 if (name != NULL) {
2326 strncpy(ext_prop.extensionName, name,
2327 sizeof(ext_prop.extensionName));
2328 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2329 '\0';
2330 }
Mark Young0ad83132016-06-30 13:02:42 -06002331 GET_JSON_ITEM(ext_item, spec_version)
2332 if (NULL != spec_version) {
2333 ext_prop.specVersion = atoi(spec_version);
2334 } else {
2335 ext_prop.specVersion = 0;
2336 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002337 bool ext_unsupported =
2338 wsi_unsupported_instance_extension(&ext_prop);
2339 if (!ext_unsupported) {
2340 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2341 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002342 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002343 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002344 }
2345 /**
2346 * device_extensions
2347 * array of
2348 * name
2349 * spec_version
2350 * entrypoints
2351 */
2352 GET_JSON_OBJECT(layer_node, device_extensions)
2353 if (device_extensions != NULL) {
2354 int count = cJSON_GetArraySize(device_extensions);
2355 for (i = 0; i < count; i++) {
2356 ext_item = cJSON_GetArrayItem(device_extensions, i);
2357 GET_JSON_ITEM(ext_item, name)
2358 GET_JSON_ITEM(ext_item, spec_version)
2359 if (name != NULL) {
2360 strncpy(ext_prop.extensionName, name,
2361 sizeof(ext_prop.extensionName));
2362 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2363 '\0';
2364 }
Mark Young0ad83132016-06-30 13:02:42 -06002365 if (NULL != spec_version) {
2366 ext_prop.specVersion = atoi(spec_version);
2367 } else {
2368 ext_prop.specVersion = 0;
2369 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002370 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2371 GET_JSON_OBJECT(ext_item, entrypoints)
2372 int entry_count;
2373 if (entrypoints == NULL) {
2374 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2375 &ext_prop, 0, NULL);
2376 continue;
2377 }
2378 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002379 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002380 entry_array =
2381 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002382 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002383 for (j = 0; j < entry_count; j++) {
2384 ext_item = cJSON_GetArrayItem(entrypoints, j);
2385 if (ext_item != NULL) {
2386 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002387 if (NULL == temp) {
2388 entry_array[j] = NULL;
2389 continue;
2390 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002391 temp[strlen(temp) - 1] = '\0';
2392 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2393 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002394 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002395 }
2396 }
2397 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2398 &ext_prop, entry_count, entry_array);
2399 }
2400 }
2401 if (is_implicit) {
2402 GET_JSON_OBJECT(layer_node, enable_environment)
2403
2404 // enable_environment is optional
2405 if (enable_environment) {
2406 strncpy(props->enable_env_var.name,
2407 enable_environment->child->string,
2408 sizeof(props->enable_env_var.name));
2409 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2410 '\0';
2411 strncpy(props->enable_env_var.value,
2412 enable_environment->child->valuestring,
2413 sizeof(props->enable_env_var.value));
2414 props->enable_env_var
2415 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2416 }
2417 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002418#undef GET_JSON_ITEM
2419#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002420}
2421
2422/**
2423 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2424 * file, add entry to the layer_list. Fill out the layer_properties in this list
2425 * entry from the input cJSON object.
2426 *
2427 * \returns
2428 * void
2429 * layer_list has a new entry and initialized accordingly.
2430 * If the json input object does not have all the required fields no entry
2431 * is added to the list.
2432 */
2433static void
2434loader_add_layer_properties(const struct loader_instance *inst,
2435 struct loader_layer_list *layer_instance_list,
2436 cJSON *json, bool is_implicit, char *filename) {
2437 /* Fields in layer manifest file that are required:
2438 * (required) “file_format_version”
2439 *
Mark Young0ad83132016-06-30 13:02:42 -06002440 * If more than one "layer" object are to be used, use the "layers" array
2441 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002442 *
2443 * First get all required items and if any missing abort
2444 */
2445
2446 cJSON *item, *layers_node, *layer_node;
2447 uint16_t file_major_vers = 0;
2448 uint16_t file_minor_vers = 0;
2449 uint16_t file_patch_vers = 0;
2450 char *vers_tok;
2451 cJSON *disable_environment = NULL;
2452 item = cJSON_GetObjectItem(json, "file_format_version");
2453 if (item == NULL) {
2454 return;
2455 }
2456 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002457 if (NULL == file_vers) {
2458 return;
2459 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002460 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2461 "Found manifest file %s, version %s", filename, file_vers);
2462 // Get the major/minor/and patch as integers for easier comparison
2463 vers_tok = strtok(file_vers, ".\"\n\r");
2464 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002465 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002466 vers_tok = strtok(NULL, ".\"\n\r");
2467 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002468 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002469 vers_tok = strtok(NULL, ".\"\n\r");
2470 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002471 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002472 }
2473 }
2474 }
2475 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2476 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2477 "%s Unexpected manifest file version (expected 1.0.0 or "
2478 "1.0.1), may cause errors",
2479 filename);
2480 }
Mark Young0ad83132016-06-30 13:02:42 -06002481 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002482 // If "layers" is present, read in the array of layer objects
2483 layers_node = cJSON_GetObjectItem(json, "layers");
2484 if (layers_node != NULL) {
2485 int numItems = cJSON_GetArraySize(layers_node);
2486 if (file_major_vers == 1 && file_minor_vers == 0 &&
2487 file_patch_vers == 0) {
2488 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2489 "\"layers\" tag not officially added until file version "
2490 "1.0.1, but %s is reporting version %s",
2491 filename, file_vers);
2492 }
2493 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2494 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2495 if (layer_node == NULL) {
2496 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2497 "Can't find \"layers\" array element %d object in "
2498 "manifest JSON file %s, skipping this file",
2499 curLayer, filename);
2500 return;
2501 }
2502 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2503 disable_environment, is_implicit, filename);
2504 }
2505 } else {
2506 // Otherwise, try to read in individual layers
2507 layer_node = cJSON_GetObjectItem(json, "layer");
2508 if (layer_node == NULL) {
2509 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2510 "Can't find \"layer\" object in manifest JSON file %s, "
2511 "skipping this file",
2512 filename);
2513 return;
2514 }
2515 // Loop through all "layer" objects in the file to get a count of them
2516 // first.
2517 uint16_t layer_count = 0;
2518 cJSON *tempNode = layer_node;
2519 do {
2520 tempNode = tempNode->next;
2521 layer_count++;
2522 } while (tempNode != NULL);
2523 /*
2524 * Throw a warning if we encounter multiple "layer" objects in file
2525 * versions newer than 1.0.0. Having multiple objects with the same
2526 * name at the same level is actually a JSON standard violation.
2527 */
2528 if (layer_count > 1 &&
2529 (file_major_vers > 1 ||
2530 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2531 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2532 "Multiple \"layer\" nodes are deprecated starting in "
2533 "file version \"1.0.1\". Please use \"layers\" : [] "
2534 "array instead in %s.",
2535 filename);
2536 } else {
2537 do {
2538 loader_read_json_layer(inst, layer_instance_list, layer_node,
2539 item, disable_environment, is_implicit,
2540 filename);
2541 layer_node = layer_node->next;
2542 } while (layer_node != NULL);
2543 }
2544 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002545 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002546}
2547
2548/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002549 * Find the Vulkan library manifest files.
2550 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002551 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002552 * for a list of JSON manifest files. If env_override is non-NULL
2553 * and has a valid value. Then the location is ignored. Otherwise
2554 * location is used to look for manifest files. The location
2555 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002556 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002557 * directory to look at. It is expanded into the dir path
2558 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2559 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002560 *
2561 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002562 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002563 * A string list of manifest files to be opened in out_files param.
2564 * List has a pointer to string for each manifest filename.
2565 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002566 * Location or override string lists can be either files or directories as
2567 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002568 * | location | override
2569 * --------------------------------
2570 * Win ICD | files | files
2571 * Win Layer | files | dirs
2572 * Linux ICD | dirs | files
2573 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002574 */
Mark Young0ad83132016-06-30 13:02:42 -06002575static VkResult loader_get_manifest_files(
2576 const struct loader_instance *inst, const char *env_override,
2577 char *source_override, bool is_layer, const char *location,
2578 const char *home_location, struct loader_manifest_files *out_files) {
2579 char * override = NULL;
2580 char *loc, *orig_loc = NULL;
2581 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002582 char *file, *next_file, *name;
2583 size_t alloced_count = 64;
2584 char full_path[2048];
2585 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002586 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002587 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002588 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002589
2590 out_files->count = 0;
2591 out_files->filename_list = NULL;
2592
Jamie Madill00c3c912016-04-06 18:26:46 -04002593 if (source_override != NULL) {
2594 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002595 } else if (env_override != NULL &&
2596 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002597#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002598 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002599 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002600 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002601 override = NULL;
2602 }
2603#endif
2604 }
2605
Jon Ashburnb6822212016-02-16 15:34:16 -07002606#if !defined(_WIN32)
2607 if (location == NULL && home_location == NULL) {
2608#else
2609 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002610 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002611#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002612 loader_log(
2613 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002614 "Can't get manifest files with NULL location, env_override=%s",
2615 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002616 res = VK_ERROR_INITIALIZATION_FAILED;
2617 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002618 }
2619
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002620#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002621 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002622#else
2623 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002624#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002625 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002626 // Also handle getting the location(s) from registry on Windows
2627 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002628 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002629 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002630 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2631 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002632 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2633 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002634 }
2635 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002636#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002637 reg = loader_get_registry_files(inst, loc);
2638 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002639 if (!is_layer) {
2640 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2641 "Registry lookup failed can't get ICD manifest "
2642 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002643 // This typically only fails when out of memory, which is
2644 // critical
2645 // if this is for the loader.
2646 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002647 } else {
2648 // warning only for layers
2649 loader_log(
2650 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2651 "Registry lookup failed can't get layer manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002652 // Return success for now since it's not critical for layers
2653 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002654 }
Mark Young0ad83132016-06-30 13:02:42 -06002655 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002656 }
Mark Young0ad83132016-06-30 13:02:42 -06002657 orig_loc = loc;
2658 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002659#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002660 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002661 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002662 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002663 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2664 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002665 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2666 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002667 }
2668 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002669 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002670 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002671 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002672 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002673
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002674 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002675 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2676 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002677
Jon Ashburn2077e382015-06-29 11:25:34 -06002678 file = loc;
2679 while (*file) {
2680 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002681 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002682 sysdir = opendir(file);
2683 name = NULL;
2684 if (sysdir) {
2685 dent = readdir(sysdir);
2686 if (dent == NULL)
2687 break;
2688 name = &(dent->d_name[0]);
2689 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2690 name = full_path;
2691 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002692 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002693#if defined(_WIN32)
2694 name = file;
2695#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002696 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002697 char *dir;
2698 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002699 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002700 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002701 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2702 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002703 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002704 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002705 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002706
2707 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2708
2709 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002710#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002711 }
2712 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002713 /* Look for files ending with ".json" suffix */
2714 uint32_t nlen = (uint32_t)strlen(name);
2715 const char *suf = name + nlen - 5;
2716 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2717 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002718 out_files->filename_list = loader_instance_heap_alloc(
2719 inst, alloced_count * sizeof(char *),
2720 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002721 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002722 out_files->filename_list = loader_instance_heap_realloc(
2723 inst, out_files->filename_list,
2724 alloced_count * sizeof(char *),
2725 alloced_count * sizeof(char *) * 2,
2726 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002727 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002728 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002729 if (out_files->filename_list == NULL) {
2730 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2731 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002732 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2733 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002734 }
Mark Young0ad83132016-06-30 13:02:42 -06002735 out_files->filename_list[out_files->count] =
2736 loader_instance_heap_alloc(
2737 inst, strlen(name) + 1,
2738 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002739 if (out_files->filename_list[out_files->count] == NULL) {
2740 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2741 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002742 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2743 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002744 }
2745 strcpy(out_files->filename_list[out_files->count], name);
2746 out_files->count++;
2747 } else if (!list_is_dirs) {
2748 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002749 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002750 "Skipping manifest file %s, file name must end in .json",
2751 name);
2752 }
2753 if (list_is_dirs) {
2754 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002755 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002756 break;
Mark Young0ad83132016-06-30 13:02:42 -06002757 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002758 name = &(dent->d_name[0]);
2759 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2760 name = full_path;
2761 } else {
2762 break;
2763 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002764 }
Mark Young0ad83132016-06-30 13:02:42 -06002765 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002766 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002767 sysdir = NULL;
2768 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002769 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002770#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002771 if (home_location != NULL &&
2772 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002773 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2774 size_t len;
2775 if (xdgdatahome != NULL) {
2776
2777 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002778 strlen(home_location));
2779 if (home_loc == NULL) {
2780 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002781 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002782 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2783 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002784 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002785 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002786 // Add directory separator if needed
2787 if (home_location[0] != DIRECTORY_SYMBOL) {
2788 len = strlen(home_loc);
2789 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002790 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002791 }
2792 strcat(home_loc, home_location);
2793 file = home_loc;
2794 next_file = loader_get_next_path(file);
2795 home_location = NULL;
2796
Jon Ashburn1530c342016-02-26 13:14:27 -07002797 loader_log(
2798 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002799 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002800 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002801 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002802
2803 } else {
2804
2805 char *home = secure_getenv("HOME");
2806 if (home != NULL) {
2807 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2808 strlen(home_location));
2809 if (home_loc == NULL) {
2810 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2811 "Out of memory can't get manifest files");
2812 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2813 goto out;
2814 }
2815 strcpy(home_loc, home);
2816
2817 len = strlen(home);
2818 if (home[len] != DIRECTORY_SYMBOL) {
2819 home_loc[len] = DIRECTORY_SYMBOL;
2820 home_loc[len + 1] = '\0';
2821 }
2822 strcat(home_loc, ".local/share");
2823
2824 if (home_location[0] != DIRECTORY_SYMBOL) {
2825 len = strlen(home_loc);
2826 home_loc[len] = DIRECTORY_SYMBOL;
2827 home_loc[len + 1] = '\0';
2828 }
2829 strcat(home_loc, home_location);
2830 file = home_loc;
2831 next_file = loader_get_next_path(file);
2832 home_location = NULL;
2833
2834 loader_log(
2835 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2836 "Searching the following path for manifest files: %s\n",
2837 home_loc);
2838 list_is_dirs = true;
2839 } else {
2840 // without knowing HOME, we just.. give up
2841 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002842 }
2843 }
2844#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002845 }
Mark Young0ad83132016-06-30 13:02:42 -06002846
2847out:
2848 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2849 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2850 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2851 }
2852 loader_instance_heap_free(inst, out_files->filename_list);
2853 out_files->count = 0;
2854 out_files->filename_list = NULL;
2855 }
2856
2857 if (NULL != sysdir) {
2858 closedir(sysdir);
2859 }
2860
2861 if (NULL != reg && reg != orig_loc) {
2862 loader_instance_heap_free(inst, reg);
2863 }
2864 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002865}
2866
Jon Ashburn23d36b12016-02-02 17:47:28 -07002867void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002868
Jon Ashburn23d36b12016-02-02 17:47:28 -07002869void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002870/**
2871 * Try to find the Vulkan ICD driver(s).
2872 *
2873 * This function scans the default system loader path(s) or path
2874 * specified by the \c VK_ICD_FILENAMES environment variable in
2875 * order to find loadable VK ICDs manifest files. From these
2876 * manifest files it finds the ICD libraries.
2877 *
2878 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002879 * Vulkan result
2880 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002881 */
Mark Young0ad83132016-06-30 13:02:42 -06002882VkResult loader_icd_scan(const struct loader_instance *inst,
2883 struct loader_icd_libs *icds) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002884 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002885 uint16_t file_major_vers = 0;
2886 uint16_t file_minor_vers = 0;
2887 uint16_t file_patch_vers = 0;
2888 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002889 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002890 VkResult res = VK_SUCCESS;
2891 bool lockedMutex = false;
2892 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002893 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002894
Mark Young0ad83132016-06-30 13:02:42 -06002895 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2896
2897 res = loader_scanned_icd_init(inst, icds);
2898 if (VK_SUCCESS != res) {
2899 goto out;
2900 }
2901
Jon Ashburn2077e382015-06-29 11:25:34 -06002902 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002903 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
2904 DEFAULT_VK_DRIVERS_INFO,
2905 HOME_VK_DRIVERS_INFO, &manifest_files);
2906 if (VK_SUCCESS != res || manifest_files.count == 0) {
2907 goto out;
2908 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002909 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002910 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002911 for (uint32_t i = 0; i < manifest_files.count; i++) {
2912 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002913 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002914 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002915 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002916
Mark Young3a587792016-08-19 15:25:08 -06002917 res = loader_get_json(inst, file_str, &json);
2918 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2919 break;
2920 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002921 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002922 }
Mark Young3a587792016-08-19 15:25:08 -06002923
Jon Ashburn005617f2015-11-17 17:35:40 -07002924 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002925 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002926 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002927 if (num_good_icds == 0) {
2928 res = VK_ERROR_INITIALIZATION_FAILED;
2929 }
Derrick Owens62e16ef2016-09-09 15:49:07 -04002930 cJSON_Delete(json);
2931 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002932 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002933 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002934 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002935 if (NULL == file_vers) {
2936 // Only reason the print can fail is if there was an allocation
2937 // issue
Mark Young3a587792016-08-19 15:25:08 -06002938 if (num_good_icds == 0) {
2939 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2940 }
Derrick Owenscd92b8b2016-09-09 15:45:13 -04002941 cJSON_Delete(json);
2942 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002943 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002944 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002945 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2946 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002947 // Get the major/minor/and patch as integers for easier comparison
2948 vers_tok = strtok(file_vers, ".\"\n\r");
2949 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002950 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002951 vers_tok = strtok(NULL, ".\"\n\r");
2952 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002953 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002954 vers_tok = strtok(NULL, ".\"\n\r");
2955 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002956 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002957 }
2958 }
2959 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002960 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002961 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002962 "Unexpected manifest file version (expected 1.0.0 or "
2963 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002964 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002965 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002966 itemICD = cJSON_GetObjectItem(json, "ICD");
2967 if (itemICD != NULL) {
2968 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002969 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002970 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002971 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002972 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002973 "Can't find \"library_path\" in ICD JSON file "
2974 "%s, skipping",
2975 file_str);
Mark Young3a587792016-08-19 15:25:08 -06002976 if (num_good_icds == 0) {
2977 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2978 }
Mark Young0ad83132016-06-30 13:02:42 -06002979 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002980 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002981 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002982 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06002983 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002984 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06002985 temp[strlen(temp) - 1] = '\0';
2986 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06002987 if (NULL == library_path) {
2988 loader_log(
2989 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2990 "Can't allocate space for \"library_path\" in ICD "
2991 "JSON file %s, skipping",
2992 file_str);
2993 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2994 cJSON_Free(temp);
2995 cJSON_Delete(json);
2996 json = NULL;
2997 goto out;
2998 }
Jon Ashburn86251302015-08-25 16:48:24 -06002999 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003000 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003001 if (strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003002 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003003 "Can't find \"library_path\" in ICD JSON file "
3004 "%s, skipping",
3005 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003006 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003007 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003008 continue;
3009 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003010 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003011 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07003012 loader_log(
3013 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04003014 "Searching for ICD drivers named %s default dir %s\n",
3015 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003016 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003017 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003018 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3019 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003020 strcpy(name_copy, file_str);
3021 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003022 loader_expand_path(library_path, rel_base, sizeof(fullpath),
3023 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003024 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003025 // a filename which is assumed in a system directory
3026 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
3027 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003028 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003029
3030 uint32_t vers = 0;
3031 item = cJSON_GetObjectItem(itemICD, "api_version");
3032 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003033 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003034 if (NULL == temp) {
3035 // Only reason the print can fail is if there was an
3036 // allocation issue
3037 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3038 goto out;
3039 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003040 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003041 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07003042 }
Mark Young3a587792016-08-19 15:25:08 -06003043 res = loader_scanned_icd_add(inst, icds, fullpath, vers);
3044 if (VK_SUCCESS != res) {
3045 goto out;
3046 }
3047 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003048 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003049 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003050 "Can't find \"library_path\" object in ICD JSON "
3051 "file %s, skipping",
3052 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003053 }
3054 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003055 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003056 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003057 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
3058 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003059 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003060
Mark Young0ad83132016-06-30 13:02:42 -06003061 cJSON_Delete(json);
3062 json = NULL;
3063 }
3064
3065out:
3066 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003067 cJSON_Delete(json);
3068 }
Mark Young0ad83132016-06-30 13:02:42 -06003069 if (NULL != manifest_files.filename_list) {
3070 for (uint32_t i = 0; i < manifest_files.count; i++) {
3071 if (NULL != manifest_files.filename_list[i]) {
3072 loader_instance_heap_free(inst,
3073 manifest_files.filename_list[i]);
3074 }
3075 }
3076 loader_instance_heap_free(inst, manifest_files.filename_list);
3077 }
3078 if (lockedMutex) {
3079 loader_platform_thread_unlock_mutex(&loader_json_lock);
3080 }
3081 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003082}
3083
Jon Ashburn23d36b12016-02-02 17:47:28 -07003084void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003085 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003086 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003087 struct loader_manifest_files
3088 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003089 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003090 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003091 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003092
Mark Young0ad83132016-06-30 13:02:42 -06003093 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3094
3095 // Get a list of manifest files for explicit layers
3096 if (VK_SUCCESS !=
3097 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
3098 true, DEFAULT_VK_ELAYERS_INFO,
3099 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3100 goto out;
3101 }
3102
3103 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003104 // Pass NULL for environment variable override - implicit layers are not
3105 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003106 if (VK_SUCCESS != loader_get_manifest_files(
3107 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
3108 HOME_VK_ILAYERS_INFO, &manifest_files[1])) {
3109 goto out;
3110 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003111
Mark Young0ad83132016-06-30 13:02:42 -06003112 // Make sure we have at least one layer, if not, go ahead and return
3113 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3114 goto out;
3115 }
3116
3117 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003118 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003119
Jon Ashburn6461ef22015-09-22 13:11:00 -06003120 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003121 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003122 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003123 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003124 file_str = manifest_files[implicit].filename_list[i];
3125 if (file_str == NULL)
3126 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003127
Jon Ashburn075ce432015-12-17 17:38:24 -07003128 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003129 VkResult res = loader_get_json(inst, file_str, &json);
3130 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3131 break;
3132 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003133 continue;
3134 }
3135
Jon Ashburn491cd042016-05-16 14:01:18 -06003136 loader_add_layer_properties(inst, instance_layers, json,
3137 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003138 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003139 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003140 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003141
3142 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003143 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3144 sizeof(std_validation_names[0]),
3145 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003146
Mark Young0ad83132016-06-30 13:02:42 -06003147out:
3148
3149 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3150 if (NULL != manifest_files[manFile].filename_list) {
3151 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3152 if (NULL != manifest_files[manFile].filename_list[i]) {
3153 loader_instance_heap_free(
3154 inst, manifest_files[manFile].filename_list[i]);
3155 }
3156 }
3157 loader_instance_heap_free(inst,
3158 manifest_files[manFile].filename_list);
3159 }
3160 }
3161 if (lockedMutex) {
3162 loader_platform_thread_unlock_mutex(&loader_json_lock);
3163 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003164}
3165
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003166void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003167 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003168 char *file_str;
3169 struct loader_manifest_files manifest_files;
3170 cJSON *json;
3171 uint32_t i;
3172
3173 // Pass NULL for environment variable override - implicit layers are not
3174 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003175 VkResult res = loader_get_manifest_files(
3176 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
3177 &manifest_files);
3178 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003179 return;
3180 }
3181
3182 /* cleanup any previously scanned libraries */
3183 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003184
3185 loader_platform_thread_lock_mutex(&loader_json_lock);
3186
3187 for (i = 0; i < manifest_files.count; i++) {
3188 file_str = manifest_files.filename_list[i];
3189 if (file_str == NULL) {
3190 continue;
3191 }
3192
3193 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003194 res = loader_get_json(inst, file_str, &json);
3195 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3196 break;
3197 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003198 continue;
3199 }
3200
Mark Young0ad83132016-06-30 13:02:42 -06003201 loader_add_layer_properties(inst, instance_layers, json, true,
3202 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003203
Mark Young0ad83132016-06-30 13:02:42 -06003204 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003205 cJSON_Delete(json);
3206 }
Mark Young0ad83132016-06-30 13:02:42 -06003207 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003208
3209 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003210 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3211 sizeof(std_validation_names[0]),
3212 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003213
3214 loader_platform_thread_unlock_mutex(&loader_json_lock);
3215}
3216
Jon Ashburn23d36b12016-02-02 17:47:28 -07003217static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3218loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003219 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003220 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003221 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003222 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003223 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003224 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003225
Jon Ashburn27cd5842015-05-12 17:26:48 -06003226 // inst is not wrapped
3227 if (inst == VK_NULL_HANDLE) {
3228 return NULL;
3229 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003230 VkLayerInstanceDispatchTable *disp_table =
3231 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003232 void *addr;
3233
3234 if (disp_table == NULL)
3235 return NULL;
3236
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003237 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003238 addr =
3239 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003240 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003241 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003242 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003243
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003244 // Don't call down the chain, this would be an infinite loop
3245 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003246 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003247 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003248}
3249
Mark Younga7c51fd2016-09-16 10:18:42 -06003250void loader_override_terminating_device_proc(
3251 VkDevice device, struct loader_dev_dispatch_table *disp_table) {
3252 struct loader_device *dev;
3253 struct loader_icd *icd = loader_get_icd_and_device(device, &dev, NULL);
3254
3255 // Certain device entry-points still need to go through a terminator before
3256 // hitting the ICD. This could be for several reasons, but the main one
3257 // is currently unwrapping an object before passing the appropriate info
3258 // along to the ICD.
3259 if ((PFN_vkVoidFunction)disp_table->core_dispatch.CreateSwapchainKHR ==
3260 (PFN_vkVoidFunction)icd->GetDeviceProcAddr(device,
3261 "vkCreateSwapchainKHR")) {
3262 disp_table->core_dispatch.CreateSwapchainKHR =
3263 terminator_vkCreateSwapchainKHR;
3264 }
3265}
Mark Young16573c72016-06-28 10:52:43 -06003266
Piers Daniellf9262be2016-09-14 11:24:36 -06003267VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003268loader_gpa_device_internal(VkDevice device, const char *pName) {
3269 struct loader_device *dev;
Mark Young16573c72016-06-28 10:52:43 -06003270 struct loader_icd *icd = loader_get_icd_and_device(device, &dev, NULL);
3271
3272 // Certain device entry-points still need to go through a terminator before
3273 // hitting the ICD. This could be for several reasons, but the main one
3274 // is currently unwrapping an object before passing the appropriate info
3275 // along to the ICD.
3276 if (!strcmp(pName, "vkCreateSwapchainKHR")) {
3277 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
3278 }
3279
Piers Daniell295fe402016-03-29 11:51:11 -06003280 return icd->GetDeviceProcAddr(device, pName);
3281}
3282
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003283/**
3284 * Initialize device_ext dispatch table entry as follows:
3285 * If dev == NULL find all logical devices created within this instance and
3286 * init the entry (given by idx) in the ext dispatch table.
3287 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003288 * The initialization value is gotten by calling down the device chain with
3289 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003290 * If GDPA returns NULL then don't initialize the dispatch table entry.
3291 */
3292static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003293 struct loader_device *dev,
3294 uint32_t idx,
3295 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003296
Jon Ashburn23d36b12016-02-02 17:47:28 -07003297{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003298 void *gdpa_value;
3299 if (dev != NULL) {
3300 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003301 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003302 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003303 dev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003304 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003305 } else {
3306 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
3307 struct loader_icd *icd = &inst->icds[i];
Karl Schultz2558bd32016-02-24 14:39:39 -07003308 struct loader_device *ldev = icd->logical_device_list;
3309 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003310 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003311 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3312 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003313 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003314 ldev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003315 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003316 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003317 }
3318 }
3319 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003320}
3321
3322/**
3323 * Find all dev extension in the hash table and initialize the dispatch table
3324 * for dev for each of those extension entrypoints found in hash table.
3325
3326 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003327void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3328 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003329 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3330 if (inst->disp_hash[i].func_name != NULL)
3331 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3332 inst->disp_hash[i].func_name);
3333 }
3334}
3335
3336static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003337 const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003338 struct loader_icd *icd;
3339 icd = inst->icds;
3340 while (icd) {
3341 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
3342 // this icd supports funcName
3343 return true;
3344 icd = icd->next;
3345 }
3346
3347 return false;
3348}
3349
Jon Ashburncc407a22016-04-15 09:25:03 -06003350static bool loader_check_layer_list_for_address(
3351 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003352 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003353 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003354 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003355 const struct loader_device_extension_list *const extensions =
3356 &(layers->list[layer].device_extension_list);
3357 for (uint32_t extension = 0; extension < extensions->count;
3358 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003359 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003360 const struct loader_dev_ext_props *const property =
3361 &(extensions->list[extension]);
3362 for (uint32_t entry = 0; entry < property->entrypoint_count;
3363 ++entry) {
3364 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003365 return true;
3366 }
3367 }
3368 }
3369 }
3370
3371 return false;
3372}
3373
Jon Ashburncc407a22016-04-15 09:25:03 -06003374static bool
3375loader_check_layers_for_address(const struct loader_instance *const inst,
3376 const char *funcName) {
3377 if (loader_check_layer_list_for_address(&inst->instance_layer_list,
3378 funcName)) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003379 return true;
3380 }
3381
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003382 return false;
3383}
3384
Jon Ashburn23d36b12016-02-02 17:47:28 -07003385static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003386 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003387 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3388 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003389 }
3390 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3391}
3392
3393static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003394 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003395 uint32_t i;
3396 uint32_t idx = *ptr_idx;
3397 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3398
3399 if (!inst->disp_hash[idx].func_name) {
3400 // no entry here at this idx, so use it
3401 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003402 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003403 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003404 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003405 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003406 "loader_add_dev_ext_table() can't allocate memory for "
3407 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003408 return false;
3409 }
3410 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3411 return true;
3412 }
3413
3414 // check for enough capacity
3415 if (list->capacity == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06003416 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003417 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3418 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003419 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003420 "loader_add_dev_ext_table() can't allocate list memory");
3421 return false;
3422 }
3423 list->capacity = 8 * sizeof(*(list->index));
3424 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0ad83132016-06-30 13:02:42 -06003425 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003426 list->capacity * 2,
3427 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003428 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003429 loader_log(
3430 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3431 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003432 return false;
3433 }
3434 list->capacity *= 2;
3435 }
3436
Jon Ashburn23d36b12016-02-02 17:47:28 -07003437 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003438 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3439 do {
3440 if (!inst->disp_hash[i].func_name) {
3441 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003442 inst->disp_hash[i].func_name =
Mark Young0ad83132016-06-30 13:02:42 -06003443 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003444 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003445 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003446 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003447 "loader_add_dev_ext_table() can't rallocate "
3448 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003449 return false;
3450 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003451 strncpy(inst->disp_hash[i].func_name, funcName,
3452 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003453 list->index[list->count] = i;
3454 list->count++;
3455 *ptr_idx = i;
3456 return true;
3457 }
3458 i = (i + 1) % MAX_NUM_DEV_EXTS;
3459 } while (i != idx);
3460
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003461 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003462 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3463 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003464 return false;
3465}
3466
3467static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003468 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003469 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003470 if (inst->disp_hash[*idx].func_name &&
3471 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003472 return true;
3473
3474 // funcName wasn't at the primary spot in the hash table
3475 // search the list of secondary locations (shallow search, not deep search)
3476 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3477 alt_idx = inst->disp_hash[*idx].list.index[i];
3478 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3479 *idx = alt_idx;
3480 return true;
3481 }
3482 }
3483
3484 return false;
3485}
3486
3487/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003488 * This function returns generic trampoline code address for unknown entry
3489 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003490 * Presumably, these unknown entry points (as given by funcName) are device
3491 * extension entrypoints. A hash table is used to keep a list of unknown entry
3492 * points and their mapping to the device extension dispatch table
3493 * (struct loader_dev_ext_dispatch_table).
3494 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003495 * For a given entry point string (funcName), if an existing mapping is found
3496 * the
3497 * trampoline address for that mapping is returned. Otherwise, this unknown
3498 * entry point
3499 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3500 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003501 * new entry in the hash table is initialized and that trampoline address for
3502 * the new entry is returned. Null is returned if the hash table is full or
3503 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3504 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003505void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003506 uint32_t idx;
3507 uint32_t seed = 0;
3508
3509 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3510
3511 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3512 // found funcName already in hash
3513 return loader_get_dev_ext_trampoline(idx);
3514
3515 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003516 if (!loader_check_icds_for_address(inst, funcName) &&
3517 !loader_check_layers_for_address(inst, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003518 // if support found in layers continue on
3519 return NULL;
3520 }
3521
3522 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3523 // successfully added new table entry
3524 // init any dev dispatch table entrys as needed
3525 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3526 return loader_get_dev_ext_trampoline(idx);
3527 }
3528
3529 return NULL;
3530}
3531
Jon Ashburn23d36b12016-02-02 17:47:28 -07003532struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003533 /* look up the loader_instance in our list by comparing dispatch tables, as
3534 * there is no guarantee the instance is still a loader_instance* after any
3535 * layers which wrap the instance object.
3536 */
3537 const VkLayerInstanceDispatchTable *disp;
3538 struct loader_instance *ptr_instance = NULL;
3539 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003540 for (struct loader_instance *inst = loader.instances; inst;
3541 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003542 if (inst->disp == disp) {
3543 ptr_instance = inst;
3544 break;
3545 }
3546 }
3547 return ptr_instance;
3548}
3549
Jon Ashburn23d36b12016-02-02 17:47:28 -07003550static loader_platform_dl_handle
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003551loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3552 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003553
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003554 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003555 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003556 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003557 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003558 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003559 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003560 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003561 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003562 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003563
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003564 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003565}
3566
Jon Ashburn23d36b12016-02-02 17:47:28 -07003567static void
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003568loader_close_layer_lib(const struct loader_instance *inst,
3569 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003570
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003571 if (prop->lib_handle) {
3572 loader_platform_close_library(prop->lib_handle);
3573 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3574 "Unloading layer library %s", prop->lib_name);
3575 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003576 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003577}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003578
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003579void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003580 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003581 struct loader_layer_list *list) {
3582 /* delete instance list of enabled layers and close any layer libraries */
3583 for (uint32_t i = 0; i < list->count; i++) {
3584 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003585
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003586 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003587 }
Mark Young0ad83132016-06-30 13:02:42 -06003588 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003589}
3590
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003591/**
3592 * Go through the search_list and find any layers which match type. If layer
3593 * type match is found in then add it to ext_list.
3594 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003595static void
3596loader_add_layer_implicit(const struct loader_instance *inst,
3597 const enum layer_type type,
3598 struct loader_layer_list *list,
3599 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003600 bool enable;
3601 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003602 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003603 for (i = 0; i < search_list->count; i++) {
3604 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003605 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003606 /* Found an implicit layer, see if it should be enabled */
3607 enable = false;
3608
Jon Ashburn23d36b12016-02-02 17:47:28 -07003609 // if no enable_environment variable is specified, this implicit
3610 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003611 // should always be enabled. Otherwise check if the variable is set
3612 if (prop->enable_env_var.name[0] == 0) {
3613 enable = true;
3614 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003615 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003616 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3617 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003618 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003619 }
3620
3621 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003622 // environment variables are set, the layer is disabled. Implicit
3623 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003624 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003625 env_value = loader_getenv(prop->disable_env_var.name, inst);
3626 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003627 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003628 }
3629 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003630
Mark Young0ad83132016-06-30 13:02:42 -06003631 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003632 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003633 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003634 }
3635 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003636}
3637
3638/**
3639 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003640 * is found in search_list then add it to layer_list. But only add it to
3641 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003642 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003643static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003644 const enum layer_type type,
3645 const char *env_name,
3646 struct loader_layer_list *layer_list,
3647 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003648 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003649 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003650
Mark Young0ad83132016-06-30 13:02:42 -06003651 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003652 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003653 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003654 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003655 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003656 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003657 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003658 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003659 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003660
Mark Young0ad83132016-06-30 13:02:42 -06003661 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003662
Jon Ashburn23d36b12016-02-02 17:47:28 -07003663 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003664 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003665 if (!strcmp(std_validation_str, name)) {
3666 /* add meta list of layers
3667 don't attempt to remove duplicate layers already added by app or
3668 env var
3669 */
3670 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3671 "Expanding meta layer %s found in environment variable",
3672 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003673 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3674 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003675 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3676 sizeof(std_validation_names[0]);
3677 i++) {
3678 loader_find_layer_name_add_list(inst, std_validation_names[i],
3679 type, search_list, layer_list);
3680 }
3681 } else {
3682 loader_find_layer_name_add_list(inst, name, type, search_list,
3683 layer_list);
3684 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003685 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003686 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003687
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003688 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003689}
3690
Jon Ashburn23d36b12016-02-02 17:47:28 -07003691VkResult
3692loader_enable_instance_layers(struct loader_instance *inst,
3693 const VkInstanceCreateInfo *pCreateInfo,
3694 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003695 VkResult err;
3696
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003697 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003698
Jon Ashburne39a4f82015-08-28 13:38:21 -06003699 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003700 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3701 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003702 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003703 }
3704
Jon Ashburn0c26e712015-07-02 16:10:32 -06003705 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003706 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3707 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003708
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003709 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003710 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3711 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003712 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003713
3714 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003715 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003716 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3717 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003718
3719 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003720}
3721
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003722/*
3723 * Given the list of layers to activate in the loader_instance
3724 * structure. This function will add a VkLayerInstanceCreateInfo
3725 * structure to the VkInstanceCreateInfo.pNext pointer.
3726 * Each activated layer will have it's own VkLayerInstanceLink
3727 * structure that tells the layer what Get*ProcAddr to call to
3728 * get function pointers to the next layer down.
3729 * Once the chain info has been created this function will
3730 * execute the CreateInstance call chain. Each layer will
3731 * then have an opportunity in it's CreateInstance function
3732 * to setup it's dispatch table when the lower layer returns
3733 * successfully.
3734 * Each layer can wrap or not-wrap the returned VkInstance object
3735 * as it sees fit.
3736 * The instance chain is terminated by a loader function
3737 * that will call CreateInstance on all available ICD's and
3738 * cache those VkInstance objects for future use.
3739 */
3740VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003741 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003742 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003743 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003744 uint32_t activated_layers = 0;
3745 VkLayerInstanceCreateInfo chain_info;
3746 VkLayerInstanceLink *layer_instance_link_info = NULL;
3747 VkInstanceCreateInfo loader_create_info;
3748 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003749
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003750 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3751 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003752
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003753 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003754
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003755 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003756
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003757 chain_info.u.pLayerInfo = NULL;
3758 chain_info.pNext = pCreateInfo->pNext;
3759 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3760 chain_info.function = VK_LAYER_LINK_INFO;
3761 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003762
Jon Ashburn23d36b12016-02-02 17:47:28 -07003763 layer_instance_link_info = loader_stack_alloc(
3764 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003765 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003766 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3767 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003768 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003769 }
3770
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003771 /* Create instance chain of enabled layers */
3772 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003773 struct loader_layer_properties *layer_prop =
3774 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003775 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003776
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003777 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003778 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003779 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003780 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3781 NULL) {
3782 if (layer_prop->functions.str_gipa == NULL ||
3783 strlen(layer_prop->functions.str_gipa) == 0) {
3784 fpGIPA = (PFN_vkGetInstanceProcAddr)
3785 loader_platform_get_proc_address(
3786 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003787 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3788 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003789 fpGIPA = (PFN_vkGetInstanceProcAddr)
3790 loader_platform_get_proc_address(
3791 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003792 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003793 loader_log(
3794 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3795 "Failed to find vkGetInstanceProcAddr in layer %s",
3796 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003797 continue;
3798 }
3799 }
3800
Jon Ashburn23d36b12016-02-02 17:47:28 -07003801 layer_instance_link_info[activated_layers].pNext =
3802 chain_info.u.pLayerInfo;
3803 layer_instance_link_info[activated_layers]
3804 .pfnNextGetInstanceProcAddr = nextGIPA;
3805 chain_info.u.pLayerInfo =
3806 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003807 nextGIPA = fpGIPA;
3808
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003809 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003810 "Insert instance layer %s (%s)",
3811 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003812
3813 activated_layers++;
3814 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003815 }
3816
Jon Ashburn23d36b12016-02-02 17:47:28 -07003817 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003818 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003819 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003820 VkLayerInstanceCreateInfo create_info_disp;
3821
Jon Ashburncc407a22016-04-15 09:25:03 -06003822 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003823 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003824
3825 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3826
3827 create_info_disp.pNext = loader_create_info.pNext;
3828 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003829 res =
3830 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003831 } else {
3832 // Couldn't find CreateInstance function!
3833 res = VK_ERROR_INITIALIZATION_FAILED;
3834 }
3835
3836 if (res != VK_SUCCESS) {
3837 // TODO: Need to clean up here
3838 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003839 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003840 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003841 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003842 }
3843
3844 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003845}
3846
Jon Ashburn23d36b12016-02-02 17:47:28 -07003847void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3848 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003849
Jon Ashburn23d36b12016-02-02 17:47:28 -07003850 loader_init_instance_extension_dispatch_table(
3851 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003852}
3853
Jon Ashburn1530c342016-02-26 13:14:27 -07003854VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003855loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3856 const VkDeviceCreateInfo *pCreateInfo,
3857 const VkAllocationCallbacks *pAllocator,
3858 const struct loader_instance *inst,
3859 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003860 uint32_t activated_layers = 0;
3861 VkLayerDeviceLink *layer_device_link_info;
3862 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003863 VkDeviceCreateInfo loader_create_info;
3864 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003865
Piers Daniell295fe402016-03-29 11:51:11 -06003866 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003867 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003868
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003869 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3870
Jon Ashburn23d36b12016-02-02 17:47:28 -07003871 layer_device_link_info = loader_stack_alloc(
3872 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003873 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003874 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3875 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003876 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003877 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003878
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003879 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003880 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3881 chain_info.function = VK_LAYER_LINK_INFO;
3882 chain_info.u.pLayerInfo = NULL;
3883 chain_info.pNext = pCreateInfo->pNext;
3884 loader_create_info.pNext = &chain_info;
3885
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003886 /* Create instance chain of enabled layers */
3887 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003888 struct loader_layer_properties *layer_prop =
3889 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003890 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003891
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003892 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003893 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003894 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003895 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3896 NULL) {
3897 if (layer_prop->functions.str_gipa == NULL ||
3898 strlen(layer_prop->functions.str_gipa) == 0) {
3899 fpGIPA = (PFN_vkGetInstanceProcAddr)
3900 loader_platform_get_proc_address(
3901 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003902 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3903 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003904 fpGIPA = (PFN_vkGetInstanceProcAddr)
3905 loader_platform_get_proc_address(
3906 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003907 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003908 loader_log(
3909 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3910 "Failed to find vkGetInstanceProcAddr in layer %s",
3911 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003912 continue;
3913 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003914 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003915 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003916 if (layer_prop->functions.str_gdpa == NULL ||
3917 strlen(layer_prop->functions.str_gdpa) == 0) {
3918 fpGDPA = (PFN_vkGetDeviceProcAddr)
3919 loader_platform_get_proc_address(lib_handle,
3920 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003921 layer_prop->functions.get_device_proc_addr = fpGDPA;
3922 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003923 fpGDPA = (PFN_vkGetDeviceProcAddr)
3924 loader_platform_get_proc_address(
3925 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003926 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003927 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003928 "Failed to find vkGetDeviceProcAddr in layer %s",
3929 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003930 continue;
3931 }
3932 }
3933
Jon Ashburn23d36b12016-02-02 17:47:28 -07003934 layer_device_link_info[activated_layers].pNext =
3935 chain_info.u.pLayerInfo;
3936 layer_device_link_info[activated_layers]
3937 .pfnNextGetInstanceProcAddr = nextGIPA;
3938 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3939 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003940 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3941 nextGIPA = fpGIPA;
3942 nextGDPA = fpGDPA;
3943
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003944 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003945 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003946 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003947
3948 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003949 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003950 }
3951
Jon Ashburncc407a22016-04-15 09:25:03 -06003952 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003953 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003954 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003955 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003956 VkLayerDeviceCreateInfo create_info_disp;
3957
Jon Ashburncc407a22016-04-15 09:25:03 -06003958 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003959 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3960
3961 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3962
3963 create_info_disp.pNext = loader_create_info.pNext;
3964 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003965 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003966 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003967 if (res != VK_SUCCESS) {
3968 return res;
3969 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003970 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003971 } else {
3972 // Couldn't find CreateDevice function!
3973 return VK_ERROR_INITIALIZATION_FAILED;
3974 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003975
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003976 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003977 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003978 dev->device);
3979
3980 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003981}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003982
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003983VkResult loader_validate_layers(const struct loader_instance *inst,
3984 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003985 const char *const *ppEnabledLayerNames,
3986 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003987 struct loader_layer_properties *prop;
3988
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003989 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003990 VkStringErrorFlags result =
3991 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003992 if (result != VK_STRING_ERROR_NONE) {
3993 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003994 "Loader: Device ppEnabledLayerNames contains string "
3995 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003996 return VK_ERROR_LAYER_NOT_PRESENT;
3997 }
3998
Jon Ashburn23d36b12016-02-02 17:47:28 -07003999 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004000 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004001 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004002 }
4003 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004004 return VK_SUCCESS;
4005}
4006
4007VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004008 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004009 const struct loader_extension_list *icd_exts,
4010 const struct loader_layer_list *instance_layer,
4011 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004012
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004013 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004014 struct loader_layer_properties *layer_prop;
4015
Jon Ashburnf19916e2016-01-11 13:12:43 -07004016 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004017 VkStringErrorFlags result = vk_string_validate(
4018 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004019 if (result != VK_STRING_ERROR_NONE) {
4020 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004021 "Loader: Instance ppEnabledExtensionNames contains "
4022 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004023 return VK_ERROR_EXTENSION_NOT_PRESENT;
4024 }
4025
Jon Ashburn23d36b12016-02-02 17:47:28 -07004026 extension_prop = get_extension_property(
4027 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004028
4029 if (extension_prop) {
4030 continue;
4031 }
4032
4033 extension_prop = NULL;
4034
4035 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004036 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004037 layer_prop = loader_get_layer_property(
Jan-Harald Fredriksene812a642016-09-28 12:10:24 +02004038 pCreateInfo->ppEnabledLayerNames[j], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004039 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004040 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004041 * should have already filtered this case out.
4042 */
4043 continue;
4044 }
4045
Jon Ashburn23d36b12016-02-02 17:47:28 -07004046 extension_prop =
4047 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
4048 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004049 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004050 /* Found the extension in one of the layers enabled by the app.
4051 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004052 break;
4053 }
4054 }
4055
4056 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004057 /* Didn't find extension name in any of the global layers, error out
4058 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004059 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004060 }
4061 }
4062 return VK_SUCCESS;
4063}
4064
4065VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06004066 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004067 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07004068 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004069 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004070 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004071 struct loader_layer_properties *layer_prop;
4072
Jon Ashburnf19916e2016-01-11 13:12:43 -07004073 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004074
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004075 VkStringErrorFlags result = vk_string_validate(
4076 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004077 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06004078 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4079 0, "Loader: Device ppEnabledExtensionNames contains "
4080 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004081 return VK_ERROR_EXTENSION_NOT_PRESENT;
4082 }
4083
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004084 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004085 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004086
4087 if (extension_prop) {
4088 continue;
4089 }
4090
Jon Ashburn471f44c2016-01-13 12:51:43 -07004091 /* Not in global list, search activated layer extension lists */
4092 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4093 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004094
Jon Ashburn23d36b12016-02-02 17:47:28 -07004095 extension_prop = get_dev_extension_property(
4096 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004097 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004098 /* Found the extension in one of the layers enabled by the app.
4099 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004100 break;
4101 }
4102 }
4103
4104 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004105 /* Didn't find extension name in any of the device layers, error out
4106 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004107 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004108 }
4109 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004110 return VK_SUCCESS;
4111}
4112
Jon Ashburn1530c342016-02-26 13:14:27 -07004113/**
4114 * Terminator functions for the Instance chain
4115 * All named terminator_<Vulakn API name>
4116 */
Mark Young0ad83132016-06-30 13:02:42 -06004117VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
4118 const VkInstanceCreateInfo *pCreateInfo,
4119 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Jon Ashburn46888392015-01-29 15:45:51 -07004120 struct loader_icd *icd;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004121 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004122 char **filtered_extension_names = NULL;
4123 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004124 VkResult res = VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004125
Jon Ashburncc407a22016-04-15 09:25:03 -06004126 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004127 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4128
Jon Ashburnf19916e2016-01-11 13:12:43 -07004129 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004130 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004131
4132 /*
4133 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004134 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004135 * No ICD will advertise support for layers. An ICD
4136 * library could support a layer, but it would be
4137 * independent of the actual ICD, just in the same library.
4138 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004139 filtered_extension_names =
4140 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004141 if (!filtered_extension_names) {
Mark Young3a587792016-08-19 15:25:08 -06004142 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4143 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004144 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004145 icd_create_info.ppEnabledExtensionNames =
4146 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004147
Jon Ashburn8810c5f2015-08-18 18:04:47 -06004148 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
4149 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06004150 if (NULL == icd) {
Mark Young3a587792016-08-19 15:25:08 -06004151 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4152 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004153 }
4154 icd_create_info.enabledExtensionCount = 0;
4155 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004156
Mark Young0ad83132016-06-30 13:02:42 -06004157 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4158 "Build ICD instance extension list");
4159 // traverse scanned icd list adding non-duplicate extensions to the
4160 // list
Mark Young3a587792016-08-19 15:25:08 -06004161 res = loader_init_generic_list(ptr_instance,
Mark Young0ad83132016-06-30 13:02:42 -06004162 (struct loader_generic_list *)&icd_exts,
4163 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004164 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4165 // If out of memory, bail immediately.
4166 goto out;
4167 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004168 // Something bad happened with this ICD, so free it and try the
4169 // next.
4170 ptr_instance->icds = icd->next;
Mark Youngdb13a2a2016-09-06 13:53:03 -06004171 icd->next = NULL;
Mark Young7e471292016-09-06 09:53:45 -06004172 loader_icd_destroy(ptr_instance, icd, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004173 continue;
4174 }
4175
4176 res = loader_add_instance_extensions(
Mark Young0ad83132016-06-30 13:02:42 -06004177 ptr_instance,
4178 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
4179 icd->this_icd_lib->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004180 if (VK_SUCCESS != res) {
4181 loader_destroy_generic_list(ptr_instance,
4182 (struct loader_generic_list *)&icd_exts);
4183 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4184 // If out of memory, bail immediately.
4185 goto out;
4186 } else {
4187 // Something bad happened with this ICD, so free it and try
4188 // the next.
4189 ptr_instance->icds = icd->next;
4190 icd->next = NULL;
4191 loader_icd_destroy(ptr_instance, icd, pAllocator);
4192 continue;
4193 }
Mark Young3a587792016-08-19 15:25:08 -06004194 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004195
Mark Young0ad83132016-06-30 13:02:42 -06004196 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4197 prop = get_extension_property(
4198 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4199 if (prop) {
4200 filtered_extension_names[icd_create_info
4201 .enabledExtensionCount] =
4202 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4203 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004204 }
4205 }
Mark Young0ad83132016-06-30 13:02:42 -06004206
4207 loader_destroy_generic_list(ptr_instance,
4208 (struct loader_generic_list *)&icd_exts);
4209
4210 res = ptr_instance->icd_libs.list[i].CreateInstance(
4211 &icd_create_info, pAllocator, &(icd->instance));
Mark Young3a587792016-08-19 15:25:08 -06004212 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4213 // If out of memory, bail immediately.
4214 goto out;
4215 } else if (VK_SUCCESS != res) {
Mark Young3a587792016-08-19 15:25:08 -06004216 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4217 "ICD ignored: failed to CreateInstance in ICD %d", i);
Mark Young7e471292016-09-06 09:53:45 -06004218 ptr_instance->icds = icd->next;
Mark Youngdb13a2a2016-09-06 13:53:03 -06004219 icd->next = NULL;
Mark Young7e471292016-09-06 09:53:45 -06004220 loader_icd_destroy(ptr_instance, icd, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004221 continue;
4222 }
Mark Young0ad83132016-06-30 13:02:42 -06004223
Mark Young3a587792016-08-19 15:25:08 -06004224 if (!loader_icd_init_entrys(
4225 icd, icd->instance,
4226 ptr_instance->icd_libs.list[i].GetInstanceProcAddr)) {
4227 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06004228 "ICD ignored: failed to CreateInstance and find "
4229 "entrypoints with ICD");
Mark Young3a587792016-08-19 15:25:08 -06004230 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004231 }
Jon Ashburn46888392015-01-29 15:45:51 -07004232 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004233
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004234 /*
4235 * If no ICDs were added to instance list and res is unchanged
4236 * from it's initial value, the loader was unable to find
4237 * a suitable ICD.
4238 */
Mark Young3a587792016-08-19 15:25:08 -06004239 if (VK_SUCCESS == res && ptr_instance->icds == NULL) {
4240 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4241 }
4242
4243out:
4244
4245 if (VK_SUCCESS != res) {
4246 while (NULL != ptr_instance->icds) {
4247 icd = ptr_instance->icds;
4248 ptr_instance->icds = icd->next;
4249 if (NULL != icd->instance) {
4250 icd->DestroyInstance(icd->instance, pAllocator);
4251 }
4252 loader_icd_destroy(ptr_instance, icd, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004253 }
Ian Elliotteb450762015-02-05 15:19:15 -07004254 }
Jon Ashburn46888392015-01-29 15:45:51 -07004255
Mark Young3a587792016-08-19 15:25:08 -06004256 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004257}
4258
Mark Young0ad83132016-06-30 13:02:42 -06004259VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4260 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004261 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004262 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06004263 struct loader_icd *next_icd;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004264
4265 // Remove this instance from the list of instances:
4266 struct loader_instance *prev = NULL;
4267 struct loader_instance *next = loader.instances;
4268 while (next != NULL) {
4269 if (next == ptr_instance) {
4270 // Remove this instance from the list:
4271 if (prev)
4272 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004273 else
4274 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004275 break;
4276 }
4277 prev = next;
4278 next = next->next;
4279 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004280
Jon Ashburn3da71f22015-05-14 12:43:38 -06004281 while (icds) {
4282 if (icds->instance) {
Chia-I Wuf7458c52015-10-26 21:10:41 +08004283 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004284 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06004285 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004286 icds->instance = VK_NULL_HANDLE;
Mark Young0ad83132016-06-30 13:02:42 -06004287 loader_icd_destroy(ptr_instance, icds, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004288
4289 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07004290 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004291
Jon Ashburn23d36b12016-02-02 17:47:28 -07004292 loader_delete_layer_properties(ptr_instance,
4293 &ptr_instance->instance_layer_list);
Jon Ashburne39a4f82015-08-28 13:38:21 -06004294 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004295 loader_destroy_generic_list(
4296 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004297 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004298 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004299 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004300}
4301
Mark Young0ad83132016-06-30 13:02:42 -06004302VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4303 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4304 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4305 VkResult res = VK_SUCCESS;
Jon Ashburn1530c342016-02-26 13:14:27 -07004306 struct loader_physical_device *phys_dev;
Jon Ashburn014438f2016-03-01 19:51:07 -07004307 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004308
Jon Ashburncc407a22016-04-15 09:25:03 -06004309 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn72690f22016-03-29 12:52:13 -06004310 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004311 struct loader_extension_list icd_exts;
4312
4313 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004314
Jon Ashburn1530c342016-02-26 13:14:27 -07004315 if (fpCreateDevice == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06004316 res = VK_ERROR_INITIALIZATION_FAILED;
4317 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004318 }
4319
Jon Ashburn1530c342016-02-26 13:14:27 -07004320 VkDeviceCreateInfo localCreateInfo;
4321 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004322
4323 /*
4324 * NOTE: Need to filter the extensions to only those
4325 * supported by the ICD.
4326 * No ICD will advertise support for layers. An ICD
4327 * library could support a layer, but it would be
4328 * independent of the actual ICD, just in the same library.
4329 */
4330 char **filtered_extension_names = NULL;
4331 filtered_extension_names =
4332 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4333 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004334 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004335 }
4336
Jon Ashburn1530c342016-02-26 13:14:27 -07004337 localCreateInfo.enabledLayerCount = 0;
4338 localCreateInfo.ppEnabledLayerNames = NULL;
4339
4340 localCreateInfo.enabledExtensionCount = 0;
4341 localCreateInfo.ppEnabledExtensionNames =
4342 (const char *const *)filtered_extension_names;
4343
Jon Ashburn014438f2016-03-01 19:51:07 -07004344 /* Get the physical device (ICD) extensions */
Mark Young3a587792016-08-19 15:25:08 -06004345 res = loader_init_generic_list(phys_dev->this_icd->this_instance,
Jon Ashburn014438f2016-03-01 19:51:07 -07004346 (struct loader_generic_list *)&icd_exts,
Mark Young3a587792016-08-19 15:25:08 -06004347 sizeof(VkExtensionProperties));
4348 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004349 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004350 }
4351
4352 res = loader_add_device_extensions(
Jon Ashburncc407a22016-04-15 09:25:03 -06004353 phys_dev->this_icd->this_instance,
4354 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn014438f2016-03-01 19:51:07 -07004355 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
4356 &icd_exts);
4357 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004358 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004359 }
4360
Jon Ashburn1530c342016-02-26 13:14:27 -07004361 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4362 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004363 VkExtensionProperties *prop =
4364 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004365 if (prop) {
4366 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4367 (char *)extension_name;
4368 localCreateInfo.enabledExtensionCount++;
4369 }
4370 }
4371
Jon Ashburn1530c342016-02-26 13:14:27 -07004372 // TODO: Why does fpCreateDevice behave differently than
4373 // this_icd->CreateDevice?
4374 // VkResult res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo,
4375 // pAllocator, &localDevice);
Jon Ashburn014438f2016-03-01 19:51:07 -07004376 res = phys_dev->this_icd->CreateDevice(phys_dev->phys_dev, &localCreateInfo,
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004377 pAllocator, &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004378
4379 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004380 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004381 }
4382
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004383 *pDevice = dev->device;
Jon Ashburncc407a22016-04-15 09:25:03 -06004384 loader_add_logical_device(phys_dev->this_icd->this_instance,
4385 phys_dev->this_icd, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004386
4387 /* Init dispatch pointer in new device object */
4388 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4389
Mark Young0ad83132016-06-30 13:02:42 -06004390out:
4391 if (NULL != icd_exts.list) {
4392 loader_destroy_generic_list(phys_dev->this_icd->this_instance,
4393 (struct loader_generic_list *)&icd_exts);
4394 }
4395
Jon Ashburn1530c342016-02-26 13:14:27 -07004396 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004397}
4398
Jon Ashburn23d36b12016-02-02 17:47:28 -07004399VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004400terminator_EnumeratePhysicalDevices(VkInstance instance,
4401 uint32_t *pPhysicalDeviceCount,
4402 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004403 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004404 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004405 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004406
Jon Ashburn014438f2016-03-01 19:51:07 -07004407 struct loader_icd *icd;
4408 struct loader_phys_dev_per_icd *phys_devs;
4409
4410 inst->total_gpu_count = 0;
4411 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4412 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4413 if (!phys_devs)
4414 return VK_ERROR_OUT_OF_HOST_MEMORY;
4415
4416 icd = inst->icds;
4417 for (i = 0; i < inst->total_icd_count; i++) {
4418 assert(icd);
4419 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
4420 NULL);
4421 if (res != VK_SUCCESS)
4422 return res;
4423 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004424 }
4425
Jon Ashburn014438f2016-03-01 19:51:07 -07004426 icd = inst->icds;
4427 for (i = 0; i < inst->total_icd_count; i++) {
4428 assert(icd);
4429 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4430 phys_devs[i].count * sizeof(VkPhysicalDevice));
4431 if (!phys_devs[i].phys_devs) {
4432 return VK_ERROR_OUT_OF_HOST_MEMORY;
4433 }
4434 res = icd->EnumeratePhysicalDevices(
4435 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
4436 if ((res == VK_SUCCESS)) {
4437 inst->total_gpu_count += phys_devs[i].count;
4438 } else {
4439 return res;
4440 }
4441 phys_devs[i].this_icd = icd;
4442 icd = icd->next;
4443 }
4444
Mark Young559d7502016-09-26 11:38:46 -06004445 uint32_t copy_count = inst->total_gpu_count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004446
Mark Young559d7502016-09-26 11:38:46 -06004447 if (NULL != pPhysicalDevices) {
4448 // Initialize the output pPhysicalDevices with wrapped loader
4449 // terminator physicalDevice objects; save this list of
4450 // wrapped objects in instance struct for later cleanup and
4451 // use by trampoline code
4452 uint32_t j, idx = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004453
Mark Young559d7502016-09-26 11:38:46 -06004454 if (copy_count > *pPhysicalDeviceCount) {
4455 copy_count = *pPhysicalDeviceCount;
4456 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004457
Mark Young559d7502016-09-26 11:38:46 -06004458 if (inst->phys_devs_term) {
4459 loader_instance_heap_free(inst, inst->phys_devs_term);
Maciej Jesionowski7c55ba42016-09-30 15:14:52 +02004460 inst->phys_devs_term = NULL;
Mark Young559d7502016-09-26 11:38:46 -06004461 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004462
Mark Young559d7502016-09-26 11:38:46 -06004463 if (copy_count > 0) {
4464 inst->phys_devs_term = loader_instance_heap_alloc(
4465 inst, sizeof(struct loader_physical_device) * copy_count,
4466 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4467 if (!inst->phys_devs_term) {
4468 return VK_ERROR_OUT_OF_HOST_MEMORY;
4469 }
4470 }
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004471
Mark Young559d7502016-09-26 11:38:46 -06004472 for (i = 0; idx < copy_count && i < inst->total_icd_count; i++) {
4473 for (j = 0; j < phys_devs[i].count && idx < copy_count; j++) {
4474 loader_set_dispatch((void *)&inst->phys_devs_term[idx],
4475 inst->disp);
4476 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
4477 inst->phys_devs_term[idx].icd_index = i;
4478 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
4479 pPhysicalDevices[idx] =
4480 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4481 idx++;
4482 }
4483 }
4484
4485 if (copy_count < inst->total_gpu_count) {
4486 inst->total_gpu_count = copy_count;
4487 res = VK_INCOMPLETE;
Jon Ashburn014438f2016-03-01 19:51:07 -07004488 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004489 }
Mark Young559d7502016-09-26 11:38:46 -06004490
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004491 *pPhysicalDeviceCount = copy_count;
4492
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004493 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004494}
4495
Jon Ashburn1530c342016-02-26 13:14:27 -07004496VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4497 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004498 struct loader_physical_device *phys_dev =
4499 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004500 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004501
Tony Barbour59a47322015-06-24 16:06:58 -06004502 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004503 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004504}
4505
Jon Ashburn1530c342016-02-26 13:14:27 -07004506VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004507 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4508 VkQueueFamilyProperties *pProperties) {
4509 struct loader_physical_device *phys_dev =
4510 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004511 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004512
Cody Northropd0802882015-08-03 17:04:53 -06004513 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004514 icd->GetPhysicalDeviceQueueFamilyProperties(
4515 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004516}
4517
Jon Ashburn1530c342016-02-26 13:14:27 -07004518VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004519 VkPhysicalDevice physicalDevice,
4520 VkPhysicalDeviceMemoryProperties *pProperties) {
4521 struct loader_physical_device *phys_dev =
4522 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004523 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004524
4525 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004526 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004527}
4528
Jon Ashburn23d36b12016-02-02 17:47:28 -07004529VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004530terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4531 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004532 struct loader_physical_device *phys_dev =
4533 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004534 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004535
4536 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004537 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004538}
4539
Jon Ashburn23d36b12016-02-02 17:47:28 -07004540VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004541terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4542 VkFormat format,
4543 VkFormatProperties *pFormatInfo) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004544 struct loader_physical_device *phys_dev =
4545 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004546 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004547
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -06004548 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004549 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4550 pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004551}
4552
Jon Ashburn1530c342016-02-26 13:14:27 -07004553VKAPI_ATTR VkResult VKAPI_CALL
4554terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004555 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4556 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4557 VkImageFormatProperties *pImageFormatProperties) {
4558 struct loader_physical_device *phys_dev =
4559 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004560 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn754864f2015-07-23 18:49:07 -06004561
Chia-I Wu17241042015-10-31 00:31:16 +08004562 if (!icd->GetPhysicalDeviceImageFormatProperties)
4563 return VK_ERROR_INITIALIZATION_FAILED;
4564
Jon Ashburn23d36b12016-02-02 17:47:28 -07004565 return icd->GetPhysicalDeviceImageFormatProperties(
4566 phys_dev->phys_dev, format, type, tiling, usage, flags,
4567 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004568}
4569
Jon Ashburn1530c342016-02-26 13:14:27 -07004570VKAPI_ATTR void VKAPI_CALL
4571terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004572 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4573 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4574 VkImageTiling tiling, uint32_t *pNumProperties,
4575 VkSparseImageFormatProperties *pProperties) {
4576 struct loader_physical_device *phys_dev =
4577 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004578 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004579
4580 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004581 icd->GetPhysicalDeviceSparseImageFormatProperties(
4582 phys_dev->phys_dev, format, type, samples, usage, tiling,
4583 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004584}
4585
Jon Ashburn1530c342016-02-26 13:14:27 -07004586VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4587 VkPhysicalDevice physicalDevice, const char *pLayerName,
4588 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburna760a512015-12-14 08:52:14 -07004589 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004590
Mark Young3a587792016-08-19 15:25:08 -06004591 struct loader_layer_list implicit_layer_list = {0};
4592 struct loader_extension_list all_exts = {0};
4593 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07004594
Jon Ashburndc5d9202016-02-29 13:00:51 -07004595 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004596
Jon Ashburndc5d9202016-02-29 13:00:51 -07004597 /* Any layer or trampoline wrapping should be removed at this point in time
4598 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn014438f2016-03-01 19:51:07 -07004599 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004600
Jon Ashburndc5d9202016-02-29 13:00:51 -07004601 /* this case is during the call down the instance chain with pLayerName
4602 * == NULL*/
4603 struct loader_icd *icd = phys_dev->this_icd;
4604 uint32_t icd_ext_count = *pPropertyCount;
4605 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004606
Jon Ashburndc5d9202016-02-29 13:00:51 -07004607 /* get device extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004608 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4609 &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06004610 if (res != VK_SUCCESS) {
4611 goto out;
4612 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004613
Mark Young3a587792016-08-19 15:25:08 -06004614 if (!loader_init_layer_list(icd->this_instance, &implicit_layer_list)) {
4615 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4616 goto out;
4617 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004618
Jon Ashburndc5d9202016-02-29 13:00:51 -07004619 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004620 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4621 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004622 /* we need to determine which implicit layers are active,
4623 * and then add their extensions. This can't be cached as
4624 * it depends on results of environment variables (which can change).
4625 */
4626 if (pProperties != NULL) {
4627 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn014438f2016-03-01 19:51:07 -07004628 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4629 icd_ext_count, pProperties,
4630 &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004631 if (res != VK_SUCCESS) {
4632 goto out;
4633 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004634
Jon Ashburndc5d9202016-02-29 13:00:51 -07004635 /* we need to determine which implicit layers are active,
4636 * and then add their extensions. This can't be cached as
4637 * it depends on results of environment variables (which can
4638 * change).
4639 */
Mark Young3a587792016-08-19 15:25:08 -06004640 res = loader_add_to_ext_list(icd->this_instance, &all_exts,
4641 icd_exts.count, icd_exts.list);
4642 if (res != VK_SUCCESS) {
4643 goto out;
4644 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004645
Jon Ashburndc5d9202016-02-29 13:00:51 -07004646 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004647 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4648 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004649
Jon Ashburndc5d9202016-02-29 13:00:51 -07004650 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4651 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004652 j < implicit_layer_list.list[i].device_extension_list.count;
4653 j++) {
Mark Young3a587792016-08-19 15:25:08 -06004654 res = loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4655 &implicit_layer_list.list[i]
4656 .device_extension_list.list[j]
4657 .props);
4658 if (res != VK_SUCCESS) {
4659 goto out;
4660 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004661 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004662 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004663 uint32_t capacity = *pPropertyCount;
4664 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004665
Jon Ashburndc5d9202016-02-29 13:00:51 -07004666 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4667 props[i] = all_exts.list[i];
4668 }
4669 /* wasn't enough space for the extensions, we did partial copy now
4670 * return VK_INCOMPLETE */
4671 if (capacity < all_exts.count) {
4672 res = VK_INCOMPLETE;
4673 } else {
4674 *pPropertyCount = all_exts.count;
4675 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004676 } else {
4677 /* just return the count; need to add in the count of implicit layer
4678 * extensions
4679 * don't worry about duplicates being added in the count */
4680 *pPropertyCount = icd_ext_count;
4681
4682 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4683 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004684 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004685 }
4686 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004687 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004688
Mark Young3a587792016-08-19 15:25:08 -06004689out:
4690
4691 if (NULL != implicit_layer_list.list) {
4692 loader_destroy_generic_list(
4693 icd->this_instance,
4694 (struct loader_generic_list *)&implicit_layer_list);
4695 }
4696 if (NULL != all_exts.list) {
4697 loader_destroy_generic_list(icd->this_instance,
4698 (struct loader_generic_list *)&all_exts);
4699 }
4700 if (NULL != icd_exts.list) {
4701 loader_destroy_generic_list(icd->this_instance,
4702 (struct loader_generic_list *)&icd_exts);
4703 }
4704
Jon Ashburndc5d9202016-02-29 13:00:51 -07004705 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004706}
4707
Jon Ashburn23d36b12016-02-02 17:47:28 -07004708VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004709terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4710 uint32_t *pPropertyCount,
4711 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004712
Jon Ashburndc5d9202016-02-29 13:00:51 -07004713 // should never get here this call isn't dispatched down the chain
4714 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004715}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004716
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004717VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004718 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004719 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004720 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004721
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004722 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004723 if (utf8[i] == 0) {
4724 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004725 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004726 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004727 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004728 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004729 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004730 num_char_bytes = 2;
4731 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4732 num_char_bytes = 3;
4733 } else {
4734 result = VK_STRING_ERROR_BAD_DATA;
4735 }
4736
4737 // Validate the following num_char_bytes of data
4738 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4739 if (++i == max_length) {
4740 result |= VK_STRING_ERROR_LENGTH;
4741 break;
4742 }
4743 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4744 result |= VK_STRING_ERROR_BAD_DATA;
4745 }
4746 }
4747 }
4748 return result;
4749}