blob: 9c43525149c5ab40073f297d78fac928f720ff04 [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>
33
Chia-I Wu13a61a52014-08-04 11:18:20 +080034#include <sys/types.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050035#if defined(_WIN32)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070036#include "dirent_on_windows.h"
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070037#else // _WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080038#include <dirent.h>
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070039#endif // _WIN32
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060040#include "vk_loader_platform.h"
Chia-I Wu19300602014-08-04 08:03:57 +080041#include "loader.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060042#include "gpa_helper.h"
43#include "table_ops.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060044#include "debug_report.h"
Ian Elliott954fa342015-10-30 15:28:23 -060045#include "wsi.h"
Mark Lobodzinski317574e2016-08-29 14:21:14 -060046#include "extensions.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070047#include "vulkan/vk_icd.h"
Jon Ashburn2077e382015-06-29 11:25:34 -060048#include "cJSON.h"
Jon Ashburnfc1031e2015-11-17 15:31:02 -070049#include "murmurhash.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080050
Jon Ashburnd8ed7992016-04-04 13:52:53 -060051#if defined(__GNUC__)
Jon Ashburn10b3f832016-05-09 11:31:40 -060052#if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 17))
Jon Ashburncc407a22016-04-15 09:25:03 -060053#define secure_getenv __secure_getenv
54#endif
Jon Ashburnd8ed7992016-04-04 13:52:53 -060055#endif
56
Jon Ashburn27cd5842015-05-12 17:26:48 -060057struct loader_struct loader = {0};
Jon Ashburn87d6aa92015-08-28 15:19:27 -060058// TLS for instance for alloc/free callbacks
59THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080060
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -070061static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap00b4aba2015-09-30 11:50:51 -050062
Jon Ashburn24cd4be2015-11-01 14:04:06 -070063struct loader_phys_dev_per_icd {
64 uint32_t count;
65 VkPhysicalDevice *phys_devs;
Mark Young0153e0b2016-11-03 14:27:13 -060066 struct loader_icd_term *this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -070067};
68
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060069enum loader_debug {
Jon Ashburn23d36b12016-02-02 17:47:28 -070070 LOADER_INFO_BIT = 0x01,
71 LOADER_WARN_BIT = 0x02,
72 LOADER_PERF_BIT = 0x04,
73 LOADER_ERROR_BIT = 0x08,
74 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060075};
76
77uint32_t g_loader_debug = 0;
78uint32_t g_loader_log_msgs = 0;
79
Jon Ashburn23d36b12016-02-02 17:47:28 -070080// thread safety lock for accessing global data structures such as "loader"
Jon Ashburn6301a0f2015-05-29 13:15:39 -060081// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburn2077e382015-06-29 11:25:34 -060082// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburn6301a0f2015-05-29 13:15:39 -060083loader_platform_thread_mutex loader_lock;
Jon Ashburn6461ef22015-09-22 13:11:00 -060084loader_platform_thread_mutex loader_json_lock;
Jon Ashburn6301a0f2015-05-29 13:15:39 -060085
Jon Ashburn86a527a2016-02-10 20:59:26 -070086const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
87
Ian Elliottd3ef02f2015-07-06 14:36:13 -060088// This table contains the loader's instance dispatch table, which contains
89// default functions if no instance layers are activated. This contains
90// pointers to "terminator functions".
Jon Ashburn6301a0f2015-05-29 13:15:39 -060091const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn69e9ea22015-09-28 16:15:00 -060092 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn1530c342016-02-26 13:14:27 -070093 .DestroyInstance = terminator_DestroyInstance,
94 .EnumeratePhysicalDevices = terminator_EnumeratePhysicalDevices,
95 .GetPhysicalDeviceFeatures = terminator_GetPhysicalDeviceFeatures,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070096 .GetPhysicalDeviceFormatProperties = terminator_GetPhysicalDeviceFormatProperties,
97 .GetPhysicalDeviceImageFormatProperties = terminator_GetPhysicalDeviceImageFormatProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -070098 .GetPhysicalDeviceProperties = terminator_GetPhysicalDeviceProperties,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070099 .GetPhysicalDeviceQueueFamilyProperties = terminator_GetPhysicalDeviceQueueFamilyProperties,
100 .GetPhysicalDeviceMemoryProperties = terminator_GetPhysicalDeviceMemoryProperties,
101 .EnumerateDeviceExtensionProperties = terminator_EnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700102 .EnumerateDeviceLayerProperties = terminator_EnumerateDeviceLayerProperties,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700103 .GetPhysicalDeviceSparseImageFormatProperties = terminator_GetPhysicalDeviceSparseImageFormatProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700104 .DestroySurfaceKHR = terminator_DestroySurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700105 .GetPhysicalDeviceSurfaceSupportKHR = terminator_GetPhysicalDeviceSurfaceSupportKHR,
106 .GetPhysicalDeviceSurfaceCapabilitiesKHR = terminator_GetPhysicalDeviceSurfaceCapabilitiesKHR,
107 .GetPhysicalDeviceSurfaceFormatsKHR = terminator_GetPhysicalDeviceSurfaceFormatsKHR,
108 .GetPhysicalDeviceSurfacePresentModesKHR = terminator_GetPhysicalDeviceSurfacePresentModesKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700109#ifdef VK_USE_PLATFORM_MIR_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700110 .CreateMirSurfaceKHR = terminator_CreateMirSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700111 .GetPhysicalDeviceMirPresentationSupportKHR = terminator_GetPhysicalDeviceMirPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700112#endif
113#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700114 .CreateWaylandSurfaceKHR = terminator_CreateWaylandSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700115 .GetPhysicalDeviceWaylandPresentationSupportKHR = terminator_GetPhysicalDeviceWaylandPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700116#endif
117#ifdef VK_USE_PLATFORM_WIN32_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700118 .CreateWin32SurfaceKHR = terminator_CreateWin32SurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700119 .GetPhysicalDeviceWin32PresentationSupportKHR = terminator_GetPhysicalDeviceWin32PresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700120#endif
121#ifdef VK_USE_PLATFORM_XCB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700122 .CreateXcbSurfaceKHR = terminator_CreateXcbSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700123 .GetPhysicalDeviceXcbPresentationSupportKHR = terminator_GetPhysicalDeviceXcbPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700124#endif
125#ifdef VK_USE_PLATFORM_XLIB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700126 .CreateXlibSurfaceKHR = terminator_CreateXlibSurfaceKHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700127 .GetPhysicalDeviceXlibPresentationSupportKHR = terminator_GetPhysicalDeviceXlibPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700128#endif
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700129#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700130 .CreateAndroidSurfaceKHR = terminator_CreateAndroidSurfaceKHR,
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700131#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700132 .GetPhysicalDeviceDisplayPropertiesKHR = terminator_GetPhysicalDeviceDisplayPropertiesKHR,
133 .GetPhysicalDeviceDisplayPlanePropertiesKHR = terminator_GetPhysicalDeviceDisplayPlanePropertiesKHR,
134 .GetDisplayPlaneSupportedDisplaysKHR = terminator_GetDisplayPlaneSupportedDisplaysKHR,
Jon Ashburncc407a22016-04-15 09:25:03 -0600135 .GetDisplayModePropertiesKHR = terminator_GetDisplayModePropertiesKHR,
136 .CreateDisplayModeKHR = terminator_CreateDisplayModeKHR,
137 .GetDisplayPlaneCapabilitiesKHR = terminator_GetDisplayPlaneCapabilitiesKHR,
138 .CreateDisplayPlaneSurfaceKHR = terminator_CreateDisplayPlaneSurfaceKHR,
Mark Youngfa552782016-12-12 16:14:55 -0700139
Mark Young39389872017-01-19 21:10:49 -0700140 // KHR_get_physical_device_properties2
141 .GetPhysicalDeviceFeatures2KHR = terminator_GetPhysicalDeviceFeatures2KHR,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700142 .GetPhysicalDeviceProperties2KHR = terminator_GetPhysicalDeviceProperties2KHR,
143 .GetPhysicalDeviceFormatProperties2KHR = terminator_GetPhysicalDeviceFormatProperties2KHR,
144 .GetPhysicalDeviceImageFormatProperties2KHR = terminator_GetPhysicalDeviceImageFormatProperties2KHR,
145 .GetPhysicalDeviceQueueFamilyProperties2KHR = terminator_GetPhysicalDeviceQueueFamilyProperties2KHR,
146 .GetPhysicalDeviceMemoryProperties2KHR = terminator_GetPhysicalDeviceMemoryProperties2KHR,
147 .GetPhysicalDeviceSparseImageFormatProperties2KHR = terminator_GetPhysicalDeviceSparseImageFormatProperties2KHR,
Mark Young39389872017-01-19 21:10:49 -0700148
149#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
150 // EXT_acquire_xlib_display
151 .AcquireXlibDisplayEXT = terminator_AcquireXlibDisplayEXT,
152 .GetRandROutputDisplayEXT = terminator_GetRandROutputDisplayEXT,
153#endif
154
155 // EXT_debug_report
156 .CreateDebugReportCallbackEXT = terminator_CreateDebugReportCallback,
157 .DestroyDebugReportCallbackEXT = terminator_DestroyDebugReportCallback,
158 .DebugReportMessageEXT = terminator_DebugReportMessage,
159
160 // EXT_direct_mode_display
161 .ReleaseDisplayEXT = terminator_ReleaseDisplayEXT,
162
163 // EXT_display_surface_counter
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700164 .GetPhysicalDeviceSurfaceCapabilities2EXT = terminator_GetPhysicalDeviceSurfaceCapabilities2EXT,
Mark Young39389872017-01-19 21:10:49 -0700165
166 // NV_external_memory_capabilities
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700167 .GetPhysicalDeviceExternalImageFormatPropertiesNV = terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV,
Mark Young39389872017-01-19 21:10:49 -0700168
Mark Youngfa552782016-12-12 16:14:55 -0700169 // NVX_device_generated_commands
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700170 .GetPhysicalDeviceGeneratedCommandsPropertiesNVX = terminator_GetPhysicalDeviceGeneratedCommandsPropertiesNVX,
Jon Ashburn27cd5842015-05-12 17:26:48 -0600171};
172
Lenny Komow4053b812016-12-29 16:27:28 -0700173// A null-terminated list of all of the instance extensions supported by the
174// loader
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700175static const char *const LOADER_INSTANCE_EXTENSIONS[] = {VK_KHR_SURFACE_EXTENSION_NAME,
176 VK_KHR_DISPLAY_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700177#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700178 VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700179#endif
180#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700181 VK_KHR_XCB_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700182#endif
183#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700184 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700185#endif
186#ifdef VK_USE_PLATFORM_MIR_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700187 VK_KHR_MIR_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700188#endif
189#ifdef VK_USE_PLATFORM_ANDROID_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700190 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700191#endif
192#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700193 VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
Lenny Komow4053b812016-12-29 16:27:28 -0700194#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700195 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700196#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700197 VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700198#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700199 VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
200 VK_EXT_DIRECT_MODE_DISPLAY_EXTENSION_NAME,
201 VK_EXT_DISPLAY_SURFACE_COUNTER_EXTENSION_NAME,
202 VK_EXT_VALIDATION_FLAGS_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700203#ifdef VK_USE_PLATFORM_VI_NN
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700204 VK_NN_VI_SURFACE_EXTENSION_NAME,
Mark Young39389872017-01-19 21:10:49 -0700205#endif
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700206 VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
207 NULL};
Lenny Komow4053b812016-12-29 16:27:28 -0700208
Jon Ashburn8810c5f2015-08-18 18:04:47 -0600209LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700210
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700211void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope alloc_scope) {
Mark Young0ad83132016-06-30 13:02:42 -0600212 void *pMemory = NULL;
213#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
214 {
215#else
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800216 if (instance && instance->alloc_callbacks.pfnAllocation) {
Mark Young0ad83132016-06-30 13:02:42 -0600217 /* These are internal structures, so it's best to align everything to
218 * the largest unit size which is the size of a uint64_t.
219 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700220 pMemory = instance->alloc_callbacks.pfnAllocation(instance->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600221 } else {
222#endif
223 pMemory = malloc(size);
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600224 }
Mark Young0ad83132016-06-30 13:02:42 -0600225 return pMemory;
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600226}
227
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700228void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory) {
Mark Young0ad83132016-06-30 13:02:42 -0600229 if (pMemory != NULL) {
230#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
231 {
232#else
233 if (instance && instance->alloc_callbacks.pfnFree) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700234 instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData, pMemory);
Mark Young0ad83132016-06-30 13:02:42 -0600235 } else {
236#endif
237 free(pMemory);
Mark Youngd077f992016-06-30 11:03:59 -0600238 }
Mark Young4b0b9222016-06-29 18:33:53 -0600239 }
Mark Young4b0b9222016-06-29 18:33:53 -0600240}
241
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700242void *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 -0600243 VkSystemAllocationScope alloc_scope) {
244 void *pNewMem = NULL;
245 if (pMemory == NULL || orig_size == 0) {
246 pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
247 } else if (size == 0) {
248 loader_instance_heap_free(instance, pMemory);
249#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
250#else
251 } else if (instance && instance->alloc_callbacks.pfnReallocation) {
252 /* These are internal structures, so it's best to align everything to
253 * the largest unit size which is the size of a uint64_t.
254 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700255 pNewMem = instance->alloc_callbacks.pfnReallocation(instance->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
256 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600257#endif
258 } else {
259 pNewMem = realloc(pMemory, size);
260 }
261 return pNewMem;
Mark Young4b0b9222016-06-29 18:33:53 -0600262}
263
Mark Young0ad83132016-06-30 13:02:42 -0600264void *loader_instance_tls_heap_alloc(size_t size) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700265 return loader_instance_heap_alloc(tls_instance, size, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young4b0b9222016-06-29 18:33:53 -0600266}
Mark Young4b0b9222016-06-29 18:33:53 -0600267
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700268void loader_instance_tls_heap_free(void *pMemory) { loader_instance_heap_free(tls_instance, pMemory); }
Mark Young0ad83132016-06-30 13:02:42 -0600269
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700270void *loader_device_heap_alloc(const struct loader_device *device, size_t size, VkSystemAllocationScope alloc_scope) {
Mark Young0ad83132016-06-30 13:02:42 -0600271 void *pMemory = NULL;
272#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
273 {
274#else
275 if (device && device->alloc_callbacks.pfnAllocation) {
276 /* These are internal structures, so it's best to align everything to
277 * the largest unit size which is the size of a uint64_t.
278 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700279 pMemory = device->alloc_callbacks.pfnAllocation(device->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600280 } else {
281#endif
282 pMemory = malloc(size);
283 }
284 return pMemory;
285}
286
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700287void loader_device_heap_free(const struct loader_device *device, void *pMemory) {
Mark Young0ad83132016-06-30 13:02:42 -0600288 if (pMemory != NULL) {
289#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
290 {
291#else
292 if (device && device->alloc_callbacks.pfnFree) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700293 device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData, pMemory);
Mark Young0ad83132016-06-30 13:02:42 -0600294 } else {
295#endif
296 free(pMemory);
297 }
298 }
299}
300
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700301void *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 -0600302 VkSystemAllocationScope alloc_scope) {
303 void *pNewMem = NULL;
304 if (pMemory == NULL || orig_size == 0) {
305 pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
306 } else if (size == 0) {
307 loader_device_heap_free(device, pMemory);
308#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
309#else
310 } else if (device && device->alloc_callbacks.pfnReallocation) {
311 /* These are internal structures, so it's best to align everything to
312 * the largest unit size which is the size of a uint64_t.
313 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700314 pNewMem = device->alloc_callbacks.pfnReallocation(device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
315 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600316#endif
317 } else {
318 pNewMem = realloc(pMemory, size);
319 }
320 return pNewMem;
321}
322
323// Environment variables
324#if defined(__linux__)
325
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700326static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600327 // No allocation of memory necessary for Linux, but we should at least touch
328 // the inst pointer to get rid of compiler warnings.
329 (void)inst;
330 return getenv(name);
331}
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700332static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600333 // No freeing of memory necessary for Linux, but we should at least touch
334 // the val and inst pointers to get rid of compiler warnings.
335 (void)val;
336 (void)inst;
337}
338
339#elif defined(WIN32)
340
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700341static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600342 char *retVal;
343 DWORD valSize;
344
345 valSize = GetEnvironmentVariableA(name, NULL, 0);
346
347 // valSize DOES include the null terminator, so for any set variable
348 // will always be at least 1. If it's 0, the variable wasn't set.
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700349 if (valSize == 0) return NULL;
Mark Young0ad83132016-06-30 13:02:42 -0600350
351 // Allocate the space necessary for the registry entry
352 if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700353 retVal = (char *)inst->alloc_callbacks.pfnAllocation(inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
354 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -0600355 } else {
356 retVal = (char *)malloc(valSize);
357 }
358
359 if (NULL != retVal) {
360 GetEnvironmentVariableA(name, retVal, valSize);
361 }
362
363 return retVal;
364}
365
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700366static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600367 if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
368 inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
369 } else {
370 free((void *)val);
371 }
372}
373
374#else
375
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700376static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600377 // stub func
378 (void)inst;
379 (void)name;
380 return NULL;
381}
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700382static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600383 // stub func
384 (void)val;
385 (void)inst;
386}
387
388#endif
389
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700390void loader_log(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code, const char *format, ...) {
Jon Ashburn86723b02015-07-31 15:47:59 -0600391 char msg[512];
Mark Young2c84c0c2017-01-13 10:27:03 -0700392 char cmd_line_msg[512];
393 uint16_t cmd_line_size = sizeof(cmd_line_msg);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700394 va_list ap;
395 int ret;
396
Jon Ashburnffad94d2015-06-30 14:46:22 -0700397 va_start(ap, format);
398 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700399 if ((ret >= (int)sizeof(msg)) || ret < 0) {
400 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnffad94d2015-06-30 14:46:22 -0700401 }
402 va_end(ap);
403
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700404 if (inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700405 util_DebugReportMessage(inst, msg_type, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, (uint64_t)(uintptr_t)inst, 0, msg_code,
406 "loader", msg);
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700407 }
408
409 if (!(msg_type & g_loader_log_msgs)) {
410 return;
411 }
412
Mark Young2c84c0c2017-01-13 10:27:03 -0700413 cmd_line_msg[0] = '\0';
414
415 va_start(ap, format);
416 if ((msg_type & LOADER_INFO_BIT) != 0) {
417 strcat(cmd_line_msg, "INFO");
418 cmd_line_size -= 4;
419 }
420 if ((msg_type & LOADER_WARN_BIT) != 0) {
421 if (cmd_line_size != sizeof(cmd_line_msg)) {
422 strcat(cmd_line_msg, " | ");
423 cmd_line_size -= 3;
424 }
425 strcat(cmd_line_msg, "WARNING");
426 cmd_line_size -= 7;
427 }
428 if ((msg_type & LOADER_PERF_BIT) != 0) {
429 if (cmd_line_size != sizeof(cmd_line_msg)) {
430 strcat(cmd_line_msg, " | ");
431 cmd_line_size -= 3;
432 }
433 strcat(cmd_line_msg, "PERF");
434 cmd_line_size -= 4;
435 }
436 if ((msg_type & LOADER_ERROR_BIT) != 0) {
437 if (cmd_line_size != sizeof(cmd_line_msg)) {
438 strcat(cmd_line_msg, " | ");
439 cmd_line_size -= 3;
440 }
441 strcat(cmd_line_msg, "ERROR");
442 cmd_line_size -= 5;
443 }
444 if ((msg_type & LOADER_DEBUG_BIT) != 0) {
445 if (cmd_line_size != sizeof(cmd_line_msg)) {
446 strcat(cmd_line_msg, " | ");
447 cmd_line_size -= 3;
448 }
449 strcat(cmd_line_msg, "DEBUG");
450 cmd_line_size -= 5;
451 }
452 if (cmd_line_size != sizeof(cmd_line_msg)) {
453 strcat(cmd_line_msg, ": ");
454 cmd_line_size -= 2;
455 }
456 strncat(cmd_line_msg, msg, cmd_line_size);
457
Ian Elliott4470a302015-02-17 10:33:47 -0700458#if defined(WIN32)
Mark Young2c84c0c2017-01-13 10:27:03 -0700459 OutputDebugString(cmd_line_msg);
mschottb9cdb782015-07-22 14:11:29 +0200460 OutputDebugString("\n");
Jon Ashburnffad94d2015-06-30 14:46:22 -0700461#endif
Mark Young2c84c0c2017-01-13 10:27:03 -0700462 fputs(cmd_line_msg, stderr);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700463 fputc('\n', stderr);
464}
465
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700466VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceDispatch(VkInstance instance, void *object) {
Jon Ashburnc3c58772016-03-29 11:16:01 -0600467 struct loader_instance *inst = loader_get_instance(instance);
468 if (!inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700469 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
470 "vkSetInstanceDispatch: Can not retrieve Instance "
471 "dispatch table.");
Jon Ashburnc3c58772016-03-29 11:16:01 -0600472 return VK_ERROR_INITIALIZATION_FAILED;
473 }
474 loader_set_dispatch(object, inst->disp);
475 return VK_SUCCESS;
476}
477
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700478VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600479 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700480 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Jon Ashburned8f2312016-03-31 10:52:22 -0600481
Mark Young0153e0b2016-11-03 14:27:13 -0600482 if (NULL == icd_term) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600483 return VK_ERROR_INITIALIZATION_FAILED;
484 }
485 loader_set_dispatch(object, &dev->loader_dispatch);
486 return VK_SUCCESS;
487}
488
Jon Ashburnffad94d2015-06-30 14:46:22 -0700489#if defined(WIN32)
Tony Barbourea968902015-07-29 14:26:21 -0600490static char *loader_get_next_path(char *path);
Mark Young2c84c0c2017-01-13 10:27:03 -0700491
492// Find the list of registry files (names within a key) in key "location".
493//
494// This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
495// given in "location"
496// for a list or name/values which are added to a returned list (function return
497// value).
498// The DWORD values within the key must be 0 or they are skipped.
499// Function return is a string with a ';' separated list of filenames.
500// Function return is NULL if no valid name/value pairs are found in the key,
501// or the key is not found.
502//
503// *reg_data contains a string list of filenames as pointer.
504// When done using the returned string list, the caller should free the pointer.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700505VkResult loaderGetRegistryFiles(const struct loader_instance *inst, char *location, char **reg_data) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700506 LONG rtn_value;
507 HKEY hive, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700508 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700509 char name[2048];
Tony Barbourea968902015-07-29 14:26:21 -0600510 char *loc = location;
511 char *next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700512 DWORD idx = 0;
513 DWORD name_size = sizeof(name);
514 DWORD value;
515 DWORD total_size = 4096;
516 DWORD value_size = sizeof(value);
Mark Young2c84c0c2017-01-13 10:27:03 -0700517 VkResult result = VK_SUCCESS;
518 bool found = false;
519
520 if (NULL == reg_data) {
521 result = VK_ERROR_INITIALIZATION_FAILED;
522 goto out;
523 }
Tony Barbourea968902015-07-29 14:26:21 -0600524
Jon Ashburn23d36b12016-02-02 17:47:28 -0700525 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600526 next = loader_get_next_path(loc);
527 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniell524ec732015-11-05 16:58:26 -0700528 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600529 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
Mark Young2c84c0c2017-01-13 10:27:03 -0700530 if (ERROR_SUCCESS != rtn_value) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700531 // We still couldn't find the key, so give up:
532 loc = next;
533 continue;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700534 }
Tony Barbourea968902015-07-29 14:26:21 -0600535
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700536 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL, NULL, (LPBYTE)&value, &value_size)) == ERROR_SUCCESS) {
Tony Barbourea968902015-07-29 14:26:21 -0600537 if (value_size == sizeof(value) && value == 0) {
Mark Young2c84c0c2017-01-13 10:27:03 -0700538 if (NULL == *reg_data) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700539 *reg_data = loader_instance_heap_alloc(inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young2c84c0c2017-01-13 10:27:03 -0700540 if (NULL == *reg_data) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700541 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
542 "loaderGetRegistryFiles: Failed to allocate "
543 "space for registry data for key %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700544 name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700545 result = VK_ERROR_OUT_OF_HOST_MEMORY;
546 goto out;
Mark Young0ad83132016-06-30 13:02:42 -0600547 }
Mark Young2c84c0c2017-01-13 10:27:03 -0700548 *reg_data[0] = '\0';
549 } else if (strlen(*reg_data) + name_size + 1 > total_size) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700550 *reg_data = loader_instance_heap_realloc(inst, *reg_data, total_size, total_size * 2,
551 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young2c84c0c2017-01-13 10:27:03 -0700552 if (NULL == *reg_data) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700553 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
554 "loaderGetRegistryFiles: Failed to reallocate "
555 "space for registry value of size %d for key %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700556 total_size * 2, name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700557 result = VK_ERROR_OUT_OF_HOST_MEMORY;
558 goto out;
Mark Young0ad83132016-06-30 13:02:42 -0600559 }
Tony Barbourea968902015-07-29 14:26:21 -0600560 total_size *= 2;
561 }
Mark Young2c84c0c2017-01-13 10:27:03 -0700562 if (strlen(*reg_data) == 0) {
Karl Schultze2ef9e62017-01-13 14:01:35 -0700563 (void)snprintf(*reg_data, name_size + 1, "%s", name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700564 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700565 (void)snprintf(*reg_data + strlen(*reg_data), name_size + 2, "%c%s", PATH_SEPARATOR, name);
Mark Young2c84c0c2017-01-13 10:27:03 -0700566 }
567 found = true;
Tony Barbourea968902015-07-29 14:26:21 -0600568 }
569 name_size = 2048;
570 }
571 loc = next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700572 }
Tony Barbourea968902015-07-29 14:26:21 -0600573
Mark Young2c84c0c2017-01-13 10:27:03 -0700574 if (!found) {
575 result = VK_ERROR_INITIALIZATION_FAILED;
576 }
577
578out:
579
580 return result;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700581}
582
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700583#endif // WIN32
Ian Elliott4470a302015-02-17 10:33:47 -0700584
Jon Ashburnc7237a72015-08-03 09:08:46 -0600585/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500586 * Combine path elements, separating each element with the platform-specific
587 * directory separator, and save the combined string to a destination buffer,
588 * not exceeding the given length. Path elements are given as variadic args,
589 * with a NULL element terminating the list.
590 *
591 * \returns the total length of the combined string, not including an ASCII
592 * NUL termination character. This length may exceed the available storage:
593 * in this case, the written string will be truncated to avoid a buffer
594 * overrun, and the return value will greater than or equal to the storage
595 * size. A NULL argument may be provided as the destination buffer in order
596 * to determine the required string length without actually writing a string.
597 */
598
Jon Ashburn23d36b12016-02-02 17:47:28 -0700599static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700600 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500601 va_list ap;
602 const char *component;
603
604 va_start(ap, len);
605
Jon Ashburn23d36b12016-02-02 17:47:28 -0700606 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500607 if (required_len > 0) {
608 // This path element is not the first non-empty element; prepend
609 // a directory separator if space allows
610 if (dest && required_len + 1 < len) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700611 (void)snprintf(dest + required_len, len - required_len, "%c", DIRECTORY_SYMBOL);
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500612 }
613 required_len++;
614 }
615
616 if (dest && required_len < len) {
617 strncpy(dest + required_len, component, len - required_len);
618 }
619 required_len += strlen(component);
620 }
621
622 va_end(ap);
623
624 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
625 if (dest && required_len >= len) {
626 dest[len - 1] = '\0';
627 }
628
629 return required_len;
630}
631
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500632/**
Jon Ashburnc7237a72015-08-03 09:08:46 -0600633 * Given string of three part form "maj.min.pat" convert to a vulkan version
634 * number.
635 */
Mark Young60861ac2016-09-02 11:39:26 -0600636static uint32_t loader_make_version(char *vers_str) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700637 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
Mark Young60861ac2016-09-02 11:39:26 -0600638 char *vers_tok;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600639
Mark Young60861ac2016-09-02 11:39:26 -0600640 if (!vers_str) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600641 return vers;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600642 }
Mark Young60861ac2016-09-02 11:39:26 -0600643
644 vers_tok = strtok(vers_str, ".\"\n\r");
645 if (NULL != vers_tok) {
646 major = (uint16_t)atoi(vers_tok);
647 vers_tok = strtok(NULL, ".\"\n\r");
648 if (NULL != vers_tok) {
649 minor = (uint16_t)atoi(vers_tok);
650 vers_tok = strtok(NULL, ".\"\n\r");
651 if (NULL != vers_tok) {
652 patch = (uint16_t)atoi(vers_tok);
653 }
654 }
655 }
Jon Ashburnc7237a72015-08-03 09:08:46 -0600656
657 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600658}
659
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700660bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800661 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600662}
663
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600664/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600665 * Search the given ext_array for an extension
666 * matching the given vk_ext_prop
667 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700668bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop, const uint32_t count,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700669 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600670 for (uint32_t i = 0; i < count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700671 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i])) return true;
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600672 }
673 return false;
674}
675
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600676/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600677 * Search the given ext_list for an extension
678 * matching the given vk_ext_prop
679 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700680bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop, const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600681 for (uint32_t i = 0; i < ext_list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700682 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop)) return true;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600683 }
684 return false;
685}
686
Jon Ashburnb8726962016-04-08 15:03:35 -0600687/**
688 * Search the given ext_list for a device extension matching the given ext_prop
689 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700690bool has_vk_dev_ext_property(const VkExtensionProperties *ext_prop, const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600691 for (uint32_t i = 0; i < ext_list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700692 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop)) return true;
Jon Ashburnb8726962016-04-08 15:03:35 -0600693 }
694 return false;
695}
696
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600697/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600698 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600699 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700700static 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 -0600701 for (uint32_t i = 0; i < layer_list->count; i++) {
702 const VkLayerProperties *item = &layer_list->list[i].info;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700703 if (strcmp(name, item->layerName) == 0) return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600704 }
705 return NULL;
706}
707
Jon Ashburne13ecc92015-08-03 17:19:30 -0600708/**
709 * Get the next unused layer property in the list. Init the property to zero.
710 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700711static struct loader_layer_properties *loader_get_next_layer_property(const struct loader_instance *inst,
712 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600713 if (layer_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700714 layer_list->list =
715 loader_instance_heap_alloc(inst, sizeof(struct loader_layer_properties) * 64, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600716 if (layer_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700717 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
718 "loader_get_next_layer_property: Out of memory can "
719 "not add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600720 return NULL;
721 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700722 memset(layer_list->list, 0, sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600723 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
724 }
725
726 // ensure enough room to add an entry
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700727 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) > layer_list->capacity) {
728 layer_list->list = loader_instance_heap_realloc(inst, layer_list->list, layer_list->capacity, layer_list->capacity * 2,
729 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600730 if (layer_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700731 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
732 "loader_get_next_layer_property: realloc failed for "
733 "layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600734 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600735 }
736 layer_list->capacity *= 2;
737 }
738
739 layer_list->count++;
740 return &(layer_list->list[layer_list->count - 1]);
741}
742
743/**
744 * Remove all layer properties entrys from the list
745 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700746void loader_delete_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700747 uint32_t i, j;
748 struct loader_device_extension_list *dev_ext_list;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700749 if (!layer_list) return;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600750
Jon Ashburne13ecc92015-08-03 17:19:30 -0600751 for (i = 0; i < layer_list->count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700752 loader_destroy_generic_list(inst, (struct loader_generic_list *)&layer_list->list[i].instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700753 dev_ext_list = &layer_list->list[i].device_extension_list;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700754 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 -0700755 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700756 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700757 }
Mark Young0ad83132016-06-30 13:02:42 -0600758 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700759 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700760 loader_destroy_generic_list(inst, (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600761 }
762 layer_list->count = 0;
763
Jon Ashburnb82c1852015-08-11 14:49:54 -0600764 if (layer_list->capacity > 0) {
765 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600766 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600767 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600768}
769
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700770static VkResult loader_add_instance_extensions(const struct loader_instance *inst,
771 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props, const char *lib_name,
772 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700773 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600774 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600775 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600776
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600777 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600778 /* No EnumerateInstanceExtensionProperties defined */
Mark Young3a587792016-08-19 15:25:08 -0600779 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600780 }
781
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600782 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600783 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700784 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
785 "loader_add_instance_extensions: Error getting Instance "
786 "extension count from %s",
Mark Youngb6399312017-01-10 14:22:15 -0700787 lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600788 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600789 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600790
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600791 if (count == 0) {
792 /* No ExtensionProperties to report */
Mark Young3a587792016-08-19 15:25:08 -0600793 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600794 }
795
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600796 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young39389872017-01-19 21:10:49 -0700797 if (NULL == ext_props) {
798 res = VK_ERROR_OUT_OF_HOST_MEMORY;
799 goto out;
800 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600801
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600802 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600803 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700804 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
805 "loader_add_instance_extensions: Error getting Instance "
806 "extensions from %s",
Mark Youngb6399312017-01-10 14:22:15 -0700807 lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600808 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600809 }
Tony Barbour59a47322015-06-24 16:06:58 -0600810
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600811 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600812 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600813
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700814 bool ext_unsupported = wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600815 if (!ext_unsupported) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700816 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_MAJOR(ext_props[i].specVersion),
817 VK_MINOR(ext_props[i].specVersion), VK_PATCH(ext_props[i].specVersion));
818 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Instance Extension: %s (%s) version %s", ext_props[i].extensionName,
819 lib_name, spec_version);
Mark Young6267ae62017-01-12 12:27:19 -0700820
Mark Young3a587792016-08-19 15:25:08 -0600821 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
822 if (res != VK_SUCCESS) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700823 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
824 "loader_add_instance_extensions: Failed to add %s "
825 "to Instance extension list",
Mark Young3a587792016-08-19 15:25:08 -0600826 lib_name);
827 goto out;
828 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600829 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600830 }
Mark Young6267ae62017-01-12 12:27:19 -0700831
Mark Young3a587792016-08-19 15:25:08 -0600832out:
833 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600834}
835
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700836/*
837 * Initialize ext_list with the physical device extensions.
838 * The extension properties are passed as inputs in count and ext_props.
839 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700840static VkResult loader_init_device_extensions(const struct loader_instance *inst, struct loader_physical_device_term *phys_dev_term,
841 uint32_t count, VkExtensionProperties *ext_props,
842 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700843 VkResult res;
844 uint32_t i;
845
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700846 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600847 if (VK_SUCCESS != res) {
848 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700849 }
850
851 for (i = 0; i < count; i++) {
852 char spec_version[64];
853
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700854 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_MAJOR(ext_props[i].specVersion),
855 VK_MINOR(ext_props[i].specVersion), VK_PATCH(ext_props[i].specVersion));
856 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
857 phys_dev_term->this_icd_term->scanned_icd->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700858 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700859 if (res != VK_SUCCESS) return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700860 }
861
862 return VK_SUCCESS;
863}
864
Jon Ashburn1530c342016-02-26 13:14:27 -0700865VkResult loader_add_device_extensions(const struct loader_instance *inst,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700866 PFN_vkEnumerateDeviceExtensionProperties fpEnumerateDeviceExtensionProperties,
867 VkPhysicalDevice physical_device, const char *lib_name,
Jon Ashburn1530c342016-02-26 13:14:27 -0700868 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600869 uint32_t i, count;
870 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600871 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600872
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700873 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700874 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700875 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young9a3ddd42016-10-21 16:25:47 -0600876 if (!ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700877 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
878 "loader_add_device_extensions: Failed to allocate space"
879 " for device extension properties.");
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700880 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young9a3ddd42016-10-21 16:25:47 -0600881 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700882 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, ext_props);
Mark Young9a3ddd42016-10-21 16:25:47 -0600883 if (res != VK_SUCCESS) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700884 return res;
Mark Young9a3ddd42016-10-21 16:25:47 -0600885 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700886 for (i = 0; i < count; i++) {
887 char spec_version[64];
888
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700889 (void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_MAJOR(ext_props[i].specVersion),
890 VK_MINOR(ext_props[i].specVersion), VK_PATCH(ext_props[i].specVersion));
891 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
892 lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700893 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
Mark Youngb6399312017-01-10 14:22:15 -0700894 if (res != VK_SUCCESS) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700895 return res;
Mark Youngb6399312017-01-10 14:22:15 -0700896 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600897 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700898 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700899 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
900 "loader_add_device_extensions: Error getting physical "
901 "device extension info count from library %s",
Jon Ashburn23d36b12016-02-02 17:47:28 -0700902 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700903 return res;
904 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600905
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700906 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600907}
908
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700909VkResult 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 -0600910 size_t capacity = 32 * element_size;
911 list_info->count = 0;
912 list_info->capacity = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700913 list_info->list = loader_instance_heap_alloc(inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700914 if (list_info->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700915 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
916 "loader_init_generic_list: Failed to allocate space "
917 "for generic list");
Mark Young3a587792016-08-19 15:25:08 -0600918 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600919 }
Mark Young84ba0482016-09-02 11:45:00 -0600920 memset(list_info->list, 0, capacity);
921 list_info->capacity = capacity;
Mark Young3a587792016-08-19 15:25:08 -0600922 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600923}
924
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700925void loader_destroy_generic_list(const struct loader_instance *inst, struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600926 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700927 list->count = 0;
928 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600929}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600930
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600931/*
932 * Append non-duplicate extension properties defined in props
933 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700934 * Return
935 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600936 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700937VkResult loader_add_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list,
938 uint32_t prop_list_count, const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600939 uint32_t i;
940 const VkExtensionProperties *cur_ext;
941
942 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700943 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600944 if (VK_SUCCESS != res) {
945 return res;
946 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600947 }
948
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600949 for (i = 0; i < prop_list_count; i++) {
950 cur_ext = &props[i];
951
952 // look for duplicates
953 if (has_vk_extension_property(cur_ext, ext_list)) {
954 continue;
955 }
956
957 // add to list at end
958 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700959 if (ext_list->count * sizeof(VkExtensionProperties) >= ext_list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700960 ext_list->list = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
961 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700962
Mark Youngb6399312017-01-10 14:22:15 -0700963 if (ext_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700964 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
965 "loader_add_to_ext_list: Failed to reallocate "
966 "space for extension list");
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700967 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Youngb6399312017-01-10 14:22:15 -0700968 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700969
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600970 // double capacity
971 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600972 }
973
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700974 memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600975 ext_list->count++;
976 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700977 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600978}
979
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700980/*
981 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600982 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700983 * Return
984 * Vk_SUCCESS on success
985 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700986VkResult loader_add_to_dev_ext_list(const struct loader_instance *inst, struct loader_device_extension_list *ext_list,
987 const VkExtensionProperties *props, uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700988 uint32_t idx;
989 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700990 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 -0600991 if (VK_SUCCESS != res) {
992 return res;
993 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700994 }
995
Jon Ashburnb8726962016-04-08 15:03:35 -0600996 // look for duplicates
997 if (has_vk_dev_ext_property(props, ext_list)) {
998 return VK_SUCCESS;
999 }
1000
Jon Ashburn23d36b12016-02-02 17:47:28 -07001001 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001002 // add to list at end
1003 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001004 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001005 ext_list->list = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
1006 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001007
Mark Youngb6399312017-01-10 14:22:15 -07001008 if (ext_list->list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001009 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1010 "loader_add_to_dev_ext_list: Failed to reallocate "
1011 "space for device extension list");
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001012 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Youngb6399312017-01-10 14:22:15 -07001013 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001014
1015 // double capacity
1016 ext_list->capacity *= 2;
1017 }
1018
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001019 memcpy(&ext_list->list[idx].props, props, sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001020 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001021 ext_list->list[idx].entrypoints =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001022 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001023 if (ext_list->list[idx].entrypoints == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001024 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1025 "loader_add_to_dev_ext_list: Failed to allocate space "
1026 "for device extension entrypoint list in list %d",
Mark Youngb6399312017-01-10 14:22:15 -07001027 idx);
Mark Young0ad83132016-06-30 13:02:42 -06001028 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001029 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001030 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001031 for (uint32_t i = 0; i < entry_count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001032 ext_list->list[idx].entrypoints[i] =
1033 loader_instance_heap_alloc(inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001034 if (ext_list->list[idx].entrypoints[i] == NULL) {
1035 for (uint32_t j = 0; j < i; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001036 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints[j]);
Mark Young0ad83132016-06-30 13:02:42 -06001037 }
1038 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
1039 ext_list->list[idx].entrypoint_count = 0;
1040 ext_list->list[idx].entrypoints = NULL;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001041 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1042 "loader_add_to_dev_ext_list: Failed to allocate space "
1043 "for device extension entrypoint %d name",
Mark Youngb6399312017-01-10 14:22:15 -07001044 i);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001045 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001046 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001047 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
1048 }
1049 ext_list->count++;
1050
1051 return VK_SUCCESS;
1052}
1053
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001054/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001055 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001056 * Add these to the output layer_list. Don't add duplicates to the output
1057 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001058 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001059static VkResult loader_add_layer_names_to_list(const struct loader_instance *inst, struct loader_layer_list *output_list,
1060 uint32_t name_count, const char *const *names,
1061 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001062 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001063 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001064
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001065 for (uint32_t i = 0; i < name_count; i++) {
1066 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001067 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001068 if (!layer_prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001069 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1070 "loader_add_layer_names_to_list: Unable to find layer"
1071 " %s",
Mark Youngb6399312017-01-10 14:22:15 -07001072 search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001073 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001074 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001075 }
1076
Mark Young0ad83132016-06-30 13:02:42 -06001077 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001078 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001079
1080 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001081}
1082
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001083/*
1084 * Manage lists of VkLayerProperties
1085 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001086static bool loader_init_layer_list(const struct loader_instance *inst, struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001087 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001088 list->list = loader_instance_heap_alloc(inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001089 if (list->list == NULL) {
1090 return false;
1091 }
1092 memset(list->list, 0, list->capacity);
1093 list->count = 0;
1094 return true;
1095}
1096
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001097void loader_destroy_layer_list(const struct loader_instance *inst, struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001098 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001099 if (device) {
1100 loader_device_heap_free(device, layer_list->list);
1101 } else {
1102 loader_instance_heap_free(inst, layer_list->list);
1103 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001104 layer_list->count = 0;
1105 layer_list->capacity = 0;
1106}
1107
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001108/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001109 * Search the given layer list for a list
1110 * matching the given VkLayerProperties
1111 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001112bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001113 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001114 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0) return true;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001115 }
1116 return false;
1117}
1118
1119/*
1120 * Search the given layer list for a layer
1121 * matching the given name
1122 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001123bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001124 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001125 if (strcmp(name, list->list[i].info.layerName) == 0) return true;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001126 }
1127 return false;
1128}
1129
1130/*
1131 * Append non-duplicate layer properties defined in prop_list
1132 * to the given layer_info list
1133 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001134VkResult 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 -06001135 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001136 uint32_t i;
1137 struct loader_layer_properties *layer;
1138
1139 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001140 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001141 }
1142
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001143 if (list->list == NULL) return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001144
1145 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001146 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001147
1148 // look for duplicates
1149 if (has_vk_layer_property(&layer->info, list)) {
1150 continue;
1151 }
1152
1153 // add to list at end
1154 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001155 if (list->count * sizeof(struct loader_layer_properties) >= list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001156 list->list = loader_instance_heap_realloc(inst, list->list, list->capacity, list->capacity * 2,
1157 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001158 if (NULL == list->list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001159 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1160 "loader_add_to_layer_list: Realloc failed for "
1161 "when attempting to add new layer");
Mark Young0ad83132016-06-30 13:02:42 -06001162 return VK_ERROR_OUT_OF_HOST_MEMORY;
1163 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001164 // double capacity
1165 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001166 }
1167
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001168 memcpy(&list->list[list->count], layer, sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001169 list->count++;
1170 }
Mark Young0ad83132016-06-30 13:02:42 -06001171
1172 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001173}
1174
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001175/**
1176 * Search the search_list for any layer with a name
1177 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001178 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001179 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001180 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001181 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001182void loader_find_layer_name_add_list(const struct loader_instance *inst, const char *name, const enum layer_type type,
1183 const struct loader_layer_list *search_list, struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001184 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001185 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001186 struct loader_layer_properties *layer_prop = &search_list->list[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001187 if (0 == strcmp(layer_prop->info.layerName, name) && (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001188 /* Found a layer with the same name, add to found_list */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001189 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
Mark Young0ad83132016-06-30 13:02:42 -06001190 found = true;
1191 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001192 }
1193 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001194 if (!found) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001195 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
1196 "loader_find_layer_name_add_list: Failed to find layer name "
1197 "%s to activate",
Mark Youngb6399312017-01-10 14:22:15 -07001198 name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001199 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001200}
1201
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001202static VkExtensionProperties *get_extension_property(const char *name, const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001203 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001204 if (strcmp(name, list->list[i].extensionName) == 0) return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001205 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001206 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001207}
1208
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001209static VkExtensionProperties *get_dev_extension_property(const char *name, const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001210 for (uint32_t i = 0; i < list->count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001211 if (strcmp(name, list->list[i].props.extensionName) == 0) return &list->list[i].props;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001212 }
1213 return NULL;
1214}
1215
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001216/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001217 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001218 * the extension must provide two entry points for the loader to use:
1219 * - "trampoline" entry point - this is the address returned by GetProcAddr
1220 * and will always do what's necessary to support a global call.
1221 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001222 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001223 * the extension for the appropriate ICDs that are available.
1224 * There is no generic mechanism for including these functions, the references
1225 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001226 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1227 * GetProcAddr requests
1228 * loader_coalesce_extensions(void) - add extension records to the list of
1229 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001230 * extension available to the app.
1231 * instance_disp - add function pointer for terminator function to this array.
1232 * The extension itself should be in a separate file that will be
1233 * linked directly with the loader.
1234 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001235
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001236VkResult loader_get_icd_loader_instance_extensions(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
1237 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001238 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001239 VkResult res = VK_SUCCESS;
1240
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001241 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001242
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001243 // traverse scanned icd list adding non-duplicate extensions to the list
Mark Young0153e0b2016-11-03 14:27:13 -06001244 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001245 res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06001246 if (VK_SUCCESS != res) {
1247 goto out;
1248 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001249 res = loader_add_instance_extensions(inst, icd_tramp_list->scanned_list[i].EnumerateInstanceExtensionProperties,
1250 icd_tramp_list->scanned_list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001251 if (VK_SUCCESS == res) {
Lenny Komow4053b812016-12-29 16:27:28 -07001252 // Remove any extensions not recognized by the loader
1253 for (int32_t j = 0; j < (int32_t)icd_exts.count; j++) {
Lenny Komow4053b812016-12-29 16:27:28 -07001254 // See if the extension is in the list of supported extensions
1255 bool found = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001256 for (uint32_t k = 0; LOADER_INSTANCE_EXTENSIONS[k] != NULL; k++) {
1257 if (strcmp(icd_exts.list[j].extensionName, LOADER_INSTANCE_EXTENSIONS[k]) == 0) {
Lenny Komow4053b812016-12-29 16:27:28 -07001258 found = true;
1259 break;
1260 }
1261 }
1262
1263 // If it isn't in the list, remove it
1264 if (!found) {
1265 for (uint32_t k = j + 1; k < icd_exts.count; k++) {
1266 icd_exts.list[k - 1] = icd_exts.list[k];
1267 }
1268 --icd_exts.count;
1269 --j;
1270 }
1271 }
1272
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001273 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count, icd_exts.list);
Mark Young3a587792016-08-19 15:25:08 -06001274 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001275 loader_destroy_generic_list(inst, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001276 if (VK_SUCCESS != res) {
1277 goto out;
1278 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001279 };
1280
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001281 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001282 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001283
1284out:
1285 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001286}
1287
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001288struct 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 -06001289 *found_dev = NULL;
Mark Young16573c72016-06-28 10:52:43 -06001290 uint32_t index = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001291 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
1292 for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
1293 for (struct loader_device *dev = icd_term->logical_device_list; dev; dev = dev->next)
Mark Young65cb3662016-11-07 13:27:02 -07001294 // Value comparison of device prevents object wrapping by layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001295 if (loader_get_dispatch(dev->icd_device) == loader_get_dispatch(device) ||
1296 loader_get_dispatch(dev->chain_device) == loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001297 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001298 if (NULL != icd_index) {
1299 *icd_index = index;
1300 }
Mark Young0153e0b2016-11-03 14:27:13 -06001301 return icd_term;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001302 }
Mark Young16573c72016-06-28 10:52:43 -06001303 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001304 }
1305 }
1306 return NULL;
1307}
1308
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001309void loader_destroy_logical_device(const struct loader_instance *inst, struct loader_device *dev,
Mark Young0ad83132016-06-30 13:02:42 -06001310 const VkAllocationCallbacks *pAllocator) {
1311 if (pAllocator) {
1312 dev->alloc_callbacks = *pAllocator;
1313 }
Mark Young0ad83132016-06-30 13:02:42 -06001314 if (NULL != dev->activated_layer_list.list) {
1315 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1316 }
1317 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001318}
1319
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001320struct loader_device *loader_create_logical_device(const struct loader_instance *inst, const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001321 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001322#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1323 {
1324#else
1325 if (pAllocator) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001326 new_dev = (struct loader_device *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(struct loader_device),
1327 sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Mark Young0ad83132016-06-30 13:02:42 -06001328 } else {
1329#endif
1330 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1331 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001332
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001333 if (!new_dev) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001334 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1335 "loader_create_logical_device: Failed to alloc struct "
1336 "loader_device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001337 return NULL;
1338 }
1339
1340 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001341 if (pAllocator) {
1342 new_dev->alloc_callbacks = *pAllocator;
1343 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001344
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001345 return new_dev;
1346}
1347
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001348void 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 -06001349 dev->next = icd_term->logical_device_list;
1350 icd_term->logical_device_list = dev;
Piers Daniell295fe402016-03-29 11:51:11 -06001351}
1352
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001353void loader_remove_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term,
1354 struct loader_device *found_dev, const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001355 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001356
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001357 if (!icd_term || !found_dev) return;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001358
1359 prev_dev = NULL;
Mark Young0153e0b2016-11-03 14:27:13 -06001360 dev = icd_term->logical_device_list;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001361 while (dev && dev != found_dev) {
1362 prev_dev = dev;
1363 dev = dev->next;
1364 }
1365
1366 if (prev_dev)
1367 prev_dev->next = found_dev->next;
1368 else
Mark Young0153e0b2016-11-03 14:27:13 -06001369 icd_term->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001370 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001371}
1372
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001373static void loader_icd_destroy(struct loader_instance *ptr_inst, struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001374 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001375 ptr_inst->total_icd_count--;
Mark Young0153e0b2016-11-03 14:27:13 -06001376 for (struct loader_device *dev = icd_term->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001377 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001378 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001379 dev = next_dev;
1380 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001381
Mark Young0153e0b2016-11-03 14:27:13 -06001382 loader_instance_heap_free(ptr_inst, icd_term);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001383}
1384
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001385static struct loader_icd_term *loader_icd_create(const struct loader_instance *inst) {
Mark Young0153e0b2016-11-03 14:27:13 -06001386 struct loader_icd_term *icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001387
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001388 icd_term = loader_instance_heap_alloc(inst, sizeof(struct loader_icd_term), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001389 if (!icd_term) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001390 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001391 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001392
Mark Young0153e0b2016-11-03 14:27:13 -06001393 memset(icd_term, 0, sizeof(struct loader_icd_term));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001394
Mark Young0153e0b2016-11-03 14:27:13 -06001395 return icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001396}
1397
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001398static 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 -06001399 struct loader_icd_term *icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001400
Mark Young0153e0b2016-11-03 14:27:13 -06001401 icd_term = loader_icd_create(ptr_inst);
1402 if (!icd_term) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001403 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001404 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001405
Mark Young0153e0b2016-11-03 14:27:13 -06001406 icd_term->scanned_icd = scanned_icd;
1407 icd_term->this_instance = ptr_inst;
Jon Ashburn3d002332015-08-20 16:35:30 -06001408
Chia-I Wu13a61a52014-08-04 11:18:20 +08001409 /* prepend to the list */
Mark Young0153e0b2016-11-03 14:27:13 -06001410 icd_term->next = ptr_inst->icd_terms;
1411 ptr_inst->icd_terms = icd_term;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001412 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001413
Mark Young0153e0b2016-11-03 14:27:13 -06001414 return icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001415}
Mark Young0153e0b2016-11-03 14:27:13 -06001416
Jon Ashburn17b4c862016-04-25 11:09:37 -06001417/**
1418 * Determine the ICD interface version to use.
1419 * @param icd
1420 * @param pVersion Output parameter indicating which version to use or 0 if
1421 * the negotiation API is not supported by the ICD
1422 * @return bool indicating true if the selected interface version is supported
1423 * by the loader, false indicates the version is not supported
Jon Ashburn17b4c862016-04-25 11:09:37 -06001424 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001425bool loader_get_icd_interface_version(PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version, uint32_t *pVersion) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001426 if (fp_negotiate_icd_version == NULL) {
1427 // ICD does not support the negotiation API, it supports version 0 or 1
1428 // calling code must determine if it is version 0 or 1
1429 *pVersion = 0;
1430 } else {
1431 // ICD supports the negotiation API, so call it with the loader's
1432 // latest version supported
1433 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1434 VkResult result = fp_negotiate_icd_version(pVersion);
1435
1436 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1437 // ICD no longer supports the loader's latest interface version so
1438 // fail loading the ICD
1439 return false;
1440 }
1441 }
1442
1443#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1444 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1445 // Loader no longer supports the ICD's latest interface version so fail
1446 // loading the ICD
1447 return false;
1448 }
1449#endif
1450 return true;
1451}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001452
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001453void loader_scanned_icd_clear(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001454 if (icd_tramp_list->capacity == 0) return;
Mark Young0153e0b2016-11-03 14:27:13 -06001455 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
1456 loader_platform_close_library(icd_tramp_list->scanned_list[i].handle);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001457 loader_instance_heap_free(inst, icd_tramp_list->scanned_list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001458 }
Mark Young0153e0b2016-11-03 14:27:13 -06001459 loader_instance_heap_free(inst, icd_tramp_list->scanned_list);
1460 icd_tramp_list->capacity = 0;
1461 icd_tramp_list->count = 0;
1462 icd_tramp_list->scanned_list = NULL;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001463}
1464
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001465static 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 -06001466 VkResult err = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06001467 loader_scanned_icd_clear(inst, icd_tramp_list);
1468 icd_tramp_list->capacity = 8 * sizeof(struct loader_scanned_icd);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001469 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 -06001470 if (NULL == icd_tramp_list->scanned_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001471 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1472 "loader_scanned_icd_init: Realloc failed for layer list when "
1473 "attempting to add new layer");
Mark Young0ad83132016-06-30 13:02:42 -06001474 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1475 }
1476 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001477}
1478
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001479static VkResult loader_scanned_icd_add(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
1480 const char *filename, uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001481 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001482 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001483 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001484 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Mark Young39389872017-01-19 21:10:49 -07001485 PFN_GetPhysicalDeviceProcAddr fp_get_phys_dev_proc_addr = NULL;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001486 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Mark Young0153e0b2016-11-03 14:27:13 -06001487 struct loader_scanned_icd *new_scanned_icd;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001488 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001489 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001490
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001491 /* TODO implement smarter opening/closing of libraries. For now this
1492 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001493 handle = loader_platform_open_library(filename);
Mark Youngb6399312017-01-10 14:22:15 -07001494 if (NULL == handle) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001495 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001496 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001497 }
1498
Jon Ashburn17b4c862016-04-25 11:09:37 -06001499 // Get and settle on an ICD interface version
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001500 fp_negotiate_icd_version = loader_platform_get_proc_address(handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001501
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001502 if (!loader_get_icd_interface_version(fp_negotiate_icd_version, &interface_vers)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001503 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1504 "loader_scanned_icd_add: ICD %s doesn't support interface"
1505 " version compatible with loader, skip this ICD.",
Mark Young0ad83132016-06-30 13:02:42 -06001506 filename);
Mark Young3a587792016-08-19 15:25:08 -06001507 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001508 }
1509
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001510 fp_get_proc_addr = loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Mark Youngb6399312017-01-10 14:22:15 -07001511 if (NULL == fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001512 assert(interface_vers == 0);
1513 // Use deprecated interface from version 0
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001514 fp_get_proc_addr = loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Mark Youngb6399312017-01-10 14:22:15 -07001515 if (NULL == fp_get_proc_addr) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001516 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1517 "loader_scanned_icd_add: Attempt to retreive either "
1518 "\'vkGetInstanceProcAddr\' or "
1519 "\'vk_icdGetInstanceProcAddr\' from ICD %s failed.",
Mark Youngb6399312017-01-10 14:22:15 -07001520 filename);
Mark Young3a587792016-08-19 15:25:08 -06001521 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001522 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001523 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
1524 "loader_scanned_icd_add: Using deprecated ICD "
1525 "interface of \'vkGetInstanceProcAddr\' instead of "
1526 "\'vk_icdGetInstanceProcAddr\' for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001527 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001528 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001529 fp_create_inst = loader_platform_get_proc_address(handle, "vkCreateInstance");
Mark Youngb6399312017-01-10 14:22:15 -07001530 if (NULL == fp_create_inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001531 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1532 "loader_scanned_icd_add: Failed querying "
1533 "\'vkCreateInstance\' via dlsym/loadlibrary for "
1534 "ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001535 filename);
Mark Young3a587792016-08-19 15:25:08 -06001536 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001537 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001538 fp_get_inst_ext_props = loader_platform_get_proc_address(handle, "vkEnumerateInstanceExtensionProperties");
Mark Youngb6399312017-01-10 14:22:15 -07001539 if (NULL == fp_get_inst_ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001540 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1541 "loader_scanned_icd_add: Could not get \'vkEnumerate"
1542 "InstanceExtensionProperties\' via dlsym/loadlibrary "
1543 "for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001544 filename);
Mark Young3a587792016-08-19 15:25:08 -06001545 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001546 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001547 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001548 // Use newer interface version 1 or later
Mark Young39389872017-01-19 21:10:49 -07001549 if (interface_vers == 0) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001550 interface_vers = 1;
Mark Young39389872017-01-19 21:10:49 -07001551 }
Jon Ashburn17b4c862016-04-25 11:09:37 -06001552
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001553 fp_create_inst = (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Mark Youngb6399312017-01-10 14:22:15 -07001554 if (NULL == fp_create_inst) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001555 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1556 "loader_scanned_icd_add: Could not get "
1557 "\'vkCreateInstance\' via \'vk_icdGetInstanceProcAddr\'"
1558 " for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001559 filename);
Mark Young3a587792016-08-19 15:25:08 -06001560 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001561 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001562 fp_get_inst_ext_props =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001563 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(NULL, "vkEnumerateInstanceExtensionProperties");
Mark Youngb6399312017-01-10 14:22:15 -07001564 if (NULL == fp_get_inst_ext_props) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001565 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1566 "loader_scanned_icd_add: Could not get \'vkEnumerate"
1567 "InstanceExtensionProperties\' via "
1568 "\'vk_icdGetInstanceProcAddr\' for ICD %s",
Mark Young0ad83132016-06-30 13:02:42 -06001569 filename);
Mark Young3a587792016-08-19 15:25:08 -06001570 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001571 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001572 fp_get_phys_dev_proc_addr = loader_platform_get_proc_address(handle, "vk_icdGetPhysicalDeviceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001573 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001574
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001575 // check for enough capacity
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001576 if ((icd_tramp_list->count * sizeof(struct loader_scanned_icd)) >= icd_tramp_list->capacity) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001577 icd_tramp_list->scanned_list =
1578 loader_instance_heap_realloc(inst, icd_tramp_list->scanned_list, icd_tramp_list->capacity, icd_tramp_list->capacity * 2,
1579 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001580 if (NULL == icd_tramp_list->scanned_list) {
Mark Young3a587792016-08-19 15:25:08 -06001581 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001582 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1583 "loader_scanned_icd_add: Realloc failed on icd library"
1584 " list for ICD %s",
Mark Youngb6399312017-01-10 14:22:15 -07001585 filename);
Mark Young3a587792016-08-19 15:25:08 -06001586 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001587 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001588 // double capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001589 icd_tramp_list->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001590 }
1591
Mark Young39389872017-01-19 21:10:49 -07001592 new_scanned_icd = &(icd_tramp_list->scanned_list[icd_tramp_list->count]);
Mark Young0153e0b2016-11-03 14:27:13 -06001593 new_scanned_icd->handle = handle;
1594 new_scanned_icd->api_version = api_version;
1595 new_scanned_icd->GetInstanceProcAddr = fp_get_proc_addr;
Mark Young39389872017-01-19 21:10:49 -07001596 new_scanned_icd->GetPhysicalDeviceProcAddr = fp_get_phys_dev_proc_addr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001597 new_scanned_icd->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Mark Young0153e0b2016-11-03 14:27:13 -06001598 new_scanned_icd->CreateInstance = fp_create_inst;
1599 new_scanned_icd->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001600
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001601 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 -06001602 if (NULL == new_scanned_icd->lib_name) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001603 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 -06001604 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001605 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001606 }
Mark Young0153e0b2016-11-03 14:27:13 -06001607 strcpy(new_scanned_icd->lib_name, filename);
1608 icd_tramp_list->count++;
Mark Young3a587792016-08-19 15:25:08 -06001609
1610out:
1611
1612 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001613}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001614
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001615static 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 -07001616/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001617
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001618#define LOOKUP_GIPA(func, required) \
1619 do { \
1620 icd_term->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1621 if (!icd_term->func && required) { \
1622 loader_log((struct loader_instance *)inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
1623 loader_platform_get_proc_address_error("vk" #func)); \
1624 return false; \
1625 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001626 } while (0)
1627
Jon Ashburnc624c882015-07-16 10:17:29 -06001628 LOOKUP_GIPA(GetDeviceProcAddr, true);
1629 LOOKUP_GIPA(DestroyInstance, true);
1630 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1631 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1632 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001633 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001634 LOOKUP_GIPA(CreateDevice, true);
1635 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1636 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001637 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001638 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001639 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Ian Elliott7e40db92015-08-21 15:09:33 -06001640 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001641 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1642 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1643 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001644 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1645 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1646 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1647 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1648 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1649 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1650 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1651 LOOKUP_GIPA(DestroySurfaceKHR, false);
Mark Young16573c72016-06-28 10:52:43 -06001652 LOOKUP_GIPA(CreateSwapchainKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001653#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Young16573c72016-06-28 10:52:43 -06001654 LOOKUP_GIPA(CreateWin32SurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001655 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1656#endif
1657#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001658 LOOKUP_GIPA(CreateXcbSurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001659 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1660#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001661#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001662 LOOKUP_GIPA(CreateXlibSurfaceKHR, false);
Karl Schultz65d20182016-03-08 07:55:27 -07001663 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1664#endif
Mark Younga7c51fd2016-09-16 10:18:42 -06001665#ifdef VK_USE_PLATFORM_MIR_KHR
1666 LOOKUP_GIPA(CreateMirSurfaceKHR, false);
1667 LOOKUP_GIPA(GetPhysicalDeviceMirPresentationSupportKHR, false);
1668#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001669#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Young16573c72016-06-28 10:52:43 -06001670 LOOKUP_GIPA(CreateWaylandSurfaceKHR, false);
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001671 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1672#endif
Mark Young39389872017-01-19 21:10:49 -07001673 LOOKUP_GIPA(CreateSharedSwapchainsKHR, false);
1674
1675 // KHR_get_physical_device_properties2
1676 LOOKUP_GIPA(GetPhysicalDeviceFeatures2KHR, false);
1677 LOOKUP_GIPA(GetPhysicalDeviceProperties2KHR, false);
1678 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties2KHR, false);
1679 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties2KHR, false);
1680 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties2KHR, false);
1681 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties2KHR, false);
1682 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties2KHR, false);
1683 // EXT_debug_marker (items needing a trampoline/terminator)
1684 LOOKUP_GIPA(DebugMarkerSetObjectTagEXT, false);
1685 LOOKUP_GIPA(DebugMarkerSetObjectNameEXT, false);
1686
1687#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
1688 // EXT_acquire_xlib_display
1689 LOOKUP_GIPA(AcquireXlibDisplayEXT, false);
1690 LOOKUP_GIPA(GetRandROutputDisplayEXT, false);
1691#endif
1692
1693 // EXT_debug_report
1694 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1695 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
1696 LOOKUP_GIPA(DebugReportMessageEXT, false);
1697
1698 // EXT_direct_mode_display
1699 LOOKUP_GIPA(ReleaseDisplayEXT, false);
1700
1701 // EXT_display_surface_counter
1702 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilities2EXT, false);
1703
Mark Youngfa552782016-12-12 16:14:55 -07001704 // NV_external_memory_capabilities
James Jones389dc0c2016-08-18 23:41:19 +01001705 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Mark Youngfa552782016-12-12 16:14:55 -07001706 // NVX_device_generated_commands
1707 LOOKUP_GIPA(GetPhysicalDeviceGeneratedCommandsPropertiesNVX, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001708
Jon Ashburnc624c882015-07-16 10:17:29 -06001709#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001710
Jon Ashburnc624c882015-07-16 10:17:29 -06001711 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001712}
1713
Jon Ashburn23d36b12016-02-02 17:47:28 -07001714static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001715 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001716
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001717 if (g_loader_debug > 0) return;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001718
1719 g_loader_debug = 0;
1720
1721 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001722 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001723 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001724 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001725 size_t len;
1726
1727 if (p)
1728 len = p - env;
1729 else
1730 len = strlen(env);
1731
1732 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001733 if (strncmp(env, "all", len) == 0) {
1734 g_loader_debug = ~0u;
1735 g_loader_log_msgs = ~0u;
1736 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001737 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001738 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001739 } else if (strncmp(env, "info", len) == 0) {
1740 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001741 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001742 } else if (strncmp(env, "perf", len) == 0) {
1743 g_loader_debug |= LOADER_PERF_BIT;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001744 g_loader_log_msgs |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001745 } else if (strncmp(env, "error", len) == 0) {
1746 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001747 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001748 } else if (strncmp(env, "debug", len) == 0) {
1749 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001750 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001751 }
1752 }
1753
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001754 if (!p) break;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001755
1756 env = p + 1;
1757 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001758
Mark Young0ad83132016-06-30 13:02:42 -06001759 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001760}
1761
Jon Ashburn23d36b12016-02-02 17:47:28 -07001762void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001763 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001764 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001765 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001766
1767 // initialize logging
1768 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001769
1770 // initial cJSON to use alloc callbacks
1771 cJSON_Hooks alloc_fns = {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001772 .malloc_fn = loader_instance_tls_heap_alloc, .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001773 };
1774 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001775}
1776
Jon Ashburn2077e382015-06-29 11:25:34 -06001777struct loader_manifest_files {
1778 uint32_t count;
1779 char **filename_list;
1780};
1781
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001782/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001783 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001784 *
1785 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001786 * A pointer to first char in the next path.
1787 * The next path (or NULL) in the list is returned in next_path.
1788 * Note: input string is modified in some cases. PASS IN A COPY!
1789 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001790static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001791 uint32_t len;
1792 char *next;
1793
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001794 if (path == NULL) return NULL;
Frank Henigman57173102016-11-24 22:15:20 -05001795 next = strchr(path, PATH_SEPARATOR);
Jon Ashburn2077e382015-06-29 11:25:34 -06001796 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001797 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001798 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001799 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001800 *next = '\0';
1801 next++;
1802 }
1803
1804 return next;
1805}
1806
1807/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001808 * Given a path which is absolute or relative, expand the path if relative or
1809 * leave the path unmodified if absolute. The base path to prepend to relative
1810 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001811 *
1812 * \returns
1813 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001814 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001815static 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 -06001816 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001817 // do not prepend a base to an absolute path
1818 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001819 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001820
1821 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001822}
1823
1824/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001825 * Given a filename (file) and a list of paths (dir), try to find an existing
1826 * file in the paths. If filename already is a path then no
1827 * searching in the given paths.
1828 *
1829 * \returns
1830 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001831 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001832static void loader_get_fullpath(const char *file, const char *dirs, size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001833 if (!loader_platform_is_path(file) && *dirs) {
1834 char *dirs_copy, *dir, *next_dir;
1835
1836 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1837 strcpy(dirs_copy, dirs);
1838
Jon Ashburn23d36b12016-02-02 17:47:28 -07001839 // find if file exists after prepending paths in given list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001840 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir)); dir = next_dir) {
1841 loader_platform_combine_path(out_fullpath, out_size, dir, file, NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001842 if (loader_platform_file_exists(out_fullpath)) {
1843 return;
1844 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001845 }
1846 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001847
Karl Schultze2ef9e62017-01-13 14:01:35 -07001848 (void)snprintf(out_fullpath, out_size, "%s", file);
Jon Ashburn2077e382015-06-29 11:25:34 -06001849}
1850
1851/**
1852 * Read a JSON file into a buffer.
1853 *
1854 * \returns
1855 * A pointer to a cJSON object representing the JSON parse tree.
1856 * This returned buffer should be freed by caller.
1857 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001858static VkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json) {
Mark Young3a587792016-08-19 15:25:08 -06001859 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001860 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001861 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001862 VkResult res = VK_SUCCESS;
1863
1864 if (NULL == json) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001865 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_json: Received invalid JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001866 res = VK_ERROR_INITIALIZATION_FAILED;
1867 goto out;
1868 }
1869
1870 *json = NULL;
1871
Jon Ashburn23d36b12016-02-02 17:47:28 -07001872 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001873 if (!file) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001874 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 -07001875 res = VK_ERROR_INITIALIZATION_FAILED;
Mark Young3a587792016-08-19 15:25:08 -06001876 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001877 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001878 fseek(file, 0, SEEK_END);
1879 len = ftell(file);
1880 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001881 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001882 if (json_buf == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001883 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1884 "loader_get_json: Failed to allocate space for "
1885 "JSON file %s buffer of length %d",
Mark Youngb6399312017-01-10 14:22:15 -07001886 filename, len);
1887 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001888 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001889 }
1890 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001891 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 -07001892 res = VK_ERROR_INITIALIZATION_FAILED;
Mark Young3a587792016-08-19 15:25:08 -06001893 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001894 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001895 json_buf[len] = '\0';
1896
Jon Ashburn23d36b12016-02-02 17:47:28 -07001897 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001898 *json = cJSON_Parse(json_buf);
1899 if (*json == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001900 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1901 "loader_get_json: Failed to parse JSON file %s, "
1902 "this is usually because something ran out of "
1903 "memory.",
Mark Youngb6399312017-01-10 14:22:15 -07001904 filename);
1905 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06001906 goto out;
1907 }
1908
1909out:
1910 if (NULL != file) {
1911 fclose(file);
1912 }
1913
1914 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001915}
1916
1917/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001918 * Do a deep copy of the loader_layer_properties structure.
1919 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001920VkResult loader_copy_layer_properties(const struct loader_instance *inst, struct loader_layer_properties *dst,
Mark Young0ad83132016-06-30 13:02:42 -06001921 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001922 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001923 memcpy(dst, src, sizeof(*src));
Mark Youngb6399312017-01-10 14:22:15 -07001924 dst->instance_extension_list.list = loader_instance_heap_alloc(
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001925 inst, sizeof(VkExtensionProperties) * src->instance_extension_list.count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001926 if (NULL == dst->instance_extension_list.list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001927 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1928 "loader_copy_layer_properties: Failed to allocate space "
1929 "for instance extension list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07001930 src->instance_extension_list.count);
Mark Young0ad83132016-06-30 13:02:42 -06001931 return VK_ERROR_OUT_OF_HOST_MEMORY;
1932 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001933 dst->instance_extension_list.capacity = sizeof(VkExtensionProperties) * src->instance_extension_list.count;
1934 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list, dst->instance_extension_list.capacity);
Mark Youngb6399312017-01-10 14:22:15 -07001935 dst->device_extension_list.list = loader_instance_heap_alloc(
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001936 inst, sizeof(struct loader_dev_ext_props) * src->device_extension_list.count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001937 if (NULL == dst->device_extension_list.list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001938 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1939 "loader_copy_layer_properties: Failed to allocate space "
1940 "for device extension list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07001941 src->device_extension_list.count);
Mark Young0ad83132016-06-30 13:02:42 -06001942 return VK_ERROR_OUT_OF_HOST_MEMORY;
1943 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001944 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 -07001945
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001946 dst->device_extension_list.capacity = sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
1947 memcpy(dst->device_extension_list.list, src->device_extension_list.list, dst->device_extension_list.capacity);
1948 if (src->device_extension_list.count > 0 && src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001949 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001950 dst->device_extension_list.list->entrypoints =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001951 loader_instance_heap_alloc(inst, sizeof(char *) * cnt, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001952 if (NULL == dst->device_extension_list.list->entrypoints) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001953 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1954 "loader_copy_layer_properties: Failed to allocate space "
1955 "for device extension entrypoint list of size %d.",
Mark Youngb6399312017-01-10 14:22:15 -07001956 cnt);
Mark Young0ad83132016-06-30 13:02:42 -06001957 return VK_ERROR_OUT_OF_HOST_MEMORY;
1958 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001959 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
Mark Young0ad83132016-06-30 13:02:42 -06001960
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001961 for (i = 0; i < cnt; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001962 dst->device_extension_list.list->entrypoints[i] = loader_instance_heap_alloc(
1963 inst, strlen(src->device_extension_list.list->entrypoints[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001964 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
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 "
1967 "allocate space for device extension entrypoint "
1968 "%d name of length",
Mark Youngb6399312017-01-10 14:22:15 -07001969 i);
Mark Young0ad83132016-06-30 13:02:42 -06001970 return VK_ERROR_OUT_OF_HOST_MEMORY;
1971 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001972 strcpy(dst->device_extension_list.list->entrypoints[i], src->device_extension_list.list->entrypoints[i]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001973 }
1974 }
Mark Young0ad83132016-06-30 13:02:42 -06001975
1976 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001977}
1978
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001979static bool loader_find_layer_name_list(const char *name, const struct loader_layer_list *layer_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001980 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001981 for (uint32_t j = 0; j < layer_list->count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001982 if (!strcmp(name, layer_list->list[j].info.layerName)) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001983 return false;
1984}
1985
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001986static bool loader_find_layer_name(const char *name, uint32_t layer_count, const char **layer_list) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001987 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001988 for (uint32_t j = 0; j < layer_count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001989 if (!strcmp(name, layer_list[j])) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001990 return false;
1991}
1992
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07001993bool 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 -07001994 if (!layer_list) return false;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001995 for (uint32_t j = 0; j < layer_count; j++)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001996 if (!strcmp(name, layer_list[j])) return true;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001997 return false;
1998}
1999
2000/**
2001 * Searches through an array of layer names (ppp_layer_names) looking for a
2002 * layer key_name.
2003 * If not found then simply returns updating nothing.
2004 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12002005 * Any duplicate (pre-existing) expand_names in layer names are removed.
2006 * Order is otherwise preserved, with the layer key_name being replaced by the
2007 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07002008 * @param inst
2009 * @param layer_count
2010 * @param ppp_layer_names
2011 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002012VkResult loader_expand_layer_names(struct loader_instance *inst, const char *key_name, uint32_t expand_count,
2013 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE], uint32_t *layer_count,
2014 char const *const **ppp_layer_names) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002015 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002016
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002017 if (!loader_find_layer_name(key_name, *layer_count, (char const **)pp_src_layers)) {
Jon Ashburn491cd042016-05-16 14:01:18 -06002018 inst->activated_layers_are_std_val = false;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002019 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06002020 }
Jon Ashburn71483442016-02-11 18:59:43 -07002021
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002022 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 +12002023
Jon Ashburn491cd042016-05-16 14:01:18 -06002024 inst->activated_layers_are_std_val = true;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002025 char const **pp_dst_layers = loader_instance_heap_alloc(inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2026 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002027 if (NULL == pp_dst_layers) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002028 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2029 "loader_expand_layer_names:: Failed to allocate space for "
2030 "std_validation layer names in pp_dst_layers.");
Mark Young0ad83132016-06-30 13:02:42 -06002031 return VK_ERROR_OUT_OF_HOST_MEMORY;
2032 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002033
2034 // copy layers from src to dst, stripping key_name and anything in
2035 // expand_names.
2036 uint32_t src_index, dst_index = 0;
2037 for (src_index = 0; src_index < *layer_count; src_index++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002038 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count, expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002039 continue;
2040 }
2041
2042 if (!strcmp(pp_src_layers[src_index], key_name)) {
2043 // insert all expand_names in place of key_name
2044 uint32_t expand_index;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002045 for (expand_index = 0; expand_index < expand_count; expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002046 pp_dst_layers[dst_index++] = expand_names[expand_index];
2047 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002048 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002049 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002050
2051 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002052 }
2053
Chris Forbesbd9de052016-04-06 20:49:02 +12002054 *ppp_layer_names = pp_dst_layers;
2055 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002056
2057 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002058}
2059
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002060void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst, const VkInstanceCreateInfo *orig,
Chris Forbesbd9de052016-04-06 20:49:02 +12002061 VkInstanceCreateInfo *ours) {
2062 /* Free the layer names array iff we had to reallocate it */
2063 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002064 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002065 }
2066}
2067
Jon Ashburn491cd042016-05-16 14:01:18 -06002068void loader_init_std_validation_props(struct loader_layer_properties *props) {
2069 memset(props, 0, sizeof(struct loader_layer_properties));
2070 props->type = VK_LAYER_TYPE_META_EXPLICT;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002071 strncpy(props->info.description, "LunarG Standard Validation Layer", sizeof(props->info.description));
Jon Ashburn491cd042016-05-16 14:01:18 -06002072 props->info.implementationVersion = 1;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002073 strncpy(props->info.layerName, std_validation_str, sizeof(props->info.layerName));
Jon Ashburn491cd042016-05-16 14:01:18 -06002074 // TODO what about specVersion? for now insert loader's built version
2075 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2076}
2077
Jon Ashburn86a527a2016-02-10 20:59:26 -07002078/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002079 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002080 * the set of required layer names. If found then it adds a meta property to the
2081 * layer list.
2082 * Assumes the required layers are the same for both instance and device lists.
2083 * @param inst
2084 * @param layer_count number of layers in layer_names
2085 * @param layer_names array of required layer names
2086 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002087 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002088static void loader_add_layer_property_meta(const struct loader_instance *inst, uint32_t layer_count,
2089 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
2090 struct loader_layer_list *layer_instance_list) {
Jon Ashburn491cd042016-05-16 14:01:18 -06002091 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002092 bool found;
2093 struct loader_layer_list *layer_list;
2094
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002095 if (0 == layer_count || (!layer_instance_list)) return;
2096 if (layer_instance_list && (layer_count > layer_instance_list->count)) return;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002097
Jon Ashburn491cd042016-05-16 14:01:18 -06002098 layer_list = layer_instance_list;
2099
2100 found = true;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002101 if (layer_list == NULL) return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002102 for (i = 0; i < layer_count; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002103 if (loader_find_layer_name_list(layer_names[i], layer_list)) continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002104 found = false;
2105 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002106 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002107
2108 struct loader_layer_properties *props;
2109 if (found) {
2110 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002111 if (NULL == props) {
2112 // Error already triggered in loader_get_next_layer_property.
2113 return;
2114 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002115 loader_init_std_validation_props(props);
Jon Ashburn491cd042016-05-16 14:01:18 -06002116 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07002117}
2118
Mark Young39389872017-01-19 21:10:49 -07002119// This structure is used to store the json file version
2120// in a more managable way.
2121typedef struct {
2122 uint16_t major;
2123 uint16_t minor;
2124 uint16_t patch;
2125} layer_json_version;
2126
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002127static void loader_read_json_layer(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list,
2128 cJSON *layer_node, layer_json_version version, cJSON *item, cJSON *disable_environment,
2129 bool is_implicit, char *filename) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002130 char *temp;
2131 char *name, *type, *library_path, *api_version;
2132 char *implementation_version, *description;
2133 cJSON *ext_item;
2134 VkExtensionProperties ext_prop;
2135
Mark Young0ad83132016-06-30 13:02:42 -06002136/*
2137 * The following are required in the "layer" object:
2138 * (required) "name"
2139 * (required) "type"
2140 * (required) “library_path”
2141 * (required) “api_version”
2142 * (required) “implementation_version”
2143 * (required) “description”
2144 * (required for implicit layers) “disable_environment”
2145 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002146
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002147#define GET_JSON_OBJECT(node, var) \
2148 { \
2149 var = cJSON_GetObjectItem(node, #var); \
2150 if (var == NULL) { \
2151 layer_node = layer_node->next; \
2152 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2153 "Didn't find required layer object %s in manifest " \
2154 "JSON file, skipping this layer", \
2155 #var); \
2156 return; \
2157 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002158 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002159#define GET_JSON_ITEM(node, var) \
2160 { \
2161 item = cJSON_GetObjectItem(node, #var); \
2162 if (item == NULL) { \
2163 layer_node = layer_node->next; \
2164 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2165 "Didn't find required layer value %s in manifest JSON " \
2166 "file, skipping this layer", \
2167 #var); \
2168 return; \
2169 } \
2170 temp = cJSON_Print(item); \
2171 if (temp == NULL) { \
2172 layer_node = layer_node->next; \
2173 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2174 "Problem accessing layer value %s in manifest JSON " \
2175 "file, skipping this layer", \
2176 #var); \
2177 return; \
2178 } \
2179 temp[strlen(temp) - 1] = '\0'; \
2180 var = loader_stack_alloc(strlen(temp) + 1); \
2181 strcpy(var, &temp[1]); \
2182 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002183 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002184 GET_JSON_ITEM(layer_node, name)
2185 GET_JSON_ITEM(layer_node, type)
2186 GET_JSON_ITEM(layer_node, library_path)
2187 GET_JSON_ITEM(layer_node, api_version)
2188 GET_JSON_ITEM(layer_node, implementation_version)
2189 GET_JSON_ITEM(layer_node, description)
2190 if (is_implicit) {
2191 GET_JSON_OBJECT(layer_node, disable_environment)
2192 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002193#undef GET_JSON_ITEM
2194#undef GET_JSON_OBJECT
2195
Mark Young39389872017-01-19 21:10:49 -07002196 // Add list entry
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002197 struct loader_layer_properties *props = NULL;
2198 if (!strcmp(type, "DEVICE")) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002199 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "Device layers are deprecated skipping this layer");
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002200 layer_node = layer_node->next;
2201 return;
2202 }
2203 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2204 // layers that must work with older loaders
2205 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2206 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002207 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002208 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002209 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002210 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002211 if (NULL == props) {
2212 // Error already triggered in loader_get_next_layer_property.
2213 return;
2214 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002215 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002216 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002217
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002218 if (props == NULL) {
2219 layer_node = layer_node->next;
2220 return;
2221 }
Jon Ashburn15315172015-07-07 15:06:25 -06002222
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002223 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2224 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2225
2226 char *fullpath = props->lib_name;
2227 char *rel_base;
2228 if (loader_platform_is_path(library_path)) {
Mark Young39389872017-01-19 21:10:49 -07002229 // A relative or absolute path
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002230 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2231 strcpy(name_copy, filename);
2232 rel_base = loader_platform_dirname(name_copy);
2233 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2234 } else {
Mark Young39389872017-01-19 21:10:49 -07002235 // A filename which is assumed in a system directory
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002236 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH, MAX_STRING_SIZE, fullpath);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002237 }
2238 props->info.specVersion = loader_make_version(api_version);
2239 props->info.implementationVersion = atoi(implementation_version);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002240 strncpy((char *)props->info.description, description, sizeof(props->info.description));
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002241 props->info.description[sizeof(props->info.description) - 1] = '\0';
2242 if (is_implicit) {
2243 if (!disable_environment || !disable_environment->child) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002244 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2245 "Didn't find required layer child value disable_environment"
2246 "in manifest JSON file, skipping this layer");
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002247 layer_node = layer_node->next;
2248 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002249 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002250 strncpy(props->disable_env_var.name, disable_environment->child->string, sizeof(props->disable_env_var.name));
2251 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] = '\0';
2252 strncpy(props->disable_env_var.value, disable_environment->child->valuestring, sizeof(props->disable_env_var.value));
2253 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002254 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002255
Jon Ashburn23d36b12016-02-02 17:47:28 -07002256/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002257* Now get all optional items and objects and put in list:
2258* functions
2259* instance_extensions
2260* device_extensions
2261* enable_environment (implicit layers only)
2262*/
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002263#define GET_JSON_OBJECT(node, var) \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002264 { var = cJSON_GetObjectItem(node, #var); }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002265#define GET_JSON_ITEM(node, var) \
2266 { \
2267 item = cJSON_GetObjectItem(node, #var); \
2268 if (item != NULL) { \
2269 temp = cJSON_Print(item); \
2270 if (temp != NULL) { \
2271 temp[strlen(temp) - 1] = '\0'; \
2272 var = loader_stack_alloc(strlen(temp) + 1); \
2273 strcpy(var, &temp[1]); \
2274 cJSON_Free(temp); \
2275 } \
2276 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002277 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002278
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002279 cJSON *instance_extensions, *device_extensions, *functions, *enable_environment;
Mark Young39389872017-01-19 21:10:49 -07002280 cJSON *entrypoints = NULL;
2281 char *vkGetInstanceProcAddr = NULL;
2282 char *vkGetDeviceProcAddr = NULL;
2283 char *vkNegotiateLoaderLayerInterfaceVersion = NULL;
2284 char *spec_version = NULL;
2285 char **entry_array = NULL;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002286 entrypoints = NULL;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002287 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002288
Mark Young39389872017-01-19 21:10:49 -07002289 // Layer interface functions
2290 // vkGetInstanceProcAddr
2291 // vkGetDeviceProcAddr
2292 // vkNegotiateLoaderLayerInterfaceVersion (starting with JSON file 1.1.0)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002293 GET_JSON_OBJECT(layer_node, functions)
2294 if (functions != NULL) {
Mark Young39389872017-01-19 21:10:49 -07002295 if (version.major > 1 || version.minor >= 1) {
2296 GET_JSON_ITEM(functions, vkNegotiateLoaderLayerInterfaceVersion)
2297 if (vkNegotiateLoaderLayerInterfaceVersion != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002298 strncpy(props->functions.str_negotiate_interface, vkNegotiateLoaderLayerInterfaceVersion,
Mark Young39389872017-01-19 21:10:49 -07002299 sizeof(props->functions.str_negotiate_interface));
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002300 props->functions.str_negotiate_interface[sizeof(props->functions.str_negotiate_interface) - 1] = '\0';
Mark Young39389872017-01-19 21:10:49 -07002301 } else {
2302 props->functions.str_negotiate_interface[0] = '\0';
2303 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002304 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2305 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
Mark Young39389872017-01-19 21:10:49 -07002306 if (vkGetInstanceProcAddr != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002307 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr, sizeof(props->functions.str_gipa));
Mark Young39389872017-01-19 21:10:49 -07002308 if (version.major > 1 || version.minor >= 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002309 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2310 "Indicating layer-specific vkGetInstanceProcAddr "
2311 "function is deprecated starting with JSON file "
2312 "version 1.1.0. Instead, use the new "
2313 "vkNegotiateLayerInterfaceVersion function to "
2314 "return the GetInstanceProcAddr function for this"
2315 "layer");
Mark Young39389872017-01-19 21:10:49 -07002316 }
2317 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002318 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
Mark Young39389872017-01-19 21:10:49 -07002319 if (vkGetDeviceProcAddr != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002320 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr, sizeof(props->functions.str_gdpa));
Mark Young39389872017-01-19 21:10:49 -07002321 if (version.major > 1 || version.minor >= 1) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002322 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2323 "Indicating layer-specific vkGetDeviceProcAddr "
2324 "function is deprecated starting with JSON file "
2325 "version 1.1.0. Instead, use the new "
2326 "vkNegotiateLayerInterfaceVersion function to "
2327 "return the GetDeviceProcAddr function for this"
2328 "layer");
Mark Young39389872017-01-19 21:10:49 -07002329 }
2330 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002331 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2332 }
Mark Young39389872017-01-19 21:10:49 -07002333
2334 // instance_extensions
2335 // array of {
2336 // name
2337 // spec_version
2338 // }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002339 GET_JSON_OBJECT(layer_node, instance_extensions)
2340 if (instance_extensions != NULL) {
2341 int count = cJSON_GetArraySize(instance_extensions);
2342 for (i = 0; i < count; i++) {
2343 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2344 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002345 if (name != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002346 strncpy(ext_prop.extensionName, name, sizeof(ext_prop.extensionName));
2347 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002348 }
Mark Young0ad83132016-06-30 13:02:42 -06002349 GET_JSON_ITEM(ext_item, spec_version)
2350 if (NULL != spec_version) {
2351 ext_prop.specVersion = atoi(spec_version);
2352 } else {
2353 ext_prop.specVersion = 0;
2354 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002355 bool ext_unsupported = wsi_unsupported_instance_extension(&ext_prop);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002356 if (!ext_unsupported) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002357 loader_add_to_ext_list(inst, &props->instance_extension_list, 1, &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002358 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002359 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002360 }
Mark Young39389872017-01-19 21:10:49 -07002361
2362 // device_extensions
2363 // array of {
2364 // name
2365 // spec_version
2366 // entrypoints
2367 // }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002368 GET_JSON_OBJECT(layer_node, device_extensions)
2369 if (device_extensions != NULL) {
2370 int count = cJSON_GetArraySize(device_extensions);
2371 for (i = 0; i < count; i++) {
2372 ext_item = cJSON_GetArrayItem(device_extensions, i);
2373 GET_JSON_ITEM(ext_item, name)
2374 GET_JSON_ITEM(ext_item, spec_version)
2375 if (name != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002376 strncpy(ext_prop.extensionName, name, sizeof(ext_prop.extensionName));
2377 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002378 }
Mark Young0ad83132016-06-30 13:02:42 -06002379 if (NULL != spec_version) {
2380 ext_prop.specVersion = atoi(spec_version);
2381 } else {
2382 ext_prop.specVersion = 0;
2383 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002384 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2385 GET_JSON_OBJECT(ext_item, entrypoints)
2386 int entry_count;
2387 if (entrypoints == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002388 loader_add_to_dev_ext_list(inst, &props->device_extension_list, &ext_prop, 0, NULL);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002389 continue;
2390 }
2391 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002392 if (entry_count) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002393 entry_array = (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002394 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002395 for (j = 0; j < entry_count; j++) {
2396 ext_item = cJSON_GetArrayItem(entrypoints, j);
2397 if (ext_item != NULL) {
2398 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002399 if (NULL == temp) {
2400 entry_array[j] = NULL;
2401 continue;
2402 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002403 temp[strlen(temp) - 1] = '\0';
2404 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2405 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002406 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002407 }
2408 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002409 loader_add_to_dev_ext_list(inst, &props->device_extension_list, &ext_prop, entry_count, entry_array);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002410 }
2411 }
2412 if (is_implicit) {
2413 GET_JSON_OBJECT(layer_node, enable_environment)
2414
2415 // enable_environment is optional
2416 if (enable_environment) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002417 strncpy(props->enable_env_var.name, enable_environment->child->string, sizeof(props->enable_env_var.name));
2418 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] = '\0';
2419 strncpy(props->enable_env_var.value, enable_environment->child->valuestring, sizeof(props->enable_env_var.value));
2420 props->enable_env_var.value[sizeof(props->enable_env_var.value) - 1] = '\0';
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002421 }
2422 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002423#undef GET_JSON_ITEM
2424#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002425}
2426
Mark Young39389872017-01-19 21:10:49 -07002427static inline bool is_valid_layer_json_version(const layer_json_version *layer_json) {
2428 // Supported versions are: 1.0.0, 1.0.1, and 1.1.0.
2429 if ((layer_json->major == 1 && layer_json->minor == 1 && layer_json->patch == 0) ||
2430 (layer_json->major == 1 && layer_json->minor == 0 && layer_json->patch < 2)) {
2431 return true;
2432 }
2433 return false;
2434}
2435
2436static inline bool layer_json_supports_layers_tag(const layer_json_version *layer_json) {
2437 // Supported versions started in 1.0.1, so anything newer
2438 if ((layer_json->major > 1 || layer_json->minor > 0 || layer_json->patch > 1)) {
2439 return true;
2440 }
2441 return false;
2442}
2443
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002444/**
2445 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2446 * file, add entry to the layer_list. Fill out the layer_properties in this list
2447 * entry from the input cJSON object.
2448 *
2449 * \returns
2450 * void
2451 * layer_list has a new entry and initialized accordingly.
2452 * If the json input object does not have all the required fields no entry
2453 * is added to the list.
2454 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002455static void loader_add_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_instance_list,
2456 cJSON *json, bool is_implicit, char *filename) {
Mark Young39389872017-01-19 21:10:49 -07002457 // The following Fields in layer manifest file that are required:
2458 // - “file_format_version”
2459 // - If more than one "layer" object are used, then the "layers" array is
2460 // requred
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002461
2462 cJSON *item, *layers_node, *layer_node;
Mark Young39389872017-01-19 21:10:49 -07002463 layer_json_version json_version;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002464 char *vers_tok;
2465 cJSON *disable_environment = NULL;
2466 item = cJSON_GetObjectItem(json, "file_format_version");
2467 if (item == NULL) {
2468 return;
2469 }
2470 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002471 if (NULL == file_vers) {
2472 return;
2473 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002474 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 -06002475 // Get the major/minor/and patch as integers for easier comparison
2476 vers_tok = strtok(file_vers, ".\"\n\r");
2477 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002478 json_version.major = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002479 vers_tok = strtok(NULL, ".\"\n\r");
2480 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002481 json_version.minor = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002482 vers_tok = strtok(NULL, ".\"\n\r");
2483 if (NULL != vers_tok) {
Mark Young39389872017-01-19 21:10:49 -07002484 json_version.patch = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002485 }
2486 }
2487 }
Mark Young39389872017-01-19 21:10:49 -07002488
2489 if (!is_valid_layer_json_version(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002490 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2491 "loader_add_layer_properties: %s invalid layer "
2492 " manifest file version %d.%d.%d. May cause errors.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002493 filename, json_version.major, json_version.minor, json_version.patch);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002494 }
Mark Young0ad83132016-06-30 13:02:42 -06002495 cJSON_Free(file_vers);
Mark Young39389872017-01-19 21:10:49 -07002496
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002497 // If "layers" is present, read in the array of layer objects
2498 layers_node = cJSON_GetObjectItem(json, "layers");
2499 if (layers_node != NULL) {
2500 int numItems = cJSON_GetArraySize(layers_node);
Mark Young39389872017-01-19 21:10:49 -07002501 if (!layer_json_supports_layers_tag(&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: \'layers\' tag not "
2504 "supported until file version 1.0.1, but %s is "
2505 "reporting version %s",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002506 filename, file_vers);
2507 }
2508 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2509 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2510 if (layer_node == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002511 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2512 "loader_add_layer_properties: Can not find "
2513 "\'layers\' array element %d object in manifest "
2514 "JSON file %s. Skipping this file",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002515 curLayer, filename);
2516 return;
2517 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002518 loader_read_json_layer(inst, layer_instance_list, layer_node, json_version, item, disable_environment, is_implicit,
2519 filename);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002520 }
2521 } else {
2522 // Otherwise, try to read in individual layers
2523 layer_node = cJSON_GetObjectItem(json, "layer");
2524 if (layer_node == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002525 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2526 "loader_add_layer_properties: Can not find \'layer\' "
2527 "object in manifest JSON file %s. Skipping this file.",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002528 filename);
2529 return;
2530 }
2531 // Loop through all "layer" objects in the file to get a count of them
2532 // first.
2533 uint16_t layer_count = 0;
2534 cJSON *tempNode = layer_node;
2535 do {
2536 tempNode = tempNode->next;
2537 layer_count++;
2538 } while (tempNode != NULL);
Mark Young39389872017-01-19 21:10:49 -07002539
2540 // Throw a warning if we encounter multiple "layer" objects in file
2541 // versions newer than 1.0.0. Having multiple objects with the same
2542 // name at the same level is actually a JSON standard violation.
2543 if (layer_count > 1 && layer_json_supports_layers_tag(&json_version)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002544 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2545 "loader_add_layer_properties: Multiple \'layer\' nodes"
2546 " are deprecated starting in file version \"1.0.1\". "
2547 "Please use \'layers\' : [] array instead in %s.",
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002548 filename);
2549 } else {
2550 do {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002551 loader_read_json_layer(inst, layer_instance_list, layer_node, json_version, item, disable_environment, is_implicit,
2552 filename);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002553 layer_node = layer_node->next;
2554 } while (layer_node != NULL);
2555 }
2556 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002557 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002558}
2559
2560/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002561 * Find the Vulkan library manifest files.
2562 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002563 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002564 * for a list of JSON manifest files. If env_override is non-NULL
2565 * and has a valid value. Then the location is ignored. Otherwise
2566 * location is used to look for manifest files. The location
2567 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002568 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002569 * directory to look at. It is expanded into the dir path
2570 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2571 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002572 *
2573 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002574 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002575 * A string list of manifest files to be opened in out_files param.
2576 * List has a pointer to string for each manifest filename.
2577 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002578 * Location or override string lists can be either files or directories as
2579 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002580 * | location | override
2581 * --------------------------------
2582 * Win ICD | files | files
2583 * Win Layer | files | dirs
2584 * Linux ICD | dirs | files
2585 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002586 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002587static VkResult loader_get_manifest_files(const struct loader_instance *inst, const char *env_override, const char *source_override,
2588 bool is_layer, bool warn_if_not_present, const char *location, const char *home_location,
2589 struct loader_manifest_files *out_files) {
Mark Young39389872017-01-19 21:10:49 -07002590 const char * override = NULL;
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002591 char *override_getenv = NULL;
Mark Young0ad83132016-06-30 13:02:42 -06002592 char *loc, *orig_loc = NULL;
2593 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002594 char *file, *next_file, *name;
2595 size_t alloced_count = 64;
2596 char full_path[2048];
2597 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002598 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002599 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002600 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002601
2602 out_files->count = 0;
2603 out_files->filename_list = NULL;
2604
Jamie Madill00c3c912016-04-06 18:26:46 -04002605 if (source_override != NULL) {
2606 override = source_override;
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002607 } else if (env_override != NULL) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002608#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002609 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002610 /* Don't allow setuid apps to use the env var: */
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002611 env_override = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002612 }
2613#endif
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002614 if (env_override != NULL) {
2615 override = override_getenv = loader_getenv(env_override, inst);
2616 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002617 }
2618
Jon Ashburnb6822212016-02-16 15:34:16 -07002619#if !defined(_WIN32)
2620 if (location == NULL && home_location == NULL) {
2621#else
2622 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002623 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002624#endif
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002625 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2626 "loader_get_manifest_files: Can not get manifest files with "
2627 "NULL location, env_override=%s",
Mark Youngb6399312017-01-10 14:22:15 -07002628 (env_override != NULL) ? env_override : "");
Mark Young0ad83132016-06-30 13:02:42 -06002629 res = VK_ERROR_INITIALIZATION_FAILED;
2630 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002631 }
2632
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002633#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002634 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002635#else
2636 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002637#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002638 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002639 // Also handle getting the location(s) from registry on Windows
2640 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002641 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002642 if (loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002643 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2644 "loader_get_manifest_files: Failed to allocate "
2645 "%d bytes for manifest file location.",
Mark Youngb6399312017-01-10 14:22:15 -07002646 strlen(location));
Mark Young0ad83132016-06-30 13:02:42 -06002647 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2648 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002649 }
2650 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002651#if defined(_WIN32)
Mark Young2c84c0c2017-01-13 10:27:03 -07002652 VkResult reg_result = loaderGetRegistryFiles(inst, loc, &reg);
2653 if (VK_SUCCESS != reg_result || NULL == reg) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002654 if (!is_layer) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002655 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2656 "loader_get_manifest_files: Registry lookup failed "
2657 "to get ICD manifest files. Possibly missing Vulkan"
2658 " driver?");
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002659 if (VK_SUCCESS == reg_result || VK_ERROR_OUT_OF_HOST_MEMORY == reg_result) {
Mark Young2c84c0c2017-01-13 10:27:03 -07002660 res = reg_result;
2661 } else {
2662 res = VK_ERROR_INCOMPATIBLE_DRIVER;
2663 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002664 } else {
Mark Youngf8c20102016-11-07 16:26:17 -07002665 if (warn_if_not_present) {
Mark Young2c84c0c2017-01-13 10:27:03 -07002666 // This is only a warning for layers
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002667 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2668 "loader_get_manifest_files: Registry lookup failed "
2669 "to get layer manifest files.");
Mark Youngf8c20102016-11-07 16:26:17 -07002670 }
Mark Young2c84c0c2017-01-13 10:27:03 -07002671 if (reg_result == VK_ERROR_OUT_OF_HOST_MEMORY) {
2672 res = reg_result;
2673 } else {
2674 // Return success for now since it's not critical for layers
2675 res = VK_SUCCESS;
2676 }
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002677 }
Mark Young0ad83132016-06-30 13:02:42 -06002678 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002679 }
Mark Young0ad83132016-06-30 13:02:42 -06002680 orig_loc = loc;
2681 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002682#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002683 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002684 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002685 if (loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002686 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2687 "loader_get_manifest_files: Failed to allocate space for "
2688 "override environment variable of length %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002689 strlen(override) + 1);
Mark Young0ad83132016-06-30 13:02:42 -06002690 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2691 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002692 }
2693 strcpy(loc, override);
2694 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002695
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002696 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002697 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 -07002698
Jon Ashburn2077e382015-06-29 11:25:34 -06002699 file = loc;
2700 while (*file) {
2701 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002702 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002703 sysdir = opendir(file);
2704 name = NULL;
2705 if (sysdir) {
2706 dent = readdir(sysdir);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002707 if (dent == NULL) break;
Jon Ashburn2077e382015-06-29 11:25:34 -06002708 name = &(dent->d_name[0]);
2709 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2710 name = full_path;
2711 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002712 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002713#if defined(_WIN32)
2714 name = file;
2715#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002716 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002717 char *dir;
2718 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002719 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002720 if (dir == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002721 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2722 "loader_get_manifest_files: Failed to allocate "
2723 "space for relative location path length %d",
Mark Youngb6399312017-01-10 14:22:15 -07002724 strlen(loc) + 1);
Mark Young0ad83132016-06-30 13:02:42 -06002725 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002726 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002727 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002728
2729 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2730
2731 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002732#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002733 }
2734 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002735 /* Look for files ending with ".json" suffix */
2736 uint32_t nlen = (uint32_t)strlen(name);
2737 const char *suf = name + nlen - 5;
2738 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2739 if (out_files->count == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002740 out_files->filename_list =
2741 loader_instance_heap_alloc(inst, alloced_count * sizeof(char *), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002742 } else if (out_files->count == alloced_count) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002743 out_files->filename_list =
2744 loader_instance_heap_realloc(inst, out_files->filename_list, alloced_count * sizeof(char *),
2745 alloced_count * sizeof(char *) * 2, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002746 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002747 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002748 if (out_files->filename_list == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002749 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2750 "loader_get_manifest_files: Failed to allocate "
2751 "space for manifest file name list");
Mark Young0ad83132016-06-30 13:02:42 -06002752 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2753 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002754 }
Mark Young0ad83132016-06-30 13:02:42 -06002755 out_files->filename_list[out_files->count] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002756 loader_instance_heap_alloc(inst, strlen(name) + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002757 if (out_files->filename_list[out_files->count] == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002758 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2759 "loader_get_manifest_files: Failed to allocate "
2760 "space for manifest file %d list",
Mark Youngb6399312017-01-10 14:22:15 -07002761 out_files->count);
Mark Young0ad83132016-06-30 13:02:42 -06002762 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2763 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002764 }
2765 strcpy(out_files->filename_list[out_files->count], name);
2766 out_files->count++;
2767 } else if (!list_is_dirs) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002768 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, "Skipping manifest file %s, file name must end in .json",
2769 name);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002770 }
2771 if (list_is_dirs) {
2772 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002773 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002774 break;
Mark Young0ad83132016-06-30 13:02:42 -06002775 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002776 name = &(dent->d_name[0]);
2777 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2778 name = full_path;
2779 } else {
2780 break;
2781 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002782 }
Mark Young0ad83132016-06-30 13:02:42 -06002783 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002784 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002785 sysdir = NULL;
2786 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002787 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002788#if !defined(_WIN32)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002789 if (home_location != NULL && (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002790 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2791 size_t len;
2792 if (xdgdatahome != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002793 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 + strlen(home_location));
Jon Ashburn67e262e2016-02-18 12:45:39 -07002794 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002795 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2796 "loader_get_manifest_files: Failed to allocate "
2797 "space for manifest file XDG Home location");
Mark Young0ad83132016-06-30 13:02:42 -06002798 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2799 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002800 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002801 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002802 // Add directory separator if needed
2803 if (home_location[0] != DIRECTORY_SYMBOL) {
2804 len = strlen(home_loc);
2805 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002806 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002807 }
2808 strcat(home_loc, home_location);
2809 file = home_loc;
2810 next_file = loader_get_next_path(file);
2811 home_location = NULL;
2812
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002813 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
2814 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002815 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002816
2817 } else {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002818 char *home = secure_getenv("HOME");
2819 if (home != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002820 char *home_loc = loader_stack_alloc(strlen(home) + 16 + strlen(home_location));
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002821 if (home_loc == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002822 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2823 "loader_get_manifest_files: Failed to allocate "
2824 "space for manifest file Home location");
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002825 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2826 goto out;
2827 }
2828 strcpy(home_loc, home);
2829
2830 len = strlen(home);
2831 if (home[len] != DIRECTORY_SYMBOL) {
2832 home_loc[len] = DIRECTORY_SYMBOL;
2833 home_loc[len + 1] = '\0';
2834 }
2835 strcat(home_loc, ".local/share");
2836
2837 if (home_location[0] != DIRECTORY_SYMBOL) {
2838 len = strlen(home_loc);
2839 home_loc[len] = DIRECTORY_SYMBOL;
2840 home_loc[len + 1] = '\0';
2841 }
2842 strcat(home_loc, home_location);
2843 file = home_loc;
2844 next_file = loader_get_next_path(file);
2845 home_location = NULL;
2846
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002847 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching the following path for manifest files: %s\n",
2848 home_loc);
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002849 list_is_dirs = true;
2850 } else {
2851 // without knowing HOME, we just.. give up
2852 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002853 }
2854 }
2855#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002856 }
Mark Young0ad83132016-06-30 13:02:42 -06002857
2858out:
2859 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2860 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2861 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2862 }
2863 loader_instance_heap_free(inst, out_files->filename_list);
2864 out_files->count = 0;
2865 out_files->filename_list = NULL;
2866 }
2867
2868 if (NULL != sysdir) {
2869 closedir(sysdir);
2870 }
2871
Frank Henigmanb1c27cb2016-11-24 20:02:09 -05002872 if (override_getenv != NULL) {
2873 loader_free_getenv(override_getenv, inst);
2874 }
2875
Mark Young0ad83132016-06-30 13:02:42 -06002876 if (NULL != reg && reg != orig_loc) {
2877 loader_instance_heap_free(inst, reg);
2878 }
2879 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002880}
2881
Jon Ashburn23d36b12016-02-02 17:47:28 -07002882void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002883
Jon Ashburn23d36b12016-02-02 17:47:28 -07002884void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002885/**
2886 * Try to find the Vulkan ICD driver(s).
2887 *
2888 * This function scans the default system loader path(s) or path
2889 * specified by the \c VK_ICD_FILENAMES environment variable in
2890 * order to find loadable VK ICDs manifest files. From these
2891 * manifest files it finds the ICD libraries.
2892 *
2893 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002894 * Vulkan result
2895 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002896 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002897VkResult loader_icd_scan(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002898 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002899 uint16_t file_major_vers = 0;
2900 uint16_t file_minor_vers = 0;
2901 uint16_t file_patch_vers = 0;
2902 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002903 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002904 VkResult res = VK_SUCCESS;
2905 bool lockedMutex = false;
2906 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002907 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002908
Mark Young0ad83132016-06-30 13:02:42 -06002909 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2910
Mark Young0153e0b2016-11-03 14:27:13 -06002911 res = loader_scanned_icd_init(inst, icd_tramp_list);
Mark Young0ad83132016-06-30 13:02:42 -06002912 if (VK_SUCCESS != res) {
2913 goto out;
2914 }
2915
Jon Ashburn2077e382015-06-29 11:25:34 -06002916 // Get a list of manifest files for ICDs
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002917 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false, true, DEFAULT_VK_DRIVERS_INFO, HOME_VK_DRIVERS_INFO,
2918 &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06002919 if (VK_SUCCESS != res || manifest_files.count == 0) {
2920 goto out;
2921 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002922 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002923 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002924 for (uint32_t i = 0; i < manifest_files.count; i++) {
2925 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002926 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002927 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002928 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002929
Mark Young3a587792016-08-19 15:25:08 -06002930 res = loader_get_json(inst, file_str, &json);
2931 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2932 break;
2933 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002934 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002935 }
Mark Young3a587792016-08-19 15:25:08 -06002936
Jon Ashburn005617f2015-11-17 17:35:40 -07002937 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002938 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002939 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002940 if (num_good_icds == 0) {
2941 res = VK_ERROR_INITIALIZATION_FAILED;
2942 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002943 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2944 "loader_icd_scan: ICD JSON %s does not have a"
2945 " \'file_format_version\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07002946 file_str);
Derrick Owens62e16ef2016-09-09 15:49:07 -04002947 cJSON_Delete(json);
2948 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002949 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002950 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002951 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002952 if (NULL == file_vers) {
2953 // Only reason the print can fail is if there was an allocation
2954 // issue
Mark Young3a587792016-08-19 15:25:08 -06002955 if (num_good_icds == 0) {
2956 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2957 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002958 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2959 "loader_icd_scan: Failed retrieving ICD JSON %s"
2960 " \'file_format_version\' field. Skipping ICD JSON",
Mark Youngb6399312017-01-10 14:22:15 -07002961 file_str);
Derrick Owenscd92b8b2016-09-09 15:45:13 -04002962 cJSON_Delete(json);
2963 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002964 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002965 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07002966 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 -06002967 // Get the major/minor/and patch as integers for easier comparison
2968 vers_tok = strtok(file_vers, ".\"\n\r");
2969 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002970 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002971 vers_tok = strtok(NULL, ".\"\n\r");
2972 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002973 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002974 vers_tok = strtok(NULL, ".\"\n\r");
2975 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002976 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002977 }
2978 }
2979 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002980 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002981 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2982 "loader_icd_scan: Unexpected manifest file version "
2983 "(expected 1.0.0 or 1.0.1), may cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002984 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002985 itemICD = cJSON_GetObjectItem(json, "ICD");
2986 if (itemICD != NULL) {
2987 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002988 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002989 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002990 if (!temp || strlen(temp) == 0) {
Mark Young3a587792016-08-19 15:25:08 -06002991 if (num_good_icds == 0) {
2992 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2993 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002994 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2995 "loader_icd_scan: Failed retrieving ICD JSON %s"
2996 " \'library_path\' field. Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07002997 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002998 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002999 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003000 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003001 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06003002 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003003 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06003004 temp[strlen(temp) - 1] = '\0';
3005 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06003006 if (NULL == library_path) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003007 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3008 "loader_icd_scan: Failed to allocate space for "
3009 "ICD JSON %s \'library_path\' value. Skipping "
3010 "ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003011 file_str);
Mark Young3a587792016-08-19 15:25:08 -06003012 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3013 cJSON_Free(temp);
3014 cJSON_Delete(json);
3015 json = NULL;
3016 goto out;
3017 }
Jon Ashburn86251302015-08-25 16:48:24 -06003018 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003019 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003020 if (strlen(library_path) == 0) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003021 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3022 "loader_icd_scan: ICD JSON %s \'library_path\'"
3023 " field is empty. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003024 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003025 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003026 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003027 continue;
3028 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003029 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003030 // Print out the paths being searched if debugging is enabled
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003031 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Searching for ICD drivers named %s, using default dir %s",
3032 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003033 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003034 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003035 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3036 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003037 strcpy(name_copy, file_str);
3038 rel_base = loader_platform_dirname(name_copy);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003039 loader_expand_path(library_path, rel_base, sizeof(fullpath), fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003040 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003041 // a filename which is assumed in a system directory
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003042 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH, sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003043 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003044
3045 uint32_t vers = 0;
3046 item = cJSON_GetObjectItem(itemICD, "api_version");
3047 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003048 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003049 if (NULL == temp) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003050 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3051 "loader_icd_scan: Failed retrieving ICD JSON %s"
3052 " \'api_version\' field. Skipping ICD JSON.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003053 file_str);
Mark Youngb6399312017-01-10 14:22:15 -07003054
Mark Young0ad83132016-06-30 13:02:42 -06003055 // Only reason the print can fail is if there was an
3056 // allocation issue
Mark Youngb6399312017-01-10 14:22:15 -07003057 if (num_good_icds == 0) {
3058 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3059 }
3060
3061 cJSON_Free(temp);
3062 cJSON_Delete(json);
3063 json = NULL;
3064 continue;
Mark Young0ad83132016-06-30 13:02:42 -06003065 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003066 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003067 cJSON_Free(temp);
Mark Youngb6399312017-01-10 14:22:15 -07003068 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003069 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3070 "loader_icd_scan: ICD JSON %s does not have an"
3071 " \'api_version\' field.",
Mark Youngb6399312017-01-10 14:22:15 -07003072 file_str);
Jon Ashburn005617f2015-11-17 17:35:40 -07003073 }
Mark Youngb6399312017-01-10 14:22:15 -07003074
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003075 res = loader_scanned_icd_add(inst, icd_tramp_list, fullpath, vers);
Mark Young3a587792016-08-19 15:25:08 -06003076 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003077 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3078 "loader_icd_scan: Failed to add ICD JSON %s. "
3079 " Skipping ICD JSON.",
Mark Youngb6399312017-01-10 14:22:15 -07003080 fullpath);
Mark Youngb6399312017-01-10 14:22:15 -07003081 continue;
Mark Young3a587792016-08-19 15:25:08 -06003082 }
3083 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003084 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003085 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3086 "loader_icd_scan: Failed to find \'library_path\' "
3087 "object in ICD JSON file %s. Skipping ICD JSON.",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003088 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003089 }
3090 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003091 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3092 "loader_icd_scan: Can not find \'ICD\' object in ICD JSON "
3093 "file %s. Skipping ICD JSON",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003094 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003095 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003096
Mark Young0ad83132016-06-30 13:02:42 -06003097 cJSON_Delete(json);
3098 json = NULL;
3099 }
3100
3101out:
Mark Youngb6399312017-01-10 14:22:15 -07003102
Mark Young0ad83132016-06-30 13:02:42 -06003103 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003104 cJSON_Delete(json);
3105 }
Mark Young0ad83132016-06-30 13:02:42 -06003106 if (NULL != manifest_files.filename_list) {
3107 for (uint32_t i = 0; i < manifest_files.count; i++) {
3108 if (NULL != manifest_files.filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003109 loader_instance_heap_free(inst, manifest_files.filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003110 }
3111 }
3112 loader_instance_heap_free(inst, manifest_files.filename_list);
3113 }
3114 if (lockedMutex) {
3115 loader_platform_thread_unlock_mutex(&loader_json_lock);
3116 }
3117 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003118}
3119
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003120void loader_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003121 char *file_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003122 struct loader_manifest_files manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003123 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003124 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003125 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003126
Mark Young0ad83132016-06-30 13:02:42 -06003127 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3128
3129 // Get a list of manifest files for explicit layers
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003130 if (VK_SUCCESS != loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH, true, true, DEFAULT_VK_ELAYERS_INFO,
3131 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
Mark Young0ad83132016-06-30 13:02:42 -06003132 goto out;
3133 }
3134
3135 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003136 // Pass NULL for environment variable override - implicit layers are not
3137 // overridden by LAYERS_PATH_ENV
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003138 if (VK_SUCCESS != loader_get_manifest_files(inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
Mark Youngf8c20102016-11-07 16:26:17 -07003139 &manifest_files[1])) {
Mark Young0ad83132016-06-30 13:02:42 -06003140 goto out;
3141 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003142
Mark Young0ad83132016-06-30 13:02:42 -06003143 // Make sure we have at least one layer, if not, go ahead and return
3144 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3145 goto out;
3146 }
3147
3148 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003149 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003150
Jon Ashburn6461ef22015-09-22 13:11:00 -06003151 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003152 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003153 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003154 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003155 file_str = manifest_files[implicit].filename_list[i];
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003156 if (file_str == NULL) continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003157
Jon Ashburn075ce432015-12-17 17:38:24 -07003158 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003159 VkResult res = loader_get_json(inst, file_str, &json);
3160 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3161 break;
3162 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003163 continue;
3164 }
3165
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003166 loader_add_layer_properties(inst, instance_layers, json, (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003167 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003168 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003169 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003170
3171 // add a meta layer for validation if the validation layers are all present
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003172 loader_add_layer_property_meta(inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]), std_validation_names,
3173 instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003174
Mark Young0ad83132016-06-30 13:02:42 -06003175out:
3176
3177 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3178 if (NULL != manifest_files[manFile].filename_list) {
3179 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3180 if (NULL != manifest_files[manFile].filename_list[i]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003181 loader_instance_heap_free(inst, manifest_files[manFile].filename_list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003182 }
3183 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003184 loader_instance_heap_free(inst, manifest_files[manFile].filename_list);
Mark Young0ad83132016-06-30 13:02:42 -06003185 }
3186 }
3187 if (lockedMutex) {
3188 loader_platform_thread_unlock_mutex(&loader_json_lock);
3189 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003190}
3191
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003192void loader_implicit_layer_scan(const struct loader_instance *inst, struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003193 char *file_str;
3194 struct loader_manifest_files manifest_files;
3195 cJSON *json;
3196 uint32_t i;
3197
3198 // Pass NULL for environment variable override - implicit layers are not
3199 // overridden by LAYERS_PATH_ENV
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003200 VkResult res =
3201 loader_get_manifest_files(inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO, &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003202 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003203 return;
3204 }
3205
3206 /* cleanup any previously scanned libraries */
3207 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003208
3209 loader_platform_thread_lock_mutex(&loader_json_lock);
3210
3211 for (i = 0; i < manifest_files.count; i++) {
3212 file_str = manifest_files.filename_list[i];
3213 if (file_str == NULL) {
3214 continue;
3215 }
3216
3217 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003218 res = loader_get_json(inst, file_str, &json);
3219 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3220 break;
3221 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003222 continue;
3223 }
3224
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003225 loader_add_layer_properties(inst, instance_layers, json, true, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003226
Mark Young0ad83132016-06-30 13:02:42 -06003227 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003228 cJSON_Delete(json);
3229 }
Mark Young0ad83132016-06-30 13:02:42 -06003230 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003231
3232 // add a meta layer for validation if the validation layers are all present
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003233 loader_add_layer_property_meta(inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]), std_validation_names,
3234 instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003235
3236 loader_platform_thread_unlock_mutex(&loader_json_lock);
3237}
3238
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003239static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003240 // inst is not wrapped
3241 if (inst == VK_NULL_HANDLE) {
3242 return NULL;
3243 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003244 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003245 void *addr;
3246
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003247 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003248
3249 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003250 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003251 if (found_name) {
3252 return addr;
3253 }
3254
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003255 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, true, NULL, &addr)) return addr;
Mark Young39389872017-01-19 21:10:49 -07003256
3257 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003258 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 -07003259 return NULL;
3260}
3261
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003262static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpdpa_instance_terminator(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003263 // inst is not wrapped
3264 if (inst == VK_NULL_HANDLE) {
3265 return NULL;
3266 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003267 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Mark Young39389872017-01-19 21:10:49 -07003268 void *addr;
3269
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003270 if (disp_table == NULL) return NULL;
Mark Young39389872017-01-19 21:10:49 -07003271
3272 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003273 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Mark Young39389872017-01-19 21:10:49 -07003274 if (found_name) {
3275 return addr;
3276 }
3277
3278 // Get the terminator, but don't perform checking since it should already
3279 // have been setup if we get here.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003280 if (loader_phys_dev_ext_gpa(loader_get_instance(inst), pName, false, NULL, &addr)) {
Mark Young39389872017-01-19 21:10:49 -07003281 return addr;
3282 }
3283
3284 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003285 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 -07003286 return NULL;
3287}
3288
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003289static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Mark Young39389872017-01-19 21:10:49 -07003290 if (!strcmp(pName, "vkGetInstanceProcAddr")) {
3291 return (PFN_vkVoidFunction)loader_gpa_instance_internal;
3292 }
3293 if (!strcmp(pName, "vk_layerGetPhysicalDeviceProcAddr")) {
3294 return (PFN_vkVoidFunction)loader_gpdpa_instance_terminator;
3295 }
3296 if (!strcmp(pName, "vkCreateInstance")) {
3297 return (PFN_vkVoidFunction)terminator_CreateInstance;
3298 }
3299 if (!strcmp(pName, "vkCreateDevice")) {
3300 return (PFN_vkVoidFunction)terminator_CreateDevice;
3301 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003302
Jon Ashburn27cd5842015-05-12 17:26:48 -06003303 // inst is not wrapped
3304 if (inst == VK_NULL_HANDLE) {
3305 return NULL;
3306 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003307 VkLayerInstanceDispatchTable *disp_table = *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003308 void *addr;
3309
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003310 if (disp_table == NULL) return NULL;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003311
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003312 bool found_name;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003313 addr = loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003314 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003315 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003316 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003317
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003318 // Don't call down the chain, this would be an infinite loop
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003319 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 -07003320 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003321}
3322
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003323VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL loader_gpa_device_internal(VkDevice device, const char *pName) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003324 struct loader_device *dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003325 struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
Mark Young16573c72016-06-28 10:52:43 -06003326
Mark Young65cb3662016-11-07 13:27:02 -07003327 // NOTE: Device Funcs needing Trampoline/Terminator.
3328 // Overrides for device functions needing a trampoline and
3329 // a terminator because certain device entry-points still need to go
3330 // through a terminator before hitting the ICD. This could be for
3331 // several reasons, but the main one is currently unwrapping an
3332 // object before passing the appropriate info along to the ICD.
3333 // This is why we also have to override the direct ICD call to
3334 // vkGetDeviceProcAddr to intercept those calls.
3335 if (!strcmp(pName, "vkGetDeviceProcAddr")) {
3336 return (PFN_vkVoidFunction)loader_gpa_device_internal;
3337 } else if (!strcmp(pName, "vkCreateSwapchainKHR")) {
Mark Young16573c72016-06-28 10:52:43 -06003338 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
Mark Young39389872017-01-19 21:10:49 -07003339 } else if (!strcmp(pName, "vkCreateSharedSwapchainsKHR")) {
3340 return (PFN_vkVoidFunction)terminator_vkCreateSharedSwapchainsKHR;
Mark Young65cb3662016-11-07 13:27:02 -07003341 } else if (!strcmp(pName, "vkDebugMarkerSetObjectTagEXT")) {
3342 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectTagEXT;
3343 } else if (!strcmp(pName, "vkDebugMarkerSetObjectNameEXT")) {
3344 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectNameEXT;
Mark Young16573c72016-06-28 10:52:43 -06003345 }
3346
Mark Young0153e0b2016-11-03 14:27:13 -06003347 return icd_term->GetDeviceProcAddr(device, pName);
Piers Daniell295fe402016-03-29 11:51:11 -06003348}
3349
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003350/**
3351 * Initialize device_ext dispatch table entry as follows:
3352 * If dev == NULL find all logical devices created within this instance and
3353 * init the entry (given by idx) in the ext dispatch table.
3354 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003355 * The initialization value is gotten by calling down the device chain with
3356 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003357 * If GDPA returns NULL then don't initialize the dispatch table entry.
3358 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003359static 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 -07003360 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003361
Jon Ashburn23d36b12016-02-02 17:47:28 -07003362{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003363 void *gdpa_value;
3364 if (dev != NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003365 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(dev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003366 if (gdpa_value != NULL) dev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003367 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003368 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 -06003369 struct loader_device *ldev = icd_term->logical_device_list;
Karl Schultz2558bd32016-02-24 14:39:39 -07003370 while (ldev) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003371 gdpa_value = ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(ldev->chain_device, funcName);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003372 if (gdpa_value != NULL) ldev->loader_dispatch.ext_dispatch.dev_ext[idx] = (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003373 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003374 }
3375 }
3376 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003377}
3378
3379/**
3380 * Find all dev extension in the hash table and initialize the dispatch table
3381 * for dev for each of those extension entrypoints found in hash table.
3382
3383 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003384void loader_init_dispatch_dev_ext(struct loader_instance *inst, struct loader_device *dev) {
Mark Young39389872017-01-19 21:10:49 -07003385 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3386 if (inst->dev_ext_disp_hash[i].func_name != NULL)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003387 loader_init_dispatch_dev_ext_entry(inst, dev, i, inst->dev_ext_disp_hash[i].func_name);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003388 }
3389}
3390
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003391static bool loader_check_icds_for_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young0153e0b2016-11-03 14:27:13 -06003392 struct loader_icd_term *icd_term;
3393 icd_term = inst->icd_terms;
3394 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003395 if (icd_term->scanned_icd->GetInstanceProcAddr(icd_term->instance, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003396 // this icd supports funcName
3397 return true;
Mark Young0153e0b2016-11-03 14:27:13 -06003398 icd_term = icd_term->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003399 }
3400
3401 return false;
3402}
3403
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003404static 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 -07003405 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003406 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003407 // Iterate over the extensions.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003408 const struct loader_device_extension_list *const extensions = &(layers->list[layer].device_extension_list);
3409 for (uint32_t extension = 0; extension < extensions->count; ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003410 // Iterate over the entry points.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003411 const struct loader_dev_ext_props *const property = &(extensions->list[extension]);
3412 for (uint32_t entry = 0; entry < property->entrypoint_count; ++entry) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003413 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003414 return true;
3415 }
3416 }
3417 }
3418 }
3419
3420 return false;
3421}
3422
Jon Ashburn23d36b12016-02-02 17:47:28 -07003423static void loader_free_dev_ext_table(struct loader_instance *inst) {
Mark Young39389872017-01-19 21:10:49 -07003424 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
3425 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].func_name);
3426 loader_instance_heap_free(inst, inst->dev_ext_disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003427 }
Mark Young39389872017-01-19 21:10:49 -07003428 memset(inst->dev_ext_disp_hash, 0, sizeof(inst->dev_ext_disp_hash));
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003429}
3430
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003431static bool loader_add_dev_ext_table(struct loader_instance *inst, uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003432 uint32_t i;
3433 uint32_t idx = *ptr_idx;
Mark Young39389872017-01-19 21:10:49 -07003434 struct loader_dispatch_hash_list *list = &inst->dev_ext_disp_hash[idx].list;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003435
Mark Young39389872017-01-19 21:10:49 -07003436 if (!inst->dev_ext_disp_hash[idx].func_name) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003437 // no entry here at this idx, so use it
3438 assert(list->capacity == 0);
Mark Young39389872017-01-19 21:10:49 -07003439 inst->dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003440 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003441 if (inst->dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003442 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3443 "loader_add_dev_ext_table: Failed to allocate memory "
3444 "for func_name %s",
Mark Youngb6399312017-01-10 14:22:15 -07003445 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003446 return false;
3447 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003448 strncpy(inst->dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003449 return true;
3450 }
3451
3452 // check for enough capacity
3453 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003454 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003455 if (list->index == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003456 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3457 "loader_add_dev_ext_table: Failed to allocate memory "
3458 "for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003459 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003460 return false;
3461 }
3462 list->capacity = 8 * sizeof(*(list->index));
3463 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003464 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3465 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003466 if (list->index == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003467 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3468 "loader_add_dev_ext_table: Failed to reallocate memory "
3469 "for list index",
Mark Youngb6399312017-01-10 14:22:15 -07003470 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003471 return false;
3472 }
3473 list->capacity *= 2;
3474 }
3475
Jon Ashburn23d36b12016-02-02 17:47:28 -07003476 // find an unused index in the hash table and use it
Mark Young39389872017-01-19 21:10:49 -07003477 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003478 do {
Mark Young39389872017-01-19 21:10:49 -07003479 if (!inst->dev_ext_disp_hash[i].func_name) {
3480 assert(inst->dev_ext_disp_hash[i].list.capacity == 0);
3481 inst->dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003482 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003483 if (inst->dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003484 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3485 "loader_add_dev_ext_table: Failed to allocate memory "
3486 "for func_name %s",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003487 funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003488 return false;
3489 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003490 strncpy(inst->dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003491 list->index[list->count] = i;
3492 list->count++;
3493 *ptr_idx = i;
3494 return true;
3495 }
Mark Young39389872017-01-19 21:10:49 -07003496 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003497 } while (i != idx);
3498
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003499 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3500 "loader_add_dev_ext_table: Could not insert into hash table; is "
3501 "it full?");
Mark Youngb6399312017-01-10 14:22:15 -07003502
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003503 return false;
3504}
3505
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003506static bool loader_name_in_dev_ext_table(struct loader_instance *inst, uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003507 uint32_t alt_idx;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003508 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 -07003509
3510 // funcName wasn't at the primary spot in the hash table
3511 // search the list of secondary locations (shallow search, not deep search)
Mark Young39389872017-01-19 21:10:49 -07003512 for (uint32_t i = 0; i < inst->dev_ext_disp_hash[*idx].list.count; i++) {
3513 alt_idx = inst->dev_ext_disp_hash[*idx].list.index[i];
3514 if (!strcmp(inst->dev_ext_disp_hash[*idx].func_name, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003515 *idx = alt_idx;
3516 return true;
3517 }
3518 }
3519
3520 return false;
3521}
3522
3523/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003524 * This function returns generic trampoline code address for unknown entry
3525 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003526 * Presumably, these unknown entry points (as given by funcName) are device
3527 * extension entrypoints. A hash table is used to keep a list of unknown entry
3528 * points and their mapping to the device extension dispatch table
3529 * (struct loader_dev_ext_dispatch_table).
3530 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003531 * For a given entry point string (funcName), if an existing mapping is found
3532 * the
3533 * trampoline address for that mapping is returned. Otherwise, this unknown
3534 * entry point
3535 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3536 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003537 * new entry in the hash table is initialized and that trampoline address for
3538 * the new entry is returned. Null is returned if the hash table is full or
3539 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3540 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003541void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003542 uint32_t idx;
3543 uint32_t seed = 0;
3544
Mark Young39389872017-01-19 21:10:49 -07003545 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003546
3547 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3548 // found funcName already in hash
3549 return loader_get_dev_ext_trampoline(idx);
3550
3551 // Check if funcName is supported in either ICDs or a layer library
Mark Young39389872017-01-19 21:10:49 -07003552 if (!loader_check_icds_for_dev_ext_address(inst, funcName) &&
3553 !loader_check_layer_list_for_dev_ext_address(&inst->instance_layer_list, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003554 // if support found in layers continue on
3555 return NULL;
3556 }
3557
3558 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3559 // successfully added new table entry
3560 // init any dev dispatch table entrys as needed
3561 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3562 return loader_get_dev_ext_trampoline(idx);
3563 }
3564
3565 return NULL;
3566}
3567
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003568static bool loader_check_icds_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
Mark Young39389872017-01-19 21:10:49 -07003569 struct loader_icd_term *icd_term;
3570 icd_term = inst->icd_terms;
3571 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003572 if (icd_term->scanned_icd->interface_version >= MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION &&
3573 icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName))
Mark Young39389872017-01-19 21:10:49 -07003574 // this icd supports funcName
3575 return true;
3576 icd_term = icd_term->next;
3577 }
3578
3579 return false;
3580}
3581
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003582static bool loader_check_layer_list_for_phys_dev_ext_address(struct loader_instance *inst, const char *funcName) {
3583 struct loader_layer_properties *layer_prop_list = inst->activated_layer_list.list;
Mark Young39389872017-01-19 21:10:49 -07003584 for (uint32_t layer = 0; layer < inst->activated_layer_list.count; ++layer) {
3585 // If this layer supports the vk_layerGetPhysicalDeviceProcAddr, then call
3586 // it and see if it returns a valid pointer for this function name.
3587 if (layer_prop_list[layer].interface_version > 1) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003588 const struct loader_layer_functions *const functions = &(layer_prop_list[layer].functions);
Mark Young39389872017-01-19 21:10:49 -07003589 if (NULL != functions->get_physical_device_proc_addr &&
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003590 NULL != functions->get_physical_device_proc_addr((VkInstance)inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003591 return true;
3592 }
3593 }
3594 }
3595
3596 return false;
3597}
3598
Mark Young39389872017-01-19 21:10:49 -07003599static void loader_free_phys_dev_ext_table(struct loader_instance *inst) {
3600 for (uint32_t i = 0; i < MAX_NUM_UNKNOWN_EXTS; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003601 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].func_name);
3602 loader_instance_heap_free(inst, inst->phys_dev_ext_disp_hash[i].list.index);
Mark Young39389872017-01-19 21:10:49 -07003603 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003604 memset(inst->phys_dev_ext_disp_hash, 0, sizeof(inst->phys_dev_ext_disp_hash));
Mark Young39389872017-01-19 21:10:49 -07003605}
3606
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003607static 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 -07003608 uint32_t i;
3609 uint32_t idx = *ptr_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003610 struct loader_dispatch_hash_list *list = &inst->phys_dev_ext_disp_hash[idx].list;
Mark Young39389872017-01-19 21:10:49 -07003611
3612 if (!inst->phys_dev_ext_disp_hash[idx].func_name) {
3613 // no entry here at this idx, so use it
3614 assert(list->capacity == 0);
3615 inst->phys_dev_ext_disp_hash[idx].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003616 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003617 if (inst->phys_dev_ext_disp_hash[idx].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003618 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3619 "loader_add_phys_dev_ext_table() can't allocate memory for "
3620 "func_name");
Mark Young39389872017-01-19 21:10:49 -07003621 return false;
3622 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003623 strncpy(inst->phys_dev_ext_disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07003624 return true;
3625 }
3626
3627 // check for enough capacity
3628 if (list->capacity == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003629 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003630 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003631 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 -07003632 return false;
3633 }
3634 list->capacity = 8 * sizeof(*(list->index));
3635 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003636 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity, list->capacity * 2,
3637 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003638 if (list->index == NULL) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003639 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 -07003640 return false;
3641 }
3642 list->capacity *= 2;
3643 }
3644
3645 // find an unused index in the hash table and use it
3646 i = (idx + 1) % MAX_NUM_UNKNOWN_EXTS;
3647 do {
3648 if (!inst->phys_dev_ext_disp_hash[i].func_name) {
3649 assert(inst->phys_dev_ext_disp_hash[i].list.capacity == 0);
3650 inst->phys_dev_ext_disp_hash[i].func_name =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003651 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young39389872017-01-19 21:10:49 -07003652 if (inst->phys_dev_ext_disp_hash[i].func_name == NULL) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003653 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3654 "loader_add_dev_ext_table() can't rallocate "
3655 "func_name memory");
Mark Young39389872017-01-19 21:10:49 -07003656 return false;
3657 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003658 strncpy(inst->phys_dev_ext_disp_hash[i].func_name, funcName, strlen(funcName) + 1);
Mark Young39389872017-01-19 21:10:49 -07003659 list->index[list->count] = i;
3660 list->count++;
3661 *ptr_idx = i;
3662 return true;
3663 }
3664 i = (i + 1) % MAX_NUM_UNKNOWN_EXTS;
3665 } while (i != idx);
3666
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003667 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3668 "loader_add_phys_dev_ext_table() couldn't insert into hash table; is "
3669 "it full?");
Mark Young39389872017-01-19 21:10:49 -07003670 return false;
3671}
3672
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003673static 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 -07003674 uint32_t alt_idx;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003675 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 -07003676 return true;
3677
3678 // funcName wasn't at the primary spot in the hash table
3679 // search the list of secondary locations (shallow search, not deep search)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003680 for (uint32_t i = 0; i < inst->phys_dev_ext_disp_hash[*idx].list.count; i++) {
Mark Young39389872017-01-19 21:10:49 -07003681 alt_idx = inst->phys_dev_ext_disp_hash[*idx].list.index[i];
3682 if (!strcmp(inst->phys_dev_ext_disp_hash[*idx].func_name, funcName)) {
3683 *idx = alt_idx;
3684 return true;
3685 }
3686 }
3687
3688 return false;
3689}
3690
3691// This function returns a generic trampoline and/or terminator function
3692// address for any unknown physical device extension commands. A hash
3693// table is used to keep a list of unknown entry points and their
3694// mapping to the physical device extension dispatch table (struct
3695// loader_phys_dev_ext_dispatch_table).
3696// For a given entry point string (funcName), if an existing mapping is
3697// found, then the trampoline address for that mapping is returned in
3698// tramp_addr (if it is not NULL) and the terminator address for that
3699// mapping is returned in term_addr (if it is not NULL). Otherwise,
3700// this unknown entry point has not been seen yet.
3701// If it has not been seen before, and perform_checking is 'true',
3702// check if a layer or and ICD supports it. If so then a new entry in
3703// the hash table is initialized and the trampoline and/or terminator
3704// addresses are returned.
3705// Null is returned if the hash table is full or if no discovered layer or
3706// ICD returns a non-NULL GetProcAddr for it.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003707bool 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 -07003708 void **term_addr) {
3709 uint32_t idx;
3710 uint32_t seed = 0;
3711 bool success = false;
3712
3713 if (inst == NULL) {
3714 goto out;
3715 }
3716
3717 if (NULL != tramp_addr) {
3718 *tramp_addr = NULL;
3719 }
3720 if (NULL != term_addr) {
3721 *term_addr = NULL;
3722 }
3723
3724 // We should always check to see if any ICD supports it.
3725 if (!loader_check_icds_for_phys_dev_ext_address(inst, funcName)) {
3726 // If we're not checking layers, or we are and it's not in a layer, just
3727 // return
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003728 if (!perform_checking || !loader_check_layer_list_for_phys_dev_ext_address(inst, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003729 goto out;
3730 }
3731 }
3732
3733 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_UNKNOWN_EXTS;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003734 if (perform_checking && !loader_name_in_phys_dev_ext_table(inst, &idx, funcName)) {
Mark Young39389872017-01-19 21:10:49 -07003735 uint32_t i;
3736 bool added = false;
3737
3738 // Only need to add first one to get index in Instance. Others will use
3739 // the same index.
3740 if (!added && loader_add_phys_dev_ext_table(inst, &idx, funcName)) {
3741 added = true;
3742 }
3743
3744 // Setup the ICD function pointers
3745 struct loader_icd_term *icd_term = inst->icd_terms;
3746 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003747 if (MIN_PHYS_DEV_EXTENSION_ICD_INTERFACE_VERSION <= icd_term->scanned_icd->interface_version &&
Mark Young39389872017-01-19 21:10:49 -07003748 NULL != icd_term->scanned_icd->GetPhysicalDeviceProcAddr) {
3749 icd_term->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003750 (PFN_PhysDevExt)icd_term->scanned_icd->GetPhysicalDeviceProcAddr(icd_term->instance, funcName);
Mark Young39389872017-01-19 21:10:49 -07003751
3752 // Make sure we set the instance dispatch to point to the
3753 // loader's terminator now since we can at least handle it
3754 // in one ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003755 inst->disp->phys_dev_ext[idx] = loader_get_phys_dev_ext_termin(idx);
Mark Young39389872017-01-19 21:10:49 -07003756 } else {
3757 icd_term->phys_dev_ext[idx] = NULL;
3758 }
3759
3760 icd_term = icd_term->next;
3761 }
3762
3763 // Now, search for the first layer attached and query using it to get
3764 // the first entry point.
3765 for (i = 0; i < inst->activated_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003766 struct loader_layer_properties *layer_prop = &inst->activated_layer_list.list[i];
3767 if (layer_prop->interface_version > 1 && NULL != layer_prop->functions.get_physical_device_proc_addr) {
Mark Young39389872017-01-19 21:10:49 -07003768 inst->disp->phys_dev_ext[idx] =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003769 (PFN_PhysDevExt)layer_prop->functions.get_physical_device_proc_addr((VkInstance)inst, funcName);
Mark Young39389872017-01-19 21:10:49 -07003770 if (NULL != inst->disp->phys_dev_ext[idx]) {
3771 break;
3772 }
3773 }
3774 }
3775 }
3776
3777 if (NULL != tramp_addr) {
3778 *tramp_addr = loader_get_phys_dev_ext_tramp(idx);
3779 }
3780
3781 if (NULL != term_addr) {
3782 *term_addr = loader_get_phys_dev_ext_termin(idx);
3783 }
3784
3785 success = true;
3786
3787out:
3788 return success;
3789}
3790
Jon Ashburn23d36b12016-02-02 17:47:28 -07003791struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003792 /* look up the loader_instance in our list by comparing dispatch tables, as
3793 * there is no guarantee the instance is still a loader_instance* after any
3794 * layers which wrap the instance object.
3795 */
3796 const VkLayerInstanceDispatchTable *disp;
3797 struct loader_instance *ptr_instance = NULL;
Mark Young39389872017-01-19 21:10:49 -07003798 disp = loader_get_instance_layer_dispatch(instance);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003799 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
Mark Young39389872017-01-19 21:10:49 -07003800 if (&inst->disp->layer_inst_disp == disp) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003801 ptr_instance = inst;
3802 break;
3803 }
3804 }
3805 return ptr_instance;
3806}
3807
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003808static loader_platform_dl_handle loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3809 struct loader_layer_properties *prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003810 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) == NULL) {
3811 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 -06003812 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003813 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Loading layer library %s", prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003814 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003815
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003816 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003817}
3818
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003819static void loader_close_layer_lib(const struct loader_instance *inst, struct loader_layer_properties *prop) {
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003820 if (prop->lib_handle) {
3821 loader_platform_close_library(prop->lib_handle);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003822 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Unloading layer library %s", prop->lib_name);
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003823 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003824 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003825}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003826
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003827void loader_deactivate_layers(const struct loader_instance *instance, struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003828 struct loader_layer_list *list) {
3829 /* delete instance list of enabled layers and close any layer libraries */
3830 for (uint32_t i = 0; i < list->count; i++) {
3831 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003832
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003833 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003834 }
Mark Young0ad83132016-06-30 13:02:42 -06003835 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003836}
3837
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003838/**
3839 * Go through the search_list and find any layers which match type. If layer
3840 * type match is found in then add it to ext_list.
3841 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003842static void loader_add_layer_implicit(const struct loader_instance *inst, const enum layer_type type,
3843 struct loader_layer_list *list, const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003844 bool enable;
3845 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003846 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003847 for (i = 0; i < search_list->count; i++) {
3848 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003849 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003850 /* Found an implicit layer, see if it should be enabled */
3851 enable = false;
3852
Jon Ashburn23d36b12016-02-02 17:47:28 -07003853 // if no enable_environment variable is specified, this implicit
3854 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003855 // should always be enabled. Otherwise check if the variable is set
3856 if (prop->enable_env_var.name[0] == 0) {
3857 enable = true;
3858 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003859 env_value = loader_getenv(prop->enable_env_var.name, inst);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003860 if (env_value && !strcmp(prop->enable_env_var.value, env_value)) enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003861 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003862 }
3863
3864 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003865 // environment variables are set, the layer is disabled. Implicit
3866 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003867 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003868 env_value = loader_getenv(prop->disable_env_var.name, inst);
3869 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003870 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003871 }
3872 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003873
Mark Young0ad83132016-06-30 13:02:42 -06003874 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003875 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003876 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003877 }
3878 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003879}
3880
3881/**
3882 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003883 * is found in search_list then add it to layer_list. But only add it to
3884 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003885 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003886static void loader_add_layer_env(struct loader_instance *inst, const enum layer_type type, const char *env_name,
3887 struct loader_layer_list *layer_list, const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003888 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003889 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003890
Mark Young0ad83132016-06-30 13:02:42 -06003891 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003892 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003893 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003894 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003895 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003896 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003897 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003898 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003899 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003900
Mark Young0ad83132016-06-30 13:02:42 -06003901 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003902
Jon Ashburn23d36b12016-02-02 17:47:28 -07003903 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003904 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003905 if (!strcmp(std_validation_str, name)) {
3906 /* add meta list of layers
3907 don't attempt to remove duplicate layers already added by app or
3908 env var
3909 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003910 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 -07003911 std_validation_str);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003912 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT) inst->activated_layers_are_std_val = true;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003913 for (uint32_t i = 0; i < sizeof(std_validation_names) / sizeof(std_validation_names[0]); i++) {
3914 loader_find_layer_name_add_list(inst, std_validation_names[i], type, search_list, layer_list);
Jon Ashburn71483442016-02-11 18:59:43 -07003915 }
3916 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003917 loader_find_layer_name_add_list(inst, name, type, search_list, layer_list);
Jon Ashburn71483442016-02-11 18:59:43 -07003918 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003919 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003920 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003921
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003922 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003923}
3924
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003925VkResult loader_enable_instance_layers(struct loader_instance *inst, const VkInstanceCreateInfo *pCreateInfo,
3926 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003927 VkResult err;
3928
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003929 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003930
Jon Ashburne39a4f82015-08-28 13:38:21 -06003931 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07003932 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3933 "loader_enable_instance_layers: Failed to initialize"
3934 " the layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003935 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003936 }
3937
Jon Ashburn0c26e712015-07-02 16:10:32 -06003938 /* Add any implicit layers first */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003939 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003940
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003941 /* Add any layers specified via environment variable next */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003942 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 -06003943
3944 /* Add layers specified by the application */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003945 err = loader_add_layer_names_to_list(inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3946 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003947
3948 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003949}
3950
Mark Young39389872017-01-19 21:10:49 -07003951// Determine the layer interface version to use.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003952bool loader_get_layer_interface_version(PFN_vkNegotiateLoaderLayerInterfaceVersion fp_negotiate_layer_version,
3953 VkNegotiateLayerInterface *interface_struct) {
Mark Young39389872017-01-19 21:10:49 -07003954 memset(interface_struct, 0, sizeof(VkNegotiateLayerInterface));
3955
3956 // Base assumption is that all layers are version 1 at least.
3957 interface_struct->loaderLayerInterfaceVersion = 1;
3958
3959 if (fp_negotiate_layer_version != NULL) {
3960 // Layer supports the negotiation API, so call it with the loader's
3961 // latest version supported
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003962 interface_struct->loaderLayerInterfaceVersion = CURRENT_LOADER_LAYER_INTERFACE_VERSION;
Mark Young39389872017-01-19 21:10:49 -07003963 VkResult result = fp_negotiate_layer_version(interface_struct);
3964
3965 if (result != VK_SUCCESS) {
3966 // Layer no longer supports the loader's latest interface version so
3967 // fail loading the Layer
3968 return false;
3969 }
3970 }
3971
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003972 if (interface_struct->loaderLayerInterfaceVersion < MIN_SUPPORTED_LOADER_LAYER_INTERFACE_VERSION) {
Mark Young39389872017-01-19 21:10:49 -07003973 // Loader no longer supports the layer's latest interface version so
3974 // fail loading the layer
3975 return false;
3976 }
3977
3978 return true;
3979}
3980
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003981/*
3982 * Given the list of layers to activate in the loader_instance
3983 * structure. This function will add a VkLayerInstanceCreateInfo
3984 * structure to the VkInstanceCreateInfo.pNext pointer.
3985 * Each activated layer will have it's own VkLayerInstanceLink
3986 * structure that tells the layer what Get*ProcAddr to call to
3987 * get function pointers to the next layer down.
3988 * Once the chain info has been created this function will
3989 * execute the CreateInstance call chain. Each layer will
3990 * then have an opportunity in it's CreateInstance function
3991 * to setup it's dispatch table when the lower layer returns
3992 * successfully.
3993 * Each layer can wrap or not-wrap the returned VkInstance object
3994 * as it sees fit.
3995 * The instance chain is terminated by a loader function
3996 * that will call CreateInstance on all available ICD's and
3997 * cache those VkInstance objects for future use.
3998 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07003999VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
4000 struct loader_instance *inst, VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004001 uint32_t activated_layers = 0;
4002 VkLayerInstanceCreateInfo chain_info;
4003 VkLayerInstanceLink *layer_instance_link_info = NULL;
4004 VkInstanceCreateInfo loader_create_info;
4005 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004006
Mark Young39389872017-01-19 21:10:49 -07004007 PFN_vkGetInstanceProcAddr next_gipa = loader_gpa_instance_internal;
4008 PFN_vkGetInstanceProcAddr cur_gipa = loader_gpa_instance_internal;
4009 PFN_GetPhysicalDeviceProcAddr next_gpdpa = loader_gpdpa_instance_internal;
4010 PFN_GetPhysicalDeviceProcAddr cur_gpdpa = loader_gpdpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004011
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004012 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06004013
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004014 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004015 chain_info.u.pLayerInfo = NULL;
4016 chain_info.pNext = pCreateInfo->pNext;
4017 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
4018 chain_info.function = VK_LAYER_LINK_INFO;
4019 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06004020
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004021 layer_instance_link_info = loader_stack_alloc(sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004022 if (!layer_instance_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004023 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4024 "loader_create_instance_chain: Failed to alloc Instance"
4025 " objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004026 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004027 }
4028
Mark Young39389872017-01-19 21:10:49 -07004029 // Create instance chain of enabled layers
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004030 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004031 struct loader_layer_properties *layer_prop = &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004032 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004033
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004034 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004035 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004036 continue;
Mark Young39389872017-01-19 21:10:49 -07004037 }
4038
4039 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004040 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
Mark Young39389872017-01-19 21:10:49 -07004041 bool functions_in_interface = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004042 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4043 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4044 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004045 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004046 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4047 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004048 }
4049
4050 // If we can negotiate an interface version, then we can also
4051 // get everything we need from the one function call, so try
4052 // that first, and see if we can get all the function pointers
4053 // necessary from that one call.
4054 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004055 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004056
4057 VkNegotiateLayerInterface interface_struct;
4058
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004059 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Young39389872017-01-19 21:10:49 -07004060 // Go ahead and set the properites version to the
4061 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004062 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004063
4064 // If the interface is 2 or newer, we have access to the
4065 // new GetPhysicalDeviceProcAddr function, so grab it,
4066 // and the other necessary functions, from the
4067 // structure.
4068 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4069 cur_gipa = interface_struct.pfnGetInstanceProcAddr;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004070 cur_gpdpa = interface_struct.pfnGetPhysicalDeviceProcAddr;
Mark Young39389872017-01-19 21:10:49 -07004071 if (cur_gipa != NULL) {
4072 // We've set the functions, so make sure we
4073 // don't do the unnecessary calls later.
4074 functions_in_interface = true;
4075 }
4076 }
4077 }
4078 }
4079
4080 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004081 if ((cur_gipa = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004082 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004083 cur_gipa =
4084 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
4085 layer_prop->functions.get_instance_proc_addr = cur_gipa;
Mark Young39389872017-01-19 21:10:49 -07004086 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004087 cur_gipa = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle,
4088 layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004089 }
4090
4091 if (NULL == cur_gipa) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004092 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4093 "loader_create_instance_chain: Failed to"
4094 " find \'vkGetInstanceProcAddr\' in "
4095 "layer %s",
Mark Young39389872017-01-19 21:10:49 -07004096 layer_prop->lib_name);
4097 continue;
4098 }
4099 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004100 }
4101 }
4102
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004103 layer_instance_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4104 layer_instance_link_info[activated_layers].pfnNextGetInstanceProcAddr = next_gipa;
4105 layer_instance_link_info[activated_layers].pfnNextGetPhysicalDeviceProcAddr = next_gpdpa;
Mark Young39389872017-01-19 21:10:49 -07004106 next_gipa = cur_gipa;
4107 if (layer_prop->interface_version > 1 && cur_gpdpa != NULL) {
4108 layer_prop->functions.get_physical_device_proc_addr = cur_gpdpa;
4109 next_gpdpa = cur_gpdpa;
4110 }
4111
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004112 chain_info.u.pLayerInfo = &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004113
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004114 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert instance layer %s (%s)", layer_prop->info.layerName,
4115 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004116
4117 activated_layers++;
4118 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06004119 }
4120
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004121 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)next_gipa(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004122 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06004123 VkLayerInstanceCreateInfo create_info_disp;
4124
Jon Ashburncc407a22016-04-15 09:25:03 -06004125 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004126 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06004127
4128 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
4129
4130 create_info_disp.pNext = loader_create_info.pNext;
4131 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004132 res = fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004133 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004134 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4135 "loader_create_instance_chain: Failed to find "
4136 "\'vkCreateInstance\'");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004137 // Couldn't find CreateInstance function!
4138 res = VK_ERROR_INITIALIZATION_FAILED;
4139 }
4140
Mark Young39389872017-01-19 21:10:49 -07004141 if (res == VK_SUCCESS) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004142 loader_init_instance_core_dispatch_table(&inst->disp->layer_inst_disp, next_gipa, *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07004143 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004144 }
4145
4146 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06004147}
4148
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004149void loader_activate_instance_layer_extensions(struct loader_instance *inst, VkInstance created_inst) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004150 loader_init_instance_extension_dispatch_table(&inst->disp->layer_inst_disp, inst->disp->layer_inst_disp.GetInstanceProcAddr,
4151 created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004152}
4153
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004154VkResult loader_create_device_chain(const struct loader_physical_device_tramp *pd, const VkDeviceCreateInfo *pCreateInfo,
4155 const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst,
4156 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004157 uint32_t activated_layers = 0;
4158 VkLayerDeviceLink *layer_device_link_info;
4159 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004160 VkDeviceCreateInfo loader_create_info;
4161 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004162
Piers Daniell295fe402016-03-29 11:51:11 -06004163 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004164 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004165
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004166 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
4167
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004168 layer_device_link_info = loader_stack_alloc(sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004169 if (!layer_device_link_info) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004170 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4171 "loader_create_device_chain: Failed to alloc Device objects"
4172 " for layer. Skipping Layer.");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004173 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06004174 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004175
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004176 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06004177 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
4178 chain_info.function = VK_LAYER_LINK_INFO;
4179 chain_info.u.pLayerInfo = NULL;
Mark Young39389872017-01-19 21:10:49 -07004180 chain_info.pNext = loader_create_info.pNext;
Jon Ashburn72690f22016-03-29 12:52:13 -06004181 loader_create_info.pNext = &chain_info;
4182
Mark Young39389872017-01-19 21:10:49 -07004183 // Create instance chain of enabled layers
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004184 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004185 struct loader_layer_properties *layer_prop = &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004186 loader_platform_dl_handle lib_handle;
Mark Young39389872017-01-19 21:10:49 -07004187 bool functions_in_interface = false;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06004188
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06004189 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Mark Young39389872017-01-19 21:10:49 -07004190 if (!lib_handle) {
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07004191 continue;
Jon Ashburn21c21ee2015-09-09 11:29:24 -06004192 }
Mark Young39389872017-01-19 21:10:49 -07004193
4194 // If we can negotiate an interface version, then we can also
4195 // get everything we need from the one function call, so try
4196 // that first, and see if we can get all the function pointers
4197 // necessary from that one call.
4198 if (NULL == layer_prop->functions.negotiate_layer_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004199 PFN_vkNegotiateLoaderLayerInterfaceVersion negotiate_interface = NULL;
4200 if (strlen(layer_prop->functions.str_negotiate_interface) == 0) {
4201 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4202 lib_handle, "vkNegotiateLoaderLayerInterfaceVersion");
Mark Young39389872017-01-19 21:10:49 -07004203 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004204 negotiate_interface = (PFN_vkNegotiateLoaderLayerInterfaceVersion)loader_platform_get_proc_address(
4205 lib_handle, layer_prop->functions.str_negotiate_interface);
Mark Young39389872017-01-19 21:10:49 -07004206 }
4207
4208 if (NULL != negotiate_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004209 layer_prop->functions.negotiate_layer_interface = negotiate_interface;
Mark Young39389872017-01-19 21:10:49 -07004210
4211 VkNegotiateLayerInterface interface_struct;
4212
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004213 if (loader_get_layer_interface_version(negotiate_interface, &interface_struct)) {
Mark Young39389872017-01-19 21:10:49 -07004214 // Go ahead and set the properites version to the
4215 // correct value.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004216 layer_prop->interface_version = interface_struct.loaderLayerInterfaceVersion;
Mark Young39389872017-01-19 21:10:49 -07004217
4218 // If the interface is 2 or newer, we have access to the
4219 // new GetPhysicalDeviceProcAddr function, so grab it,
4220 // and the other necessary functions, from the structure.
4221 if (interface_struct.loaderLayerInterfaceVersion > 1) {
4222 fpGIPA = interface_struct.pfnGetInstanceProcAddr;
4223 fpGDPA = interface_struct.pfnGetDeviceProcAddr;
4224 if (fpGIPA != NULL && fpGDPA) {
4225 // We've set the functions, so make sure we
4226 // don't do the unnecessary calls later.
4227 functions_in_interface = true;
4228 }
4229 }
4230 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004231 }
4232 }
4233
Mark Young39389872017-01-19 21:10:49 -07004234 if (!functions_in_interface) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004235 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) == NULL) {
Mark Young39389872017-01-19 21:10:49 -07004236 if (strlen(layer_prop->functions.str_gipa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004237 fpGIPA = (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004238 layer_prop->functions.get_instance_proc_addr = fpGIPA;
4239 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004240 fpGIPA =
4241 (PFN_vkGetInstanceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gipa);
Mark Young39389872017-01-19 21:10:49 -07004242 if (!fpGIPA) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004243 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4244 "loader_create_device_chain: Failed to find "
4245 "\'vkGetInstanceProcAddr\' in layer %s. Skipping"
4246 " layer.",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004247 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004248 continue;
4249 }
4250 }
4251 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
4252 if (strlen(layer_prop->functions.str_gdpa) == 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004253 fpGDPA = (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
Mark Young39389872017-01-19 21:10:49 -07004254 layer_prop->functions.get_device_proc_addr = fpGDPA;
4255 } else
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004256 fpGDPA =
4257 (PFN_vkGetDeviceProcAddr)loader_platform_get_proc_address(lib_handle, layer_prop->functions.str_gdpa);
Mark Young39389872017-01-19 21:10:49 -07004258 if (!fpGDPA) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004259 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Failed to find vkGetDeviceProcAddr in layer %s",
4260 layer_prop->lib_name);
Mark Young39389872017-01-19 21:10:49 -07004261 continue;
4262 }
4263 }
4264 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004265 layer_device_link_info[activated_layers].pNext = chain_info.u.pLayerInfo;
4266 layer_device_link_info[activated_layers].pfnNextGetInstanceProcAddr = nextGIPA;
4267 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr = nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004268 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
4269 nextGIPA = fpGIPA;
4270 nextGDPA = fpGDPA;
4271
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004272 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Insert device layer %s (%s)", layer_prop->info.layerName,
4273 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004274
4275 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06004276 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004277 }
4278
Jon Ashburncc407a22016-04-15 09:25:03 -06004279 VkDevice created_device = (VkDevice)dev;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004280 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004281 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06004282 VkLayerDeviceCreateInfo create_info_disp;
4283
Jon Ashburncc407a22016-04-15 09:25:03 -06004284 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06004285 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
4286
4287 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
4288
4289 create_info_disp.pNext = loader_create_info.pNext;
4290 loader_create_info.pNext = &create_info_disp;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004291 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator, &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06004292 if (res != VK_SUCCESS) {
4293 return res;
4294 }
Mark Young65cb3662016-11-07 13:27:02 -07004295 dev->chain_device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004296 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004297 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4298 "loader_create_device_chain: Failed to find \'vkCreateDevice\' "
4299 "in layer %s");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004300 // Couldn't find CreateDevice function!
4301 return VK_ERROR_INITIALIZATION_FAILED;
4302 }
Jon Ashburn94e70492015-06-10 10:13:10 -06004303
Mark Young65cb3662016-11-07 13:27:02 -07004304 // Initialize device dispatch table
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004305 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA, dev->chain_device);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004306
4307 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06004308}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06004309
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004310VkResult loader_validate_layers(const struct loader_instance *inst, const uint32_t layer_count,
4311 const char *const *ppEnabledLayerNames, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004312 struct loader_layer_properties *prop;
4313
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004314 for (uint32_t i = 0; i < layer_count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004315 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004316 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004317 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4318 "loader_validate_layers: Device ppEnabledLayerNames "
4319 "contains string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004320 return VK_ERROR_LAYER_NOT_PRESENT;
4321 }
4322
Jon Ashburn23d36b12016-02-02 17:47:28 -07004323 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004324 if (!prop) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004325 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4326 "loader_validate_layers: Layer %d does not exist in "
4327 "the list of available layers",
Mark Youngb6399312017-01-10 14:22:15 -07004328 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004329 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004330 }
4331 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004332 return VK_SUCCESS;
4333}
4334
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004335VkResult loader_validate_instance_extensions(const struct loader_instance *inst, const struct loader_extension_list *icd_exts,
4336 const struct loader_layer_list *instance_layers,
4337 const VkInstanceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004338 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004339 struct loader_layer_properties *layer_prop;
4340
Jon Ashburnf19916e2016-01-11 13:12:43 -07004341 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004342 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004343 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004344 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4345 "loader_validate_instance_extensions: Instance "
4346 "ppEnabledExtensionNames contains "
4347 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004348 return VK_ERROR_EXTENSION_NOT_PRESENT;
4349 }
4350
Lenny Komow4053b812016-12-29 16:27:28 -07004351 // See if the extension is in the list of supported extensions
4352 bool found = false;
4353 for (uint32_t j = 0; LOADER_INSTANCE_EXTENSIONS[j] != NULL; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004354 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], LOADER_INSTANCE_EXTENSIONS[j]) == 0) {
Lenny Komow4053b812016-12-29 16:27:28 -07004355 found = true;
4356 break;
4357 }
4358 }
4359
4360 // If it isn't in the list, return an error
4361 if (!found) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004362 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4363 "loader_validate_instance_extensions: Extension %d "
4364 "not found in list of available extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004365 i);
Lenny Komow4053b812016-12-29 16:27:28 -07004366 return VK_ERROR_EXTENSION_NOT_PRESENT;
4367 }
4368
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004369 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004370
4371 if (extension_prop) {
4372 continue;
4373 }
4374
4375 extension_prop = NULL;
4376
4377 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004378 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004379 layer_prop = loader_get_layer_property(pCreateInfo->ppEnabledLayerNames[j], instance_layers);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004380 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004381 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004382 * should have already filtered this case out.
4383 */
4384 continue;
4385 }
4386
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004387 extension_prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[i], &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004388 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004389 /* Found the extension in one of the layers enabled by the app.
4390 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004391 break;
4392 }
4393 }
4394
4395 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004396 // Didn't find extension name in any of the global layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004397 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4398 "loader_validate_instance_extensions: Extension %d "
4399 "not found in enabled layer list extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004400 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004401 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004402 }
4403 }
4404 return VK_SUCCESS;
4405}
4406
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004407VkResult loader_validate_device_extensions(struct loader_physical_device_tramp *phys_dev,
4408 const struct loader_layer_list *activated_device_layers,
4409 const struct loader_extension_list *icd_exts, const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004410 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004411 struct loader_layer_properties *layer_prop;
4412
Jon Ashburnf19916e2016-01-11 13:12:43 -07004413 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004414 VkStringErrorFlags result = vk_string_validate(MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004415 if (result != VK_STRING_ERROR_NONE) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004416 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4417 "loader_validate_device_extensions: Device "
4418 "ppEnabledExtensionNames contains "
4419 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004420 return VK_ERROR_EXTENSION_NOT_PRESENT;
4421 }
4422
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004423 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004424 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004425
4426 if (extension_prop) {
4427 continue;
4428 }
4429
Mark Youngb6399312017-01-10 14:22:15 -07004430 // Not in global list, search activated layer extension lists
Jon Ashburn471f44c2016-01-13 12:51:43 -07004431 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4432 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004433
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004434 extension_prop = get_dev_extension_property(extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004435 if (extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004436 // Found the extension in one of the layers enabled by the app.
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004437 break;
4438 }
4439 }
4440
4441 if (!extension_prop) {
Mark Youngb6399312017-01-10 14:22:15 -07004442 // Didn't find extension name in any of the device layers, error out
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004443 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4444 "loader_validate_device_extensions: Extension %d "
4445 "not found in enabled layer list extensions.",
Mark Youngb6399312017-01-10 14:22:15 -07004446 i);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004447 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004448 }
4449 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004450 return VK_SUCCESS;
4451}
4452
Mark Youngb6399312017-01-10 14:22:15 -07004453// Terminator functions for the Instance chain
4454// All named terminator_<Vulakn API name>
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004455VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
4456 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Mark Young0153e0b2016-11-03 14:27:13 -06004457 struct loader_icd_term *icd_term;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004458 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004459 char **filtered_extension_names = NULL;
4460 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004461 VkResult res = VK_SUCCESS;
Mark Young8b4edb52016-11-11 09:31:55 -07004462 bool one_icd_successful = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004463
Jon Ashburncc407a22016-04-15 09:25:03 -06004464 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004465 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4466
Jon Ashburnf19916e2016-01-11 13:12:43 -07004467 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004468 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004469
Mark Youngb6399312017-01-10 14:22:15 -07004470 // NOTE: Need to filter the extensions to only those supported by the ICD.
4471 // No ICD will advertise support for layers. An ICD library could
4472 // support a layer, but it would be independent of the actual ICD,
4473 // just in the same library.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004474 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004475 if (!filtered_extension_names) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004476 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4477 "terminator_CreateInstance: Failed create extension name "
4478 "array for %d extensions",
Mark Youngb6399312017-01-10 14:22:15 -07004479 pCreateInfo->enabledExtensionCount);
Mark Young3a587792016-08-19 15:25:08 -06004480 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4481 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004482 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004483 icd_create_info.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004484
Mark Young0153e0b2016-11-03 14:27:13 -06004485 for (uint32_t i = 0; i < ptr_instance->icd_tramp_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004486 icd_term = loader_icd_add(ptr_instance, &ptr_instance->icd_tramp_list.scanned_list[i]);
Mark Young0153e0b2016-11-03 14:27:13 -06004487 if (NULL == icd_term) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004488 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4489 "terminator_CreateInstance: Failed to add ICD %d to ICD "
4490 "trampoline list.",
Mark Youngb6399312017-01-10 14:22:15 -07004491 i);
Mark Young3a587792016-08-19 15:25:08 -06004492 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4493 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004494 }
Mark Young6267ae62017-01-12 12:27:19 -07004495
Mark Young0ad83132016-06-30 13:02:42 -06004496 icd_create_info.enabledExtensionCount = 0;
4497 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004498
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004499 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Build ICD instance extension list");
Mark Young0ad83132016-06-30 13:02:42 -06004500 // traverse scanned icd list adding non-duplicate extensions to the
4501 // list
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004502 res = loader_init_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004503 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4504 // If out of memory, bail immediately.
4505 goto out;
4506 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004507 // Something bad happened with this ICD, so free it and try the
4508 // next.
Mark Young0153e0b2016-11-03 14:27:13 -06004509 ptr_instance->icd_terms = icd_term->next;
4510 icd_term->next = NULL;
4511 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004512 continue;
4513 }
4514
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004515 res = loader_add_instance_extensions(ptr_instance, icd_term->scanned_icd->EnumerateInstanceExtensionProperties,
4516 icd_term->scanned_icd->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004517 if (VK_SUCCESS != res) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004518 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004519 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4520 // If out of memory, bail immediately.
4521 goto out;
4522 } else {
4523 // Something bad happened with this ICD, so free it and try
4524 // the next.
Mark Young0153e0b2016-11-03 14:27:13 -06004525 ptr_instance->icd_terms = icd_term->next;
4526 icd_term->next = NULL;
4527 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004528 continue;
4529 }
Mark Young3a587792016-08-19 15:25:08 -06004530 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004531
Mark Young0ad83132016-06-30 13:02:42 -06004532 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004533 prop = get_extension_property(pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004534 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004535 filtered_extension_names[icd_create_info.enabledExtensionCount] = (char *)pCreateInfo->ppEnabledExtensionNames[j];
Mark Young0ad83132016-06-30 13:02:42 -06004536 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004537 }
4538 }
Mark Young0ad83132016-06-30 13:02:42 -06004539
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004540 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004541
Mark Young8b4edb52016-11-11 09:31:55 -07004542 VkResult icd_result =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004543 ptr_instance->icd_tramp_list.scanned_list[i].CreateInstance(&icd_create_info, pAllocator, &(icd_term->instance));
Mark Young8b4edb52016-11-11 09:31:55 -07004544 if (VK_ERROR_OUT_OF_HOST_MEMORY == icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004545 // If out of memory, bail immediately.
Mark Young8b4edb52016-11-11 09:31:55 -07004546 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06004547 goto out;
Mark Young8b4edb52016-11-11 09:31:55 -07004548 } else if (VK_SUCCESS != icd_result) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004549 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4550 "terminator_CreateInstance: Failed to CreateInstance in "
4551 "ICD %d. Skipping ICD.",
Mark Youngb6399312017-01-10 14:22:15 -07004552 i);
Mark Young0153e0b2016-11-03 14:27:13 -06004553 ptr_instance->icd_terms = icd_term->next;
4554 icd_term->next = NULL;
4555 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004556 continue;
4557 }
Mark Young0ad83132016-06-30 13:02:42 -06004558
Mark Young0153e0b2016-11-03 14:27:13 -06004559 if (!loader_icd_init_entrys(icd_term, icd_term->instance,
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004560 ptr_instance->icd_tramp_list.scanned_list[i].GetInstanceProcAddr)) {
4561 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4562 "terminator_CreateInstance: Failed to CreateInstance and find "
4563 "entrypoints with ICD. Skipping ICD.");
Mark Young3a587792016-08-19 15:25:08 -06004564 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004565 }
Mark Young8b4edb52016-11-11 09:31:55 -07004566
4567 // If we made it this far, at least one ICD was successful
4568 one_icd_successful = true;
Jon Ashburn46888392015-01-29 15:45:51 -07004569 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004570
Mark Young8b4edb52016-11-11 09:31:55 -07004571 // If no ICDs were added to instance list and res is unchanged
4572 // from it's initial value, the loader was unable to find
4573 // a suitable ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004574 if (VK_SUCCESS == res && (ptr_instance->icd_terms == NULL || !one_icd_successful)) {
Mark Young3a587792016-08-19 15:25:08 -06004575 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4576 }
4577
4578out:
4579
4580 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004581 while (NULL != ptr_instance->icd_terms) {
4582 icd_term = ptr_instance->icd_terms;
4583 ptr_instance->icd_terms = icd_term->next;
4584 if (NULL != icd_term->instance) {
4585 icd_term->DestroyInstance(icd_term->instance, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004586 }
Mark Young0153e0b2016-11-03 14:27:13 -06004587 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004588 }
Ian Elliotteb450762015-02-05 15:19:15 -07004589 }
Jon Ashburn46888392015-01-29 15:45:51 -07004590
Mark Young3a587792016-08-19 15:25:08 -06004591 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004592}
4593
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004594VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004595 struct loader_instance *ptr_instance = loader_instance(instance);
Karl Schultze2ef9e62017-01-13 14:01:35 -07004596 if (NULL == ptr_instance) {
4597 return;
4598 }
Mark Young0153e0b2016-11-03 14:27:13 -06004599 struct loader_icd_term *icd_terms = ptr_instance->icd_terms;
4600 struct loader_icd_term *next_icd_term;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004601
4602 // Remove this instance from the list of instances:
4603 struct loader_instance *prev = NULL;
4604 struct loader_instance *next = loader.instances;
4605 while (next != NULL) {
4606 if (next == ptr_instance) {
4607 // Remove this instance from the list:
4608 if (prev)
4609 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004610 else
4611 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004612 break;
4613 }
4614 prev = next;
4615 next = next->next;
4616 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004617
Mark Young0153e0b2016-11-03 14:27:13 -06004618 while (NULL != icd_terms) {
4619 if (icd_terms->instance) {
4620 icd_terms->DestroyInstance(icd_terms->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004621 }
Mark Young0153e0b2016-11-03 14:27:13 -06004622 next_icd_term = icd_terms->next;
4623 icd_terms->instance = VK_NULL_HANDLE;
4624 loader_icd_destroy(ptr_instance, icd_terms, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004625
Mark Young0153e0b2016-11-03 14:27:13 -06004626 icd_terms = next_icd_term;
Jon Ashburn46888392015-01-29 15:45:51 -07004627 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004628
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004629 loader_delete_layer_properties(ptr_instance, &ptr_instance->instance_layer_list);
Mark Young0153e0b2016-11-03 14:27:13 -06004630 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_tramp_list);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004631 loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Mark Young39389872017-01-19 21:10:49 -07004632 if (NULL != ptr_instance->phys_devs_term) {
Mark Young0193d652016-12-28 16:10:10 -07004633 for (uint32_t i = 0; i < ptr_instance->phys_dev_count_term; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004634 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term[i]);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07004635 }
Mark Young0ad83132016-06-30 13:02:42 -06004636 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Lenny Komow8a1f8a52016-12-20 15:35:11 -07004637 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004638 loader_free_dev_ext_table(ptr_instance);
Mark Young39389872017-01-19 21:10:49 -07004639 loader_free_phys_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004640}
4641
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004642VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4643 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
Mark Young0ad83132016-06-30 13:02:42 -06004644 VkResult res = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06004645 struct loader_physical_device_term *phys_dev_term;
4646 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
4647 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004648
Jon Ashburncc407a22016-04-15 09:25:03 -06004649 struct loader_device *dev = (struct loader_device *)*pDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06004650 PFN_vkCreateDevice fpCreateDevice = icd_term->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004651 struct loader_extension_list icd_exts;
4652
Mark Young65cb3662016-11-07 13:27:02 -07004653 dev->phys_dev_term = phys_dev_term;
4654
Mark Young0ad83132016-06-30 13:02:42 -06004655 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004656
Jon Ashburn1530c342016-02-26 13:14:27 -07004657 if (fpCreateDevice == NULL) {
Mark Young0153e0b2016-11-03 14:27:13 -06004658 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07004659 "terminator_CreateDevice: No vkCreateDevice command exposed "
4660 "by ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004661 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004662 res = VK_ERROR_INITIALIZATION_FAILED;
4663 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004664 }
4665
Jon Ashburn1530c342016-02-26 13:14:27 -07004666 VkDeviceCreateInfo localCreateInfo;
4667 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004668
Mark Youngb6399312017-01-10 14:22:15 -07004669 // NOTE: Need to filter the extensions to only those supported by the ICD.
4670 // No ICD will advertise support for layers. An ICD library could
4671 // support a layer, but it would be independent of the actual ICD,
4672 // just in the same library.
Jon Ashburn1530c342016-02-26 13:14:27 -07004673 char **filtered_extension_names = NULL;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004674 filtered_extension_names = loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Mark Youngb6399312017-01-10 14:22:15 -07004675 if (NULL == filtered_extension_names) {
4676 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4677 "terminator_CreateDevice: Failed to create extension name "
4678 "storage for %d extensions %d",
4679 pCreateInfo->enabledExtensionCount);
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004680 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004681 }
4682
Jon Ashburn1530c342016-02-26 13:14:27 -07004683 localCreateInfo.enabledLayerCount = 0;
4684 localCreateInfo.ppEnabledLayerNames = NULL;
4685
4686 localCreateInfo.enabledExtensionCount = 0;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004687 localCreateInfo.ppEnabledExtensionNames = (const char *const *)filtered_extension_names;
Jon Ashburn1530c342016-02-26 13:14:27 -07004688
Mark Youngb6399312017-01-10 14:22:15 -07004689 // Get the physical device (ICD) extensions
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004690 res = loader_init_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts, sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004691 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004692 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004693 }
4694
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004695 res = loader_add_device_extensions(icd_term->this_instance, icd_term->EnumerateDeviceExtensionProperties,
4696 phys_dev_term->phys_dev, icd_term->scanned_icd->lib_name, &icd_exts);
Jon Ashburn014438f2016-03-01 19:51:07 -07004697 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004698 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004699 }
4700
Jon Ashburn1530c342016-02-26 13:14:27 -07004701 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4702 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004703 VkExtensionProperties *prop = get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004704 if (prop) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004705 filtered_extension_names[localCreateInfo.enabledExtensionCount] = (char *)extension_name;
Jon Ashburn1530c342016-02-26 13:14:27 -07004706 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06004707 } else {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004708 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4709 "vkCreateDevice extension %s not available for "
4710 "devices associated with ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004711 extension_name, icd_term->scanned_icd->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07004712 }
4713 }
4714
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004715 res = fpCreateDevice(phys_dev_term->phys_dev, &localCreateInfo, pAllocator, &dev->icd_device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004716 if (res != VK_SUCCESS) {
Mark Young0153e0b2016-11-03 14:27:13 -06004717 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Youngb6399312017-01-10 14:22:15 -07004718 "terminator_CreateDevice: Failed in ICD %s vkCreateDevice"
4719 "call",
Mark Young0153e0b2016-11-03 14:27:13 -06004720 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004721 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004722 }
4723
Mark Young65cb3662016-11-07 13:27:02 -07004724 *pDevice = dev->icd_device;
Mark Young0153e0b2016-11-03 14:27:13 -06004725 loader_add_logical_device(icd_term->this_instance, icd_term, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004726
4727 /* Init dispatch pointer in new device object */
4728 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4729
Mark Young0ad83132016-06-30 13:02:42 -06004730out:
4731 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004732 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004733 }
4734
Jon Ashburn1530c342016-02-26 13:14:27 -07004735 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004736}
4737
Mark Young6267ae62017-01-12 12:27:19 -07004738VkResult setupLoaderTrampPhysDevs(VkInstance instance) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004739 VkResult res = VK_SUCCESS;
Mark Young6267ae62017-01-12 12:27:19 -07004740 VkPhysicalDevice *local_phys_devs = NULL;
4741 struct loader_instance *inst;
4742 uint32_t total_count = 0;
4743 struct loader_physical_device_tramp **new_phys_devs = NULL;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004744
Mark Young6267ae62017-01-12 12:27:19 -07004745 inst = loader_get_instance(instance);
4746 if (NULL == inst) {
4747 res = VK_ERROR_INITIALIZATION_FAILED;
4748 goto out;
4749 }
4750 total_count = inst->total_gpu_count;
4751
4752 // Create an array for the new physical devices, which will be stored
4753 // in the instance for the trampoline code.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004754 new_phys_devs = (struct loader_physical_device_tramp **)loader_instance_heap_alloc(
4755 inst, total_count * sizeof(struct loader_physical_device_tramp *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004756 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004757 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4758 "setupLoaderTrampPhysDevs: Failed to allocate new physical device"
4759 " array of size %d",
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004760 total_count);
Mark Youngd8382d72016-12-23 16:59:58 -07004761 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4762 goto out;
4763 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004764 memset(new_phys_devs, 0, total_count * sizeof(struct loader_physical_device_tramp *));
Jon Ashburn014438f2016-03-01 19:51:07 -07004765
Mark Young6267ae62017-01-12 12:27:19 -07004766 // Create a temporary array (on the stack) to keep track of the
4767 // returned VkPhysicalDevice values.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004768 local_phys_devs = loader_stack_alloc(sizeof(VkPhysicalDevice) * total_count);
Mark Young6267ae62017-01-12 12:27:19 -07004769 if (NULL == local_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004770 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4771 "setupLoaderTrampPhysDevs: Failed to allocate local "
4772 "physical device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004773 total_count);
4774 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4775 goto out;
4776 }
4777 memset(local_phys_devs, 0, sizeof(VkPhysicalDevice) * total_count);
4778
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004779 res = inst->disp->layer_inst_disp.EnumeratePhysicalDevices(instance, &total_count, local_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004780 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004781 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4782 "setupLoaderTrampPhysDevs: Failed during dispatch call "
4783 "of \'vkEnumeratePhysicalDevices\' to lower layers or "
4784 "loader.");
Mark Young6267ae62017-01-12 12:27:19 -07004785 goto out;
4786 }
4787
4788 // Copy or create everything to fill the new array of physical devices
4789 for (uint32_t new_idx = 0; new_idx < total_count; new_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07004790 // Check if this physical device is already in the old buffer
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004791 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_tramp; old_idx++) {
4792 if (local_phys_devs[new_idx] == inst->phys_devs_tramp[old_idx]->phys_dev) {
Mark Young6267ae62017-01-12 12:27:19 -07004793 new_phys_devs[new_idx] = inst->phys_devs_tramp[old_idx];
4794 break;
4795 }
Mark Youngd8382d72016-12-23 16:59:58 -07004796 }
4797
Mark Young6267ae62017-01-12 12:27:19 -07004798 // If this physical device isn't in the old buffer, create it
4799 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004800 new_phys_devs[new_idx] = (struct loader_physical_device_tramp *)loader_instance_heap_alloc(
4801 inst, sizeof(struct loader_physical_device_tramp), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004802 if (NULL == new_phys_devs[new_idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004803 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4804 "setupLoaderTrampPhysDevs: Failed to allocate "
4805 "physical device trampoline object %d",
Mark Young6267ae62017-01-12 12:27:19 -07004806 new_idx);
4807 total_count = new_idx;
Mark Youngd8382d72016-12-23 16:59:58 -07004808 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4809 goto out;
4810 }
4811
Mark Young6267ae62017-01-12 12:27:19 -07004812 // Initialize the new physicalDevice object
4813 loader_set_dispatch((void *)new_phys_devs[new_idx], inst->disp);
4814 new_phys_devs[new_idx]->this_instance = inst;
4815 new_phys_devs[new_idx]->phys_dev = local_phys_devs[new_idx];
Mark Young559d7502016-09-26 11:38:46 -06004816 }
Lenny Komowa5e01122016-12-22 15:29:43 -07004817 }
Mark Young559d7502016-09-26 11:38:46 -06004818
Lenny Komowa5e01122016-12-22 15:29:43 -07004819out:
Mark Youngd8382d72016-12-23 16:59:58 -07004820
Mark Young6267ae62017-01-12 12:27:19 -07004821 if (VK_SUCCESS != res) {
4822 if (NULL != new_phys_devs) {
4823 for (uint32_t i = 0; i < total_count; i++) {
4824 loader_instance_heap_free(inst, new_phys_devs[i]);
Lenny Komowa5e01122016-12-22 15:29:43 -07004825 }
4826 loader_instance_heap_free(inst, new_phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004827 }
4828 total_count = 0;
4829 } else {
4830 // Free everything that didn't carry over to the new array of
4831 // physical devices
4832 if (NULL != inst->phys_devs_tramp) {
4833 for (uint32_t i = 0; i < inst->phys_dev_count_tramp; i++) {
4834 bool found = false;
4835 for (uint32_t j = 0; j < total_count; j++) {
4836 if (inst->phys_devs_tramp[i] == new_phys_devs[j]) {
4837 found = true;
4838 break;
4839 }
4840 }
4841 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004842 loader_instance_heap_free(inst, inst->phys_devs_tramp[i]);
Mark Young6267ae62017-01-12 12:27:19 -07004843 }
4844 }
4845 loader_instance_heap_free(inst, inst->phys_devs_tramp);
4846 }
Mark Youngd8382d72016-12-23 16:59:58 -07004847
Mark Young6267ae62017-01-12 12:27:19 -07004848 // Swap in the new physical device list
4849 inst->phys_dev_count_tramp = total_count;
4850 inst->phys_devs_tramp = new_phys_devs;
4851 }
4852
4853 return res;
4854}
4855
4856VkResult setupLoaderTermPhysDevs(struct loader_instance *inst) {
4857 VkResult res = VK_SUCCESS;
4858 struct loader_icd_term *icd_term;
4859 struct loader_phys_dev_per_icd *icd_phys_dev_array = NULL;
4860 struct loader_physical_device_term **new_phys_devs = NULL;
4861 uint32_t i = 0;
4862
4863 inst->total_gpu_count = 0;
4864
4865 // Allocate something to store the physical device characteristics
4866 // that we read from each ICD.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004867 icd_phys_dev_array =
4868 (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 -07004869 if (NULL == icd_phys_dev_array) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004870 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4871 "setupLoaderTermPhysDevs: Failed to allocate temporary "
4872 "ICD Physical device info array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004873 inst->total_gpu_count);
4874 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4875 goto out;
4876 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004877 memset(icd_phys_dev_array, 0, sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
Mark Young6267ae62017-01-12 12:27:19 -07004878 icd_term = inst->icd_terms;
4879
4880 // For each ICD, query the number of physical devices, and then get an
4881 // internal value for those physical devices.
4882 while (NULL != icd_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004883 res = icd_term->EnumeratePhysicalDevices(icd_term->instance, &icd_phys_dev_array[i].count, NULL);
Mark Young6267ae62017-01-12 12:27:19 -07004884 if (VK_SUCCESS != res) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004885 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4886 "setupLoaderTermPhysDevs: Call to "
4887 "ICD %d's \'vkEnumeratePhysicalDevices\' failed with"
4888 " error 0x%08x",
Mark Young6267ae62017-01-12 12:27:19 -07004889 i, res);
4890 goto out;
4891 }
4892
4893 icd_phys_dev_array[i].phys_devs =
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004894 (VkPhysicalDevice *)loader_stack_alloc(icd_phys_dev_array[i].count * sizeof(VkPhysicalDevice));
Mark Young6267ae62017-01-12 12:27:19 -07004895 if (NULL == icd_phys_dev_array[i].phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004896 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4897 "setupLoaderTermPhysDevs: Failed to allocate temporary "
4898 "ICD Physical device array for ICD %d of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004899 i, inst->total_gpu_count);
4900 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4901 goto out;
4902 }
4903
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004904 res =
4905 icd_term->EnumeratePhysicalDevices(icd_term->instance, &(icd_phys_dev_array[i].count), icd_phys_dev_array[i].phys_devs);
Mark Young6267ae62017-01-12 12:27:19 -07004906 if (VK_SUCCESS != res) {
4907 goto out;
4908 }
4909 inst->total_gpu_count += icd_phys_dev_array[i].count;
4910 icd_phys_dev_array[i].this_icd_term = icd_term;
4911
4912 icd_term = icd_term->next;
4913 i++;
4914 }
4915
4916 if (0 == inst->total_gpu_count) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004917 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4918 "setupLoaderTermPhysDevs: Failed to detect any valid"
4919 " GPUs in the current config");
Mark Young6267ae62017-01-12 12:27:19 -07004920 res = VK_ERROR_INITIALIZATION_FAILED;
4921 goto out;
4922 }
4923
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004924 new_phys_devs = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count,
4925 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004926 if (NULL == new_phys_devs) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004927 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4928 "setupLoaderTermPhysDevs: Failed to allocate new physical"
4929 " device array of size %d",
Mark Young6267ae62017-01-12 12:27:19 -07004930 inst->total_gpu_count);
4931 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4932 goto out;
4933 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004934 memset(new_phys_devs, 0, sizeof(struct loader_physical_device_term *) * inst->total_gpu_count);
Mark Young6267ae62017-01-12 12:27:19 -07004935
4936 // Copy or create everything to fill the new array of physical devices
4937 uint32_t idx = 0;
4938 for (uint32_t icd_idx = 0; icd_idx < inst->total_icd_count; icd_idx++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004939 for (uint32_t pd_idx = 0; pd_idx < icd_phys_dev_array[icd_idx].count; pd_idx++) {
Mark Young6267ae62017-01-12 12:27:19 -07004940 // Check if this physical device is already in the old buffer
4941 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004942 for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_term; old_idx++) {
4943 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 -07004944 new_phys_devs[idx] = inst->phys_devs_term[old_idx];
4945 break;
4946 }
4947 }
4948 }
4949 // If this physical device isn't in the old buffer, then we
4950 // need to create it.
4951 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004952 new_phys_devs[idx] = loader_instance_heap_alloc(inst, sizeof(struct loader_physical_device_term),
4953 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young6267ae62017-01-12 12:27:19 -07004954 if (NULL == new_phys_devs[idx]) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07004955 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4956 "setupLoaderTermPhysDevs: Failed to allocate "
4957 "physical device terminator object %d",
Mark Young6267ae62017-01-12 12:27:19 -07004958 idx);
4959 inst->total_gpu_count = idx;
4960 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4961 goto out;
4962 }
4963
4964 loader_set_dispatch((void *)new_phys_devs[idx], inst->disp);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004965 new_phys_devs[idx]->this_icd_term = icd_phys_dev_array[icd_idx].this_icd_term;
Mark Young6267ae62017-01-12 12:27:19 -07004966 new_phys_devs[idx]->icd_index = (uint8_t)(icd_idx);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004967 new_phys_devs[idx]->phys_dev = icd_phys_dev_array[icd_idx].phys_devs[pd_idx];
Mark Young6267ae62017-01-12 12:27:19 -07004968 }
4969 idx++;
4970 }
4971 }
4972
4973out:
4974
4975 if (VK_SUCCESS != res) {
4976 if (NULL != inst->phys_devs_term) {
4977 // We've encountered an error, so we should free the
4978 // new buffers.
4979 for (uint32_t i = 0; i < inst->total_gpu_count; i++) {
4980 loader_instance_heap_free(inst, new_phys_devs[i]);
4981 }
4982 loader_instance_heap_free(inst, inst->phys_devs_term);
4983 inst->total_gpu_count = 0;
4984 }
4985 } else {
4986 // Free everything that didn't carry over to the new array of
4987 // physical devices. Everything else will have been copied over
4988 // to the new array.
4989 if (NULL != inst->phys_devs_term) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004990 for (uint32_t cur_pd = 0; cur_pd < inst->phys_dev_count_term; cur_pd++) {
Mark Young6267ae62017-01-12 12:27:19 -07004991 bool found = false;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004992 for (uint32_t new_pd_idx = 0; new_pd_idx < inst->total_gpu_count; new_pd_idx++) {
4993 if (inst->phys_devs_term[cur_pd] == new_phys_devs[new_pd_idx]) {
Mark Young6267ae62017-01-12 12:27:19 -07004994 found = true;
4995 break;
4996 }
4997 }
4998 if (!found) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07004999 loader_instance_heap_free(inst, inst->phys_devs_term[cur_pd]);
Mark Young6267ae62017-01-12 12:27:19 -07005000 }
5001 }
5002 loader_instance_heap_free(inst, inst->phys_devs_term);
5003 }
5004
5005 // Swap out old and new devices list
5006 inst->phys_dev_count_term = inst->total_gpu_count;
5007 inst->phys_devs_term = new_phys_devs;
5008 }
5009
5010 return res;
5011}
5012
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005013VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
5014 VkPhysicalDevice *pPhysicalDevices) {
Mark Young6267ae62017-01-12 12:27:19 -07005015 struct loader_instance *inst = (struct loader_instance *)instance;
5016 VkResult res = VK_SUCCESS;
5017
5018 // Only do the setup if we're re-querying the number of devices, or
5019 // our count is currently 0.
5020 if (NULL == pPhysicalDevices || 0 == inst->total_gpu_count) {
5021 res = setupLoaderTermPhysDevs(inst);
5022 if (VK_SUCCESS != res) {
5023 goto out;
5024 }
5025 }
5026
5027 uint32_t copy_count = inst->total_gpu_count;
5028 if (NULL != pPhysicalDevices) {
5029 if (copy_count > *pPhysicalDeviceCount) {
5030 copy_count = *pPhysicalDeviceCount;
5031 res = VK_INCOMPLETE;
5032 }
5033
5034 for (uint32_t i = 0; i < copy_count; i++) {
5035 pPhysicalDevices[i] = (VkPhysicalDevice)inst->phys_devs_term[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07005036 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06005037 }
Mark Young559d7502016-09-26 11:38:46 -06005038
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07005039 *pPhysicalDeviceCount = copy_count;
5040
Mark Young6267ae62017-01-12 12:27:19 -07005041out:
5042
Jon Ashburn24cd4be2015-11-01 14:04:06 -07005043 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07005044}
5045
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005046VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
5047 VkPhysicalDeviceProperties *pProperties) {
5048 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005049 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005050 if (NULL != icd_term->GetPhysicalDeviceProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005051 icd_term->GetPhysicalDeviceProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005052 }
Tony Barbour59a47322015-06-24 16:06:58 -06005053}
5054
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005055VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
5056 uint32_t *pQueueFamilyPropertyCount,
5057 VkQueueFamilyProperties *pProperties) {
5058 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005059 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005060 if (NULL != icd_term->GetPhysicalDeviceQueueFamilyProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005061 icd_term->GetPhysicalDeviceQueueFamilyProperties(phys_dev_term->phys_dev, pQueueFamilyPropertyCount, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005062 }
Tony Barbour59a47322015-06-24 16:06:58 -06005063}
5064
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005065VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
5066 VkPhysicalDeviceMemoryProperties *pProperties) {
5067 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005068 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005069 if (NULL != icd_term->GetPhysicalDeviceMemoryProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005070 icd_term->GetPhysicalDeviceMemoryProperties(phys_dev_term->phys_dev, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005071 }
Jon Ashburn3da71f22015-05-14 12:43:38 -06005072}
5073
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005074VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
5075 VkPhysicalDeviceFeatures *pFeatures) {
5076 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005077 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005078 if (NULL != icd_term->GetPhysicalDeviceFeatures) {
Mark Young0153e0b2016-11-03 14:27:13 -06005079 icd_term->GetPhysicalDeviceFeatures(phys_dev_term->phys_dev, pFeatures);
Mark Youngb6399312017-01-10 14:22:15 -07005080 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005081}
5082
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005083VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5084 VkFormatProperties *pFormatInfo) {
5085 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005086 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005087 if (NULL != icd_term->GetPhysicalDeviceFormatProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005088 icd_term->GetPhysicalDeviceFormatProperties(phys_dev_term->phys_dev, format, pFormatInfo);
Mark Youngb6399312017-01-10 14:22:15 -07005089 }
Chris Forbesbc0bb772015-06-21 22:55:02 +12005090}
5091
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005092VKAPI_ATTR VkResult VKAPI_CALL terminator_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5093 VkImageType type, VkImageTiling tiling,
5094 VkImageUsageFlags usage, VkImageCreateFlags flags,
5095 VkImageFormatProperties *pImageFormatProperties) {
5096 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005097 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005098 if (NULL == icd_term->GetPhysicalDeviceImageFormatProperties) {
5099 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5100 "Encountered the vkEnumerateDeviceLayerProperties "
5101 "terminator. This means a layer improperly continued.");
Chia-I Wu17241042015-10-31 00:31:16 +08005102 return VK_ERROR_INITIALIZATION_FAILED;
Mark Youngb6399312017-01-10 14:22:15 -07005103 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005104 return icd_term->GetPhysicalDeviceImageFormatProperties(phys_dev_term->phys_dev, format, type, tiling, usage, flags,
5105 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06005106}
5107
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005108VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
5109 VkImageType type, VkSampleCountFlagBits samples,
5110 VkImageUsageFlags usage, VkImageTiling tiling,
5111 uint32_t *pNumProperties,
5112 VkSparseImageFormatProperties *pProperties) {
5113 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06005114 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Youngb6399312017-01-10 14:22:15 -07005115 if (NULL != icd_term->GetPhysicalDeviceSparseImageFormatProperties) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005116 icd_term->GetPhysicalDeviceSparseImageFormatProperties(phys_dev_term->phys_dev, format, type, samples, usage, tiling,
5117 pNumProperties, pProperties);
Mark Youngb6399312017-01-10 14:22:15 -07005118 }
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06005119}
5120
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005121VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
5122 const char *pLayerName, uint32_t *pPropertyCount,
5123 VkExtensionProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06005124 struct loader_physical_device_term *phys_dev_term;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07005125
Mark Young3a587792016-08-19 15:25:08 -06005126 struct loader_layer_list implicit_layer_list = {0};
5127 struct loader_extension_list all_exts = {0};
5128 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07005129
Jon Ashburndc5d9202016-02-29 13:00:51 -07005130 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005131
Jon Ashburndc5d9202016-02-29 13:00:51 -07005132 /* Any layer or trampoline wrapping should be removed at this point in time
5133 * can just cast to the expected type for VkPhysicalDevice. */
Mark Young0153e0b2016-11-03 14:27:13 -06005134 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005135
Jon Ashburndc5d9202016-02-29 13:00:51 -07005136 /* this case is during the call down the instance chain with pLayerName
5137 * == NULL*/
Mark Young0153e0b2016-11-03 14:27:13 -06005138 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005139 uint32_t icd_ext_count = *pPropertyCount;
5140 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005141
Jon Ashburndc5d9202016-02-29 13:00:51 -07005142 /* get device extensions */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005143 res = icd_term->EnumerateDeviceExtensionProperties(phys_dev_term->phys_dev, NULL, &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06005144 if (res != VK_SUCCESS) {
5145 goto out;
5146 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07005147
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005148 if (!loader_init_layer_list(icd_term->this_instance, &implicit_layer_list)) {
Mark Young3a587792016-08-19 15:25:08 -06005149 res = VK_ERROR_OUT_OF_HOST_MEMORY;
5150 goto out;
5151 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005152
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005153 loader_add_layer_implicit(icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &implicit_layer_list,
5154 &icd_term->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07005155 /* we need to determine which implicit layers are active,
5156 * and then add their extensions. This can't be cached as
5157 * it depends on results of environment variables (which can change).
5158 */
5159 if (pProperties != NULL) {
5160 /* initialize dev_extension list within the physicalDevice object */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005161 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 -06005162 if (res != VK_SUCCESS) {
5163 goto out;
5164 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005165
Jon Ashburndc5d9202016-02-29 13:00:51 -07005166 /* we need to determine which implicit layers are active,
5167 * and then add their extensions. This can't be cached as
5168 * it depends on results of environment variables (which can
5169 * change).
5170 */
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005171 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 -06005172 if (res != VK_SUCCESS) {
5173 goto out;
5174 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005175
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005176 loader_add_layer_implicit(icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT, &implicit_layer_list,
5177 &icd_term->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07005178
Jon Ashburndc5d9202016-02-29 13:00:51 -07005179 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005180 for (uint32_t j = 0; j < implicit_layer_list.list[i].device_extension_list.count; j++) {
5181 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts, 1,
5182 &implicit_layer_list.list[i].device_extension_list.list[j].props);
Mark Young3a587792016-08-19 15:25:08 -06005183 if (res != VK_SUCCESS) {
5184 goto out;
5185 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005186 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07005187 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005188 uint32_t capacity = *pPropertyCount;
5189 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07005190
Jon Ashburndc5d9202016-02-29 13:00:51 -07005191 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
5192 props[i] = all_exts.list[i];
5193 }
5194 /* wasn't enough space for the extensions, we did partial copy now
5195 * return VK_INCOMPLETE */
5196 if (capacity < all_exts.count) {
5197 res = VK_INCOMPLETE;
5198 } else {
5199 *pPropertyCount = all_exts.count;
5200 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005201 } else {
5202 /* just return the count; need to add in the count of implicit layer
5203 * extensions
5204 * don't worry about duplicates being added in the count */
5205 *pPropertyCount = icd_ext_count;
5206
5207 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005208 *pPropertyCount += implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07005209 }
5210 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06005211 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07005212
Mark Young3a587792016-08-19 15:25:08 -06005213out:
5214
5215 if (NULL != implicit_layer_list.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005216 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Mark Young3a587792016-08-19 15:25:08 -06005217 }
5218 if (NULL != all_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005219 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&all_exts);
Mark Young3a587792016-08-19 15:25:08 -06005220 }
5221 if (NULL != icd_exts.list) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005222 loader_destroy_generic_list(icd_term->this_instance, (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06005223 }
5224
Jon Ashburndc5d9202016-02-29 13:00:51 -07005225 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06005226}
5227
Mark Lobodzinski729a8d32017-01-26 12:16:30 -07005228VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
5229 VkLayerProperties *pProperties) {
5230 struct loader_physical_device_term *phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Mark Youngb6399312017-01-10 14:22:15 -07005231 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07005232 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
5233 "Encountered the vkEnumerateDeviceLayerProperties "
5234 "terminator. This means a layer improperly continued.");
Mark Youngb6399312017-01-10 14:22:15 -07005235 // Should never get here this call isn't dispatched down the chain
Jon Ashburndc5d9202016-02-29 13:00:51 -07005236 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06005237}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005238
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005239VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005240 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07005241 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005242 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005243
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005244 for (i = 0; i <= max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005245 if (utf8[i] == 0) {
5246 break;
Courtney Goeltzenleuchter7a3486d2016-12-21 16:24:34 -07005247 } else if (i == max_length) {
5248 result |= VK_STRING_ERROR_LENGTH;
5249 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07005250 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005251 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005252 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005253 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07005254 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07005255 num_char_bytes = 2;
5256 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
5257 num_char_bytes = 3;
5258 } else {
5259 result = VK_STRING_ERROR_BAD_DATA;
5260 }
5261
5262 // Validate the following num_char_bytes of data
5263 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
5264 if (++i == max_length) {
5265 result |= VK_STRING_ERROR_LENGTH;
5266 break;
5267 }
5268 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
5269 result |= VK_STRING_ERROR_BAD_DATA;
5270 }
5271 }
5272 }
5273 return result;
5274}