blob: 0b6ab4cecd932da500d55e188b8ff33e8e1bf52f [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) {
418 strcat(cmd_line_msg, "INFO");
419 cmd_line_size -= 4;
420 }
421 if ((msg_type & LOADER_WARN_BIT) != 0) {
422 if (cmd_line_size != sizeof(cmd_line_msg)) {
423 strcat(cmd_line_msg, " | ");
424 cmd_line_size -= 3;
425 }
426 strcat(cmd_line_msg, "WARNING");
427 cmd_line_size -= 7;
428 }
429 if ((msg_type & LOADER_PERF_BIT) != 0) {
430 if (cmd_line_size != sizeof(cmd_line_msg)) {
431 strcat(cmd_line_msg, " | ");
432 cmd_line_size -= 3;
433 }
434 strcat(cmd_line_msg, "PERF");
435 cmd_line_size -= 4;
436 }
437 if ((msg_type & LOADER_ERROR_BIT) != 0) {
438 if (cmd_line_size != sizeof(cmd_line_msg)) {
439 strcat(cmd_line_msg, " | ");
440 cmd_line_size -= 3;
441 }
442 strcat(cmd_line_msg, "ERROR");
443 cmd_line_size -= 5;
444 }
445 if ((msg_type & LOADER_DEBUG_BIT) != 0) {
446 if (cmd_line_size != sizeof(cmd_line_msg)) {
447 strcat(cmd_line_msg, " | ");
448 cmd_line_size -= 3;
449 }
450 strcat(cmd_line_msg, "DEBUG");
451 cmd_line_size -= 5;
452 }
453 if (cmd_line_size != sizeof(cmd_line_msg)) {
454 strcat(cmd_line_msg, ": ");
455 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) {
Benjamin Saunders31a48012017-01-29 14:49:54 -08002895 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 + strlen(relative_location));
Jon Ashburn67e262e2016-02-18 12:45:39 -07002896 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002897 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2898 "loader_get_manifest_files: Failed to allocate "
2899 "space for manifest file XDG Home location");
Mark Young0ad83132016-06-30 13:02:42 -06002900 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2901 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002902 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002903 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002904 // Add directory separator if needed
Benjamin Saunders31a48012017-01-29 14:49:54 -08002905 if (relative_location[0] != DIRECTORY_SYMBOL) {
Jon Ashburn67e262e2016-02-18 12:45:39 -07002906 len = strlen(home_loc);
2907 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002908 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002909 }
Benjamin Saunders31a48012017-01-29 14:49:54 -08002910 strcat(home_loc, relative_location);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002911 file = home_loc;
2912 next_file = loader_get_next_path(file);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002913 relative_location = NULL;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002914
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002915 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
2916 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002917 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002918
2919 } else {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002920 char *home = secure_getenv("HOME");
2921 if (home != NULL) {
Benjamin Saunders31a48012017-01-29 14:49:54 -08002922 char *home_loc = loader_stack_alloc(strlen(home) + 16 + strlen(relative_location));
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002923 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002924 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2925 "loader_get_manifest_files: Failed to allocate "
2926 "space for manifest file Home location");
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002927 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2928 goto out;
2929 }
2930 strcpy(home_loc, home);
2931
2932 len = strlen(home);
2933 if (home[len] != DIRECTORY_SYMBOL) {
2934 home_loc[len] = DIRECTORY_SYMBOL;
2935 home_loc[len + 1] = '\0';
2936 }
2937 strcat(home_loc, ".local/share");
2938
Benjamin Saunders31a48012017-01-29 14:49:54 -08002939 if (relative_location[0] != DIRECTORY_SYMBOL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002940 len = strlen(home_loc);
2941 home_loc[len] = DIRECTORY_SYMBOL;
2942 home_loc[len + 1] = '\0';
2943 }
Benjamin Saunders31a48012017-01-29 14:49:54 -08002944 strcat(home_loc, relative_location);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002945 file = home_loc;
2946 next_file = loader_get_next_path(file);
Benjamin Saunders31a48012017-01-29 14:49:54 -08002947 relative_location = NULL;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002948
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002949 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
2950 home_loc);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002951 list_is_dirs = true;
2952 } else {
2953 // without knowing HOME, we just.. give up
2954 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002955 }
2956 }
2957#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002958 }
Mark Young0ad83132016-06-30 13:02:42 -06002959
2960out:
2961 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2962 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2963 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2964 }
2965 loader_instance_heap_free(inst, out_files->filename_list);
2966 out_files->count = 0;
2967 out_files->filename_list = NULL;
2968 }
2969
2970 if (NULL != sysdir) {
2971 closedir(sysdir);
2972 }
2973
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002974 if (override_getenv != NULL) {
2975 loader_free_getenv(override_getenv, inst);
2976 }
2977
Mark Young0ad83132016-06-30 13:02:42 -06002978 if (NULL != reg && reg != orig_loc) {
2979 loader_instance_heap_free(inst, reg);
2980 }
2981 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002982}
2983
Jon Ashburn23d36b12016-02-02 17:47:28 -07002984void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002985
Jon Ashburn23d36b12016-02-02 17:47:28 -07002986void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002987/**
2988 * Try to find the Vulkan ICD driver(s).
2989 *
2990 * This function scans the default system loader path(s) or path
2991 * specified by the \c VK_ICD_FILENAMES environment variable in
2992 * order to find loadable VK ICDs manifest files. From these
2993 * manifest files it finds the ICD libraries.
2994 *
2995 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002996 * Vulkan result
2997 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002998 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002999VkResult loader_icd_scan(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003000 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003001 uint16_t file_major_vers = 0;
3002 uint16_t file_minor_vers = 0;
3003 uint16_t file_patch_vers = 0;
3004 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06003005 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06003006 VkResult res = VK_SUCCESS;
3007 bool lockedMutex = false;
3008 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003009 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06003010
Mark Young0ad83132016-06-30 13:02:42 -06003011 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
3012
Mark Young0153e0b2016-11-03 14:27:13 -06003013 res = loader_scanned_icd_init(inst, icd_tramp_list);
Mark Young0ad83132016-06-30 13:02:42 -06003014 if (VK_SUCCESS != res) {
3015 goto out;
3016 }
3017
Jon Ashburn2077e382015-06-29 11:25:34 -06003018 // Get a list of manifest files for ICDs
Benjamin Saunders31a48012017-01-29 14:49:54 -08003019 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 -07003020 &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003021 if (VK_SUCCESS != res || manifest_files.count == 0) {
3022 goto out;
3023 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06003024 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003025 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06003026 for (uint32_t i = 0; i < manifest_files.count; i++) {
3027 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06003028 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003029 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003030 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003031
Mark Young3a587792016-08-19 15:25:08 -06003032 res = loader_get_json(inst, file_str, &json);
3033 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3034 break;
3035 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06003036 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003037 }
Mark Young3a587792016-08-19 15:25:08 -06003038
Jon Ashburn005617f2015-11-17 17:35:40 -07003039 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003040 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06003041 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06003042 if (num_good_icds == 0) {
3043 res = VK_ERROR_INITIALIZATION_FAILED;
3044 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003045 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3046 "loader_icd_scan: ICD JSON %s does not have a"
3047 " \'file_format_version\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003048 file_str);
Derrick Owens62e16ef2016-09-09 15:49:07 -04003049 cJSON_Delete(json);
3050 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003051 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06003052 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003053 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003054 if (NULL == file_vers) {
3055 // Only reason the print can fail is if there was an allocation
3056 // issue
Mark Young3a587792016-08-19 15:25:08 -06003057 if (num_good_icds == 0) {
3058 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3059 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003060 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3061 "loader_icd_scan: Failed retrieving ICD JSON %s"
3062 " \'file_format_version\' field. Skipping ICD JSON",
Mark Youngb6399312017-01-10 14:22:15 -07003063 file_str);
Derrick Owenscd92b8b2016-09-09 15:45:13 -04003064 cJSON_Delete(json);
3065 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06003066 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003067 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003068 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 -06003069 // Get the major/minor/and patch as integers for easier comparison
3070 vers_tok = strtok(file_vers, ".\"\n\r");
3071 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003072 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003073 vers_tok = strtok(NULL, ".\"\n\r");
3074 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003075 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003076 vers_tok = strtok(NULL, ".\"\n\r");
3077 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003078 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003079 }
3080 }
3081 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06003082 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003083 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3084 "loader_icd_scan: Unexpected manifest file version "
3085 "(expected 1.0.0 or 1.0.1), may cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06003086 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07003087 itemICD = cJSON_GetObjectItem(json, "ICD");
3088 if (itemICD != NULL) {
3089 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003090 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003091 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06003092 if (!temp || strlen(temp) == 0) {
Mark Young3a587792016-08-19 15:25:08 -06003093 if (num_good_icds == 0) {
3094 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3095 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003096 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3097 "loader_icd_scan: Failed retrieving ICD JSON %s"
3098 " \'library_path\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003099 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003100 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06003101 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003102 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003103 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06003104 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003105 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06003106 temp[strlen(temp) - 1] = '\0';
3107 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06003108 if (NULL == library_path) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003109 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3110 "loader_icd_scan: Failed to allocate space for "
3111 "ICD JSON %s \'library_path\' value. Skipping "
3112 "ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003113 file_str);
Mark Young3a587792016-08-19 15:25:08 -06003114 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3115 cJSON_Free(temp);
3116 cJSON_Delete(json);
3117 json = NULL;
3118 goto out;
3119 }
Jon Ashburn86251302015-08-25 16:48:24 -06003120 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003121 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003122 if (strlen(library_path) == 0) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003123 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3124 "loader_icd_scan: ICD JSON %s \'library_path\'"
3125 " field is empty. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003126 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003127 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003128 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003129 continue;
3130 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003131 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003132 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003133 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching for ICD drivers named %s, using default dir %s",
3134 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003135 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003136 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003137 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3138 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003139 strcpy(name_copy, file_str);
3140 rel_base = loader_platform_dirname(name_copy);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003141 loader_expand_path(library_path, rel_base, sizeof(fullpath), fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003142 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003143 // a filename which is assumed in a system directory
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003144 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH, sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003145 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003146
3147 uint32_t vers = 0;
3148 item = cJSON_GetObjectItem(itemICD, "api_version");
3149 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003150 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003151 if (NULL == temp) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003152 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3153 "loader_icd_scan: Failed retrieving ICD JSON %s"
3154 " \'api_version\' field. Skipping ICD JSON.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003155 file_str);
Mark Youngb6399312017-01-10 14:22:15 -07003156
Mark Young0ad83132016-06-30 13:02:42 -06003157 // Only reason the print can fail is if there was an
3158 // allocation issue
Mark Youngb6399312017-01-10 14:22:15 -07003159 if (num_good_icds == 0) {
3160 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3161 }
3162
3163 cJSON_Free(temp);
3164 cJSON_Delete(json);
3165 json = NULL;
3166 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003167 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003168 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003169 cJSON_Free(temp);
Mark Youngb6399312017-01-10 14:22:15 -07003170 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003171 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3172 "loader_icd_scan: ICD JSON %s does not have an"
3173 " \'api_version\' field.",
Mark Youngb6399312017-01-10 14:22:15 -07003174 file_str);
Jon Ashburn005617f2015-11-17 17:35:40 -07003175 }
Mark Youngb6399312017-01-10 14:22:15 -07003176
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003177 res = loader_scanned_icd_add(inst, icd_tramp_list, fullpath, vers);
Mark Young3a587792016-08-19 15:25:08 -06003178 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003179 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3180 "loader_icd_scan: Failed to add ICD JSON %s. "
3181 " Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003182 fullpath);
Mark Youngb6399312017-01-10 14:22:15 -07003183 continue;
Mark Young3a587792016-08-19 15:25:08 -06003184 }
3185 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003186 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003187 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3188 "loader_icd_scan: Failed to find \'library_path\' "
3189 "object in ICD JSON file %s. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003190 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003191 }
3192 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003193 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3194 "loader_icd_scan: Can not find \'ICD\' object in ICD JSON "
3195 "file %s. Skipping ICD JSON",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003196 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003197 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003198
Mark Young0ad83132016-06-30 13:02:42 -06003199 cJSON_Delete(json);
3200 json = NULL;
3201 }
3202
3203out:
Mark Youngb6399312017-01-10 14:22:15 -07003204
Mark Young0ad83132016-06-30 13:02:42 -06003205 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003206 cJSON_Delete(json);
3207 }
Mark Young0ad83132016-06-30 13:02:42 -06003208 if (NULL != manifest_files.filename_list) {
3209 for (uint32_t i = 0; i < manifest_files.count; i++) {
3210 if (NULL != manifest_files.filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003211 loader_instance_heap_free(inst, manifest_files.filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003212 }
3213 }
3214 loader_instance_heap_free(inst, manifest_files.filename_list);
3215 }
3216 if (lockedMutex) {
3217 loader_platform_thread_unlock_mutex(&loader_json_lock);
3218 }
3219 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003220}
3221
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003222void loader_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003223 char *file_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003224 struct loader_manifest_files manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003225 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003226 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003227 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003228
Mark Young0ad83132016-06-30 13:02:42 -06003229 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3230
3231 // Get a list of manifest files for explicit layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003232 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 -08003233 RELATIVE_VK_ELAYERS_INFO, &manifest_files[0])) {
Mark Young0ad83132016-06-30 13:02:42 -06003234 goto out;
3235 }
3236
3237 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003238 // Pass NULL for environment variable override - implicit layers are not
3239 // overridden by LAYERS_PATH_ENV
Benjamin Saunders31a48012017-01-29 14:49:54 -08003240 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 -07003241 &manifest_files[1])) {
Mark Young0ad83132016-06-30 13:02:42 -06003242 goto out;
3243 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003244
Mark Young0ad83132016-06-30 13:02:42 -06003245 // Make sure we have at least one layer, if not, go ahead and return
3246 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3247 goto out;
3248 }
3249
3250 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003251 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003252
Jon Ashburn6461ef22015-09-22 13:11:00 -06003253 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003254 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003255 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003256 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003257 file_str = manifest_files[implicit].filename_list[i];
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003258 if (file_str == NULL) continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003259
Jon Ashburn075ce432015-12-17 17:38:24 -07003260 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003261 VkResult res = loader_get_json(inst, file_str, &json);
3262 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3263 break;
3264 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003265 continue;
3266 }
3267
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003268 loader_add_layer_properties(inst, instance_layers, json, (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003269 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003270 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003271 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003272
3273 // add a meta layer for validation if the validation layers are all present
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003274 loader_add_layer_property_meta(inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]), std_validation_names,
3275 instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003276
Mark Young0ad83132016-06-30 13:02:42 -06003277out:
3278
3279 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3280 if (NULL != manifest_files[manFile].filename_list) {
3281 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3282 if (NULL != manifest_files[manFile].filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003283 loader_instance_heap_free(inst, manifest_files[manFile].filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003284 }
3285 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003286 loader_instance_heap_free(inst, manifest_files[manFile].filename_list);
Mark Young0ad83132016-06-30 13:02:42 -06003287 }
3288 }
3289 if (lockedMutex) {
3290 loader_platform_thread_unlock_mutex(&loader_json_lock);
3291 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003292}
3293
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003294void loader_implicit_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003295 char *file_str;
3296 struct loader_manifest_files manifest_files;
3297 cJSON *json;
3298 uint32_t i;
3299
3300 // Pass NULL for environment variable override - implicit layers are not
3301 // overridden by LAYERS_PATH_ENV
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003302 VkResult res =
Benjamin Saunders31a48012017-01-29 14:49:54 -08003303 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 -06003304 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003305 return;
3306 }
3307
3308 /* cleanup any previously scanned libraries */
3309 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003310
3311 loader_platform_thread_lock_mutex(&loader_json_lock);
3312
3313 for (i = 0; i < manifest_files.count; i++) {
3314 file_str = manifest_files.filename_list[i];
3315 if (file_str == NULL) {
3316 continue;
3317 }
3318
3319 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003320 res = loader_get_json(inst, file_str, &json);
3321 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3322 break;
3323 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003324 continue;
3325 }
3326
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003327 loader_add_layer_properties(inst, instance_layers, json, true, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003328
Mark Young0ad83132016-06-30 13:02:42 -06003329 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003330 cJSON_Delete(json);
3331 }
Mark Young0ad83132016-06-30 13:02:42 -06003332 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003333
3334 // add a meta layer for validation if the validation layers are all present
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003335 loader_add_layer_property_meta(inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]), std_validation_names,
3336 instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003337
3338 loader_platform_thread_unlock_mutex(&loader_json_lock);
3339}
3340
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003341static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003342 // inst is not wrapped
3343 if (inst == VK_NULL_HANDLE) {
3344 return NULL;
3345 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003346 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003347 void *addr;
3348
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003349 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003350
3351 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003352 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003353 if (found_name) {
3354 return addr;
3355 }
3356
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003357 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, true, NULL, &addr)) return addr;
Mark Young39389872017-01-19 21:10:49 -07003358
3359 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003360 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 -07003361 return NULL;
3362}
3363
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003364static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_terminator(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003365 // inst is not wrapped
3366 if (inst == VK_NULL_HANDLE) {
3367 return NULL;
3368 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003369 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003370 void *addr;
3371
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003372 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003373
3374 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003375 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003376 if (found_name) {
3377 return addr;
3378 }
3379
3380 // Get the terminator, but don't perform checking since it should already
3381 // have been setup if we get here.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003382 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, false, NULL, &addr)) {
Mark Young39389872017-01-19 21:10:49 -07003383 return addr;
3384 }
3385
3386 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003387 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 -07003388 return NULL;
3389}
3390
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003391static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003392 if (!strcmp(pName, "vkGetInstanceProcAddr")) {
3393 return (PFN_vkVoidFunction)loader_gpa_instance_internal;
3394 }
3395 if (!strcmp(pName, "vk_layerGetPhysicalDeviceProcAddr")) {
3396 return (PFN_vkVoidFunction)loader_gpdpa_instance_terminator;
3397 }
3398 if (!strcmp(pName, "vkCreateInstance")) {
3399 return (PFN_vkVoidFunction)terminator_CreateInstance;
3400 }
3401 if (!strcmp(pName, "vkCreateDevice")) {
3402 return (PFN_vkVoidFunction)terminator_CreateDevice;
3403 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003404
Jon Ashburn27cd5842015-05-12 17:26:48 -06003405 // inst is not wrapped
3406 if (inst == VK_NULL_HANDLE) {
3407 return NULL;
3408 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003409 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003410 void *addr;
3411
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003412 if (disp_table == NULL) return NULL;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003413
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003414 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003415 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003416 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003417 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003418 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003419
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003420 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003421 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 -07003422 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003423}
3424
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003425VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_device_internal(VkDevice device, const char *pName) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003426 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003427 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Mark Young16573c72016-06-28 10:52:43 -06003428
Mark Young65cb3662016-11-07 13:27:02 -07003429 // NOTE: Device Funcs needing Trampoline/Terminator.
3430 // Overrides for device functions needing a trampoline and
3431 // a terminator because certain device entry-points still need to go
3432 // through a terminator before hitting the ICD. This could be for
3433 // several reasons, but the main one is currently unwrapping an
3434 // object before passing the appropriate info along to the ICD.
3435 // This is why we also have to override the direct ICD call to
3436 // vkGetDeviceProcAddr to intercept those calls.
3437 if (!strcmp(pName, "vkGetDeviceProcAddr")) {
3438 return (PFN_vkVoidFunction)loader_gpa_device_internal;
3439 } else if (!strcmp(pName, "vkCreateSwapchainKHR")) {
Mark Young16573c72016-06-28 10:52:43 -06003440 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
Mark Young39389872017-01-19 21:10:49 -07003441 } else if (!strcmp(pName, "vkCreateSharedSwapchainsKHR")) {
3442 return (PFN_vkVoidFunction)terminator_vkCreateSharedSwapchainsKHR;
Mark Young65cb3662016-11-07 13:27:02 -07003443 } else if (!strcmp(pName, "vkDebugMarkerSetObjectTagEXT")) {
3444 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectTagEXT;
3445 } else if (!strcmp(pName, "vkDebugMarkerSetObjectNameEXT")) {
3446 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectNameEXT;
Mark Young16573c72016-06-28 10:52:43 -06003447 }
3448
Mark Young0153e0b2016-11-03 14:27:13 -06003449 return icd_term->GetDeviceProcAddr(device, pName);
Piers Daniell295fe402016-03-29 11:51:11 -06003450}
3451
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003452/**
3453 * Initialize device_ext dispatch table entry as follows:
3454 * If dev == NULL find all logical devices created within this instance and
3455 * init the entry (given by idx) in the ext dispatch table.
3456 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003457 * The initialization value is gotten by calling down the device chain with
3458 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003459 * If GDPA returns NULL then don't initialize the dispatch table entry.
3460 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003461static 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 -07003462 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003463
Jon Ashburn23d36b12016-02-02 17:47:28 -07003464{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003465 void *gdpa_value;
3466 if (dev != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003467 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(dev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003468 if (gdpa_value != NULL) dev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003469 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003470 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 -06003471 struct loader_device *ldev = icd_term->logical_device_list;
Karl Schultz2558bd32016-02-24 14:39:39 -07003472 while (ldev) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003473 gdpa_value = ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(ldev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003474 if (gdpa_value != NULL) ldev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003475 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003476 }
3477 }
3478 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003479}
3480
3481/**
3482 * Find all dev extension in the hash table and initialize the dispatch table
3483 * for dev for each of those extension entrypoints found in hash table.
3484
3485 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003486void loader_init_dispatch_dev_ext(struct loader_instance *inst, struct loader_device *dev) {
Mark Young39389872017-01-19 21:10:49 -07003487 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3488 if (inst->dev_ext_disp_hash[i].func_name != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003489 loader_init_dispatch_dev_ext_entry(inst, dev, i, inst->dev_ext_disp_hash[i].func_name);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003490 }
3491}
3492
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003493static bool loader_check_icds_for_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young0153e0b2016-11-03 14:27:13 -06003494 struct loader_icd_term *icd_term;
3495 icd_term = inst->icd_terms;
3496 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003497 if (icd_term->scanned_icd->GetInstanceProcAddr(icd_term->instance, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003498 // this icd supports funcName
3499 return true;
Mark Young0153e0b2016-11-03 14:27:13 -06003500 icd_term = icd_term->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003501 }
3502
3503 return false;
3504}
3505
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003506static 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 -07003507 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003508 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003509 // Iterate over the extensions.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003510 const struct loader_device_extension_list *const extensions = &(layers->list[layer].device_extension_list);
3511 for (uint32_t extension = 0; extension < extensions->count; ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003512 // Iterate over the entry points.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003513 const struct loader_dev_ext_props *const property = &(extensions->list[extension]);
3514 for (uint32_t entry = 0; entry < property->entrypoint_count; ++entry) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003515 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003516 return true;
3517 }
3518 }
3519 }
3520 }
3521
3522 return false;
3523}
3524
Jon Ashburn23d36b12016-02-02 17:47:28 -07003525static void loader_free_dev_ext_table(struct loader_instance *inst) {
Mark Young39389872017-01-19 21:10:49 -07003526 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3527 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].func_name);
3528 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003529 }
Mark Young39389872017-01-19 21:10:49 -07003530 memset(inst->dev_ext_disp_hash, 0, sizeof(inst->dev_ext_disp_hash));
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003531}
3532
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003533static bool loader_add_dev_ext_table(struct loader_instance *inst, uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003534 uint32_t i;
3535 uint32_t idx = *ptr_idx;
Mark Young39389872017-01-19 21:10:49 -07003536 struct loader_dispatch_hash_list *list = &inst->dev_ext_disp_hash[idx].list;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003537
Mark Young39389872017-01-19 21:10:49 -07003538 if (!inst->dev_ext_disp_hash[idx].func_name) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003539 // no entry here at this idx, so use it
3540 assert(list->capacity == 0);
Mark Young39389872017-01-19 21:10:49 -07003541 inst->dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003542 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003543 if (inst->dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003544 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3545 "loader_add_dev_ext_table: Failed to allocate memory "
3546 "for func_name %s",
Mark Youngb6399312017-01-10 14:22:15 -07003547 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003548 return false;
3549 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003550 strncpy(inst->dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003551 return true;
3552 }
3553
3554 // check for enough capacity
3555 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003556 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003557 if (list->index == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003558 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3559 "loader_add_dev_ext_table: Failed to allocate memory "
3560 "for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003561 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003562 return false;
3563 }
3564 list->capacity = 8 * sizeof(*(list->index));
3565 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003566 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3567 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003568 if (list->index == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003569 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3570 "loader_add_dev_ext_table: Failed to reallocate memory "
3571 "for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003572 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003573 return false;
3574 }
3575 list->capacity *= 2;
3576 }
3577
Jon Ashburn23d36b12016-02-02 17:47:28 -07003578 // find an unused index in the hash table and use it
Mark Young39389872017-01-19 21:10:49 -07003579 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003580 do {
Mark Young39389872017-01-19 21:10:49 -07003581 if (!inst->dev_ext_disp_hash[i].func_name) {
3582 assert(inst->dev_ext_disp_hash[i].list.capacity == 0);
3583 inst->dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003584 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003585 if (inst->dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003586 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3587 "loader_add_dev_ext_table: Failed to allocate memory "
3588 "for func_name %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003589 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003590 return false;
3591 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003592 strncpy(inst->dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003593 list->index[list->count] = i;
3594 list->count++;
3595 *ptr_idx = i;
3596 return true;
3597 }
Mark Young39389872017-01-19 21:10:49 -07003598 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003599 } while (i != idx);
3600
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003601 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3602 "loader_add_dev_ext_table: Could not insert into hash table; is "
3603 "it full?");
Mark Youngb6399312017-01-10 14:22:15 -07003604
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003605 return false;
3606}
3607
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003608static bool loader_name_in_dev_ext_table(struct loader_instance *inst, uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003609 uint32_t alt_idx;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003610 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 -07003611
3612 // funcName wasn't at the primary spot in the hash table
3613 // search the list of secondary locations (shallow search, not deep search)
Mark Young39389872017-01-19 21:10:49 -07003614 for (uint32_t i = 0; i < inst->dev_ext_disp_hash[*idx].list.count; i++) {
3615 alt_idx = inst->dev_ext_disp_hash[*idx].list.index[i];
3616 if (!strcmp(inst->dev_ext_disp_hash[*idx].func_name, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003617 *idx = alt_idx;
3618 return true;
3619 }
3620 }
3621
3622 return false;
3623}
3624
3625/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003626 * This function returns generic trampoline code address for unknown entry
3627 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003628 * Presumably, these unknown entry points (as given by funcName) are device
3629 * extension entrypoints. A hash table is used to keep a list of unknown entry
3630 * points and their mapping to the device extension dispatch table
3631 * (struct loader_dev_ext_dispatch_table).
3632 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003633 * For a given entry point string (funcName), if an existing mapping is found
3634 * the
3635 * trampoline address for that mapping is returned. Otherwise, this unknown
3636 * entry point
3637 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3638 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003639 * new entry in the hash table is initialized and that trampoline address for
3640 * the new entry is returned. Null is returned if the hash table is full or
3641 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3642 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003643void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003644 uint32_t idx;
3645 uint32_t seed = 0;
3646
Mark Young39389872017-01-19 21:10:49 -07003647 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003648
3649 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3650 // found funcName already in hash
3651 return loader_get_dev_ext_trampoline(idx);
3652
3653 // Check if funcName is supported in either ICDs or a layer library
Mark Young39389872017-01-19 21:10:49 -07003654 if (!loader_check_icds_for_dev_ext_address(inst, funcName) &&
3655 !loader_check_layer_list_for_dev_ext_address(&inst->instance_layer_list, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003656 // if support found in layers continue on
3657 return NULL;
3658 }
3659
3660 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3661 // successfully added new table entry
3662 // init any dev dispatch table entrys as needed
3663 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3664 return loader_get_dev_ext_trampoline(idx);
3665 }
3666
3667 return NULL;
3668}
3669
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003670static bool loader_check_icds_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07003671 struct loader_icd_term *icd_term;
3672 icd_term = inst->icd_terms;
3673 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003674 if (icd_term->scanned_icd->interface_version >= MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION &&
3675 icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName))
Mark Young39389872017-01-19 21:10:49 -07003676 // this icd supports funcName
3677 return true;
3678 icd_term = icd_term->next;
3679 }
3680
3681 return false;
3682}
3683
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003684static bool loader_check_layer_list_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
3685 struct loader_layer_properties *layer_prop_list = inst->activated_layer_list.list;
Mark Young39389872017-01-19 21:10:49 -07003686 for (uint32_t layer = 0; layer < inst->activated_layer_list.count; ++layer) {
3687 // If this layer supports the vk_layerGetPhysicalDeviceProcAddr, then call
3688 // it and see if it returns a valid pointer for this function name.
3689 if (layer_prop_list[layer].interface_version > 1) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003690 const struct loader_layer_functions *const functions = &(layer_prop_list[layer].functions);
Mark Young39389872017-01-19 21:10:49 -07003691 if (NULL != functions->get_physical_device_proc_addr &&
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003692 NULL != functions->get_physical_device_proc_addr((VkInstance)inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003693 return true;
3694 }
3695 }
3696 }
3697
3698 return false;
3699}
3700
Mark Young39389872017-01-19 21:10:49 -07003701static void loader_free_phys_dev_ext_table(struct loader_instance *inst) {
3702 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003703 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].func_name);
3704 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].list.index);
Mark Young39389872017-01-19 21:10:49 -07003705 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003706 memset(inst->phys_dev_ext_disp_hash, 0, sizeof(inst->phys_dev_ext_disp_hash));
Mark Young39389872017-01-19 21:10:49 -07003707}
3708
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003709static 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 -07003710 uint32_t i;
3711 uint32_t idx = *ptr_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003712 struct loader_dispatch_hash_list *list = &inst->phys_dev_ext_disp_hash[idx].list;
Mark Young39389872017-01-19 21:10:49 -07003713
3714 if (!inst->phys_dev_ext_disp_hash[idx].func_name) {
3715 // no entry here at this idx, so use it
3716 assert(list->capacity == 0);
3717 inst->phys_dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003718 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003719 if (inst->phys_dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003720 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3721 "loader_add_phys_dev_ext_table() can't allocate memory for "
3722 "func_name");
Mark Young39389872017-01-19 21:10:49 -07003723 return false;
3724 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003725 strncpy(inst->phys_dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07003726 return true;
3727 }
3728
3729 // check for enough capacity
3730 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003731 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003732 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003733 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 -07003734 return false;
3735 }
3736 list->capacity = 8 * sizeof(*(list->index));
3737 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003738 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3739 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003740 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003741 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 -07003742 return false;
3743 }
3744 list->capacity *= 2;
3745 }
3746
3747 // find an unused index in the hash table and use it
3748 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
3749 do {
3750 if (!inst->phys_dev_ext_disp_hash[i].func_name) {
3751 assert(inst->phys_dev_ext_disp_hash[i].list.capacity == 0);
3752 inst->phys_dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003753 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003754 if (inst->phys_dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003755 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3756 "loader_add_dev_ext_table() can't rallocate "
3757 "func_name memory");
Mark Young39389872017-01-19 21:10:49 -07003758 return false;
3759 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003760 strncpy(inst->phys_dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07003761 list->index[list->count] = i;
3762 list->count++;
3763 *ptr_idx = i;
3764 return true;
3765 }
3766 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
3767 } while (i != idx);
3768
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003769 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3770 "loader_add_phys_dev_ext_table() couldn't insert into hash table; is "
3771 "it full?");
Mark Young39389872017-01-19 21:10:49 -07003772 return false;
3773}
3774
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003775static 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 -07003776 uint32_t alt_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003777 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 -07003778 return true;
3779
3780 // funcName wasn't at the primary spot in the hash table
3781 // search the list of secondary locations (shallow search, not deep search)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003782 for (uint32_t i = 0; i < inst->phys_dev_ext_disp_hash[*idx].list.count; i++) {
Mark Young39389872017-01-19 21:10:49 -07003783 alt_idx = inst->phys_dev_ext_disp_hash[*idx].list.index[i];
3784 if (!strcmp(inst->phys_dev_ext_disp_hash[*idx].func_name, funcName)) {
3785 *idx = alt_idx;
3786 return true;
3787 }
3788 }
3789
3790 return false;
3791}
3792
3793// This function returns a generic trampoline and/or terminator function
3794// address for any unknown physical device extension commands. A hash
3795// table is used to keep a list of unknown entry points and their
3796// mapping to the physical device extension dispatch table (struct
3797// loader_phys_dev_ext_dispatch_table).
3798// For a given entry point string (funcName), if an existing mapping is
3799// found, then the trampoline address for that mapping is returned in
3800// tramp_addr (if it is not NULL) and the terminator address for that
3801// mapping is returned in term_addr (if it is not NULL). Otherwise,
3802// this unknown entry point has not been seen yet.
3803// If it has not been seen before, and perform_checking is 'true',
3804// check if a layer or and ICD supports it. If so then a new entry in
3805// the hash table is initialized and the trampoline and/or terminator
3806// addresses are returned.
3807// Null is returned if the hash table is full or if no discovered layer or
3808// ICD returns a non-NULL GetProcAddr for it.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003809bool 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 -07003810 void **term_addr) {
3811 uint32_t idx;
3812 uint32_t seed = 0;
3813 bool success = false;
3814
3815 if (inst == NULL) {
3816 goto out;
3817 }
3818
3819 if (NULL != tramp_addr) {
3820 *tramp_addr = NULL;
3821 }
3822 if (NULL != term_addr) {
3823 *term_addr = NULL;
3824 }
3825
3826 // We should always check to see if any ICD supports it.
3827 if (!loader_check_icds_for_phys_dev_ext_address(inst, funcName)) {
3828 // If we're not checking layers, or we are and it's not in a layer, just
3829 // return
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003830 if (!perform_checking || !loader_check_layer_list_for_phys_dev_ext_address(inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003831 goto out;
3832 }
3833 }
3834
3835 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003836 if (perform_checking && !loader_name_in_phys_dev_ext_table(inst, &idx, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003837 uint32_t i;
3838 bool added = false;
3839
3840 // Only need to add first one to get index in Instance. Others will use
3841 // the same index.
3842 if (!added && loader_add_phys_dev_ext_table(inst, &idx, funcName)) {
3843 added = true;
3844 }
3845
3846 // Setup the ICD function pointers
3847 struct loader_icd_term *icd_term = inst->icd_terms;
3848 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003849 if (MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION <= icd_term->scanned_icd->interface_version &&
Mark Young39389872017-01-19 21:10:49 -07003850 NULL != icd_term->scanned_icd->GetPhysicalDeviceProcAddr) {
3851 icd_term->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003852 (PFN_PhysDevExt)icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName);
Mark Young39389872017-01-19 21:10:49 -07003853
3854 // Make sure we set the instance dispatch to point to the
3855 // loader's terminator now since we can at least handle it
3856 // in one ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003857 inst->disp->phys_dev_ext[idx] = loader_get_phys_dev_ext_termin(idx);
Mark Young39389872017-01-19 21:10:49 -07003858 } else {
3859 icd_term->phys_dev_ext[idx] = NULL;
3860 }
3861
3862 icd_term = icd_term->next;
3863 }
3864
3865 // Now, search for the first layer attached and query using it to get
3866 // the first entry point.
3867 for (i = 0; i < inst->activated_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003868 struct loader_layer_properties *layer_prop = &inst->activated_layer_list.list[i];
3869 if (layer_prop->interface_version > 1 && NULL != layer_prop->functions.get_physical_device_proc_addr) {
Mark Young39389872017-01-19 21:10:49 -07003870 inst->disp->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003871 (PFN_PhysDevExt)layer_prop->functions.get_physical_device_proc_addr((VkInstance)inst, funcName);
Mark Young39389872017-01-19 21:10:49 -07003872 if (NULL != inst->disp->phys_dev_ext[idx]) {
3873 break;
3874 }
3875 }
3876 }
3877 }
3878
3879 if (NULL != tramp_addr) {
3880 *tramp_addr = loader_get_phys_dev_ext_tramp(idx);
3881 }
3882
3883 if (NULL != term_addr) {
3884 *term_addr = loader_get_phys_dev_ext_termin(idx);
3885 }
3886
3887 success = true;
3888
3889out:
3890 return success;
3891}
3892
Jon Ashburn23d36b12016-02-02 17:47:28 -07003893struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003894 /* look up the loader_instance in our list by comparing dispatch tables, as
3895 * there is no guarantee the instance is still a loader_instance* after any
3896 * layers which wrap the instance object.
3897 */
3898 const VkLayerInstanceDispatchTable *disp;
3899 struct loader_instance *ptr_instance = NULL;
Mark Young39389872017-01-19 21:10:49 -07003900 disp = loader_get_instance_layer_dispatch(instance);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003901 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
Mark Young39389872017-01-19 21:10:49 -07003902 if (&inst->disp->layer_inst_disp == disp) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003903 ptr_instance = inst;
3904 break;
3905 }
3906 }
3907 return ptr_instance;
3908}
3909
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003910static loader_platform_dl_handle loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3911 struct loader_layer_properties *prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003912 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) == NULL) {
3913 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 -06003914 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003915 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Loading layer library %s", prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003916 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003917
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003918 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003919}
3920
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003921static void loader_close_layer_lib(const struct loader_instance *inst, struct loader_layer_properties *prop) {
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003922 if (prop->lib_handle) {
3923 loader_platform_close_library(prop->lib_handle);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003924 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Unloading layer library %s", prop->lib_name);
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003925 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003926 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003927}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003928
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003929void loader_deactivate_layers(const struct loader_instance *instance, struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003930 struct loader_layer_list *list) {
3931 /* delete instance list of enabled layers and close any layer libraries */
3932 for (uint32_t i = 0; i < list->count; i++) {
3933 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003934
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003935 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003936 }
Mark Young0ad83132016-06-30 13:02:42 -06003937 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003938}
3939
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003940/**
3941 * Go through the search_list and find any layers which match type. If layer
3942 * type match is found in then add it to ext_list.
3943 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003944static void loader_add_layer_implicit(const struct loader_instance *inst, const enum layer_type type,
3945 struct loader_layer_list *list, const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003946 bool enable;
3947 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003948 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003949 for (i = 0; i < search_list->count; i++) {
3950 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003951 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003952 /* Found an implicit layer, see if it should be enabled */
3953 enable = false;
3954
Jon Ashburn23d36b12016-02-02 17:47:28 -07003955 // if no enable_environment variable is specified, this implicit
3956 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003957 // should always be enabled. Otherwise check if the variable is set
3958 if (prop->enable_env_var.name[0] == 0) {
3959 enable = true;
3960 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003961 env_value = loader_getenv(prop->enable_env_var.name, inst);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003962 if (env_value && !strcmp(prop->enable_env_var.value, env_value)) enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003963 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003964 }
3965
3966 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003967 // environment variables are set, the layer is disabled. Implicit
3968 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003969 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003970 env_value = loader_getenv(prop->disable_env_var.name, inst);
3971 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003972 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003973 }
3974 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003975
Mark Young0ad83132016-06-30 13:02:42 -06003976 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003977 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003978 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003979 }
3980 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003981}
3982
3983/**
3984 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003985 * is found in search_list then add it to layer_list. But only add it to
3986 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003987 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003988static void loader_add_layer_env(struct loader_instance *inst, const enum layer_type type, const char *env_name,
3989 struct loader_layer_list *layer_list, const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003990 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003991 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003992
Mark Young0ad83132016-06-30 13:02:42 -06003993 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003994 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003995 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003996 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003997 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003998 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003999 return;
Ian Elliott4470a302015-02-17 10:33:47 -07004000 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06004001 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004002
Mark Young0ad83132016-06-30 13:02:42 -06004003 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07004004
Jon Ashburn23d36b12016-02-02 17:47:28 -07004005 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06004006 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07004007 if (!strcmp(std_validation_str, name)) {
4008 /* add meta list of layers
4009 don't attempt to remove duplicate layers already added by app or
4010 env var
4011 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004012 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 -07004013 std_validation_str);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004014 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT) inst->activated_layers_are_std_val = true;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004015 for (uint32_t i = 0; i < sizeof(std_validation_names) / sizeof(std_validation_names[0]); i++) {
4016 loader_find_layer_name_add_list(inst, std_validation_names[i], type, search_list, layer_list);
Jon Ashburn71483442016-02-11 18:59:43 -07004017 }
4018 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004019 loader_find_layer_name_add_list(inst, name, type, search_list, layer_list);
Jon Ashburn71483442016-02-11 18:59:43 -07004020 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06004021 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004022 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004023
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004024 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004025}
4026
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004027VkResult loader_enable_instance_layers(struct loader_instance *inst, const VkInstanceCreateInfo *pCreateInfo,
4028 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004029 VkResult err;
4030
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004031 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004032
Jon Ashburne39a4f82015-08-28 13:38:21 -06004033 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004034 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4035 "loader_enable_instance_layers: Failed to initialize"
4036 " the layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004037 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06004038 }
4039
Jon Ashburn0c26e712015-07-02 16:10:32 -06004040 /* Add any implicit layers first */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004041 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06004042
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06004043 /* Add any layers specified via environment variable next */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004044 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 -06004045
4046 /* Add layers specified by the application */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004047 err = loader_add_layer_names_to_list(inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
4048 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004049
4050 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004051}
4052
Mark Young39389872017-01-19 21:10:49 -07004053// Determine the layer interface version to use.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004054bool loader_get_layer_interface_version(PFN_vkNegotiateLoaderLayerInterfaceVersion fp_negotiate_layer_version,
4055 VkNegotiateLayerInterface *interface_struct) {
Mark Young39389872017-01-19 21:10:49 -07004056 memset(interface_struct, 0, sizeof(VkNegotiateLayerInterface));
4057
4058 // Base assumption is that all layers are version 1 at least.
4059 interface_struct->loaderLayerInterfaceVersion = 1;
4060
4061 if (fp_negotiate_layer_version != NULL) {
4062 // Layer supports the negotiation API, so call it with the loader's
4063 // latest version supported
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004064 interface_struct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
Mark Young39389872017-01-19 21:10:49 -07004065 VkResult result = fp_negotiate_layer_version(interface_struct);
4066
4067 if (result != VK_SUCCESS) {
4068 // Layer no longer supports the loader's latest interface version so
4069 // fail loading the Layer
4070 return false;
4071 }
4072 }
4073
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004074 if (interface_struct->loaderLayerInterfaceVersion < MIN_SUPPORTED_LOADER_LAYER_INTERFACE_VERSION) {
Mark Young39389872017-01-19 21:10:49 -07004075 // Loader no longer supports the layer's latest interface version so
4076 // fail loading the layer
4077 return false;
4078 }
4079
4080 return true;
4081}
4082
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004083/*
4084 * Given the list of layers to activate in the loader_instance
4085 * structure. This function will add a VkLayerInstanceCreateInfo
4086 * structure to the VkInstanceCreateInfo.pNext pointer.
4087 * Each activated layer will have it's own VkLayerInstanceLink
4088 * structure that tells the layer what Get*ProcAddr to call to
4089 * get function pointers to the next layer down.
4090 * Once the chain info has been created this function will
4091 * execute the CreateInstance call chain. Each layer will
4092 * then have an opportunity in it's CreateInstance function
4093 * to setup it's dispatch table when the lower layer returns
4094 * successfully.
4095 * Each layer can wrap or not-wrap the returned VkInstance object
4096 * as it sees fit.
4097 * The instance chain is terminated by a loader function
4098 * that will call CreateInstance on all available ICD's and
4099 * cache those VkInstance objects for future use.
4100 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004101VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
4102 struct loader_instance *inst, VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004103 uint32_t activated_layers = 0;
4104 VkLayerInstanceCreateInfo chain_info;
4105 VkLayerInstanceLink *layer_instance_link_info = NULL;
4106 VkInstanceCreateInfo loader_create_info;
4107 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004108
Mark Young39389872017-01-19 21:10:49 -07004109 PFN_vkGetInstanceProcAddr next_gipa = loader_gpa_instance_internal;
4110 PFN_vkGetInstanceProcAddr cur_gipa = loader_gpa_instance_internal;
4111 PFN_GetPhysicalDeviceProcAddr next_gpdpa = loader_gpdpa_instance_internal;
4112 PFN_GetPhysicalDeviceProcAddr cur_gpdpa = loader_gpdpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004113
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004114 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06004115
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004116 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004117 chain_info.u.pLayerInfo = NULL;
4118 chain_info.pNext = pCreateInfo->pNext;
4119 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
4120 chain_info.function = VK_LAYER_LINK_INFO;
4121 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004122
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004123 layer_instance_link_info = loader_stack_alloc(sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004124 if (!layer_instance_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004125 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4126 "loader_create_instance_chain: Failed to alloc Instance"
4127 " objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004128 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004129 }
4130
Mark Young39389872017-01-19 21:10:49 -07004131 // Create instance chain of enabled layers
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004132 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004133 struct loader_layer_properties *layer_prop = &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004134 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004135
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004136 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004137 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004138 continue;
Mark Young39389872017-01-19 21:10:49 -07004139 }
4140
4141 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004142 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
Mark Young39389872017-01-19 21:10:49 -07004143 bool functions_in_interface = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004144 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4145 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4146 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004147 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004148 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4149 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004150 }
4151
4152 // If we can negotiate an interface version, then we can also
4153 // get everything we need from the one function call, so try
4154 // that first, and see if we can get all the function pointers
4155 // necessary from that one call.
4156 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004157 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004158
4159 VkNegotiateLayerInterface interface_struct;
4160
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004161 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Young39389872017-01-19 21:10:49 -07004162 // Go ahead and set the properites version to the
4163 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004164 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004165
4166 // If the interface is 2 or newer, we have access to the
4167 // new GetPhysicalDeviceProcAddr function, so grab it,
4168 // and the other necessary functions, from the
4169 // structure.
4170 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4171 cur_gipa = interface_struct.pfnGetInstanceProcAddr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004172 cur_gpdpa = interface_struct.pfnGetPhysicalDeviceProcAddr;
Mark Young39389872017-01-19 21:10:49 -07004173 if (cur_gipa != NULL) {
4174 // We've set the functions, so make sure we
4175 // don't do the unnecessary calls later.
4176 functions_in_interface = true;
4177 }
4178 }
4179 }
4180 }
4181
4182 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004183 if ((cur_gipa = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004184 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004185 cur_gipa =
4186 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
4187 layer_prop->functions.get_instance_proc_addr = cur_gipa;
Mark Young39389872017-01-19 21:10:49 -07004188 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004189 cur_gipa = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle,
4190 layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004191 }
4192
4193 if (NULL == cur_gipa) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004194 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4195 "loader_create_instance_chain: Failed to"
4196 " find \'vkGetInstanceProcAddr\' in "
4197 "layer %s",
Mark Young39389872017-01-19 21:10:49 -07004198 layer_prop->lib_name);
4199 continue;
4200 }
4201 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004202 }
4203 }
4204
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004205 layer_instance_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4206 layer_instance_link_info[activated_layers].pfnNextGetInstanceProcAddr = next_gipa;
4207 layer_instance_link_info[activated_layers].pfnNextGetPhysicalDeviceProcAddr = next_gpdpa;
Mark Young39389872017-01-19 21:10:49 -07004208 next_gipa = cur_gipa;
4209 if (layer_prop->interface_version > 1 && cur_gpdpa != NULL) {
4210 layer_prop->functions.get_physical_device_proc_addr = cur_gpdpa;
4211 next_gpdpa = cur_gpdpa;
4212 }
4213
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004214 chain_info.u.pLayerInfo = &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004215
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004216 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert instance layer %s (%s)", layer_prop->info.layerName,
4217 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004218
4219 activated_layers++;
4220 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06004221 }
4222
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004223 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)next_gipa(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004224 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06004225 VkLayerInstanceCreateInfo create_info_disp;
4226
Jon Ashburncc407a22016-04-15 09:25:03 -06004227 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004228 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06004229
4230 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
4231
4232 create_info_disp.pNext = loader_create_info.pNext;
4233 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004234 res = fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004235 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004236 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4237 "loader_create_instance_chain: Failed to find "
4238 "\'vkCreateInstance\'");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004239 // Couldn't find CreateInstance function!
4240 res = VK_ERROR_INITIALIZATION_FAILED;
4241 }
4242
Mark Young39389872017-01-19 21:10:49 -07004243 if (res == VK_SUCCESS) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004244 loader_init_instance_core_dispatch_table(&inst->disp->layer_inst_disp, next_gipa, *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07004245 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004246 }
4247
4248 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004249}
4250
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004251void loader_activate_instance_layer_extensions(struct loader_instance *inst, VkInstance created_inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004252 loader_init_instance_extension_dispatch_table(&inst->disp->layer_inst_disp, inst->disp->layer_inst_disp.GetInstanceProcAddr,
4253 created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004254}
4255
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004256VkResult loader_create_device_chain(const struct loader_physical_device_tramp *pd, const VkDeviceCreateInfo *pCreateInfo,
4257 const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst,
4258 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004259 uint32_t activated_layers = 0;
4260 VkLayerDeviceLink *layer_device_link_info;
4261 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004262 VkDeviceCreateInfo loader_create_info;
4263 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004264
Piers Daniell295fe402016-03-29 11:51:11 -06004265 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004266 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004267
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004268 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
4269
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004270 layer_device_link_info = loader_stack_alloc(sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004271 if (!layer_device_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004272 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4273 "loader_create_device_chain: Failed to alloc Device objects"
4274 " for layer. Skipping Layer.");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004275 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06004276 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004277
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004278 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06004279 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
4280 chain_info.function = VK_LAYER_LINK_INFO;
4281 chain_info.u.pLayerInfo = NULL;
Mark Young39389872017-01-19 21:10:49 -07004282 chain_info.pNext = loader_create_info.pNext;
Jon Ashburn72690f22016-03-29 12:52:13 -06004283 loader_create_info.pNext = &chain_info;
4284
Mark Young39389872017-01-19 21:10:49 -07004285 // Create instance chain of enabled layers
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004286 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004287 struct loader_layer_properties *layer_prop = &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004288 loader_platform_dl_handle lib_handle;
Mark Young39389872017-01-19 21:10:49 -07004289 bool functions_in_interface = false;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004290
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004291 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004292 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004293 continue;
Jon Ashburn21c21ee2015-09-09 11:29:24 -06004294 }
Mark Young39389872017-01-19 21:10:49 -07004295
4296 // If we can negotiate an interface version, then we can also
4297 // get everything we need from the one function call, so try
4298 // that first, and see if we can get all the function pointers
4299 // necessary from that one call.
4300 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004301 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
4302 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4303 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4304 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004305 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004306 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4307 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004308 }
4309
4310 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004311 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004312
4313 VkNegotiateLayerInterface interface_struct;
4314
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004315 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Young39389872017-01-19 21:10:49 -07004316 // Go ahead and set the properites version to the
4317 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004318 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004319
4320 // If the interface is 2 or newer, we have access to the
4321 // new GetPhysicalDeviceProcAddr function, so grab it,
4322 // and the other necessary functions, from the structure.
4323 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4324 fpGIPA = interface_struct.pfnGetInstanceProcAddr;
4325 fpGDPA = interface_struct.pfnGetDeviceProcAddr;
4326 if (fpGIPA != NULL && fpGDPA) {
4327 // We've set the functions, so make sure we
4328 // don't do the unnecessary calls later.
4329 functions_in_interface = true;
4330 }
4331 }
4332 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004333 }
4334 }
4335
Mark Young39389872017-01-19 21:10:49 -07004336 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004337 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004338 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004339 fpGIPA = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004340 layer_prop->functions.get_instance_proc_addr = fpGIPA;
4341 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004342 fpGIPA =
4343 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004344 if (!fpGIPA) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004345 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4346 "loader_create_device_chain: Failed to find "
4347 "\'vkGetInstanceProcAddr\' in layer %s. Skipping"
4348 " layer.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004349 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004350 continue;
4351 }
4352 }
4353 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
4354 if (strlen(layer_prop->functions.str_gdpa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004355 fpGDPA = (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004356 layer_prop->functions.get_device_proc_addr = fpGDPA;
4357 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004358 fpGDPA =
4359 (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gdpa);
Mark Young39389872017-01-19 21:10:49 -07004360 if (!fpGDPA) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004361 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Failed to find vkGetDeviceProcAddr in layer %s",
4362 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004363 continue;
4364 }
4365 }
4366 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004367 layer_device_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4368 layer_device_link_info[activated_layers].pfnNextGetInstanceProcAddr = nextGIPA;
4369 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr = nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004370 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
4371 nextGIPA = fpGIPA;
4372 nextGDPA = fpGDPA;
4373
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004374 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert device layer %s (%s)", layer_prop->info.layerName,
4375 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004376
4377 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06004378 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004379 }
4380
Jon Ashburncc407a22016-04-15 09:25:03 -06004381 VkDevice created_device = (VkDevice)dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004382 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004383 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06004384 VkLayerDeviceCreateInfo create_info_disp;
4385
Jon Ashburncc407a22016-04-15 09:25:03 -06004386 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004387 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
4388
4389 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
4390
4391 create_info_disp.pNext = loader_create_info.pNext;
4392 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004393 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator, &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06004394 if (res != VK_SUCCESS) {
4395 return res;
4396 }
Mark Young65cb3662016-11-07 13:27:02 -07004397 dev->chain_device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004398 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004399 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4400 "loader_create_device_chain: Failed to find \'vkCreateDevice\' "
4401 "in layer %s");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004402 // Couldn't find CreateDevice function!
4403 return VK_ERROR_INITIALIZATION_FAILED;
4404 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004405
Mark Young65cb3662016-11-07 13:27:02 -07004406 // Initialize device dispatch table
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004407 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA, dev->chain_device);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004408
4409 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06004410}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004411
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004412VkResult loader_validate_layers(const struct loader_instance *inst, const uint32_t layer_count,
4413 const char *const *ppEnabledLayerNames, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004414 struct loader_layer_properties *prop;
4415
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004416 for (uint32_t i = 0; i < layer_count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004417 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004418 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004419 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4420 "loader_validate_layers: Device ppEnabledLayerNames "
4421 "contains string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004422 return VK_ERROR_LAYER_NOT_PRESENT;
4423 }
4424
Jon Ashburn23d36b12016-02-02 17:47:28 -07004425 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004426 if (!prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004427 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4428 "loader_validate_layers: Layer %d does not exist in "
4429 "the list of available layers",
Mark Youngb6399312017-01-10 14:22:15 -07004430 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004431 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004432 }
4433 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004434 return VK_SUCCESS;
4435}
4436
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004437VkResult loader_validate_instance_extensions(const struct loader_instance *inst, const struct loader_extension_list *icd_exts,
4438 const struct loader_layer_list *instance_layers,
4439 const VkInstanceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004440 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004441 struct loader_layer_properties *layer_prop;
4442
Jon Ashburnf19916e2016-01-11 13:12:43 -07004443 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004444 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004445 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004446 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4447 "loader_validate_instance_extensions: Instance "
4448 "ppEnabledExtensionNames contains "
4449 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004450 return VK_ERROR_EXTENSION_NOT_PRESENT;
4451 }
4452
Lenny Komow4053b812016-12-29 16:27:28 -07004453 // See if the extension is in the list of supported extensions
4454 bool found = false;
4455 for (uint32_t j = 0; LOADER_INSTANCE_EXTENSIONS[j] != NULL; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004456 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], LOADER_INSTANCE_EXTENSIONS[j]) == 0) {
Lenny Komow4053b812016-12-29 16:27:28 -07004457 found = true;
4458 break;
4459 }
4460 }
4461
4462 // If it isn't in the list, return an error
4463 if (!found) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004464 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4465 "loader_validate_instance_extensions: Extension %d "
4466 "not found in list of available extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004467 i);
Lenny Komow4053b812016-12-29 16:27:28 -07004468 return VK_ERROR_EXTENSION_NOT_PRESENT;
4469 }
4470
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004471 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004472
4473 if (extension_prop) {
4474 continue;
4475 }
4476
4477 extension_prop = NULL;
4478
4479 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004480 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004481 layer_prop = loader_get_layer_property(pCreateInfo->ppEnabledLayerNames[j], instance_layers);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004482 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004483 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004484 * should have already filtered this case out.
4485 */
4486 continue;
4487 }
4488
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004489 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004490 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004491 /* Found the extension in one of the layers enabled by the app.
4492 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004493 break;
4494 }
4495 }
4496
4497 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004498 // Didn't find extension name in any of the global layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004499 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4500 "loader_validate_instance_extensions: Extension %d "
4501 "not found in enabled layer list extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004502 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004503 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004504 }
4505 }
4506 return VK_SUCCESS;
4507}
4508
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004509VkResult loader_validate_device_extensions(struct loader_physical_device_tramp *phys_dev,
4510 const struct loader_layer_list *activated_device_layers,
4511 const struct loader_extension_list *icd_exts, const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004512 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004513 struct loader_layer_properties *layer_prop;
4514
Jon Ashburnf19916e2016-01-11 13:12:43 -07004515 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004516 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004517 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004518 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4519 "loader_validate_device_extensions: Device "
4520 "ppEnabledExtensionNames contains "
4521 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004522 return VK_ERROR_EXTENSION_NOT_PRESENT;
4523 }
4524
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004525 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004526 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004527
4528 if (extension_prop) {
4529 continue;
4530 }
4531
Mark Youngb6399312017-01-10 14:22:15 -07004532 // Not in global list, search activated layer extension lists
Jon Ashburn471f44c2016-01-13 12:51:43 -07004533 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4534 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004535
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004536 extension_prop = get_dev_extension_property(extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004537 if (extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004538 // Found the extension in one of the layers enabled by the app.
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004539 break;
4540 }
4541 }
4542
4543 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004544 // Didn't find extension name in any of the device layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004545 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4546 "loader_validate_device_extensions: Extension %d "
4547 "not found in enabled layer list extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004548 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004549 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004550 }
4551 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004552 return VK_SUCCESS;
4553}
4554
Mark Youngb6399312017-01-10 14:22:15 -07004555// Terminator functions for the Instance chain
4556// All named terminator_<Vulakn API name>
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004557VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
4558 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Mark Young0153e0b2016-11-03 14:27:13 -06004559 struct loader_icd_term *icd_term;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004560 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004561 char **filtered_extension_names = NULL;
4562 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004563 VkResult res = VK_SUCCESS;
Mark Young8b4edb52016-11-11 09:31:55 -07004564 bool one_icd_successful = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004565
Jon Ashburncc407a22016-04-15 09:25:03 -06004566 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004567 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4568
Jon Ashburnf19916e2016-01-11 13:12:43 -07004569 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004570 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004571
Mark Youngb6399312017-01-10 14:22:15 -07004572 // NOTE: Need to filter the extensions to only those supported by the ICD.
4573 // No ICD will advertise support for layers. An ICD library could
4574 // support a layer, but it would be independent of the actual ICD,
4575 // just in the same library.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004576 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004577 if (!filtered_extension_names) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004578 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4579 "terminator_CreateInstance: Failed create extension name "
4580 "array for %d extensions",
Mark Youngb6399312017-01-10 14:22:15 -07004581 pCreateInfo->enabledExtensionCount);
Mark Young3a587792016-08-19 15:25:08 -06004582 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4583 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004584 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004585 icd_create_info.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004586
Mark Young0153e0b2016-11-03 14:27:13 -06004587 for (uint32_t i = 0; i < ptr_instance->icd_tramp_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004588 icd_term = loader_icd_add(ptr_instance, &ptr_instance->icd_tramp_list.scanned_list[i]);
Mark Young0153e0b2016-11-03 14:27:13 -06004589 if (NULL == icd_term) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004590 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4591 "terminator_CreateInstance: Failed to add ICD %d to ICD "
4592 "trampoline list.",
Mark Youngb6399312017-01-10 14:22:15 -07004593 i);
Mark Young3a587792016-08-19 15:25:08 -06004594 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4595 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004596 }
Mark Young6267ae62017-01-12 12:27:19 -07004597
Mark Young0ad83132016-06-30 13:02:42 -06004598 icd_create_info.enabledExtensionCount = 0;
4599 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004600
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004601 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young0ad83132016-06-30 13:02:42 -06004602 // traverse scanned icd list adding non-duplicate extensions to the
4603 // list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004604 res = loader_init_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004605 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4606 // If out of memory, bail immediately.
4607 goto out;
4608 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004609 // Something bad happened with this ICD, so free it and try the
4610 // next.
Mark Young0153e0b2016-11-03 14:27:13 -06004611 ptr_instance->icd_terms = icd_term->next;
4612 icd_term->next = NULL;
4613 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004614 continue;
4615 }
4616
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004617 res = loader_add_instance_extensions(ptr_instance, icd_term->scanned_icd->EnumerateInstanceExtensionProperties,
4618 icd_term->scanned_icd->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004619 if (VK_SUCCESS != res) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004620 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004621 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4622 // If out of memory, bail immediately.
4623 goto out;
4624 } else {
4625 // Something bad happened with this ICD, so free it and try
4626 // the next.
Mark Young0153e0b2016-11-03 14:27:13 -06004627 ptr_instance->icd_terms = icd_term->next;
4628 icd_term->next = NULL;
4629 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004630 continue;
4631 }
Mark Young3a587792016-08-19 15:25:08 -06004632 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004633
Mark Young0ad83132016-06-30 13:02:42 -06004634 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004635 prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004636 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004637 filtered_extension_names[icd_create_info.enabledExtensionCount] = (char *)pCreateInfo->ppEnabledExtensionNames[j];
Mark Young0ad83132016-06-30 13:02:42 -06004638 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004639 }
4640 }
Mark Young0ad83132016-06-30 13:02:42 -06004641
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004642 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004643
Mark Young8b4edb52016-11-11 09:31:55 -07004644 VkResult icd_result =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004645 ptr_instance->icd_tramp_list.scanned_list[i].CreateInstance(&icd_create_info, pAllocator, &(icd_term->instance));
Mark Young8b4edb52016-11-11 09:31:55 -07004646 if (VK_ERROR_OUT_OF_HOST_MEMORY == icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004647 // If out of memory, bail immediately.
Mark Young8b4edb52016-11-11 09:31:55 -07004648 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06004649 goto out;
Mark Young8b4edb52016-11-11 09:31:55 -07004650 } else if (VK_SUCCESS != icd_result) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004651 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4652 "terminator_CreateInstance: Failed to CreateInstance in "
4653 "ICD %d. Skipping ICD.",
Mark Youngb6399312017-01-10 14:22:15 -07004654 i);
Mark Young0153e0b2016-11-03 14:27:13 -06004655 ptr_instance->icd_terms = icd_term->next;
4656 icd_term->next = NULL;
4657 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004658 continue;
4659 }
Mark Young0ad83132016-06-30 13:02:42 -06004660
Mark Young0153e0b2016-11-03 14:27:13 -06004661 if (!loader_icd_init_entrys(icd_term, icd_term->instance,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004662 ptr_instance->icd_tramp_list.scanned_list[i].GetInstanceProcAddr)) {
4663 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4664 "terminator_CreateInstance: Failed to CreateInstance and find "
4665 "entrypoints with ICD. Skipping ICD.");
Mark Young3a587792016-08-19 15:25:08 -06004666 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004667 }
Mark Young8b4edb52016-11-11 09:31:55 -07004668
4669 // If we made it this far, at least one ICD was successful
4670 one_icd_successful = true;
Jon Ashburn46888392015-01-29 15:45:51 -07004671 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004672
Mark Young8b4edb52016-11-11 09:31:55 -07004673 // If no ICDs were added to instance list and res is unchanged
4674 // from it's initial value, the loader was unable to find
4675 // a suitable ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004676 if (VK_SUCCESS == res && (ptr_instance->icd_terms == NULL || !one_icd_successful)) {
Mark Young3a587792016-08-19 15:25:08 -06004677 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4678 }
4679
4680out:
4681
4682 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004683 while (NULL != ptr_instance->icd_terms) {
4684 icd_term = ptr_instance->icd_terms;
4685 ptr_instance->icd_terms = icd_term->next;
4686 if (NULL != icd_term->instance) {
4687 icd_term->DestroyInstance(icd_term->instance, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004688 }
Mark Young0153e0b2016-11-03 14:27:13 -06004689 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004690 }
Ian Elliotteb450762015-02-05 15:19:15 -07004691 }
Jon Ashburn46888392015-01-29 15:45:51 -07004692
Mark Young3a587792016-08-19 15:25:08 -06004693 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004694}
4695
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004696VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004697 struct loader_instance *ptr_instance = loader_instance(instance);
Karl Schultze2ef9e62017-01-13 14:01:35 -07004698 if (NULL == ptr_instance) {
4699 return;
4700 }
Mark Young0153e0b2016-11-03 14:27:13 -06004701 struct loader_icd_term *icd_terms = ptr_instance->icd_terms;
4702 struct loader_icd_term *next_icd_term;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004703
4704 // Remove this instance from the list of instances:
4705 struct loader_instance *prev = NULL;
4706 struct loader_instance *next = loader.instances;
4707 while (next != NULL) {
4708 if (next == ptr_instance) {
4709 // Remove this instance from the list:
4710 if (prev)
4711 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004712 else
4713 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004714 break;
4715 }
4716 prev = next;
4717 next = next->next;
4718 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004719
Mark Young0153e0b2016-11-03 14:27:13 -06004720 while (NULL != icd_terms) {
4721 if (icd_terms->instance) {
4722 icd_terms->DestroyInstance(icd_terms->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004723 }
Mark Young0153e0b2016-11-03 14:27:13 -06004724 next_icd_term = icd_terms->next;
4725 icd_terms->instance = VK_NULL_HANDLE;
4726 loader_icd_destroy(ptr_instance, icd_terms, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004727
Mark Young0153e0b2016-11-03 14:27:13 -06004728 icd_terms = next_icd_term;
Jon Ashburn46888392015-01-29 15:45:51 -07004729 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004730
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004731 loader_delete_layer_properties(ptr_instance, &ptr_instance->instance_layer_list);
Mark Young0153e0b2016-11-03 14:27:13 -06004732 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_tramp_list);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004733 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Mark Young39389872017-01-19 21:10:49 -07004734 if (NULL != ptr_instance->phys_devs_term) {
Mark Young0193d652016-12-28 16:10:10 -07004735 for (uint32_t i = 0; i < ptr_instance->phys_dev_count_term; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004736 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term[i]);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07004737 }
Mark Young0ad83132016-06-30 13:02:42 -06004738 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07004739 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004740 loader_free_dev_ext_table(ptr_instance);
Mark Young39389872017-01-19 21:10:49 -07004741 loader_free_phys_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004742}
4743
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004744VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4745 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Mark Young0ad83132016-06-30 13:02:42 -06004746 VkResult res = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06004747 struct loader_physical_device_term *phys_dev_term;
4748 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
4749 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004750
Jon Ashburncc407a22016-04-15 09:25:03 -06004751 struct loader_device *dev = (struct loader_device *)*pDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06004752 PFN_vkCreateDevice fpCreateDevice = icd_term->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004753 struct loader_extension_list icd_exts;
4754
Mark Young65cb3662016-11-07 13:27:02 -07004755 dev->phys_dev_term = phys_dev_term;
4756
Mark Young0ad83132016-06-30 13:02:42 -06004757 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004758
Jon Ashburn1530c342016-02-26 13:14:27 -07004759 if (fpCreateDevice == NULL) {
Mark Young0153e0b2016-11-03 14:27:13 -06004760 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07004761 "terminator_CreateDevice: No vkCreateDevice command exposed "
4762 "by ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004763 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004764 res = VK_ERROR_INITIALIZATION_FAILED;
4765 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004766 }
4767
Jon Ashburn1530c342016-02-26 13:14:27 -07004768 VkDeviceCreateInfo localCreateInfo;
4769 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004770
Mark Youngb6399312017-01-10 14:22:15 -07004771 // NOTE: Need to filter the extensions to only those supported by the ICD.
4772 // No ICD will advertise support for layers. An ICD library could
4773 // support a layer, but it would be independent of the actual ICD,
4774 // just in the same library.
Jon Ashburn1530c342016-02-26 13:14:27 -07004775 char **filtered_extension_names = NULL;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004776 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Mark Youngb6399312017-01-10 14:22:15 -07004777 if (NULL == filtered_extension_names) {
4778 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4779 "terminator_CreateDevice: Failed to create extension name "
4780 "storage for %d extensions %d",
4781 pCreateInfo->enabledExtensionCount);
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004782 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004783 }
4784
Jon Ashburn1530c342016-02-26 13:14:27 -07004785 localCreateInfo.enabledLayerCount = 0;
4786 localCreateInfo.ppEnabledLayerNames = NULL;
4787
4788 localCreateInfo.enabledExtensionCount = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004789 localCreateInfo.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Jon Ashburn1530c342016-02-26 13:14:27 -07004790
Mark Youngb6399312017-01-10 14:22:15 -07004791 // Get the physical device (ICD) extensions
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004792 res = loader_init_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004793 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004794 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004795 }
4796
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004797 res = loader_add_device_extensions(icd_term->this_instance, icd_term->EnumerateDeviceExtensionProperties,
4798 phys_dev_term->phys_dev, icd_term->scanned_icd->lib_name, &icd_exts);
Jon Ashburn014438f2016-03-01 19:51:07 -07004799 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004800 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004801 }
4802
Jon Ashburn1530c342016-02-26 13:14:27 -07004803 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4804 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004805 VkExtensionProperties *prop = get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004806 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004807 filtered_extension_names[localCreateInfo.enabledExtensionCount] = (char *)extension_name;
Jon Ashburn1530c342016-02-26 13:14:27 -07004808 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06004809 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004810 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4811 "vkCreateDevice extension %s not available for "
4812 "devices associated with ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004813 extension_name, icd_term->scanned_icd->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07004814 }
4815 }
4816
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004817 res = fpCreateDevice(phys_dev_term->phys_dev, &localCreateInfo, pAllocator, &dev->icd_device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004818 if (res != VK_SUCCESS) {
Mark Young0153e0b2016-11-03 14:27:13 -06004819 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07004820 "terminator_CreateDevice: Failed in ICD %s vkCreateDevice"
4821 "call",
Mark Young0153e0b2016-11-03 14:27:13 -06004822 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004823 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004824 }
4825
Mark Young65cb3662016-11-07 13:27:02 -07004826 *pDevice = dev->icd_device;
Mark Young0153e0b2016-11-03 14:27:13 -06004827 loader_add_logical_device(icd_term->this_instance, icd_term, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004828
4829 /* Init dispatch pointer in new device object */
4830 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4831
Mark Young0ad83132016-06-30 13:02:42 -06004832out:
4833 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004834 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004835 }
4836
Jon Ashburn1530c342016-02-26 13:14:27 -07004837 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004838}
4839
Mark Young6267ae62017-01-12 12:27:19 -07004840VkResult setupLoaderTrampPhysDevs(VkInstance instance) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004841 VkResult res = VK_SUCCESS;
Mark Young6267ae62017-01-12 12:27:19 -07004842 VkPhysicalDevice *local_phys_devs = NULL;
4843 struct loader_instance *inst;
4844 uint32_t total_count = 0;
4845 struct loader_physical_device_tramp **new_phys_devs = NULL;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004846
Mark Young6267ae62017-01-12 12:27:19 -07004847 inst = loader_get_instance(instance);
4848 if (NULL == inst) {
4849 res = VK_ERROR_INITIALIZATION_FAILED;
4850 goto out;
4851 }
4852 total_count = inst->total_gpu_count;
4853
4854 // Create an array for the new physical devices, which will be stored
4855 // in the instance for the trampoline code.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004856 new_phys_devs = (struct loader_physical_device_tramp **)loader_instance_heap_alloc(
4857 inst, total_count * sizeof(struct loader_physical_device_tramp *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004858 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004859 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4860 "setupLoaderTrampPhysDevs: Failed to allocate new physical device"
4861 " array of size %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004862 total_count);
Mark Youngd8382d72016-12-23 16:59:58 -07004863 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4864 goto out;
4865 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004866 memset(new_phys_devs, 0, total_count * sizeof(struct loader_physical_device_tramp *));
Jon Ashburn014438f2016-03-01 19:51:07 -07004867
Mark Young6267ae62017-01-12 12:27:19 -07004868 // Create a temporary array (on the stack) to keep track of the
4869 // returned VkPhysicalDevice values.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004870 local_phys_devs = loader_stack_alloc(sizeof(VkPhysicalDevice) * total_count);
Mark Young6267ae62017-01-12 12:27:19 -07004871 if (NULL == local_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004872 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4873 "setupLoaderTrampPhysDevs: Failed to allocate local "
4874 "physical device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004875 total_count);
4876 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4877 goto out;
4878 }
4879 memset(local_phys_devs, 0, sizeof(VkPhysicalDevice) * total_count);
4880
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004881 res = inst->disp->layer_inst_disp.EnumeratePhysicalDevices(instance, &total_count, local_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004882 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004883 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4884 "setupLoaderTrampPhysDevs: Failed during dispatch call "
4885 "of \'vkEnumeratePhysicalDevices\' to lower layers or "
4886 "loader.");
Mark Young6267ae62017-01-12 12:27:19 -07004887 goto out;
4888 }
4889
4890 // Copy or create everything to fill the new array of physical devices
4891 for (uint32_t new_idx = 0; new_idx < total_count; new_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07004892 // Check if this physical device is already in the old buffer
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004893 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_tramp; old_idx++) {
4894 if (local_phys_devs[new_idx] == inst->phys_devs_tramp[old_idx]->phys_dev) {
Mark Young6267ae62017-01-12 12:27:19 -07004895 new_phys_devs[new_idx] = inst->phys_devs_tramp[old_idx];
4896 break;
4897 }
Mark Youngd8382d72016-12-23 16:59:58 -07004898 }
4899
Mark Young6267ae62017-01-12 12:27:19 -07004900 // If this physical device isn't in the old buffer, create it
4901 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004902 new_phys_devs[new_idx] = (struct loader_physical_device_tramp *)loader_instance_heap_alloc(
4903 inst, sizeof(struct loader_physical_device_tramp), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004904 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004905 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4906 "setupLoaderTrampPhysDevs: Failed to allocate "
4907 "physical device trampoline object %d",
Mark Young6267ae62017-01-12 12:27:19 -07004908 new_idx);
4909 total_count = new_idx;
Mark Youngd8382d72016-12-23 16:59:58 -07004910 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4911 goto out;
4912 }
4913
Mark Young6267ae62017-01-12 12:27:19 -07004914 // Initialize the new physicalDevice object
4915 loader_set_dispatch((void *)new_phys_devs[new_idx], inst->disp);
4916 new_phys_devs[new_idx]->this_instance = inst;
4917 new_phys_devs[new_idx]->phys_dev = local_phys_devs[new_idx];
Mark Young559d7502016-09-26 11:38:46 -06004918 }
Lenny Komowa5e01122016-12-22 15:29:43 -07004919 }
Mark Young559d7502016-09-26 11:38:46 -06004920
Lenny Komowa5e01122016-12-22 15:29:43 -07004921out:
Mark Youngd8382d72016-12-23 16:59:58 -07004922
Mark Young6267ae62017-01-12 12:27:19 -07004923 if (VK_SUCCESS != res) {
4924 if (NULL != new_phys_devs) {
4925 for (uint32_t i = 0; i < total_count; i++) {
4926 loader_instance_heap_free(inst, new_phys_devs[i]);
Lenny Komowa5e01122016-12-22 15:29:43 -07004927 }
4928 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004929 }
4930 total_count = 0;
4931 } else {
4932 // Free everything that didn't carry over to the new array of
4933 // physical devices
4934 if (NULL != inst->phys_devs_tramp) {
4935 for (uint32_t i = 0; i < inst->phys_dev_count_tramp; i++) {
4936 bool found = false;
4937 for (uint32_t j = 0; j < total_count; j++) {
4938 if (inst->phys_devs_tramp[i] == new_phys_devs[j]) {
4939 found = true;
4940 break;
4941 }
4942 }
4943 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004944 loader_instance_heap_free(inst, inst->phys_devs_tramp[i]);
Mark Young6267ae62017-01-12 12:27:19 -07004945 }
4946 }
4947 loader_instance_heap_free(inst, inst->phys_devs_tramp);
4948 }
Mark Youngd8382d72016-12-23 16:59:58 -07004949
Mark Young6267ae62017-01-12 12:27:19 -07004950 // Swap in the new physical device list
4951 inst->phys_dev_count_tramp = total_count;
4952 inst->phys_devs_tramp = new_phys_devs;
4953 }
4954
4955 return res;
4956}
4957
4958VkResult setupLoaderTermPhysDevs(struct loader_instance *inst) {
4959 VkResult res = VK_SUCCESS;
4960 struct loader_icd_term *icd_term;
4961 struct loader_phys_dev_per_icd *icd_phys_dev_array = NULL;
4962 struct loader_physical_device_term **new_phys_devs = NULL;
Mark Young6267ae62017-01-12 12:27:19 -07004963
4964 inst->total_gpu_count = 0;
4965
4966 // Allocate something to store the physical device characteristics
4967 // that we read from each ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004968 icd_phys_dev_array =
4969 (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 -07004970 if (NULL == icd_phys_dev_array) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004971 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4972 "setupLoaderTermPhysDevs: Failed to allocate temporary "
4973 "ICD Physical device info array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004974 inst->total_gpu_count);
4975 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4976 goto out;
4977 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004978 memset(icd_phys_dev_array, 0, sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
Mark Young6267ae62017-01-12 12:27:19 -07004979 icd_term = inst->icd_terms;
4980
4981 // For each ICD, query the number of physical devices, and then get an
4982 // internal value for those physical devices.
Karl Schultz47dd59d2017-01-20 13:19:20 -07004983 for (uint32_t icd_idx = 0; NULL != icd_term; icd_term = icd_term->next, icd_idx++) {
4984 res = icd_term->EnumeratePhysicalDevices(icd_term->instance, &icd_phys_dev_array[icd_idx].count, NULL);
Mark Young6267ae62017-01-12 12:27:19 -07004985 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004986 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4987 "setupLoaderTermPhysDevs: Call to "
4988 "ICD %d's \'vkEnumeratePhysicalDevices\' failed with"
4989 " error 0x%08x",
Karl Schultz47dd59d2017-01-20 13:19:20 -07004990 icd_idx, res);
Mark Young6267ae62017-01-12 12:27:19 -07004991 goto out;
4992 }
4993
Karl Schultz47dd59d2017-01-20 13:19:20 -07004994 icd_phys_dev_array[icd_idx].phys_devs =
4995 (VkPhysicalDevice *)loader_stack_alloc(icd_phys_dev_array[icd_idx].count * sizeof(VkPhysicalDevice));
4996 if (NULL == icd_phys_dev_array[icd_idx].phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004997 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4998 "setupLoaderTermPhysDevs: Failed to allocate temporary "
4999 "ICD Physical device array for ICD %d of size %d",
Karl Schultz47dd59d2017-01-20 13:19:20 -07005000 icd_idx, inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07005001 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5002 goto out;
5003 }
5004
Karl Schultz47dd59d2017-01-20 13:19:20 -07005005 res = icd_term->EnumeratePhysicalDevices(icd_term->instance, &(icd_phys_dev_array[icd_idx].count),
5006 icd_phys_dev_array[icd_idx].phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005007 if (VK_SUCCESS != res) {
5008 goto out;
5009 }
Karl Schultz47dd59d2017-01-20 13:19:20 -07005010 inst->total_gpu_count += icd_phys_dev_array[icd_idx].count;
5011 icd_phys_dev_array[icd_idx].this_icd_term = icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07005012 }
5013
5014 if (0 == inst->total_gpu_count) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005015 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5016 "setupLoaderTermPhysDevs: Failed to detect any valid"
5017 " GPUs in the current config");
Mark Young6267ae62017-01-12 12:27:19 -07005018 res = VK_ERROR_INITIALIZATION_FAILED;
5019 goto out;
5020 }
5021
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005022 new_phys_devs = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count,
5023 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005024 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005025 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5026 "setupLoaderTermPhysDevs: Failed to allocate new physical"
5027 " device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07005028 inst->total_gpu_count);
5029 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5030 goto out;
5031 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005032 memset(new_phys_devs, 0, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07005033
5034 // Copy or create everything to fill the new array of physical devices
5035 uint32_t idx = 0;
5036 for (uint32_t icd_idx = 0; icd_idx < inst->total_icd_count; icd_idx++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005037 for (uint32_t pd_idx = 0; pd_idx < icd_phys_dev_array[icd_idx].count; pd_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07005038 // Check if this physical device is already in the old buffer
5039 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005040 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_term; old_idx++) {
5041 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 -07005042 new_phys_devs[idx] = inst->phys_devs_term[old_idx];
5043 break;
5044 }
5045 }
5046 }
5047 // If this physical device isn't in the old buffer, then we
5048 // need to create it.
5049 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005050 new_phys_devs[idx] = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term),
5051 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07005052 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005053 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5054 "setupLoaderTermPhysDevs: Failed to allocate "
5055 "physical device terminator object %d",
Mark Young6267ae62017-01-12 12:27:19 -07005056 idx);
5057 inst->total_gpu_count = idx;
5058 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5059 goto out;
5060 }
5061
5062 loader_set_dispatch((void *)new_phys_devs[idx], inst->disp);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005063 new_phys_devs[idx]->this_icd_term = icd_phys_dev_array[icd_idx].this_icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07005064 new_phys_devs[idx]->icd_index = (uint8_t)(icd_idx);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005065 new_phys_devs[idx]->phys_dev = icd_phys_dev_array[icd_idx].phys_devs[pd_idx];
Mark Young6267ae62017-01-12 12:27:19 -07005066 }
5067 idx++;
5068 }
5069 }
5070
5071out:
5072
5073 if (VK_SUCCESS != res) {
Mark Young156e8552017-02-03 16:27:42 -07005074 if (NULL != new_phys_devs) {
5075 // We've encountered an error, so we should free the new buffers.
Mark Young6267ae62017-01-12 12:27:19 -07005076 for (uint32_t i = 0; i < inst->total_gpu_count; i++) {
5077 loader_instance_heap_free(inst, new_phys_devs[i]);
5078 }
Mark Young156e8552017-02-03 16:27:42 -07005079 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07005080 }
Mark Young156e8552017-02-03 16:27:42 -07005081 if (NULL != inst->phys_devs_term) {
5082 loader_instance_heap_free(inst, inst->phys_devs_term);
5083 }
5084 inst->total_gpu_count = 0;
Mark Young6267ae62017-01-12 12:27:19 -07005085 } else {
5086 // Free everything that didn't carry over to the new array of
5087 // physical devices. Everything else will have been copied over
5088 // to the new array.
5089 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005090 for (uint32_t cur_pd = 0; cur_pd < inst->phys_dev_count_term; cur_pd++) {
Mark Young6267ae62017-01-12 12:27:19 -07005091 bool found = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005092 for (uint32_t new_pd_idx = 0; new_pd_idx < inst->total_gpu_count; new_pd_idx++) {
5093 if (inst->phys_devs_term[cur_pd] == new_phys_devs[new_pd_idx]) {
Mark Young6267ae62017-01-12 12:27:19 -07005094 found = true;
5095 break;
5096 }
5097 }
5098 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005099 loader_instance_heap_free(inst, inst->phys_devs_term[cur_pd]);
Mark Young6267ae62017-01-12 12:27:19 -07005100 }
5101 }
5102 loader_instance_heap_free(inst, inst->phys_devs_term);
5103 }
5104
5105 // Swap out old and new devices list
5106 inst->phys_dev_count_term = inst->total_gpu_count;
5107 inst->phys_devs_term = new_phys_devs;
5108 }
5109
5110 return res;
5111}
5112
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005113VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
5114 VkPhysicalDevice *pPhysicalDevices) {
Mark Young6267ae62017-01-12 12:27:19 -07005115 struct loader_instance *inst = (struct loader_instance *)instance;
5116 VkResult res = VK_SUCCESS;
5117
5118 // Only do the setup if we're re-querying the number of devices, or
5119 // our count is currently 0.
5120 if (NULL == pPhysicalDevices || 0 == inst->total_gpu_count) {
5121 res = setupLoaderTermPhysDevs(inst);
5122 if (VK_SUCCESS != res) {
5123 goto out;
5124 }
5125 }
5126
5127 uint32_t copy_count = inst->total_gpu_count;
5128 if (NULL != pPhysicalDevices) {
5129 if (copy_count > *pPhysicalDeviceCount) {
5130 copy_count = *pPhysicalDeviceCount;
5131 res = VK_INCOMPLETE;
5132 }
5133
5134 for (uint32_t i = 0; i < copy_count; i++) {
5135 pPhysicalDevices[i] = (VkPhysicalDevice)inst->phys_devs_term[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07005136 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005137 }
Mark Young559d7502016-09-26 11:38:46 -06005138
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07005139 *pPhysicalDeviceCount = copy_count;
5140
Mark Young6267ae62017-01-12 12:27:19 -07005141out:
5142
Jon Ashburn24cd4be2015-11-01 14:04:06 -07005143 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005144}
5145
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005146VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
5147 VkPhysicalDeviceProperties *pProperties) {
5148 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005149 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005150 if (NULL != icd_term->GetPhysicalDeviceProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005151 icd_term->GetPhysicalDeviceProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005152 }
Tony Barbour59a47322015-06-24 16:06:58 -06005153}
5154
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005155VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
5156 uint32_t *pQueueFamilyPropertyCount,
5157 VkQueueFamilyProperties *pProperties) {
5158 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005159 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005160 if (NULL != icd_term->GetPhysicalDeviceQueueFamilyProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005161 icd_term->GetPhysicalDeviceQueueFamilyProperties(phys_dev_term->phys_dev, pQueueFamilyPropertyCount, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005162 }
Tony Barbour59a47322015-06-24 16:06:58 -06005163}
5164
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005165VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
5166 VkPhysicalDeviceMemoryProperties *pProperties) {
5167 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005168 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005169 if (NULL != icd_term->GetPhysicalDeviceMemoryProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005170 icd_term->GetPhysicalDeviceMemoryProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005171 }
Jon Ashburn3da71f22015-05-14 12:43:38 -06005172}
5173
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005174VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
5175 VkPhysicalDeviceFeatures *pFeatures) {
5176 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005177 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005178 if (NULL != icd_term->GetPhysicalDeviceFeatures) {
Mark Young0153e0b2016-11-03 14:27:13 -06005179 icd_term->GetPhysicalDeviceFeatures(phys_dev_term->phys_dev, pFeatures);
Mark Youngb6399312017-01-10 14:22:15 -07005180 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005181}
5182
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005183VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5184 VkFormatProperties *pFormatInfo) {
5185 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005186 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005187 if (NULL != icd_term->GetPhysicalDeviceFormatProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005188 icd_term->GetPhysicalDeviceFormatProperties(phys_dev_term->phys_dev, format, pFormatInfo);
Mark Youngb6399312017-01-10 14:22:15 -07005189 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005190}
5191
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005192VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5193 VkImageType type, VkImageTiling tiling,
5194 VkImageUsageFlags usage, VkImageCreateFlags flags,
5195 VkImageFormatProperties *pImageFormatProperties) {
5196 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005197 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005198 if (NULL == icd_term->GetPhysicalDeviceImageFormatProperties) {
5199 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5200 "Encountered the vkEnumerateDeviceLayerProperties "
5201 "terminator. This means a layer improperly continued.");
Chia-I Wu17241042015-10-31 00:31:16 +08005202 return VK_ERROR_INITIALIZATION_FAILED;
Mark Youngb6399312017-01-10 14:22:15 -07005203 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005204 return icd_term->GetPhysicalDeviceImageFormatProperties(phys_dev_term->phys_dev, format, type, tiling, usage, flags,
5205 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06005206}
5207
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005208VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5209 VkImageType type, VkSampleCountFlagBits samples,
5210 VkImageUsageFlags usage, VkImageTiling tiling,
5211 uint32_t *pNumProperties,
5212 VkSparseImageFormatProperties *pProperties) {
5213 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005214 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005215 if (NULL != icd_term->GetPhysicalDeviceSparseImageFormatProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005216 icd_term->GetPhysicalDeviceSparseImageFormatProperties(phys_dev_term->phys_dev, format, type, samples, usage, tiling,
5217 pNumProperties, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005218 }
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06005219}
5220
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005221VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
5222 const char *pLayerName, uint32_t *pPropertyCount,
5223 VkExtensionProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06005224 struct loader_physical_device_term *phys_dev_term;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07005225
Mark Young3a587792016-08-19 15:25:08 -06005226 struct loader_layer_list implicit_layer_list = {0};
5227 struct loader_extension_list all_exts = {0};
5228 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07005229
Jon Ashburndc5d9202016-02-29 13:00:51 -07005230 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005231
Jon Ashburndc5d9202016-02-29 13:00:51 -07005232 /* Any layer or trampoline wrapping should be removed at this point in time
5233 * can just cast to the expected type for VkPhysicalDevice. */
Mark Young0153e0b2016-11-03 14:27:13 -06005234 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005235
Jon Ashburndc5d9202016-02-29 13:00:51 -07005236 /* this case is during the call down the instance chain with pLayerName
5237 * == NULL*/
Mark Young0153e0b2016-11-03 14:27:13 -06005238 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005239 uint32_t icd_ext_count = *pPropertyCount;
5240 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005241
Jon Ashburndc5d9202016-02-29 13:00:51 -07005242 /* get device extensions */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005243 res = icd_term->EnumerateDeviceExtensionProperties(phys_dev_term->phys_dev, NULL, &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06005244 if (res != VK_SUCCESS) {
5245 goto out;
5246 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07005247
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005248 if (!loader_init_layer_list(icd_term->this_instance, &implicit_layer_list)) {
Mark Young3a587792016-08-19 15:25:08 -06005249 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5250 goto out;
5251 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005252
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005253 loader_add_layer_implicit(icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &implicit_layer_list,
5254 &icd_term->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07005255 /* we need to determine which implicit layers are active,
5256 * and then add their extensions. This can't be cached as
5257 * it depends on results of environment variables (which can change).
5258 */
5259 if (pProperties != NULL) {
5260 /* initialize dev_extension list within the physicalDevice object */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005261 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 -06005262 if (res != VK_SUCCESS) {
5263 goto out;
5264 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005265
Jon Ashburndc5d9202016-02-29 13:00:51 -07005266 /* we need to determine which implicit layers are active,
5267 * and then add their extensions. This can't be cached as
5268 * it depends on results of environment variables (which can
5269 * change).
5270 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005271 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 -06005272 if (res != VK_SUCCESS) {
5273 goto out;
5274 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005275
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005276 loader_add_layer_implicit(icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &implicit_layer_list,
5277 &icd_term->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07005278
Jon Ashburndc5d9202016-02-29 13:00:51 -07005279 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005280 for (uint32_t j = 0; j < implicit_layer_list.list[i].device_extension_list.count; j++) {
5281 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts, 1,
5282 &implicit_layer_list.list[i].device_extension_list.list[j].props);
Mark Young3a587792016-08-19 15:25:08 -06005283 if (res != VK_SUCCESS) {
5284 goto out;
5285 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005286 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005287 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005288 uint32_t capacity = *pPropertyCount;
5289 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005290
Jon Ashburndc5d9202016-02-29 13:00:51 -07005291 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
5292 props[i] = all_exts.list[i];
5293 }
5294 /* wasn't enough space for the extensions, we did partial copy now
5295 * return VK_INCOMPLETE */
5296 if (capacity < all_exts.count) {
5297 res = VK_INCOMPLETE;
5298 } else {
5299 *pPropertyCount = all_exts.count;
5300 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005301 } else {
5302 /* just return the count; need to add in the count of implicit layer
5303 * extensions
5304 * don't worry about duplicates being added in the count */
5305 *pPropertyCount = icd_ext_count;
5306
5307 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005308 *pPropertyCount += implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005309 }
5310 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06005311 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005312
Mark Young3a587792016-08-19 15:25:08 -06005313out:
5314
5315 if (NULL != implicit_layer_list.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005316 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Mark Young3a587792016-08-19 15:25:08 -06005317 }
5318 if (NULL != all_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005319 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&all_exts);
Mark Young3a587792016-08-19 15:25:08 -06005320 }
5321 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005322 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06005323 }
5324
Jon Ashburndc5d9202016-02-29 13:00:51 -07005325 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005326}
5327
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005328VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
5329 VkLayerProperties *pProperties) {
5330 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Youngb6399312017-01-10 14:22:15 -07005331 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005332 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5333 "Encountered the vkEnumerateDeviceLayerProperties "
5334 "terminator. This means a layer improperly continued.");
Mark Youngb6399312017-01-10 14:22:15 -07005335 // Should never get here this call isn't dispatched down the chain
Jon Ashburndc5d9202016-02-29 13:00:51 -07005336 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06005337}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005338
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005339VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005340 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07005341 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005342 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005343
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005344 for (i = 0; i <= max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005345 if (utf8[i] == 0) {
5346 break;
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005347 } else if (i == max_length) {
5348 result |= VK_STRING_ERROR_LENGTH;
5349 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07005350 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005351 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005352 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005353 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005354 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005355 num_char_bytes = 2;
5356 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
5357 num_char_bytes = 3;
5358 } else {
5359 result = VK_STRING_ERROR_BAD_DATA;
5360 }
5361
5362 // Validate the following num_char_bytes of data
5363 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
5364 if (++i == max_length) {
5365 result |= VK_STRING_ERROR_LENGTH;
5366 break;
5367 }
5368 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
5369 result |= VK_STRING_ERROR_BAD_DATA;
5370 }
5371 }
5372 }
5373 return result;
5374}