blob: 6893d94cde96f756398d52ce78373cb571cb7eb5 [file] [log] [blame]
Mark Lobodzinski317574e2016-08-29 14:21:14 -06001/*
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002 *
Mark Young39389872017-01-19 21:10:49 -07003 * Copyright (c) 2014-2017 The Khronos Group Inc.
4 * Copyright (c) 2014-2017 Valve Corporation
5 * Copyright (c) 2014-2017 LunarG, Inc.
Courtney Goeltzenleuchterf821dad2015-12-02 14:53:22 -07006 * Copyright (C) 2015 Google Inc.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08007 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06008 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080013 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19
Jon Ashburn23d36b12016-02-02 17:47:28 -070020 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060021 * Author: Jon Ashburn <jon@lunarg.com>
Jon Ashburn23d36b12016-02-02 17:47:28 -070022 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Mark Young39389872017-01-19 21:10:49 -070023 * Author: Mark Young <marky@lunarg.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060024 *
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080025 */
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070026
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060027#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080028#include <stdio.h>
29#include <stdlib.h>
30#include <stdarg.h>
31#include <stdbool.h>
32#include <string.h>
Benjamin Saunders31a48012017-01-29 14:49:54 -080033#include <stddef.h>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080034
Chia-I Wu13a61a52014-08-04 11:18:20 +080035#include <sys/types.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050036#if defined(_WIN32)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070037#include "dirent_on_windows.h"
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070038#else // _WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080039#include <dirent.h>
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070040#endif // _WIN32
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060041#include "vk_loader_platform.h"
Chia-I Wu19300602014-08-04 08:03:57 +080042#include "loader.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060043#include "gpa_helper.h"
44#include "table_ops.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060045#include "debug_report.h"
Ian Elliott954fa342015-10-30 15:28:23 -060046#include "wsi.h"
Mark Lobodzinski317574e2016-08-29 14:21:14 -060047#include "extensions.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070048#include "vulkan/vk_icd.h"
Jon Ashburn2077e382015-06-29 11:25:34 -060049#include "cJSON.h"
Jon Ashburnfc1031e2015-11-17 15:31:02 -070050#include "murmurhash.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080051
Jon Ashburnd8ed7992016-04-04 13:52:53 -060052#if defined(__GNUC__)
Jon Ashburn10b3f832016-05-09 11:31:40 -060053#if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 17))
Jon Ashburncc407a22016-04-15 09:25:03 -060054#define secure_getenv __secure_getenv
55#endif
Jon Ashburnd8ed7992016-04-04 13:52:53 -060056#endif
57
Jon Ashburn27cd5842015-05-12 17:26:48 -060058struct loader_struct loader = {0};
Jon Ashburn87d6aa92015-08-28 15:19:27 -060059// TLS for instance for alloc/free callbacks
60THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080061
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -070062static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap00b4aba2015-09-30 11:50:51 -050063
Jon Ashburn24cd4be2015-11-01 14:04:06 -070064struct loader_phys_dev_per_icd {
65 uint32_t count;
66 VkPhysicalDevice *phys_devs;
Mark Young0153e0b2016-11-03 14:27:13 -060067 struct loader_icd_term *this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -070068};
69
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060070enum loader_debug {
Jon Ashburn23d36b12016-02-02 17:47:28 -070071 LOADER_INFO_BIT = 0x01,
72 LOADER_WARN_BIT = 0x02,
73 LOADER_PERF_BIT = 0x04,
74 LOADER_ERROR_BIT = 0x08,
75 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060076};
77
78uint32_t g_loader_debug = 0;
79uint32_t g_loader_log_msgs = 0;
80
Jon Ashburn23d36b12016-02-02 17:47:28 -070081// thread safety lock for accessing global data structures such as "loader"
Jon Ashburn6301a0f2015-05-29 13:15:39 -060082// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburn2077e382015-06-29 11:25:34 -060083// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburn6301a0f2015-05-29 13:15:39 -060084loader_platform_thread_mutex loader_lock;
Jon Ashburn6461ef22015-09-22 13:11:00 -060085loader_platform_thread_mutex loader_json_lock;
Jon Ashburn6301a0f2015-05-29 13:15:39 -060086
Jon Ashburn86a527a2016-02-10 20:59:26 -070087const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
88
Ian Elliottd3ef02f2015-07-06 14:36:13 -060089// This table contains the loader's instance dispatch table, which contains
90// default functions if no instance layers are activated. This contains
91// pointers to "terminator functions".
Jon Ashburn6301a0f2015-05-29 13:15:39 -060092const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn69e9ea22015-09-28 16:15:00 -060093 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn1530c342016-02-26 13:14:27 -070094 .DestroyInstance = terminator_DestroyInstance,
95 .EnumeratePhysicalDevices = terminator_EnumeratePhysicalDevices,
96 .GetPhysicalDeviceFeatures = terminator_GetPhysicalDeviceFeatures,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070097 .GetPhysicalDeviceFormatProperties = terminator_GetPhysicalDeviceFormatProperties,
98 .GetPhysicalDeviceImageFormatProperties = terminator_GetPhysicalDeviceImageFormatProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -070099 .GetPhysicalDeviceProperties = terminator_GetPhysicalDeviceProperties,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700100 .GetPhysicalDeviceQueueFamilyProperties = terminator_GetPhysicalDeviceQueueFamilyProperties,
101 .GetPhysicalDeviceMemoryProperties = terminator_GetPhysicalDeviceMemoryProperties,
102 .EnumerateDeviceExtensionProperties = terminator_EnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700103 .EnumerateDeviceLayerProperties = terminator_EnumerateDeviceLayerProperties,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700104 .GetPhysicalDeviceSparseImageFormatProperties = terminator_GetPhysicalDeviceSparseImageFormatProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700105 .DestroySurfaceKHR = terminator_DestroySurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700106 .GetPhysicalDeviceSurfaceSupportKHR = terminator_GetPhysicalDeviceSurfaceSupportKHR,
107 .GetPhysicalDeviceSurfaceCapabilitiesKHR = terminator_GetPhysicalDeviceSurfaceCapabilitiesKHR,
108 .GetPhysicalDeviceSurfaceFormatsKHR = terminator_GetPhysicalDeviceSurfaceFormatsKHR,
109 .GetPhysicalDeviceSurfacePresentModesKHR = terminator_GetPhysicalDeviceSurfacePresentModesKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700110#ifdef VK_USE_PLATFORM_MIR_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700111 .CreateMirSurfaceKHR = terminator_CreateMirSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700112 .GetPhysicalDeviceMirPresentationSupportKHR = terminator_GetPhysicalDeviceMirPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700113#endif
114#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700115 .CreateWaylandSurfaceKHR = terminator_CreateWaylandSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700116 .GetPhysicalDeviceWaylandPresentationSupportKHR = terminator_GetPhysicalDeviceWaylandPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700117#endif
118#ifdef VK_USE_PLATFORM_WIN32_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700119 .CreateWin32SurfaceKHR = terminator_CreateWin32SurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700120 .GetPhysicalDeviceWin32PresentationSupportKHR = terminator_GetPhysicalDeviceWin32PresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700121#endif
122#ifdef VK_USE_PLATFORM_XCB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700123 .CreateXcbSurfaceKHR = terminator_CreateXcbSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700124 .GetPhysicalDeviceXcbPresentationSupportKHR = terminator_GetPhysicalDeviceXcbPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700125#endif
126#ifdef VK_USE_PLATFORM_XLIB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700127 .CreateXlibSurfaceKHR = terminator_CreateXlibSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700128 .GetPhysicalDeviceXlibPresentationSupportKHR = terminator_GetPhysicalDeviceXlibPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700129#endif
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700130#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700131 .CreateAndroidSurfaceKHR = terminator_CreateAndroidSurfaceKHR,
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700132#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700133 .GetPhysicalDeviceDisplayPropertiesKHR = terminator_GetPhysicalDeviceDisplayPropertiesKHR,
134 .GetPhysicalDeviceDisplayPlanePropertiesKHR = terminator_GetPhysicalDeviceDisplayPlanePropertiesKHR,
135 .GetDisplayPlaneSupportedDisplaysKHR = terminator_GetDisplayPlaneSupportedDisplaysKHR,
Jon Ashburncc407a22016-04-15 09:25:03 -0600136 .GetDisplayModePropertiesKHR = terminator_GetDisplayModePropertiesKHR,
137 .CreateDisplayModeKHR = terminator_CreateDisplayModeKHR,
138 .GetDisplayPlaneCapabilitiesKHR = terminator_GetDisplayPlaneCapabilitiesKHR,
139 .CreateDisplayPlaneSurfaceKHR = terminator_CreateDisplayPlaneSurfaceKHR,
Mark Youngfa552782016-12-12 16:14:55 -0700140
Mark Young39389872017-01-19 21:10:49 -0700141 // KHR_get_physical_device_properties2
142 .GetPhysicalDeviceFeatures2KHR = terminator_GetPhysicalDeviceFeatures2KHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700143 .GetPhysicalDeviceProperties2KHR = terminator_GetPhysicalDeviceProperties2KHR,
144 .GetPhysicalDeviceFormatProperties2KHR = terminator_GetPhysicalDeviceFormatProperties2KHR,
145 .GetPhysicalDeviceImageFormatProperties2KHR = terminator_GetPhysicalDeviceImageFormatProperties2KHR,
146 .GetPhysicalDeviceQueueFamilyProperties2KHR = terminator_GetPhysicalDeviceQueueFamilyProperties2KHR,
147 .GetPhysicalDeviceMemoryProperties2KHR = terminator_GetPhysicalDeviceMemoryProperties2KHR,
148 .GetPhysicalDeviceSparseImageFormatProperties2KHR = terminator_GetPhysicalDeviceSparseImageFormatProperties2KHR,
Mark Young39389872017-01-19 21:10:49 -0700149
150#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
151 // EXT_acquire_xlib_display
152 .AcquireXlibDisplayEXT = terminator_AcquireXlibDisplayEXT,
153 .GetRandROutputDisplayEXT = terminator_GetRandROutputDisplayEXT,
154#endif
155
156 // EXT_debug_report
157 .CreateDebugReportCallbackEXT = terminator_CreateDebugReportCallback,
158 .DestroyDebugReportCallbackEXT = terminator_DestroyDebugReportCallback,
159 .DebugReportMessageEXT = terminator_DebugReportMessage,
160
161 // EXT_direct_mode_display
162 .ReleaseDisplayEXT = terminator_ReleaseDisplayEXT,
163
164 // EXT_display_surface_counter
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700165 .GetPhysicalDeviceSurfaceCapabilities2EXT = terminator_GetPhysicalDeviceSurfaceCapabilities2EXT,
Mark Young39389872017-01-19 21:10:49 -0700166
167 // NV_external_memory_capabilities
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700168 .GetPhysicalDeviceExternalImageFormatPropertiesNV = terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV,
Mark Young39389872017-01-19 21:10:49 -0700169
Mark Youngfa552782016-12-12 16:14:55 -0700170 // NVX_device_generated_commands
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700171 .GetPhysicalDeviceGeneratedCommandsPropertiesNVX = terminator_GetPhysicalDeviceGeneratedCommandsPropertiesNVX,
Jon Ashburn27cd5842015-05-12 17:26:48 -0600172};
173
Lenny Komow4053b812016-12-29 16:27:28 -0700174// A null-terminated list of all of the instance extensions supported by the
175// loader
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700176static const char *const LOADER_INSTANCE_EXTENSIONS[] = {VK_KHR_SURFACE_EXTENSION_NAME,
177 VK_KHR_DISPLAY_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700178#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700179 VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700180#endif
181#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700182 VK_KHR_XCB_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700183#endif
184#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700185 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700186#endif
187#ifdef VK_USE_PLATFORM_MIR_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700188 VK_KHR_MIR_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700189#endif
190#ifdef VK_USE_PLATFORM_ANDROID_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700191 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700192#endif
193#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700194 VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700195#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700196 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700197#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700198 VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700199#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700200 VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
201 VK_EXT_DIRECT_MODE_DISPLAY_EXTENSION_NAME,
202 VK_EXT_DISPLAY_SURFACE_COUNTER_EXTENSION_NAME,
203 VK_EXT_VALIDATION_FLAGS_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700204#ifdef VK_USE_PLATFORM_VI_NN
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700205 VK_NN_VI_SURFACE_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700206#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700207 VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
208 NULL};
Lenny Komow4053b812016-12-29 16:27:28 -0700209
Jon Ashburn8810c5f2015-08-18 18:04:47 -0600210LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700211
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700212void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope alloc_scope) {
Mark Young0ad83132016-06-30 13:02:42 -0600213 void *pMemory = NULL;
214#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
215 {
216#else
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800217 if (instance && instance->alloc_callbacks.pfnAllocation) {
Mark Young0ad83132016-06-30 13:02:42 -0600218 /* These are internal structures, so it's best to align everything to
219 * the largest unit size which is the size of a uint64_t.
220 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700221 pMemory = instance->alloc_callbacks.pfnAllocation(instance->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600222 } else {
223#endif
224 pMemory = malloc(size);
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600225 }
Mark Young0ad83132016-06-30 13:02:42 -0600226 return pMemory;
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600227}
228
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700229void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory) {
Mark Young0ad83132016-06-30 13:02:42 -0600230 if (pMemory != NULL) {
231#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
232 {
233#else
234 if (instance && instance->alloc_callbacks.pfnFree) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700235 instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData, pMemory);
Mark Young0ad83132016-06-30 13:02:42 -0600236 } else {
237#endif
238 free(pMemory);
Mark Youngd077f992016-06-30 11:03:59 -0600239 }
Mark Young4b0b9222016-06-29 18:33:53 -0600240 }
Mark Young4b0b9222016-06-29 18:33:53 -0600241}
242
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700243void *loader_instance_heap_realloc(const struct loader_instance *instance, void *pMemory, size_t orig_size, size_t size,
Mark Young0ad83132016-06-30 13:02:42 -0600244 VkSystemAllocationScope alloc_scope) {
245 void *pNewMem = NULL;
246 if (pMemory == NULL || orig_size == 0) {
247 pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
248 } else if (size == 0) {
249 loader_instance_heap_free(instance, pMemory);
250#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
251#else
252 } else if (instance && instance->alloc_callbacks.pfnReallocation) {
253 /* These are internal structures, so it's best to align everything to
254 * the largest unit size which is the size of a uint64_t.
255 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700256 pNewMem = instance->alloc_callbacks.pfnReallocation(instance->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
257 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600258#endif
259 } else {
260 pNewMem = realloc(pMemory, size);
261 }
262 return pNewMem;
Mark Young4b0b9222016-06-29 18:33:53 -0600263}
264
Mark Young0ad83132016-06-30 13:02:42 -0600265void *loader_instance_tls_heap_alloc(size_t size) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700266 return loader_instance_heap_alloc(tls_instance, size, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young4b0b9222016-06-29 18:33:53 -0600267}
Mark Young4b0b9222016-06-29 18:33:53 -0600268
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700269void loader_instance_tls_heap_free(void *pMemory) { loader_instance_heap_free(tls_instance, pMemory); }
Mark Young0ad83132016-06-30 13:02:42 -0600270
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700271void *loader_device_heap_alloc(const struct loader_device *device, size_t size, VkSystemAllocationScope alloc_scope) {
Mark Young0ad83132016-06-30 13:02:42 -0600272 void *pMemory = NULL;
273#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
274 {
275#else
276 if (device && device->alloc_callbacks.pfnAllocation) {
277 /* These are internal structures, so it's best to align everything to
278 * the largest unit size which is the size of a uint64_t.
279 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700280 pMemory = device->alloc_callbacks.pfnAllocation(device->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600281 } else {
282#endif
283 pMemory = malloc(size);
284 }
285 return pMemory;
286}
287
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700288void loader_device_heap_free(const struct loader_device *device, void *pMemory) {
Mark Young0ad83132016-06-30 13:02:42 -0600289 if (pMemory != NULL) {
290#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
291 {
292#else
293 if (device && device->alloc_callbacks.pfnFree) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700294 device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData, pMemory);
Mark Young0ad83132016-06-30 13:02:42 -0600295 } else {
296#endif
297 free(pMemory);
298 }
299 }
300}
301
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700302void *loader_device_heap_realloc(const struct loader_device *device, void *pMemory, size_t orig_size, size_t size,
Mark Young0ad83132016-06-30 13:02:42 -0600303 VkSystemAllocationScope alloc_scope) {
304 void *pNewMem = NULL;
305 if (pMemory == NULL || orig_size == 0) {
306 pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
307 } else if (size == 0) {
308 loader_device_heap_free(device, pMemory);
309#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
310#else
311 } else if (device && device->alloc_callbacks.pfnReallocation) {
312 /* These are internal structures, so it's best to align everything to
313 * the largest unit size which is the size of a uint64_t.
314 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700315 pNewMem = device->alloc_callbacks.pfnReallocation(device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
316 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600317#endif
318 } else {
319 pNewMem = realloc(pMemory, size);
320 }
321 return pNewMem;
322}
323
324// Environment variables
325#if defined(__linux__)
326
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700327static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600328 // No allocation of memory necessary for Linux, but we should at least touch
329 // the inst pointer to get rid of compiler warnings.
330 (void)inst;
331 return getenv(name);
332}
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700333static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600334 // No freeing of memory necessary for Linux, but we should at least touch
335 // the val and inst pointers to get rid of compiler warnings.
336 (void)val;
337 (void)inst;
338}
339
340#elif defined(WIN32)
341
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700342static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600343 char *retVal;
344 DWORD valSize;
345
346 valSize = GetEnvironmentVariableA(name, NULL, 0);
347
348 // valSize DOES include the null terminator, so for any set variable
349 // will always be at least 1. If it's 0, the variable wasn't set.
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700350 if (valSize == 0) return NULL;
Mark Young0ad83132016-06-30 13:02:42 -0600351
352 // Allocate the space necessary for the registry entry
353 if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700354 retVal = (char *)inst->alloc_callbacks.pfnAllocation(inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
355 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -0600356 } else {
357 retVal = (char *)malloc(valSize);
358 }
359
360 if (NULL != retVal) {
361 GetEnvironmentVariableA(name, retVal, valSize);
362 }
363
364 return retVal;
365}
366
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700367static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600368 if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
369 inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
370 } else {
371 free((void *)val);
372 }
373}
374
375#else
376
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700377static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600378 // stub func
379 (void)inst;
380 (void)name;
381 return NULL;
382}
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700383static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600384 // stub func
385 (void)val;
386 (void)inst;
387}
388
389#endif
390
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700391void loader_log(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code, const char *format, ...) {
Jon Ashburn86723b02015-07-31 15:47:59 -0600392 char msg[512];
Mark Young2c84c0c2017-01-13 10:27:03 -0700393 char cmd_line_msg[512];
394 uint16_t cmd_line_size = sizeof(cmd_line_msg);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700395 va_list ap;
396 int ret;
397
Jon Ashburnffad94d2015-06-30 14:46:22 -0700398 va_start(ap, format);
399 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700400 if ((ret >= (int)sizeof(msg)) || ret < 0) {
401 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnffad94d2015-06-30 14:46:22 -0700402 }
403 va_end(ap);
404
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700405 if (inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700406 util_DebugReportMessage(inst, msg_type, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, (uint64_t)(uintptr_t)inst, 0, msg_code,
407 "loader", msg);
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700408 }
409
410 if (!(msg_type & g_loader_log_msgs)) {
411 return;
412 }
413
Mark Young2c84c0c2017-01-13 10:27:03 -0700414 cmd_line_msg[0] = '\0';
415
416 va_start(ap, format);
417 if ((msg_type & LOADER_INFO_BIT) != 0) {
Mark Young6ef95f42017-02-17 09:02:23 -0700418 strncat(cmd_line_msg, "INFO", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700419 cmd_line_size -= 4;
420 }
421 if ((msg_type & LOADER_WARN_BIT) != 0) {
422 if (cmd_line_size != sizeof(cmd_line_msg)) {
Mark Young6ef95f42017-02-17 09:02:23 -0700423 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700424 cmd_line_size -= 3;
425 }
Mark Young6ef95f42017-02-17 09:02:23 -0700426 strncat(cmd_line_msg, "WARNING", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700427 cmd_line_size -= 7;
428 }
429 if ((msg_type & LOADER_PERF_BIT) != 0) {
430 if (cmd_line_size != sizeof(cmd_line_msg)) {
Mark Young6ef95f42017-02-17 09:02:23 -0700431 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700432 cmd_line_size -= 3;
433 }
Mark Young6ef95f42017-02-17 09:02:23 -0700434 strncat(cmd_line_msg, "PERF", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700435 cmd_line_size -= 4;
436 }
437 if ((msg_type & LOADER_ERROR_BIT) != 0) {
438 if (cmd_line_size != sizeof(cmd_line_msg)) {
Mark Young6ef95f42017-02-17 09:02:23 -0700439 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700440 cmd_line_size -= 3;
441 }
Mark Young6ef95f42017-02-17 09:02:23 -0700442 strncat(cmd_line_msg, "ERROR", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700443 cmd_line_size -= 5;
444 }
445 if ((msg_type & LOADER_DEBUG_BIT) != 0) {
446 if (cmd_line_size != sizeof(cmd_line_msg)) {
Mark Young6ef95f42017-02-17 09:02:23 -0700447 strncat(cmd_line_msg, " | ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700448 cmd_line_size -= 3;
449 }
Mark Young6ef95f42017-02-17 09:02:23 -0700450 strncat(cmd_line_msg, "DEBUG", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700451 cmd_line_size -= 5;
452 }
453 if (cmd_line_size != sizeof(cmd_line_msg)) {
Mark Young6ef95f42017-02-17 09:02:23 -0700454 strncat(cmd_line_msg, ": ", cmd_line_size);
Mark Young2c84c0c2017-01-13 10:27:03 -0700455 cmd_line_size -= 2;
456 }
457 strncat(cmd_line_msg, msg, cmd_line_size);
458
Ian Elliott4470a302015-02-17 10:33:47 -0700459#if defined(WIN32)
Mark Young2c84c0c2017-01-13 10:27:03 -0700460 OutputDebugString(cmd_line_msg);
mschottb9cdb782015-07-22 14:11:29 +0200461 OutputDebugString("\n");
Jon Ashburnffad94d2015-06-30 14:46:22 -0700462#endif
Mark Young2c84c0c2017-01-13 10:27:03 -0700463 fputs(cmd_line_msg, stderr);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700464 fputc('\n', stderr);
465}
466
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700467VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceDispatch(VkInstance instance, void *object) {
Jon Ashburnc3c58772016-03-29 11:16:01 -0600468 struct loader_instance *inst = loader_get_instance(instance);
469 if (!inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700470 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
471 "vkSetInstanceDispatch: Can not retrieve Instance "
472 "dispatch table.");
Jon Ashburnc3c58772016-03-29 11:16:01 -0600473 return VK_ERROR_INITIALIZATION_FAILED;
474 }
475 loader_set_dispatch(object, inst->disp);
476 return VK_SUCCESS;
477}
478
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700479VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600480 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700481 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Jon Ashburned8f2312016-03-31 10:52:22 -0600482
Mark Young0153e0b2016-11-03 14:27:13 -0600483 if (NULL == icd_term) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600484 return VK_ERROR_INITIALIZATION_FAILED;
485 }
486 loader_set_dispatch(object, &dev->loader_dispatch);
487 return VK_SUCCESS;
488}
489
Jon Ashburnffad94d2015-06-30 14:46:22 -0700490#if defined(WIN32)
Tony Barbourea968902015-07-29 14:26:21 -0600491static char *loader_get_next_path(char *path);
Mark Young2c84c0c2017-01-13 10:27:03 -0700492
493// Find the list of registry files (names within a key) in key "location".
494//
495// This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
496// given in "location"
497// for a list or name/values which are added to a returned list (function return
498// value).
499// The DWORD values within the key must be 0 or they are skipped.
500// Function return is a string with a ';' separated list of filenames.
501// Function return is NULL if no valid name/value pairs are found in the key,
502// or the key is not found.
503//
504// *reg_data contains a string list of filenames as pointer.
505// When done using the returned string list, the caller should free the pointer.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700506VkResult loaderGetRegistryFiles(const struct loader_instance *inst, char *location, char **reg_data) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700507 LONG rtn_value;
508 HKEY hive, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700509 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700510 char name[2048];
Tony Barbourea968902015-07-29 14:26:21 -0600511 char *loc = location;
512 char *next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700513 DWORD idx = 0;
514 DWORD name_size = sizeof(name);
515 DWORD value;
516 DWORD total_size = 4096;
517 DWORD value_size = sizeof(value);
Mark Young2c84c0c2017-01-13 10:27:03 -0700518 VkResult result = VK_SUCCESS;
519 bool found = false;
520
521 if (NULL == reg_data) {
522 result = VK_ERROR_INITIALIZATION_FAILED;
523 goto out;
524 }
Tony Barbourea968902015-07-29 14:26:21 -0600525
Jon Ashburn23d36b12016-02-02 17:47:28 -0700526 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600527 next = loader_get_next_path(loc);
528 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniell524ec732015-11-05 16:58:26 -0700529 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600530 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
Mark Young2c84c0c2017-01-13 10:27:03 -0700531 if (ERROR_SUCCESS != rtn_value) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700532 // We still couldn't find the key, so give up:
533 loc = next;
534 continue;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700535 }
Tony Barbourea968902015-07-29 14:26:21 -0600536
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700537 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL, NULL, (LPBYTE)&value, &value_size)) == ERROR_SUCCESS) {
Tony Barbourea968902015-07-29 14:26:21 -0600538 if (value_size == sizeof(value) && value == 0) {
Mark Young2c84c0c2017-01-13 10:27:03 -0700539 if (NULL == *reg_data) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700540 *reg_data = loader_instance_heap_alloc(inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young2c84c0c2017-01-13 10:27:03 -0700541 if (NULL == *reg_data) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700542 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
543 "loaderGetRegistryFiles: Failed to allocate "
544 "space for registry data for key %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700545 name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700546 result = VK_ERROR_OUT_OF_HOST_MEMORY;
547 goto out;
Mark Young0ad83132016-06-30 13:02:42 -0600548 }
Mark Young2c84c0c2017-01-13 10:27:03 -0700549 *reg_data[0] = '\0';
550 } else if (strlen(*reg_data) + name_size + 1 > total_size) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700551 *reg_data = loader_instance_heap_realloc(inst, *reg_data, total_size, total_size * 2,
552 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young2c84c0c2017-01-13 10:27:03 -0700553 if (NULL == *reg_data) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700554 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
555 "loaderGetRegistryFiles: Failed to reallocate "
556 "space for registry value of size %d for key %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700557 total_size * 2, name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700558 result = VK_ERROR_OUT_OF_HOST_MEMORY;
559 goto out;
Mark Young0ad83132016-06-30 13:02:42 -0600560 }
Tony Barbourea968902015-07-29 14:26:21 -0600561 total_size *= 2;
562 }
Mark Young2c84c0c2017-01-13 10:27:03 -0700563 if (strlen(*reg_data) == 0) {
Karl Schultze2ef9e62017-01-13 14:01:35 -0700564 (void)snprintf(*reg_data, name_size + 1, "%s", name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700565 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700566 (void)snprintf(*reg_data + strlen(*reg_data), name_size + 2, "%c%s", PATH_SEPARATOR, name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700567 }
568 found = true;
Tony Barbourea968902015-07-29 14:26:21 -0600569 }
570 name_size = 2048;
571 }
572 loc = next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700573 }
Tony Barbourea968902015-07-29 14:26:21 -0600574
Mark Young2c84c0c2017-01-13 10:27:03 -0700575 if (!found) {
576 result = VK_ERROR_INITIALIZATION_FAILED;
577 }
578
579out:
580
581 return result;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700582}
583
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700584#endif // WIN32
Ian Elliott4470a302015-02-17 10:33:47 -0700585
Jon Ashburnc7237a72015-08-03 09:08:46 -0600586/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500587 * Combine path elements, separating each element with the platform-specific
588 * directory separator, and save the combined string to a destination buffer,
589 * not exceeding the given length. Path elements are given as variadic args,
590 * with a NULL element terminating the list.
591 *
592 * \returns the total length of the combined string, not including an ASCII
593 * NUL termination character. This length may exceed the available storage:
594 * in this case, the written string will be truncated to avoid a buffer
595 * overrun, and the return value will greater than or equal to the storage
596 * size. A NULL argument may be provided as the destination buffer in order
597 * to determine the required string length without actually writing a string.
598 */
599
Jon Ashburn23d36b12016-02-02 17:47:28 -0700600static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700601 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500602 va_list ap;
603 const char *component;
604
605 va_start(ap, len);
606
Jon Ashburn23d36b12016-02-02 17:47:28 -0700607 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500608 if (required_len > 0) {
609 // This path element is not the first non-empty element; prepend
610 // a directory separator if space allows
611 if (dest && required_len + 1 < len) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700612 (void)snprintf(dest + required_len, len - required_len, "%c", DIRECTORY_SYMBOL);
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500613 }
614 required_len++;
615 }
616
617 if (dest && required_len < len) {
618 strncpy(dest + required_len, component, len - required_len);
619 }
620 required_len += strlen(component);
621 }
622
623 va_end(ap);
624
625 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
626 if (dest && required_len >= len) {
627 dest[len - 1] = '\0';
628 }
629
630 return required_len;
631}
632
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500633/**
Jon Ashburnc7237a72015-08-03 09:08:46 -0600634 * Given string of three part form "maj.min.pat" convert to a vulkan version
635 * number.
636 */
Mark Young60861ac2016-09-02 11:39:26 -0600637static uint32_t loader_make_version(char *vers_str) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700638 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
Mark Young60861ac2016-09-02 11:39:26 -0600639 char *vers_tok;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600640
Mark Young60861ac2016-09-02 11:39:26 -0600641 if (!vers_str) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600642 return vers;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600643 }
Mark Young60861ac2016-09-02 11:39:26 -0600644
645 vers_tok = strtok(vers_str, ".\"\n\r");
646 if (NULL != vers_tok) {
647 major = (uint16_t)atoi(vers_tok);
648 vers_tok = strtok(NULL, ".\"\n\r");
649 if (NULL != vers_tok) {
650 minor = (uint16_t)atoi(vers_tok);
651 vers_tok = strtok(NULL, ".\"\n\r");
652 if (NULL != vers_tok) {
653 patch = (uint16_t)atoi(vers_tok);
654 }
655 }
656 }
Jon Ashburnc7237a72015-08-03 09:08:46 -0600657
658 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600659}
660
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700661bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800662 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600663}
664
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600665/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600666 * Search the given ext_array for an extension
667 * matching the given vk_ext_prop
668 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700669bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop, const uint32_t count,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700670 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600671 for (uint32_t i = 0; i < count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700672 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i])) return true;
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600673 }
674 return false;
675}
676
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600677/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600678 * Search the given ext_list for an extension
679 * matching the given vk_ext_prop
680 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700681bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop, const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600682 for (uint32_t i = 0; i < ext_list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700683 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop)) return true;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600684 }
685 return false;
686}
687
Jon Ashburnb8726962016-04-08 15:03:35 -0600688/**
689 * Search the given ext_list for a device extension matching the given ext_prop
690 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700691bool has_vk_dev_ext_property(const VkExtensionProperties *ext_prop, const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600692 for (uint32_t i = 0; i < ext_list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700693 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop)) return true;
Jon Ashburnb8726962016-04-08 15:03:35 -0600694 }
695 return false;
696}
697
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600698/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600699 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600700 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700701static struct loader_layer_properties *loader_get_layer_property(const char *name, const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600702 for (uint32_t i = 0; i < layer_list->count; i++) {
703 const VkLayerProperties *item = &layer_list->list[i].info;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700704 if (strcmp(name, item->layerName) == 0) return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600705 }
706 return NULL;
707}
708
Jon Ashburne13ecc92015-08-03 17:19:30 -0600709/**
710 * Get the next unused layer property in the list. Init the property to zero.
711 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700712static struct loader_layer_properties *loader_get_next_layer_property(const struct loader_instance *inst,
713 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600714 if (layer_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700715 layer_list->list =
716 loader_instance_heap_alloc(inst, sizeof(struct loader_layer_properties) * 64, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600717 if (layer_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700718 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
719 "loader_get_next_layer_property: Out of memory can "
720 "not add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600721 return NULL;
722 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700723 memset(layer_list->list, 0, sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600724 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
725 }
726
727 // ensure enough room to add an entry
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700728 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) > layer_list->capacity) {
729 layer_list->list = loader_instance_heap_realloc(inst, layer_list->list, layer_list->capacity, layer_list->capacity * 2,
730 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600731 if (layer_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700732 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
733 "loader_get_next_layer_property: realloc failed for "
734 "layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600735 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600736 }
737 layer_list->capacity *= 2;
738 }
739
740 layer_list->count++;
741 return &(layer_list->list[layer_list->count - 1]);
742}
743
744/**
745 * Remove all layer properties entrys from the list
746 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700747void loader_delete_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700748 uint32_t i, j;
749 struct loader_device_extension_list *dev_ext_list;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700750 if (!layer_list) return;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600751
Jon Ashburne13ecc92015-08-03 17:19:30 -0600752 for (i = 0; i < layer_list->count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700753 loader_destroy_generic_list(inst, (struct loader_generic_list *)&layer_list->list[i].instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700754 dev_ext_list = &layer_list->list[i].device_extension_list;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700755 if (dev_ext_list->capacity > 0 && NULL != dev_ext_list->list && dev_ext_list->list->entrypoint_count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700756 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700757 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700758 }
Mark Young0ad83132016-06-30 13:02:42 -0600759 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700760 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700761 loader_destroy_generic_list(inst, (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600762 }
763 layer_list->count = 0;
764
Jon Ashburnb82c1852015-08-11 14:49:54 -0600765 if (layer_list->capacity > 0) {
766 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600767 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600768 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600769}
770
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700771static VkResult loader_add_instance_extensions(const struct loader_instance *inst,
772 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props, const char *lib_name,
773 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700774 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600775 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600776 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600777
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600778 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600779 /* No EnumerateInstanceExtensionProperties defined */
Mark Young3a587792016-08-19 15:25:08 -0600780 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600781 }
782
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600783 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600784 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700785 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
786 "loader_add_instance_extensions: Error getting Instance "
787 "extension count from %s",
Mark Youngb6399312017-01-10 14:22:15 -0700788 lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600789 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600790 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600791
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600792 if (count == 0) {
793 /* No ExtensionProperties to report */
Mark Young3a587792016-08-19 15:25:08 -0600794 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600795 }
796
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600797 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young39389872017-01-19 21:10:49 -0700798 if (NULL == ext_props) {
799 res = VK_ERROR_OUT_OF_HOST_MEMORY;
800 goto out;
801 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600802
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600803 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600804 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700805 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
806 "loader_add_instance_extensions: Error getting Instance "
807 "extensions from %s",
Mark Youngb6399312017-01-10 14:22:15 -0700808 lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600809 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600810 }
Tony Barbour59a47322015-06-24 16:06:58 -0600811
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600812 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600813 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600814
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700815 bool ext_unsupported = wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600816 if (!ext_unsupported) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700817 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_MAJOR(ext_props[i].specVersion),
818 VK_MINOR(ext_props[i].specVersion), VK_PATCH(ext_props[i].specVersion));
819 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Instance Extension: %s (%s) version %s", ext_props[i].extensionName,
820 lib_name, spec_version);
Mark Young6267ae62017-01-12 12:27:19 -0700821
Mark Young3a587792016-08-19 15:25:08 -0600822 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
823 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700824 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
825 "loader_add_instance_extensions: Failed to add %s "
826 "to Instance extension list",
Mark Young3a587792016-08-19 15:25:08 -0600827 lib_name);
828 goto out;
829 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600830 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600831 }
Mark Young6267ae62017-01-12 12:27:19 -0700832
Mark Young3a587792016-08-19 15:25:08 -0600833out:
834 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600835}
836
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700837/*
838 * Initialize ext_list with the physical device extensions.
839 * The extension properties are passed as inputs in count and ext_props.
840 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700841static VkResult loader_init_device_extensions(const struct loader_instance *inst, struct loader_physical_device_term *phys_dev_term,
842 uint32_t count, VkExtensionProperties *ext_props,
843 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700844 VkResult res;
845 uint32_t i;
846
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700847 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600848 if (VK_SUCCESS != res) {
849 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700850 }
851
852 for (i = 0; i < count; i++) {
853 char spec_version[64];
854
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700855 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_MAJOR(ext_props[i].specVersion),
856 VK_MINOR(ext_props[i].specVersion), VK_PATCH(ext_props[i].specVersion));
857 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
858 phys_dev_term->this_icd_term->scanned_icd->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700859 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700860 if (res != VK_SUCCESS) return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700861 }
862
863 return VK_SUCCESS;
864}
865
Jon Ashburn1530c342016-02-26 13:14:27 -0700866VkResult loader_add_device_extensions(const struct loader_instance *inst,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700867 PFN_vkEnumerateDeviceExtensionProperties fpEnumerateDeviceExtensionProperties,
868 VkPhysicalDevice physical_device, const char *lib_name,
Jon Ashburn1530c342016-02-26 13:14:27 -0700869 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600870 uint32_t i, count;
871 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600872 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600873
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700874 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700875 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700876 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young9a3ddd42016-10-21 16:25:47 -0600877 if (!ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700878 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
879 "loader_add_device_extensions: Failed to allocate space"
880 " for device extension properties.");
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700881 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young9a3ddd42016-10-21 16:25:47 -0600882 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700883 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, ext_props);
Mark Young9a3ddd42016-10-21 16:25:47 -0600884 if (res != VK_SUCCESS) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700885 return res;
Mark Young9a3ddd42016-10-21 16:25:47 -0600886 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700887 for (i = 0; i < count; i++) {
888 char spec_version[64];
889
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700890 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_MAJOR(ext_props[i].specVersion),
891 VK_MINOR(ext_props[i].specVersion), VK_PATCH(ext_props[i].specVersion));
892 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
893 lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700894 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
Mark Youngb6399312017-01-10 14:22:15 -0700895 if (res != VK_SUCCESS) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700896 return res;
Mark Youngb6399312017-01-10 14:22:15 -0700897 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600898 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700899 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700900 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
901 "loader_add_device_extensions: Error getting physical "
902 "device extension info count from library %s",
Jon Ashburn23d36b12016-02-02 17:47:28 -0700903 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700904 return res;
905 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600906
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700907 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600908}
909
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700910VkResult loader_init_generic_list(const struct loader_instance *inst, struct loader_generic_list *list_info, size_t element_size) {
Mark Young84ba0482016-09-02 11:45:00 -0600911 size_t capacity = 32 * element_size;
912 list_info->count = 0;
913 list_info->capacity = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700914 list_info->list = loader_instance_heap_alloc(inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700915 if (list_info->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700916 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
917 "loader_init_generic_list: Failed to allocate space "
918 "for generic list");
Mark Young3a587792016-08-19 15:25:08 -0600919 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600920 }
Mark Young84ba0482016-09-02 11:45:00 -0600921 memset(list_info->list, 0, capacity);
922 list_info->capacity = capacity;
Mark Young3a587792016-08-19 15:25:08 -0600923 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600924}
925
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700926void loader_destroy_generic_list(const struct loader_instance *inst, struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600927 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700928 list->count = 0;
929 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600930}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600931
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600932/*
933 * Append non-duplicate extension properties defined in props
934 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700935 * Return
936 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600937 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700938VkResult loader_add_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list,
939 uint32_t prop_list_count, const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600940 uint32_t i;
941 const VkExtensionProperties *cur_ext;
942
943 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700944 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600945 if (VK_SUCCESS != res) {
946 return res;
947 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600948 }
949
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600950 for (i = 0; i < prop_list_count; i++) {
951 cur_ext = &props[i];
952
953 // look for duplicates
954 if (has_vk_extension_property(cur_ext, ext_list)) {
955 continue;
956 }
957
958 // add to list at end
959 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700960 if (ext_list->count * sizeof(VkExtensionProperties) >= ext_list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700961 ext_list->list = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
962 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700963
Mark Youngb6399312017-01-10 14:22:15 -0700964 if (ext_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700965 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
966 "loader_add_to_ext_list: Failed to reallocate "
967 "space for extension list");
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700968 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Youngb6399312017-01-10 14:22:15 -0700969 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700970
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600971 // double capacity
972 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600973 }
974
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700975 memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600976 ext_list->count++;
977 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700978 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600979}
980
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700981/*
982 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600983 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700984 * Return
985 * Vk_SUCCESS on success
986 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700987VkResult loader_add_to_dev_ext_list(const struct loader_instance *inst, struct loader_device_extension_list *ext_list,
988 const VkExtensionProperties *props, uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700989 uint32_t idx;
990 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700991 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(struct loader_dev_ext_props));
Mark Young3a587792016-08-19 15:25:08 -0600992 if (VK_SUCCESS != res) {
993 return res;
994 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700995 }
996
Jon Ashburnb8726962016-04-08 15:03:35 -0600997 // look for duplicates
998 if (has_vk_dev_ext_property(props, ext_list)) {
999 return VK_SUCCESS;
1000 }
1001
Jon Ashburn23d36b12016-02-02 17:47:28 -07001002 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001003 // add to list at end
1004 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001005 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001006 ext_list->list = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
1007 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001008
Mark Youngb6399312017-01-10 14:22:15 -07001009 if (ext_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001010 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1011 "loader_add_to_dev_ext_list: Failed to reallocate "
1012 "space for device extension list");
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001013 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Youngb6399312017-01-10 14:22:15 -07001014 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001015
1016 // double capacity
1017 ext_list->capacity *= 2;
1018 }
1019
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001020 memcpy(&ext_list->list[idx].props, props, sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001021 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001022 ext_list->list[idx].entrypoints =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001023 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001024 if (ext_list->list[idx].entrypoints == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001025 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1026 "loader_add_to_dev_ext_list: Failed to allocate space "
1027 "for device extension entrypoint list in list %d",
Mark Youngb6399312017-01-10 14:22:15 -07001028 idx);
Mark Young0ad83132016-06-30 13:02:42 -06001029 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001030 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001031 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001032 for (uint32_t i = 0; i < entry_count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001033 ext_list->list[idx].entrypoints[i] =
1034 loader_instance_heap_alloc(inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001035 if (ext_list->list[idx].entrypoints[i] == NULL) {
1036 for (uint32_t j = 0; j < i; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001037 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints[j]);
Mark Young0ad83132016-06-30 13:02:42 -06001038 }
1039 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
1040 ext_list->list[idx].entrypoint_count = 0;
1041 ext_list->list[idx].entrypoints = NULL;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001042 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1043 "loader_add_to_dev_ext_list: Failed to allocate space "
1044 "for device extension entrypoint %d name",
Mark Youngb6399312017-01-10 14:22:15 -07001045 i);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001046 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001047 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001048 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
1049 }
1050 ext_list->count++;
1051
1052 return VK_SUCCESS;
1053}
1054
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001055/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001056 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001057 * Add these to the output layer_list. Don't add duplicates to the output
1058 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001059 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001060static VkResult loader_add_layer_names_to_list(const struct loader_instance *inst, struct loader_layer_list *output_list,
1061 uint32_t name_count, const char *const *names,
1062 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001063 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001064 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001065
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001066 for (uint32_t i = 0; i < name_count; i++) {
1067 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001068 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001069 if (!layer_prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001070 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1071 "loader_add_layer_names_to_list: Unable to find layer"
1072 " %s",
Mark Youngb6399312017-01-10 14:22:15 -07001073 search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001074 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001075 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001076 }
1077
Mark Young0ad83132016-06-30 13:02:42 -06001078 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001079 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001080
1081 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001082}
1083
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001084/*
1085 * Manage lists of VkLayerProperties
1086 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001087static bool loader_init_layer_list(const struct loader_instance *inst, struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001088 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001089 list->list = loader_instance_heap_alloc(inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001090 if (list->list == NULL) {
1091 return false;
1092 }
1093 memset(list->list, 0, list->capacity);
1094 list->count = 0;
1095 return true;
1096}
1097
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001098void loader_destroy_layer_list(const struct loader_instance *inst, struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001099 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001100 if (device) {
1101 loader_device_heap_free(device, layer_list->list);
1102 } else {
1103 loader_instance_heap_free(inst, layer_list->list);
1104 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001105 layer_list->count = 0;
1106 layer_list->capacity = 0;
1107}
1108
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001109/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001110 * Search the given layer list for a list
1111 * matching the given VkLayerProperties
1112 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001113bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001114 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001115 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0) return true;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001116 }
1117 return false;
1118}
1119
1120/*
1121 * Search the given layer list for a layer
1122 * matching the given name
1123 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001124bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001125 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001126 if (strcmp(name, list->list[i].info.layerName) == 0) return true;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001127 }
1128 return false;
1129}
1130
1131/*
1132 * Append non-duplicate layer properties defined in prop_list
1133 * to the given layer_info list
1134 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001135VkResult loader_add_to_layer_list(const struct loader_instance *inst, struct loader_layer_list *list, uint32_t prop_list_count,
Mark Young0ad83132016-06-30 13:02:42 -06001136 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001137 uint32_t i;
1138 struct loader_layer_properties *layer;
1139
1140 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001141 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001142 }
1143
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001144 if (list->list == NULL) return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001145
1146 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001147 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001148
1149 // look for duplicates
1150 if (has_vk_layer_property(&layer->info, list)) {
1151 continue;
1152 }
1153
1154 // add to list at end
1155 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001156 if (list->count * sizeof(struct loader_layer_properties) >= list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001157 list->list = loader_instance_heap_realloc(inst, list->list, list->capacity, list->capacity * 2,
1158 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001159 if (NULL == list->list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001160 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1161 "loader_add_to_layer_list: Realloc failed for "
1162 "when attempting to add new layer");
Mark Young0ad83132016-06-30 13:02:42 -06001163 return VK_ERROR_OUT_OF_HOST_MEMORY;
1164 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001165 // double capacity
1166 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001167 }
1168
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001169 memcpy(&list->list[list->count], layer, sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001170 list->count++;
1171 }
Mark Young0ad83132016-06-30 13:02:42 -06001172
1173 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001174}
1175
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001176/**
1177 * Search the search_list for any layer with a name
1178 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001179 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001180 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001181 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001182 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001183void loader_find_layer_name_add_list(const struct loader_instance *inst, const char *name, const enum layer_type type,
1184 const struct loader_layer_list *search_list, struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001185 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001186 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001187 struct loader_layer_properties *layer_prop = &search_list->list[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001188 if (0 == strcmp(layer_prop->info.layerName, name) && (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001189 /* Found a layer with the same name, add to found_list */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001190 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
Mark Young0ad83132016-06-30 13:02:42 -06001191 found = true;
1192 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001193 }
1194 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001195 if (!found) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001196 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
1197 "loader_find_layer_name_add_list: Failed to find layer name "
1198 "%s to activate",
Mark Youngb6399312017-01-10 14:22:15 -07001199 name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001200 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001201}
1202
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001203static VkExtensionProperties *get_extension_property(const char *name, const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001204 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001205 if (strcmp(name, list->list[i].extensionName) == 0) return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001206 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001207 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001208}
1209
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001210static VkExtensionProperties *get_dev_extension_property(const char *name, const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001211 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001212 if (strcmp(name, list->list[i].props.extensionName) == 0) return &list->list[i].props;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001213 }
1214 return NULL;
1215}
1216
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001217/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001218 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001219 * the extension must provide two entry points for the loader to use:
1220 * - "trampoline" entry point - this is the address returned by GetProcAddr
1221 * and will always do what's necessary to support a global call.
1222 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001223 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001224 * the extension for the appropriate ICDs that are available.
1225 * There is no generic mechanism for including these functions, the references
1226 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001227 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1228 * GetProcAddr requests
1229 * loader_coalesce_extensions(void) - add extension records to the list of
1230 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001231 * extension available to the app.
1232 * instance_disp - add function pointer for terminator function to this array.
1233 * The extension itself should be in a separate file that will be
1234 * linked directly with the loader.
1235 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001236
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001237VkResult loader_get_icd_loader_instance_extensions(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
1238 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001239 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001240 VkResult res = VK_SUCCESS;
Mark Young2b2ece72017-02-10 11:19:02 -07001241 char *env_value;
1242 bool filter_extensions = true;
Mark Young3a587792016-08-19 15:25:08 -06001243
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001244 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001245
Mark Young2b2ece72017-02-10 11:19:02 -07001246 // Check if a user wants to disable the instance extension filtering behavior
1247 env_value = loader_getenv("VK_LOADER_DISABLE_INST_EXT_FILTER", inst);
1248 if (NULL != env_value && atoi(env_value) != 0) {
1249 filter_extensions = false;
1250 }
1251 loader_free_getenv(env_value, inst);
1252
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001253 // traverse scanned icd list adding non-duplicate extensions to the list
Mark Young0153e0b2016-11-03 14:27:13 -06001254 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001255 res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06001256 if (VK_SUCCESS != res) {
1257 goto out;
1258 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001259 res = loader_add_instance_extensions(inst, icd_tramp_list->scanned_list[i].EnumerateInstanceExtensionProperties,
1260 icd_tramp_list->scanned_list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001261 if (VK_SUCCESS == res) {
Mark Young2b2ece72017-02-10 11:19:02 -07001262 if (filter_extensions) {
1263 // Remove any extensions not recognized by the loader
1264 for (int32_t j = 0; j < (int32_t)icd_exts.count; j++) {
1265 // See if the extension is in the list of supported extensions
1266 bool found = false;
1267 for (uint32_t k = 0; LOADER_INSTANCE_EXTENSIONS[k] != NULL; k++) {
1268 if (strcmp(icd_exts.list[j].extensionName, LOADER_INSTANCE_EXTENSIONS[k]) == 0) {
1269 found = true;
1270 break;
1271 }
Lenny Komow4053b812016-12-29 16:27:28 -07001272 }
Lenny Komow4053b812016-12-29 16:27:28 -07001273
Mark Young2b2ece72017-02-10 11:19:02 -07001274 // If it isn't in the list, remove it
1275 if (!found) {
1276 for (uint32_t k = j + 1; k < icd_exts.count; k++) {
1277 icd_exts.list[k - 1] = icd_exts.list[k];
1278 }
1279 --icd_exts.count;
1280 --j;
Lenny Komow4053b812016-12-29 16:27:28 -07001281 }
Lenny Komow4053b812016-12-29 16:27:28 -07001282 }
1283 }
1284
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001285 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count, icd_exts.list);
Mark Young3a587792016-08-19 15:25:08 -06001286 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001287 loader_destroy_generic_list(inst, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001288 if (VK_SUCCESS != res) {
1289 goto out;
1290 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001291 };
1292
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001293 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001294 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001295
1296out:
1297 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001298}
1299
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001300struct loader_icd_term *loader_get_icd_and_device(const VkDevice device, struct loader_device **found_dev, uint32_t *icd_index) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001301 *found_dev = NULL;
Mark Young16573c72016-06-28 10:52:43 -06001302 uint32_t index = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001303 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
1304 for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
1305 for (struct loader_device *dev = icd_term->logical_device_list; dev; dev = dev->next)
Mark Young65cb3662016-11-07 13:27:02 -07001306 // Value comparison of device prevents object wrapping by layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001307 if (loader_get_dispatch(dev->icd_device) == loader_get_dispatch(device) ||
1308 loader_get_dispatch(dev->chain_device) == loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001309 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001310 if (NULL != icd_index) {
1311 *icd_index = index;
1312 }
Mark Young0153e0b2016-11-03 14:27:13 -06001313 return icd_term;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001314 }
Mark Young16573c72016-06-28 10:52:43 -06001315 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001316 }
1317 }
1318 return NULL;
1319}
1320
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001321void loader_destroy_logical_device(const struct loader_instance *inst, struct loader_device *dev,
Mark Young0ad83132016-06-30 13:02:42 -06001322 const VkAllocationCallbacks *pAllocator) {
1323 if (pAllocator) {
1324 dev->alloc_callbacks = *pAllocator;
1325 }
Mark Young0ad83132016-06-30 13:02:42 -06001326 if (NULL != dev->activated_layer_list.list) {
1327 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1328 }
1329 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001330}
1331
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001332struct loader_device *loader_create_logical_device(const struct loader_instance *inst, const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001333 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001334#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1335 {
1336#else
1337 if (pAllocator) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001338 new_dev = (struct loader_device *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(struct loader_device),
1339 sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Mark Young0ad83132016-06-30 13:02:42 -06001340 } else {
1341#endif
1342 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1343 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001344
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001345 if (!new_dev) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001346 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1347 "loader_create_logical_device: Failed to alloc struct "
1348 "loader_device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001349 return NULL;
1350 }
1351
1352 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001353 if (pAllocator) {
1354 new_dev->alloc_callbacks = *pAllocator;
1355 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001356
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001357 return new_dev;
1358}
1359
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001360void loader_add_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term, struct loader_device *dev) {
Mark Young0153e0b2016-11-03 14:27:13 -06001361 dev->next = icd_term->logical_device_list;
1362 icd_term->logical_device_list = dev;
Piers Daniell295fe402016-03-29 11:51:11 -06001363}
1364
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001365void loader_remove_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term,
1366 struct loader_device *found_dev, const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001367 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001368
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001369 if (!icd_term || !found_dev) return;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001370
1371 prev_dev = NULL;
Mark Young0153e0b2016-11-03 14:27:13 -06001372 dev = icd_term->logical_device_list;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001373 while (dev && dev != found_dev) {
1374 prev_dev = dev;
1375 dev = dev->next;
1376 }
1377
1378 if (prev_dev)
1379 prev_dev->next = found_dev->next;
1380 else
Mark Young0153e0b2016-11-03 14:27:13 -06001381 icd_term->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001382 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001383}
1384
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001385static void loader_icd_destroy(struct loader_instance *ptr_inst, struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001386 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001387 ptr_inst->total_icd_count--;
Mark Young0153e0b2016-11-03 14:27:13 -06001388 for (struct loader_device *dev = icd_term->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001389 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001390 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001391 dev = next_dev;
1392 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001393
Mark Young0153e0b2016-11-03 14:27:13 -06001394 loader_instance_heap_free(ptr_inst, icd_term);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001395}
1396
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001397static struct loader_icd_term *loader_icd_create(const struct loader_instance *inst) {
Mark Young0153e0b2016-11-03 14:27:13 -06001398 struct loader_icd_term *icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001399
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001400 icd_term = loader_instance_heap_alloc(inst, sizeof(struct loader_icd_term), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001401 if (!icd_term) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001402 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001403 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001404
Mark Young0153e0b2016-11-03 14:27:13 -06001405 memset(icd_term, 0, sizeof(struct loader_icd_term));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001406
Mark Young0153e0b2016-11-03 14:27:13 -06001407 return icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001408}
1409
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001410static struct loader_icd_term *loader_icd_add(struct loader_instance *ptr_inst, const struct loader_scanned_icd *scanned_icd) {
Mark Young0153e0b2016-11-03 14:27:13 -06001411 struct loader_icd_term *icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001412
Mark Young0153e0b2016-11-03 14:27:13 -06001413 icd_term = loader_icd_create(ptr_inst);
1414 if (!icd_term) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001415 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001416 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001417
Mark Young0153e0b2016-11-03 14:27:13 -06001418 icd_term->scanned_icd = scanned_icd;
1419 icd_term->this_instance = ptr_inst;
Jon Ashburn3d002332015-08-20 16:35:30 -06001420
Chia-I Wu13a61a52014-08-04 11:18:20 +08001421 /* prepend to the list */
Mark Young0153e0b2016-11-03 14:27:13 -06001422 icd_term->next = ptr_inst->icd_terms;
1423 ptr_inst->icd_terms = icd_term;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001424 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001425
Mark Young0153e0b2016-11-03 14:27:13 -06001426 return icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001427}
Mark Young0153e0b2016-11-03 14:27:13 -06001428
Jon Ashburn17b4c862016-04-25 11:09:37 -06001429/**
1430 * Determine the ICD interface version to use.
1431 * @param icd
1432 * @param pVersion Output parameter indicating which version to use or 0 if
1433 * the negotiation API is not supported by the ICD
1434 * @return bool indicating true if the selected interface version is supported
1435 * by the loader, false indicates the version is not supported
Jon Ashburn17b4c862016-04-25 11:09:37 -06001436 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001437bool loader_get_icd_interface_version(PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version, uint32_t *pVersion) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001438 if (fp_negotiate_icd_version == NULL) {
1439 // ICD does not support the negotiation API, it supports version 0 or 1
1440 // calling code must determine if it is version 0 or 1
1441 *pVersion = 0;
1442 } else {
1443 // ICD supports the negotiation API, so call it with the loader's
1444 // latest version supported
1445 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1446 VkResult result = fp_negotiate_icd_version(pVersion);
1447
1448 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1449 // ICD no longer supports the loader's latest interface version so
1450 // fail loading the ICD
1451 return false;
1452 }
1453 }
1454
1455#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1456 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1457 // Loader no longer supports the ICD's latest interface version so fail
1458 // loading the ICD
1459 return false;
1460 }
1461#endif
1462 return true;
1463}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001464
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001465void loader_scanned_icd_clear(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001466 if (icd_tramp_list->capacity == 0) return;
Mark Young0153e0b2016-11-03 14:27:13 -06001467 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
1468 loader_platform_close_library(icd_tramp_list->scanned_list[i].handle);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001469 loader_instance_heap_free(inst, icd_tramp_list->scanned_list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001470 }
Mark Young0153e0b2016-11-03 14:27:13 -06001471 loader_instance_heap_free(inst, icd_tramp_list->scanned_list);
1472 icd_tramp_list->capacity = 0;
1473 icd_tramp_list->count = 0;
1474 icd_tramp_list->scanned_list = NULL;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001475}
1476
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001477static VkResult loader_scanned_icd_init(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001478 VkResult err = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06001479 loader_scanned_icd_clear(inst, icd_tramp_list);
1480 icd_tramp_list->capacity = 8 * sizeof(struct loader_scanned_icd);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001481 icd_tramp_list->scanned_list = loader_instance_heap_alloc(inst, icd_tramp_list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001482 if (NULL == icd_tramp_list->scanned_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001483 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1484 "loader_scanned_icd_init: Realloc failed for layer list when "
1485 "attempting to add new layer");
Mark Young0ad83132016-06-30 13:02:42 -06001486 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1487 }
1488 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001489}
1490
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001491static VkResult loader_scanned_icd_add(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
1492 const char *filename, uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001493 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001494 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001495 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001496 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Mark Young39389872017-01-19 21:10:49 -07001497 PFN_GetPhysicalDeviceProcAddr fp_get_phys_dev_proc_addr = NULL;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001498 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Mark Young0153e0b2016-11-03 14:27:13 -06001499 struct loader_scanned_icd *new_scanned_icd;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001500 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001501 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001502
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001503 /* TODO implement smarter opening/closing of libraries. For now this
1504 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001505 handle = loader_platform_open_library(filename);
Mark Youngb6399312017-01-10 14:22:15 -07001506 if (NULL == handle) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001507 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001508 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001509 }
1510
Jon Ashburn17b4c862016-04-25 11:09:37 -06001511 // Get and settle on an ICD interface version
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001512 fp_negotiate_icd_version = loader_platform_get_proc_address(handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001513
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001514 if (!loader_get_icd_interface_version(fp_negotiate_icd_version, &interface_vers)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001515 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1516 "loader_scanned_icd_add: ICD %s doesn't support interface"
1517 " version compatible with loader, skip this ICD.",
Mark Young0ad83132016-06-30 13:02:42 -06001518 filename);
Mark Young3a587792016-08-19 15:25:08 -06001519 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001520 }
1521
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001522 fp_get_proc_addr = loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Mark Youngb6399312017-01-10 14:22:15 -07001523 if (NULL == fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001524 assert(interface_vers == 0);
1525 // Use deprecated interface from version 0
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001526 fp_get_proc_addr = loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Mark Youngb6399312017-01-10 14:22:15 -07001527 if (NULL == fp_get_proc_addr) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001528 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1529 "loader_scanned_icd_add: Attempt to retreive either "
1530 "\'vkGetInstanceProcAddr\' or "
1531 "\'vk_icdGetInstanceProcAddr\' from ICD %s failed.",
Mark Youngb6399312017-01-10 14:22:15 -07001532 filename);
Mark Young3a587792016-08-19 15:25:08 -06001533 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001534 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001535 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
1536 "loader_scanned_icd_add: Using deprecated ICD "
1537 "interface of \'vkGetInstanceProcAddr\' instead of "
1538 "\'vk_icdGetInstanceProcAddr\' for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001539 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001540 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001541 fp_create_inst = loader_platform_get_proc_address(handle, "vkCreateInstance");
Mark Youngb6399312017-01-10 14:22:15 -07001542 if (NULL == fp_create_inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001543 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1544 "loader_scanned_icd_add: Failed querying "
1545 "\'vkCreateInstance\' via dlsym/loadlibrary for "
1546 "ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001547 filename);
Mark Young3a587792016-08-19 15:25:08 -06001548 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001549 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001550 fp_get_inst_ext_props = loader_platform_get_proc_address(handle, "vkEnumerateInstanceExtensionProperties");
Mark Youngb6399312017-01-10 14:22:15 -07001551 if (NULL == fp_get_inst_ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001552 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1553 "loader_scanned_icd_add: Could not get \'vkEnumerate"
1554 "InstanceExtensionProperties\' via dlsym/loadlibrary "
1555 "for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001556 filename);
Mark Young3a587792016-08-19 15:25:08 -06001557 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001558 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001559 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001560 // Use newer interface version 1 or later
Mark Young39389872017-01-19 21:10:49 -07001561 if (interface_vers == 0) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001562 interface_vers = 1;
Mark Young39389872017-01-19 21:10:49 -07001563 }
Jon Ashburn17b4c862016-04-25 11:09:37 -06001564
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001565 fp_create_inst = (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Mark Youngb6399312017-01-10 14:22:15 -07001566 if (NULL == fp_create_inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001567 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1568 "loader_scanned_icd_add: Could not get "
1569 "\'vkCreateInstance\' via \'vk_icdGetInstanceProcAddr\'"
1570 " for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001571 filename);
Mark Young3a587792016-08-19 15:25:08 -06001572 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001573 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001574 fp_get_inst_ext_props =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001575 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(NULL, "vkEnumerateInstanceExtensionProperties");
Mark Youngb6399312017-01-10 14:22:15 -07001576 if (NULL == fp_get_inst_ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001577 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1578 "loader_scanned_icd_add: Could not get \'vkEnumerate"
1579 "InstanceExtensionProperties\' via "
1580 "\'vk_icdGetInstanceProcAddr\' for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001581 filename);
Mark Young3a587792016-08-19 15:25:08 -06001582 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001583 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001584 fp_get_phys_dev_proc_addr = loader_platform_get_proc_address(handle, "vk_icdGetPhysicalDeviceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001585 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001586
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001587 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001588 if ((icd_tramp_list->count * sizeof(struct loader_scanned_icd)) >= icd_tramp_list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001589 icd_tramp_list->scanned_list =
1590 loader_instance_heap_realloc(inst, icd_tramp_list->scanned_list, icd_tramp_list->capacity, icd_tramp_list->capacity * 2,
1591 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001592 if (NULL == icd_tramp_list->scanned_list) {
Mark Young3a587792016-08-19 15:25:08 -06001593 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001594 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1595 "loader_scanned_icd_add: Realloc failed on icd library"
1596 " list for ICD %s",
Mark Youngb6399312017-01-10 14:22:15 -07001597 filename);
Mark Young3a587792016-08-19 15:25:08 -06001598 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001599 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001600 // double capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001601 icd_tramp_list->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001602 }
1603
Mark Young39389872017-01-19 21:10:49 -07001604 new_scanned_icd = &(icd_tramp_list->scanned_list[icd_tramp_list->count]);
Mark Young0153e0b2016-11-03 14:27:13 -06001605 new_scanned_icd->handle = handle;
1606 new_scanned_icd->api_version = api_version;
1607 new_scanned_icd->GetInstanceProcAddr = fp_get_proc_addr;
Mark Young39389872017-01-19 21:10:49 -07001608 new_scanned_icd->GetPhysicalDeviceProcAddr = fp_get_phys_dev_proc_addr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001609 new_scanned_icd->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Mark Young0153e0b2016-11-03 14:27:13 -06001610 new_scanned_icd->CreateInstance = fp_create_inst;
1611 new_scanned_icd->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001612
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001613 new_scanned_icd->lib_name = (char *)loader_instance_heap_alloc(inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001614 if (NULL == new_scanned_icd->lib_name) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001615 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_scanned_icd_add: Out of memory can't add ICD %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001616 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001617 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001618 }
Mark Young0153e0b2016-11-03 14:27:13 -06001619 strcpy(new_scanned_icd->lib_name, filename);
1620 icd_tramp_list->count++;
Mark Young3a587792016-08-19 15:25:08 -06001621
1622out:
1623
1624 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001625}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001626
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001627static bool loader_icd_init_entrys(struct loader_icd_term *icd_term, VkInstance inst, const PFN_vkGetInstanceProcAddr fp_gipa) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001628/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001629
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001630#define LOOKUP_GIPA(func, required) \
1631 do { \
1632 icd_term->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1633 if (!icd_term->func && required) { \
1634 loader_log((struct loader_instance *)inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
1635 loader_platform_get_proc_address_error("vk" #func)); \
1636 return false; \
1637 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001638 } while (0)
1639
Jon Ashburnc624c882015-07-16 10:17:29 -06001640 LOOKUP_GIPA(GetDeviceProcAddr, true);
1641 LOOKUP_GIPA(DestroyInstance, true);
1642 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1643 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1644 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001645 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001646 LOOKUP_GIPA(CreateDevice, true);
1647 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1648 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001649 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001650 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001651 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Ian Elliott7e40db92015-08-21 15:09:33 -06001652 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001653 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1654 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1655 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001656 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1657 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1658 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1659 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1660 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1661 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1662 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1663 LOOKUP_GIPA(DestroySurfaceKHR, false);
Mark Young16573c72016-06-28 10:52:43 -06001664 LOOKUP_GIPA(CreateSwapchainKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001665#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Young16573c72016-06-28 10:52:43 -06001666 LOOKUP_GIPA(CreateWin32SurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001667 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1668#endif
1669#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001670 LOOKUP_GIPA(CreateXcbSurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001671 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1672#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001673#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001674 LOOKUP_GIPA(CreateXlibSurfaceKHR, false);
Karl Schultz65d20182016-03-08 07:55:27 -07001675 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1676#endif
Mark Younga7c51fd2016-09-16 10:18:42 -06001677#ifdef VK_USE_PLATFORM_MIR_KHR
1678 LOOKUP_GIPA(CreateMirSurfaceKHR, false);
1679 LOOKUP_GIPA(GetPhysicalDeviceMirPresentationSupportKHR, false);
1680#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001681#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Young16573c72016-06-28 10:52:43 -06001682 LOOKUP_GIPA(CreateWaylandSurfaceKHR, false);
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001683 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1684#endif
Mark Young39389872017-01-19 21:10:49 -07001685 LOOKUP_GIPA(CreateSharedSwapchainsKHR, false);
1686
1687 // KHR_get_physical_device_properties2
1688 LOOKUP_GIPA(GetPhysicalDeviceFeatures2KHR, false);
1689 LOOKUP_GIPA(GetPhysicalDeviceProperties2KHR, false);
1690 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties2KHR, false);
1691 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties2KHR, false);
1692 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties2KHR, false);
1693 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties2KHR, false);
1694 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties2KHR, false);
1695 // EXT_debug_marker (items needing a trampoline/terminator)
1696 LOOKUP_GIPA(DebugMarkerSetObjectTagEXT, false);
1697 LOOKUP_GIPA(DebugMarkerSetObjectNameEXT, false);
1698
1699#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
1700 // EXT_acquire_xlib_display
1701 LOOKUP_GIPA(AcquireXlibDisplayEXT, false);
1702 LOOKUP_GIPA(GetRandROutputDisplayEXT, false);
1703#endif
1704
1705 // EXT_debug_report
1706 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1707 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
1708 LOOKUP_GIPA(DebugReportMessageEXT, false);
1709
1710 // EXT_direct_mode_display
1711 LOOKUP_GIPA(ReleaseDisplayEXT, false);
1712
1713 // EXT_display_surface_counter
1714 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilities2EXT, false);
1715
Mark Youngfa552782016-12-12 16:14:55 -07001716 // NV_external_memory_capabilities
James Jones389dc0c2016-08-18 23:41:19 +01001717 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Mark Youngfa552782016-12-12 16:14:55 -07001718 // NVX_device_generated_commands
1719 LOOKUP_GIPA(GetPhysicalDeviceGeneratedCommandsPropertiesNVX, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001720
Jon Ashburnc624c882015-07-16 10:17:29 -06001721#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001722
Jon Ashburnc624c882015-07-16 10:17:29 -06001723 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001724}
1725
Jon Ashburn23d36b12016-02-02 17:47:28 -07001726static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001727 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001728
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001729 if (g_loader_debug > 0) return;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001730
1731 g_loader_debug = 0;
1732
1733 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001734 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001735 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001736 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001737 size_t len;
1738
1739 if (p)
1740 len = p - env;
1741 else
1742 len = strlen(env);
1743
1744 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001745 if (strncmp(env, "all", len) == 0) {
1746 g_loader_debug = ~0u;
1747 g_loader_log_msgs = ~0u;
1748 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001749 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001750 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001751 } else if (strncmp(env, "info", len) == 0) {
1752 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001753 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001754 } else if (strncmp(env, "perf", len) == 0) {
1755 g_loader_debug |= LOADER_PERF_BIT;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001756 g_loader_log_msgs |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001757 } else if (strncmp(env, "error", len) == 0) {
1758 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001759 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001760 } else if (strncmp(env, "debug", len) == 0) {
1761 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001762 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001763 }
1764 }
1765
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001766 if (!p) break;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001767
1768 env = p + 1;
1769 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001770
Mark Young0ad83132016-06-30 13:02:42 -06001771 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001772}
1773
Jon Ashburn23d36b12016-02-02 17:47:28 -07001774void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001775 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001776 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001777 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001778
1779 // initialize logging
1780 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001781
1782 // initial cJSON to use alloc callbacks
1783 cJSON_Hooks alloc_fns = {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001784 .malloc_fn = loader_instance_tls_heap_alloc, .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001785 };
1786 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001787}
1788
Jon Ashburn2077e382015-06-29 11:25:34 -06001789struct loader_manifest_files {
1790 uint32_t count;
1791 char **filename_list;
1792};
1793
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001794/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001795 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001796 *
1797 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001798 * A pointer to first char in the next path.
1799 * The next path (or NULL) in the list is returned in next_path.
1800 * Note: input string is modified in some cases. PASS IN A COPY!
1801 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001802static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001803 uint32_t len;
1804 char *next;
1805
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001806 if (path == NULL) return NULL;
Frank Henigman57173102016-11-24 22:15:20 -05001807 next = strchr(path, PATH_SEPARATOR);
Jon Ashburn2077e382015-06-29 11:25:34 -06001808 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001809 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001810 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001811 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001812 *next = '\0';
1813 next++;
1814 }
1815
1816 return next;
1817}
1818
1819/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001820 * Given a path which is absolute or relative, expand the path if relative or
1821 * leave the path unmodified if absolute. The base path to prepend to relative
1822 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001823 *
1824 * \returns
1825 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001826 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001827static void loader_expand_path(const char *path, const char *rel_base, size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001828 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001829 // do not prepend a base to an absolute path
1830 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001831 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001832
1833 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001834}
1835
1836/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001837 * Given a filename (file) and a list of paths (dir), try to find an existing
1838 * file in the paths. If filename already is a path then no
1839 * searching in the given paths.
1840 *
1841 * \returns
1842 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001843 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001844static void loader_get_fullpath(const char *file, const char *dirs, size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001845 if (!loader_platform_is_path(file) && *dirs) {
1846 char *dirs_copy, *dir, *next_dir;
1847
1848 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1849 strcpy(dirs_copy, dirs);
1850
Jon Ashburn23d36b12016-02-02 17:47:28 -07001851 // find if file exists after prepending paths in given list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001852 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir)); dir = next_dir) {
1853 loader_platform_combine_path(out_fullpath, out_size, dir, file, NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001854 if (loader_platform_file_exists(out_fullpath)) {
1855 return;
1856 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001857 }
1858 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001859
Karl Schultze2ef9e62017-01-13 14:01:35 -07001860 (void)snprintf(out_fullpath, out_size, "%s", file);
Jon Ashburn2077e382015-06-29 11:25:34 -06001861}
1862
1863/**
1864 * Read a JSON file into a buffer.
1865 *
1866 * \returns
1867 * A pointer to a cJSON object representing the JSON parse tree.
1868 * This returned buffer should be freed by caller.
1869 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001870static VkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json) {
Mark Young3a587792016-08-19 15:25:08 -06001871 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001872 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001873 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001874 VkResult res = VK_SUCCESS;
1875
1876 if (NULL == json) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001877 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Received invalid JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001878 res = VK_ERROR_INITIALIZATION_FAILED;
1879 goto out;
1880 }
1881
1882 *json = NULL;
1883
Jon Ashburn23d36b12016-02-02 17:47:28 -07001884 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001885 if (!file) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001886 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Failed to open JSON file %s", filename);
Mark Youngb6399312017-01-10 14:22:15 -07001887 res = VK_ERROR_INITIALIZATION_FAILED;
Mark Young3a587792016-08-19 15:25:08 -06001888 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001889 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001890 fseek(file, 0, SEEK_END);
1891 len = ftell(file);
1892 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001893 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001894 if (json_buf == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001895 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1896 "loader_get_json: Failed to allocate space for "
1897 "JSON file %s buffer of length %d",
Mark Youngb6399312017-01-10 14:22:15 -07001898 filename, len);
1899 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001900 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001901 }
1902 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001903 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Failed to read JSON file %s.", filename);
Mark Youngb6399312017-01-10 14:22:15 -07001904 res = VK_ERROR_INITIALIZATION_FAILED;
Mark Young3a587792016-08-19 15:25:08 -06001905 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001906 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001907 json_buf[len] = '\0';
1908
Jon Ashburn23d36b12016-02-02 17:47:28 -07001909 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001910 *json = cJSON_Parse(json_buf);
1911 if (*json == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001912 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1913 "loader_get_json: Failed to parse JSON file %s, "
1914 "this is usually because something ran out of "
1915 "memory.",
Mark Youngb6399312017-01-10 14:22:15 -07001916 filename);
1917 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001918 goto out;
1919 }
1920
1921out:
1922 if (NULL != file) {
1923 fclose(file);
1924 }
1925
1926 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001927}
1928
1929/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001930 * Do a deep copy of the loader_layer_properties structure.
1931 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001932VkResult loader_copy_layer_properties(const struct loader_instance *inst, struct loader_layer_properties *dst,
Mark Young0ad83132016-06-30 13:02:42 -06001933 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001934 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001935 memcpy(dst, src, sizeof(*src));
Mark Youngb6399312017-01-10 14:22:15 -07001936 dst->instance_extension_list.list = loader_instance_heap_alloc(
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001937 inst, sizeof(VkExtensionProperties) * src->instance_extension_list.count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001938 if (NULL == dst->instance_extension_list.list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001939 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1940 "loader_copy_layer_properties: Failed to allocate space "
1941 "for instance extension list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07001942 src->instance_extension_list.count);
Mark Young0ad83132016-06-30 13:02:42 -06001943 return VK_ERROR_OUT_OF_HOST_MEMORY;
1944 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001945 dst->instance_extension_list.capacity = sizeof(VkExtensionProperties) * src->instance_extension_list.count;
1946 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list, dst->instance_extension_list.capacity);
Mark Youngb6399312017-01-10 14:22:15 -07001947 dst->device_extension_list.list = loader_instance_heap_alloc(
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001948 inst, sizeof(struct loader_dev_ext_props) * src->device_extension_list.count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001949 if (NULL == dst->device_extension_list.list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001950 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1951 "loader_copy_layer_properties: Failed to allocate space "
1952 "for device extension list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07001953 src->device_extension_list.count);
Mark Young0ad83132016-06-30 13:02:42 -06001954 return VK_ERROR_OUT_OF_HOST_MEMORY;
1955 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001956 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) * src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001957
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001958 dst->device_extension_list.capacity = sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
1959 memcpy(dst->device_extension_list.list, src->device_extension_list.list, dst->device_extension_list.capacity);
1960 if (src->device_extension_list.count > 0 && src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001961 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001962 dst->device_extension_list.list->entrypoints =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001963 loader_instance_heap_alloc(inst, sizeof(char *) * cnt, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001964 if (NULL == dst->device_extension_list.list->entrypoints) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001965 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1966 "loader_copy_layer_properties: Failed to allocate space "
1967 "for device extension entrypoint list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07001968 cnt);
Mark Young0ad83132016-06-30 13:02:42 -06001969 return VK_ERROR_OUT_OF_HOST_MEMORY;
1970 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001971 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
Mark Young0ad83132016-06-30 13:02:42 -06001972
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001973 for (i = 0; i < cnt; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001974 dst->device_extension_list.list->entrypoints[i] = loader_instance_heap_alloc(
1975 inst, strlen(src->device_extension_list.list->entrypoints[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001976 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001977 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1978 "loader_copy_layer_properties: Failed to "
1979 "allocate space for device extension entrypoint "
1980 "%d name of length",
Mark Youngb6399312017-01-10 14:22:15 -07001981 i);
Mark Young0ad83132016-06-30 13:02:42 -06001982 return VK_ERROR_OUT_OF_HOST_MEMORY;
1983 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001984 strcpy(dst->device_extension_list.list->entrypoints[i], src->device_extension_list.list->entrypoints[i]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001985 }
1986 }
Mark Young0ad83132016-06-30 13:02:42 -06001987
1988 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001989}
1990
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001991static bool loader_find_layer_name_list(const char *name, const struct loader_layer_list *layer_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001992 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001993 for (uint32_t j = 0; j < layer_list->count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001994 if (!strcmp(name, layer_list->list[j].info.layerName)) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001995 return false;
1996}
1997
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001998static bool loader_find_layer_name(const char *name, uint32_t layer_count, const char **layer_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001999 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002000 for (uint32_t j = 0; j < layer_count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002001 if (!strcmp(name, layer_list[j])) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002002 return false;
2003}
2004
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002005bool loader_find_layer_name_array(const char *name, uint32_t layer_count, const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002006 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002007 for (uint32_t j = 0; j < layer_count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002008 if (!strcmp(name, layer_list[j])) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002009 return false;
2010}
2011
2012/**
2013 * Searches through an array of layer names (ppp_layer_names) looking for a
2014 * layer key_name.
2015 * If not found then simply returns updating nothing.
2016 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12002017 * Any duplicate (pre-existing) expand_names in layer names are removed.
2018 * Order is otherwise preserved, with the layer key_name being replaced by the
2019 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07002020 * @param inst
2021 * @param layer_count
2022 * @param ppp_layer_names
2023 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002024VkResult loader_expand_layer_names(struct loader_instance *inst, const char *key_name, uint32_t expand_count,
2025 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE], uint32_t *layer_count,
2026 char const *const **ppp_layer_names) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002027 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002028
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002029 if (!loader_find_layer_name(key_name, *layer_count, (char const **)pp_src_layers)) {
Jon Ashburn491cd042016-05-16 14:01:18 -06002030 inst->activated_layers_are_std_val = false;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002031 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06002032 }
Jon Ashburn71483442016-02-11 18:59:43 -07002033
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002034 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Found meta layer %s, replacing with actual layer group", key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12002035
Jon Ashburn491cd042016-05-16 14:01:18 -06002036 inst->activated_layers_are_std_val = true;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002037 char const **pp_dst_layers = loader_instance_heap_alloc(inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2038 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002039 if (NULL == pp_dst_layers) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002040 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2041 "loader_expand_layer_names:: Failed to allocate space for "
2042 "std_validation layer names in pp_dst_layers.");
Mark Young0ad83132016-06-30 13:02:42 -06002043 return VK_ERROR_OUT_OF_HOST_MEMORY;
2044 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002045
2046 // copy layers from src to dst, stripping key_name and anything in
2047 // expand_names.
2048 uint32_t src_index, dst_index = 0;
2049 for (src_index = 0; src_index < *layer_count; src_index++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002050 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count, expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002051 continue;
2052 }
2053
2054 if (!strcmp(pp_src_layers[src_index], key_name)) {
2055 // insert all expand_names in place of key_name
2056 uint32_t expand_index;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002057 for (expand_index = 0; expand_index < expand_count; expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002058 pp_dst_layers[dst_index++] = expand_names[expand_index];
2059 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002060 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002061 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002062
2063 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002064 }
2065
Chris Forbesbd9de052016-04-06 20:49:02 +12002066 *ppp_layer_names = pp_dst_layers;
2067 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002068
2069 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002070}
2071
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002072void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst, const VkInstanceCreateInfo *orig,
Chris Forbesbd9de052016-04-06 20:49:02 +12002073 VkInstanceCreateInfo *ours) {
2074 /* Free the layer names array iff we had to reallocate it */
2075 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002076 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002077 }
2078}
2079
Jon Ashburn491cd042016-05-16 14:01:18 -06002080void loader_init_std_validation_props(struct loader_layer_properties *props) {
2081 memset(props, 0, sizeof(struct loader_layer_properties));
2082 props->type = VK_LAYER_TYPE_META_EXPLICT;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002083 strncpy(props->info.description, "LunarG Standard Validation Layer", sizeof(props->info.description));
Jon Ashburn491cd042016-05-16 14:01:18 -06002084 props->info.implementationVersion = 1;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002085 strncpy(props->info.layerName, std_validation_str, sizeof(props->info.layerName));
Jon Ashburn491cd042016-05-16 14:01:18 -06002086 // TODO what about specVersion? for now insert loader's built version
2087 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2088}
2089
Jon Ashburn86a527a2016-02-10 20:59:26 -07002090/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002091 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002092 * the set of required layer names. If found then it adds a meta property to the
2093 * layer list.
2094 * Assumes the required layers are the same for both instance and device lists.
2095 * @param inst
2096 * @param layer_count number of layers in layer_names
2097 * @param layer_names array of required layer names
2098 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002099 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002100static void loader_add_layer_property_meta(const struct loader_instance *inst, uint32_t layer_count,
2101 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
2102 struct loader_layer_list *layer_instance_list) {
Jon Ashburn491cd042016-05-16 14:01:18 -06002103 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002104 bool found;
2105 struct loader_layer_list *layer_list;
2106
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002107 if (0 == layer_count || (!layer_instance_list)) return;
2108 if (layer_instance_list && (layer_count > layer_instance_list->count)) return;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002109
Jon Ashburn491cd042016-05-16 14:01:18 -06002110 layer_list = layer_instance_list;
2111
2112 found = true;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002113 if (layer_list == NULL) return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002114 for (i = 0; i < layer_count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002115 if (loader_find_layer_name_list(layer_names[i], layer_list)) continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002116 found = false;
2117 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002118 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002119
2120 struct loader_layer_properties *props;
2121 if (found) {
2122 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002123 if (NULL == props) {
2124 // Error already triggered in loader_get_next_layer_property.
2125 return;
2126 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002127 loader_init_std_validation_props(props);
Jon Ashburn491cd042016-05-16 14:01:18 -06002128 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07002129}
2130
Mark Young39389872017-01-19 21:10:49 -07002131// This structure is used to store the json file version
2132// in a more managable way.
2133typedef struct {
2134 uint16_t major;
2135 uint16_t minor;
2136 uint16_t patch;
2137} layer_json_version;
2138
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002139static void loader_read_json_layer(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list,
2140 cJSON *layer_node, layer_json_version version, cJSON *item, cJSON *disable_environment,
2141 bool is_implicit, char *filename) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002142 char *temp;
2143 char *name, *type, *library_path, *api_version;
2144 char *implementation_version, *description;
2145 cJSON *ext_item;
2146 VkExtensionProperties ext_prop;
2147
Mark Young0ad83132016-06-30 13:02:42 -06002148/*
2149 * The following are required in the "layer" object:
2150 * (required) "name"
2151 * (required) "type"
2152 * (required) “library_path”
2153 * (required) “api_version”
2154 * (required) “implementation_version”
2155 * (required) “description”
2156 * (required for implicit layers) “disable_environment”
2157 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002158
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002159#define GET_JSON_OBJECT(node, var) \
2160 { \
2161 var = cJSON_GetObjectItem(node, #var); \
2162 if (var == NULL) { \
2163 layer_node = layer_node->next; \
2164 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2165 "Didn't find required layer object %s in manifest " \
2166 "JSON file, skipping this layer", \
2167 #var); \
2168 return; \
2169 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002170 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002171#define GET_JSON_ITEM(node, var) \
2172 { \
2173 item = cJSON_GetObjectItem(node, #var); \
2174 if (item == NULL) { \
2175 layer_node = layer_node->next; \
2176 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2177 "Didn't find required layer value %s in manifest JSON " \
2178 "file, skipping this layer", \
2179 #var); \
2180 return; \
2181 } \
2182 temp = cJSON_Print(item); \
2183 if (temp == NULL) { \
2184 layer_node = layer_node->next; \
2185 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2186 "Problem accessing layer value %s in manifest JSON " \
2187 "file, skipping this layer", \
2188 #var); \
2189 return; \
2190 } \
2191 temp[strlen(temp) - 1] = '\0'; \
2192 var = loader_stack_alloc(strlen(temp) + 1); \
2193 strcpy(var, &temp[1]); \
2194 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002195 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002196 GET_JSON_ITEM(layer_node, name)
2197 GET_JSON_ITEM(layer_node, type)
2198 GET_JSON_ITEM(layer_node, library_path)
2199 GET_JSON_ITEM(layer_node, api_version)
2200 GET_JSON_ITEM(layer_node, implementation_version)
2201 GET_JSON_ITEM(layer_node, description)
2202 if (is_implicit) {
2203 GET_JSON_OBJECT(layer_node, disable_environment)
2204 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002205#undef GET_JSON_ITEM
2206#undef GET_JSON_OBJECT
2207
Mark Young39389872017-01-19 21:10:49 -07002208 // Add list entry
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002209 struct loader_layer_properties *props = NULL;
2210 if (!strcmp(type, "DEVICE")) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002211 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "Device layers are deprecated skipping this layer");
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002212 layer_node = layer_node->next;
2213 return;
2214 }
2215 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2216 // layers that must work with older loaders
2217 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2218 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002219 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002220 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002221 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002222 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002223 if (NULL == props) {
2224 // Error already triggered in loader_get_next_layer_property.
2225 return;
2226 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002227 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002228 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002229
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002230 if (props == NULL) {
2231 layer_node = layer_node->next;
2232 return;
2233 }
Jon Ashburn15315172015-07-07 15:06:25 -06002234
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002235 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2236 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2237
2238 char *fullpath = props->lib_name;
2239 char *rel_base;
2240 if (loader_platform_is_path(library_path)) {
Mark Young39389872017-01-19 21:10:49 -07002241 // A relative or absolute path
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002242 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2243 strcpy(name_copy, filename);
2244 rel_base = loader_platform_dirname(name_copy);
2245 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2246 } else {
Mark Young39389872017-01-19 21:10:49 -07002247 // A filename which is assumed in a system directory
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002248 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH, MAX_STRING_SIZE, fullpath);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002249 }
2250 props->info.specVersion = loader_make_version(api_version);
2251 props->info.implementationVersion = atoi(implementation_version);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002252 strncpy((char *)props->info.description, description, sizeof(props->info.description));
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002253 props->info.description[sizeof(props->info.description) - 1] = '\0';
2254 if (is_implicit) {
2255 if (!disable_environment || !disable_environment->child) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002256 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2257 "Didn't find required layer child value disable_environment"
2258 "in manifest JSON file, skipping this layer");
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002259 layer_node = layer_node->next;
2260 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002261 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002262 strncpy(props->disable_env_var.name, disable_environment->child->string, sizeof(props->disable_env_var.name));
2263 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] = '\0';
2264 strncpy(props->disable_env_var.value, disable_environment->child->valuestring, sizeof(props->disable_env_var.value));
2265 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002266 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002267
Jon Ashburn23d36b12016-02-02 17:47:28 -07002268/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002269* Now get all optional items and objects and put in list:
2270* functions
2271* instance_extensions
2272* device_extensions
2273* enable_environment (implicit layers only)
2274*/
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002275#define GET_JSON_OBJECT(node, var) \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002276 { var = cJSON_GetObjectItem(node, #var); }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002277#define GET_JSON_ITEM(node, var) \
2278 { \
2279 item = cJSON_GetObjectItem(node, #var); \
2280 if (item != NULL) { \
2281 temp = cJSON_Print(item); \
2282 if (temp != NULL) { \
2283 temp[strlen(temp) - 1] = '\0'; \
2284 var = loader_stack_alloc(strlen(temp) + 1); \
2285 strcpy(var, &temp[1]); \
2286 cJSON_Free(temp); \
2287 } \
2288 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002289 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002290
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002291 cJSON *instance_extensions, *device_extensions, *functions, *enable_environment;
Mark Young39389872017-01-19 21:10:49 -07002292 cJSON *entrypoints = NULL;
2293 char *vkGetInstanceProcAddr = NULL;
2294 char *vkGetDeviceProcAddr = NULL;
2295 char *vkNegotiateLoaderLayerInterfaceVersion = NULL;
2296 char *spec_version = NULL;
2297 char **entry_array = NULL;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002298 entrypoints = NULL;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002299 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002300
Mark Young39389872017-01-19 21:10:49 -07002301 // Layer interface functions
2302 // vkGetInstanceProcAddr
2303 // vkGetDeviceProcAddr
2304 // vkNegotiateLoaderLayerInterfaceVersion (starting with JSON file 1.1.0)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002305 GET_JSON_OBJECT(layer_node, functions)
2306 if (functions != NULL) {
Mark Young39389872017-01-19 21:10:49 -07002307 if (version.major > 1 || version.minor >= 1) {
2308 GET_JSON_ITEM(functions, vkNegotiateLoaderLayerInterfaceVersion)
2309 if (vkNegotiateLoaderLayerInterfaceVersion != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002310 strncpy(props->functions.str_negotiate_interface, vkNegotiateLoaderLayerInterfaceVersion,
Mark Young39389872017-01-19 21:10:49 -07002311 sizeof(props->functions.str_negotiate_interface));
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002312 props->functions.str_negotiate_interface[sizeof(props->functions.str_negotiate_interface) - 1] = '\0';
Mark Young39389872017-01-19 21:10:49 -07002313 } else {
2314 props->functions.str_negotiate_interface[0] = '\0';
2315 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002316 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2317 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
Mark Young39389872017-01-19 21:10:49 -07002318 if (vkGetInstanceProcAddr != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002319 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr, sizeof(props->functions.str_gipa));
Mark Young39389872017-01-19 21:10:49 -07002320 if (version.major > 1 || version.minor >= 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002321 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2322 "Indicating layer-specific vkGetInstanceProcAddr "
2323 "function is deprecated starting with JSON file "
2324 "version 1.1.0. Instead, use the new "
2325 "vkNegotiateLayerInterfaceVersion function to "
2326 "return the GetInstanceProcAddr function for this"
2327 "layer");
Mark Young39389872017-01-19 21:10:49 -07002328 }
2329 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002330 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
Mark Young39389872017-01-19 21:10:49 -07002331 if (vkGetDeviceProcAddr != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002332 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr, sizeof(props->functions.str_gdpa));
Mark Young39389872017-01-19 21:10:49 -07002333 if (version.major > 1 || version.minor >= 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002334 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2335 "Indicating layer-specific vkGetDeviceProcAddr "
2336 "function is deprecated starting with JSON file "
2337 "version 1.1.0. Instead, use the new "
2338 "vkNegotiateLayerInterfaceVersion function to "
2339 "return the GetDeviceProcAddr function for this"
2340 "layer");
Mark Young39389872017-01-19 21:10:49 -07002341 }
2342 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002343 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2344 }
Mark Young39389872017-01-19 21:10:49 -07002345
2346 // instance_extensions
2347 // array of {
2348 // name
2349 // spec_version
2350 // }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002351 GET_JSON_OBJECT(layer_node, instance_extensions)
2352 if (instance_extensions != NULL) {
2353 int count = cJSON_GetArraySize(instance_extensions);
2354 for (i = 0; i < count; i++) {
2355 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2356 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002357 if (name != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002358 strncpy(ext_prop.extensionName, name, sizeof(ext_prop.extensionName));
2359 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002360 }
Mark Young0ad83132016-06-30 13:02:42 -06002361 GET_JSON_ITEM(ext_item, spec_version)
2362 if (NULL != spec_version) {
2363 ext_prop.specVersion = atoi(spec_version);
2364 } else {
2365 ext_prop.specVersion = 0;
2366 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002367 bool ext_unsupported = wsi_unsupported_instance_extension(&ext_prop);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002368 if (!ext_unsupported) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002369 loader_add_to_ext_list(inst, &props->instance_extension_list, 1, &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002370 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002371 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002372 }
Mark Young39389872017-01-19 21:10:49 -07002373
2374 // device_extensions
2375 // array of {
2376 // name
2377 // spec_version
2378 // entrypoints
2379 // }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002380 GET_JSON_OBJECT(layer_node, device_extensions)
2381 if (device_extensions != NULL) {
2382 int count = cJSON_GetArraySize(device_extensions);
2383 for (i = 0; i < count; i++) {
2384 ext_item = cJSON_GetArrayItem(device_extensions, i);
2385 GET_JSON_ITEM(ext_item, name)
2386 GET_JSON_ITEM(ext_item, spec_version)
2387 if (name != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002388 strncpy(ext_prop.extensionName, name, sizeof(ext_prop.extensionName));
2389 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002390 }
Mark Young0ad83132016-06-30 13:02:42 -06002391 if (NULL != spec_version) {
2392 ext_prop.specVersion = atoi(spec_version);
2393 } else {
2394 ext_prop.specVersion = 0;
2395 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002396 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2397 GET_JSON_OBJECT(ext_item, entrypoints)
2398 int entry_count;
2399 if (entrypoints == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002400 loader_add_to_dev_ext_list(inst, &props->device_extension_list, &ext_prop, 0, NULL);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002401 continue;
2402 }
2403 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002404 if (entry_count) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002405 entry_array = (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002406 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002407 for (j = 0; j < entry_count; j++) {
2408 ext_item = cJSON_GetArrayItem(entrypoints, j);
2409 if (ext_item != NULL) {
2410 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002411 if (NULL == temp) {
2412 entry_array[j] = NULL;
2413 continue;
2414 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002415 temp[strlen(temp) - 1] = '\0';
2416 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2417 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002418 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002419 }
2420 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002421 loader_add_to_dev_ext_list(inst, &props->device_extension_list, &ext_prop, entry_count, entry_array);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002422 }
2423 }
2424 if (is_implicit) {
2425 GET_JSON_OBJECT(layer_node, enable_environment)
2426
2427 // enable_environment is optional
2428 if (enable_environment) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002429 strncpy(props->enable_env_var.name, enable_environment->child->string, sizeof(props->enable_env_var.name));
2430 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] = '\0';
2431 strncpy(props->enable_env_var.value, enable_environment->child->valuestring, sizeof(props->enable_env_var.value));
2432 props->enable_env_var.value[sizeof(props->enable_env_var.value) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002433 }
2434 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002435#undef GET_JSON_ITEM
2436#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002437}
2438
Mark Young39389872017-01-19 21:10:49 -07002439static inline bool is_valid_layer_json_version(const layer_json_version *layer_json) {
2440 // Supported versions are: 1.0.0, 1.0.1, and 1.1.0.
2441 if ((layer_json->major == 1 && layer_json->minor == 1 && layer_json->patch == 0) ||
2442 (layer_json->major == 1 && layer_json->minor == 0 && layer_json->patch < 2)) {
2443 return true;
2444 }
2445 return false;
2446}
2447
2448static inline bool layer_json_supports_layers_tag(const layer_json_version *layer_json) {
2449 // Supported versions started in 1.0.1, so anything newer
2450 if ((layer_json->major > 1 || layer_json->minor > 0 || layer_json->patch > 1)) {
2451 return true;
2452 }
2453 return false;
2454}
2455
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002456/**
2457 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2458 * file, add entry to the layer_list. Fill out the layer_properties in this list
2459 * entry from the input cJSON object.
2460 *
2461 * \returns
2462 * void
2463 * layer_list has a new entry and initialized accordingly.
2464 * If the json input object does not have all the required fields no entry
2465 * is added to the list.
2466 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002467static void loader_add_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list,
2468 cJSON *json, bool is_implicit, char *filename) {
Mark Young39389872017-01-19 21:10:49 -07002469 // The following Fields in layer manifest file that are required:
2470 // - “file_format_version”
2471 // - If more than one "layer" object are used, then the "layers" array is
2472 // requred
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002473
2474 cJSON *item, *layers_node, *layer_node;
Mark Young39389872017-01-19 21:10:49 -07002475 layer_json_version json_version;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002476 char *vers_tok;
2477 cJSON *disable_environment = NULL;
2478 item = cJSON_GetObjectItem(json, "file_format_version");
2479 if (item == NULL) {
2480 return;
2481 }
2482 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002483 if (NULL == file_vers) {
2484 return;
2485 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002486 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Found manifest file %s, version %s", filename, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002487 // Get the major/minor/and patch as integers for easier comparison
2488 vers_tok = strtok(file_vers, ".\"\n\r");
2489 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002490 json_version.major = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002491 vers_tok = strtok(NULL, ".\"\n\r");
2492 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002493 json_version.minor = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002494 vers_tok = strtok(NULL, ".\"\n\r");
2495 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002496 json_version.patch = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002497 }
2498 }
2499 }
Mark Young39389872017-01-19 21:10:49 -07002500
2501 if (!is_valid_layer_json_version(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002502 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2503 "loader_add_layer_properties: %s invalid layer "
2504 " manifest file version %d.%d.%d. May cause errors.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002505 filename, json_version.major, json_version.minor, json_version.patch);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002506 }
Mark Young0ad83132016-06-30 13:02:42 -06002507 cJSON_Free(file_vers);
Mark Young39389872017-01-19 21:10:49 -07002508
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002509 // If "layers" is present, read in the array of layer objects
2510 layers_node = cJSON_GetObjectItem(json, "layers");
2511 if (layers_node != NULL) {
2512 int numItems = cJSON_GetArraySize(layers_node);
Mark Young39389872017-01-19 21:10:49 -07002513 if (!layer_json_supports_layers_tag(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002514 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2515 "loader_add_layer_properties: \'layers\' tag not "
2516 "supported until file version 1.0.1, but %s is "
2517 "reporting version %s",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002518 filename, file_vers);
2519 }
2520 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2521 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2522 if (layer_node == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002523 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2524 "loader_add_layer_properties: Can not find "
2525 "\'layers\' array element %d object in manifest "
2526 "JSON file %s. Skipping this file",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002527 curLayer, filename);
2528 return;
2529 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002530 loader_read_json_layer(inst, layer_instance_list, layer_node, json_version, item, disable_environment, is_implicit,
2531 filename);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002532 }
2533 } else {
2534 // Otherwise, try to read in individual layers
2535 layer_node = cJSON_GetObjectItem(json, "layer");
2536 if (layer_node == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002537 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2538 "loader_add_layer_properties: Can not find \'layer\' "
2539 "object in manifest JSON file %s. Skipping this file.",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002540 filename);
2541 return;
2542 }
2543 // Loop through all "layer" objects in the file to get a count of them
2544 // first.
2545 uint16_t layer_count = 0;
2546 cJSON *tempNode = layer_node;
2547 do {
2548 tempNode = tempNode->next;
2549 layer_count++;
2550 } while (tempNode != NULL);
Mark Young39389872017-01-19 21:10:49 -07002551
2552 // Throw a warning if we encounter multiple "layer" objects in file
2553 // versions newer than 1.0.0. Having multiple objects with the same
2554 // name at the same level is actually a JSON standard violation.
2555 if (layer_count > 1 && layer_json_supports_layers_tag(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002556 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2557 "loader_add_layer_properties: Multiple \'layer\' nodes"
2558 " are deprecated starting in file version \"1.0.1\". "
2559 "Please use \'layers\' : [] array instead in %s.",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002560 filename);
2561 } else {
2562 do {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002563 loader_read_json_layer(inst, layer_instance_list, layer_node, json_version, item, disable_environment, is_implicit,
2564 filename);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002565 layer_node = layer_node->next;
2566 } while (layer_node != NULL);
2567 }
2568 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002569 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002570}
2571
2572/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002573 * Find the Vulkan library manifest files.
2574 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002575 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002576 * for a list of JSON manifest files. If env_override is non-NULL
2577 * and has a valid value. Then the location is ignored. Otherwise
2578 * location is used to look for manifest files. The location
2579 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002580 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002581 * directory to look at. It is expanded into the dir path
2582 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2583 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002584 *
2585 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002586 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002587 * A string list of manifest files to be opened in out_files param.
2588 * List has a pointer to string for each manifest filename.
2589 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002590 * Location or override string lists can be either files or directories as
2591 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002592 * | location | override
2593 * --------------------------------
2594 * Win ICD | files | files
2595 * Win Layer | files | dirs
2596 * Linux ICD | dirs | files
2597 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002598 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002599static VkResult loader_get_manifest_files(const struct loader_instance *inst, const char *env_override, const char *source_override,
Benjamin Saunders31a48012017-01-29 14:49:54 -08002600 bool is_layer, bool warn_if_not_present, const char *location, const char *relative_location,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002601 struct loader_manifest_files *out_files) {
Mark Young39389872017-01-19 21:10:49 -07002602 const char * override = NULL;
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002603 char *override_getenv = NULL;
Mark Young0ad83132016-06-30 13:02:42 -06002604 char *loc, *orig_loc = NULL;
2605 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002606 char *file, *next_file, *name;
2607 size_t alloced_count = 64;
2608 char full_path[2048];
2609 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002610 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002611 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002612 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002613
2614 out_files->count = 0;
2615 out_files->filename_list = NULL;
2616
Jamie Madill00c3c912016-04-06 18:26:46 -04002617 if (source_override != NULL) {
2618 override = source_override;
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002619 } else if (env_override != NULL) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002620#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002621 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002622 /* Don't allow setuid apps to use the env var: */
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002623 env_override = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002624 }
2625#endif
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002626 if (env_override != NULL) {
2627 override = override_getenv = loader_getenv(env_override, inst);
2628 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002629 }
2630
Jon Ashburnb6822212016-02-16 15:34:16 -07002631#if !defined(_WIN32)
Benjamin Saunders31a48012017-01-29 14:49:54 -08002632 if (relative_location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002633#else
Benjamin Saunders31a48012017-01-29 14:49:54 -08002634 relative_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002635 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002636#endif
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002637 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2638 "loader_get_manifest_files: Can not get manifest files with "
2639 "NULL location, env_override=%s",
Mark Youngb6399312017-01-10 14:22:15 -07002640 (env_override != NULL) ? env_override : "");
Mark Young0ad83132016-06-30 13:02:42 -06002641 res = VK_ERROR_INITIALIZATION_FAILED;
2642 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002643 }
2644
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002645#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002646 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002647#else
2648 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002649#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002650 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002651 // Also handle getting the location(s) from registry on Windows
2652 if (override == NULL) {
Benjamin Saunders31a48012017-01-29 14:49:54 -08002653 size_t loc_size = 0;
2654#if !defined(_WIN32)
2655 const char *xdgconfdirs = secure_getenv("XDG_CONFIG_DIRS");
2656 const char *xdgdatadirs = secure_getenv("XDG_DATA_DIRS");
2657 if (xdgconfdirs == NULL || xdgconfdirs[0] == '\0')
2658 xdgconfdirs = "/etc/xdg";
2659 if (xdgdatadirs == NULL || xdgdatadirs[0] == '\0')
2660 xdgdatadirs = "/usr/local/share:/usr/share";
2661 const size_t rel_size = strlen(relative_location);
2662 // Leave space for trailing separators
2663 loc_size += strlen(xdgconfdirs) + strlen(xdgdatadirs) + 2*rel_size + 2;
2664 for (const char *x = xdgconfdirs; *x; ++x)
2665 if (*x == PATH_SEPARATOR) loc_size += rel_size;
2666 for (const char *x = xdgdatadirs; *x; ++x)
2667 if (*x == PATH_SEPARATOR) loc_size += rel_size;
2668 loc_size += strlen(SYSCONFDIR) + rel_size + 1;
2669#if defined(EXTRASYSCONFDIR)
2670 loc_size += strlen(EXTRASYSCONFDIR) + rel_size + 1;
2671#endif
2672#else
2673 loc_size += strlen(location) + 1;
2674#endif
2675 loc = loader_stack_alloc(loc_size);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002676 if (loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002677 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2678 "loader_get_manifest_files: Failed to allocate "
2679 "%d bytes for manifest file location.",
Benjamin Saunders31a48012017-01-29 14:49:54 -08002680 loc_size);
Mark Young0ad83132016-06-30 13:02:42 -06002681 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2682 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002683 }
Benjamin Saunders31a48012017-01-29 14:49:54 -08002684 char *loc_write = loc;
2685#if !defined(_WIN32)
2686 const char *loc_read;
Karl Schultz52f5daa2017-02-12 12:34:03 -07002687 size_t start, stop;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002688
2689 loc_read = &xdgconfdirs[0];
Karl Schultz52f5daa2017-02-12 12:34:03 -07002690 start = 0;
2691 while (loc_read[start] != '\0') {
2692 while (loc_read[start] == PATH_SEPARATOR) {
2693 start++;
2694 }
2695 stop = start;
2696 while (loc_read[stop] != PATH_SEPARATOR && loc_read[stop] != '\0') {
2697 stop++;
2698 }
2699 const size_t s = stop - start;
2700 if (s) {
2701 memcpy(loc_write, &loc_read[start], s);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002702 loc_write += s;
2703 memcpy(loc_write, relative_location, rel_size);
2704 loc_write += rel_size;
2705 *loc_write++ = PATH_SEPARATOR;
Karl Schultz52f5daa2017-02-12 12:34:03 -07002706 start = stop;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002707 }
2708 }
2709
2710 memcpy(loc_write, SYSCONFDIR, strlen(SYSCONFDIR));
2711 loc_write += strlen(SYSCONFDIR);
2712 memcpy(loc_write, relative_location, rel_size);
2713 loc_write += rel_size;
2714 *loc_write++ = PATH_SEPARATOR;
2715
2716#if defined(EXTRASYSCONFDIR)
2717 memcpy(loc_write, EXTRASYSCONFDIR, strlen(EXTRASYSCONFDIR));
2718 loc_write += strlen(EXTRASYSCONFDIR);
2719 memcpy(loc_write, relative_location, rel_size);
2720 loc_write += rel_size;
2721 *loc_write++ = PATH_SEPARATOR;
2722#endif
2723
2724 loc_read = &xdgdatadirs[0];
Karl Schultz52f5daa2017-02-12 12:34:03 -07002725 start = 0;
2726 while (loc_read[start] != '\0') {
2727 while (loc_read[start] == PATH_SEPARATOR) {
2728 start++;
2729 }
2730 stop = start;
2731 while (loc_read[stop] != PATH_SEPARATOR && loc_read[stop] != '\0') {
2732 stop++;
2733 }
2734 const size_t s = stop - start;
2735 if (s) {
2736 memcpy(loc_write, &loc_read[start], s);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002737 loc_write += s;
2738 memcpy(loc_write, relative_location, rel_size);
2739 loc_write += rel_size;
2740 *loc_write++ = PATH_SEPARATOR;
Karl Schultz52f5daa2017-02-12 12:34:03 -07002741 start = stop;
Benjamin Saunders31a48012017-01-29 14:49:54 -08002742 }
2743 }
Karl Schultz52f5daa2017-02-12 12:34:03 -07002744
Benjamin Saunders31a48012017-01-29 14:49:54 -08002745 --loc_write;
2746#else
2747 memcpy(loc_write, location, strlen(location));
2748 loc_write += strlen(location);
2749#endif
2750 assert(loc_write - loc < (ptrdiff_t)loc_size);
2751 *loc_write = '\0';
2752
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002753#if defined(_WIN32)
Mark Young2c84c0c2017-01-13 10:27:03 -07002754 VkResult reg_result = loaderGetRegistryFiles(inst, loc, &reg);
2755 if (VK_SUCCESS != reg_result || NULL == reg) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002756 if (!is_layer) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002757 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2758 "loader_get_manifest_files: Registry lookup failed "
2759 "to get ICD manifest files. Possibly missing Vulkan"
2760 " driver?");
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002761 if (VK_SUCCESS == reg_result || VK_ERROR_OUT_OF_HOST_MEMORY == reg_result) {
Mark Young2c84c0c2017-01-13 10:27:03 -07002762 res = reg_result;
2763 } else {
2764 res = VK_ERROR_INCOMPATIBLE_DRIVER;
2765 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002766 } else {
Mark Youngf8c20102016-11-07 16:26:17 -07002767 if (warn_if_not_present) {
Mark Young2c84c0c2017-01-13 10:27:03 -07002768 // This is only a warning for layers
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002769 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2770 "loader_get_manifest_files: Registry lookup failed "
2771 "to get layer manifest files.");
Mark Youngf8c20102016-11-07 16:26:17 -07002772 }
Mark Young2c84c0c2017-01-13 10:27:03 -07002773 if (reg_result == VK_ERROR_OUT_OF_HOST_MEMORY) {
2774 res = reg_result;
2775 } else {
2776 // Return success for now since it's not critical for layers
2777 res = VK_SUCCESS;
2778 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002779 }
Mark Young0ad83132016-06-30 13:02:42 -06002780 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002781 }
Mark Young0ad83132016-06-30 13:02:42 -06002782 orig_loc = loc;
2783 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002784#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002785 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002786 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002787 if (loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002788 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2789 "loader_get_manifest_files: Failed to allocate space for "
2790 "override environment variable of length %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002791 strlen(override) + 1);
Mark Young0ad83132016-06-30 13:02:42 -06002792 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2793 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002794 }
2795 strcpy(loc, override);
2796 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002797
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002798 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002799 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002800
Jon Ashburn2077e382015-06-29 11:25:34 -06002801 file = loc;
2802 while (*file) {
2803 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002804 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002805 sysdir = opendir(file);
2806 name = NULL;
2807 if (sysdir) {
2808 dent = readdir(sysdir);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002809 if (dent == NULL) break;
Jon Ashburn2077e382015-06-29 11:25:34 -06002810 name = &(dent->d_name[0]);
2811 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2812 name = full_path;
2813 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002814 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002815#if defined(_WIN32)
2816 name = file;
2817#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002818 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002819 char *dir;
2820 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002821 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002822 if (dir == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002823 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2824 "loader_get_manifest_files: Failed to allocate "
2825 "space for relative location path length %d",
Mark Youngb6399312017-01-10 14:22:15 -07002826 strlen(loc) + 1);
Mark Young0ad83132016-06-30 13:02:42 -06002827 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002828 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002829 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002830
2831 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2832
2833 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002834#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002835 }
2836 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002837 /* Look for files ending with ".json" suffix */
2838 uint32_t nlen = (uint32_t)strlen(name);
2839 const char *suf = name + nlen - 5;
2840 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2841 if (out_files->count == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002842 out_files->filename_list =
2843 loader_instance_heap_alloc(inst, alloced_count * sizeof(char *), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002844 } else if (out_files->count == alloced_count) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002845 out_files->filename_list =
2846 loader_instance_heap_realloc(inst, out_files->filename_list, alloced_count * sizeof(char *),
2847 alloced_count * sizeof(char *) * 2, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002848 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002849 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002850 if (out_files->filename_list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002851 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2852 "loader_get_manifest_files: Failed to allocate "
2853 "space for manifest file name list");
Mark Young0ad83132016-06-30 13:02:42 -06002854 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2855 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002856 }
Mark Young0ad83132016-06-30 13:02:42 -06002857 out_files->filename_list[out_files->count] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002858 loader_instance_heap_alloc(inst, strlen(name) + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002859 if (out_files->filename_list[out_files->count] == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002860 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2861 "loader_get_manifest_files: Failed to allocate "
2862 "space for manifest file %d list",
Mark Youngb6399312017-01-10 14:22:15 -07002863 out_files->count);
Mark Young0ad83132016-06-30 13:02:42 -06002864 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2865 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002866 }
2867 strcpy(out_files->filename_list[out_files->count], name);
2868 out_files->count++;
2869 } else if (!list_is_dirs) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002870 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "Skipping manifest file %s, file name must end in .json",
2871 name);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002872 }
2873 if (list_is_dirs) {
2874 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002875 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002876 break;
Mark Young0ad83132016-06-30 13:02:42 -06002877 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002878 name = &(dent->d_name[0]);
2879 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2880 name = full_path;
2881 } else {
2882 break;
2883 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002884 }
Mark Young0ad83132016-06-30 13:02:42 -06002885 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002886 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002887 sysdir = NULL;
2888 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002889 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002890#if !defined(_WIN32)
Benjamin Saunders31a48012017-01-29 14:49:54 -08002891 if (relative_location != NULL && (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002892 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2893 size_t len;
2894 if (xdgdatahome != NULL) {
Mark Young6ef95f42017-02-17 09:02:23 -07002895 size_t alloc_len = strlen(xdgdatahome) + 2 + strlen(relative_location);
2896 char *home_loc = loader_stack_alloc(alloc_len);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002897 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002898 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2899 "loader_get_manifest_files: Failed to allocate "
2900 "space for manifest file XDG Home location");
Mark Young0ad83132016-06-30 13:02:42 -06002901 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2902 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002903 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002904 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002905 // Add directory separator if needed
Benjamin Saunders31a48012017-01-29 14:49:54 -08002906 if (relative_location[0] != DIRECTORY_SYMBOL) {
Jon Ashburn67e262e2016-02-18 12:45:39 -07002907 len = strlen(home_loc);
2908 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002909 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002910 }
Mark Young6ef95f42017-02-17 09:02:23 -07002911 strncat(home_loc, relative_location, alloc_len);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002912 file = home_loc;
2913 next_file = loader_get_next_path(file);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002914 relative_location = NULL;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002915
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002916 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
2917 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002918 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002919
2920 } else {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002921 char *home = secure_getenv("HOME");
2922 if (home != NULL) {
Mark Young6ef95f42017-02-17 09:02:23 -07002923 size_t alloc_len = strlen(home) + 16 + strlen(relative_location);
2924 char *home_loc = loader_stack_alloc(alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002925 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002926 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2927 "loader_get_manifest_files: Failed to allocate "
2928 "space for manifest file Home location");
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002929 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2930 goto out;
2931 }
Mark Young6ef95f42017-02-17 09:02:23 -07002932 strncpy(home_loc, home, alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002933
2934 len = strlen(home);
2935 if (home[len] != DIRECTORY_SYMBOL) {
2936 home_loc[len] = DIRECTORY_SYMBOL;
2937 home_loc[len + 1] = '\0';
2938 }
Mark Young6ef95f42017-02-17 09:02:23 -07002939 strncat(home_loc, ".local/share", alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002940
Benjamin Saunders31a48012017-01-29 14:49:54 -08002941 if (relative_location[0] != DIRECTORY_SYMBOL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002942 len = strlen(home_loc);
2943 home_loc[len] = DIRECTORY_SYMBOL;
2944 home_loc[len + 1] = '\0';
2945 }
Mark Young6ef95f42017-02-17 09:02:23 -07002946 strncat(home_loc, relative_location, alloc_len);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002947 file = home_loc;
2948 next_file = loader_get_next_path(file);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002949 relative_location = NULL;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002950
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002951 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
2952 home_loc);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002953 list_is_dirs = true;
2954 } else {
2955 // without knowing HOME, we just.. give up
2956 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002957 }
2958 }
2959#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002960 }
Mark Young0ad83132016-06-30 13:02:42 -06002961
2962out:
2963 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2964 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2965 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2966 }
2967 loader_instance_heap_free(inst, out_files->filename_list);
2968 out_files->count = 0;
2969 out_files->filename_list = NULL;
2970 }
2971
2972 if (NULL != sysdir) {
2973 closedir(sysdir);
2974 }
2975
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002976 if (override_getenv != NULL) {
2977 loader_free_getenv(override_getenv, inst);
2978 }
2979
Mark Young0ad83132016-06-30 13:02:42 -06002980 if (NULL != reg && reg != orig_loc) {
2981 loader_instance_heap_free(inst, reg);
2982 }
2983 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002984}
2985
Jon Ashburn23d36b12016-02-02 17:47:28 -07002986void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002987
Jon Ashburn23d36b12016-02-02 17:47:28 -07002988void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002989/**
2990 * Try to find the Vulkan ICD driver(s).
2991 *
2992 * This function scans the default system loader path(s) or path
2993 * specified by the \c VK_ICD_FILENAMES environment variable in
2994 * order to find loadable VK ICDs manifest files. From these
2995 * manifest files it finds the ICD libraries.
2996 *
2997 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002998 * Vulkan result
2999 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06003000 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003001VkResult loader_icd_scan(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003002 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003003 uint16_t file_major_vers = 0;
3004 uint16_t file_minor_vers = 0;
3005 uint16_t file_patch_vers = 0;
3006 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06003007 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06003008 VkResult res = VK_SUCCESS;
3009 bool lockedMutex = false;
3010 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003011 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06003012
Mark Young0ad83132016-06-30 13:02:42 -06003013 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
3014
Mark Young0153e0b2016-11-03 14:27:13 -06003015 res = loader_scanned_icd_init(inst, icd_tramp_list);
Mark Young0ad83132016-06-30 13:02:42 -06003016 if (VK_SUCCESS != res) {
3017 goto out;
3018 }
3019
Jon Ashburn2077e382015-06-29 11:25:34 -06003020 // Get a list of manifest files for ICDs
Benjamin Saunders31a48012017-01-29 14:49:54 -08003021 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false, true, DEFAULT_VK_DRIVERS_INFO, RELATIVE_VK_DRIVERS_INFO,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003022 &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003023 if (VK_SUCCESS != res || manifest_files.count == 0) {
3024 goto out;
3025 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06003026 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003027 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06003028 for (uint32_t i = 0; i < manifest_files.count; i++) {
3029 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06003030 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003031 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003032 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003033
Mark Young3a587792016-08-19 15:25:08 -06003034 res = loader_get_json(inst, file_str, &json);
3035 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3036 break;
3037 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06003038 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003039 }
Mark Young3a587792016-08-19 15:25:08 -06003040
Jon Ashburn005617f2015-11-17 17:35:40 -07003041 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003042 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06003043 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06003044 if (num_good_icds == 0) {
3045 res = VK_ERROR_INITIALIZATION_FAILED;
3046 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003047 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3048 "loader_icd_scan: ICD JSON %s does not have a"
3049 " \'file_format_version\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003050 file_str);
Derrick Owens62e16ef2016-09-09 15:49:07 -04003051 cJSON_Delete(json);
3052 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003053 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06003054 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003055 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003056 if (NULL == file_vers) {
3057 // Only reason the print can fail is if there was an allocation
3058 // issue
Mark Young3a587792016-08-19 15:25:08 -06003059 if (num_good_icds == 0) {
3060 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3061 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003062 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3063 "loader_icd_scan: Failed retrieving ICD JSON %s"
3064 " \'file_format_version\' field. Skipping ICD JSON",
Mark Youngb6399312017-01-10 14:22:15 -07003065 file_str);
Derrick Owenscd92b8b2016-09-09 15:45:13 -04003066 cJSON_Delete(json);
3067 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003068 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003069 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003070 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Found ICD manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003071 // Get the major/minor/and patch as integers for easier comparison
3072 vers_tok = strtok(file_vers, ".\"\n\r");
3073 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003074 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003075 vers_tok = strtok(NULL, ".\"\n\r");
3076 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003077 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003078 vers_tok = strtok(NULL, ".\"\n\r");
3079 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003080 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003081 }
3082 }
3083 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003084 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003085 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3086 "loader_icd_scan: Unexpected manifest file version "
3087 "(expected 1.0.0 or 1.0.1), may cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06003088 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07003089 itemICD = cJSON_GetObjectItem(json, "ICD");
3090 if (itemICD != NULL) {
3091 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003092 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003093 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06003094 if (!temp || strlen(temp) == 0) {
Mark Young3a587792016-08-19 15:25:08 -06003095 if (num_good_icds == 0) {
3096 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3097 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003098 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3099 "loader_icd_scan: Failed retrieving ICD JSON %s"
3100 " \'library_path\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003101 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003102 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06003103 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003104 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003105 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06003106 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003107 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06003108 temp[strlen(temp) - 1] = '\0';
3109 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06003110 if (NULL == library_path) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003111 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3112 "loader_icd_scan: Failed to allocate space for "
3113 "ICD JSON %s \'library_path\' value. Skipping "
3114 "ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003115 file_str);
Mark Young3a587792016-08-19 15:25:08 -06003116 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3117 cJSON_Free(temp);
3118 cJSON_Delete(json);
3119 json = NULL;
3120 goto out;
3121 }
Jon Ashburn86251302015-08-25 16:48:24 -06003122 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003123 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003124 if (strlen(library_path) == 0) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003125 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3126 "loader_icd_scan: ICD JSON %s \'library_path\'"
3127 " field is empty. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003128 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003129 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003130 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003131 continue;
3132 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003133 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003134 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003135 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching for ICD drivers named %s, using default dir %s",
3136 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003137 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003138 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003139 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3140 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003141 strcpy(name_copy, file_str);
3142 rel_base = loader_platform_dirname(name_copy);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003143 loader_expand_path(library_path, rel_base, sizeof(fullpath), fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003144 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003145 // a filename which is assumed in a system directory
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003146 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH, sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003147 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003148
3149 uint32_t vers = 0;
3150 item = cJSON_GetObjectItem(itemICD, "api_version");
3151 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003152 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003153 if (NULL == temp) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003154 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3155 "loader_icd_scan: Failed retrieving ICD JSON %s"
3156 " \'api_version\' field. Skipping ICD JSON.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003157 file_str);
Mark Youngb6399312017-01-10 14:22:15 -07003158
Mark Young0ad83132016-06-30 13:02:42 -06003159 // Only reason the print can fail is if there was an
3160 // allocation issue
Mark Youngb6399312017-01-10 14:22:15 -07003161 if (num_good_icds == 0) {
3162 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3163 }
3164
3165 cJSON_Free(temp);
3166 cJSON_Delete(json);
3167 json = NULL;
3168 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003169 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003170 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003171 cJSON_Free(temp);
Mark Youngb6399312017-01-10 14:22:15 -07003172 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003173 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3174 "loader_icd_scan: ICD JSON %s does not have an"
3175 " \'api_version\' field.",
Mark Youngb6399312017-01-10 14:22:15 -07003176 file_str);
Jon Ashburn005617f2015-11-17 17:35:40 -07003177 }
Mark Youngb6399312017-01-10 14:22:15 -07003178
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003179 res = loader_scanned_icd_add(inst, icd_tramp_list, fullpath, vers);
Mark Young3a587792016-08-19 15:25:08 -06003180 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003181 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3182 "loader_icd_scan: Failed to add ICD JSON %s. "
3183 " Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003184 fullpath);
Mark Youngb6399312017-01-10 14:22:15 -07003185 continue;
Mark Young3a587792016-08-19 15:25:08 -06003186 }
3187 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003188 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003189 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3190 "loader_icd_scan: Failed to find \'library_path\' "
3191 "object in ICD JSON file %s. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003192 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003193 }
3194 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003195 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3196 "loader_icd_scan: Can not find \'ICD\' object in ICD JSON "
3197 "file %s. Skipping ICD JSON",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003198 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003199 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003200
Mark Young0ad83132016-06-30 13:02:42 -06003201 cJSON_Delete(json);
3202 json = NULL;
3203 }
3204
3205out:
Mark Youngb6399312017-01-10 14:22:15 -07003206
Mark Young0ad83132016-06-30 13:02:42 -06003207 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003208 cJSON_Delete(json);
3209 }
Mark Young0ad83132016-06-30 13:02:42 -06003210 if (NULL != manifest_files.filename_list) {
3211 for (uint32_t i = 0; i < manifest_files.count; i++) {
3212 if (NULL != manifest_files.filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003213 loader_instance_heap_free(inst, manifest_files.filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003214 }
3215 }
3216 loader_instance_heap_free(inst, manifest_files.filename_list);
3217 }
3218 if (lockedMutex) {
3219 loader_platform_thread_unlock_mutex(&loader_json_lock);
3220 }
3221 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003222}
3223
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003224void loader_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003225 char *file_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003226 struct loader_manifest_files manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003227 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003228 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003229 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003230
Mark Young0ad83132016-06-30 13:02:42 -06003231 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3232
3233 // Get a list of manifest files for explicit layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003234 if (VK_SUCCESS != loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH, true, true, DEFAULT_VK_ELAYERS_INFO,
Benjamin Saunders31a48012017-01-29 14:49:54 -08003235 RELATIVE_VK_ELAYERS_INFO, &manifest_files[0])) {
Mark Young0ad83132016-06-30 13:02:42 -06003236 goto out;
3237 }
3238
3239 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003240 // Pass NULL for environment variable override - implicit layers are not
3241 // overridden by LAYERS_PATH_ENV
Benjamin Saunders31a48012017-01-29 14:49:54 -08003242 if (VK_SUCCESS != loader_get_manifest_files(inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO, RELATIVE_VK_ILAYERS_INFO,
Mark Youngf8c20102016-11-07 16:26:17 -07003243 &manifest_files[1])) {
Mark Young0ad83132016-06-30 13:02:42 -06003244 goto out;
3245 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003246
Mark Young0ad83132016-06-30 13:02:42 -06003247 // Make sure we have at least one layer, if not, go ahead and return
3248 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3249 goto out;
3250 }
3251
3252 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003253 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003254
Jon Ashburn6461ef22015-09-22 13:11:00 -06003255 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003256 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003257 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003258 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003259 file_str = manifest_files[implicit].filename_list[i];
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003260 if (file_str == NULL) continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003261
Jon Ashburn075ce432015-12-17 17:38:24 -07003262 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003263 VkResult res = loader_get_json(inst, file_str, &json);
3264 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3265 break;
3266 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003267 continue;
3268 }
3269
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003270 loader_add_layer_properties(inst, instance_layers, json, (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003271 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003272 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003273 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003274
3275 // add a meta layer for validation if the validation layers are all present
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003276 loader_add_layer_property_meta(inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]), std_validation_names,
3277 instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003278
Mark Young0ad83132016-06-30 13:02:42 -06003279out:
3280
3281 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3282 if (NULL != manifest_files[manFile].filename_list) {
3283 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3284 if (NULL != manifest_files[manFile].filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003285 loader_instance_heap_free(inst, manifest_files[manFile].filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003286 }
3287 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003288 loader_instance_heap_free(inst, manifest_files[manFile].filename_list);
Mark Young0ad83132016-06-30 13:02:42 -06003289 }
3290 }
3291 if (lockedMutex) {
3292 loader_platform_thread_unlock_mutex(&loader_json_lock);
3293 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003294}
3295
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003296void loader_implicit_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003297 char *file_str;
3298 struct loader_manifest_files manifest_files;
3299 cJSON *json;
3300 uint32_t i;
3301
3302 // Pass NULL for environment variable override - implicit layers are not
3303 // overridden by LAYERS_PATH_ENV
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003304 VkResult res =
Benjamin Saunders31a48012017-01-29 14:49:54 -08003305 loader_get_manifest_files(inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO, RELATIVE_VK_ILAYERS_INFO, &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003306 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003307 return;
3308 }
3309
3310 /* cleanup any previously scanned libraries */
3311 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003312
3313 loader_platform_thread_lock_mutex(&loader_json_lock);
3314
3315 for (i = 0; i < manifest_files.count; i++) {
3316 file_str = manifest_files.filename_list[i];
3317 if (file_str == NULL) {
3318 continue;
3319 }
3320
3321 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003322 res = loader_get_json(inst, file_str, &json);
3323 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3324 break;
3325 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003326 continue;
3327 }
3328
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003329 loader_add_layer_properties(inst, instance_layers, json, true, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003330
Mark Young0ad83132016-06-30 13:02:42 -06003331 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003332 cJSON_Delete(json);
3333 }
Mark Young0ad83132016-06-30 13:02:42 -06003334 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003335
3336 // add a meta layer for validation if the validation layers are all present
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003337 loader_add_layer_property_meta(inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]), std_validation_names,
3338 instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003339
3340 loader_platform_thread_unlock_mutex(&loader_json_lock);
3341}
3342
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003343static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003344 // inst is not wrapped
3345 if (inst == VK_NULL_HANDLE) {
3346 return NULL;
3347 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003348 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003349 void *addr;
3350
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003351 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003352
3353 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003354 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003355 if (found_name) {
3356 return addr;
3357 }
3358
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003359 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, true, NULL, &addr)) return addr;
Mark Young39389872017-01-19 21:10:49 -07003360
3361 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003362 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "loader_gpdpa_instance_internal() unrecognized name %s", pName);
Mark Young39389872017-01-19 21:10:49 -07003363 return NULL;
3364}
3365
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003366static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_terminator(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003367 // inst is not wrapped
3368 if (inst == VK_NULL_HANDLE) {
3369 return NULL;
3370 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003371 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003372 void *addr;
3373
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003374 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003375
3376 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003377 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003378 if (found_name) {
3379 return addr;
3380 }
3381
3382 // Get the terminator, but don't perform checking since it should already
3383 // have been setup if we get here.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003384 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, false, NULL, &addr)) {
Mark Young39389872017-01-19 21:10:49 -07003385 return addr;
3386 }
3387
3388 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003389 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "loader_gpdpa_instance_terminator() unrecognized name %s", pName);
Mark Young39389872017-01-19 21:10:49 -07003390 return NULL;
3391}
3392
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003393static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003394 if (!strcmp(pName, "vkGetInstanceProcAddr")) {
3395 return (PFN_vkVoidFunction)loader_gpa_instance_internal;
3396 }
3397 if (!strcmp(pName, "vk_layerGetPhysicalDeviceProcAddr")) {
3398 return (PFN_vkVoidFunction)loader_gpdpa_instance_terminator;
3399 }
3400 if (!strcmp(pName, "vkCreateInstance")) {
3401 return (PFN_vkVoidFunction)terminator_CreateInstance;
3402 }
3403 if (!strcmp(pName, "vkCreateDevice")) {
3404 return (PFN_vkVoidFunction)terminator_CreateDevice;
3405 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003406
Jon Ashburn27cd5842015-05-12 17:26:48 -06003407 // inst is not wrapped
3408 if (inst == VK_NULL_HANDLE) {
3409 return NULL;
3410 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003411 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003412 void *addr;
3413
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003414 if (disp_table == NULL) return NULL;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003415
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003416 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003417 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003418 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003419 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003420 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003421
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003422 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003423 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003424 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003425}
3426
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003427VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_device_internal(VkDevice device, const char *pName) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003428 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003429 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Mark Young16573c72016-06-28 10:52:43 -06003430
Mark Young65cb3662016-11-07 13:27:02 -07003431 // NOTE: Device Funcs needing Trampoline/Terminator.
3432 // Overrides for device functions needing a trampoline and
3433 // a terminator because certain device entry-points still need to go
3434 // through a terminator before hitting the ICD. This could be for
3435 // several reasons, but the main one is currently unwrapping an
3436 // object before passing the appropriate info along to the ICD.
3437 // This is why we also have to override the direct ICD call to
3438 // vkGetDeviceProcAddr to intercept those calls.
3439 if (!strcmp(pName, "vkGetDeviceProcAddr")) {
3440 return (PFN_vkVoidFunction)loader_gpa_device_internal;
3441 } else if (!strcmp(pName, "vkCreateSwapchainKHR")) {
Mark Young16573c72016-06-28 10:52:43 -06003442 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
Mark Young39389872017-01-19 21:10:49 -07003443 } else if (!strcmp(pName, "vkCreateSharedSwapchainsKHR")) {
3444 return (PFN_vkVoidFunction)terminator_vkCreateSharedSwapchainsKHR;
Mark Young65cb3662016-11-07 13:27:02 -07003445 } else if (!strcmp(pName, "vkDebugMarkerSetObjectTagEXT")) {
3446 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectTagEXT;
3447 } else if (!strcmp(pName, "vkDebugMarkerSetObjectNameEXT")) {
3448 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectNameEXT;
Mark Young16573c72016-06-28 10:52:43 -06003449 }
3450
Mark Young0153e0b2016-11-03 14:27:13 -06003451 return icd_term->GetDeviceProcAddr(device, pName);
Piers Daniell295fe402016-03-29 11:51:11 -06003452}
3453
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003454/**
3455 * Initialize device_ext dispatch table entry as follows:
3456 * If dev == NULL find all logical devices created within this instance and
3457 * init the entry (given by idx) in the ext dispatch table.
3458 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003459 * The initialization value is gotten by calling down the device chain with
3460 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003461 * If GDPA returns NULL then don't initialize the dispatch table entry.
3462 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003463static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst, struct loader_device *dev, uint32_t idx,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003464 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003465
Jon Ashburn23d36b12016-02-02 17:47:28 -07003466{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003467 void *gdpa_value;
3468 if (dev != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003469 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(dev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003470 if (gdpa_value != NULL) dev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003471 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003472 for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
Mark Young0153e0b2016-11-03 14:27:13 -06003473 struct loader_device *ldev = icd_term->logical_device_list;
Karl Schultz2558bd32016-02-24 14:39:39 -07003474 while (ldev) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003475 gdpa_value = ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(ldev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003476 if (gdpa_value != NULL) ldev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003477 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003478 }
3479 }
3480 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003481}
3482
3483/**
3484 * Find all dev extension in the hash table and initialize the dispatch table
3485 * for dev for each of those extension entrypoints found in hash table.
3486
3487 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003488void loader_init_dispatch_dev_ext(struct loader_instance *inst, struct loader_device *dev) {
Mark Young39389872017-01-19 21:10:49 -07003489 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3490 if (inst->dev_ext_disp_hash[i].func_name != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003491 loader_init_dispatch_dev_ext_entry(inst, dev, i, inst->dev_ext_disp_hash[i].func_name);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003492 }
3493}
3494
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003495static bool loader_check_icds_for_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young0153e0b2016-11-03 14:27:13 -06003496 struct loader_icd_term *icd_term;
3497 icd_term = inst->icd_terms;
3498 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003499 if (icd_term->scanned_icd->GetInstanceProcAddr(icd_term->instance, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003500 // this icd supports funcName
3501 return true;
Mark Young0153e0b2016-11-03 14:27:13 -06003502 icd_term = icd_term->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003503 }
3504
3505 return false;
3506}
3507
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003508static bool loader_check_layer_list_for_dev_ext_address(const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003509 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003510 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003511 // Iterate over the extensions.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003512 const struct loader_device_extension_list *const extensions = &(layers->list[layer].device_extension_list);
3513 for (uint32_t extension = 0; extension < extensions->count; ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003514 // Iterate over the entry points.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003515 const struct loader_dev_ext_props *const property = &(extensions->list[extension]);
3516 for (uint32_t entry = 0; entry < property->entrypoint_count; ++entry) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003517 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003518 return true;
3519 }
3520 }
3521 }
3522 }
3523
3524 return false;
3525}
3526
Jon Ashburn23d36b12016-02-02 17:47:28 -07003527static void loader_free_dev_ext_table(struct loader_instance *inst) {
Mark Young39389872017-01-19 21:10:49 -07003528 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3529 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].func_name);
3530 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003531 }
Mark Young39389872017-01-19 21:10:49 -07003532 memset(inst->dev_ext_disp_hash, 0, sizeof(inst->dev_ext_disp_hash));
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003533}
3534
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003535static bool loader_add_dev_ext_table(struct loader_instance *inst, uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003536 uint32_t i;
3537 uint32_t idx = *ptr_idx;
Mark Young39389872017-01-19 21:10:49 -07003538 struct loader_dispatch_hash_list *list = &inst->dev_ext_disp_hash[idx].list;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003539
Mark Young39389872017-01-19 21:10:49 -07003540 if (!inst->dev_ext_disp_hash[idx].func_name) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003541 // no entry here at this idx, so use it
3542 assert(list->capacity == 0);
Mark Young39389872017-01-19 21:10:49 -07003543 inst->dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003544 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003545 if (inst->dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003546 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3547 "loader_add_dev_ext_table: Failed to allocate memory "
3548 "for func_name %s",
Mark Youngb6399312017-01-10 14:22:15 -07003549 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003550 return false;
3551 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003552 strncpy(inst->dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003553 return true;
3554 }
3555
3556 // check for enough capacity
3557 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003558 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003559 if (list->index == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003560 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3561 "loader_add_dev_ext_table: Failed to allocate memory "
3562 "for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003563 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003564 return false;
3565 }
3566 list->capacity = 8 * sizeof(*(list->index));
3567 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003568 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3569 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003570 if (list->index == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003571 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3572 "loader_add_dev_ext_table: Failed to reallocate memory "
3573 "for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003574 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003575 return false;
3576 }
3577 list->capacity *= 2;
3578 }
3579
Jon Ashburn23d36b12016-02-02 17:47:28 -07003580 // find an unused index in the hash table and use it
Mark Young39389872017-01-19 21:10:49 -07003581 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003582 do {
Mark Young39389872017-01-19 21:10:49 -07003583 if (!inst->dev_ext_disp_hash[i].func_name) {
3584 assert(inst->dev_ext_disp_hash[i].list.capacity == 0);
3585 inst->dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003586 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003587 if (inst->dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003588 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3589 "loader_add_dev_ext_table: Failed to allocate memory "
3590 "for func_name %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003591 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003592 return false;
3593 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003594 strncpy(inst->dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003595 list->index[list->count] = i;
3596 list->count++;
3597 *ptr_idx = i;
3598 return true;
3599 }
Mark Young39389872017-01-19 21:10:49 -07003600 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003601 } while (i != idx);
3602
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003603 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3604 "loader_add_dev_ext_table: Could not insert into hash table; is "
3605 "it full?");
Mark Youngb6399312017-01-10 14:22:15 -07003606
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003607 return false;
3608}
3609
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003610static bool loader_name_in_dev_ext_table(struct loader_instance *inst, uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003611 uint32_t alt_idx;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003612 if (inst->dev_ext_disp_hash[*idx].func_name && !strcmp(inst->dev_ext_disp_hash[*idx].func_name, funcName)) return true;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003613
3614 // funcName wasn't at the primary spot in the hash table
3615 // search the list of secondary locations (shallow search, not deep search)
Mark Young39389872017-01-19 21:10:49 -07003616 for (uint32_t i = 0; i < inst->dev_ext_disp_hash[*idx].list.count; i++) {
3617 alt_idx = inst->dev_ext_disp_hash[*idx].list.index[i];
3618 if (!strcmp(inst->dev_ext_disp_hash[*idx].func_name, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003619 *idx = alt_idx;
3620 return true;
3621 }
3622 }
3623
3624 return false;
3625}
3626
3627/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003628 * This function returns generic trampoline code address for unknown entry
3629 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003630 * Presumably, these unknown entry points (as given by funcName) are device
3631 * extension entrypoints. A hash table is used to keep a list of unknown entry
3632 * points and their mapping to the device extension dispatch table
3633 * (struct loader_dev_ext_dispatch_table).
3634 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003635 * For a given entry point string (funcName), if an existing mapping is found
3636 * the
3637 * trampoline address for that mapping is returned. Otherwise, this unknown
3638 * entry point
3639 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3640 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003641 * new entry in the hash table is initialized and that trampoline address for
3642 * the new entry is returned. Null is returned if the hash table is full or
3643 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3644 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003645void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003646 uint32_t idx;
3647 uint32_t seed = 0;
3648
Mark Young39389872017-01-19 21:10:49 -07003649 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003650
3651 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3652 // found funcName already in hash
3653 return loader_get_dev_ext_trampoline(idx);
3654
3655 // Check if funcName is supported in either ICDs or a layer library
Mark Young39389872017-01-19 21:10:49 -07003656 if (!loader_check_icds_for_dev_ext_address(inst, funcName) &&
3657 !loader_check_layer_list_for_dev_ext_address(&inst->instance_layer_list, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003658 // if support found in layers continue on
3659 return NULL;
3660 }
3661
3662 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3663 // successfully added new table entry
3664 // init any dev dispatch table entrys as needed
3665 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3666 return loader_get_dev_ext_trampoline(idx);
3667 }
3668
3669 return NULL;
3670}
3671
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003672static bool loader_check_icds_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07003673 struct loader_icd_term *icd_term;
3674 icd_term = inst->icd_terms;
3675 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003676 if (icd_term->scanned_icd->interface_version >= MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION &&
3677 icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName))
Mark Young39389872017-01-19 21:10:49 -07003678 // this icd supports funcName
3679 return true;
3680 icd_term = icd_term->next;
3681 }
3682
3683 return false;
3684}
3685
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003686static bool loader_check_layer_list_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
3687 struct loader_layer_properties *layer_prop_list = inst->activated_layer_list.list;
Mark Young39389872017-01-19 21:10:49 -07003688 for (uint32_t layer = 0; layer < inst->activated_layer_list.count; ++layer) {
3689 // If this layer supports the vk_layerGetPhysicalDeviceProcAddr, then call
3690 // it and see if it returns a valid pointer for this function name.
3691 if (layer_prop_list[layer].interface_version > 1) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003692 const struct loader_layer_functions *const functions = &(layer_prop_list[layer].functions);
Mark Young39389872017-01-19 21:10:49 -07003693 if (NULL != functions->get_physical_device_proc_addr &&
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003694 NULL != functions->get_physical_device_proc_addr((VkInstance)inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003695 return true;
3696 }
3697 }
3698 }
3699
3700 return false;
3701}
3702
Mark Young39389872017-01-19 21:10:49 -07003703static void loader_free_phys_dev_ext_table(struct loader_instance *inst) {
3704 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003705 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].func_name);
3706 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].list.index);
Mark Young39389872017-01-19 21:10:49 -07003707 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003708 memset(inst->phys_dev_ext_disp_hash, 0, sizeof(inst->phys_dev_ext_disp_hash));
Mark Young39389872017-01-19 21:10:49 -07003709}
3710
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003711static bool loader_add_phys_dev_ext_table(struct loader_instance *inst, uint32_t *ptr_idx, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07003712 uint32_t i;
3713 uint32_t idx = *ptr_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003714 struct loader_dispatch_hash_list *list = &inst->phys_dev_ext_disp_hash[idx].list;
Mark Young39389872017-01-19 21:10:49 -07003715
3716 if (!inst->phys_dev_ext_disp_hash[idx].func_name) {
3717 // no entry here at this idx, so use it
3718 assert(list->capacity == 0);
3719 inst->phys_dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003720 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003721 if (inst->phys_dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003722 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3723 "loader_add_phys_dev_ext_table() can't allocate memory for "
3724 "func_name");
Mark Young39389872017-01-19 21:10:49 -07003725 return false;
3726 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003727 strncpy(inst->phys_dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07003728 return true;
3729 }
3730
3731 // check for enough capacity
3732 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003733 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003734 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003735 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_add_phys_dev_ext_table() can't allocate list memory");
Mark Young39389872017-01-19 21:10:49 -07003736 return false;
3737 }
3738 list->capacity = 8 * sizeof(*(list->index));
3739 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003740 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3741 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003742 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003743 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_add_phys_dev_ext_table() can't reallocate list memory");
Mark Young39389872017-01-19 21:10:49 -07003744 return false;
3745 }
3746 list->capacity *= 2;
3747 }
3748
3749 // find an unused index in the hash table and use it
3750 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
3751 do {
3752 if (!inst->phys_dev_ext_disp_hash[i].func_name) {
3753 assert(inst->phys_dev_ext_disp_hash[i].list.capacity == 0);
3754 inst->phys_dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003755 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003756 if (inst->phys_dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003757 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3758 "loader_add_dev_ext_table() can't rallocate "
3759 "func_name memory");
Mark Young39389872017-01-19 21:10:49 -07003760 return false;
3761 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003762 strncpy(inst->phys_dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07003763 list->index[list->count] = i;
3764 list->count++;
3765 *ptr_idx = i;
3766 return true;
3767 }
3768 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
3769 } while (i != idx);
3770
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003771 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3772 "loader_add_phys_dev_ext_table() couldn't insert into hash table; is "
3773 "it full?");
Mark Young39389872017-01-19 21:10:49 -07003774 return false;
3775}
3776
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003777static bool loader_name_in_phys_dev_ext_table(struct loader_instance *inst, uint32_t *idx, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07003778 uint32_t alt_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003779 if (inst->phys_dev_ext_disp_hash[*idx].func_name && !strcmp(inst->phys_dev_ext_disp_hash[*idx].func_name, funcName))
Mark Young39389872017-01-19 21:10:49 -07003780 return true;
3781
3782 // funcName wasn't at the primary spot in the hash table
3783 // search the list of secondary locations (shallow search, not deep search)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003784 for (uint32_t i = 0; i < inst->phys_dev_ext_disp_hash[*idx].list.count; i++) {
Mark Young39389872017-01-19 21:10:49 -07003785 alt_idx = inst->phys_dev_ext_disp_hash[*idx].list.index[i];
3786 if (!strcmp(inst->phys_dev_ext_disp_hash[*idx].func_name, funcName)) {
3787 *idx = alt_idx;
3788 return true;
3789 }
3790 }
3791
3792 return false;
3793}
3794
3795// This function returns a generic trampoline and/or terminator function
3796// address for any unknown physical device extension commands. A hash
3797// table is used to keep a list of unknown entry points and their
3798// mapping to the physical device extension dispatch table (struct
3799// loader_phys_dev_ext_dispatch_table).
3800// For a given entry point string (funcName), if an existing mapping is
3801// found, then the trampoline address for that mapping is returned in
3802// tramp_addr (if it is not NULL) and the terminator address for that
3803// mapping is returned in term_addr (if it is not NULL). Otherwise,
3804// this unknown entry point has not been seen yet.
3805// If it has not been seen before, and perform_checking is 'true',
3806// check if a layer or and ICD supports it. If so then a new entry in
3807// the hash table is initialized and the trampoline and/or terminator
3808// addresses are returned.
3809// Null is returned if the hash table is full or if no discovered layer or
3810// ICD returns a non-NULL GetProcAddr for it.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003811bool loader_phys_dev_ext_gpa(struct loader_instance *inst, const char *funcName, bool perform_checking, void **tramp_addr,
Mark Young39389872017-01-19 21:10:49 -07003812 void **term_addr) {
3813 uint32_t idx;
3814 uint32_t seed = 0;
3815 bool success = false;
3816
3817 if (inst == NULL) {
3818 goto out;
3819 }
3820
3821 if (NULL != tramp_addr) {
3822 *tramp_addr = NULL;
3823 }
3824 if (NULL != term_addr) {
3825 *term_addr = NULL;
3826 }
3827
3828 // We should always check to see if any ICD supports it.
3829 if (!loader_check_icds_for_phys_dev_ext_address(inst, funcName)) {
3830 // If we're not checking layers, or we are and it's not in a layer, just
3831 // return
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003832 if (!perform_checking || !loader_check_layer_list_for_phys_dev_ext_address(inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003833 goto out;
3834 }
3835 }
3836
3837 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003838 if (perform_checking && !loader_name_in_phys_dev_ext_table(inst, &idx, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003839 uint32_t i;
3840 bool added = false;
3841
3842 // Only need to add first one to get index in Instance. Others will use
3843 // the same index.
3844 if (!added && loader_add_phys_dev_ext_table(inst, &idx, funcName)) {
3845 added = true;
3846 }
3847
3848 // Setup the ICD function pointers
3849 struct loader_icd_term *icd_term = inst->icd_terms;
3850 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003851 if (MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION <= icd_term->scanned_icd->interface_version &&
Mark Young39389872017-01-19 21:10:49 -07003852 NULL != icd_term->scanned_icd->GetPhysicalDeviceProcAddr) {
3853 icd_term->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003854 (PFN_PhysDevExt)icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName);
Mark Young39389872017-01-19 21:10:49 -07003855
3856 // Make sure we set the instance dispatch to point to the
3857 // loader's terminator now since we can at least handle it
3858 // in one ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003859 inst->disp->phys_dev_ext[idx] = loader_get_phys_dev_ext_termin(idx);
Mark Young39389872017-01-19 21:10:49 -07003860 } else {
3861 icd_term->phys_dev_ext[idx] = NULL;
3862 }
3863
3864 icd_term = icd_term->next;
3865 }
3866
3867 // Now, search for the first layer attached and query using it to get
3868 // the first entry point.
3869 for (i = 0; i < inst->activated_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003870 struct loader_layer_properties *layer_prop = &inst->activated_layer_list.list[i];
3871 if (layer_prop->interface_version > 1 && NULL != layer_prop->functions.get_physical_device_proc_addr) {
Mark Young39389872017-01-19 21:10:49 -07003872 inst->disp->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003873 (PFN_PhysDevExt)layer_prop->functions.get_physical_device_proc_addr((VkInstance)inst, funcName);
Mark Young39389872017-01-19 21:10:49 -07003874 if (NULL != inst->disp->phys_dev_ext[idx]) {
3875 break;
3876 }
3877 }
3878 }
3879 }
3880
3881 if (NULL != tramp_addr) {
3882 *tramp_addr = loader_get_phys_dev_ext_tramp(idx);
3883 }
3884
3885 if (NULL != term_addr) {
3886 *term_addr = loader_get_phys_dev_ext_termin(idx);
3887 }
3888
3889 success = true;
3890
3891out:
3892 return success;
3893}
3894
Jon Ashburn23d36b12016-02-02 17:47:28 -07003895struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003896 /* look up the loader_instance in our list by comparing dispatch tables, as
3897 * there is no guarantee the instance is still a loader_instance* after any
3898 * layers which wrap the instance object.
3899 */
3900 const VkLayerInstanceDispatchTable *disp;
3901 struct loader_instance *ptr_instance = NULL;
Mark Young39389872017-01-19 21:10:49 -07003902 disp = loader_get_instance_layer_dispatch(instance);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003903 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
Mark Young39389872017-01-19 21:10:49 -07003904 if (&inst->disp->layer_inst_disp == disp) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003905 ptr_instance = inst;
3906 break;
3907 }
3908 }
3909 return ptr_instance;
3910}
3911
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003912static loader_platform_dl_handle loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3913 struct loader_layer_properties *prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003914 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) == NULL) {
3915 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_open_layer_lib: Failed to open library %s", prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003916 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003917 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Loading layer library %s", prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003918 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003919
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003920 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003921}
3922
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003923static void loader_close_layer_lib(const struct loader_instance *inst, struct loader_layer_properties *prop) {
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003924 if (prop->lib_handle) {
3925 loader_platform_close_library(prop->lib_handle);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003926 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Unloading layer library %s", prop->lib_name);
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003927 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003928 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003929}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003930
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003931void loader_deactivate_layers(const struct loader_instance *instance, struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003932 struct loader_layer_list *list) {
3933 /* delete instance list of enabled layers and close any layer libraries */
3934 for (uint32_t i = 0; i < list->count; i++) {
3935 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003936
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003937 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003938 }
Mark Young0ad83132016-06-30 13:02:42 -06003939 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003940}
3941
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003942/**
3943 * Go through the search_list and find any layers which match type. If layer
3944 * type match is found in then add it to ext_list.
3945 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003946static void loader_add_layer_implicit(const struct loader_instance *inst, const enum layer_type type,
3947 struct loader_layer_list *list, const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003948 bool enable;
3949 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003950 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003951 for (i = 0; i < search_list->count; i++) {
3952 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003953 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003954 /* Found an implicit layer, see if it should be enabled */
3955 enable = false;
3956
Jon Ashburn23d36b12016-02-02 17:47:28 -07003957 // if no enable_environment variable is specified, this implicit
3958 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003959 // should always be enabled. Otherwise check if the variable is set
3960 if (prop->enable_env_var.name[0] == 0) {
3961 enable = true;
3962 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003963 env_value = loader_getenv(prop->enable_env_var.name, inst);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003964 if (env_value && !strcmp(prop->enable_env_var.value, env_value)) enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003965 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003966 }
3967
3968 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003969 // environment variables are set, the layer is disabled. Implicit
3970 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003971 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003972 env_value = loader_getenv(prop->disable_env_var.name, inst);
3973 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003974 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003975 }
3976 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003977
Mark Young0ad83132016-06-30 13:02:42 -06003978 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003979 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003980 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003981 }
3982 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003983}
3984
3985/**
3986 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003987 * is found in search_list then add it to layer_list. But only add it to
3988 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003989 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003990static void loader_add_layer_env(struct loader_instance *inst, const enum layer_type type, const char *env_name,
3991 struct loader_layer_list *layer_list, const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003992 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003993 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003994
Mark Young0ad83132016-06-30 13:02:42 -06003995 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003996 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003997 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003998 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003999 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06004000 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004001 return;
Ian Elliott4470a302015-02-17 10:33:47 -07004002 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06004003 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004004
Mark Young0ad83132016-06-30 13:02:42 -06004005 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07004006
Jon Ashburn23d36b12016-02-02 17:47:28 -07004007 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06004008 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07004009 if (!strcmp(std_validation_str, name)) {
4010 /* add meta list of layers
4011 don't attempt to remove duplicate layers already added by app or
4012 env var
4013 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004014 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Expanding meta layer %s found in environment variable",
Jon Ashburn71483442016-02-11 18:59:43 -07004015 std_validation_str);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004016 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT) inst->activated_layers_are_std_val = true;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004017 for (uint32_t i = 0; i < sizeof(std_validation_names) / sizeof(std_validation_names[0]); i++) {
4018 loader_find_layer_name_add_list(inst, std_validation_names[i], type, search_list, layer_list);
Jon Ashburn71483442016-02-11 18:59:43 -07004019 }
4020 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004021 loader_find_layer_name_add_list(inst, name, type, search_list, layer_list);
Jon Ashburn71483442016-02-11 18:59:43 -07004022 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06004023 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004024 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004025
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004026 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004027}
4028
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004029VkResult loader_enable_instance_layers(struct loader_instance *inst, const VkInstanceCreateInfo *pCreateInfo,
4030 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004031 VkResult err;
4032
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004033 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004034
Jon Ashburne39a4f82015-08-28 13:38:21 -06004035 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004036 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4037 "loader_enable_instance_layers: Failed to initialize"
4038 " the layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004039 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06004040 }
4041
Jon Ashburn0c26e712015-07-02 16:10:32 -06004042 /* Add any implicit layers first */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004043 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06004044
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06004045 /* Add any layers specified via environment variable next */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004046 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT, "VK_INSTANCE_LAYERS", &inst->activated_layer_list, instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06004047
4048 /* Add layers specified by the application */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004049 err = loader_add_layer_names_to_list(inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
4050 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004051
4052 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004053}
4054
Mark Young39389872017-01-19 21:10:49 -07004055// Determine the layer interface version to use.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004056bool loader_get_layer_interface_version(PFN_vkNegotiateLoaderLayerInterfaceVersion fp_negotiate_layer_version,
4057 VkNegotiateLayerInterface *interface_struct) {
Mark Young39389872017-01-19 21:10:49 -07004058 memset(interface_struct, 0, sizeof(VkNegotiateLayerInterface));
4059
4060 // Base assumption is that all layers are version 1 at least.
4061 interface_struct->loaderLayerInterfaceVersion = 1;
4062
4063 if (fp_negotiate_layer_version != NULL) {
4064 // Layer supports the negotiation API, so call it with the loader's
4065 // latest version supported
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004066 interface_struct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
Mark Young39389872017-01-19 21:10:49 -07004067 VkResult result = fp_negotiate_layer_version(interface_struct);
4068
4069 if (result != VK_SUCCESS) {
4070 // Layer no longer supports the loader's latest interface version so
4071 // fail loading the Layer
4072 return false;
4073 }
4074 }
4075
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004076 if (interface_struct->loaderLayerInterfaceVersion < MIN_SUPPORTED_LOADER_LAYER_INTERFACE_VERSION) {
Mark Young39389872017-01-19 21:10:49 -07004077 // Loader no longer supports the layer's latest interface version so
4078 // fail loading the layer
4079 return false;
4080 }
4081
4082 return true;
4083}
4084
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004085/*
4086 * Given the list of layers to activate in the loader_instance
4087 * structure. This function will add a VkLayerInstanceCreateInfo
4088 * structure to the VkInstanceCreateInfo.pNext pointer.
4089 * Each activated layer will have it's own VkLayerInstanceLink
4090 * structure that tells the layer what Get*ProcAddr to call to
4091 * get function pointers to the next layer down.
4092 * Once the chain info has been created this function will
4093 * execute the CreateInstance call chain. Each layer will
4094 * then have an opportunity in it's CreateInstance function
4095 * to setup it's dispatch table when the lower layer returns
4096 * successfully.
4097 * Each layer can wrap or not-wrap the returned VkInstance object
4098 * as it sees fit.
4099 * The instance chain is terminated by a loader function
4100 * that will call CreateInstance on all available ICD's and
4101 * cache those VkInstance objects for future use.
4102 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004103VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
4104 struct loader_instance *inst, VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004105 uint32_t activated_layers = 0;
4106 VkLayerInstanceCreateInfo chain_info;
4107 VkLayerInstanceLink *layer_instance_link_info = NULL;
4108 VkInstanceCreateInfo loader_create_info;
4109 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004110
Mark Young39389872017-01-19 21:10:49 -07004111 PFN_vkGetInstanceProcAddr next_gipa = loader_gpa_instance_internal;
4112 PFN_vkGetInstanceProcAddr cur_gipa = loader_gpa_instance_internal;
4113 PFN_GetPhysicalDeviceProcAddr next_gpdpa = loader_gpdpa_instance_internal;
4114 PFN_GetPhysicalDeviceProcAddr cur_gpdpa = loader_gpdpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004115
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004116 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06004117
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004118 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004119 chain_info.u.pLayerInfo = NULL;
4120 chain_info.pNext = pCreateInfo->pNext;
4121 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
4122 chain_info.function = VK_LAYER_LINK_INFO;
4123 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004124
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004125 layer_instance_link_info = loader_stack_alloc(sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004126 if (!layer_instance_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004127 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4128 "loader_create_instance_chain: Failed to alloc Instance"
4129 " objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004130 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004131 }
4132
Mark Young39389872017-01-19 21:10:49 -07004133 // Create instance chain of enabled layers
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004134 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004135 struct loader_layer_properties *layer_prop = &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004136 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004137
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004138 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004139 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004140 continue;
Mark Young39389872017-01-19 21:10:49 -07004141 }
4142
4143 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004144 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
Mark Young39389872017-01-19 21:10:49 -07004145 bool functions_in_interface = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004146 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4147 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4148 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004149 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004150 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4151 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004152 }
4153
4154 // If we can negotiate an interface version, then we can also
4155 // get everything we need from the one function call, so try
4156 // that first, and see if we can get all the function pointers
4157 // necessary from that one call.
4158 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004159 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004160
4161 VkNegotiateLayerInterface interface_struct;
4162
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004163 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Young39389872017-01-19 21:10:49 -07004164 // Go ahead and set the properites version to the
4165 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004166 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004167
4168 // If the interface is 2 or newer, we have access to the
4169 // new GetPhysicalDeviceProcAddr function, so grab it,
4170 // and the other necessary functions, from the
4171 // structure.
4172 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4173 cur_gipa = interface_struct.pfnGetInstanceProcAddr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004174 cur_gpdpa = interface_struct.pfnGetPhysicalDeviceProcAddr;
Mark Young39389872017-01-19 21:10:49 -07004175 if (cur_gipa != NULL) {
4176 // We've set the functions, so make sure we
4177 // don't do the unnecessary calls later.
4178 functions_in_interface = true;
4179 }
4180 }
4181 }
4182 }
4183
4184 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004185 if ((cur_gipa = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004186 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004187 cur_gipa =
4188 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
4189 layer_prop->functions.get_instance_proc_addr = cur_gipa;
Mark Young39389872017-01-19 21:10:49 -07004190 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004191 cur_gipa = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle,
4192 layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004193 }
4194
4195 if (NULL == cur_gipa) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004196 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4197 "loader_create_instance_chain: Failed to"
4198 " find \'vkGetInstanceProcAddr\' in "
4199 "layer %s",
Mark Young39389872017-01-19 21:10:49 -07004200 layer_prop->lib_name);
4201 continue;
4202 }
4203 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004204 }
4205 }
4206
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004207 layer_instance_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4208 layer_instance_link_info[activated_layers].pfnNextGetInstanceProcAddr = next_gipa;
4209 layer_instance_link_info[activated_layers].pfnNextGetPhysicalDeviceProcAddr = next_gpdpa;
Mark Young39389872017-01-19 21:10:49 -07004210 next_gipa = cur_gipa;
4211 if (layer_prop->interface_version > 1 && cur_gpdpa != NULL) {
4212 layer_prop->functions.get_physical_device_proc_addr = cur_gpdpa;
4213 next_gpdpa = cur_gpdpa;
4214 }
4215
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004216 chain_info.u.pLayerInfo = &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004217
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004218 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert instance layer %s (%s)", layer_prop->info.layerName,
4219 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004220
4221 activated_layers++;
4222 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06004223 }
4224
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004225 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)next_gipa(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004226 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06004227 VkLayerInstanceCreateInfo create_info_disp;
4228
Jon Ashburncc407a22016-04-15 09:25:03 -06004229 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004230 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06004231
4232 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
4233
4234 create_info_disp.pNext = loader_create_info.pNext;
4235 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004236 res = fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004237 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004238 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4239 "loader_create_instance_chain: Failed to find "
4240 "\'vkCreateInstance\'");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004241 // Couldn't find CreateInstance function!
4242 res = VK_ERROR_INITIALIZATION_FAILED;
4243 }
4244
Mark Young39389872017-01-19 21:10:49 -07004245 if (res == VK_SUCCESS) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004246 loader_init_instance_core_dispatch_table(&inst->disp->layer_inst_disp, next_gipa, *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07004247 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004248 }
4249
4250 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004251}
4252
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004253void loader_activate_instance_layer_extensions(struct loader_instance *inst, VkInstance created_inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004254 loader_init_instance_extension_dispatch_table(&inst->disp->layer_inst_disp, inst->disp->layer_inst_disp.GetInstanceProcAddr,
4255 created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004256}
4257
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004258VkResult loader_create_device_chain(const struct loader_physical_device_tramp *pd, const VkDeviceCreateInfo *pCreateInfo,
4259 const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst,
4260 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004261 uint32_t activated_layers = 0;
4262 VkLayerDeviceLink *layer_device_link_info;
4263 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004264 VkDeviceCreateInfo loader_create_info;
4265 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004266
Piers Daniell295fe402016-03-29 11:51:11 -06004267 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004268 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004269
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004270 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
4271
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004272 layer_device_link_info = loader_stack_alloc(sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004273 if (!layer_device_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004274 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4275 "loader_create_device_chain: Failed to alloc Device objects"
4276 " for layer. Skipping Layer.");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004277 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06004278 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004279
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004280 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06004281 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
4282 chain_info.function = VK_LAYER_LINK_INFO;
4283 chain_info.u.pLayerInfo = NULL;
Mark Young39389872017-01-19 21:10:49 -07004284 chain_info.pNext = loader_create_info.pNext;
Jon Ashburn72690f22016-03-29 12:52:13 -06004285 loader_create_info.pNext = &chain_info;
4286
Mark Young39389872017-01-19 21:10:49 -07004287 // Create instance chain of enabled layers
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004288 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004289 struct loader_layer_properties *layer_prop = &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004290 loader_platform_dl_handle lib_handle;
Mark Young39389872017-01-19 21:10:49 -07004291 bool functions_in_interface = false;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004292
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004293 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004294 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004295 continue;
Jon Ashburn21c21ee2015-09-09 11:29:24 -06004296 }
Mark Young39389872017-01-19 21:10:49 -07004297
4298 // If we can negotiate an interface version, then we can also
4299 // get everything we need from the one function call, so try
4300 // that first, and see if we can get all the function pointers
4301 // necessary from that one call.
4302 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004303 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
4304 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4305 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4306 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004307 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004308 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4309 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004310 }
4311
4312 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004313 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004314
4315 VkNegotiateLayerInterface interface_struct;
4316
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004317 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Young39389872017-01-19 21:10:49 -07004318 // Go ahead and set the properites version to the
4319 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004320 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004321
4322 // If the interface is 2 or newer, we have access to the
4323 // new GetPhysicalDeviceProcAddr function, so grab it,
4324 // and the other necessary functions, from the structure.
4325 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4326 fpGIPA = interface_struct.pfnGetInstanceProcAddr;
4327 fpGDPA = interface_struct.pfnGetDeviceProcAddr;
4328 if (fpGIPA != NULL && fpGDPA) {
4329 // We've set the functions, so make sure we
4330 // don't do the unnecessary calls later.
4331 functions_in_interface = true;
4332 }
4333 }
4334 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004335 }
4336 }
4337
Mark Young39389872017-01-19 21:10:49 -07004338 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004339 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004340 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004341 fpGIPA = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004342 layer_prop->functions.get_instance_proc_addr = fpGIPA;
4343 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004344 fpGIPA =
4345 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004346 if (!fpGIPA) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004347 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4348 "loader_create_device_chain: Failed to find "
4349 "\'vkGetInstanceProcAddr\' in layer %s. Skipping"
4350 " layer.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004351 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004352 continue;
4353 }
4354 }
4355 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
4356 if (strlen(layer_prop->functions.str_gdpa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004357 fpGDPA = (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004358 layer_prop->functions.get_device_proc_addr = fpGDPA;
4359 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004360 fpGDPA =
4361 (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gdpa);
Mark Young39389872017-01-19 21:10:49 -07004362 if (!fpGDPA) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004363 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Failed to find vkGetDeviceProcAddr in layer %s",
4364 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004365 continue;
4366 }
4367 }
4368 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004369 layer_device_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4370 layer_device_link_info[activated_layers].pfnNextGetInstanceProcAddr = nextGIPA;
4371 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr = nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004372 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
4373 nextGIPA = fpGIPA;
4374 nextGDPA = fpGDPA;
4375
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004376 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert device layer %s (%s)", layer_prop->info.layerName,
4377 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004378
4379 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06004380 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004381 }
4382
Jon Ashburncc407a22016-04-15 09:25:03 -06004383 VkDevice created_device = (VkDevice)dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004384 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004385 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06004386 VkLayerDeviceCreateInfo create_info_disp;
4387
Jon Ashburncc407a22016-04-15 09:25:03 -06004388 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004389 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
4390
4391 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
4392
4393 create_info_disp.pNext = loader_create_info.pNext;
4394 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004395 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator, &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06004396 if (res != VK_SUCCESS) {
4397 return res;
4398 }
Mark Young65cb3662016-11-07 13:27:02 -07004399 dev->chain_device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004400 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004401 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4402 "loader_create_device_chain: Failed to find \'vkCreateDevice\' "
4403 "in layer %s");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004404 // Couldn't find CreateDevice function!
4405 return VK_ERROR_INITIALIZATION_FAILED;
4406 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004407
Mark Young65cb3662016-11-07 13:27:02 -07004408 // Initialize device dispatch table
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004409 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA, dev->chain_device);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004410
4411 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06004412}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004413
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004414VkResult loader_validate_layers(const struct loader_instance *inst, const uint32_t layer_count,
4415 const char *const *ppEnabledLayerNames, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004416 struct loader_layer_properties *prop;
4417
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004418 for (uint32_t i = 0; i < layer_count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004419 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004420 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004421 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4422 "loader_validate_layers: Device ppEnabledLayerNames "
4423 "contains string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004424 return VK_ERROR_LAYER_NOT_PRESENT;
4425 }
4426
Jon Ashburn23d36b12016-02-02 17:47:28 -07004427 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004428 if (!prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004429 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4430 "loader_validate_layers: Layer %d does not exist in "
4431 "the list of available layers",
Mark Youngb6399312017-01-10 14:22:15 -07004432 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004433 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004434 }
4435 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004436 return VK_SUCCESS;
4437}
4438
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004439VkResult loader_validate_instance_extensions(const struct loader_instance *inst, const struct loader_extension_list *icd_exts,
4440 const struct loader_layer_list *instance_layers,
4441 const VkInstanceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004442 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004443 struct loader_layer_properties *layer_prop;
4444
Jon Ashburnf19916e2016-01-11 13:12:43 -07004445 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004446 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004447 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004448 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4449 "loader_validate_instance_extensions: Instance "
4450 "ppEnabledExtensionNames contains "
4451 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004452 return VK_ERROR_EXTENSION_NOT_PRESENT;
4453 }
4454
Lenny Komow4053b812016-12-29 16:27:28 -07004455 // See if the extension is in the list of supported extensions
4456 bool found = false;
4457 for (uint32_t j = 0; LOADER_INSTANCE_EXTENSIONS[j] != NULL; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004458 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], LOADER_INSTANCE_EXTENSIONS[j]) == 0) {
Lenny Komow4053b812016-12-29 16:27:28 -07004459 found = true;
4460 break;
4461 }
4462 }
4463
4464 // If it isn't in the list, return an error
4465 if (!found) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004466 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4467 "loader_validate_instance_extensions: Extension %d "
4468 "not found in list of available extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004469 i);
Lenny Komow4053b812016-12-29 16:27:28 -07004470 return VK_ERROR_EXTENSION_NOT_PRESENT;
4471 }
4472
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004473 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004474
4475 if (extension_prop) {
4476 continue;
4477 }
4478
4479 extension_prop = NULL;
4480
4481 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004482 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004483 layer_prop = loader_get_layer_property(pCreateInfo->ppEnabledLayerNames[j], instance_layers);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004484 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004485 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004486 * should have already filtered this case out.
4487 */
4488 continue;
4489 }
4490
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004491 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004492 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004493 /* Found the extension in one of the layers enabled by the app.
4494 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004495 break;
4496 }
4497 }
4498
4499 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004500 // Didn't find extension name in any of the global layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004501 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4502 "loader_validate_instance_extensions: Extension %d "
4503 "not found in enabled layer list extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004504 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004505 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004506 }
4507 }
4508 return VK_SUCCESS;
4509}
4510
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004511VkResult loader_validate_device_extensions(struct loader_physical_device_tramp *phys_dev,
4512 const struct loader_layer_list *activated_device_layers,
4513 const struct loader_extension_list *icd_exts, const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004514 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004515 struct loader_layer_properties *layer_prop;
4516
Jon Ashburnf19916e2016-01-11 13:12:43 -07004517 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004518 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004519 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004520 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4521 "loader_validate_device_extensions: Device "
4522 "ppEnabledExtensionNames contains "
4523 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004524 return VK_ERROR_EXTENSION_NOT_PRESENT;
4525 }
4526
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004527 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004528 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004529
4530 if (extension_prop) {
4531 continue;
4532 }
4533
Mark Youngb6399312017-01-10 14:22:15 -07004534 // Not in global list, search activated layer extension lists
Jon Ashburn471f44c2016-01-13 12:51:43 -07004535 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4536 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004537
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004538 extension_prop = get_dev_extension_property(extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004539 if (extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004540 // Found the extension in one of the layers enabled by the app.
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004541 break;
4542 }
4543 }
4544
4545 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004546 // Didn't find extension name in any of the device layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004547 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4548 "loader_validate_device_extensions: Extension %d "
4549 "not found in enabled layer list extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004550 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004551 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004552 }
4553 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004554 return VK_SUCCESS;
4555}
4556
Mark Youngb6399312017-01-10 14:22:15 -07004557// Terminator functions for the Instance chain
4558// All named terminator_<Vulakn API name>
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004559VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
4560 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Mark Young0153e0b2016-11-03 14:27:13 -06004561 struct loader_icd_term *icd_term;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004562 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004563 char **filtered_extension_names = NULL;
4564 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004565 VkResult res = VK_SUCCESS;
Mark Young8b4edb52016-11-11 09:31:55 -07004566 bool one_icd_successful = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004567
Jon Ashburncc407a22016-04-15 09:25:03 -06004568 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004569 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4570
Jon Ashburnf19916e2016-01-11 13:12:43 -07004571 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004572 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004573
Mark Youngb6399312017-01-10 14:22:15 -07004574 // NOTE: Need to filter the extensions to only those supported by the ICD.
4575 // No ICD will advertise support for layers. An ICD library could
4576 // support a layer, but it would be independent of the actual ICD,
4577 // just in the same library.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004578 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004579 if (!filtered_extension_names) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004580 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4581 "terminator_CreateInstance: Failed create extension name "
4582 "array for %d extensions",
Mark Youngb6399312017-01-10 14:22:15 -07004583 pCreateInfo->enabledExtensionCount);
Mark Young3a587792016-08-19 15:25:08 -06004584 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4585 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004586 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004587 icd_create_info.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004588
Mark Young0153e0b2016-11-03 14:27:13 -06004589 for (uint32_t i = 0; i < ptr_instance->icd_tramp_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004590 icd_term = loader_icd_add(ptr_instance, &ptr_instance->icd_tramp_list.scanned_list[i]);
Mark Young0153e0b2016-11-03 14:27:13 -06004591 if (NULL == icd_term) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004592 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4593 "terminator_CreateInstance: Failed to add ICD %d to ICD "
4594 "trampoline list.",
Mark Youngb6399312017-01-10 14:22:15 -07004595 i);
Mark Young3a587792016-08-19 15:25:08 -06004596 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4597 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004598 }
Mark Young6267ae62017-01-12 12:27:19 -07004599
Mark Young0ad83132016-06-30 13:02:42 -06004600 icd_create_info.enabledExtensionCount = 0;
4601 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004602
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004603 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young0ad83132016-06-30 13:02:42 -06004604 // traverse scanned icd list adding non-duplicate extensions to the
4605 // list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004606 res = loader_init_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004607 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4608 // If out of memory, bail immediately.
4609 goto out;
4610 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004611 // Something bad happened with this ICD, so free it and try the
4612 // next.
Mark Young0153e0b2016-11-03 14:27:13 -06004613 ptr_instance->icd_terms = icd_term->next;
4614 icd_term->next = NULL;
4615 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004616 continue;
4617 }
4618
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004619 res = loader_add_instance_extensions(ptr_instance, icd_term->scanned_icd->EnumerateInstanceExtensionProperties,
4620 icd_term->scanned_icd->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004621 if (VK_SUCCESS != res) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004622 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004623 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4624 // If out of memory, bail immediately.
4625 goto out;
4626 } else {
4627 // Something bad happened with this ICD, so free it and try
4628 // the next.
Mark Young0153e0b2016-11-03 14:27:13 -06004629 ptr_instance->icd_terms = icd_term->next;
4630 icd_term->next = NULL;
4631 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004632 continue;
4633 }
Mark Young3a587792016-08-19 15:25:08 -06004634 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004635
Mark Young0ad83132016-06-30 13:02:42 -06004636 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004637 prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004638 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004639 filtered_extension_names[icd_create_info.enabledExtensionCount] = (char *)pCreateInfo->ppEnabledExtensionNames[j];
Mark Young0ad83132016-06-30 13:02:42 -06004640 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004641 }
4642 }
Mark Young0ad83132016-06-30 13:02:42 -06004643
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004644 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004645
Mark Young8b4edb52016-11-11 09:31:55 -07004646 VkResult icd_result =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004647 ptr_instance->icd_tramp_list.scanned_list[i].CreateInstance(&icd_create_info, pAllocator, &(icd_term->instance));
Mark Young8b4edb52016-11-11 09:31:55 -07004648 if (VK_ERROR_OUT_OF_HOST_MEMORY == icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004649 // If out of memory, bail immediately.
Mark Young8b4edb52016-11-11 09:31:55 -07004650 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06004651 goto out;
Mark Young8b4edb52016-11-11 09:31:55 -07004652 } else if (VK_SUCCESS != icd_result) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004653 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4654 "terminator_CreateInstance: Failed to CreateInstance in "
4655 "ICD %d. Skipping ICD.",
Mark Youngb6399312017-01-10 14:22:15 -07004656 i);
Mark Young0153e0b2016-11-03 14:27:13 -06004657 ptr_instance->icd_terms = icd_term->next;
4658 icd_term->next = NULL;
4659 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004660 continue;
4661 }
Mark Young0ad83132016-06-30 13:02:42 -06004662
Mark Young0153e0b2016-11-03 14:27:13 -06004663 if (!loader_icd_init_entrys(icd_term, icd_term->instance,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004664 ptr_instance->icd_tramp_list.scanned_list[i].GetInstanceProcAddr)) {
4665 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4666 "terminator_CreateInstance: Failed to CreateInstance and find "
4667 "entrypoints with ICD. Skipping ICD.");
Mark Young3a587792016-08-19 15:25:08 -06004668 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004669 }
Mark Young8b4edb52016-11-11 09:31:55 -07004670
4671 // If we made it this far, at least one ICD was successful
4672 one_icd_successful = true;
Jon Ashburn46888392015-01-29 15:45:51 -07004673 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004674
Mark Young8b4edb52016-11-11 09:31:55 -07004675 // If no ICDs were added to instance list and res is unchanged
4676 // from it's initial value, the loader was unable to find
4677 // a suitable ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004678 if (VK_SUCCESS == res && (ptr_instance->icd_terms == NULL || !one_icd_successful)) {
Mark Young3a587792016-08-19 15:25:08 -06004679 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4680 }
4681
4682out:
4683
4684 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004685 while (NULL != ptr_instance->icd_terms) {
4686 icd_term = ptr_instance->icd_terms;
4687 ptr_instance->icd_terms = icd_term->next;
4688 if (NULL != icd_term->instance) {
4689 icd_term->DestroyInstance(icd_term->instance, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004690 }
Mark Young0153e0b2016-11-03 14:27:13 -06004691 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004692 }
Ian Elliotteb450762015-02-05 15:19:15 -07004693 }
Jon Ashburn46888392015-01-29 15:45:51 -07004694
Mark Young3a587792016-08-19 15:25:08 -06004695 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004696}
4697
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004698VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004699 struct loader_instance *ptr_instance = loader_instance(instance);
Karl Schultze2ef9e62017-01-13 14:01:35 -07004700 if (NULL == ptr_instance) {
4701 return;
4702 }
Mark Young0153e0b2016-11-03 14:27:13 -06004703 struct loader_icd_term *icd_terms = ptr_instance->icd_terms;
4704 struct loader_icd_term *next_icd_term;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004705
4706 // Remove this instance from the list of instances:
4707 struct loader_instance *prev = NULL;
4708 struct loader_instance *next = loader.instances;
4709 while (next != NULL) {
4710 if (next == ptr_instance) {
4711 // Remove this instance from the list:
4712 if (prev)
4713 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004714 else
4715 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004716 break;
4717 }
4718 prev = next;
4719 next = next->next;
4720 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004721
Mark Young0153e0b2016-11-03 14:27:13 -06004722 while (NULL != icd_terms) {
4723 if (icd_terms->instance) {
4724 icd_terms->DestroyInstance(icd_terms->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004725 }
Mark Young0153e0b2016-11-03 14:27:13 -06004726 next_icd_term = icd_terms->next;
4727 icd_terms->instance = VK_NULL_HANDLE;
4728 loader_icd_destroy(ptr_instance, icd_terms, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004729
Mark Young0153e0b2016-11-03 14:27:13 -06004730 icd_terms = next_icd_term;
Jon Ashburn46888392015-01-29 15:45:51 -07004731 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004732
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004733 loader_delete_layer_properties(ptr_instance, &ptr_instance->instance_layer_list);
Mark Young0153e0b2016-11-03 14:27:13 -06004734 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_tramp_list);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004735 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Mark Young39389872017-01-19 21:10:49 -07004736 if (NULL != ptr_instance->phys_devs_term) {
Mark Young0193d652016-12-28 16:10:10 -07004737 for (uint32_t i = 0; i < ptr_instance->phys_dev_count_term; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004738 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term[i]);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07004739 }
Mark Young0ad83132016-06-30 13:02:42 -06004740 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07004741 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004742 loader_free_dev_ext_table(ptr_instance);
Mark Young39389872017-01-19 21:10:49 -07004743 loader_free_phys_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004744}
4745
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004746VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4747 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Mark Young0ad83132016-06-30 13:02:42 -06004748 VkResult res = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06004749 struct loader_physical_device_term *phys_dev_term;
4750 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
4751 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004752
Jon Ashburncc407a22016-04-15 09:25:03 -06004753 struct loader_device *dev = (struct loader_device *)*pDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06004754 PFN_vkCreateDevice fpCreateDevice = icd_term->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004755 struct loader_extension_list icd_exts;
4756
Mark Young65cb3662016-11-07 13:27:02 -07004757 dev->phys_dev_term = phys_dev_term;
4758
Mark Young0ad83132016-06-30 13:02:42 -06004759 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004760
Jon Ashburn1530c342016-02-26 13:14:27 -07004761 if (fpCreateDevice == NULL) {
Mark Young0153e0b2016-11-03 14:27:13 -06004762 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07004763 "terminator_CreateDevice: No vkCreateDevice command exposed "
4764 "by ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004765 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004766 res = VK_ERROR_INITIALIZATION_FAILED;
4767 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004768 }
4769
Jon Ashburn1530c342016-02-26 13:14:27 -07004770 VkDeviceCreateInfo localCreateInfo;
4771 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004772
Mark Youngb6399312017-01-10 14:22:15 -07004773 // NOTE: Need to filter the extensions to only those supported by the ICD.
4774 // No ICD will advertise support for layers. An ICD library could
4775 // support a layer, but it would be independent of the actual ICD,
4776 // just in the same library.
Jon Ashburn1530c342016-02-26 13:14:27 -07004777 char **filtered_extension_names = NULL;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004778 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Mark Youngb6399312017-01-10 14:22:15 -07004779 if (NULL == filtered_extension_names) {
4780 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4781 "terminator_CreateDevice: Failed to create extension name "
4782 "storage for %d extensions %d",
4783 pCreateInfo->enabledExtensionCount);
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004784 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004785 }
4786
Jon Ashburn1530c342016-02-26 13:14:27 -07004787 localCreateInfo.enabledLayerCount = 0;
4788 localCreateInfo.ppEnabledLayerNames = NULL;
4789
4790 localCreateInfo.enabledExtensionCount = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004791 localCreateInfo.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Jon Ashburn1530c342016-02-26 13:14:27 -07004792
Mark Youngb6399312017-01-10 14:22:15 -07004793 // Get the physical device (ICD) extensions
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004794 res = loader_init_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004795 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004796 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004797 }
4798
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004799 res = loader_add_device_extensions(icd_term->this_instance, icd_term->EnumerateDeviceExtensionProperties,
4800 phys_dev_term->phys_dev, icd_term->scanned_icd->lib_name, &icd_exts);
Jon Ashburn014438f2016-03-01 19:51:07 -07004801 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004802 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004803 }
4804
Jon Ashburn1530c342016-02-26 13:14:27 -07004805 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4806 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004807 VkExtensionProperties *prop = get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004808 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004809 filtered_extension_names[localCreateInfo.enabledExtensionCount] = (char *)extension_name;
Jon Ashburn1530c342016-02-26 13:14:27 -07004810 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06004811 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004812 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4813 "vkCreateDevice extension %s not available for "
4814 "devices associated with ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004815 extension_name, icd_term->scanned_icd->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07004816 }
4817 }
4818
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004819 res = fpCreateDevice(phys_dev_term->phys_dev, &localCreateInfo, pAllocator, &dev->icd_device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004820 if (res != VK_SUCCESS) {
Mark Young0153e0b2016-11-03 14:27:13 -06004821 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07004822 "terminator_CreateDevice: Failed in ICD %s vkCreateDevice"
4823 "call",
Mark Young0153e0b2016-11-03 14:27:13 -06004824 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004825 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004826 }
4827
Mark Young65cb3662016-11-07 13:27:02 -07004828 *pDevice = dev->icd_device;
Mark Young0153e0b2016-11-03 14:27:13 -06004829 loader_add_logical_device(icd_term->this_instance, icd_term, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004830
4831 /* Init dispatch pointer in new device object */
4832 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4833
Mark Young0ad83132016-06-30 13:02:42 -06004834out:
4835 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004836 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004837 }
4838
Jon Ashburn1530c342016-02-26 13:14:27 -07004839 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004840}
4841
Mark Young6267ae62017-01-12 12:27:19 -07004842VkResult setupLoaderTrampPhysDevs(VkInstance instance) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004843 VkResult res = VK_SUCCESS;
Mark Young6267ae62017-01-12 12:27:19 -07004844 VkPhysicalDevice *local_phys_devs = NULL;
4845 struct loader_instance *inst;
4846 uint32_t total_count = 0;
4847 struct loader_physical_device_tramp **new_phys_devs = NULL;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004848
Mark Young6267ae62017-01-12 12:27:19 -07004849 inst = loader_get_instance(instance);
4850 if (NULL == inst) {
4851 res = VK_ERROR_INITIALIZATION_FAILED;
4852 goto out;
4853 }
4854 total_count = inst->total_gpu_count;
4855
4856 // Create an array for the new physical devices, which will be stored
4857 // in the instance for the trampoline code.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004858 new_phys_devs = (struct loader_physical_device_tramp **)loader_instance_heap_alloc(
4859 inst, total_count * sizeof(struct loader_physical_device_tramp *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004860 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004861 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4862 "setupLoaderTrampPhysDevs: Failed to allocate new physical device"
4863 " array of size %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004864 total_count);
Mark Youngd8382d72016-12-23 16:59:58 -07004865 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4866 goto out;
4867 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004868 memset(new_phys_devs, 0, total_count * sizeof(struct loader_physical_device_tramp *));
Jon Ashburn014438f2016-03-01 19:51:07 -07004869
Mark Young6267ae62017-01-12 12:27:19 -07004870 // Create a temporary array (on the stack) to keep track of the
4871 // returned VkPhysicalDevice values.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004872 local_phys_devs = loader_stack_alloc(sizeof(VkPhysicalDevice) * total_count);
Mark Young6267ae62017-01-12 12:27:19 -07004873 if (NULL == local_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004874 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4875 "setupLoaderTrampPhysDevs: Failed to allocate local "
4876 "physical device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004877 total_count);
4878 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4879 goto out;
4880 }
4881 memset(local_phys_devs, 0, sizeof(VkPhysicalDevice) * total_count);
4882
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004883 res = inst->disp->layer_inst_disp.EnumeratePhysicalDevices(instance, &total_count, local_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004884 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004885 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4886 "setupLoaderTrampPhysDevs: Failed during dispatch call "
4887 "of \'vkEnumeratePhysicalDevices\' to lower layers or "
4888 "loader.");
Mark Young6267ae62017-01-12 12:27:19 -07004889 goto out;
4890 }
4891
4892 // Copy or create everything to fill the new array of physical devices
4893 for (uint32_t new_idx = 0; new_idx < total_count; new_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07004894 // Check if this physical device is already in the old buffer
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004895 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_tramp; old_idx++) {
4896 if (local_phys_devs[new_idx] == inst->phys_devs_tramp[old_idx]->phys_dev) {
Mark Young6267ae62017-01-12 12:27:19 -07004897 new_phys_devs[new_idx] = inst->phys_devs_tramp[old_idx];
4898 break;
4899 }
Mark Youngd8382d72016-12-23 16:59:58 -07004900 }
4901
Mark Young6267ae62017-01-12 12:27:19 -07004902 // If this physical device isn't in the old buffer, create it
4903 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004904 new_phys_devs[new_idx] = (struct loader_physical_device_tramp *)loader_instance_heap_alloc(
4905 inst, sizeof(struct loader_physical_device_tramp), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004906 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004907 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4908 "setupLoaderTrampPhysDevs: Failed to allocate "
4909 "physical device trampoline object %d",
Mark Young6267ae62017-01-12 12:27:19 -07004910 new_idx);
4911 total_count = new_idx;
Mark Youngd8382d72016-12-23 16:59:58 -07004912 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4913 goto out;
4914 }
4915
Mark Young6267ae62017-01-12 12:27:19 -07004916 // Initialize the new physicalDevice object
4917 loader_set_dispatch((void *)new_phys_devs[new_idx], inst->disp);
4918 new_phys_devs[new_idx]->this_instance = inst;
4919 new_phys_devs[new_idx]->phys_dev = local_phys_devs[new_idx];
Mark Young559d7502016-09-26 11:38:46 -06004920 }
Lenny Komowa5e01122016-12-22 15:29:43 -07004921 }
Mark Young559d7502016-09-26 11:38:46 -06004922
Lenny Komowa5e01122016-12-22 15:29:43 -07004923out:
Mark Youngd8382d72016-12-23 16:59:58 -07004924
Mark Young6267ae62017-01-12 12:27:19 -07004925 if (VK_SUCCESS != res) {
4926 if (NULL != new_phys_devs) {
4927 for (uint32_t i = 0; i < total_count; i++) {
4928 loader_instance_heap_free(inst, new_phys_devs[i]);
Lenny Komowa5e01122016-12-22 15:29:43 -07004929 }
4930 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004931 }
4932 total_count = 0;
4933 } else {
4934 // Free everything that didn't carry over to the new array of
4935 // physical devices
4936 if (NULL != inst->phys_devs_tramp) {
4937 for (uint32_t i = 0; i < inst->phys_dev_count_tramp; i++) {
4938 bool found = false;
4939 for (uint32_t j = 0; j < total_count; j++) {
4940 if (inst->phys_devs_tramp[i] == new_phys_devs[j]) {
4941 found = true;
4942 break;
4943 }
4944 }
4945 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004946 loader_instance_heap_free(inst, inst->phys_devs_tramp[i]);
Mark Young6267ae62017-01-12 12:27:19 -07004947 }
4948 }
4949 loader_instance_heap_free(inst, inst->phys_devs_tramp);
4950 }
Mark Youngd8382d72016-12-23 16:59:58 -07004951
Mark Young6267ae62017-01-12 12:27:19 -07004952 // Swap in the new physical device list
4953 inst->phys_dev_count_tramp = total_count;
4954 inst->phys_devs_tramp = new_phys_devs;
4955 }
4956
4957 return res;
4958}
4959
4960VkResult setupLoaderTermPhysDevs(struct loader_instance *inst) {
4961 VkResult res = VK_SUCCESS;
4962 struct loader_icd_term *icd_term;
4963 struct loader_phys_dev_per_icd *icd_phys_dev_array = NULL;
4964 struct loader_physical_device_term **new_phys_devs = NULL;
Mark Young6267ae62017-01-12 12:27:19 -07004965
4966 inst->total_gpu_count = 0;
4967
4968 // Allocate something to store the physical device characteristics
4969 // that we read from each ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004970 icd_phys_dev_array =
4971 (struct loader_phys_dev_per_icd *)loader_stack_alloc(sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
Mark Young6267ae62017-01-12 12:27:19 -07004972 if (NULL == icd_phys_dev_array) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004973 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4974 "setupLoaderTermPhysDevs: Failed to allocate temporary "
4975 "ICD Physical device info array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004976 inst->total_gpu_count);
4977 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4978 goto out;
4979 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004980 memset(icd_phys_dev_array, 0, sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
Mark Young6267ae62017-01-12 12:27:19 -07004981 icd_term = inst->icd_terms;
4982
4983 // For each ICD, query the number of physical devices, and then get an
4984 // internal value for those physical devices.
Karl Schultz47dd59d2017-01-20 13:19:20 -07004985 for (uint32_t icd_idx = 0; NULL != icd_term; icd_term = icd_term->next, icd_idx++) {
4986 res = icd_term->EnumeratePhysicalDevices(icd_term->instance, &icd_phys_dev_array[icd_idx].count, NULL);
Mark Young6267ae62017-01-12 12:27:19 -07004987 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004988 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4989 "setupLoaderTermPhysDevs: Call to "
4990 "ICD %d's \'vkEnumeratePhysicalDevices\' failed with"
4991 " error 0x%08x",
Karl Schultz47dd59d2017-01-20 13:19:20 -07004992 icd_idx, res);
Mark Young6267ae62017-01-12 12:27:19 -07004993 goto out;
4994 }
4995
Karl Schultz47dd59d2017-01-20 13:19:20 -07004996 icd_phys_dev_array[icd_idx].phys_devs =
4997 (VkPhysicalDevice *)loader_stack_alloc(icd_phys_dev_array[icd_idx].count * sizeof(VkPhysicalDevice));
4998 if (NULL == icd_phys_dev_array[icd_idx].phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004999 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5000 "setupLoaderTermPhysDevs: Failed to allocate temporary "
5001 "ICD Physical device array for ICD %d of size %d",
Karl Schultz47dd59d2017-01-20 13:19:20 -07005002 icd_idx, inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07005003 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5004 goto out;
5005 }
5006
Karl Schultz47dd59d2017-01-20 13:19:20 -07005007 res = icd_term->EnumeratePhysicalDevices(icd_term->instance, &(icd_phys_dev_array[icd_idx].count),
5008 icd_phys_dev_array[icd_idx].phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005009 if (VK_SUCCESS != res) {
5010 goto out;
5011 }
Karl Schultz47dd59d2017-01-20 13:19:20 -07005012 inst->total_gpu_count += icd_phys_dev_array[icd_idx].count;
5013 icd_phys_dev_array[icd_idx].this_icd_term = icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07005014 }
5015
5016 if (0 == inst->total_gpu_count) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005017 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5018 "setupLoaderTermPhysDevs: Failed to detect any valid"
5019 " GPUs in the current config");
Mark Young6267ae62017-01-12 12:27:19 -07005020 res = VK_ERROR_INITIALIZATION_FAILED;
5021 goto out;
5022 }
5023
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005024 new_phys_devs = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count,
5025 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005026 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005027 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5028 "setupLoaderTermPhysDevs: Failed to allocate new physical"
5029 " device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07005030 inst->total_gpu_count);
5031 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5032 goto out;
5033 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005034 memset(new_phys_devs, 0, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07005035
5036 // Copy or create everything to fill the new array of physical devices
5037 uint32_t idx = 0;
5038 for (uint32_t icd_idx = 0; icd_idx < inst->total_icd_count; icd_idx++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005039 for (uint32_t pd_idx = 0; pd_idx < icd_phys_dev_array[icd_idx].count; pd_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07005040 // Check if this physical device is already in the old buffer
5041 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005042 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_term; old_idx++) {
5043 if (icd_phys_dev_array[icd_idx].phys_devs[pd_idx] == inst->phys_devs_term[old_idx]->phys_dev) {
Mark Young6267ae62017-01-12 12:27:19 -07005044 new_phys_devs[idx] = inst->phys_devs_term[old_idx];
5045 break;
5046 }
5047 }
5048 }
5049 // If this physical device isn't in the old buffer, then we
5050 // need to create it.
5051 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005052 new_phys_devs[idx] = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term),
5053 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005054 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005055 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5056 "setupLoaderTermPhysDevs: Failed to allocate "
5057 "physical device terminator object %d",
Mark Young6267ae62017-01-12 12:27:19 -07005058 idx);
5059 inst->total_gpu_count = idx;
5060 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5061 goto out;
5062 }
5063
5064 loader_set_dispatch((void *)new_phys_devs[idx], inst->disp);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005065 new_phys_devs[idx]->this_icd_term = icd_phys_dev_array[icd_idx].this_icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07005066 new_phys_devs[idx]->icd_index = (uint8_t)(icd_idx);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005067 new_phys_devs[idx]->phys_dev = icd_phys_dev_array[icd_idx].phys_devs[pd_idx];
Mark Young6267ae62017-01-12 12:27:19 -07005068 }
5069 idx++;
5070 }
5071 }
5072
5073out:
5074
5075 if (VK_SUCCESS != res) {
Mark Young156e8552017-02-03 16:27:42 -07005076 if (NULL != new_phys_devs) {
5077 // We've encountered an error, so we should free the new buffers.
Mark Young6267ae62017-01-12 12:27:19 -07005078 for (uint32_t i = 0; i < inst->total_gpu_count; i++) {
5079 loader_instance_heap_free(inst, new_phys_devs[i]);
5080 }
Mark Young156e8552017-02-03 16:27:42 -07005081 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005082 }
Mark Young156e8552017-02-03 16:27:42 -07005083 if (NULL != inst->phys_devs_term) {
5084 loader_instance_heap_free(inst, inst->phys_devs_term);
5085 }
5086 inst->total_gpu_count = 0;
Mark Young6267ae62017-01-12 12:27:19 -07005087 } else {
5088 // Free everything that didn't carry over to the new array of
5089 // physical devices. Everything else will have been copied over
5090 // to the new array.
5091 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005092 for (uint32_t cur_pd = 0; cur_pd < inst->phys_dev_count_term; cur_pd++) {
Mark Young6267ae62017-01-12 12:27:19 -07005093 bool found = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005094 for (uint32_t new_pd_idx = 0; new_pd_idx < inst->total_gpu_count; new_pd_idx++) {
5095 if (inst->phys_devs_term[cur_pd] == new_phys_devs[new_pd_idx]) {
Mark Young6267ae62017-01-12 12:27:19 -07005096 found = true;
5097 break;
5098 }
5099 }
5100 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005101 loader_instance_heap_free(inst, inst->phys_devs_term[cur_pd]);
Mark Young6267ae62017-01-12 12:27:19 -07005102 }
5103 }
5104 loader_instance_heap_free(inst, inst->phys_devs_term);
5105 }
5106
5107 // Swap out old and new devices list
5108 inst->phys_dev_count_term = inst->total_gpu_count;
5109 inst->phys_devs_term = new_phys_devs;
5110 }
5111
5112 return res;
5113}
5114
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005115VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
5116 VkPhysicalDevice *pPhysicalDevices) {
Mark Young6267ae62017-01-12 12:27:19 -07005117 struct loader_instance *inst = (struct loader_instance *)instance;
5118 VkResult res = VK_SUCCESS;
5119
5120 // Only do the setup if we're re-querying the number of devices, or
5121 // our count is currently 0.
5122 if (NULL == pPhysicalDevices || 0 == inst->total_gpu_count) {
5123 res = setupLoaderTermPhysDevs(inst);
5124 if (VK_SUCCESS != res) {
5125 goto out;
5126 }
5127 }
5128
5129 uint32_t copy_count = inst->total_gpu_count;
5130 if (NULL != pPhysicalDevices) {
5131 if (copy_count > *pPhysicalDeviceCount) {
5132 copy_count = *pPhysicalDeviceCount;
5133 res = VK_INCOMPLETE;
5134 }
5135
5136 for (uint32_t i = 0; i < copy_count; i++) {
5137 pPhysicalDevices[i] = (VkPhysicalDevice)inst->phys_devs_term[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07005138 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005139 }
Mark Young559d7502016-09-26 11:38:46 -06005140
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07005141 *pPhysicalDeviceCount = copy_count;
5142
Mark Young6267ae62017-01-12 12:27:19 -07005143out:
5144
Jon Ashburn24cd4be2015-11-01 14:04:06 -07005145 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005146}
5147
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005148VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
5149 VkPhysicalDeviceProperties *pProperties) {
5150 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005151 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005152 if (NULL != icd_term->GetPhysicalDeviceProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005153 icd_term->GetPhysicalDeviceProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005154 }
Tony Barbour59a47322015-06-24 16:06:58 -06005155}
5156
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005157VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
5158 uint32_t *pQueueFamilyPropertyCount,
5159 VkQueueFamilyProperties *pProperties) {
5160 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005161 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005162 if (NULL != icd_term->GetPhysicalDeviceQueueFamilyProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005163 icd_term->GetPhysicalDeviceQueueFamilyProperties(phys_dev_term->phys_dev, pQueueFamilyPropertyCount, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005164 }
Tony Barbour59a47322015-06-24 16:06:58 -06005165}
5166
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005167VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
5168 VkPhysicalDeviceMemoryProperties *pProperties) {
5169 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005170 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005171 if (NULL != icd_term->GetPhysicalDeviceMemoryProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005172 icd_term->GetPhysicalDeviceMemoryProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005173 }
Jon Ashburn3da71f22015-05-14 12:43:38 -06005174}
5175
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005176VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
5177 VkPhysicalDeviceFeatures *pFeatures) {
5178 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005179 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005180 if (NULL != icd_term->GetPhysicalDeviceFeatures) {
Mark Young0153e0b2016-11-03 14:27:13 -06005181 icd_term->GetPhysicalDeviceFeatures(phys_dev_term->phys_dev, pFeatures);
Mark Youngb6399312017-01-10 14:22:15 -07005182 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005183}
5184
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005185VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5186 VkFormatProperties *pFormatInfo) {
5187 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005188 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005189 if (NULL != icd_term->GetPhysicalDeviceFormatProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005190 icd_term->GetPhysicalDeviceFormatProperties(phys_dev_term->phys_dev, format, pFormatInfo);
Mark Youngb6399312017-01-10 14:22:15 -07005191 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005192}
5193
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005194VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5195 VkImageType type, VkImageTiling tiling,
5196 VkImageUsageFlags usage, VkImageCreateFlags flags,
5197 VkImageFormatProperties *pImageFormatProperties) {
5198 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005199 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005200 if (NULL == icd_term->GetPhysicalDeviceImageFormatProperties) {
5201 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5202 "Encountered the vkEnumerateDeviceLayerProperties "
5203 "terminator. This means a layer improperly continued.");
Chia-I Wu17241042015-10-31 00:31:16 +08005204 return VK_ERROR_INITIALIZATION_FAILED;
Mark Youngb6399312017-01-10 14:22:15 -07005205 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005206 return icd_term->GetPhysicalDeviceImageFormatProperties(phys_dev_term->phys_dev, format, type, tiling, usage, flags,
5207 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06005208}
5209
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005210VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5211 VkImageType type, VkSampleCountFlagBits samples,
5212 VkImageUsageFlags usage, VkImageTiling tiling,
5213 uint32_t *pNumProperties,
5214 VkSparseImageFormatProperties *pProperties) {
5215 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005216 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005217 if (NULL != icd_term->GetPhysicalDeviceSparseImageFormatProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005218 icd_term->GetPhysicalDeviceSparseImageFormatProperties(phys_dev_term->phys_dev, format, type, samples, usage, tiling,
5219 pNumProperties, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005220 }
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06005221}
5222
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005223VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
5224 const char *pLayerName, uint32_t *pPropertyCount,
5225 VkExtensionProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06005226 struct loader_physical_device_term *phys_dev_term;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07005227
Mark Young3a587792016-08-19 15:25:08 -06005228 struct loader_layer_list implicit_layer_list = {0};
5229 struct loader_extension_list all_exts = {0};
5230 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07005231
Jon Ashburndc5d9202016-02-29 13:00:51 -07005232 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005233
Jon Ashburndc5d9202016-02-29 13:00:51 -07005234 /* Any layer or trampoline wrapping should be removed at this point in time
5235 * can just cast to the expected type for VkPhysicalDevice. */
Mark Young0153e0b2016-11-03 14:27:13 -06005236 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005237
Jon Ashburndc5d9202016-02-29 13:00:51 -07005238 /* this case is during the call down the instance chain with pLayerName
5239 * == NULL*/
Mark Young0153e0b2016-11-03 14:27:13 -06005240 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005241 uint32_t icd_ext_count = *pPropertyCount;
5242 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005243
Jon Ashburndc5d9202016-02-29 13:00:51 -07005244 /* get device extensions */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005245 res = icd_term->EnumerateDeviceExtensionProperties(phys_dev_term->phys_dev, NULL, &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06005246 if (res != VK_SUCCESS) {
5247 goto out;
5248 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07005249
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005250 if (!loader_init_layer_list(icd_term->this_instance, &implicit_layer_list)) {
Mark Young3a587792016-08-19 15:25:08 -06005251 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5252 goto out;
5253 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005254
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005255 loader_add_layer_implicit(icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &implicit_layer_list,
5256 &icd_term->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07005257 /* we need to determine which implicit layers are active,
5258 * and then add their extensions. This can't be cached as
5259 * it depends on results of environment variables (which can change).
5260 */
5261 if (pProperties != NULL) {
5262 /* initialize dev_extension list within the physicalDevice object */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005263 res = loader_init_device_extensions(icd_term->this_instance, phys_dev_term, icd_ext_count, pProperties, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06005264 if (res != VK_SUCCESS) {
5265 goto out;
5266 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005267
Jon Ashburndc5d9202016-02-29 13:00:51 -07005268 /* we need to determine which implicit layers are active,
5269 * and then add their extensions. This can't be cached as
5270 * it depends on results of environment variables (which can
5271 * change).
5272 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005273 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts, icd_exts.count, icd_exts.list);
Mark Young3a587792016-08-19 15:25:08 -06005274 if (res != VK_SUCCESS) {
5275 goto out;
5276 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005277
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005278 loader_add_layer_implicit(icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &implicit_layer_list,
5279 &icd_term->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07005280
Jon Ashburndc5d9202016-02-29 13:00:51 -07005281 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005282 for (uint32_t j = 0; j < implicit_layer_list.list[i].device_extension_list.count; j++) {
5283 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts, 1,
5284 &implicit_layer_list.list[i].device_extension_list.list[j].props);
Mark Young3a587792016-08-19 15:25:08 -06005285 if (res != VK_SUCCESS) {
5286 goto out;
5287 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005288 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005289 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005290 uint32_t capacity = *pPropertyCount;
5291 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005292
Jon Ashburndc5d9202016-02-29 13:00:51 -07005293 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
5294 props[i] = all_exts.list[i];
5295 }
5296 /* wasn't enough space for the extensions, we did partial copy now
5297 * return VK_INCOMPLETE */
5298 if (capacity < all_exts.count) {
5299 res = VK_INCOMPLETE;
5300 } else {
5301 *pPropertyCount = all_exts.count;
5302 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005303 } else {
5304 /* just return the count; need to add in the count of implicit layer
5305 * extensions
5306 * don't worry about duplicates being added in the count */
5307 *pPropertyCount = icd_ext_count;
5308
5309 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005310 *pPropertyCount += implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005311 }
5312 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06005313 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005314
Mark Young3a587792016-08-19 15:25:08 -06005315out:
5316
5317 if (NULL != implicit_layer_list.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005318 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Mark Young3a587792016-08-19 15:25:08 -06005319 }
5320 if (NULL != all_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005321 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&all_exts);
Mark Young3a587792016-08-19 15:25:08 -06005322 }
5323 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005324 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06005325 }
5326
Jon Ashburndc5d9202016-02-29 13:00:51 -07005327 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005328}
5329
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005330VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
5331 VkLayerProperties *pProperties) {
5332 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Youngb6399312017-01-10 14:22:15 -07005333 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005334 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5335 "Encountered the vkEnumerateDeviceLayerProperties "
5336 "terminator. This means a layer improperly continued.");
Mark Youngb6399312017-01-10 14:22:15 -07005337 // Should never get here this call isn't dispatched down the chain
Jon Ashburndc5d9202016-02-29 13:00:51 -07005338 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06005339}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005340
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005341VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005342 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07005343 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005344 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005345
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005346 for (i = 0; i <= max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005347 if (utf8[i] == 0) {
5348 break;
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005349 } else if (i == max_length) {
5350 result |= VK_STRING_ERROR_LENGTH;
5351 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07005352 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005353 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005354 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005355 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005356 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005357 num_char_bytes = 2;
5358 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
5359 num_char_bytes = 3;
5360 } else {
5361 result = VK_STRING_ERROR_BAD_DATA;
5362 }
5363
5364 // Validate the following num_char_bytes of data
5365 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
5366 if (++i == max_length) {
5367 result |= VK_STRING_ERROR_LENGTH;
5368 break;
5369 }
5370 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
5371 result |= VK_STRING_ERROR_BAD_DATA;
5372 }
5373 }
5374 }
5375 return result;
5376}