blob: b2dd10a4227da89491e5d6ab0ac530317b6bbd02 [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002 *
Jon Ashburn44aed662016-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 Goeltzenleuchterebbb96d2015-12-02 14:53:22 -07006 * Copyright (C) 2015 Google Inc.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08007 *
Jon Ashburn44aed662016-02-02 17:47:28 -07008 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and/or associated documentation files (the "Materials"), to
10 * deal in the Materials without restriction, including without limitation the
11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Materials, and to permit persons to whom the Materials are
13 * furnished to do so, subject to the following conditions:
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080014 *
Jon Ashburn44aed662016-02-02 17:47:28 -070015 * The above copyright notice(s) and this permission notice shall be included in
16 * all copies or substantial portions of the Materials.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080017 *
Jon Ashburn44aed662016-02-02 17:47:28 -070018 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jon Ashburn44aed662016-02-02 17:47:28 -070020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Chia-I Wu44e42362014-09-02 08:32:09 +080021 *
Jon Ashburn44aed662016-02-02 17:47:28 -070022 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25 * USE OR OTHER DEALINGS IN THE MATERIALS.
26 *
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060027 * Author: Jon Ashburn <jon@lunarg.com>
Jon Ashburn44aed662016-02-02 17:47:28 -070028 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060029 *
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080030 */
Mark Lobodzinski28214d92015-11-25 13:26:15 -070031
Jon Ashburn183dfd02014-10-22 18:13:16 -060032#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080033#include <stdio.h>
34#include <stdlib.h>
35#include <stdarg.h>
36#include <stdbool.h>
37#include <string.h>
38
Chia-I Wu894a1172014-08-04 11:18:20 +080039#include <sys/types.h>
Johannes van Waverena6ce7b72015-10-28 11:45:00 -050040#if defined(_WIN32)
Ian Elliott81ac44c2015-01-13 17:52:38 -070041#include "dirent_on_windows.h"
Johannes van Waverena6ce7b72015-10-28 11:45:00 -050042#else // _WIN32
Chia-I Wu894a1172014-08-04 11:18:20 +080043#include <dirent.h>
Johannes van Waverena6ce7b72015-10-28 11:45:00 -050044#endif // _WIN32
Tobin Ehlis7a51d902015-07-03 10:34:49 -060045#include "vk_loader_platform.h"
Chia-I Wu468e3c32014-08-04 08:03:57 +080046#include "loader.h"
Jon Ashburnfce93d92015-05-12 17:26:48 -060047#include "gpa_helper.h"
48#include "table_ops.h"
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060049#include "debug_report.h"
Ian Elliotta81e8ac2015-10-30 15:28:23 -060050#include "wsi.h"
David Pinedo329ca9e2015-11-06 12:54:48 -070051#include "vulkan/vk_icd.h"
Jon Ashburnffd5d672015-06-29 11:25:34 -060052#include "cJSON.h"
Jon Ashburn429e19f2015-11-17 15:31:02 -070053#include "murmurhash.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080054
Jon Ashburn7f7fea82016-04-04 13:52:53 -060055#if defined(__GNUC__)
Jon Ashburn9b2a8c92016-04-15 09:25:03 -060056#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 17)
57#define secure_getenv __secure_getenv
58#endif
Jon Ashburn7f7fea82016-04-04 13:52:53 -060059#endif
60
Jon Ashburn44aed662016-02-02 17:47:28 -070061static loader_platform_dl_handle
62loader_add_layer_lib(const struct loader_instance *inst, const char *chain_type,
63 struct loader_layer_properties *layer_prop);
Courtney Goeltzenleuchter71cd74d2015-06-01 14:09:34 -060064
Jon Ashburn44aed662016-02-02 17:47:28 -070065static void loader_remove_layer_lib(struct loader_instance *inst,
66 struct loader_layer_properties *layer_prop);
Courtney Goeltzenleuchter71cd74d2015-06-01 14:09:34 -060067
Jon Ashburnfce93d92015-05-12 17:26:48 -060068struct loader_struct loader = {0};
Jon Ashburn413d6582015-08-28 15:19:27 -060069// TLS for instance for alloc/free callbacks
70THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080071
Courtney Goeltzenleuchtere728d122015-12-16 16:19:46 -070072static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap2e13fca2015-09-30 11:50:51 -050073
Jon Ashburn251258f2015-11-01 14:04:06 -070074struct loader_phys_dev_per_icd {
75 uint32_t count;
76 VkPhysicalDevice *phys_devs;
Jon Ashburn87660432016-03-01 19:51:07 -070077 struct loader_icd *this_icd;
Jon Ashburn251258f2015-11-01 14:04:06 -070078};
79
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -060080enum loader_debug {
Jon Ashburn44aed662016-02-02 17:47:28 -070081 LOADER_INFO_BIT = 0x01,
82 LOADER_WARN_BIT = 0x02,
83 LOADER_PERF_BIT = 0x04,
84 LOADER_ERROR_BIT = 0x08,
85 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -060086};
87
88uint32_t g_loader_debug = 0;
89uint32_t g_loader_log_msgs = 0;
90
Jon Ashburn44aed662016-02-02 17:47:28 -070091// thread safety lock for accessing global data structures such as "loader"
Jon Ashburnb40f2562015-05-29 13:15:39 -060092// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburnffd5d672015-06-29 11:25:34 -060093// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburnb40f2562015-05-29 13:15:39 -060094loader_platform_thread_mutex loader_lock;
Jon Ashburnd02fc2c2015-09-22 13:11:00 -060095loader_platform_thread_mutex loader_json_lock;
Jon Ashburnb40f2562015-05-29 13:15:39 -060096
Jon Ashburn715de582016-02-10 20:59:26 -070097const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
98
Ian Elliott1d73e662015-07-06 14:36:13 -060099// This table contains the loader's instance dispatch table, which contains
100// default functions if no instance layers are activated. This contains
101// pointers to "terminator functions".
Jon Ashburnb40f2562015-05-29 13:15:39 -0600102const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn8aa75cb2015-09-28 16:15:00 -0600103 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburna9c4a572016-02-26 13:14:27 -0700104 .DestroyInstance = terminator_DestroyInstance,
105 .EnumeratePhysicalDevices = terminator_EnumeratePhysicalDevices,
106 .GetPhysicalDeviceFeatures = terminator_GetPhysicalDeviceFeatures,
Jon Ashburn44aed662016-02-02 17:47:28 -0700107 .GetPhysicalDeviceFormatProperties =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700108 terminator_GetPhysicalDeviceFormatProperties,
Jon Ashburn44aed662016-02-02 17:47:28 -0700109 .GetPhysicalDeviceImageFormatProperties =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700110 terminator_GetPhysicalDeviceImageFormatProperties,
111 .GetPhysicalDeviceProperties = terminator_GetPhysicalDeviceProperties,
Jon Ashburn44aed662016-02-02 17:47:28 -0700112 .GetPhysicalDeviceQueueFamilyProperties =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700113 terminator_GetPhysicalDeviceQueueFamilyProperties,
Jon Ashburn44aed662016-02-02 17:47:28 -0700114 .GetPhysicalDeviceMemoryProperties =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700115 terminator_GetPhysicalDeviceMemoryProperties,
Jon Ashburn44aed662016-02-02 17:47:28 -0700116 .EnumerateDeviceExtensionProperties =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700117 terminator_EnumerateDeviceExtensionProperties,
118 .EnumerateDeviceLayerProperties = terminator_EnumerateDeviceLayerProperties,
Jon Ashburn44aed662016-02-02 17:47:28 -0700119 .GetPhysicalDeviceSparseImageFormatProperties =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700120 terminator_GetPhysicalDeviceSparseImageFormatProperties,
121 .DestroySurfaceKHR = terminator_DestroySurfaceKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700122 .GetPhysicalDeviceSurfaceSupportKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700123 terminator_GetPhysicalDeviceSurfaceSupportKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700124 .GetPhysicalDeviceSurfaceCapabilitiesKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700125 terminator_GetPhysicalDeviceSurfaceCapabilitiesKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700126 .GetPhysicalDeviceSurfaceFormatsKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700127 terminator_GetPhysicalDeviceSurfaceFormatsKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700128 .GetPhysicalDeviceSurfacePresentModesKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700129 terminator_GetPhysicalDeviceSurfacePresentModesKHR,
130 .CreateDebugReportCallbackEXT = terminator_CreateDebugReportCallback,
131 .DestroyDebugReportCallbackEXT = terminator_DestroyDebugReportCallback,
132 .DebugReportMessageEXT = terminator_DebugReportMessage,
Ian Elliott40c4a1f2015-11-23 10:17:23 -0700133#ifdef VK_USE_PLATFORM_MIR_KHR
Jon Ashburna9c4a572016-02-26 13:14:27 -0700134 .CreateMirSurfaceKHR = terminator_CreateMirSurfaceKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700135 .GetPhysicalDeviceMirPresentationSupportKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700136 terminator_GetPhysicalDeviceMirPresentationSupportKHR,
Ian Elliott40c4a1f2015-11-23 10:17:23 -0700137#endif
138#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jon Ashburna9c4a572016-02-26 13:14:27 -0700139 .CreateWaylandSurfaceKHR = terminator_CreateWaylandSurfaceKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700140 .GetPhysicalDeviceWaylandPresentationSupportKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700141 terminator_GetPhysicalDeviceWaylandPresentationSupportKHR,
Ian Elliott40c4a1f2015-11-23 10:17:23 -0700142#endif
143#ifdef VK_USE_PLATFORM_WIN32_KHR
Jon Ashburna9c4a572016-02-26 13:14:27 -0700144 .CreateWin32SurfaceKHR = terminator_CreateWin32SurfaceKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700145 .GetPhysicalDeviceWin32PresentationSupportKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700146 terminator_GetPhysicalDeviceWin32PresentationSupportKHR,
Ian Elliott40c4a1f2015-11-23 10:17:23 -0700147#endif
148#ifdef VK_USE_PLATFORM_XCB_KHR
Jon Ashburna9c4a572016-02-26 13:14:27 -0700149 .CreateXcbSurfaceKHR = terminator_CreateXcbSurfaceKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700150 .GetPhysicalDeviceXcbPresentationSupportKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700151 terminator_GetPhysicalDeviceXcbPresentationSupportKHR,
Ian Elliott40c4a1f2015-11-23 10:17:23 -0700152#endif
153#ifdef VK_USE_PLATFORM_XLIB_KHR
Jon Ashburna9c4a572016-02-26 13:14:27 -0700154 .CreateXlibSurfaceKHR = terminator_CreateXlibSurfaceKHR,
Jon Ashburn44aed662016-02-02 17:47:28 -0700155 .GetPhysicalDeviceXlibPresentationSupportKHR =
Jon Ashburna9c4a572016-02-26 13:14:27 -0700156 terminator_GetPhysicalDeviceXlibPresentationSupportKHR,
Ian Elliott40c4a1f2015-11-23 10:17:23 -0700157#endif
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -0700158#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jon Ashburna9c4a572016-02-26 13:14:27 -0700159 .CreateAndroidSurfaceKHR = terminator_CreateAndroidSurfaceKHR,
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -0700160#endif
Jon Ashburn00df0452016-03-08 09:30:30 -0700161 .GetPhysicalDeviceDisplayPropertiesKHR =
162 terminator_GetPhysicalDeviceDisplayPropertiesKHR,
163 .GetPhysicalDeviceDisplayPlanePropertiesKHR =
164 terminator_GetPhysicalDeviceDisplayPlanePropertiesKHR,
165 .GetDisplayPlaneSupportedDisplaysKHR =
166 terminator_GetDisplayPlaneSupportedDisplaysKHR,
Jon Ashburn9b2a8c92016-04-15 09:25:03 -0600167 .GetDisplayModePropertiesKHR = terminator_GetDisplayModePropertiesKHR,
168 .CreateDisplayModeKHR = terminator_CreateDisplayModeKHR,
169 .GetDisplayPlaneCapabilitiesKHR = terminator_GetDisplayPlaneCapabilitiesKHR,
170 .CreateDisplayPlaneSurfaceKHR = terminator_CreateDisplayPlaneSurfaceKHR,
Jon Ashburnfce93d92015-05-12 17:26:48 -0600171};
172
Jon Ashburn754f1992015-08-18 18:04:47 -0600173LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700174
Jon Ashburn44aed662016-02-02 17:47:28 -0700175void *loader_heap_alloc(const struct loader_instance *instance, size_t size,
176 VkSystemAllocationScope alloc_scope) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800177 if (instance && instance->alloc_callbacks.pfnAllocation) {
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600178 /* TODO: What should default alignment be? 1, 4, 8, other? */
Jon Ashburn44aed662016-02-02 17:47:28 -0700179 return instance->alloc_callbacks.pfnAllocation(
180 instance->alloc_callbacks.pUserData, size, sizeof(int),
181 alloc_scope);
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600182 }
183 return malloc(size);
184}
185
Jon Ashburn44aed662016-02-02 17:47:28 -0700186void loader_heap_free(const struct loader_instance *instance, void *pMemory) {
187 if (pMemory == NULL)
188 return;
Jon Ashburncfe4e682015-08-14 12:51:47 -0600189 if (instance && instance->alloc_callbacks.pfnFree) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700190 instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData,
191 pMemory);
Jon Ashburnfab6ee22015-08-13 08:28:48 -0600192 return;
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600193 }
Chia-I Wu1f851912015-10-27 18:04:07 +0800194 free(pMemory);
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600195}
196
Jon Ashburn44aed662016-02-02 17:47:28 -0700197void *loader_heap_realloc(const struct loader_instance *instance, void *pMemory,
198 size_t orig_size, size_t size,
199 VkSystemAllocationScope alloc_scope) {
200 if (pMemory == NULL || orig_size == 0)
Chia-I Wu69f40122015-10-26 21:10:41 +0800201 return loader_heap_alloc(instance, size, alloc_scope);
Jon Ashburne5e60df2015-08-27 15:23:52 -0600202 if (size == 0) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800203 loader_heap_free(instance, pMemory);
Jon Ashburne5e60df2015-08-27 15:23:52 -0600204 return NULL;
205 }
Jon Ashburn44aed662016-02-02 17:47:28 -0700206 // TODO use the callback realloc function
Chia-I Wu1f851912015-10-27 18:04:07 +0800207 if (instance && instance->alloc_callbacks.pfnAllocation) {
Jon Ashburne5e60df2015-08-27 15:23:52 -0600208 if (size <= orig_size) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700209 memset(((uint8_t *)pMemory) + size, 0, orig_size - size);
Chia-I Wu1f851912015-10-27 18:04:07 +0800210 return pMemory;
Jon Ashburne5e60df2015-08-27 15:23:52 -0600211 }
Jon Ashburn9a3c2b42016-01-07 15:21:14 -0700212 /* TODO: What should default alignment be? 1, 4, 8, other? */
Jon Ashburn44aed662016-02-02 17:47:28 -0700213 void *new_ptr = instance->alloc_callbacks.pfnAllocation(
214 instance->alloc_callbacks.pUserData, size, sizeof(int),
215 alloc_scope);
Jon Ashburne5e60df2015-08-27 15:23:52 -0600216 if (!new_ptr)
217 return NULL;
Chia-I Wu1f851912015-10-27 18:04:07 +0800218 memcpy(new_ptr, pMemory, orig_size);
Jon Ashburn44aed662016-02-02 17:47:28 -0700219 instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData,
220 pMemory);
Jon Ashburn38692662016-01-27 10:56:10 -0700221 return new_ptr;
Jon Ashburne5e60df2015-08-27 15:23:52 -0600222 }
Chia-I Wu1f851912015-10-27 18:04:07 +0800223 return realloc(pMemory, size);
Jon Ashburne5e60df2015-08-27 15:23:52 -0600224}
225
Jon Ashburn44aed662016-02-02 17:47:28 -0700226void *loader_tls_heap_alloc(size_t size) {
227 return loader_heap_alloc(tls_instance, size,
228 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn413d6582015-08-28 15:19:27 -0600229}
230
Jon Ashburn44aed662016-02-02 17:47:28 -0700231void loader_tls_heap_free(void *pMemory) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800232 loader_heap_free(tls_instance, pMemory);
Jon Ashburn413d6582015-08-28 15:19:27 -0600233}
234
Jon Ashburn7d1a53d2016-02-12 08:20:06 -0700235void loader_log(const struct loader_instance *inst, VkFlags msg_type,
Jon Ashburna9c4a572016-02-26 13:14:27 -0700236 int32_t msg_code, const char *format, ...) {
Jon Ashburn2b779162015-07-31 15:47:59 -0600237 char msg[512];
Jon Ashburnee33ae72015-06-30 14:46:22 -0700238 va_list ap;
239 int ret;
240
Jon Ashburnee33ae72015-06-30 14:46:22 -0700241 va_start(ap, format);
242 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn44aed662016-02-02 17:47:28 -0700243 if ((ret >= (int)sizeof(msg)) || ret < 0) {
244 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnee33ae72015-06-30 14:46:22 -0700245 }
246 va_end(ap);
247
Courtney Goeltzenleuchter05525da2015-12-03 13:48:01 -0700248 if (inst) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700249 util_DebugReportMessage(inst, msg_type,
250 VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
251 (uint64_t)inst, 0, msg_code, "loader", msg);
Courtney Goeltzenleuchter05525da2015-12-03 13:48:01 -0700252 }
253
254 if (!(msg_type & g_loader_log_msgs)) {
255 return;
256 }
257
Ian Elliott225188f2015-02-17 10:33:47 -0700258#if defined(WIN32)
Jon Ashburnee33ae72015-06-30 14:46:22 -0700259 OutputDebugString(msg);
mschott632564c2015-07-22 14:11:29 +0200260 OutputDebugString("\n");
Jon Ashburnee33ae72015-06-30 14:46:22 -0700261#endif
262 fputs(msg, stderr);
263 fputc('\n', stderr);
264}
265
Jon Ashburn9b2a8c92016-04-15 09:25:03 -0600266VKAPI_ATTR VkResult VKAPI_CALL
267vkSetInstanceDispatch(VkInstance instance, void *object) {
Jon Ashburn7ec8ec82016-03-29 11:16:01 -0600268
269 struct loader_instance *inst = loader_get_instance(instance);
270 if (!inst) {
271 return VK_ERROR_INITIALIZATION_FAILED;
272 }
273 loader_set_dispatch(object, inst->disp);
274 return VK_SUCCESS;
275}
276
Jon Ashburn9b2a8c92016-04-15 09:25:03 -0600277VKAPI_ATTR VkResult VKAPI_CALL
278vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburn94d58ea2016-03-31 10:52:22 -0600279 struct loader_device *dev;
280 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
281
282 if (!icd) {
283 return VK_ERROR_INITIALIZATION_FAILED;
284 }
285 loader_set_dispatch(object, &dev->loader_dispatch);
286 return VK_SUCCESS;
287}
288
Jon Ashburnee33ae72015-06-30 14:46:22 -0700289#if defined(WIN32)
Tony Barbour76096e62015-07-29 14:26:21 -0600290static char *loader_get_next_path(char *path);
Jon Ashburnee33ae72015-06-30 14:46:22 -0700291/**
292* Find the list of registry files (names within a key) in key "location".
293*
Jon Ashburn44aed662016-02-02 17:47:28 -0700294* This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
295*given in "location"
296* for a list or name/values which are added to a returned list (function return
297*value).
Jon Ashburnee33ae72015-06-30 14:46:22 -0700298* The DWORD values within the key must be 0 or they are skipped.
Jon Ashburne58f1a32015-08-28 13:38:21 -0600299* Function return is a string with a ';' separated list of filenames.
Jon Ashburnee33ae72015-06-30 14:46:22 -0700300* Function return is NULL if no valid name/value pairs are found in the key,
301* or the key is not found.
302*
303* \returns
304* A string list of filenames as pointer.
305* When done using the returned string list, pointer should be freed.
306*/
Jon Ashburn44aed662016-02-02 17:47:28 -0700307static char *loader_get_registry_files(const struct loader_instance *inst,
308 char *location) {
Jon Ashburnee33ae72015-06-30 14:46:22 -0700309 LONG rtn_value;
310 HKEY hive, key;
Piers Daniellb1f6f6d2015-11-05 16:58:26 -0700311 DWORD access_flags;
Jon Ashburnee33ae72015-06-30 14:46:22 -0700312 char name[2048];
313 char *out = NULL;
Tony Barbour76096e62015-07-29 14:26:21 -0600314 char *loc = location;
315 char *next;
Jon Ashburnee33ae72015-06-30 14:46:22 -0700316 DWORD idx = 0;
317 DWORD name_size = sizeof(name);
318 DWORD value;
319 DWORD total_size = 4096;
320 DWORD value_size = sizeof(value);
Tony Barbour76096e62015-07-29 14:26:21 -0600321
Jon Ashburn44aed662016-02-02 17:47:28 -0700322 while (*loc) {
Tony Barbour76096e62015-07-29 14:26:21 -0600323 next = loader_get_next_path(loc);
324 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniellb1f6f6d2015-11-05 16:58:26 -0700325 access_flags = KEY_QUERY_VALUE;
Tony Barbour76096e62015-07-29 14:26:21 -0600326 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
327 if (rtn_value != ERROR_SUCCESS) {
Mark Young2acdd152016-01-13 13:47:16 -0700328 // We still couldn't find the key, so give up:
329 loc = next;
330 continue;
Jon Ashburnee33ae72015-06-30 14:46:22 -0700331 }
Tony Barbour76096e62015-07-29 14:26:21 -0600332
Jon Ashburn44aed662016-02-02 17:47:28 -0700333 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL,
334 NULL, (LPBYTE)&value, &value_size)) ==
335 ERROR_SUCCESS) {
Tony Barbour76096e62015-07-29 14:26:21 -0600336 if (value_size == sizeof(value) && value == 0) {
337 if (out == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700338 out = loader_heap_alloc(
339 inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Tony Barbour76096e62015-07-29 14:26:21 -0600340 out[0] = '\0';
Jon Ashburn44aed662016-02-02 17:47:28 -0700341 } else if (strlen(out) + name_size + 1 > total_size) {
342 out = loader_heap_realloc(
343 inst, out, total_size, total_size * 2,
344 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Tony Barbour76096e62015-07-29 14:26:21 -0600345 total_size *= 2;
346 }
347 if (out == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700348 loader_log(
349 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
350 "Out of memory, failed loader_get_registry_files");
Tony Barbour76096e62015-07-29 14:26:21 -0600351 return NULL;
352 }
353 if (strlen(out) == 0)
Jon Ashburn44aed662016-02-02 17:47:28 -0700354 snprintf(out, name_size + 1, "%s", name);
Tony Barbour76096e62015-07-29 14:26:21 -0600355 else
Jon Ashburn44aed662016-02-02 17:47:28 -0700356 snprintf(out + strlen(out), name_size + 2, "%c%s",
357 PATH_SEPERATOR, name);
Tony Barbour76096e62015-07-29 14:26:21 -0600358 }
359 name_size = 2048;
360 }
361 loc = next;
Jon Ashburnee33ae72015-06-30 14:46:22 -0700362 }
Tony Barbour76096e62015-07-29 14:26:21 -0600363
Jon Ashburnee33ae72015-06-30 14:46:22 -0700364 return out;
365}
366
Ian Elliott225188f2015-02-17 10:33:47 -0700367#endif // WIN32
368
Jon Ashburn4b7f35f2015-08-03 09:08:46 -0600369/**
Daniel Dadap2e13fca2015-09-30 11:50:51 -0500370 * Combine path elements, separating each element with the platform-specific
371 * directory separator, and save the combined string to a destination buffer,
372 * not exceeding the given length. Path elements are given as variadic args,
373 * with a NULL element terminating the list.
374 *
375 * \returns the total length of the combined string, not including an ASCII
376 * NUL termination character. This length may exceed the available storage:
377 * in this case, the written string will be truncated to avoid a buffer
378 * overrun, and the return value will greater than or equal to the storage
379 * size. A NULL argument may be provided as the destination buffer in order
380 * to determine the required string length without actually writing a string.
381 */
382
Jon Ashburn44aed662016-02-02 17:47:28 -0700383static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchtere728d122015-12-16 16:19:46 -0700384 size_t required_len = 0;
Daniel Dadap2e13fca2015-09-30 11:50:51 -0500385 va_list ap;
386 const char *component;
387
388 va_start(ap, len);
389
Jon Ashburn44aed662016-02-02 17:47:28 -0700390 while ((component = va_arg(ap, const char *))) {
Daniel Dadap2e13fca2015-09-30 11:50:51 -0500391 if (required_len > 0) {
392 // This path element is not the first non-empty element; prepend
393 // a directory separator if space allows
394 if (dest && required_len + 1 < len) {
395 snprintf(dest + required_len, len - required_len, "%c",
396 DIRECTORY_SYMBOL);
397 }
398 required_len++;
399 }
400
401 if (dest && required_len < len) {
402 strncpy(dest + required_len, component, len - required_len);
403 }
404 required_len += strlen(component);
405 }
406
407 va_end(ap);
408
409 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
410 if (dest && required_len >= len) {
411 dest[len - 1] = '\0';
412 }
413
414 return required_len;
415}
416
Daniel Dadap2e13fca2015-09-30 11:50:51 -0500417/**
Jon Ashburn4b7f35f2015-08-03 09:08:46 -0600418 * Given string of three part form "maj.min.pat" convert to a vulkan version
419 * number.
420 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700421static uint32_t loader_make_version(const char *vers_str) {
422 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
423 char *minor_str = NULL;
Jon Ashburn4b7f35f2015-08-03 09:08:46 -0600424 char *patch_str = NULL;
425 char *cstr;
426 char *str;
427
428 if (!vers_str)
429 return vers;
430 cstr = loader_stack_alloc(strlen(vers_str) + 1);
431 strcpy(cstr, vers_str);
432 while ((str = strchr(cstr, '.')) != NULL) {
433 if (minor_str == NULL) {
434 minor_str = str + 1;
435 *str = '\0';
436 major = atoi(cstr);
Jon Ashburn44aed662016-02-02 17:47:28 -0700437 } else if (patch_str == NULL) {
Jon Ashburn4b7f35f2015-08-03 09:08:46 -0600438 patch_str = str + 1;
439 *str = '\0';
440 minor = atoi(minor_str);
Jon Ashburn44aed662016-02-02 17:47:28 -0700441 } else {
Jon Ashburn4b7f35f2015-08-03 09:08:46 -0600442 return vers;
443 }
444 cstr = str + 1;
445 }
446 patch = atoi(patch_str);
447
448 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburn4b7f35f2015-08-03 09:08:46 -0600449}
450
Jon Ashburn44aed662016-02-02 17:47:28 -0700451bool compare_vk_extension_properties(const VkExtensionProperties *op1,
452 const VkExtensionProperties *op2) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800453 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600454}
455
Jon Ashburn1b111de2015-07-06 15:40:35 -0600456/**
Jon Ashburn60378412015-07-02 12:59:25 -0600457 * Search the given ext_array for an extension
458 * matching the given vk_ext_prop
459 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700460bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
461 const uint32_t count,
462 const VkExtensionProperties *ext_array) {
Jon Ashburn60378412015-07-02 12:59:25 -0600463 for (uint32_t i = 0; i < count; i++) {
464 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i]))
465 return true;
466 }
467 return false;
468}
469
Jon Ashburn1b111de2015-07-06 15:40:35 -0600470/**
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600471 * Search the given ext_list for an extension
472 * matching the given vk_ext_prop
473 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700474bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop,
475 const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600476 for (uint32_t i = 0; i < ext_list->count; i++) {
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600477 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop))
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600478 return true;
479 }
480 return false;
481}
482
Jon Ashburne1783102016-04-08 15:03:35 -0600483/**
484 * Search the given ext_list for a device extension matching the given ext_prop
485 */
Jon Ashburn9b2a8c92016-04-15 09:25:03 -0600486bool has_vk_dev_ext_property(
487 const VkExtensionProperties *ext_prop,
488 const struct loader_device_extension_list *ext_list) {
Jon Ashburne1783102016-04-08 15:03:35 -0600489 for (uint32_t i = 0; i < ext_list->count; i++) {
490 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop))
491 return true;
492 }
493 return false;
494}
495
Jon Ashburn0bf6a182015-07-16 17:19:31 -0600496static inline bool loader_is_layer_type_device(const enum layer_type type) {
497 if ((type & VK_LAYER_TYPE_DEVICE_EXPLICIT) ||
Jon Ashburn44aed662016-02-02 17:47:28 -0700498 (type & VK_LAYER_TYPE_DEVICE_IMPLICIT))
Jon Ashburn0bf6a182015-07-16 17:19:31 -0600499 return true;
500 return false;
501}
502
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600503/*
Jon Ashburn1b111de2015-07-06 15:40:35 -0600504 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600505 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700506static struct loader_layer_properties *
507loader_get_layer_property(const char *name,
508 const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600509 for (uint32_t i = 0; i < layer_list->count; i++) {
510 const VkLayerProperties *item = &layer_list->list[i].info;
511 if (strcmp(name, item->layerName) == 0)
512 return &layer_list->list[i];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600513 }
514 return NULL;
515}
516
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600517/**
518 * Get the next unused layer property in the list. Init the property to zero.
519 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700520static struct loader_layer_properties *
521loader_get_next_layer_property(const struct loader_instance *inst,
522 struct loader_layer_list *layer_list) {
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600523 if (layer_list->capacity == 0) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700524 layer_list->list =
525 loader_heap_alloc(inst, sizeof(struct loader_layer_properties) * 64,
526 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600527 if (layer_list->list == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700528 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
529 "Out of memory can't add any layer properties to list");
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600530 return NULL;
531 }
Jon Ashburn44aed662016-02-02 17:47:28 -0700532 memset(layer_list->list, 0,
533 sizeof(struct loader_layer_properties) * 64);
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600534 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
535 }
536
537 // ensure enough room to add an entry
Jon Ashburn44aed662016-02-02 17:47:28 -0700538 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
539 layer_list->capacity) {
540 layer_list->list = loader_heap_realloc(
541 inst, layer_list->list, layer_list->capacity,
542 layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600543 if (layer_list->list == NULL) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700544 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -0700545 "realloc failed for layer list");
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600546 }
547 layer_list->capacity *= 2;
548 }
549
550 layer_list->count++;
551 return &(layer_list->list[layer_list->count - 1]);
552}
553
554/**
555 * Remove all layer properties entrys from the list
556 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700557void loader_delete_layer_properties(const struct loader_instance *inst,
558 struct loader_layer_list *layer_list) {
Jon Ashburn738136f2015-12-10 18:17:34 -0700559 uint32_t i, j;
560 struct loader_device_extension_list *dev_ext_list;
Jon Ashburn182b8302015-08-11 14:49:54 -0600561 if (!layer_list)
562 return;
563
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600564 for (i = 0; i < layer_list->count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700565 loader_destroy_generic_list(
566 inst, (struct loader_generic_list *)&layer_list->list[i]
567 .instance_extension_list);
Jon Ashburn738136f2015-12-10 18:17:34 -0700568 dev_ext_list = &layer_list->list[i].device_extension_list;
Jon Ashburn44aed662016-02-02 17:47:28 -0700569 if (dev_ext_list->capacity > 0 &&
570 dev_ext_list->list->entrypoint_count > 0) {
571 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Jon Ashburn738136f2015-12-10 18:17:34 -0700572 loader_heap_free(inst, dev_ext_list->list->entrypoints[j]);
573 }
574 loader_heap_free(inst, dev_ext_list->list->entrypoints);
575 }
Jon Ashburn44aed662016-02-02 17:47:28 -0700576 loader_destroy_generic_list(inst,
577 (struct loader_generic_list *)dev_ext_list);
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600578 }
579 layer_list->count = 0;
580
Jon Ashburn182b8302015-08-11 14:49:54 -0600581 if (layer_list->capacity > 0) {
582 layer_list->capacity = 0;
Jon Ashburne58f1a32015-08-28 13:38:21 -0600583 loader_heap_free(inst, layer_list->list);
Jon Ashburn182b8302015-08-11 14:49:54 -0600584 }
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600585}
586
Courtney Goeltzenleuchter03c80a62015-12-02 14:00:19 -0700587static void loader_add_instance_extensions(
Jon Ashburn44aed662016-02-02 17:47:28 -0700588 const struct loader_instance *inst,
589 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props,
590 const char *lib_name, struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf843d2d2015-12-21 16:41:47 -0700591 uint32_t i, count = 0;
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600592 VkExtensionProperties *ext_props;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600593 VkResult res;
594
Courtney Goeltzenleuchteraa9faab2015-07-06 22:28:18 -0600595 if (!fp_get_props) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600596 /* No EnumerateInstanceExtensionProperties defined */
Courtney Goeltzenleuchteraa9faab2015-07-06 22:28:18 -0600597 return;
598 }
599
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600600 res = fp_get_props(NULL, &count, NULL);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600601 if (res != VK_SUCCESS) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -0700602 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -0700603 "Error getting Instance extension count from %s", lib_name);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600604 return;
605 }
Jon Ashburn60699262015-06-10 16:11:42 -0600606
Courtney Goeltzenleuchteraa9faab2015-07-06 22:28:18 -0600607 if (count == 0) {
608 /* No ExtensionProperties to report */
609 return;
610 }
611
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600612 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600613
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600614 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600615 if (res != VK_SUCCESS) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -0700616 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -0700617 "Error getting Instance extensions from %s", lib_name);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600618 return;
619 }
Tony Barbour426b9052015-06-24 16:06:58 -0600620
Jon Ashburneb2728b2015-04-10 14:33:07 -0600621 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter73a21d32015-07-12 13:20:05 -0600622 char spec_version[64];
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600623
Jon Ashburn9b2a8c92016-04-15 09:25:03 -0600624 bool ext_unsupported =
625 wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburnc666cff2016-03-25 12:49:35 -0600626 if (!ext_unsupported) {
627 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
628 VK_MAJOR(ext_props[i].specVersion),
629 VK_MINOR(ext_props[i].specVersion),
630 VK_PATCH(ext_props[i].specVersion));
631 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
632 "Instance Extension: %s (%s) version %s",
633 ext_props[i].extensionName, lib_name, spec_version);
634 loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
635 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600636 }
637
638 return;
639}
640
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700641/*
642 * Initialize ext_list with the physical device extensions.
643 * The extension properties are passed as inputs in count and ext_props.
644 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700645static VkResult
646loader_init_device_extensions(const struct loader_instance *inst,
647 struct loader_physical_device *phys_dev,
648 uint32_t count, VkExtensionProperties *ext_props,
649 struct loader_extension_list *ext_list) {
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700650 VkResult res;
651 uint32_t i;
652
Jon Ashburn44aed662016-02-02 17:47:28 -0700653 if (!loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn8f6be302015-12-10 08:51:10 -0700654 sizeof(VkExtensionProperties))) {
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700655 return VK_ERROR_OUT_OF_HOST_MEMORY;
656 }
657
658 for (i = 0; i < count; i++) {
659 char spec_version[64];
660
Jon Ashburn44aed662016-02-02 17:47:28 -0700661 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
662 VK_MAJOR(ext_props[i].specVersion),
663 VK_MINOR(ext_props[i].specVersion),
664 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700665 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -0700666 "Device Extension: %s (%s) version %s",
667 ext_props[i].extensionName,
668 phys_dev->this_icd->this_icd_lib->lib_name, spec_version);
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700669 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
670 if (res != VK_SUCCESS)
671 return res;
672 }
673
674 return VK_SUCCESS;
675}
676
Jon Ashburna9c4a572016-02-26 13:14:27 -0700677VkResult loader_add_device_extensions(const struct loader_instance *inst,
Jon Ashburn9b2a8c92016-04-15 09:25:03 -0600678 PFN_vkEnumerateDeviceExtensionProperties
679 fpEnumerateDeviceExtensionProperties,
Jon Ashburna9c4a572016-02-26 13:14:27 -0700680 VkPhysicalDevice physical_device,
681 const char *lib_name,
682 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600683 uint32_t i, count;
684 VkResult res;
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600685 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600686
Piers Daniellfc500412016-03-29 11:51:11 -0600687 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count,
688 NULL);
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700689 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700690 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700691 if (!ext_props)
692 return VK_ERROR_OUT_OF_HOST_MEMORY;
Piers Daniellfc500412016-03-29 11:51:11 -0600693 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
694 &count, ext_props);
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700695 if (res != VK_SUCCESS)
Jon Ashburn251258f2015-11-01 14:04:06 -0700696 return res;
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700697 for (i = 0; i < count; i++) {
698 char spec_version[64];
699
Jon Ashburn44aed662016-02-02 17:47:28 -0700700 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
701 VK_MAJOR(ext_props[i].specVersion),
702 VK_MINOR(ext_props[i].specVersion),
703 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700704 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -0700705 "Device Extension: %s (%s) version %s",
706 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700707 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
708 if (res != VK_SUCCESS)
709 return res;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600710 }
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700711 } else {
Jon Ashburn44aed662016-02-02 17:47:28 -0700712 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
713 "Error getting physical device extension info count from "
714 "library %s",
715 lib_name);
Jon Ashburndb5a5bc2015-11-02 17:40:01 -0700716 return res;
717 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600718
Jon Ashburn251258f2015-11-01 14:04:06 -0700719 return VK_SUCCESS;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600720}
721
Jon Ashburna9c4a572016-02-26 13:14:27 -0700722bool loader_init_generic_list(const struct loader_instance *inst,
723 struct loader_generic_list *list_info,
724 size_t element_size) {
Jon Ashburn8f6be302015-12-10 08:51:10 -0700725 list_info->capacity = 32 * element_size;
Jon Ashburn44aed662016-02-02 17:47:28 -0700726 list_info->list = loader_heap_alloc(inst, list_info->capacity,
727 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn8f6be302015-12-10 08:51:10 -0700728 if (list_info->list == NULL) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600729 return false;
730 }
Jon Ashburn8f6be302015-12-10 08:51:10 -0700731 memset(list_info->list, 0, list_info->capacity);
732 list_info->count = 0;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600733 return true;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600734}
735
Jon Ashburn8f6be302015-12-10 08:51:10 -0700736void loader_destroy_generic_list(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -0700737 struct loader_generic_list *list) {
Jon Ashburn8f6be302015-12-10 08:51:10 -0700738 loader_heap_free(inst, list->list);
739 list->count = 0;
740 list->capacity = 0;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600741}
Jon Ashburneb2728b2015-04-10 14:33:07 -0600742
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600743/*
744 * Append non-duplicate extension properties defined in props
745 * to the given ext_list.
Jon Ashburn251258f2015-11-01 14:04:06 -0700746 * Return
747 * Vk_SUCCESS on success
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600748 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700749VkResult loader_add_to_ext_list(const struct loader_instance *inst,
750 struct loader_extension_list *ext_list,
751 uint32_t prop_list_count,
752 const VkExtensionProperties *props) {
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600753 uint32_t i;
754 const VkExtensionProperties *cur_ext;
755
756 if (ext_list->list == NULL || ext_list->capacity == 0) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700757 loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn8f6be302015-12-10 08:51:10 -0700758 sizeof(VkExtensionProperties));
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600759 }
760
761 if (ext_list->list == NULL)
Jon Ashburn251258f2015-11-01 14:04:06 -0700762 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600763
764 for (i = 0; i < prop_list_count; i++) {
765 cur_ext = &props[i];
766
767 // look for duplicates
768 if (has_vk_extension_property(cur_ext, ext_list)) {
769 continue;
770 }
771
772 // add to list at end
773 // check for enough capacity
Jon Ashburn44aed662016-02-02 17:47:28 -0700774 if (ext_list->count * sizeof(VkExtensionProperties) >=
775 ext_list->capacity) {
Jon Ashburne58f1a32015-08-28 13:38:21 -0600776
Jon Ashburn44aed662016-02-02 17:47:28 -0700777 ext_list->list = loader_heap_realloc(
778 inst, ext_list->list, ext_list->capacity,
779 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn251258f2015-11-01 14:04:06 -0700780
781 if (ext_list->list == NULL)
782 return VK_ERROR_OUT_OF_HOST_MEMORY;
783
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600784 // double capacity
785 ext_list->capacity *= 2;
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600786 }
787
Jon Ashburn44aed662016-02-02 17:47:28 -0700788 memcpy(&ext_list->list[ext_list->count], cur_ext,
789 sizeof(VkExtensionProperties));
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600790 ext_list->count++;
791 }
Jon Ashburn251258f2015-11-01 14:04:06 -0700792 return VK_SUCCESS;
Jon Ashburnc4748dc2015-08-04 11:14:18 -0600793}
794
Jon Ashburn738136f2015-12-10 18:17:34 -0700795/*
796 * Append one extension property defined in props with entrypoints
Jon Ashburne1783102016-04-08 15:03:35 -0600797 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn738136f2015-12-10 18:17:34 -0700798 * Return
799 * Vk_SUCCESS on success
800 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700801VkResult
802loader_add_to_dev_ext_list(const struct loader_instance *inst,
803 struct loader_device_extension_list *ext_list,
804 const VkExtensionProperties *props,
805 uint32_t entry_count, char **entrys) {
Jon Ashburn738136f2015-12-10 18:17:34 -0700806 uint32_t idx;
807 if (ext_list->list == NULL || ext_list->capacity == 0) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700808 loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn738136f2015-12-10 18:17:34 -0700809 sizeof(struct loader_dev_ext_props));
810 }
811
812 if (ext_list->list == NULL)
813 return VK_ERROR_OUT_OF_HOST_MEMORY;
814
Jon Ashburne1783102016-04-08 15:03:35 -0600815 // look for duplicates
816 if (has_vk_dev_ext_property(props, ext_list)) {
817 return VK_SUCCESS;
818 }
819
Jon Ashburn44aed662016-02-02 17:47:28 -0700820 idx = ext_list->count;
Jon Ashburn738136f2015-12-10 18:17:34 -0700821 // add to list at end
822 // check for enough capacity
Jon Ashburn44aed662016-02-02 17:47:28 -0700823 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn738136f2015-12-10 18:17:34 -0700824
Jon Ashburn44aed662016-02-02 17:47:28 -0700825 ext_list->list = loader_heap_realloc(
826 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
827 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn738136f2015-12-10 18:17:34 -0700828
829 if (ext_list->list == NULL)
830 return VK_ERROR_OUT_OF_HOST_MEMORY;
831
832 // double capacity
833 ext_list->capacity *= 2;
834 }
835
Jon Ashburn44aed662016-02-02 17:47:28 -0700836 memcpy(&ext_list->list[idx].props, props,
837 sizeof(struct loader_dev_ext_props));
Jon Ashburn738136f2015-12-10 18:17:34 -0700838 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn44aed662016-02-02 17:47:28 -0700839 ext_list->list[idx].entrypoints =
840 loader_heap_alloc(inst, sizeof(char *) * entry_count,
841 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn738136f2015-12-10 18:17:34 -0700842 if (ext_list->list[idx].entrypoints == NULL)
843 return VK_ERROR_OUT_OF_HOST_MEMORY;
844 for (uint32_t i = 0; i < entry_count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700845 ext_list->list[idx].entrypoints[i] = loader_heap_alloc(
846 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn738136f2015-12-10 18:17:34 -0700847 if (ext_list->list[idx].entrypoints[i] == NULL)
848 return VK_ERROR_OUT_OF_HOST_MEMORY;
849 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
850 }
851 ext_list->count++;
852
853 return VK_SUCCESS;
854}
855
Jon Ashburn60378412015-07-02 12:59:25 -0600856/**
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600857 * Search the given search_list for any layers in the props list.
Jon Ashburn44aed662016-02-02 17:47:28 -0700858 * Add these to the output layer_list. Don't add duplicates to the output
859 * layer_list.
Jon Ashburn60378412015-07-02 12:59:25 -0600860 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700861static VkResult
862loader_add_layer_names_to_list(const struct loader_instance *inst,
863 struct loader_layer_list *output_list,
864 uint32_t name_count, const char *const *names,
865 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600866 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -0600867 VkResult err = VK_SUCCESS;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600868
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600869 for (uint32_t i = 0; i < name_count; i++) {
870 const char *search_target = names[i];
Jon Ashburn8e4fab42015-08-03 17:19:30 -0600871 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600872 if (!layer_prop) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700873 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
874 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -0600875 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600876 continue;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600877 }
878
Jon Ashburne58f1a32015-08-28 13:38:21 -0600879 loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600880 }
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -0600881
882 return err;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600883}
884
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -0600885/*
886 * Manage lists of VkLayerProperties
887 */
Jon Ashburne58f1a32015-08-28 13:38:21 -0600888static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -0700889 struct loader_layer_list *list) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600890 list->capacity = 32 * sizeof(struct loader_layer_properties);
Jon Ashburn44aed662016-02-02 17:47:28 -0700891 list->list = loader_heap_alloc(inst, list->capacity,
892 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600893 if (list->list == NULL) {
894 return false;
895 }
896 memset(list->list, 0, list->capacity);
897 list->count = 0;
898 return true;
899}
900
Jon Ashburne58f1a32015-08-28 13:38:21 -0600901void loader_destroy_layer_list(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -0700902 struct loader_layer_list *layer_list) {
Jon Ashburne58f1a32015-08-28 13:38:21 -0600903 loader_heap_free(inst, layer_list->list);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600904 layer_list->count = 0;
905 layer_list->capacity = 0;
906}
907
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600908/*
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600909 * Manage list of layer libraries (loader_lib_info)
910 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700911static bool
912loader_init_layer_library_list(const struct loader_instance *inst,
913 struct loader_layer_library_list *list) {
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600914 list->capacity = 32 * sizeof(struct loader_lib_info);
Jon Ashburn44aed662016-02-02 17:47:28 -0700915 list->list = loader_heap_alloc(inst, list->capacity,
916 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600917 if (list->list == NULL) {
918 return false;
919 }
920 memset(list->list, 0, list->capacity);
921 list->count = 0;
922 return true;
923}
924
Jon Ashburne58f1a32015-08-28 13:38:21 -0600925void loader_destroy_layer_library_list(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -0700926 struct loader_layer_library_list *list) {
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600927 for (uint32_t i = 0; i < list->count; i++) {
Jon Ashburne58f1a32015-08-28 13:38:21 -0600928 loader_heap_free(inst, list->list[i].lib_name);
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600929 }
Jon Ashburne58f1a32015-08-28 13:38:21 -0600930 loader_heap_free(inst, list->list);
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600931 list->count = 0;
932 list->capacity = 0;
933}
934
Jon Ashburn44aed662016-02-02 17:47:28 -0700935void loader_add_to_layer_library_list(const struct loader_instance *inst,
936 struct loader_layer_library_list *list,
937 uint32_t item_count,
938 const struct loader_lib_info *new_items) {
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600939 uint32_t i;
940 struct loader_lib_info *item;
941
942 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne58f1a32015-08-28 13:38:21 -0600943 loader_init_layer_library_list(inst, list);
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600944 }
945
946 if (list->list == NULL)
947 return;
948
949 for (i = 0; i < item_count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -0700950 item = (struct loader_lib_info *)&new_items[i];
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600951
952 // look for duplicates
953 for (uint32_t j = 0; j < list->count; j++) {
954 if (strcmp(list->list[i].lib_name, new_items->lib_name) == 0) {
955 continue;
956 }
957 }
958
959 // add to list at end
960 // check for enough capacity
Jon Ashburn44aed662016-02-02 17:47:28 -0700961 if (list->count * sizeof(struct loader_lib_info) >= list->capacity) {
Jon Ashburne58f1a32015-08-28 13:38:21 -0600962
Jon Ashburn44aed662016-02-02 17:47:28 -0700963 list->list = loader_heap_realloc(
964 inst, list->list, list->capacity, list->capacity * 2,
965 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600966 // double capacity
967 list->capacity *= 2;
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600968 }
969
970 memcpy(&list->list[list->count], item, sizeof(struct loader_lib_info));
971 list->count++;
972 }
973}
974
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -0600975/*
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600976 * Search the given layer list for a list
977 * matching the given VkLayerProperties
978 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700979bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
980 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600981 for (uint32_t i = 0; i < list->count; i++) {
982 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
983 return true;
984 }
985 return false;
986}
987
988/*
989 * Search the given layer list for a layer
990 * matching the given name
991 */
Jon Ashburn44aed662016-02-02 17:47:28 -0700992bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600993 for (uint32_t i = 0; i < list->count; i++) {
994 if (strcmp(name, list->list[i].info.layerName) == 0)
995 return true;
996 }
997 return false;
998}
999
1000/*
1001 * Append non-duplicate layer properties defined in prop_list
1002 * to the given layer_info list
1003 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001004void loader_add_to_layer_list(const struct loader_instance *inst,
1005 struct loader_layer_list *list,
1006 uint32_t prop_list_count,
1007 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001008 uint32_t i;
1009 struct loader_layer_properties *layer;
1010
1011 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne58f1a32015-08-28 13:38:21 -06001012 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001013 }
1014
1015 if (list->list == NULL)
1016 return;
1017
1018 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001019 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001020
1021 // look for duplicates
1022 if (has_vk_layer_property(&layer->info, list)) {
1023 continue;
1024 }
1025
1026 // add to list at end
1027 // check for enough capacity
Jon Ashburn44aed662016-02-02 17:47:28 -07001028 if (list->count * sizeof(struct loader_layer_properties) >=
1029 list->capacity) {
Jon Ashburne58f1a32015-08-28 13:38:21 -06001030
Jon Ashburn44aed662016-02-02 17:47:28 -07001031 list->list = loader_heap_realloc(
1032 inst, list->list, list->capacity, list->capacity * 2,
1033 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001034 // double capacity
1035 list->capacity *= 2;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001036 }
1037
Jon Ashburn44aed662016-02-02 17:47:28 -07001038 memcpy(&list->list[list->count], layer,
1039 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001040 list->count++;
1041 }
1042}
1043
Jon Ashburnf2ddb732015-07-07 10:27:45 -06001044/**
1045 * Search the search_list for any layer with a name
1046 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001047 * Add all matching layers to the found_list
Jon Ashburnf2ddb732015-07-07 10:27:45 -06001048 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001049 * on the found_list.
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001050 */
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001051void loader_find_layer_name_add_list(
1052 const struct loader_instance *inst, const char *name,
1053 const enum layer_type type, const struct loader_layer_list *search_list,
1054 struct loader_layer_list *found_list) {
Jon Ashburn87c8fe92015-10-05 09:03:21 -06001055 bool found = false;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001056 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001057 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnf2ddb732015-07-07 10:27:45 -06001058 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn44aed662016-02-02 17:47:28 -07001059 (layer_prop->type & type)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001060 /* Found a layer with the same name, add to found_list */
Jon Ashburne58f1a32015-08-28 13:38:21 -06001061 loader_add_to_layer_list(inst, found_list, 1, layer_prop);
Jon Ashburn87c8fe92015-10-05 09:03:21 -06001062 found = true;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001063 }
1064 }
Jon Ashburn87c8fe92015-10-05 09:03:21 -06001065 if (!found) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001066 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001067 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn87c8fe92015-10-05 09:03:21 -06001068 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001069}
1070
Jon Ashburn44aed662016-02-02 17:47:28 -07001071static VkExtensionProperties *
1072get_extension_property(const char *name,
1073 const struct loader_extension_list *list) {
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06001074 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08001075 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06001076 return &list->list[i];
Jon Ashburn42e41032015-04-14 09:15:32 -06001077 }
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06001078 return NULL;
Jon Ashburn42e41032015-04-14 09:15:32 -06001079}
1080
Jon Ashburn44aed662016-02-02 17:47:28 -07001081static VkExtensionProperties *
1082get_dev_extension_property(const char *name,
1083 const struct loader_device_extension_list *list) {
Jon Ashburn738136f2015-12-10 18:17:34 -07001084 for (uint32_t i = 0; i < list->count; i++) {
1085 if (strcmp(name, list->list[i].props.extensionName) == 0)
1086 return &list->list[i].props;
1087 }
1088 return NULL;
1089}
1090
Jon Ashburn60378412015-07-02 12:59:25 -06001091/*
Courtney Goeltzenleuchterf5f244d2016-01-15 14:15:00 -07001092 * This function will return the pNext pointer of any
1093 * CreateInfo extensions that are not loader extensions.
1094 * This is used to skip past the loader extensions prepended
1095 * to the list during CreateInstance and CreateDevice.
1096 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001097void *loader_strip_create_extensions(const void *pNext) {
1098 VkLayerInstanceCreateInfo *create_info = (VkLayerInstanceCreateInfo *)pNext;
Courtney Goeltzenleuchterf5f244d2016-01-15 14:15:00 -07001099
Jon Ashburn44aed662016-02-02 17:47:28 -07001100 while (
1101 create_info &&
1102 (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
1103 create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
1104 create_info = (VkLayerInstanceCreateInfo *)create_info->pNext;
Courtney Goeltzenleuchterf5f244d2016-01-15 14:15:00 -07001105 }
1106
1107 return create_info;
1108}
1109
1110/*
Courtney Goeltzenleuchter03c80a62015-12-02 14:00:19 -07001111 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburn60378412015-07-02 12:59:25 -06001112 * the extension must provide two entry points for the loader to use:
1113 * - "trampoline" entry point - this is the address returned by GetProcAddr
1114 * and will always do what's necessary to support a global call.
1115 * - "terminator" function - this function will be put at the end of the
Jon Ashburn2818eb72015-11-30 17:21:25 -07001116 * instance chain and will contain the necessary logic to call / process
Jon Ashburn60378412015-07-02 12:59:25 -06001117 * the extension for the appropriate ICDs that are available.
1118 * There is no generic mechanism for including these functions, the references
1119 * must be placed into the appropriate loader entry points.
Jon Ashburn44aed662016-02-02 17:47:28 -07001120 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1121 * GetProcAddr requests
1122 * loader_coalesce_extensions(void) - add extension records to the list of
1123 * global
Jon Ashburn60378412015-07-02 12:59:25 -06001124 * extension available to the app.
1125 * instance_disp - add function pointer for terminator function to this array.
1126 * The extension itself should be in a separate file that will be
1127 * linked directly with the loader.
1128 */
Jon Ashburnc1d1eb72015-08-14 11:57:54 -06001129
Jon Ashburn754f1992015-08-18 18:04:47 -06001130void loader_get_icd_loader_instance_extensions(
Jon Ashburn44aed662016-02-02 17:47:28 -07001131 const struct loader_instance *inst, struct loader_icd_libs *icd_libs,
1132 struct loader_extension_list *inst_exts) {
Jon Ashburn85f98072015-08-14 14:49:22 -06001133 struct loader_extension_list icd_exts;
Jon Ashburn44aed662016-02-02 17:47:28 -07001134 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1135 "Build ICD instance extension list");
Jon Ashburneb2728b2015-04-10 14:33:07 -06001136 // traverse scanned icd list adding non-duplicate extensions to the list
Jon Ashburn754f1992015-08-18 18:04:47 -06001137 for (uint32_t i = 0; i < icd_libs->count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001138 loader_init_generic_list(inst, (struct loader_generic_list *)&icd_exts,
Jon Ashburn8f6be302015-12-10 08:51:10 -07001139 sizeof(VkExtensionProperties));
Jon Ashburn44aed662016-02-02 17:47:28 -07001140 loader_add_instance_extensions(
1141 inst, icd_libs->list[i].EnumerateInstanceExtensionProperties,
1142 icd_libs->list[i].lib_name, &icd_exts);
1143 loader_add_to_ext_list(inst, inst_exts, icd_exts.count, icd_exts.list);
1144 loader_destroy_generic_list(inst,
1145 (struct loader_generic_list *)&icd_exts);
Jon Ashburneb2728b2015-04-10 14:33:07 -06001146 };
1147
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001148 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne58f1a32015-08-28 13:38:21 -06001149 debug_report_add_instance_extensions(inst, inst_exts);
Jon Ashburneb2728b2015-04-10 14:33:07 -06001150}
1151
Jon Ashburne58f1a32015-08-28 13:38:21 -06001152struct loader_icd *loader_get_icd_and_device(const VkDevice device,
Jon Ashburn44aed662016-02-02 17:47:28 -07001153 struct loader_device **found_dev) {
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001154 *found_dev = NULL;
Jon Ashburn44aed662016-02-02 17:47:28 -07001155 for (struct loader_instance *inst = loader.instances; inst;
1156 inst = inst->next) {
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001157 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001158 for (struct loader_device *dev = icd->logical_device_list; dev;
1159 dev = dev->next)
1160 /* Value comparison of device prevents object wrapping by layers
1161 */
1162 if (loader_get_dispatch(dev->device) ==
1163 loader_get_dispatch(device)) {
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001164 *found_dev = dev;
1165 return icd;
1166 }
1167 }
1168 }
1169 return NULL;
1170}
1171
Jon Ashburne58f1a32015-08-28 13:38:21 -06001172static void loader_destroy_logical_device(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07001173 struct loader_device *dev) {
Jon Ashburne58f1a32015-08-28 13:38:21 -06001174 loader_heap_free(inst, dev->app_extension_props);
BogDan Vatrababfa702016-01-04 14:05:49 +02001175 loader_destroy_layer_list(inst, &dev->activated_layer_list);
Jon Ashburne58f1a32015-08-28 13:38:21 -06001176 loader_heap_free(inst, dev);
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001177}
1178
Jon Ashburna9c4a572016-02-26 13:14:27 -07001179struct loader_device *
Piers Daniellfc500412016-03-29 11:51:11 -06001180loader_create_logical_device(const struct loader_instance *inst) {
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001181 struct loader_device *new_dev;
1182
Jon Ashburn44aed662016-02-02 17:47:28 -07001183 new_dev = loader_heap_alloc(inst, sizeof(struct loader_device),
1184 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001185 if (!new_dev) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001186 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1187 "Failed to alloc struct loader-device");
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001188 return NULL;
1189 }
1190
1191 memset(new_dev, 0, sizeof(struct loader_device));
1192
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001193 return new_dev;
1194}
1195
Piers Daniellfc500412016-03-29 11:51:11 -06001196void loader_add_logical_device(const struct loader_instance *inst,
1197 struct loader_icd *icd,
1198 struct loader_device *dev) {
1199 dev->next = icd->logical_device_list;
1200 icd->logical_device_list = dev;
1201}
1202
Jon Ashburn44aed662016-02-02 17:47:28 -07001203void loader_remove_logical_device(const struct loader_instance *inst,
1204 struct loader_icd *icd,
1205 struct loader_device *found_dev) {
Jon Ashburndbf8cee2015-11-19 15:43:26 -07001206 struct loader_device *dev, *prev_dev;
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001207
1208 if (!icd || !found_dev)
1209 return;
1210
1211 prev_dev = NULL;
1212 dev = icd->logical_device_list;
1213 while (dev && dev != found_dev) {
1214 prev_dev = dev;
1215 dev = dev->next;
1216 }
1217
1218 if (prev_dev)
1219 prev_dev->next = found_dev->next;
1220 else
1221 icd->logical_device_list = found_dev->next;
Jon Ashburne58f1a32015-08-28 13:38:21 -06001222 loader_destroy_logical_device(inst, found_dev);
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001223}
1224
Jon Ashburn44aed662016-02-02 17:47:28 -07001225static void loader_icd_destroy(struct loader_instance *ptr_inst,
1226 struct loader_icd *icd) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001227 ptr_inst->total_icd_count--;
Jon Ashburn44aed662016-02-02 17:47:28 -07001228 for (struct loader_device *dev = icd->logical_device_list; dev;) {
Courtney Goeltzenleuchter6b70e362015-06-14 19:57:15 -06001229 struct loader_device *next_dev = dev->next;
Jon Ashburne58f1a32015-08-28 13:38:21 -06001230 loader_destroy_logical_device(ptr_inst, dev);
Courtney Goeltzenleuchter6b70e362015-06-14 19:57:15 -06001231 dev = next_dev;
1232 }
Jon Ashburncb5a5ac2015-06-10 10:06:06 -06001233
Jon Ashburne58f1a32015-08-28 13:38:21 -06001234 loader_heap_free(ptr_inst, icd);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001235}
1236
Jon Ashburn44aed662016-02-02 17:47:28 -07001237static struct loader_icd *
1238loader_icd_create(const struct loader_instance *inst) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001239 struct loader_icd *icd;
1240
Jon Ashburn44aed662016-02-02 17:47:28 -07001241 icd = loader_heap_alloc(inst, sizeof(*icd),
1242 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001243 if (!icd)
1244 return NULL;
1245
Courtney Goeltzenleuchter6f928162014-10-28 10:29:27 -06001246 memset(icd, 0, sizeof(*icd));
1247
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001248 return icd;
1249}
1250
Jon Ashburn44aed662016-02-02 17:47:28 -07001251static struct loader_icd *
1252loader_icd_add(struct loader_instance *ptr_inst,
1253 const struct loader_scanned_icds *icd_lib) {
Chia-I Wu894a1172014-08-04 11:18:20 +08001254 struct loader_icd *icd;
1255
Jon Ashburne58f1a32015-08-28 13:38:21 -06001256 icd = loader_icd_create(ptr_inst);
Chia-I Wu894a1172014-08-04 11:18:20 +08001257 if (!icd)
1258 return NULL;
1259
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06001260 icd->this_icd_lib = icd_lib;
1261 icd->this_instance = ptr_inst;
1262
Chia-I Wu894a1172014-08-04 11:18:20 +08001263 /* prepend to the list */
Jon Ashburn3336df82015-01-29 15:45:51 -07001264 icd->next = ptr_inst->icds;
1265 ptr_inst->icds = icd;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001266 ptr_inst->total_icd_count++;
Chia-I Wu894a1172014-08-04 11:18:20 +08001267
1268 return icd;
1269}
1270
Jon Ashburn44aed662016-02-02 17:47:28 -07001271void loader_scanned_icd_clear(const struct loader_instance *inst,
1272 struct loader_icd_libs *icd_libs) {
Jon Ashburn754f1992015-08-18 18:04:47 -06001273 if (icd_libs->capacity == 0)
1274 return;
1275 for (uint32_t i = 0; i < icd_libs->count; i++) {
1276 loader_platform_close_library(icd_libs->list[i].handle);
Jon Ashburne58f1a32015-08-28 13:38:21 -06001277 loader_heap_free(inst, icd_libs->list[i].lib_name);
Jon Ashburn754f1992015-08-18 18:04:47 -06001278 }
Jon Ashburne58f1a32015-08-28 13:38:21 -06001279 loader_heap_free(inst, icd_libs->list);
Jon Ashburn754f1992015-08-18 18:04:47 -06001280 icd_libs->capacity = 0;
1281 icd_libs->count = 0;
1282 icd_libs->list = NULL;
1283}
1284
Jon Ashburne58f1a32015-08-28 13:38:21 -06001285static void loader_scanned_icd_init(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07001286 struct loader_icd_libs *icd_libs) {
Jon Ashburne58f1a32015-08-28 13:38:21 -06001287 loader_scanned_icd_clear(inst, icd_libs);
Jon Ashburn754f1992015-08-18 18:04:47 -06001288 icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
Jon Ashburn44aed662016-02-02 17:47:28 -07001289 icd_libs->list = loader_heap_alloc(inst, icd_libs->capacity,
1290 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn754f1992015-08-18 18:04:47 -06001291}
1292
Jon Ashburn44aed662016-02-02 17:47:28 -07001293static void loader_scanned_icd_add(const struct loader_instance *inst,
1294 struct loader_icd_libs *icd_libs,
1295 const char *filename, uint32_t api_version) {
Ian Elliott81ac44c2015-01-13 17:52:38 -07001296 loader_platform_dl_handle handle;
Tony Barbourde4124d2015-07-03 10:33:54 -06001297 PFN_vkCreateInstance fp_create_inst;
Jon Ashburn6b689942016-01-07 09:44:27 -07001298 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001299 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn14275da2015-01-28 11:01:35 -07001300 struct loader_scanned_icds *new_node;
1301
Jon Ashburn754f1992015-08-18 18:04:47 -06001302 /* TODO implement ref counting of libraries, for now this function leaves
1303 libraries open and the scanned_icd_clear closes them */
Ian Elliott81ac44c2015-01-13 17:52:38 -07001304 // Used to call: dlopen(filename, RTLD_LAZY);
1305 handle = loader_platform_open_library(filename);
Jon Ashburn14275da2015-01-28 11:01:35 -07001306 if (!handle) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001307 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001308 loader_platform_open_library_error(filename));
Jon Ashburn14275da2015-01-28 11:01:35 -07001309 return;
1310 }
1311
Jon Ashburn44aed662016-02-02 17:47:28 -07001312 fp_get_proc_addr =
1313 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburn6b689942016-01-07 09:44:27 -07001314 if (!fp_get_proc_addr) {
Jon Ashburn3f19e052016-01-11 14:41:35 -07001315 // Use deprecated interface
Jon Ashburn44aed662016-02-02 17:47:28 -07001316 fp_get_proc_addr =
1317 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburn6b689942016-01-07 09:44:27 -07001318 if (!fp_get_proc_addr) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001319 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1320 loader_platform_get_proc_address_error(
1321 "vk_icdGetInstanceProcAddr"));
Jon Ashburn6b689942016-01-07 09:44:27 -07001322 return;
1323 } else {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001324 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001325 "Using deprecated ICD interface of "
1326 "vkGetInstanceProcAddr instead of "
1327 "vk_icdGetInstanceProcAddr");
Jon Ashburn6b689942016-01-07 09:44:27 -07001328 }
Jon Ashburn44aed662016-02-02 17:47:28 -07001329 fp_create_inst =
1330 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001331 if (!fp_create_inst) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001332 loader_log(
1333 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1334 "Couldn't get vkCreateInstance via dlsym/loadlibrary from ICD");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001335 return;
1336 }
Jon Ashburn44aed662016-02-02 17:47:28 -07001337 fp_get_inst_ext_props = loader_platform_get_proc_address(
1338 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001339 if (!fp_get_inst_ext_props) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001340 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1341 "Couldn't get vkEnumerateInstanceExtensionProperties "
1342 "via dlsym/loadlibrary from ICD");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001343 return;
1344 }
Jon Ashburn44aed662016-02-02 17:47:28 -07001345 } else {
Jon Ashburn3f19e052016-01-11 14:41:35 -07001346 // Use newer interface
Jon Ashburn44aed662016-02-02 17:47:28 -07001347 fp_create_inst =
1348 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001349 if (!fp_create_inst) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001350 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1351 "Couldn't get vkCreateInstance via "
1352 "vk_icdGetInstanceProcAddr from ICD");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001353 return;
1354 }
Jon Ashburn44aed662016-02-02 17:47:28 -07001355 fp_get_inst_ext_props =
1356 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1357 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001358 if (!fp_get_inst_ext_props) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001359 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1360 "Couldn't get vkEnumerateInstanceExtensionProperties "
1361 "via vk_icdGetInstanceProcAddr from ICD");
Jon Ashburn3f19e052016-01-11 14:41:35 -07001362 return;
1363 }
Jon Ashburn6b689942016-01-07 09:44:27 -07001364 }
Jon Ashburn14275da2015-01-28 11:01:35 -07001365
Jon Ashburn754f1992015-08-18 18:04:47 -06001366 // check for enough capacity
Jon Ashburn44aed662016-02-02 17:47:28 -07001367 if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
1368 icd_libs->capacity) {
Jon Ashburne58f1a32015-08-28 13:38:21 -06001369
Jon Ashburn44aed662016-02-02 17:47:28 -07001370 icd_libs->list = loader_heap_realloc(
1371 inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
1372 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1373 // double capacity
1374 icd_libs->capacity *= 2;
Jon Ashburn14275da2015-01-28 11:01:35 -07001375 }
Jon Ashburn754f1992015-08-18 18:04:47 -06001376 new_node = &(icd_libs->list[icd_libs->count]);
Jon Ashburn14275da2015-01-28 11:01:35 -07001377
1378 new_node->handle = handle;
Jon Ashburn0083b222015-11-17 17:35:40 -07001379 new_node->api_version = api_version;
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001380 new_node->GetInstanceProcAddr = fp_get_proc_addr;
Jon Ashburn6b689942016-01-07 09:44:27 -07001381 new_node->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Jon Ashburn3336df82015-01-29 15:45:51 -07001382 new_node->CreateInstance = fp_create_inst;
Jon Ashburn14275da2015-01-28 11:01:35 -07001383
Jon Ashburn44aed662016-02-02 17:47:28 -07001384 new_node->lib_name = (char *)loader_heap_alloc(
1385 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001386 if (!new_node->lib_name) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001387 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001388 "Out of memory can't add icd");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001389 return;
1390 }
1391 strcpy(new_node->lib_name, filename);
Jon Ashburn754f1992015-08-18 18:04:47 -06001392 icd_libs->count++;
Jon Ashburneb2728b2015-04-10 14:33:07 -06001393}
Ian Elliott81ac44c2015-01-13 17:52:38 -07001394
Jon Ashburn44aed662016-02-02 17:47:28 -07001395static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
1396 const PFN_vkGetInstanceProcAddr fp_gipa) {
1397/* initialize entrypoint function pointers */
Jon Ashburn0dd356d2015-05-14 12:43:38 -06001398
Jon Ashburn44aed662016-02-02 17:47:28 -07001399#define LOOKUP_GIPA(func, required) \
1400 do { \
1401 icd->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1402 if (!icd->func && required) { \
1403 loader_log((struct loader_instance *)inst, \
Jon Ashburna9c4a572016-02-26 13:14:27 -07001404 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn44aed662016-02-02 17:47:28 -07001405 loader_platform_get_proc_address_error("vk" #func)); \
1406 return false; \
1407 } \
Jon Ashburn0dd356d2015-05-14 12:43:38 -06001408 } while (0)
1409
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001410 LOOKUP_GIPA(GetDeviceProcAddr, true);
1411 LOOKUP_GIPA(DestroyInstance, true);
1412 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1413 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1414 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn4e189562015-07-23 18:49:07 -06001415 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001416 LOOKUP_GIPA(CreateDevice, true);
1417 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1418 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropef72e2a2015-08-03 17:04:53 -06001419 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001420 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001421 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001422 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1423 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott338dedb2015-08-21 15:09:33 -06001424 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott8cda1802015-11-19 16:05:09 -07001425 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1426 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1427 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas5ba5d5d2016-04-14 12:50:42 +01001428 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1429 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1430 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1431 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1432 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1433 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1434 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1435 LOOKUP_GIPA(DestroySurfaceKHR, false);
Ian Elliott4e309e92015-11-24 15:39:10 -07001436#ifdef VK_USE_PLATFORM_WIN32_KHR
1437 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1438#endif
1439#ifdef VK_USE_PLATFORM_XCB_KHR
1440 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1441#endif
Karl Schultzb7cfe362016-03-08 07:55:27 -07001442#ifdef VK_USE_PLATFORM_XLIB_KHR
1443 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1444#endif
Jason Ekstrandcd0672c2016-02-12 17:25:03 -08001445#ifdef VK_USE_PLATFORM_WAYLAND_KHR
1446 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1447#endif
Jon Ashburn0dd356d2015-05-14 12:43:38 -06001448
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001449#undef LOOKUP_GIPA
Ian Elliott1d73e662015-07-06 14:36:13 -06001450
Jon Ashburnce7e3d92015-07-16 10:17:29 -06001451 return true;
Jon Ashburn0dd356d2015-05-14 12:43:38 -06001452}
1453
Jon Ashburn44aed662016-02-02 17:47:28 -07001454static void loader_debug_init(void) {
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07001455 const char *env, *orig;
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001456
1457 if (g_loader_debug > 0)
1458 return;
1459
1460 g_loader_debug = 0;
1461
1462 /* parse comma-separated debug options */
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07001463 orig = env = loader_getenv("VK_LOADER_DEBUG");
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001464 while (env) {
1465 const char *p = strchr(env, ',');
1466 size_t len;
1467
1468 if (p)
1469 len = p - env;
1470 else
1471 len = strlen(env);
1472
1473 if (len > 0) {
Michael Worcesterc21f5ad2015-12-10 18:06:24 +00001474 if (strncmp(env, "all", len) == 0) {
1475 g_loader_debug = ~0u;
1476 g_loader_log_msgs = ~0u;
1477 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001478 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001479 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001480 } else if (strncmp(env, "info", len) == 0) {
1481 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001482 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001483 } else if (strncmp(env, "perf", len) == 0) {
1484 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburna9c4a572016-02-26 13:14:27 -07001485 g_loader_log_msgs |=
1486 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001487 } else if (strncmp(env, "error", len) == 0) {
1488 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001489 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001490 } else if (strncmp(env, "debug", len) == 0) {
1491 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001492 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001493 }
1494 }
1495
1496 if (!p)
1497 break;
1498
1499 env = p + 1;
1500 }
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07001501
1502 loader_free_getenv(orig);
Courtney Goeltzenleuchter8b253f92015-06-08 15:11:18 -06001503}
1504
Jon Ashburn44aed662016-02-02 17:47:28 -07001505void loader_initialize(void) {
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06001506 // initialize mutexs
Jon Ashburn754f1992015-08-18 18:04:47 -06001507 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06001508 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn754f1992015-08-18 18:04:47 -06001509
1510 // initialize logging
1511 loader_debug_init();
Jon Ashburn413d6582015-08-28 15:19:27 -06001512
1513 // initial cJSON to use alloc callbacks
1514 cJSON_Hooks alloc_fns = {
Jon Ashburn44aed662016-02-02 17:47:28 -07001515 .malloc_fn = loader_tls_heap_alloc, .free_fn = loader_tls_heap_free,
Jon Ashburn413d6582015-08-28 15:19:27 -06001516 };
1517 cJSON_InitHooks(&alloc_fns);
Jon Ashburn754f1992015-08-18 18:04:47 -06001518}
1519
Jon Ashburnffd5d672015-06-29 11:25:34 -06001520struct loader_manifest_files {
1521 uint32_t count;
1522 char **filename_list;
1523};
1524
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001525/**
Jon Ashburnffd5d672015-06-29 11:25:34 -06001526 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001527 *
1528 * \returns
Jon Ashburnffd5d672015-06-29 11:25:34 -06001529 * A pointer to first char in the next path.
1530 * The next path (or NULL) in the list is returned in next_path.
1531 * Note: input string is modified in some cases. PASS IN A COPY!
1532 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001533static char *loader_get_next_path(char *path) {
Jon Ashburnffd5d672015-06-29 11:25:34 -06001534 uint32_t len;
1535 char *next;
1536
1537 if (path == NULL)
1538 return NULL;
1539 next = strchr(path, PATH_SEPERATOR);
1540 if (next == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001541 len = (uint32_t)strlen(path);
Jon Ashburnffd5d672015-06-29 11:25:34 -06001542 next = path + len;
Jon Ashburn44aed662016-02-02 17:47:28 -07001543 } else {
Jon Ashburnffd5d672015-06-29 11:25:34 -06001544 *next = '\0';
1545 next++;
1546 }
1547
1548 return next;
1549}
1550
1551/**
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001552 * Given a path which is absolute or relative, expand the path if relative or
1553 * leave the path unmodified if absolute. The base path to prepend to relative
1554 * paths is given in rel_base.
Jon Ashburn38144502015-07-07 15:06:25 -06001555 *
1556 * \returns
1557 * A string in out_fullpath of the full absolute path
Jon Ashburn38144502015-07-07 15:06:25 -06001558 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001559static void loader_expand_path(const char *path, const char *rel_base,
1560 size_t out_size, char *out_fullpath) {
Jon Ashburn38144502015-07-07 15:06:25 -06001561 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001562 // do not prepend a base to an absolute path
1563 rel_base = "";
Jon Ashburn38144502015-07-07 15:06:25 -06001564 }
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001565
1566 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn38144502015-07-07 15:06:25 -06001567}
1568
1569/**
Jon Ashburnffd5d672015-06-29 11:25:34 -06001570 * Given a filename (file) and a list of paths (dir), try to find an existing
1571 * file in the paths. If filename already is a path then no
1572 * searching in the given paths.
1573 *
1574 * \returns
1575 * A string in out_fullpath of either the full path or file.
Jon Ashburnffd5d672015-06-29 11:25:34 -06001576 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001577static void loader_get_fullpath(const char *file, const char *dirs,
1578 size_t out_size, char *out_fullpath) {
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001579 if (!loader_platform_is_path(file) && *dirs) {
1580 char *dirs_copy, *dir, *next_dir;
1581
1582 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1583 strcpy(dirs_copy, dirs);
1584
Jon Ashburn44aed662016-02-02 17:47:28 -07001585 // find if file exists after prepending paths in given list
1586 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001587 dir = next_dir) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001588 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1589 NULL);
Jon Ashburnffd5d672015-06-29 11:25:34 -06001590 if (loader_platform_file_exists(out_fullpath)) {
1591 return;
1592 }
Jon Ashburnffd5d672015-06-29 11:25:34 -06001593 }
1594 }
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001595
Jon Ashburnffd5d672015-06-29 11:25:34 -06001596 snprintf(out_fullpath, out_size, "%s", file);
1597}
1598
1599/**
1600 * Read a JSON file into a buffer.
1601 *
1602 * \returns
1603 * A pointer to a cJSON object representing the JSON parse tree.
1604 * This returned buffer should be freed by caller.
1605 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001606static cJSON *loader_get_json(const struct loader_instance *inst,
1607 const char *filename) {
Jon Ashburnffd5d672015-06-29 11:25:34 -06001608 FILE *file;
1609 char *json_buf;
1610 cJSON *json;
Mark Young2acdd152016-01-13 13:47:16 -07001611 size_t len;
Jon Ashburn44aed662016-02-02 17:47:28 -07001612 file = fopen(filename, "rb");
Jon Ashburna01fb3e2015-08-27 08:30:50 -06001613 if (!file) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001614 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1615 "Couldn't open JSON file %s", filename);
Jon Ashburna01fb3e2015-08-27 08:30:50 -06001616 return NULL;
1617 }
Jon Ashburnffd5d672015-06-29 11:25:34 -06001618 fseek(file, 0, SEEK_END);
1619 len = ftell(file);
1620 fseek(file, 0, SEEK_SET);
Jon Ashburn44aed662016-02-02 17:47:28 -07001621 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburnffd5d672015-06-29 11:25:34 -06001622 if (json_buf == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001623 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1624 "Out of memory can't get JSON file");
Jon Ashburnffd5d672015-06-29 11:25:34 -06001625 fclose(file);
1626 return NULL;
1627 }
1628 if (fread(json_buf, sizeof(char), len, file) != len) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001629 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1630 "fread failed can't get JSON file");
Jon Ashburnffd5d672015-06-29 11:25:34 -06001631 fclose(file);
1632 return NULL;
1633 }
1634 fclose(file);
1635 json_buf[len] = '\0';
1636
Jon Ashburn44aed662016-02-02 17:47:28 -07001637 // parse text from file
Jon Ashburnffd5d672015-06-29 11:25:34 -06001638 json = cJSON_Parse(json_buf);
1639 if (json == NULL)
Jon Ashburn44aed662016-02-02 17:47:28 -07001640 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1641 "Can't parse JSON file %s", filename);
Jon Ashburnffd5d672015-06-29 11:25:34 -06001642 return json;
1643}
1644
1645/**
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06001646 * Do a deep copy of the loader_layer_properties structure.
1647 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001648static void loader_copy_layer_properties(const struct loader_instance *inst,
1649 struct loader_layer_properties *dst,
1650 struct loader_layer_properties *src) {
Jon Ashburn738136f2015-12-10 18:17:34 -07001651 uint32_t cnt, i;
Jon Ashburn44aed662016-02-02 17:47:28 -07001652 memcpy(dst, src, sizeof(*src));
1653 dst->instance_extension_list.list =
1654 loader_heap_alloc(inst, sizeof(VkExtensionProperties) *
1655 src->instance_extension_list.count,
1656 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1657 dst->instance_extension_list.capacity =
1658 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne58f1a32015-08-28 13:38:21 -06001659 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn44aed662016-02-02 17:47:28 -07001660 dst->instance_extension_list.capacity);
1661 dst->device_extension_list.list =
1662 loader_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1663 src->device_extension_list.count,
1664 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn738136f2015-12-10 18:17:34 -07001665
Jon Ashburn44aed662016-02-02 17:47:28 -07001666 dst->device_extension_list.capacity =
1667 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne58f1a32015-08-28 13:38:21 -06001668 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn44aed662016-02-02 17:47:28 -07001669 dst->device_extension_list.capacity);
Jon Ashburn738136f2015-12-10 18:17:34 -07001670 if (src->device_extension_list.count > 0 &&
Jon Ashburn44aed662016-02-02 17:47:28 -07001671 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn738136f2015-12-10 18:17:34 -07001672 cnt = src->device_extension_list.list->entrypoint_count;
1673 dst->device_extension_list.list->entrypoints = loader_heap_alloc(
Jon Ashburn44aed662016-02-02 17:47:28 -07001674 inst, sizeof(char *) * cnt, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn738136f2015-12-10 18:17:34 -07001675 for (i = 0; i < cnt; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07001676 dst->device_extension_list.list->entrypoints[i] = loader_heap_alloc(
1677 inst,
1678 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1679 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn738136f2015-12-10 18:17:34 -07001680 strcpy(dst->device_extension_list.list->entrypoints[i],
1681 src->device_extension_list.list->entrypoints[i]);
1682 }
1683 }
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06001684}
1685
Jon Ashburn715de582016-02-10 20:59:26 -07001686static bool
1687loader_find_layer_name_list(const char *name,
1688 const struct loader_layer_list *layer_list) {
1689 if (!layer_list)
1690 return false;
1691 for (uint32_t j = 0; j < layer_list->count; j++)
1692 if (!strcmp(name, layer_list->list[j].info.layerName))
1693 return true;
1694 return false;
1695}
1696
1697static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1698 const char **layer_list) {
1699 if (!layer_list)
1700 return false;
1701 for (uint32_t j = 0; j < layer_count; j++)
1702 if (!strcmp(name, layer_list[j]))
1703 return true;
1704 return false;
1705}
1706
1707static bool loader_find_layer_name_array(
1708 const char *name, uint32_t layer_count,
1709 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1710 if (!layer_list)
1711 return false;
1712 for (uint32_t j = 0; j < layer_count; j++)
1713 if (!strcmp(name, layer_list[j]))
1714 return true;
1715 return false;
1716}
1717
1718/**
1719 * Searches through an array of layer names (ppp_layer_names) looking for a
1720 * layer key_name.
1721 * If not found then simply returns updating nothing.
1722 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes5ca7f062016-04-07 09:04:49 +12001723 * Any duplicate (pre-existing) expand_names in layer names are removed.
1724 * Order is otherwise preserved, with the layer key_name being replaced by the
1725 * expand_names.
Jon Ashburn715de582016-02-10 20:59:26 -07001726 * @param inst
1727 * @param layer_count
1728 * @param ppp_layer_names
1729 */
1730void loader_expand_layer_names(
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001731 const struct loader_instance *inst, const char *key_name,
Jon Ashburn715de582016-02-10 20:59:26 -07001732 uint32_t expand_count,
1733 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001734 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburna0eecb22016-02-11 18:59:43 -07001735
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001736 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn715de582016-02-10 20:59:26 -07001737
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001738 if (!loader_find_layer_name(key_name, *layer_count,
1739 (char const **)pp_src_layers))
Chris Forbeseef20d02016-04-06 20:49:02 +12001740 return; // didn't find the key_name in the list.
Jon Ashburna0eecb22016-02-11 18:59:43 -07001741
Jon Ashburn715de582016-02-10 20:59:26 -07001742 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
1743 "Found meta layer %s, replacing with actual layer group",
1744 key_name);
Chris Forbeseef20d02016-04-06 20:49:02 +12001745
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001746 char const **pp_dst_layers = loader_heap_alloc(
1747 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
1748 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Chris Forbeseef20d02016-04-06 20:49:02 +12001749
1750 // copy layers from src to dst, stripping key_name and anything in
1751 // expand_names.
1752 uint32_t src_index, dst_index = 0;
1753 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001754 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
1755 expand_names)) {
Chris Forbes5ca7f062016-04-07 09:04:49 +12001756 continue;
1757 }
1758
1759 if (!strcmp(pp_src_layers[src_index], key_name)) {
1760 // insert all expand_names in place of key_name
1761 uint32_t expand_index;
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06001762 for (expand_index = 0; expand_index < expand_count;
1763 expand_index++) {
Chris Forbes5ca7f062016-04-07 09:04:49 +12001764 pp_dst_layers[dst_index++] = expand_names[expand_index];
1765 }
Chris Forbeseef20d02016-04-06 20:49:02 +12001766 continue;
Jon Ashburn715de582016-02-10 20:59:26 -07001767 }
Chris Forbeseef20d02016-04-06 20:49:02 +12001768
1769 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn715de582016-02-10 20:59:26 -07001770 }
1771
Chris Forbeseef20d02016-04-06 20:49:02 +12001772 *ppp_layer_names = pp_dst_layers;
1773 *layer_count = dst_index;
Jon Ashburn715de582016-02-10 20:59:26 -07001774}
1775
Chris Forbeseef20d02016-04-06 20:49:02 +12001776void loader_delete_shadow_dev_layer_names(const struct loader_instance *inst,
1777 const VkDeviceCreateInfo *orig,
1778 VkDeviceCreateInfo *ours) {
1779 /* Free the layer names array iff we had to reallocate it */
1780 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
1781 loader_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn715de582016-02-10 20:59:26 -07001782 }
1783}
1784
Chris Forbeseef20d02016-04-06 20:49:02 +12001785void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
1786 const VkInstanceCreateInfo *orig,
1787 VkInstanceCreateInfo *ours) {
1788 /* Free the layer names array iff we had to reallocate it */
1789 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
1790 loader_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn715de582016-02-10 20:59:26 -07001791 }
1792}
1793
1794/**
1795 * Searches through the existing instance and device layer lists looking for
1796 * the set of required layer names. If found then it adds a meta property to the
1797 * layer list.
1798 * Assumes the required layers are the same for both instance and device lists.
1799 * @param inst
1800 * @param layer_count number of layers in layer_names
1801 * @param layer_names array of required layer names
1802 * @param layer_instance_list
1803 * @param layer_device_list
1804 */
1805static void loader_add_layer_property_meta(
1806 const struct loader_instance *inst, uint32_t layer_count,
1807 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
1808 struct loader_layer_list *layer_instance_list,
1809 struct loader_layer_list *layer_device_list) {
1810 uint32_t i, j;
1811 bool found;
1812 struct loader_layer_list *layer_list;
1813
Jon Ashburn9255c182016-02-19 15:22:10 -07001814 if (0 == layer_count || (!layer_instance_list && !layer_device_list))
1815 return;
1816 if ((layer_instance_list && (layer_count > layer_instance_list->count)) &&
1817 (layer_device_list && (layer_count > layer_device_list->count)))
Jon Ashburn715de582016-02-10 20:59:26 -07001818 return;
1819
1820 for (j = 0; j < 2; j++) {
1821 if (j == 0)
1822 layer_list = layer_instance_list;
1823 else
1824 layer_list = layer_device_list;
1825 found = true;
Jon Ashburn9255c182016-02-19 15:22:10 -07001826 if (layer_list == NULL)
1827 continue;
Jon Ashburn715de582016-02-10 20:59:26 -07001828 for (i = 0; i < layer_count; i++) {
1829 if (loader_find_layer_name_list(layer_names[i], layer_list))
1830 continue;
1831 found = false;
1832 break;
1833 }
1834
1835 struct loader_layer_properties *props;
1836 if (found) {
1837 props = loader_get_next_layer_property(inst, layer_list);
1838 props->type = VK_LAYER_TYPE_META_EXPLICT;
1839 strncpy(props->info.description, "LunarG Standard Validation Layer",
1840 sizeof(props->info.description));
1841 props->info.implementationVersion = 1;
1842 strncpy(props->info.layerName, std_validation_str,
1843 sizeof(props->info.layerName));
1844 // TODO what about specVersion? for now insert loader's built
1845 // version
Jon Ashburn310a2562016-04-07 11:57:03 -06001846 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
Jon Ashburn715de582016-02-10 20:59:26 -07001847 }
1848 }
1849}
1850
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06001851/**
Jon Ashburn1b111de2015-07-06 15:40:35 -06001852 * Given a cJSON struct (json) of the top level JSON object from layer manifest
1853 * file, add entry to the layer_list.
Jon Ashburn8e4fab42015-08-03 17:19:30 -06001854 * Fill out the layer_properties in this list entry from the input cJSON object.
Jon Ashburn1b111de2015-07-06 15:40:35 -06001855 *
1856 * \returns
1857 * void
1858 * layer_list has a new entry and initialized accordingly.
1859 * If the json input object does not have all the required fields no entry
1860 * is added to the list.
1861 */
Jon Ashburn44aed662016-02-02 17:47:28 -07001862static void
1863loader_add_layer_properties(const struct loader_instance *inst,
1864 struct loader_layer_list *layer_instance_list,
1865 struct loader_layer_list *layer_device_list,
1866 cJSON *json, bool is_implicit, char *filename) {
Jon Ashburn1b111de2015-07-06 15:40:35 -06001867 /* Fields in layer manifest file that are required:
1868 * (required) “file_format_version”
1869 * following are required in the "layer" object:
1870 * (required) "name"
1871 * (required) "type"
1872 * (required) “library_path”
Jon Ashburn44fe0062015-11-18 16:46:48 -07001873 * (required) “api_version”
Jon Ashburn1b111de2015-07-06 15:40:35 -06001874 * (required) “implementation_version”
1875 * (required) “description”
1876 * (required for implicit layers) “disable_environment”
1877 *
1878 * First get all required items and if any missing abort
1879 */
1880
1881 cJSON *item, *layer_node, *ext_item;
1882 char *temp;
Jon Ashburn44fe0062015-11-18 16:46:48 -07001883 char *name, *type, *library_path, *api_version;
Jon Ashburn1b111de2015-07-06 15:40:35 -06001884 char *implementation_version, *description;
Karl Schultzc50a5982016-02-24 14:39:39 -07001885 cJSON *disable_environment = NULL;
Jon Ashburn738136f2015-12-10 18:17:34 -07001886 int i, j;
Jon Ashburnc4748dc2015-08-04 11:14:18 -06001887 VkExtensionProperties ext_prop;
Jon Ashburn1b111de2015-07-06 15:40:35 -06001888 item = cJSON_GetObjectItem(json, "file_format_version");
1889 if (item == NULL) {
1890 return;
1891 }
1892 char *file_vers = cJSON_PrintUnformatted(item);
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001893 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001894 "Found manifest file %s, version %s", filename, file_vers);
Jon Ashburn44fe0062015-11-18 16:46:48 -07001895 if (strcmp(file_vers, "\"1.0.0\"") != 0)
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001896 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001897 "Unexpected manifest file version (expected 1.0.0), may "
1898 "cause errors");
Jon Ashburn413d6582015-08-28 15:19:27 -06001899 loader_tls_heap_free(file_vers);
Jon Ashburn1b111de2015-07-06 15:40:35 -06001900
Jon Ashburn1b111de2015-07-06 15:40:35 -06001901 layer_node = cJSON_GetObjectItem(json, "layer");
1902 if (layer_node == NULL) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07001903 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07001904 "Can't find \"layer\" object in manifest JSON file, "
1905 "skipping this file");
Jon Ashburn1b111de2015-07-06 15:40:35 -06001906 return;
1907 }
Jon Ashburn082569b2015-08-12 16:39:32 -06001908
1909 // loop through all "layer" objects in the file
1910 do {
Jon Ashburn44aed662016-02-02 17:47:28 -07001911#define GET_JSON_OBJECT(node, var) \
1912 { \
1913 var = cJSON_GetObjectItem(node, #var); \
1914 if (var == NULL) { \
1915 layer_node = layer_node->next; \
Jon Ashburna9c4a572016-02-26 13:14:27 -07001916 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn44aed662016-02-02 17:47:28 -07001917 "Didn't find required layer object %s in manifest " \
1918 "JSON file, skipping this layer", \
1919 #var); \
1920 continue; \
1921 } \
1922 }
1923#define GET_JSON_ITEM(node, var) \
1924 { \
1925 item = cJSON_GetObjectItem(node, #var); \
1926 if (item == NULL) { \
1927 layer_node = layer_node->next; \
Jon Ashburna9c4a572016-02-26 13:14:27 -07001928 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn44aed662016-02-02 17:47:28 -07001929 "Didn't find required layer value %s in manifest JSON " \
1930 "file, skipping this layer", \
1931 #var); \
1932 continue; \
1933 } \
1934 temp = cJSON_Print(item); \
1935 temp[strlen(temp) - 1] = '\0'; \
1936 var = loader_stack_alloc(strlen(temp) + 1); \
1937 strcpy(var, &temp[1]); \
1938 loader_tls_heap_free(temp); \
1939 }
Jon Ashburn082569b2015-08-12 16:39:32 -06001940 GET_JSON_ITEM(layer_node, name)
1941 GET_JSON_ITEM(layer_node, type)
1942 GET_JSON_ITEM(layer_node, library_path)
Jon Ashburn44fe0062015-11-18 16:46:48 -07001943 GET_JSON_ITEM(layer_node, api_version)
Jon Ashburn082569b2015-08-12 16:39:32 -06001944 GET_JSON_ITEM(layer_node, implementation_version)
1945 GET_JSON_ITEM(layer_node, description)
1946 if (is_implicit) {
1947 GET_JSON_OBJECT(layer_node, disable_environment)
1948 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06001949#undef GET_JSON_ITEM
1950#undef GET_JSON_OBJECT
1951
Jon Ashburn082569b2015-08-12 16:39:32 -06001952 // add list entry
Jon Ashburn44aed662016-02-02 17:47:28 -07001953 struct loader_layer_properties *props = NULL;
Jon Ashburn082569b2015-08-12 16:39:32 -06001954 if (!strcmp(type, "DEVICE")) {
1955 if (layer_device_list == NULL) {
1956 layer_node = layer_node->next;
1957 continue;
1958 }
Jon Ashburne58f1a32015-08-28 13:38:21 -06001959 props = loader_get_next_layer_property(inst, layer_device_list);
Jon Ashburn44aed662016-02-02 17:47:28 -07001960 props->type = (is_implicit) ? VK_LAYER_TYPE_DEVICE_IMPLICIT
1961 : VK_LAYER_TYPE_DEVICE_EXPLICIT;
Jon Ashburn082569b2015-08-12 16:39:32 -06001962 }
1963 if (!strcmp(type, "INSTANCE")) {
1964 if (layer_instance_list == NULL) {
1965 layer_node = layer_node->next;
1966 continue;
1967 }
Jon Ashburne58f1a32015-08-28 13:38:21 -06001968 props = loader_get_next_layer_property(inst, layer_instance_list);
Jon Ashburn44aed662016-02-02 17:47:28 -07001969 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
1970 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
Jon Ashburn082569b2015-08-12 16:39:32 -06001971 }
1972 if (!strcmp(type, "GLOBAL")) {
1973 if (layer_instance_list != NULL)
Jon Ashburn44aed662016-02-02 17:47:28 -07001974 props =
1975 loader_get_next_layer_property(inst, layer_instance_list);
Jon Ashburn082569b2015-08-12 16:39:32 -06001976 else if (layer_device_list != NULL)
Jon Ashburne58f1a32015-08-28 13:38:21 -06001977 props = loader_get_next_layer_property(inst, layer_device_list);
Jon Ashburn082569b2015-08-12 16:39:32 -06001978 else {
1979 layer_node = layer_node->next;
1980 continue;
1981 }
Jon Ashburn44aed662016-02-02 17:47:28 -07001982 props->type = (is_implicit) ? VK_LAYER_TYPE_GLOBAL_IMPLICIT
1983 : VK_LAYER_TYPE_GLOBAL_EXPLICIT;
Jon Ashburn082569b2015-08-12 16:39:32 -06001984 }
Jon Ashburn0bf6a182015-07-16 17:19:31 -06001985
Jon Ashburnb8605fe2015-09-18 12:53:16 -06001986 if (props == NULL) {
1987 layer_node = layer_node->next;
1988 continue;
1989 }
1990
Jon Ashburn44aed662016-02-02 17:47:28 -07001991 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
1992 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
Jon Ashburn38144502015-07-07 15:06:25 -06001993
Jon Ashburn082569b2015-08-12 16:39:32 -06001994 char *rel_base;
Jon Ashburnfc8f38d2015-12-01 17:11:02 -07001995 if (loader_platform_is_path(library_path)) {
Jon Ashburn082569b2015-08-12 16:39:32 -06001996 // a relative or absolute path
Daniel Dadap2e13fca2015-09-30 11:50:51 -05001997 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
Jon Ashburn082569b2015-08-12 16:39:32 -06001998 strcpy(name_copy, filename);
1999 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn44aed662016-02-02 17:47:28 -07002000 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE,
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002001 props->lib_name);
Daniel Dadap2e13fca2015-09-30 11:50:51 -05002002 } else {
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002003 // a filename which will be passed to the OSes library loader
2004 strncpy(props->lib_name, library_path, sizeof(props->lib_name));
2005 props->lib_name[sizeof(props->lib_name) - 1] = '\0';
Jon Ashburn082569b2015-08-12 16:39:32 -06002006 }
Jon Ashburn44fe0062015-11-18 16:46:48 -07002007 props->info.specVersion = loader_make_version(api_version);
2008 props->info.implementationVersion = atoi(implementation_version);
Jon Ashburn44aed662016-02-02 17:47:28 -07002009 strncpy((char *)props->info.description, description,
2010 sizeof(props->info.description));
2011 props->info.description[sizeof(props->info.description) - 1] = '\0';
Jon Ashburn082569b2015-08-12 16:39:32 -06002012 if (is_implicit) {
Jon Ashburn42afe582016-02-12 14:46:41 -07002013 if (!disable_environment || !disable_environment->child) {
Jon Ashburna9c4a572016-02-26 13:14:27 -07002014 loader_log(
2015 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2016 "Didn't find required layer child value disable_environment"
2017 "in manifest JSON file, skipping this layer");
Jon Ashburn42afe582016-02-12 14:46:41 -07002018 layer_node = layer_node->next;
2019 continue;
2020 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002021 strncpy(props->disable_env_var.name,
2022 disable_environment->child->string,
2023 sizeof(props->disable_env_var.name));
2024 props->disable_env_var
2025 .name[sizeof(props->disable_env_var.name) - 1] = '\0';
2026 strncpy(props->disable_env_var.value,
2027 disable_environment->child->valuestring,
2028 sizeof(props->disable_env_var.value));
2029 props->disable_env_var
2030 .value[sizeof(props->disable_env_var.value) - 1] = '\0';
Jon Ashburn082569b2015-08-12 16:39:32 -06002031 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002032
Jon Ashburn44aed662016-02-02 17:47:28 -07002033/**
2034 * Now get all optional items and objects and put in list:
2035 * functions
2036 * instance_extensions
2037 * device_extensions
2038 * enable_environment (implicit layers only)
2039 */
2040#define GET_JSON_OBJECT(node, var) \
2041 { var = cJSON_GetObjectItem(node, #var); }
2042#define GET_JSON_ITEM(node, var) \
2043 { \
2044 item = cJSON_GetObjectItem(node, #var); \
2045 if (item != NULL) { \
2046 temp = cJSON_Print(item); \
2047 temp[strlen(temp) - 1] = '\0'; \
2048 var = loader_stack_alloc(strlen(temp) + 1); \
2049 strcpy(var, &temp[1]); \
2050 loader_tls_heap_free(temp); \
2051 } \
2052 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002053
Jon Ashburn44aed662016-02-02 17:47:28 -07002054 cJSON *instance_extensions, *device_extensions, *functions,
2055 *enable_environment;
Jon Ashburn738136f2015-12-10 18:17:34 -07002056 cJSON *entrypoints;
2057 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2058 char **entry_array;
2059 vkGetInstanceProcAddr = NULL;
2060 vkGetDeviceProcAddr = NULL;
2061 spec_version = NULL;
2062 entrypoints = NULL;
2063 entry_array = NULL;
2064 /**
2065 * functions
2066 * vkGetInstanceProcAddr
2067 * vkGetDeviceProcAddr
2068 */
Jon Ashburn082569b2015-08-12 16:39:32 -06002069 GET_JSON_OBJECT(layer_node, functions)
2070 if (functions != NULL) {
2071 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2072 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
Jon Ashburn2db08042015-09-09 11:29:24 -06002073 if (vkGetInstanceProcAddr != NULL)
Jon Ashburn44aed662016-02-02 17:47:28 -07002074 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2075 sizeof(props->functions.str_gipa));
2076 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] =
2077 '\0';
Jon Ashburn2db08042015-09-09 11:29:24 -06002078 if (vkGetDeviceProcAddr != NULL)
Jon Ashburn44aed662016-02-02 17:47:28 -07002079 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2080 sizeof(props->functions.str_gdpa));
2081 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] =
2082 '\0';
Jon Ashburn1b111de2015-07-06 15:40:35 -06002083 }
Jon Ashburn738136f2015-12-10 18:17:34 -07002084 /**
2085 * instance_extensions
2086 * array of
2087 * name
2088 * spec_version
2089 */
Jon Ashburn082569b2015-08-12 16:39:32 -06002090 GET_JSON_OBJECT(layer_node, instance_extensions)
2091 if (instance_extensions != NULL) {
2092 int count = cJSON_GetArraySize(instance_extensions);
2093 for (i = 0; i < count; i++) {
2094 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2095 GET_JSON_ITEM(ext_item, name)
Jon Ashburn44fe0062015-11-18 16:46:48 -07002096 GET_JSON_ITEM(ext_item, spec_version)
Jon Ashburn738136f2015-12-10 18:17:34 -07002097 if (name != NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002098 strncpy(ext_prop.extensionName, name,
2099 sizeof(ext_prop.extensionName));
2100 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2101 '\0';
Jon Ashburn738136f2015-12-10 18:17:34 -07002102 }
Jon Ashburn44fe0062015-11-18 16:46:48 -07002103 ext_prop.specVersion = atoi(spec_version);
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002104 bool ext_unsupported =
2105 wsi_unsupported_instance_extension(&ext_prop);
Jon Ashburnc666cff2016-03-25 12:49:35 -06002106 if (!ext_unsupported) {
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002107 loader_add_to_ext_list(
2108 inst, &props->instance_extension_list, 1, &ext_prop);
Jon Ashburnc666cff2016-03-25 12:49:35 -06002109 }
Jon Ashburn082569b2015-08-12 16:39:32 -06002110 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002111 }
Jon Ashburn738136f2015-12-10 18:17:34 -07002112 /**
2113 * device_extensions
2114 * array of
2115 * name
2116 * spec_version
2117 * entrypoints
2118 */
Jon Ashburn082569b2015-08-12 16:39:32 -06002119 GET_JSON_OBJECT(layer_node, device_extensions)
2120 if (device_extensions != NULL) {
2121 int count = cJSON_GetArraySize(device_extensions);
2122 for (i = 0; i < count; i++) {
2123 ext_item = cJSON_GetArrayItem(device_extensions, i);
Jon Ashburn738136f2015-12-10 18:17:34 -07002124 GET_JSON_ITEM(ext_item, name)
2125 GET_JSON_ITEM(ext_item, spec_version)
2126 if (name != NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002127 strncpy(ext_prop.extensionName, name,
2128 sizeof(ext_prop.extensionName));
2129 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2130 '\0';
Jon Ashburn738136f2015-12-10 18:17:34 -07002131 }
Jon Ashburn44fe0062015-11-18 16:46:48 -07002132 ext_prop.specVersion = atoi(spec_version);
Jon Ashburn44aed662016-02-02 17:47:28 -07002133 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
Jon Ashburn738136f2015-12-10 18:17:34 -07002134 GET_JSON_OBJECT(ext_item, entrypoints)
2135 int entry_count;
Courtney Goeltzenleuchterd0200702016-02-05 13:09:59 -07002136 if (entrypoints == NULL) {
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07002137 loader_add_to_dev_ext_list(inst,
2138 &props->device_extension_list,
Courtney Goeltzenleuchterd0200702016-02-05 13:09:59 -07002139 &ext_prop, 0, NULL);
Jon Ashburn738136f2015-12-10 18:17:34 -07002140 continue;
Courtney Goeltzenleuchterd0200702016-02-05 13:09:59 -07002141 }
Jon Ashburn738136f2015-12-10 18:17:34 -07002142 entry_count = cJSON_GetArraySize(entrypoints);
2143 if (entry_count)
Jon Ashburn44aed662016-02-02 17:47:28 -07002144 entry_array = (char **)loader_stack_alloc(sizeof(char *) *
2145 entry_count);
Jon Ashburn738136f2015-12-10 18:17:34 -07002146 for (j = 0; j < entry_count; j++) {
2147 ext_item = cJSON_GetArrayItem(entrypoints, j);
2148 if (ext_item != NULL) {
2149 temp = cJSON_Print(ext_item);
2150 temp[strlen(temp) - 1] = '\0';
2151 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2152 strcpy(entry_array[j], &temp[1]);
2153 loader_tls_heap_free(temp);
Jon Ashburn8f6be302015-12-10 08:51:10 -07002154 }
2155 }
Jon Ashburn738136f2015-12-10 18:17:34 -07002156 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
Jon Ashburn44aed662016-02-02 17:47:28 -07002157 &ext_prop, entry_count, entry_array);
Jon Ashburn738136f2015-12-10 18:17:34 -07002158 }
2159 }
Jon Ashburn082569b2015-08-12 16:39:32 -06002160 if (is_implicit) {
2161 GET_JSON_OBJECT(layer_node, enable_environment)
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002162
2163 // enable_environment is optional
2164 if (enable_environment) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002165 strncpy(props->enable_env_var.name,
2166 enable_environment->child->string,
2167 sizeof(props->enable_env_var.name));
2168 props->enable_env_var
2169 .name[sizeof(props->enable_env_var.name) - 1] = '\0';
2170 strncpy(props->enable_env_var.value,
2171 enable_environment->child->valuestring,
2172 sizeof(props->enable_env_var.value));
2173 props->enable_env_var
2174 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002175 }
Jon Ashburn082569b2015-08-12 16:39:32 -06002176 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002177#undef GET_JSON_ITEM
2178#undef GET_JSON_OBJECT
Jon Ashburn082569b2015-08-12 16:39:32 -06002179 // for global layers need to add them to both device and instance list
2180 if (!strcmp(type, "GLOBAL")) {
2181 struct loader_layer_properties *dev_props;
2182 if (layer_instance_list == NULL || layer_device_list == NULL) {
2183 layer_node = layer_node->next;
2184 continue;
2185 }
Jon Ashburne58f1a32015-08-28 13:38:21 -06002186 dev_props = loader_get_next_layer_property(inst, layer_device_list);
Jon Ashburn44aed662016-02-02 17:47:28 -07002187 // copy into device layer list
Jon Ashburne58f1a32015-08-28 13:38:21 -06002188 loader_copy_layer_properties(inst, dev_props, props);
Jon Ashburn082569b2015-08-12 16:39:32 -06002189 }
2190 layer_node = layer_node->next;
2191 } while (layer_node != NULL);
2192 return;
Jon Ashburn1b111de2015-07-06 15:40:35 -06002193}
2194
2195/**
Jon Ashburnffd5d672015-06-29 11:25:34 -06002196 * Find the Vulkan library manifest files.
2197 *
Jon Ashburna867ee42016-02-16 15:34:16 -07002198 * This function scans the "location" or "env_override" directories/files
Jon Ashburnffd5d672015-06-29 11:25:34 -06002199 * for a list of JSON manifest files. If env_override is non-NULL
2200 * and has a valid value. Then the location is ignored. Otherwise
2201 * location is used to look for manifest files. The location
2202 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburna867ee42016-02-16 15:34:16 -07002203 * on Linux. "home_location" is an additional directory in the users home
2204 * directory to look at. It is exapanded into the dir path $HOME/home_location.
2205 * This "home_location" is only used on Linux.
Jon Ashburnffd5d672015-06-29 11:25:34 -06002206 *
2207 * \returns
2208 * A string list of manifest files to be opened in out_files param.
2209 * List has a pointer to string for each manifest filename.
2210 * When done using the list in out_files, pointers should be freed.
Jon Ashburn44aed662016-02-02 17:47:28 -07002211 * Location or override string lists can be either files or directories as
2212 *follows:
Jon Ashburnee33ae72015-06-30 14:46:22 -07002213 * | location | override
2214 * --------------------------------
2215 * Win ICD | files | files
2216 * Win Layer | files | dirs
2217 * Linux ICD | dirs | files
2218 * Linux Layer| dirs | dirs
Jon Ashburnffd5d672015-06-29 11:25:34 -06002219 */
Jon Ashburne58f1a32015-08-28 13:38:21 -06002220static void loader_get_manifest_files(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07002221 const char *env_override, bool is_layer,
Jon Ashburnee33ae72015-06-30 14:46:22 -07002222 const char *location,
Jon Ashburna867ee42016-02-16 15:34:16 -07002223 const char *home_location,
Jon Ashburn44aed662016-02-02 17:47:28 -07002224 struct loader_manifest_files *out_files) {
Jon Ashburnffd5d672015-06-29 11:25:34 -06002225 char *override = NULL;
2226 char *loc;
2227 char *file, *next_file, *name;
2228 size_t alloced_count = 64;
2229 char full_path[2048];
2230 DIR *sysdir = NULL;
Jon Ashburnee33ae72015-06-30 14:46:22 -07002231 bool list_is_dirs = false;
Jon Ashburnffd5d672015-06-29 11:25:34 -06002232 struct dirent *dent;
2233
2234 out_files->count = 0;
2235 out_files->filename_list = NULL;
2236
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07002237 if (env_override != NULL && (override = loader_getenv(env_override))) {
Johannes van Waverena6ce7b72015-10-28 11:45:00 -05002238#if !defined(_WIN32)
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002239 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnee33ae72015-06-30 14:46:22 -07002240 /* Don't allow setuid apps to use the env var: */
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07002241 loader_free_getenv(override);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002242 override = NULL;
2243 }
2244#endif
2245 }
2246
Jon Ashburna867ee42016-02-16 15:34:16 -07002247#if !defined(_WIN32)
2248 if (location == NULL && home_location == NULL) {
2249#else
2250 home_location = NULL;
Jon Ashburnffd5d672015-06-29 11:25:34 -06002251 if (location == NULL) {
Jon Ashburna867ee42016-02-16 15:34:16 -07002252#endif
Jon Ashburn44aed662016-02-02 17:47:28 -07002253 loader_log(
2254 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnee33ae72015-06-30 14:46:22 -07002255 "Can't get manifest files with NULL location, env_override=%s",
2256 env_override);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002257 return;
2258 }
2259
Johannes van Waverena6ce7b72015-10-28 11:45:00 -05002260#if defined(_WIN32)
Jon Ashburnee33ae72015-06-30 14:46:22 -07002261 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waverena6ce7b72015-10-28 11:45:00 -05002262#else
2263 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnee33ae72015-06-30 14:46:22 -07002264#endif
Jon Ashburnffd5d672015-06-29 11:25:34 -06002265 // Make a copy of the input we are using so it is not modified
Jon Ashburnee33ae72015-06-30 14:46:22 -07002266 // Also handle getting the location(s) from registry on Windows
2267 if (override == NULL) {
Jon Ashburncdc9f0a2015-07-31 10:11:24 -06002268 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnee33ae72015-06-30 14:46:22 -07002269 if (loc == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002270 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2271 "Out of memory can't get manifest files");
Jon Ashburnee33ae72015-06-30 14:46:22 -07002272 return;
2273 }
2274 strcpy(loc, location);
Johannes van Waverena6ce7b72015-10-28 11:45:00 -05002275#if defined(_WIN32)
Jon Ashburn6fb9a532015-08-28 14:58:46 -07002276 loc = loader_get_registry_files(inst, loc);
Jon Ashburnc1381ed2015-07-31 09:33:21 -06002277 if (loc == NULL) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002278 if (!is_layer) {
2279 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2280 "Registry lookup failed can't get ICD manifest "
2281 "files, do you have a Vulkan driver installed");
2282 } else {
2283 // warning only for layers
2284 loader_log(
2285 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2286 "Registry lookup failed can't get layer manifest files");
2287 }
Jon Ashburnc1381ed2015-07-31 09:33:21 -06002288 return;
2289 }
Jon Ashburnee33ae72015-06-30 14:46:22 -07002290#endif
Jon Ashburn44aed662016-02-02 17:47:28 -07002291 } else {
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -06002292 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnee33ae72015-06-30 14:46:22 -07002293 if (loc == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002294 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2295 "Out of memory can't get manifest files");
Jon Ashburnee33ae72015-06-30 14:46:22 -07002296 return;
2297 }
2298 strcpy(loc, override);
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07002299 loader_free_getenv(override);
Jon Ashburnee33ae72015-06-30 14:46:22 -07002300 }
Jon Ashburnffd5d672015-06-29 11:25:34 -06002301
Liam Middlebrookf2a6ec42015-07-23 18:32:20 -07002302 // Print out the paths being searched if debugging is enabled
Jon Ashburn44aed662016-02-02 17:47:28 -07002303 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2304 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrookf2a6ec42015-07-23 18:32:20 -07002305
Jon Ashburnffd5d672015-06-29 11:25:34 -06002306 file = loc;
2307 while (*file) {
2308 next_file = loader_get_next_path(file);
Jon Ashburnee33ae72015-06-30 14:46:22 -07002309 if (list_is_dirs) {
Jon Ashburnffd5d672015-06-29 11:25:34 -06002310 sysdir = opendir(file);
2311 name = NULL;
2312 if (sysdir) {
2313 dent = readdir(sysdir);
2314 if (dent == NULL)
2315 break;
2316 name = &(dent->d_name[0]);
2317 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2318 name = full_path;
2319 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002320 } else {
Johannes van Waverena6ce7b72015-10-28 11:45:00 -05002321#if defined(_WIN32)
2322 name = file;
2323#else
Jon Ashburnee33ae72015-06-30 14:46:22 -07002324 // only Linux has relative paths
Jon Ashburnffd5d672015-06-29 11:25:34 -06002325 char *dir;
2326 // make a copy of location so it isn't modified
Jason Ekstrand35fa1a12015-10-10 08:33:37 -07002327 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002328 if (dir == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002329 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2330 "Out of memory can't get manifest files");
Jon Ashburnffd5d672015-06-29 11:25:34 -06002331 return;
2332 }
Jason Ekstrand35fa1a12015-10-10 08:33:37 -07002333 strcpy(dir, loc);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002334
2335 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2336
2337 name = full_path;
Jon Ashburnee33ae72015-06-30 14:46:22 -07002338#endif
Jon Ashburnffd5d672015-06-29 11:25:34 -06002339 }
2340 while (name) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002341 /* Look for files ending with ".json" suffix */
2342 uint32_t nlen = (uint32_t)strlen(name);
2343 const char *suf = name + nlen - 5;
2344 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2345 if (out_files->count == 0) {
2346 out_files->filename_list =
2347 loader_heap_alloc(inst, alloced_count * sizeof(char *),
2348 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
2349 } else if (out_files->count == alloced_count) {
2350 out_files->filename_list =
2351 loader_heap_realloc(inst, out_files->filename_list,
2352 alloced_count * sizeof(char *),
2353 alloced_count * sizeof(char *) * 2,
2354 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
2355 alloced_count *= 2;
Jon Ashburnffd5d672015-06-29 11:25:34 -06002356 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002357 if (out_files->filename_list == NULL) {
2358 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2359 "Out of memory can't alloc manifest file list");
2360 return;
Jon Ashburnffd5d672015-06-29 11:25:34 -06002361 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002362 out_files->filename_list[out_files->count] = loader_heap_alloc(
2363 inst, strlen(name) + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
2364 if (out_files->filename_list[out_files->count] == NULL) {
2365 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2366 "Out of memory can't get manifest files");
2367 return;
2368 }
2369 strcpy(out_files->filename_list[out_files->count], name);
2370 out_files->count++;
2371 } else if (!list_is_dirs) {
2372 loader_log(
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002373 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002374 "Skipping manifest file %s, file name must end in .json",
2375 name);
2376 }
2377 if (list_is_dirs) {
2378 dent = readdir(sysdir);
2379 if (dent == NULL)
Jon Ashburnffd5d672015-06-29 11:25:34 -06002380 break;
Jon Ashburn44aed662016-02-02 17:47:28 -07002381 name = &(dent->d_name[0]);
2382 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2383 name = full_path;
2384 } else {
2385 break;
2386 }
Jon Ashburnffd5d672015-06-29 11:25:34 -06002387 }
2388 if (sysdir)
2389 closedir(sysdir);
2390 file = next_file;
Jon Ashburn824e7642016-02-18 12:45:39 -07002391#if !defined(_WIN32)
Jon Ashburna9c4a572016-02-26 13:14:27 -07002392 if (home_location != NULL &&
2393 (next_file == NULL || *next_file == '\0') && override == NULL) {
Jon Ashburn824e7642016-02-18 12:45:39 -07002394 char *home = secure_getenv("HOME");
2395 if (home != NULL) {
2396 size_t len;
2397 char *home_loc = loader_stack_alloc(strlen(home) + 2 +
2398 strlen(home_location));
2399 if (home_loc == NULL) {
2400 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburna9c4a572016-02-26 13:14:27 -07002401 "Out of memory can't get manifest files");
Jon Ashburn824e7642016-02-18 12:45:39 -07002402 return;
2403 }
2404 strcpy(home_loc, home);
2405 // Add directory separator if needed
2406 if (home_location[0] != DIRECTORY_SYMBOL) {
2407 len = strlen(home_loc);
2408 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburna9c4a572016-02-26 13:14:27 -07002409 home_loc[len + 1] = '\0';
Jon Ashburn824e7642016-02-18 12:45:39 -07002410 }
2411 strcat(home_loc, home_location);
2412 file = home_loc;
2413 next_file = loader_get_next_path(file);
2414 home_location = NULL;
2415
Jon Ashburna9c4a572016-02-26 13:14:27 -07002416 loader_log(
2417 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2418 "Searching the following paths for manifest files: %s\n",
2419 home_loc);
Jon Ashburn824e7642016-02-18 12:45:39 -07002420 list_is_dirs = true;
2421 }
2422 }
2423#endif
Jon Ashburnffd5d672015-06-29 11:25:34 -06002424 }
2425 return;
2426}
2427
Jon Ashburn44aed662016-02-02 17:47:28 -07002428void loader_init_icd_lib_list() {}
Jon Ashburn754f1992015-08-18 18:04:47 -06002429
Jon Ashburn44aed662016-02-02 17:47:28 -07002430void loader_destroy_icd_lib_list() {}
Jon Ashburnffd5d672015-06-29 11:25:34 -06002431/**
2432 * Try to find the Vulkan ICD driver(s).
2433 *
2434 * This function scans the default system loader path(s) or path
2435 * specified by the \c VK_ICD_FILENAMES environment variable in
2436 * order to find loadable VK ICDs manifest files. From these
2437 * manifest files it finds the ICD libraries.
2438 *
2439 * \returns
Jon Ashburn754f1992015-08-18 18:04:47 -06002440 * a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002441 */
Jon Ashburn44aed662016-02-02 17:47:28 -07002442void loader_icd_scan(const struct loader_instance *inst,
2443 struct loader_icd_libs *icds) {
Jon Ashburnffd5d672015-06-29 11:25:34 -06002444 char *file_str;
2445 struct loader_manifest_files manifest_files;
2446
Jon Ashburne58f1a32015-08-28 13:38:21 -06002447 loader_scanned_icd_init(inst, icds);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002448 // Get a list of manifest files for ICDs
Jon Ashburne58f1a32015-08-28 13:38:21 -06002449 loader_get_manifest_files(inst, "VK_ICD_FILENAMES", false,
Jon Ashburna867ee42016-02-16 15:34:16 -07002450 DEFAULT_VK_DRIVERS_INFO, HOME_VK_DRIVERS_INFO,
2451 &manifest_files);
Jon Ashburn1b111de2015-07-06 15:40:35 -06002452 if (manifest_files.count == 0)
2453 return;
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06002454 loader_platform_thread_lock_mutex(&loader_json_lock);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002455 for (uint32_t i = 0; i < manifest_files.count; i++) {
2456 file_str = manifest_files.filename_list[i];
2457 if (file_str == NULL)
2458 continue;
2459
Jon Ashburn1b111de2015-07-06 15:40:35 -06002460 cJSON *json;
Courtney Goeltzenleuchter05525da2015-12-03 13:48:01 -07002461 json = loader_get_json(inst, file_str);
Jon Ashburna01fb3e2015-08-27 08:30:50 -06002462 if (!json)
2463 continue;
Jon Ashburn0083b222015-11-17 17:35:40 -07002464 cJSON *item, *itemICD;
Jon Ashburn1b111de2015-07-06 15:40:35 -06002465 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06002466 if (item == NULL) {
2467 loader_platform_thread_unlock_mutex(&loader_json_lock);
Jon Ashburn1b111de2015-07-06 15:40:35 -06002468 return;
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06002469 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002470 char *file_vers = cJSON_Print(item);
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002471 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002472 "Found manifest file %s, version %s", file_str, file_vers);
Jon Ashburn1b111de2015-07-06 15:40:35 -06002473 if (strcmp(file_vers, "\"1.0.0\"") != 0)
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002474 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002475 "Unexpected manifest file version (expected 1.0.0), may "
2476 "cause errors");
Jon Ashburn413d6582015-08-28 15:19:27 -06002477 loader_tls_heap_free(file_vers);
Jon Ashburn0083b222015-11-17 17:35:40 -07002478 itemICD = cJSON_GetObjectItem(json, "ICD");
2479 if (itemICD != NULL) {
2480 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn1b111de2015-07-06 15:40:35 -06002481 if (item != NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002482 char *temp = cJSON_Print(item);
Jon Ashburn10442c12015-08-25 16:48:24 -06002483 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002484 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002485 "Can't find \"library_path\" in ICD JSON file "
2486 "%s, skipping",
2487 file_str);
Jon Ashburn413d6582015-08-28 15:19:27 -06002488 loader_tls_heap_free(temp);
Jon Ashburne58f1a32015-08-28 13:38:21 -06002489 loader_heap_free(inst, file_str);
Jon Ashburn10442c12015-08-25 16:48:24 -06002490 cJSON_Delete(json);
2491 continue;
Jon Ashburnffd5d672015-06-29 11:25:34 -06002492 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002493 // strip out extra quotes
Jon Ashburn10442c12015-08-25 16:48:24 -06002494 temp[strlen(temp) - 1] = '\0';
2495 char *library_path = loader_stack_alloc(strlen(temp) + 1);
2496 strcpy(library_path, &temp[1]);
Jon Ashburn413d6582015-08-28 15:19:27 -06002497 loader_tls_heap_free(temp);
Jon Ashburn10442c12015-08-25 16:48:24 -06002498 if (!library_path || strlen(library_path) == 0) {
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002499 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002500 "Can't find \"library_path\" in ICD JSON file "
2501 "%s, skipping",
2502 file_str);
Jon Ashburne58f1a32015-08-28 13:38:21 -06002503 loader_heap_free(inst, file_str);
Jon Ashburn10442c12015-08-25 16:48:24 -06002504 cJSON_Delete(json);
2505 continue;
2506 }
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002507 char fullpath[MAX_STRING_SIZE], *fpath;
Jon Ashburn10442c12015-08-25 16:48:24 -06002508 // Print out the paths being searched if debugging is enabled
Jon Ashburn44aed662016-02-02 17:47:28 -07002509 loader_log(
2510 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002511 "Searching for ICD drivers named %s\n",
2512 library_path);
Daniel Dadap2e13fca2015-09-30 11:50:51 -05002513 if (loader_platform_is_path(library_path)) {
Jon Ashburn10442c12015-08-25 16:48:24 -06002514 // a relative or absolute path
Daniel Dadap2e13fca2015-09-30 11:50:51 -05002515 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
2516 char *rel_base;
Jon Ashburn10442c12015-08-25 16:48:24 -06002517 strcpy(name_copy, file_str);
2518 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn44aed662016-02-02 17:47:28 -07002519 loader_expand_path(library_path, rel_base, sizeof(fullpath),
2520 fullpath);
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002521 fpath = fullpath;
Daniel Dadap2e13fca2015-09-30 11:50:51 -05002522 } else {
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002523 // a filename which will be passed to the OSes library loader
2524 fpath = library_path;
Jon Ashburn10442c12015-08-25 16:48:24 -06002525 }
Jon Ashburn0083b222015-11-17 17:35:40 -07002526
2527 uint32_t vers = 0;
2528 item = cJSON_GetObjectItem(itemICD, "api_version");
2529 if (item != NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002530 temp = cJSON_Print(item);
Jon Ashburn0083b222015-11-17 17:35:40 -07002531 vers = loader_make_version(temp);
2532 loader_tls_heap_free(temp);
2533 }
Jon Ashburnb1e6b562016-04-15 13:19:43 -06002534 loader_scanned_icd_add(inst, icds, fpath, vers);
Jon Ashburn44aed662016-02-02 17:47:28 -07002535 } else
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002536 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002537 "Can't find \"library_path\" object in ICD JSON "
2538 "file %s, skipping",
2539 file_str);
2540 } else
2541 loader_log(
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002542 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002543 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
2544 file_str);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002545
Jon Ashburne58f1a32015-08-28 13:38:21 -06002546 loader_heap_free(inst, file_str);
Jon Ashburnffd5d672015-06-29 11:25:34 -06002547 cJSON_Delete(json);
2548 }
Jon Ashburne58f1a32015-08-28 13:38:21 -06002549 loader_heap_free(inst, manifest_files.filename_list);
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06002550 loader_platform_thread_unlock_mutex(&loader_json_lock);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002551}
2552
Jon Ashburn44aed662016-02-02 17:47:28 -07002553void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburne58f1a32015-08-28 13:38:21 -06002554 struct loader_layer_list *instance_layers,
Jon Ashburn44aed662016-02-02 17:47:28 -07002555 struct loader_layer_list *device_layers) {
Jon Ashburn1b111de2015-07-06 15:40:35 -06002556 char *file_str;
Jon Ashburn44aed662016-02-02 17:47:28 -07002557 struct loader_manifest_files
2558 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn1b111de2015-07-06 15:40:35 -06002559 cJSON *json;
2560 uint32_t i;
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002561 uint32_t implicit;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06002562
Jon Ashburn715de582016-02-10 20:59:26 -07002563 // Get a list of manifest files for explicit layers
Jon Ashburn44aed662016-02-02 17:47:28 -07002564 loader_get_manifest_files(inst, LAYERS_PATH_ENV, true,
Jon Ashburna867ee42016-02-16 15:34:16 -07002565 DEFAULT_VK_ELAYERS_INFO, HOME_VK_ELAYERS_INFO,
2566 &manifest_files[0]);
Jon Ashburn44aed662016-02-02 17:47:28 -07002567 // Pass NULL for environment variable override - implicit layers are not
2568 // overridden by LAYERS_PATH_ENV
Jon Ashburnec71e112015-12-24 13:58:35 -07002569 loader_get_manifest_files(inst, NULL, true, DEFAULT_VK_ILAYERS_INFO,
Jon Ashburna867ee42016-02-16 15:34:16 -07002570 HOME_VK_ILAYERS_INFO, &manifest_files[1]);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002571 if (manifest_files[0].count == 0 && manifest_files[1].count == 0)
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -07002572 return;
Jon Ashburn0dcd6192015-06-04 15:30:58 -06002573
Jon Ashburn44aed662016-02-02 17:47:28 -07002574#if 0 // TODO
Jon Ashburn1b111de2015-07-06 15:40:35 -06002575 /**
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -06002576 * We need a list of the layer libraries, not just a list of
2577 * the layer properties (a layer library could expose more than
2578 * one layer property). This list of scanned layers would be
2579 * used to check for global and physicaldevice layer properties.
2580 */
2581 if (!loader_init_layer_library_list(&loader.scanned_layer_libraries)) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07002582 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburne58f1a32015-08-28 13:38:21 -06002583 "Alloc for layer list failed: %s line: %d", __FILE__, __LINE__);
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -06002584 return;
Jon Ashburn68a63922015-07-02 09:40:15 -06002585 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002586#endif
Jon Ashburnd43f9b62014-10-14 19:15:22 -06002587
Jon Ashburn8e4fab42015-08-03 17:19:30 -06002588 /* cleanup any previously scanned libraries */
Jon Ashburne58f1a32015-08-28 13:38:21 -06002589 loader_delete_layer_properties(inst, instance_layers);
2590 loader_delete_layer_properties(inst, device_layers);
Jon Ashburn0bf6a182015-07-16 17:19:31 -06002591
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06002592 loader_platform_thread_lock_mutex(&loader_json_lock);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002593 for (implicit = 0; implicit < 2; implicit++) {
2594 for (i = 0; i < manifest_files[implicit].count; i++) {
2595 file_str = manifest_files[implicit].filename_list[i];
2596 if (file_str == NULL)
2597 continue;
Courtney Goeltzenleuchteraa685052015-06-01 14:49:17 -06002598
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002599 // parse file into JSON struct
2600 json = loader_get_json(inst, file_str);
2601 if (!json) {
2602 continue;
2603 }
2604
Jon Ashburn44aed662016-02-02 17:47:28 -07002605 // TODO error if device layers expose instance_extensions
2606 // TODO error if instance layers expose device extensions
2607 loader_add_layer_properties(inst, instance_layers, device_layers,
2608 json, (implicit == 1), file_str);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002609
2610 loader_heap_free(inst, file_str);
2611 cJSON_Delete(json);
Jon Ashburn1b111de2015-07-06 15:40:35 -06002612 }
Jon Ashburn1b111de2015-07-06 15:40:35 -06002613 }
Jon Ashburnd2c905c2015-12-17 17:38:24 -07002614 if (manifest_files[0].count != 0)
2615 loader_heap_free(inst, manifest_files[0].filename_list);
2616
2617 if (manifest_files[1].count != 0)
2618 loader_heap_free(inst, manifest_files[1].filename_list);
Jon Ashburn715de582016-02-10 20:59:26 -07002619
2620 // add a meta layer for validation if the validation layers are all present
2621 loader_add_layer_property_meta(
2622 inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]),
2623 std_validation_names, instance_layers, device_layers);
2624
Jon Ashburnd02fc2c2015-09-22 13:11:00 -06002625 loader_platform_thread_unlock_mutex(&loader_json_lock);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06002626}
2627
Jeremy Hayes0005c0c2016-04-01 11:40:26 -06002628void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002629 struct loader_layer_list *instance_layers,
2630 struct loader_layer_list *device_layers) {
Jeremy Hayes0005c0c2016-04-01 11:40:26 -06002631 char *file_str;
2632 struct loader_manifest_files manifest_files;
2633 cJSON *json;
2634 uint32_t i;
2635
2636 // Pass NULL for environment variable override - implicit layers are not
2637 // overridden by LAYERS_PATH_ENV
2638 loader_get_manifest_files(inst, NULL, true, DEFAULT_VK_ILAYERS_INFO,
2639 HOME_VK_ILAYERS_INFO, &manifest_files);
2640 if (manifest_files.count == 0) {
2641 return;
2642 }
2643
2644 /* cleanup any previously scanned libraries */
2645 loader_delete_layer_properties(inst, instance_layers);
2646 loader_delete_layer_properties(inst, device_layers);
2647
2648 loader_platform_thread_lock_mutex(&loader_json_lock);
2649
2650 for (i = 0; i < manifest_files.count; i++) {
2651 file_str = manifest_files.filename_list[i];
2652 if (file_str == NULL) {
2653 continue;
2654 }
2655
2656 // parse file into JSON struct
2657 json = loader_get_json(inst, file_str);
2658 if (!json) {
2659 continue;
2660 }
2661
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002662 loader_add_layer_properties(inst, instance_layers, device_layers, json,
2663 true, file_str);
Jeremy Hayes0005c0c2016-04-01 11:40:26 -06002664
2665 loader_heap_free(inst, file_str);
2666 cJSON_Delete(json);
2667 }
2668
2669 if (manifest_files.count != 0) {
2670 loader_heap_free(inst, manifest_files.filename_list);
2671 }
2672
2673 // add a meta layer for validation if the validation layers are all present
2674 loader_add_layer_property_meta(
2675 inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]),
2676 std_validation_names, instance_layers, device_layers);
2677
2678 loader_platform_thread_unlock_mutex(&loader_json_lock);
2679}
2680
Jon Ashburn44aed662016-02-02 17:47:28 -07002681static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
2682loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07002683 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn44aed662016-02-02 17:47:28 -07002684 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07002685 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburna9c4a572016-02-26 13:14:27 -07002686 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07002687 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburna9c4a572016-02-26 13:14:27 -07002688 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07002689
Jon Ashburnfce93d92015-05-12 17:26:48 -06002690 // inst is not wrapped
2691 if (inst == VK_NULL_HANDLE) {
2692 return NULL;
2693 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002694 VkLayerInstanceDispatchTable *disp_table =
2695 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburnfce93d92015-05-12 17:26:48 -06002696 void *addr;
2697
2698 if (disp_table == NULL)
2699 return NULL;
2700
Jon Ashburn00df0452016-03-08 09:30:30 -07002701 bool found_name;
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002702 addr =
2703 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburn00df0452016-03-08 09:30:30 -07002704 if (found_name) {
Jon Ashburnfce93d92015-05-12 17:26:48 -06002705 return addr;
Jon Ashburne18431b2015-04-13 18:10:06 -06002706 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002707
Jon Ashburn00df0452016-03-08 09:30:30 -07002708 // Don't call down the chain, this would be an infinite loop
2709 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002710 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburn00df0452016-03-08 09:30:30 -07002711 return NULL;
Jon Ashburne18431b2015-04-13 18:10:06 -06002712}
2713
Piers Daniellfc500412016-03-29 11:51:11 -06002714static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002715loader_gpa_device_internal(VkDevice device, const char *pName) {
2716 struct loader_device *dev;
2717 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
Piers Daniellfc500412016-03-29 11:51:11 -06002718 return icd->GetDeviceProcAddr(device, pName);
2719}
2720
Jon Ashburn429e19f2015-11-17 15:31:02 -07002721/**
2722 * Initialize device_ext dispatch table entry as follows:
2723 * If dev == NULL find all logical devices created within this instance and
2724 * init the entry (given by idx) in the ext dispatch table.
2725 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn44aed662016-02-02 17:47:28 -07002726 * The initialization value is gotten by calling down the device chain with
2727 * GDPA.
Jon Ashburn429e19f2015-11-17 15:31:02 -07002728 * If GDPA returns NULL then don't initialize the dispatch table entry.
2729 */
2730static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07002731 struct loader_device *dev,
2732 uint32_t idx,
2733 const char *funcName)
Jon Ashburn429e19f2015-11-17 15:31:02 -07002734
Jon Ashburn44aed662016-02-02 17:47:28 -07002735{
Jon Ashburn429e19f2015-11-17 15:31:02 -07002736 void *gdpa_value;
2737 if (dev != NULL) {
2738 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn44aed662016-02-02 17:47:28 -07002739 dev->device, funcName);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002740 if (gdpa_value != NULL)
Jon Ashburn44aed662016-02-02 17:47:28 -07002741 dev->loader_dispatch.ext_dispatch.DevExt[idx] =
2742 (PFN_vkDevExt)gdpa_value;
Jon Ashburn429e19f2015-11-17 15:31:02 -07002743 } else {
2744 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
2745 struct loader_icd *icd = &inst->icds[i];
Karl Schultzc50a5982016-02-24 14:39:39 -07002746 struct loader_device *ldev = icd->logical_device_list;
2747 while (ldev) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002748 gdpa_value =
Karl Schultzc50a5982016-02-24 14:39:39 -07002749 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
2750 ldev->device, funcName);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002751 if (gdpa_value != NULL)
Karl Schultzc50a5982016-02-24 14:39:39 -07002752 ldev->loader_dispatch.ext_dispatch.DevExt[idx] =
Jon Ashburn44aed662016-02-02 17:47:28 -07002753 (PFN_vkDevExt)gdpa_value;
Karl Schultzc50a5982016-02-24 14:39:39 -07002754 ldev = ldev->next;
Jon Ashburn429e19f2015-11-17 15:31:02 -07002755 }
2756 }
2757 }
Jon Ashburn429e19f2015-11-17 15:31:02 -07002758}
2759
2760/**
2761 * Find all dev extension in the hash table and initialize the dispatch table
2762 * for dev for each of those extension entrypoints found in hash table.
2763
2764 */
Jon Ashburna9c4a572016-02-26 13:14:27 -07002765void loader_init_dispatch_dev_ext(struct loader_instance *inst,
2766 struct loader_device *dev) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002767 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
2768 if (inst->disp_hash[i].func_name != NULL)
2769 loader_init_dispatch_dev_ext_entry(inst, dev, i,
2770 inst->disp_hash[i].func_name);
2771 }
2772}
2773
2774static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07002775 const char *funcName) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002776 struct loader_icd *icd;
2777 icd = inst->icds;
2778 while (icd) {
2779 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
2780 // this icd supports funcName
2781 return true;
2782 icd = icd->next;
2783 }
2784
2785 return false;
2786}
2787
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002788static bool loader_check_layer_list_for_address(
2789 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002790 // Iterate over the layers.
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002791 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002792 // Iterate over the extensions.
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002793 const struct loader_device_extension_list *const extensions =
2794 &(layers->list[layer].device_extension_list);
2795 for (uint32_t extension = 0; extension < extensions->count;
2796 ++extension) {
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002797 // Iterate over the entry points.
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002798 const struct loader_dev_ext_props *const property =
2799 &(extensions->list[extension]);
2800 for (uint32_t entry = 0; entry < property->entrypoint_count;
2801 ++entry) {
2802 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002803 return true;
2804 }
2805 }
2806 }
2807 }
2808
2809 return false;
2810}
2811
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002812static bool
2813loader_check_layers_for_address(const struct loader_instance *const inst,
2814 const char *funcName) {
2815 if (loader_check_layer_list_for_address(&inst->instance_layer_list,
2816 funcName)) {
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002817 return true;
2818 }
2819
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06002820 if (loader_check_layer_list_for_address(&inst->device_layer_list,
2821 funcName)) {
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002822 return true;
2823 }
2824
2825 return false;
2826}
2827
Jon Ashburn44aed662016-02-02 17:47:28 -07002828static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002829 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
2830 loader_heap_free(inst, inst->disp_hash[i].func_name);
2831 loader_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002832 }
2833 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
2834}
2835
2836static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07002837 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002838 uint32_t i;
2839 uint32_t idx = *ptr_idx;
2840 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
2841
2842 if (!inst->disp_hash[idx].func_name) {
2843 // no entry here at this idx, so use it
2844 assert(list->capacity == 0);
Jon Ashburn44aed662016-02-02 17:47:28 -07002845 inst->disp_hash[idx].func_name = (char *)loader_heap_alloc(
2846 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002847 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07002848 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002849 "loader_add_dev_ext_table() can't allocate memory for "
2850 "func_name");
Jon Ashburn429e19f2015-11-17 15:31:02 -07002851 return false;
2852 }
2853 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
2854 return true;
2855 }
2856
2857 // check for enough capacity
2858 if (list->capacity == 0) {
2859 list->index = loader_heap_alloc(inst, 8 * sizeof(*(list->index)),
2860 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
2861 if (list->index == NULL) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07002862 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn429e19f2015-11-17 15:31:02 -07002863 "loader_add_dev_ext_table() can't allocate list memory");
2864 return false;
2865 }
2866 list->capacity = 8 * sizeof(*(list->index));
2867 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
2868 list->index = loader_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn44aed662016-02-02 17:47:28 -07002869 list->capacity * 2,
2870 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002871 if (list->index == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07002872 loader_log(
2873 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2874 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburn429e19f2015-11-17 15:31:02 -07002875 return false;
2876 }
2877 list->capacity *= 2;
2878 }
2879
Jon Ashburn44aed662016-02-02 17:47:28 -07002880 // find an unused index in the hash table and use it
Jon Ashburn429e19f2015-11-17 15:31:02 -07002881 i = (idx + 1) % MAX_NUM_DEV_EXTS;
2882 do {
2883 if (!inst->disp_hash[i].func_name) {
2884 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn44aed662016-02-02 17:47:28 -07002885 inst->disp_hash[i].func_name =
2886 (char *)loader_heap_alloc(inst, strlen(funcName) + 1,
2887 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002888 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07002889 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002890 "loader_add_dev_ext_table() can't rallocate "
2891 "func_name memory");
Jon Ashburn429e19f2015-11-17 15:31:02 -07002892 return false;
2893 }
Jon Ashburn44aed662016-02-02 17:47:28 -07002894 strncpy(inst->disp_hash[i].func_name, funcName,
2895 strlen(funcName) + 1);
Jon Ashburn429e19f2015-11-17 15:31:02 -07002896 list->index[list->count] = i;
2897 list->count++;
2898 *ptr_idx = i;
2899 return true;
2900 }
2901 i = (i + 1) % MAX_NUM_DEV_EXTS;
2902 } while (i != idx);
2903
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07002904 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07002905 "loader_add_dev_ext_table() couldn't insert into hash table; is "
2906 "it full?");
Jon Ashburn429e19f2015-11-17 15:31:02 -07002907 return false;
2908}
2909
2910static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07002911 uint32_t *idx, const char *funcName) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002912 uint32_t alt_idx;
Jon Ashburn44aed662016-02-02 17:47:28 -07002913 if (inst->disp_hash[*idx].func_name &&
2914 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburn429e19f2015-11-17 15:31:02 -07002915 return true;
2916
2917 // funcName wasn't at the primary spot in the hash table
2918 // search the list of secondary locations (shallow search, not deep search)
2919 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
2920 alt_idx = inst->disp_hash[*idx].list.index[i];
2921 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
2922 *idx = alt_idx;
2923 return true;
2924 }
2925 }
2926
2927 return false;
2928}
2929
2930/**
Jon Ashburn44aed662016-02-02 17:47:28 -07002931 * This function returns generic trampoline code address for unknown entry
2932 * points.
Jon Ashburn429e19f2015-11-17 15:31:02 -07002933 * Presumably, these unknown entry points (as given by funcName) are device
2934 * extension entrypoints. A hash table is used to keep a list of unknown entry
2935 * points and their mapping to the device extension dispatch table
2936 * (struct loader_dev_ext_dispatch_table).
2937 * \returns
Jon Ashburn44aed662016-02-02 17:47:28 -07002938 * For a given entry point string (funcName), if an existing mapping is found
2939 * the
2940 * trampoline address for that mapping is returned. Otherwise, this unknown
2941 * entry point
2942 * has not been seen yet. Next check if a layer or ICD supports it. If so then
2943 * a
Jon Ashburn429e19f2015-11-17 15:31:02 -07002944 * new entry in the hash table is initialized and that trampoline address for
2945 * the new entry is returned. Null is returned if the hash table is full or
2946 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
2947 */
Jon Ashburn44aed662016-02-02 17:47:28 -07002948void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002949 uint32_t idx;
2950 uint32_t seed = 0;
2951
2952 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
2953
2954 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
2955 // found funcName already in hash
2956 return loader_get_dev_ext_trampoline(idx);
2957
2958 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes4c90acf2016-03-03 16:03:03 -07002959 if (!loader_check_icds_for_address(inst, funcName) &&
2960 !loader_check_layers_for_address(inst, funcName)) {
Jon Ashburn429e19f2015-11-17 15:31:02 -07002961 // if support found in layers continue on
2962 return NULL;
2963 }
2964
2965 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
2966 // successfully added new table entry
2967 // init any dev dispatch table entrys as needed
2968 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
2969 return loader_get_dev_ext_trampoline(idx);
2970 }
2971
2972 return NULL;
2973}
2974
Jon Ashburn44aed662016-02-02 17:47:28 -07002975struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburn0c5eea22015-09-30 12:56:42 -06002976 /* look up the loader_instance in our list by comparing dispatch tables, as
2977 * there is no guarantee the instance is still a loader_instance* after any
2978 * layers which wrap the instance object.
2979 */
2980 const VkLayerInstanceDispatchTable *disp;
2981 struct loader_instance *ptr_instance = NULL;
2982 disp = loader_get_instance_dispatch(instance);
Jon Ashburn44aed662016-02-02 17:47:28 -07002983 for (struct loader_instance *inst = loader.instances; inst;
2984 inst = inst->next) {
Jon Ashburn0c5eea22015-09-30 12:56:42 -06002985 if (inst->disp == disp) {
2986 ptr_instance = inst;
2987 break;
2988 }
2989 }
2990 return ptr_instance;
2991}
2992
Jon Ashburn44aed662016-02-02 17:47:28 -07002993static loader_platform_dl_handle
2994loader_add_layer_lib(const struct loader_instance *inst, const char *chain_type,
2995 struct loader_layer_properties *layer_prop) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002996 struct loader_lib_info *new_layer_lib_list, *my_lib;
Jon Ashburne58f1a32015-08-28 13:38:21 -06002997 size_t new_alloc_size;
Courtney Goeltzenleuchterc5cf7d72015-07-05 12:53:31 -06002998 /*
2999 * TODO: We can now track this information in the
3000 * scanned_layer_libraries list.
3001 */
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003002 for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003003 if (strcmp(loader.loaded_layer_lib_list[i].lib_name,
3004 layer_prop->lib_name) == 0) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003005 /* Have already loaded this library, just increment ref count */
3006 loader.loaded_layer_lib_list[i].ref_count++;
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07003007 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07003008 "%s Chain: Increment layer reference count for layer "
3009 "library %s",
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06003010 chain_type, layer_prop->lib_name);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003011 return loader.loaded_layer_lib_list[i].lib_handle;
3012 }
3013 }
3014
3015 /* Haven't seen this library so load it */
Jon Ashburne58f1a32015-08-28 13:38:21 -06003016 new_alloc_size = 0;
3017 if (loader.loaded_layer_lib_capacity == 0)
3018 new_alloc_size = 8 * sizeof(struct loader_lib_info);
Jon Ashburn44aed662016-02-02 17:47:28 -07003019 else if (loader.loaded_layer_lib_capacity <=
3020 loader.loaded_layer_lib_count * sizeof(struct loader_lib_info))
Jon Ashburne58f1a32015-08-28 13:38:21 -06003021 new_alloc_size = loader.loaded_layer_lib_capacity * 2;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003022
Jon Ashburne58f1a32015-08-28 13:38:21 -06003023 if (new_alloc_size) {
3024 new_layer_lib_list = loader_heap_realloc(
Jon Ashburn44aed662016-02-02 17:47:28 -07003025 inst, loader.loaded_layer_lib_list,
3026 loader.loaded_layer_lib_capacity, new_alloc_size,
3027 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne58f1a32015-08-28 13:38:21 -06003028 if (!new_layer_lib_list) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003029 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3030 "loader: realloc failed in loader_add_layer_lib");
Jon Ashburne58f1a32015-08-28 13:38:21 -06003031 return NULL;
3032 }
3033 loader.loaded_layer_lib_capacity = new_alloc_size;
Courtney Goeltzenleuchter65e31cc2015-12-09 12:26:34 -07003034 loader.loaded_layer_lib_list = new_layer_lib_list;
Jon Ashburne58f1a32015-08-28 13:38:21 -06003035 } else
3036 new_layer_lib_list = loader.loaded_layer_lib_list;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003037 my_lib = &new_layer_lib_list[loader.loaded_layer_lib_count];
3038
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06003039 strncpy(my_lib->lib_name, layer_prop->lib_name, sizeof(my_lib->lib_name));
3040 my_lib->lib_name[sizeof(my_lib->lib_name) - 1] = '\0';
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003041 my_lib->ref_count = 0;
3042 my_lib->lib_handle = NULL;
3043
Jon Ashburn44aed662016-02-02 17:47:28 -07003044 if ((my_lib->lib_handle = loader_platform_open_library(my_lib->lib_name)) ==
3045 NULL) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07003046 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003047 loader_platform_open_library_error(my_lib->lib_name));
3048 return NULL;
3049 } else {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07003050 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07003051 "Chain: %s: Loading layer library %s", chain_type,
3052 layer_prop->lib_name);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003053 }
3054 loader.loaded_layer_lib_count++;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003055 my_lib->ref_count++;
3056
3057 return my_lib->lib_handle;
3058}
3059
Jon Ashburn44aed662016-02-02 17:47:28 -07003060static void
3061loader_remove_layer_lib(struct loader_instance *inst,
3062 struct loader_layer_properties *layer_prop) {
Courtney Goeltzenleuchter46724972015-12-17 09:51:22 -07003063 uint32_t idx = loader.loaded_layer_lib_count;
Tony Barbour9687cb12015-07-14 13:34:05 -06003064 struct loader_lib_info *new_layer_lib_list, *my_lib = NULL;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003065
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003066 for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003067 if (strcmp(loader.loaded_layer_lib_list[i].lib_name,
3068 layer_prop->lib_name) == 0) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003069 /* found matching library */
3070 idx = i;
3071 my_lib = &loader.loaded_layer_lib_list[i];
3072 break;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003073 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003074 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003075
Courtney Goeltzenleuchter46724972015-12-17 09:51:22 -07003076 if (idx == loader.loaded_layer_lib_count) {
Jon Ashburndb68e4b2015-12-17 14:30:21 -07003077 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Courtney Goeltzenleuchter46724972015-12-17 09:51:22 -07003078 "Unable to unref library %s", layer_prop->lib_name);
3079 return;
3080 }
3081
Tony Barbour9687cb12015-07-14 13:34:05 -06003082 if (my_lib) {
3083 my_lib->ref_count--;
3084 if (my_lib->ref_count > 0) {
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07003085 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07003086 "Decrement reference count for layer library %s",
3087 layer_prop->lib_name);
Tony Barbour9687cb12015-07-14 13:34:05 -06003088 return;
3089 }
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07003090 }
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003091 loader_platform_close_library(my_lib->lib_handle);
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07003092 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburne9ca8fa2015-08-20 16:35:30 -06003093 "Unloading layer library %s", layer_prop->lib_name);
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003094
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003095 /* Need to remove unused library from list */
Jon Ashburn44aed662016-02-02 17:47:28 -07003096 new_layer_lib_list =
3097 loader_heap_alloc(inst, loader.loaded_layer_lib_capacity,
3098 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003099 if (!new_layer_lib_list) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003100 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3101 "loader: heap alloc failed loader_remove_layer_library");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003102 return;
3103 }
3104
3105 if (idx > 0) {
3106 /* Copy records before idx */
3107 memcpy(new_layer_lib_list, &loader.loaded_layer_lib_list[0],
3108 sizeof(struct loader_lib_info) * idx);
3109 }
3110 if (idx < (loader.loaded_layer_lib_count - 1)) {
3111 /* Copy records after idx */
Jon Ashburn44aed662016-02-02 17:47:28 -07003112 memcpy(&new_layer_lib_list[idx], &loader.loaded_layer_lib_list[idx + 1],
3113 sizeof(struct loader_lib_info) *
3114 (loader.loaded_layer_lib_count - idx - 1));
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003115 }
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003116
Jon Ashburne58f1a32015-08-28 13:38:21 -06003117 loader_heap_free(inst, loader.loaded_layer_lib_list);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003118 loader.loaded_layer_lib_count--;
3119 loader.loaded_layer_lib_list = new_layer_lib_list;
Jon Ashburnead95c52014-11-18 09:06:04 -07003120}
3121
Jon Ashburn1b111de2015-07-06 15:40:35 -06003122/**
3123 * Go through the search_list and find any layers which match type. If layer
3124 * type match is found in then add it to ext_list.
3125 */
Jon Ashburn44aed662016-02-02 17:47:28 -07003126static void
3127loader_add_layer_implicit(const struct loader_instance *inst,
3128 const enum layer_type type,
3129 struct loader_layer_list *list,
3130 const struct loader_layer_list *search_list) {
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003131 bool enable;
3132 char *env_value;
Jon Ashburn535bd002015-07-02 16:10:32 -06003133 uint32_t i;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06003134 for (i = 0; i < search_list->count; i++) {
3135 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn535bd002015-07-02 16:10:32 -06003136 if (prop->type & type) {
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003137 /* Found an implicit layer, see if it should be enabled */
3138 enable = false;
3139
Jon Ashburn44aed662016-02-02 17:47:28 -07003140 // if no enable_environment variable is specified, this implicit
3141 // layer
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003142 // should always be enabled. Otherwise check if the variable is set
3143 if (prop->enable_env_var.name[0] == 0) {
3144 enable = true;
3145 } else {
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07003146 env_value = loader_getenv(prop->enable_env_var.name);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003147 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3148 enable = true;
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07003149 loader_free_getenv(env_value);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003150 }
3151
3152 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn44aed662016-02-02 17:47:28 -07003153 // environment variables are set, the layer is disabled. Implicit
3154 // layers
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003155 // are required to have a disable_environment variables
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07003156 env_value = loader_getenv(prop->disable_env_var.name);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003157 if (env_value)
3158 enable = false;
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07003159 loader_free_getenv(env_value);
Jon Ashburnd2c905c2015-12-17 17:38:24 -07003160
3161 if (enable)
3162 loader_add_to_layer_list(inst, list, 1, prop);
Jon Ashburn535bd002015-07-02 16:10:32 -06003163 }
3164 }
Jon Ashburn535bd002015-07-02 16:10:32 -06003165}
3166
3167/**
3168 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnf2ddb732015-07-07 10:27:45 -06003169 * is found in search_list then add it to layer_list. But only add it to
3170 * layer_list if type matches.
Jon Ashburn535bd002015-07-02 16:10:32 -06003171 */
Jon Ashburn44aed662016-02-02 17:47:28 -07003172static void loader_add_layer_env(const struct loader_instance *inst,
3173 const enum layer_type type,
3174 const char *env_name,
3175 struct loader_layer_list *layer_list,
3176 const struct loader_layer_list *search_list) {
Ian Elliott225188f2015-02-17 10:33:47 -07003177 char *layerEnv;
Jon Ashburna2e6efe2015-07-02 14:10:53 -06003178 char *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003179
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07003180 layerEnv = loader_getenv(env_name);
Ian Elliott225188f2015-02-17 10:33:47 -07003181 if (layerEnv == NULL) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003182 return;
Ian Elliott225188f2015-02-17 10:33:47 -07003183 }
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -06003184 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburna2e6efe2015-07-02 14:10:53 -06003185 if (name == NULL) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003186 return;
Ian Elliott225188f2015-02-17 10:33:47 -07003187 }
Jon Ashburna2e6efe2015-07-02 14:10:53 -06003188 strcpy(name, layerEnv);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003189
Jon Ashburn5b25d9f2016-01-04 14:01:38 -07003190 loader_free_getenv(layerEnv);
3191
Jon Ashburn44aed662016-02-02 17:47:28 -07003192 while (name && *name) {
Jon Ashburna2e6efe2015-07-02 14:10:53 -06003193 next = loader_get_next_path(name);
Jon Ashburna0eecb22016-02-11 18:59:43 -07003194 if (!strcmp(std_validation_str, name)) {
3195 /* add meta list of layers
3196 don't attempt to remove duplicate layers already added by app or
3197 env var
3198 */
3199 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3200 "Expanding meta layer %s found in environment variable",
3201 std_validation_str);
3202 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3203 sizeof(std_validation_names[0]);
3204 i++) {
3205 loader_find_layer_name_add_list(inst, std_validation_names[i],
3206 type, search_list, layer_list);
3207 }
3208 } else {
3209 loader_find_layer_name_add_list(inst, name, type, search_list,
3210 layer_list);
3211 }
Jon Ashburna2e6efe2015-07-02 14:10:53 -06003212 name = next;
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07003213 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003214
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003215 return;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003216}
3217
Jon Ashburn44aed662016-02-02 17:47:28 -07003218void loader_deactivate_instance_layers(struct loader_instance *instance) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003219 /* Create instance chain of enabled layers */
Courtney Goeltzenleuchter3d8dc1f2015-06-08 15:09:22 -06003220 for (uint32_t i = 0; i < instance->activated_layer_list.count; i++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003221 struct loader_layer_properties *layer_prop =
3222 &instance->activated_layer_list.list[i];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003223
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06003224 loader_remove_layer_lib(instance, layer_prop);
Jon Ashburn183dfd02014-10-22 18:13:16 -06003225 }
Jon Ashburne58f1a32015-08-28 13:38:21 -06003226 loader_destroy_layer_list(instance, &instance->activated_layer_list);
Jon Ashburn183dfd02014-10-22 18:13:16 -06003227}
3228
Jon Ashburn44aed662016-02-02 17:47:28 -07003229VkResult
3230loader_enable_instance_layers(struct loader_instance *inst,
3231 const VkInstanceCreateInfo *pCreateInfo,
3232 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003233 VkResult err;
3234
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06003235 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003236
Jon Ashburne58f1a32015-08-28 13:38:21 -06003237 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003238 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3239 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003240 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn60378412015-07-02 12:59:25 -06003241 }
3242
Jon Ashburn535bd002015-07-02 16:10:32 -06003243 /* Add any implicit layers first */
Jon Ashburn44aed662016-02-02 17:47:28 -07003244 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3245 &inst->activated_layer_list, instance_layers);
Jon Ashburn535bd002015-07-02 16:10:32 -06003246
Jon Ashburn1b111de2015-07-06 15:40:35 -06003247 /* Add any layers specified via environment variable next */
Jon Ashburn44aed662016-02-02 17:47:28 -07003248 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3249 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003250 instance_layers);
Jon Ashburn60378412015-07-02 12:59:25 -06003251
3252 /* Add layers specified by the application */
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003253 err = loader_add_layer_names_to_list(
Jon Ashburn44aed662016-02-02 17:47:28 -07003254 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3255 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003256
3257 return err;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003258}
3259
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003260/*
3261 * Given the list of layers to activate in the loader_instance
3262 * structure. This function will add a VkLayerInstanceCreateInfo
3263 * structure to the VkInstanceCreateInfo.pNext pointer.
3264 * Each activated layer will have it's own VkLayerInstanceLink
3265 * structure that tells the layer what Get*ProcAddr to call to
3266 * get function pointers to the next layer down.
3267 * Once the chain info has been created this function will
3268 * execute the CreateInstance call chain. Each layer will
3269 * then have an opportunity in it's CreateInstance function
3270 * to setup it's dispatch table when the lower layer returns
3271 * successfully.
3272 * Each layer can wrap or not-wrap the returned VkInstance object
3273 * as it sees fit.
3274 * The instance chain is terminated by a loader function
3275 * that will call CreateInstance on all available ICD's and
3276 * cache those VkInstance objects for future use.
3277 */
3278VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn44aed662016-02-02 17:47:28 -07003279 const VkAllocationCallbacks *pAllocator,
Jon Ashburne44367a2016-01-20 08:08:25 -07003280 struct loader_instance *inst,
Jon Ashburna4b34942016-02-03 12:37:30 -07003281 VkInstance *created_instance) {
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003282 uint32_t activated_layers = 0;
3283 VkLayerInstanceCreateInfo chain_info;
3284 VkLayerInstanceLink *layer_instance_link_info = NULL;
3285 VkInstanceCreateInfo loader_create_info;
3286 VkResult res;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003287
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003288 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3289 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburnfce93d92015-05-12 17:26:48 -06003290
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003291 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburnfce93d92015-05-12 17:26:48 -06003292
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003293 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003294
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003295 chain_info.u.pLayerInfo = NULL;
3296 chain_info.pNext = pCreateInfo->pNext;
3297 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3298 chain_info.function = VK_LAYER_LINK_INFO;
3299 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003300
Jon Ashburn44aed662016-02-02 17:47:28 -07003301 layer_instance_link_info = loader_stack_alloc(
3302 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003303 if (!layer_instance_link_info) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003304 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3305 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003306 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnfce93d92015-05-12 17:26:48 -06003307 }
3308
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003309 /* Create instance chain of enabled layers */
3310 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003311 struct loader_layer_properties *layer_prop =
3312 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003313 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003314
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003315 lib_handle = loader_add_layer_lib(inst, "instance", layer_prop);
3316 if (!lib_handle)
Courtney Goeltzenleuchter5e5065e2016-01-14 16:06:06 -07003317 continue;
Jon Ashburn44aed662016-02-02 17:47:28 -07003318 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3319 NULL) {
3320 if (layer_prop->functions.str_gipa == NULL ||
3321 strlen(layer_prop->functions.str_gipa) == 0) {
3322 fpGIPA = (PFN_vkGetInstanceProcAddr)
3323 loader_platform_get_proc_address(
3324 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003325 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3326 } else
Jon Ashburn44aed662016-02-02 17:47:28 -07003327 fpGIPA = (PFN_vkGetInstanceProcAddr)
3328 loader_platform_get_proc_address(
3329 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003330 if (!fpGIPA) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003331 loader_log(
3332 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3333 "Failed to find vkGetInstanceProcAddr in layer %s",
3334 layer_prop->lib_name);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003335 continue;
3336 }
3337 }
3338
Jon Ashburn44aed662016-02-02 17:47:28 -07003339 layer_instance_link_info[activated_layers].pNext =
3340 chain_info.u.pLayerInfo;
3341 layer_instance_link_info[activated_layers]
3342 .pfnNextGetInstanceProcAddr = nextGIPA;
3343 chain_info.u.pLayerInfo =
3344 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003345 nextGIPA = fpGIPA;
3346
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07003347 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07003348 "Insert instance layer %s (%s)",
3349 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003350
3351 activated_layers++;
3352 }
Jon Ashburnfce93d92015-05-12 17:26:48 -06003353 }
3354
Jon Ashburn44aed662016-02-02 17:47:28 -07003355 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburna4b34942016-02-03 12:37:30 -07003356 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003357 if (fpCreateInstance) {
Jon Ashburn7ec8ec82016-03-29 11:16:01 -06003358 VkLayerInstanceCreateInfo create_info_disp;
3359
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003360 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburn94d58ea2016-03-31 10:52:22 -06003361 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburn7ec8ec82016-03-29 11:16:01 -06003362
3363 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3364
3365 create_info_disp.pNext = loader_create_info.pNext;
3366 loader_create_info.pNext = &create_info_disp;
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07003367 res =
3368 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003369 } else {
3370 // Couldn't find CreateInstance function!
3371 res = VK_ERROR_INITIALIZATION_FAILED;
3372 }
3373
3374 if (res != VK_SUCCESS) {
3375 // TODO: Need to clean up here
3376 } else {
Jon Ashburn44aed662016-02-02 17:47:28 -07003377 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburna4b34942016-02-03 12:37:30 -07003378 *created_instance);
Jon Ashburn48cd3ae2016-03-08 15:21:30 -07003379 inst->instance = *created_instance;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003380 }
3381
3382 return res;
Jon Ashburnfce93d92015-05-12 17:26:48 -06003383}
3384
Jon Ashburn44aed662016-02-02 17:47:28 -07003385void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3386 VkInstance created_inst) {
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003387
Jon Ashburn44aed662016-02-02 17:47:28 -07003388 loader_init_instance_extension_dispatch_table(
3389 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003390}
3391
Jon Ashburna9c4a572016-02-26 13:14:27 -07003392VkResult
Jon Ashburn44aed662016-02-02 17:47:28 -07003393loader_enable_device_layers(const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07003394 struct loader_layer_list *activated_layer_list,
3395 const VkDeviceCreateInfo *pCreateInfo,
3396 const struct loader_layer_list *device_layers)
Jon Ashburn182b8302015-08-11 14:49:54 -06003397
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003398{
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003399 VkResult err;
3400
Jon Ashburn6c2fa492016-01-13 12:51:43 -07003401 assert(activated_layer_list && "Cannot have null output layer list");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003402
Jon Ashburn44aed662016-02-02 17:47:28 -07003403 if (activated_layer_list->list == NULL ||
3404 activated_layer_list->capacity == 0) {
Jon Ashburn6c2fa492016-01-13 12:51:43 -07003405 loader_init_layer_list(inst, activated_layer_list);
Jon Ashburn60378412015-07-02 12:59:25 -06003406 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003407
Jon Ashburn6c2fa492016-01-13 12:51:43 -07003408 if (activated_layer_list->list == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003409 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3410 "Failed to alloc device activated layer list");
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003411 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn60378412015-07-02 12:59:25 -06003412 }
3413
Jon Ashburn535bd002015-07-02 16:10:32 -06003414 /* Add any implicit layers first */
Jon Ashburn44aed662016-02-02 17:47:28 -07003415 loader_add_layer_implicit(inst, VK_LAYER_TYPE_DEVICE_IMPLICIT,
3416 activated_layer_list, device_layers);
Jon Ashburn535bd002015-07-02 16:10:32 -06003417
3418 /* Add any layers specified via environment variable next */
Jon Ashburn44aed662016-02-02 17:47:28 -07003419 loader_add_layer_env(inst, VK_LAYER_TYPE_DEVICE_EXPLICIT,
3420 "VK_DEVICE_LAYERS", activated_layer_list,
3421 device_layers);
Jon Ashburn60378412015-07-02 12:59:25 -06003422
3423 /* Add layers specified by the application */
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003424 err = loader_add_layer_names_to_list(
Jon Ashburn44aed662016-02-02 17:47:28 -07003425 inst, activated_layer_list, pCreateInfo->enabledLayerCount,
3426 pCreateInfo->ppEnabledLayerNames, device_layers);
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003427
3428 return err;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003429}
3430
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003431VkResult
3432loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3433 const VkDeviceCreateInfo *pCreateInfo,
3434 const VkAllocationCallbacks *pAllocator,
3435 const struct loader_instance *inst,
3436 struct loader_device *dev) {
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003437 uint32_t activated_layers = 0;
3438 VkLayerDeviceLink *layer_device_link_info;
3439 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003440 VkDeviceCreateInfo loader_create_info;
3441 VkResult res;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06003442
Piers Daniellfc500412016-03-29 11:51:11 -06003443 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003444 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06003445
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003446 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3447
Jon Ashburn44aed662016-02-02 17:47:28 -07003448 layer_device_link_info = loader_stack_alloc(
3449 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003450 if (!layer_device_link_info) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003451 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3452 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003453 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedo0fab78b2015-06-24 15:29:18 -06003454 }
Jon Ashburn612539f2015-06-10 10:13:10 -06003455
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003456 if (dev->activated_layer_list.count > 0) {
Jon Ashburn1d41bc52016-03-29 12:52:13 -06003457 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3458 chain_info.function = VK_LAYER_LINK_INFO;
3459 chain_info.u.pLayerInfo = NULL;
3460 chain_info.pNext = pCreateInfo->pNext;
3461 loader_create_info.pNext = &chain_info;
3462
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003463 /* Create instance chain of enabled layers */
3464 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003465 struct loader_layer_properties *layer_prop =
3466 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003467 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06003468
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003469 lib_handle = loader_add_layer_lib(inst, "device", layer_prop);
3470 if (!lib_handle)
Courtney Goeltzenleuchter5e5065e2016-01-14 16:06:06 -07003471 continue;
Jon Ashburn44aed662016-02-02 17:47:28 -07003472 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3473 NULL) {
3474 if (layer_prop->functions.str_gipa == NULL ||
3475 strlen(layer_prop->functions.str_gipa) == 0) {
3476 fpGIPA = (PFN_vkGetInstanceProcAddr)
3477 loader_platform_get_proc_address(
3478 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003479 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3480 } else
Jon Ashburn44aed662016-02-02 17:47:28 -07003481 fpGIPA = (PFN_vkGetInstanceProcAddr)
3482 loader_platform_get_proc_address(
3483 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003484 if (!fpGIPA) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003485 loader_log(
3486 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3487 "Failed to find vkGetInstanceProcAddr in layer %s",
3488 layer_prop->lib_name);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003489 continue;
3490 }
Jon Ashburn2db08042015-09-09 11:29:24 -06003491 }
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003492 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003493 if (layer_prop->functions.str_gdpa == NULL ||
3494 strlen(layer_prop->functions.str_gdpa) == 0) {
3495 fpGDPA = (PFN_vkGetDeviceProcAddr)
3496 loader_platform_get_proc_address(lib_handle,
3497 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003498 layer_prop->functions.get_device_proc_addr = fpGDPA;
3499 } else
Jon Ashburn44aed662016-02-02 17:47:28 -07003500 fpGDPA = (PFN_vkGetDeviceProcAddr)
3501 loader_platform_get_proc_address(
3502 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003503 if (!fpGDPA) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003504 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3505 "Failed to find vkGetDeviceProcAddr in layer %s",
3506 layer_prop->lib_name);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003507 continue;
3508 }
3509 }
3510
Jon Ashburn44aed662016-02-02 17:47:28 -07003511 layer_device_link_info[activated_layers].pNext =
3512 chain_info.u.pLayerInfo;
3513 layer_device_link_info[activated_layers]
3514 .pfnNextGetInstanceProcAddr = nextGIPA;
3515 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3516 nextGDPA;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003517 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3518 nextGIPA = fpGIPA;
3519 nextGDPA = fpGDPA;
3520
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07003521 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003522 "Insert device layer %s (%s)",
Jon Ashburn44aed662016-02-02 17:47:28 -07003523 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003524
3525 activated_layers++;
Jon Ashburn612539f2015-06-10 10:13:10 -06003526 }
Jon Ashburn612539f2015-06-10 10:13:10 -06003527 }
3528
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003529 VkDevice created_device = (VkDevice)dev;
Jon Ashburn44aed662016-02-02 17:47:28 -07003530 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn48cd3ae2016-03-08 15:21:30 -07003531 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003532 if (fpCreateDevice) {
Jon Ashburn94d58ea2016-03-31 10:52:22 -06003533 VkLayerDeviceCreateInfo create_info_disp;
3534
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003535 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburn94d58ea2016-03-31 10:52:22 -06003536 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3537
3538 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3539
3540 create_info_disp.pNext = loader_create_info.pNext;
3541 loader_create_info.pNext = &create_info_disp;
Jon Ashburn87660432016-03-01 19:51:07 -07003542 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn1d41bc52016-03-29 12:52:13 -06003543 &created_device);
Piers Danielle4353972016-04-05 17:28:06 -06003544 if (res != VK_SUCCESS) {
3545 return res;
3546 }
Jon Ashburn1d41bc52016-03-29 12:52:13 -06003547 dev->device = created_device;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003548 } else {
3549 // Couldn't find CreateDevice function!
3550 return VK_ERROR_INITIALIZATION_FAILED;
3551 }
Jon Ashburn612539f2015-06-10 10:13:10 -06003552
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003553 /* Initialize device dispatch table */
Jon Ashburn44aed662016-02-02 17:47:28 -07003554 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003555 dev->device);
3556
3557 return res;
Jon Ashburn183dfd02014-10-22 18:13:16 -06003558}
Jon Ashburnd43f9b62014-10-14 19:15:22 -06003559
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003560VkResult loader_validate_layers(const struct loader_instance *inst,
3561 const uint32_t layer_count,
Jon Ashburn44aed662016-02-02 17:47:28 -07003562 const char *const *ppEnabledLayerNames,
3563 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter5d9f29b2015-07-06 17:45:08 -06003564 struct loader_layer_properties *prop;
3565
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003566 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07003567 VkStringErrorFlags result =
3568 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003569 if (result != VK_STRING_ERROR_NONE) {
3570 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07003571 "Loader: Device ppEnabledLayerNames contains string "
3572 "that is too long or is badly formed");
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003573 return VK_ERROR_LAYER_NOT_PRESENT;
3574 }
3575
Jon Ashburn44aed662016-02-02 17:47:28 -07003576 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter5d9f29b2015-07-06 17:45:08 -06003577 if (!prop) {
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06003578 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter5d9f29b2015-07-06 17:45:08 -06003579 }
3580 }
Courtney Goeltzenleuchter5d9f29b2015-07-06 17:45:08 -06003581 return VK_SUCCESS;
3582}
3583
3584VkResult loader_validate_instance_extensions(
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003585 const struct loader_instance *inst,
Jon Ashburn44aed662016-02-02 17:47:28 -07003586 const struct loader_extension_list *icd_exts,
3587 const struct loader_layer_list *instance_layer,
3588 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003589
Jon Ashburnc4748dc2015-08-04 11:14:18 -06003590 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003591 struct loader_layer_properties *layer_prop;
3592
Jon Ashburna4ae48b2016-01-11 13:12:43 -07003593 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07003594 VkStringErrorFlags result = vk_string_validate(
3595 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003596 if (result != VK_STRING_ERROR_NONE) {
3597 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07003598 "Loader: Instance ppEnabledExtensionNames contains "
3599 "string that is too long or is badly formed");
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003600 return VK_ERROR_EXTENSION_NOT_PRESENT;
3601 }
3602
Jon Ashburn44aed662016-02-02 17:47:28 -07003603 extension_prop = get_extension_property(
3604 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003605
3606 if (extension_prop) {
3607 continue;
3608 }
3609
3610 extension_prop = NULL;
3611
3612 /* Not in global list, search layer extension lists */
Jon Ashburna4ae48b2016-01-11 13:12:43 -07003613 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003614 layer_prop = loader_get_layer_property(
3615 pCreateInfo->ppEnabledLayerNames[i], instance_layer);
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003616 if (!layer_prop) {
Courtney Goeltzenleuchter91371ff2015-07-06 20:46:50 -06003617 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003618 * should have already filtered this case out.
3619 */
3620 continue;
3621 }
3622
Jon Ashburn44aed662016-02-02 17:47:28 -07003623 extension_prop =
3624 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
3625 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003626 if (extension_prop) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003627 /* Found the extension in one of the layers enabled by the app.
3628 */
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003629 break;
3630 }
3631 }
3632
3633 if (!extension_prop) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003634 /* Didn't find extension name in any of the global layers, error out
3635 */
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06003636 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003637 }
3638 }
3639 return VK_SUCCESS;
3640}
3641
3642VkResult loader_validate_device_extensions(
Jon Ashburn3c4a1142016-03-24 15:49:57 -06003643 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn44aed662016-02-02 17:47:28 -07003644 const struct loader_layer_list *activated_device_layers,
Jon Ashburn87660432016-03-01 19:51:07 -07003645 const struct loader_extension_list *icd_exts,
Jon Ashburn44aed662016-02-02 17:47:28 -07003646 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburnc4748dc2015-08-04 11:14:18 -06003647 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003648 struct loader_layer_properties *layer_prop;
3649
Jon Ashburna4ae48b2016-01-11 13:12:43 -07003650 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003651
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07003652 VkStringErrorFlags result = vk_string_validate(
3653 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003654 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003655 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
3656 0, "Loader: Device ppEnabledExtensionNames contains "
3657 "string that is too long or is badly formed");
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07003658 return VK_ERROR_EXTENSION_NOT_PRESENT;
3659 }
3660
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003661 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn87660432016-03-01 19:51:07 -07003662 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003663
3664 if (extension_prop) {
3665 continue;
3666 }
3667
Jon Ashburn6c2fa492016-01-13 12:51:43 -07003668 /* Not in global list, search activated layer extension lists */
3669 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
3670 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003671
Jon Ashburn44aed662016-02-02 17:47:28 -07003672 extension_prop = get_dev_extension_property(
3673 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003674 if (extension_prop) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003675 /* Found the extension in one of the layers enabled by the app.
3676 */
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003677 break;
3678 }
3679 }
3680
3681 if (!extension_prop) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003682 /* Didn't find extension name in any of the device layers, error out
3683 */
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06003684 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003685 }
3686 }
Courtney Goeltzenleuchter5d9f29b2015-07-06 17:45:08 -06003687 return VK_SUCCESS;
3688}
3689
Jon Ashburna9c4a572016-02-26 13:14:27 -07003690/**
3691 * Terminator functions for the Instance chain
3692 * All named terminator_<Vulakn API name>
3693 */
Jon Ashburn44aed662016-02-02 17:47:28 -07003694VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburna9c4a572016-02-26 13:14:27 -07003695terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
3696 const VkAllocationCallbacks *pAllocator,
3697 VkInstance *pInstance) {
Jon Ashburn3336df82015-01-29 15:45:51 -07003698 struct loader_icd *icd;
Jon Ashburnc4748dc2015-08-04 11:14:18 -06003699 VkExtensionProperties *prop;
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003700 char **filtered_extension_names = NULL;
3701 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003702 VkResult res = VK_SUCCESS;
Jon Ashburn2869bc72015-11-19 09:29:51 -07003703 bool success = false;
Jon Ashburn349508d2015-01-26 14:51:40 -07003704
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003705 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour809d5d32015-12-04 13:24:39 -07003706 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
3707
Jon Ashburna4ae48b2016-01-11 13:12:43 -07003708 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003709 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003710
Courtney Goeltzenleuchterf5f244d2016-01-15 14:15:00 -07003711 // strip off the VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO entries
3712 icd_create_info.pNext = loader_strip_create_extensions(pCreateInfo->pNext);
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07003713
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003714 /*
3715 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchtera17697f2015-07-06 20:14:18 -06003716 * supported by the ICD.
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003717 * No ICD will advertise support for layers. An ICD
3718 * library could support a layer, but it would be
3719 * independent of the actual ICD, just in the same library.
3720 */
Jon Ashburn44aed662016-02-02 17:47:28 -07003721 filtered_extension_names =
3722 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003723 if (!filtered_extension_names) {
3724 return VK_ERROR_OUT_OF_HOST_MEMORY;
3725 }
Jon Ashburn44aed662016-02-02 17:47:28 -07003726 icd_create_info.ppEnabledExtensionNames =
3727 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003728
Jon Ashburn754f1992015-08-18 18:04:47 -06003729 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
3730 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Jon Ashburn3336df82015-01-29 15:45:51 -07003731 if (icd) {
Jon Ashburna4ae48b2016-01-11 13:12:43 -07003732 icd_create_info.enabledExtensionCount = 0;
Courtney Goeltzenleuchterf843d2d2015-12-21 16:41:47 -07003733 struct loader_extension_list icd_exts;
3734
Jon Ashburn44aed662016-02-02 17:47:28 -07003735 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3736 "Build ICD instance extension list");
3737 // traverse scanned icd list adding non-duplicate extensions to the
3738 // list
3739 loader_init_generic_list(ptr_instance,
3740 (struct loader_generic_list *)&icd_exts,
Courtney Goeltzenleuchterf843d2d2015-12-21 16:41:47 -07003741 sizeof(VkExtensionProperties));
Jon Ashburn44aed662016-02-02 17:47:28 -07003742 loader_add_instance_extensions(
3743 ptr_instance,
3744 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
3745 icd->this_icd_lib->lib_name, &icd_exts);
Courtney Goeltzenleuchterf843d2d2015-12-21 16:41:47 -07003746
Karl Schultzc50a5982016-02-24 14:39:39 -07003747 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003748 prop = get_extension_property(
Karl Schultzc50a5982016-02-24 14:39:39 -07003749 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003750 if (prop) {
Jon Ashburn44aed662016-02-02 17:47:28 -07003751 filtered_extension_names[icd_create_info
3752 .enabledExtensionCount] =
Karl Schultzc50a5982016-02-24 14:39:39 -07003753 (char *)pCreateInfo->ppEnabledExtensionNames[j];
Jon Ashburna4ae48b2016-01-11 13:12:43 -07003754 icd_create_info.enabledExtensionCount++;
Courtney Goeltzenleuchterab27f462015-07-06 17:42:01 -06003755 }
3756 }
3757
Jon Ashburn44aed662016-02-02 17:47:28 -07003758 loader_destroy_generic_list(
3759 ptr_instance, (struct loader_generic_list *)&icd_exts);
Courtney Goeltzenleuchterf843d2d2015-12-21 16:41:47 -07003760
Jon Ashburn44aed662016-02-02 17:47:28 -07003761 res = ptr_instance->icd_libs.list[i].CreateInstance(
3762 &icd_create_info, pAllocator, &(icd->instance));
Jon Ashburn2869bc72015-11-19 09:29:51 -07003763 if (res == VK_SUCCESS)
3764 success = loader_icd_init_entrys(
Jon Ashburn44aed662016-02-02 17:47:28 -07003765 icd, icd->instance,
3766 ptr_instance->icd_libs.list[i].GetInstanceProcAddr);
Jon Ashburnce7e3d92015-07-16 10:17:29 -06003767
Jon Ashburn44aed662016-02-02 17:47:28 -07003768 if (res != VK_SUCCESS || !success) {
Jon Ashburn3336df82015-01-29 15:45:51 -07003769 ptr_instance->icds = ptr_instance->icds->next;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06003770 loader_icd_destroy(ptr_instance, icd);
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07003771 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn44aed662016-02-02 17:47:28 -07003772 "ICD ignored: failed to CreateInstance and find "
3773 "entrypoints with ICD");
Jon Ashburn3336df82015-01-29 15:45:51 -07003774 }
3775 }
Jon Ashburn3336df82015-01-29 15:45:51 -07003776 }
Jon Ashburn349508d2015-01-26 14:51:40 -07003777
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003778 /*
3779 * If no ICDs were added to instance list and res is unchanged
3780 * from it's initial value, the loader was unable to find
3781 * a suitable ICD.
3782 */
Ian Elliott617fdec2015-02-05 15:19:15 -07003783 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003784 if (res == VK_SUCCESS) {
3785 return VK_ERROR_INCOMPATIBLE_DRIVER;
3786 } else {
3787 return res;
3788 }
Ian Elliott617fdec2015-02-05 15:19:15 -07003789 }
Jon Ashburn3336df82015-01-29 15:45:51 -07003790
Courtney Goeltzenleuchter1fcceeb2015-07-06 09:06:34 -06003791 return VK_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -07003792}
3793
Jon Ashburn44aed662016-02-02 17:47:28 -07003794VKAPI_ATTR void VKAPI_CALL
Jon Ashburna9c4a572016-02-26 13:14:27 -07003795terminator_DestroyInstance(VkInstance instance,
3796 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchter8afefb52015-06-08 15:04:02 -06003797 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn0dd356d2015-05-14 12:43:38 -06003798 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburn16a16d62015-06-16 14:43:19 -06003799 struct loader_icd *next_icd;
Jon Ashburn349508d2015-01-26 14:51:40 -07003800
3801 // Remove this instance from the list of instances:
3802 struct loader_instance *prev = NULL;
3803 struct loader_instance *next = loader.instances;
3804 while (next != NULL) {
3805 if (next == ptr_instance) {
3806 // Remove this instance from the list:
3807 if (prev)
3808 prev->next = next->next;
Jon Ashburn2cabd252015-02-03 09:26:59 -07003809 else
3810 loader.instances = next->next;
Jon Ashburn349508d2015-01-26 14:51:40 -07003811 break;
3812 }
3813 prev = next;
3814 next = next->next;
3815 }
Jon Ashburn349508d2015-01-26 14:51:40 -07003816
Jon Ashburn0dd356d2015-05-14 12:43:38 -06003817 while (icds) {
3818 if (icds->instance) {
Chia-I Wu69f40122015-10-26 21:10:41 +08003819 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbour22a30862015-04-22 09:02:32 -06003820 }
Jon Ashburn16a16d62015-06-16 14:43:19 -06003821 next_icd = icds->next;
Jon Ashburn0dd356d2015-05-14 12:43:38 -06003822 icds->instance = VK_NULL_HANDLE;
Jon Ashburn16a16d62015-06-16 14:43:19 -06003823 loader_icd_destroy(ptr_instance, icds);
3824
3825 icds = next_icd;
Jon Ashburn3336df82015-01-29 15:45:51 -07003826 }
Jon Ashburn44aed662016-02-02 17:47:28 -07003827 loader_delete_layer_properties(ptr_instance,
3828 &ptr_instance->device_layer_list);
3829 loader_delete_layer_properties(ptr_instance,
3830 &ptr_instance->instance_layer_list);
Jon Ashburne58f1a32015-08-28 13:38:21 -06003831 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn44aed662016-02-02 17:47:28 -07003832 loader_destroy_generic_list(
3833 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn87660432016-03-01 19:51:07 -07003834 if (ptr_instance->phys_devs_term)
3835 loader_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburn429e19f2015-11-17 15:31:02 -07003836 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn349508d2015-01-26 14:51:40 -07003837}
3838
Jon Ashburna9c4a572016-02-26 13:14:27 -07003839VKAPI_ATTR VkResult VKAPI_CALL
3840terminator_CreateDevice(VkPhysicalDevice physicalDevice,
3841 const VkDeviceCreateInfo *pCreateInfo,
3842 const VkAllocationCallbacks *pAllocator,
3843 VkDevice *pDevice) {
3844 struct loader_physical_device *phys_dev;
Jon Ashburn87660432016-03-01 19:51:07 -07003845 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07003846
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003847 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn1d41bc52016-03-29 12:52:13 -06003848 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003849
Jon Ashburna9c4a572016-02-26 13:14:27 -07003850 if (fpCreateDevice == NULL) {
3851 return VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003852 }
3853
Jon Ashburna9c4a572016-02-26 13:14:27 -07003854 VkDeviceCreateInfo localCreateInfo;
3855 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
3856 localCreateInfo.pNext = loader_strip_create_extensions(pCreateInfo->pNext);
3857
3858 /*
3859 * NOTE: Need to filter the extensions to only those
3860 * supported by the ICD.
3861 * No ICD will advertise support for layers. An ICD
3862 * library could support a layer, but it would be
3863 * independent of the actual ICD, just in the same library.
3864 */
3865 char **filtered_extension_names = NULL;
3866 filtered_extension_names =
3867 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
3868 if (!filtered_extension_names) {
Jon Ashburn251258f2015-11-01 14:04:06 -07003869 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003870 }
3871
Jon Ashburna9c4a572016-02-26 13:14:27 -07003872 localCreateInfo.enabledLayerCount = 0;
3873 localCreateInfo.ppEnabledLayerNames = NULL;
3874
3875 localCreateInfo.enabledExtensionCount = 0;
3876 localCreateInfo.ppEnabledExtensionNames =
3877 (const char *const *)filtered_extension_names;
3878
Jon Ashburn87660432016-03-01 19:51:07 -07003879 /* Get the physical device (ICD) extensions */
3880 struct loader_extension_list icd_exts;
3881 VkResult res;
3882 if (!loader_init_generic_list(phys_dev->this_icd->this_instance,
3883 (struct loader_generic_list *)&icd_exts,
3884 sizeof(VkExtensionProperties))) {
3885 return VK_ERROR_OUT_OF_HOST_MEMORY;
3886 }
3887
3888 res = loader_add_device_extensions(
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003889 phys_dev->this_icd->this_instance,
3890 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn87660432016-03-01 19:51:07 -07003891 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
3892 &icd_exts);
3893 if (res != VK_SUCCESS) {
3894 return res;
3895 }
3896
Jon Ashburna9c4a572016-02-26 13:14:27 -07003897 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
3898 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn87660432016-03-01 19:51:07 -07003899 VkExtensionProperties *prop =
3900 get_extension_property(extension_name, &icd_exts);
Jon Ashburna9c4a572016-02-26 13:14:27 -07003901 if (prop) {
3902 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
3903 (char *)extension_name;
3904 localCreateInfo.enabledExtensionCount++;
3905 }
3906 }
3907
Jon Ashburna9c4a572016-02-26 13:14:27 -07003908 // TODO: Why does fpCreateDevice behave differently than
3909 // this_icd->CreateDevice?
3910 // VkResult res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo,
3911 // pAllocator, &localDevice);
Jon Ashburn87660432016-03-01 19:51:07 -07003912 res = phys_dev->this_icd->CreateDevice(phys_dev->phys_dev, &localCreateInfo,
Jon Ashburn0c24d892016-04-01 11:49:39 -06003913 pAllocator, &dev->device);
Jon Ashburna9c4a572016-02-26 13:14:27 -07003914
3915 if (res != VK_SUCCESS) {
3916 return res;
3917 }
3918
Jon Ashburn0c24d892016-04-01 11:49:39 -06003919 *pDevice = dev->device;
Jon Ashburn9b2a8c92016-04-15 09:25:03 -06003920 loader_add_logical_device(phys_dev->this_icd->this_instance,
3921 phys_dev->this_icd, dev);
Jon Ashburna9c4a572016-02-26 13:14:27 -07003922
3923 /* Init dispatch pointer in new device object */
3924 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
3925
3926 return res;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003927}
3928
Jon Ashburn44aed662016-02-02 17:47:28 -07003929VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburna9c4a572016-02-26 13:14:27 -07003930terminator_EnumeratePhysicalDevices(VkInstance instance,
3931 uint32_t *pPhysicalDeviceCount,
3932 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn251258f2015-11-01 14:04:06 -07003933 uint32_t i;
Jon Ashburn87660432016-03-01 19:51:07 -07003934 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn251258f2015-11-01 14:04:06 -07003935 VkResult res = VK_SUCCESS;
Jon Ashburnb048a9b2015-01-28 19:57:09 -07003936
Jon Ashburn87660432016-03-01 19:51:07 -07003937 struct loader_icd *icd;
3938 struct loader_phys_dev_per_icd *phys_devs;
3939
3940 inst->total_gpu_count = 0;
3941 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
3942 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
3943 if (!phys_devs)
3944 return VK_ERROR_OUT_OF_HOST_MEMORY;
3945
3946 icd = inst->icds;
3947 for (i = 0; i < inst->total_icd_count; i++) {
3948 assert(icd);
3949 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
3950 NULL);
3951 if (res != VK_SUCCESS)
3952 return res;
3953 icd = icd->next;
Jon Ashburnb048a9b2015-01-28 19:57:09 -07003954 }
3955
Jon Ashburn87660432016-03-01 19:51:07 -07003956 icd = inst->icds;
3957 for (i = 0; i < inst->total_icd_count; i++) {
3958 assert(icd);
3959 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
3960 phys_devs[i].count * sizeof(VkPhysicalDevice));
3961 if (!phys_devs[i].phys_devs) {
3962 return VK_ERROR_OUT_OF_HOST_MEMORY;
3963 }
3964 res = icd->EnumeratePhysicalDevices(
3965 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
3966 if ((res == VK_SUCCESS)) {
3967 inst->total_gpu_count += phys_devs[i].count;
3968 } else {
3969 return res;
3970 }
3971 phys_devs[i].this_icd = icd;
3972 icd = icd->next;
3973 }
3974
3975 *pPhysicalDeviceCount = inst->total_gpu_count;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003976 if (!pPhysicalDevices) {
Jon Ashburn251258f2015-11-01 14:04:06 -07003977 return res;
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06003978 }
Jon Ashburnb048a9b2015-01-28 19:57:09 -07003979
Jon Ashburn87660432016-03-01 19:51:07 -07003980 /* Initialize the output pPhysicalDevices with wrapped loader terminator
3981 * physicalDevice objects; save this list of wrapped objects in instance
3982 * struct for later cleanup and use by trampoline code */
3983 uint32_t j, idx = 0;
3984 uint32_t copy_count = 0;
3985
3986 copy_count = (inst->total_gpu_count < *pPhysicalDeviceCount)
3987 ? inst->total_gpu_count
Jon Ashburn44aed662016-02-02 17:47:28 -07003988 : *pPhysicalDeviceCount;
Jon Ashburn87660432016-03-01 19:51:07 -07003989
Jon Ashburn87660432016-03-01 19:51:07 -07003990 if (inst->phys_devs_term)
3991 loader_heap_free(inst, inst->phys_devs_term);
3992 inst->phys_devs_term = loader_heap_alloc(
3993 inst, sizeof(struct loader_physical_device) * copy_count,
3994 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3995 if (!inst->phys_devs_term)
3996 return VK_ERROR_OUT_OF_HOST_MEMORY;
3997
3998 for (i = 0; idx < copy_count && i < inst->total_icd_count; i++) {
Jon Ashburnfc53d902016-03-11 14:43:57 -07003999
Jon Ashburn87660432016-03-01 19:51:07 -07004000 for (j = 0; j < phys_devs[i].count && idx < copy_count; j++) {
4001 loader_set_dispatch((void *)&inst->phys_devs_term[idx], inst->disp);
4002 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
4003 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
4004 pPhysicalDevices[idx] =
4005 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4006 idx++;
4007 }
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -06004008 }
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07004009 *pPhysicalDeviceCount = copy_count;
4010
Piers Daniellfc500412016-03-29 11:51:11 -06004011 // TODO: Is phys_devs being leaked?
4012
Jon Ashburn87660432016-03-01 19:51:07 -07004013 if (copy_count < inst->total_gpu_count) {
4014 inst->total_gpu_count = copy_count;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -07004015 return VK_INCOMPLETE;
4016 }
Jon Ashburn251258f2015-11-01 14:04:06 -07004017 return res;
Jon Ashburn349508d2015-01-26 14:51:40 -07004018}
4019
Jon Ashburna9c4a572016-02-26 13:14:27 -07004020VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4021 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn44aed662016-02-02 17:47:28 -07004022 struct loader_physical_device *phys_dev =
4023 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004024 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn0dd356d2015-05-14 12:43:38 -06004025
Tony Barbour426b9052015-06-24 16:06:58 -06004026 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn251258f2015-11-01 14:04:06 -07004027 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -06004028}
4029
Jon Ashburna9c4a572016-02-26 13:14:27 -07004030VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn44aed662016-02-02 17:47:28 -07004031 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4032 VkQueueFamilyProperties *pProperties) {
4033 struct loader_physical_device *phys_dev =
4034 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004035 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour426b9052015-06-24 16:06:58 -06004036
Cody Northropef72e2a2015-08-03 17:04:53 -06004037 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn44aed662016-02-02 17:47:28 -07004038 icd->GetPhysicalDeviceQueueFamilyProperties(
4039 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -06004040}
4041
Jon Ashburna9c4a572016-02-26 13:14:27 -07004042VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn44aed662016-02-02 17:47:28 -07004043 VkPhysicalDevice physicalDevice,
4044 VkPhysicalDeviceMemoryProperties *pProperties) {
4045 struct loader_physical_device *phys_dev =
4046 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004047 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour426b9052015-06-24 16:06:58 -06004048
4049 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn251258f2015-11-01 14:04:06 -07004050 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn0dd356d2015-05-14 12:43:38 -06004051}
4052
Jon Ashburn44aed662016-02-02 17:47:28 -07004053VKAPI_ATTR void VKAPI_CALL
Jon Ashburna9c4a572016-02-26 13:14:27 -07004054terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4055 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn44aed662016-02-02 17:47:28 -07004056 struct loader_physical_device *phys_dev =
4057 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004058 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesd7576302015-06-21 22:55:02 +12004059
4060 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn251258f2015-11-01 14:04:06 -07004061 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesd7576302015-06-21 22:55:02 +12004062}
4063
Jon Ashburn44aed662016-02-02 17:47:28 -07004064VKAPI_ATTR void VKAPI_CALL
Jon Ashburna9c4a572016-02-26 13:14:27 -07004065terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4066 VkFormat format,
4067 VkFormatProperties *pFormatInfo) {
Jon Ashburn44aed662016-02-02 17:47:28 -07004068 struct loader_physical_device *phys_dev =
4069 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004070 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesd7576302015-06-21 22:55:02 +12004071
Courtney Goeltzenleuchter4da96aa2015-07-12 12:52:09 -06004072 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn44aed662016-02-02 17:47:28 -07004073 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4074 pFormatInfo);
Chris Forbesd7576302015-06-21 22:55:02 +12004075}
4076
Jon Ashburna9c4a572016-02-26 13:14:27 -07004077VKAPI_ATTR VkResult VKAPI_CALL
4078terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn44aed662016-02-02 17:47:28 -07004079 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4080 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4081 VkImageFormatProperties *pImageFormatProperties) {
4082 struct loader_physical_device *phys_dev =
4083 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004084 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn4e189562015-07-23 18:49:07 -06004085
Chia-I Wu5202c542015-10-31 00:31:16 +08004086 if (!icd->GetPhysicalDeviceImageFormatProperties)
4087 return VK_ERROR_INITIALIZATION_FAILED;
4088
Jon Ashburn44aed662016-02-02 17:47:28 -07004089 return icd->GetPhysicalDeviceImageFormatProperties(
4090 phys_dev->phys_dev, format, type, tiling, usage, flags,
4091 pImageFormatProperties);
Jon Ashburn4e189562015-07-23 18:49:07 -06004092}
4093
Jon Ashburna9c4a572016-02-26 13:14:27 -07004094VKAPI_ATTR void VKAPI_CALL
4095terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn44aed662016-02-02 17:47:28 -07004096 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4097 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4098 VkImageTiling tiling, uint32_t *pNumProperties,
4099 VkSparseImageFormatProperties *pProperties) {
4100 struct loader_physical_device *phys_dev =
4101 (struct loader_physical_device *)physicalDevice;
Jon Ashburn251258f2015-11-01 14:04:06 -07004102 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -06004103
4104 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn44aed662016-02-02 17:47:28 -07004105 icd->GetPhysicalDeviceSparseImageFormatProperties(
4106 phys_dev->phys_dev, format, type, samples, usage, tiling,
4107 pNumProperties, pProperties);
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -06004108}
4109
Jon Ashburna9c4a572016-02-26 13:14:27 -07004110VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4111 VkPhysicalDevice physicalDevice, const char *pLayerName,
4112 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburned158d12015-12-14 08:52:14 -07004113 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07004114
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004115 struct loader_layer_list implicit_layer_list;
4116
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004117 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06004118
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004119 /* Any layer or trampoline wrapping should be removed at this point in time
4120 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn87660432016-03-01 19:51:07 -07004121 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004122
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004123 /* this case is during the call down the instance chain with pLayerName
4124 * == NULL*/
4125 struct loader_icd *icd = phys_dev->this_icd;
4126 uint32_t icd_ext_count = *pPropertyCount;
4127 VkResult res;
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004128
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004129 /* get device extensions */
Jon Ashburn87660432016-03-01 19:51:07 -07004130 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4131 &icd_ext_count, pProperties);
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004132 if (res != VK_SUCCESS)
4133 return res;
Jon Ashburn44aed662016-02-02 17:47:28 -07004134
Jon Ashburn87660432016-03-01 19:51:07 -07004135 loader_init_layer_list(icd->this_instance, &implicit_layer_list);
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004136
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004137 loader_add_layer_implicit(
Jon Ashburn87660432016-03-01 19:51:07 -07004138 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4139 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004140 /* we need to determine which implicit layers are active,
4141 * and then add their extensions. This can't be cached as
4142 * it depends on results of environment variables (which can change).
4143 */
4144 if (pProperties != NULL) {
Jon Ashburn87660432016-03-01 19:51:07 -07004145 struct loader_extension_list icd_exts;
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004146 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn87660432016-03-01 19:51:07 -07004147 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4148 icd_ext_count, pProperties,
4149 &icd_exts);
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004150 if (res != VK_SUCCESS)
4151 return res;
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004152
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004153 /* we need to determine which implicit layers are active,
4154 * and then add their extensions. This can't be cached as
4155 * it depends on results of environment variables (which can
4156 * change).
4157 */
4158 struct loader_extension_list all_exts = {0};
Jon Ashburn87660432016-03-01 19:51:07 -07004159 loader_add_to_ext_list(icd->this_instance, &all_exts, icd_exts.count,
4160 icd_exts.list);
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004161
Jon Ashburn87660432016-03-01 19:51:07 -07004162 loader_init_layer_list(icd->this_instance, &implicit_layer_list);
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004163
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004164 loader_add_layer_implicit(
Jon Ashburn87660432016-03-01 19:51:07 -07004165 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4166 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004167
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004168 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4169 for (uint32_t j = 0;
Jon Ashburn87660432016-03-01 19:51:07 -07004170 j < implicit_layer_list.list[i].device_extension_list.count;
4171 j++) {
4172 loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4173 &implicit_layer_list.list[i]
4174 .device_extension_list.list[j]
4175 .props);
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004176 }
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004177 }
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004178 uint32_t capacity = *pPropertyCount;
4179 VkExtensionProperties *props = pProperties;
Jon Ashburn6c2fa492016-01-13 12:51:43 -07004180
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004181 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4182 props[i] = all_exts.list[i];
4183 }
4184 /* wasn't enough space for the extensions, we did partial copy now
4185 * return VK_INCOMPLETE */
4186 if (capacity < all_exts.count) {
4187 res = VK_INCOMPLETE;
4188 } else {
4189 *pPropertyCount = all_exts.count;
4190 }
Jon Ashburn87660432016-03-01 19:51:07 -07004191 loader_destroy_generic_list(icd->this_instance,
4192 (struct loader_generic_list *)&all_exts);
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004193 } else {
4194 /* just return the count; need to add in the count of implicit layer
4195 * extensions
4196 * don't worry about duplicates being added in the count */
4197 *pPropertyCount = icd_ext_count;
4198
4199 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4200 *pPropertyCount +=
Jon Ashburn87660432016-03-01 19:51:07 -07004201 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004202 }
4203 res = VK_SUCCESS;
Jon Ashburn182b8302015-08-11 14:49:54 -06004204 }
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004205
4206 loader_destroy_generic_list(
Jon Ashburn87660432016-03-01 19:51:07 -07004207 icd->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004208 return res;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06004209}
4210
Jon Ashburn44aed662016-02-02 17:47:28 -07004211VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburna9c4a572016-02-26 13:14:27 -07004212terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4213 uint32_t *pPropertyCount,
4214 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06004215
Jon Ashburn6c2c0612016-02-29 13:00:51 -07004216 // should never get here this call isn't dispatched down the chain
4217 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn2666e2f2015-05-15 15:09:35 -06004218}
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004219
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07004220VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004221 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultzc50a5982016-02-24 14:39:39 -07004222 int num_char_bytes = 0;
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07004223 int i, j;
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004224
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07004225 for (i = 0; i < max_length; i++) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004226 if (utf8[i] == 0) {
4227 break;
Mark Lobodzinski48706352016-02-12 11:30:14 -07004228 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004229 num_char_bytes = 0;
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07004230 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004231 num_char_bytes = 1;
Jon Ashburn7ec85cd2016-02-10 20:50:19 -07004232 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskic882eee2016-02-02 18:53:34 -07004233 num_char_bytes = 2;
4234 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4235 num_char_bytes = 3;
4236 } else {
4237 result = VK_STRING_ERROR_BAD_DATA;
4238 }
4239
4240 // Validate the following num_char_bytes of data
4241 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4242 if (++i == max_length) {
4243 result |= VK_STRING_ERROR_LENGTH;
4244 break;
4245 }
4246 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4247 result |= VK_STRING_ERROR_BAD_DATA;
4248 }
4249 }
4250 }
4251 return result;
4252}