blob: 30a3b2d6bdbf910bed6e8f4274b96f0404271e43 [file] [log] [blame]
Mark Lobodzinski317574e2016-08-29 14:21:14 -06001/*
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002 *
Jon Ashburn23d36b12016-02-02 17:47:28 -07003 * Copyright (c) 2014-2016 The Khronos Group Inc.
4 * Copyright (c) 2014-2016 Valve Corporation
5 * Copyright (c) 2014-2016 LunarG, Inc.
Courtney Goeltzenleuchterf821dad2015-12-02 14:53:22 -07006 * Copyright (C) 2015 Google Inc.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08007 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06008 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * http://www.apache.org/licenses/LICENSE-2.0
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080013 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19
Jon Ashburn23d36b12016-02-02 17:47:28 -070020 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060021 * Author: Jon Ashburn <jon@lunarg.com>
Jon Ashburn23d36b12016-02-02 17:47:28 -070022 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060023 *
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080024 */
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070025
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060026#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080027#include <stdio.h>
28#include <stdlib.h>
29#include <stdarg.h>
30#include <stdbool.h>
31#include <string.h>
32
Chia-I Wu13a61a52014-08-04 11:18:20 +080033#include <sys/types.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050034#if defined(_WIN32)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070035#include "dirent_on_windows.h"
Johannes van Waveren9bd805012015-10-28 11:45:00 -050036#else // _WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080037#include <dirent.h>
Johannes van Waveren9bd805012015-10-28 11:45:00 -050038#endif // _WIN32
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060039#include "vk_loader_platform.h"
Chia-I Wu19300602014-08-04 08:03:57 +080040#include "loader.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060041#include "gpa_helper.h"
42#include "table_ops.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060043#include "debug_report.h"
Ian Elliott954fa342015-10-30 15:28:23 -060044#include "wsi.h"
Mark Lobodzinski317574e2016-08-29 14:21:14 -060045#include "extensions.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070046#include "vulkan/vk_icd.h"
Jon Ashburn2077e382015-06-29 11:25:34 -060047#include "cJSON.h"
Jon Ashburnfc1031e2015-11-17 15:31:02 -070048#include "murmurhash.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080049
Jon Ashburnd8ed7992016-04-04 13:52:53 -060050#if defined(__GNUC__)
Jon Ashburn10b3f832016-05-09 11:31:40 -060051#if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 17))
Jon Ashburncc407a22016-04-15 09:25:03 -060052#define secure_getenv __secure_getenv
53#endif
Jon Ashburnd8ed7992016-04-04 13:52:53 -060054#endif
55
Jon Ashburn27cd5842015-05-12 17:26:48 -060056struct loader_struct loader = {0};
Jon Ashburn87d6aa92015-08-28 15:19:27 -060057// TLS for instance for alloc/free callbacks
58THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080059
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -070060static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap00b4aba2015-09-30 11:50:51 -050061
Jon Ashburn24cd4be2015-11-01 14:04:06 -070062struct loader_phys_dev_per_icd {
63 uint32_t count;
64 VkPhysicalDevice *phys_devs;
Mark Young0153e0b2016-11-03 14:27:13 -060065 struct loader_icd_term *this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -070066};
67
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060068enum loader_debug {
Jon Ashburn23d36b12016-02-02 17:47:28 -070069 LOADER_INFO_BIT = 0x01,
70 LOADER_WARN_BIT = 0x02,
71 LOADER_PERF_BIT = 0x04,
72 LOADER_ERROR_BIT = 0x08,
73 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060074};
75
76uint32_t g_loader_debug = 0;
77uint32_t g_loader_log_msgs = 0;
78
Jon Ashburn23d36b12016-02-02 17:47:28 -070079// thread safety lock for accessing global data structures such as "loader"
Jon Ashburn6301a0f2015-05-29 13:15:39 -060080// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburn2077e382015-06-29 11:25:34 -060081// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburn6301a0f2015-05-29 13:15:39 -060082loader_platform_thread_mutex loader_lock;
Jon Ashburn6461ef22015-09-22 13:11:00 -060083loader_platform_thread_mutex loader_json_lock;
Jon Ashburn6301a0f2015-05-29 13:15:39 -060084
Jon Ashburn86a527a2016-02-10 20:59:26 -070085const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
86
Ian Elliottd3ef02f2015-07-06 14:36:13 -060087// This table contains the loader's instance dispatch table, which contains
88// default functions if no instance layers are activated. This contains
89// pointers to "terminator functions".
Jon Ashburn6301a0f2015-05-29 13:15:39 -060090const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn69e9ea22015-09-28 16:15:00 -060091 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn1530c342016-02-26 13:14:27 -070092 .DestroyInstance = terminator_DestroyInstance,
93 .EnumeratePhysicalDevices = terminator_EnumeratePhysicalDevices,
94 .GetPhysicalDeviceFeatures = terminator_GetPhysicalDeviceFeatures,
Jon Ashburn23d36b12016-02-02 17:47:28 -070095 .GetPhysicalDeviceFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -070096 terminator_GetPhysicalDeviceFormatProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -070097 .GetPhysicalDeviceImageFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -070098 terminator_GetPhysicalDeviceImageFormatProperties,
99 .GetPhysicalDeviceProperties = terminator_GetPhysicalDeviceProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700100 .GetPhysicalDeviceQueueFamilyProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700101 terminator_GetPhysicalDeviceQueueFamilyProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700102 .GetPhysicalDeviceMemoryProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700103 terminator_GetPhysicalDeviceMemoryProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700104 .EnumerateDeviceExtensionProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700105 terminator_EnumerateDeviceExtensionProperties,
106 .EnumerateDeviceLayerProperties = terminator_EnumerateDeviceLayerProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700107 .GetPhysicalDeviceSparseImageFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700108 terminator_GetPhysicalDeviceSparseImageFormatProperties,
109 .DestroySurfaceKHR = terminator_DestroySurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700110 .GetPhysicalDeviceSurfaceSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700111 terminator_GetPhysicalDeviceSurfaceSupportKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700112 .GetPhysicalDeviceSurfaceCapabilitiesKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700113 terminator_GetPhysicalDeviceSurfaceCapabilitiesKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700114 .GetPhysicalDeviceSurfaceFormatsKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700115 terminator_GetPhysicalDeviceSurfaceFormatsKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700116 .GetPhysicalDeviceSurfacePresentModesKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700117 terminator_GetPhysicalDeviceSurfacePresentModesKHR,
118 .CreateDebugReportCallbackEXT = terminator_CreateDebugReportCallback,
119 .DestroyDebugReportCallbackEXT = terminator_DestroyDebugReportCallback,
120 .DebugReportMessageEXT = terminator_DebugReportMessage,
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600121 .GetPhysicalDeviceExternalImageFormatPropertiesNV =
122 terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700123#ifdef VK_USE_PLATFORM_MIR_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700124 .CreateMirSurfaceKHR = terminator_CreateMirSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700125 .GetPhysicalDeviceMirPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700126 terminator_GetPhysicalDeviceMirPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700127#endif
128#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700129 .CreateWaylandSurfaceKHR = terminator_CreateWaylandSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700130 .GetPhysicalDeviceWaylandPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700131 terminator_GetPhysicalDeviceWaylandPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700132#endif
133#ifdef VK_USE_PLATFORM_WIN32_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700134 .CreateWin32SurfaceKHR = terminator_CreateWin32SurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700135 .GetPhysicalDeviceWin32PresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700136 terminator_GetPhysicalDeviceWin32PresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700137#endif
138#ifdef VK_USE_PLATFORM_XCB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700139 .CreateXcbSurfaceKHR = terminator_CreateXcbSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700140 .GetPhysicalDeviceXcbPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700141 terminator_GetPhysicalDeviceXcbPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700142#endif
143#ifdef VK_USE_PLATFORM_XLIB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700144 .CreateXlibSurfaceKHR = terminator_CreateXlibSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700145 .GetPhysicalDeviceXlibPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700146 terminator_GetPhysicalDeviceXlibPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700147#endif
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700148#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700149 .CreateAndroidSurfaceKHR = terminator_CreateAndroidSurfaceKHR,
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700150#endif
Jon Ashburnc7d3e732016-03-08 09:30:30 -0700151 .GetPhysicalDeviceDisplayPropertiesKHR =
152 terminator_GetPhysicalDeviceDisplayPropertiesKHR,
153 .GetPhysicalDeviceDisplayPlanePropertiesKHR =
154 terminator_GetPhysicalDeviceDisplayPlanePropertiesKHR,
155 .GetDisplayPlaneSupportedDisplaysKHR =
156 terminator_GetDisplayPlaneSupportedDisplaysKHR,
Jon Ashburncc407a22016-04-15 09:25:03 -0600157 .GetDisplayModePropertiesKHR = terminator_GetDisplayModePropertiesKHR,
158 .CreateDisplayModeKHR = terminator_CreateDisplayModeKHR,
159 .GetDisplayPlaneCapabilitiesKHR = terminator_GetDisplayPlaneCapabilitiesKHR,
160 .CreateDisplayPlaneSurfaceKHR = terminator_CreateDisplayPlaneSurfaceKHR,
Jon Ashburn27cd5842015-05-12 17:26:48 -0600161};
162
Jon Ashburn8810c5f2015-08-18 18:04:47 -0600163LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700164
Mark Young0ad83132016-06-30 13:02:42 -0600165void *loader_instance_heap_alloc(const struct loader_instance *instance,
166 size_t size,
167 VkSystemAllocationScope alloc_scope) {
168 void *pMemory = NULL;
169#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
170 {
171#else
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800172 if (instance && instance->alloc_callbacks.pfnAllocation) {
Mark Young0ad83132016-06-30 13:02:42 -0600173 /* These are internal structures, so it's best to align everything to
174 * the largest unit size which is the size of a uint64_t.
175 */
176 pMemory = instance->alloc_callbacks.pfnAllocation(
177 instance->alloc_callbacks.pUserData, size, sizeof(uint64_t),
Jon Ashburn23d36b12016-02-02 17:47:28 -0700178 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600179 } else {
180#endif
181 pMemory = malloc(size);
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600182 }
Mark Young0ad83132016-06-30 13:02:42 -0600183 return pMemory;
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600184}
185
Mark Young0ad83132016-06-30 13:02:42 -0600186void loader_instance_heap_free(const struct loader_instance *instance,
187 void *pMemory) {
188 if (pMemory != NULL) {
189#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
190 {
191#else
192 if (instance && instance->alloc_callbacks.pfnFree) {
193 instance->alloc_callbacks.pfnFree(
194 instance->alloc_callbacks.pUserData, pMemory);
195 } else {
196#endif
197 free(pMemory);
Mark Youngd077f992016-06-30 11:03:59 -0600198 }
Mark Young4b0b9222016-06-29 18:33:53 -0600199 }
Mark Young4b0b9222016-06-29 18:33:53 -0600200}
201
Mark Young0ad83132016-06-30 13:02:42 -0600202void *loader_instance_heap_realloc(const struct loader_instance *instance,
203 void *pMemory, size_t orig_size, size_t size,
204 VkSystemAllocationScope alloc_scope) {
205 void *pNewMem = NULL;
206 if (pMemory == NULL || orig_size == 0) {
207 pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
208 } else if (size == 0) {
209 loader_instance_heap_free(instance, pMemory);
210#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
211#else
212 } else if (instance && instance->alloc_callbacks.pfnReallocation) {
213 /* These are internal structures, so it's best to align everything to
214 * the largest unit size which is the size of a uint64_t.
215 */
216 pNewMem = instance->alloc_callbacks.pfnReallocation(
217 instance->alloc_callbacks.pUserData, pMemory, size,
218 sizeof(uint64_t), alloc_scope);
219#endif
220 } else {
221 pNewMem = realloc(pMemory, size);
222 }
223 return pNewMem;
Mark Young4b0b9222016-06-29 18:33:53 -0600224}
225
Mark Young0ad83132016-06-30 13:02:42 -0600226void *loader_instance_tls_heap_alloc(size_t size) {
227 return loader_instance_heap_alloc(tls_instance, size,
228 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young4b0b9222016-06-29 18:33:53 -0600229}
Mark Young4b0b9222016-06-29 18:33:53 -0600230
Mark Young0ad83132016-06-30 13:02:42 -0600231void loader_instance_tls_heap_free(void *pMemory) {
232 loader_instance_heap_free(tls_instance, pMemory);
233}
234
235void *loader_device_heap_alloc(const struct loader_device *device, size_t size,
236 VkSystemAllocationScope alloc_scope) {
237 void *pMemory = NULL;
238#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
239 {
240#else
241 if (device && device->alloc_callbacks.pfnAllocation) {
242 /* These are internal structures, so it's best to align everything to
243 * the largest unit size which is the size of a uint64_t.
244 */
245 pMemory = device->alloc_callbacks.pfnAllocation(
246 device->alloc_callbacks.pUserData, size, sizeof(uint64_t),
247 alloc_scope);
248 } else {
249#endif
250 pMemory = malloc(size);
251 }
252 return pMemory;
253}
254
255void loader_device_heap_free(const struct loader_device *device,
256 void *pMemory) {
257 if (pMemory != NULL) {
258#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
259 {
260#else
261 if (device && device->alloc_callbacks.pfnFree) {
262 device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData,
263 pMemory);
264 } else {
265#endif
266 free(pMemory);
267 }
268 }
269}
270
271void *loader_device_heap_realloc(const struct loader_device *device,
272 void *pMemory, size_t orig_size, size_t size,
273 VkSystemAllocationScope alloc_scope) {
274 void *pNewMem = NULL;
275 if (pMemory == NULL || orig_size == 0) {
276 pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
277 } else if (size == 0) {
278 loader_device_heap_free(device, pMemory);
279#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
280#else
281 } else if (device && device->alloc_callbacks.pfnReallocation) {
282 /* These are internal structures, so it's best to align everything to
283 * the largest unit size which is the size of a uint64_t.
284 */
285 pNewMem = device->alloc_callbacks.pfnReallocation(
286 device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
287 alloc_scope);
288#endif
289 } else {
290 pNewMem = realloc(pMemory, size);
291 }
292 return pNewMem;
293}
294
295// Environment variables
296#if defined(__linux__)
297
298static inline char *loader_getenv(const char *name,
299 const struct loader_instance *inst) {
300 // No allocation of memory necessary for Linux, but we should at least touch
301 // the inst pointer to get rid of compiler warnings.
302 (void)inst;
303 return getenv(name);
304}
305static inline void loader_free_getenv(const char *val,
306 const struct loader_instance *inst) {
307 // No freeing of memory necessary for Linux, but we should at least touch
308 // the val and inst pointers to get rid of compiler warnings.
309 (void)val;
310 (void)inst;
311}
312
313#elif defined(WIN32)
314
315static inline char *loader_getenv(const char *name,
316 const struct loader_instance *inst) {
317 char *retVal;
318 DWORD valSize;
319
320 valSize = GetEnvironmentVariableA(name, NULL, 0);
321
322 // valSize DOES include the null terminator, so for any set variable
323 // will always be at least 1. If it's 0, the variable wasn't set.
324 if (valSize == 0)
325 return NULL;
326
327 // Allocate the space necessary for the registry entry
328 if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
329 retVal = (char *)inst->alloc_callbacks.pfnAllocation(
330 inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
331 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
332 } else {
333 retVal = (char *)malloc(valSize);
334 }
335
336 if (NULL != retVal) {
337 GetEnvironmentVariableA(name, retVal, valSize);
338 }
339
340 return retVal;
341}
342
343static inline void loader_free_getenv(char *val,
344 const struct loader_instance *inst) {
345 if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
346 inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
347 } else {
348 free((void *)val);
349 }
350}
351
352#else
353
354static inline char *loader_getenv(const char *name,
Mark Young0153e0b2016-11-03 14:27:13 -0600355 const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600356 // stub func
357 (void)inst;
358 (void)name;
359 return NULL;
360}
361static inline void loader_free_getenv(const char *val,
Mark Young0153e0b2016-11-03 14:27:13 -0600362 const struct loader_instance *inst) {
Mark Young0ad83132016-06-30 13:02:42 -0600363 // stub func
364 (void)val;
365 (void)inst;
366}
367
368#endif
369
Jon Ashburn2e37d752016-02-12 08:20:06 -0700370void loader_log(const struct loader_instance *inst, VkFlags msg_type,
Jon Ashburn1530c342016-02-26 13:14:27 -0700371 int32_t msg_code, const char *format, ...) {
Jon Ashburn86723b02015-07-31 15:47:59 -0600372 char msg[512];
Jon Ashburnffad94d2015-06-30 14:46:22 -0700373 va_list ap;
374 int ret;
375
Jon Ashburnffad94d2015-06-30 14:46:22 -0700376 va_start(ap, format);
377 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700378 if ((ret >= (int)sizeof(msg)) || ret < 0) {
379 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnffad94d2015-06-30 14:46:22 -0700380 }
381 va_end(ap);
382
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700383 if (inst) {
Karl Schultz94971292016-11-19 09:02:27 -0700384 util_DebugReportMessage(
385 inst, msg_type, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
386 (uint64_t)(uintptr_t)inst, 0, msg_code, "loader", msg);
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700387 }
388
389 if (!(msg_type & g_loader_log_msgs)) {
390 return;
391 }
392
Ian Elliott4470a302015-02-17 10:33:47 -0700393#if defined(WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -0700394 OutputDebugString(msg);
mschottb9cdb782015-07-22 14:11:29 +0200395 OutputDebugString("\n");
Jon Ashburnffad94d2015-06-30 14:46:22 -0700396#endif
397 fputs(msg, stderr);
398 fputc('\n', stderr);
399}
400
Mark Young0153e0b2016-11-03 14:27:13 -0600401VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceDispatch(VkInstance instance,
402 void *object) {
Jon Ashburnc3c58772016-03-29 11:16:01 -0600403
404 struct loader_instance *inst = loader_get_instance(instance);
405 if (!inst) {
406 return VK_ERROR_INITIALIZATION_FAILED;
407 }
408 loader_set_dispatch(object, inst->disp);
409 return VK_SUCCESS;
410}
411
Mark Young0153e0b2016-11-03 14:27:13 -0600412VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceDispatch(VkDevice device,
413 void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600414 struct loader_device *dev;
Mark Young0153e0b2016-11-03 14:27:13 -0600415 struct loader_icd_term *icd_term =
416 loader_get_icd_and_device(device, &dev, NULL);
Jon Ashburned8f2312016-03-31 10:52:22 -0600417
Mark Young0153e0b2016-11-03 14:27:13 -0600418 if (NULL == icd_term) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600419 return VK_ERROR_INITIALIZATION_FAILED;
420 }
421 loader_set_dispatch(object, &dev->loader_dispatch);
422 return VK_SUCCESS;
423}
424
Jon Ashburnffad94d2015-06-30 14:46:22 -0700425#if defined(WIN32)
Tony Barbourea968902015-07-29 14:26:21 -0600426static char *loader_get_next_path(char *path);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700427/**
428* Find the list of registry files (names within a key) in key "location".
429*
Jon Ashburn23d36b12016-02-02 17:47:28 -0700430* This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
431*given in "location"
432* for a list or name/values which are added to a returned list (function return
433*value).
Jon Ashburnffad94d2015-06-30 14:46:22 -0700434* The DWORD values within the key must be 0 or they are skipped.
Jon Ashburne39a4f82015-08-28 13:38:21 -0600435* Function return is a string with a ';' separated list of filenames.
Jon Ashburnffad94d2015-06-30 14:46:22 -0700436* Function return is NULL if no valid name/value pairs are found in the key,
437* or the key is not found.
438*
439* \returns
440* A string list of filenames as pointer.
441* When done using the returned string list, pointer should be freed.
442*/
Jon Ashburn23d36b12016-02-02 17:47:28 -0700443static char *loader_get_registry_files(const struct loader_instance *inst,
444 char *location) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700445 LONG rtn_value;
446 HKEY hive, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700447 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700448 char name[2048];
449 char *out = NULL;
Tony Barbourea968902015-07-29 14:26:21 -0600450 char *loc = location;
451 char *next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700452 DWORD idx = 0;
453 DWORD name_size = sizeof(name);
454 DWORD value;
455 DWORD total_size = 4096;
456 DWORD value_size = sizeof(value);
Tony Barbourea968902015-07-29 14:26:21 -0600457
Jon Ashburn23d36b12016-02-02 17:47:28 -0700458 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600459 next = loader_get_next_path(loc);
460 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniell524ec732015-11-05 16:58:26 -0700461 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600462 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
463 if (rtn_value != ERROR_SUCCESS) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700464 // We still couldn't find the key, so give up:
465 loc = next;
466 continue;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700467 }
Tony Barbourea968902015-07-29 14:26:21 -0600468
Jon Ashburn23d36b12016-02-02 17:47:28 -0700469 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL,
470 NULL, (LPBYTE)&value, &value_size)) ==
471 ERROR_SUCCESS) {
Tony Barbourea968902015-07-29 14:26:21 -0600472 if (value_size == sizeof(value) && value == 0) {
473 if (out == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -0600474 out = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700475 inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600476 if (NULL == out) {
477 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young0153e0b2016-11-03 14:27:13 -0600478 "Out of memory can't alloc space for "
479 "registry data");
Mark Young0ad83132016-06-30 13:02:42 -0600480 return NULL;
481 }
Tony Barbourea968902015-07-29 14:26:21 -0600482 out[0] = '\0';
Jon Ashburn23d36b12016-02-02 17:47:28 -0700483 } else if (strlen(out) + name_size + 1 > total_size) {
Mark Young0ad83132016-06-30 13:02:42 -0600484 out = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700485 inst, out, total_size, total_size * 2,
486 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600487 if (NULL == out) {
488 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young0153e0b2016-11-03 14:27:13 -0600489 "Out of memory can't realloc space for "
490 "registry data");
Mark Young0ad83132016-06-30 13:02:42 -0600491 return NULL;
492 }
Tony Barbourea968902015-07-29 14:26:21 -0600493 total_size *= 2;
494 }
Tony Barbourea968902015-07-29 14:26:21 -0600495 if (strlen(out) == 0)
Jon Ashburn23d36b12016-02-02 17:47:28 -0700496 snprintf(out, name_size + 1, "%s", name);
Tony Barbourea968902015-07-29 14:26:21 -0600497 else
Jon Ashburn23d36b12016-02-02 17:47:28 -0700498 snprintf(out + strlen(out), name_size + 2, "%c%s",
499 PATH_SEPERATOR, name);
Tony Barbourea968902015-07-29 14:26:21 -0600500 }
501 name_size = 2048;
502 }
503 loc = next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700504 }
Tony Barbourea968902015-07-29 14:26:21 -0600505
Jon Ashburnffad94d2015-06-30 14:46:22 -0700506 return out;
507}
508
Ian Elliott4470a302015-02-17 10:33:47 -0700509#endif // WIN32
510
Jon Ashburnc7237a72015-08-03 09:08:46 -0600511/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500512 * Combine path elements, separating each element with the platform-specific
513 * directory separator, and save the combined string to a destination buffer,
514 * not exceeding the given length. Path elements are given as variadic args,
515 * with a NULL element terminating the list.
516 *
517 * \returns the total length of the combined string, not including an ASCII
518 * NUL termination character. This length may exceed the available storage:
519 * in this case, the written string will be truncated to avoid a buffer
520 * overrun, and the return value will greater than or equal to the storage
521 * size. A NULL argument may be provided as the destination buffer in order
522 * to determine the required string length without actually writing a string.
523 */
524
Jon Ashburn23d36b12016-02-02 17:47:28 -0700525static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700526 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500527 va_list ap;
528 const char *component;
529
530 va_start(ap, len);
531
Jon Ashburn23d36b12016-02-02 17:47:28 -0700532 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500533 if (required_len > 0) {
534 // This path element is not the first non-empty element; prepend
535 // a directory separator if space allows
536 if (dest && required_len + 1 < len) {
537 snprintf(dest + required_len, len - required_len, "%c",
538 DIRECTORY_SYMBOL);
539 }
540 required_len++;
541 }
542
543 if (dest && required_len < len) {
544 strncpy(dest + required_len, component, len - required_len);
545 }
546 required_len += strlen(component);
547 }
548
549 va_end(ap);
550
551 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
552 if (dest && required_len >= len) {
553 dest[len - 1] = '\0';
554 }
555
556 return required_len;
557}
558
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500559/**
Jon Ashburnc7237a72015-08-03 09:08:46 -0600560 * Given string of three part form "maj.min.pat" convert to a vulkan version
561 * number.
562 */
Mark Young60861ac2016-09-02 11:39:26 -0600563static uint32_t loader_make_version(char *vers_str) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700564 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
Mark Young60861ac2016-09-02 11:39:26 -0600565 char *vers_tok;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600566
Mark Young60861ac2016-09-02 11:39:26 -0600567 if (!vers_str) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600568 return vers;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600569 }
Mark Young60861ac2016-09-02 11:39:26 -0600570
571 vers_tok = strtok(vers_str, ".\"\n\r");
572 if (NULL != vers_tok) {
573 major = (uint16_t)atoi(vers_tok);
574 vers_tok = strtok(NULL, ".\"\n\r");
575 if (NULL != vers_tok) {
576 minor = (uint16_t)atoi(vers_tok);
577 vers_tok = strtok(NULL, ".\"\n\r");
578 if (NULL != vers_tok) {
579 patch = (uint16_t)atoi(vers_tok);
580 }
581 }
582 }
Jon Ashburnc7237a72015-08-03 09:08:46 -0600583
584 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600585}
586
Jon Ashburn23d36b12016-02-02 17:47:28 -0700587bool compare_vk_extension_properties(const VkExtensionProperties *op1,
588 const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800589 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600590}
591
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600592/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600593 * Search the given ext_array for an extension
594 * matching the given vk_ext_prop
595 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700596bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
597 const uint32_t count,
598 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600599 for (uint32_t i = 0; i < count; i++) {
600 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i]))
601 return true;
602 }
603 return false;
604}
605
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600606/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600607 * Search the given ext_list for an extension
608 * matching the given vk_ext_prop
609 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700610bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop,
611 const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600612 for (uint32_t i = 0; i < ext_list->count; i++) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600613 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop))
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600614 return true;
615 }
616 return false;
617}
618
Jon Ashburnb8726962016-04-08 15:03:35 -0600619/**
620 * Search the given ext_list for a device extension matching the given ext_prop
621 */
Jon Ashburncc407a22016-04-15 09:25:03 -0600622bool has_vk_dev_ext_property(
623 const VkExtensionProperties *ext_prop,
624 const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600625 for (uint32_t i = 0; i < ext_list->count; i++) {
626 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop))
627 return true;
628 }
629 return false;
630}
631
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600632/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600633 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600634 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700635static struct loader_layer_properties *
636loader_get_layer_property(const char *name,
637 const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600638 for (uint32_t i = 0; i < layer_list->count; i++) {
639 const VkLayerProperties *item = &layer_list->list[i].info;
640 if (strcmp(name, item->layerName) == 0)
641 return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600642 }
643 return NULL;
644}
645
Jon Ashburne13ecc92015-08-03 17:19:30 -0600646/**
647 * Get the next unused layer property in the list. Init the property to zero.
648 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700649static struct loader_layer_properties *
650loader_get_next_layer_property(const struct loader_instance *inst,
651 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600652 if (layer_list->capacity == 0) {
Mark Young0153e0b2016-11-03 14:27:13 -0600653 layer_list->list = loader_instance_heap_alloc(
654 inst, sizeof(struct loader_layer_properties) * 64,
655 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600656 if (layer_list->list == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700657 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
658 "Out of memory can't add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600659 return NULL;
660 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700661 memset(layer_list->list, 0,
662 sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600663 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
664 }
665
666 // ensure enough room to add an entry
Jon Ashburn23d36b12016-02-02 17:47:28 -0700667 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
668 layer_list->capacity) {
Mark Young0ad83132016-06-30 13:02:42 -0600669 layer_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700670 inst, layer_list->list, layer_list->capacity,
671 layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600672 if (layer_list->list == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700673 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700674 "realloc failed for layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600675 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600676 }
677 layer_list->capacity *= 2;
678 }
679
680 layer_list->count++;
681 return &(layer_list->list[layer_list->count - 1]);
682}
683
684/**
685 * Remove all layer properties entrys from the list
686 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700687void loader_delete_layer_properties(const struct loader_instance *inst,
688 struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700689 uint32_t i, j;
690 struct loader_device_extension_list *dev_ext_list;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600691 if (!layer_list)
692 return;
693
Jon Ashburne13ecc92015-08-03 17:19:30 -0600694 for (i = 0; i < layer_list->count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700695 loader_destroy_generic_list(
696 inst, (struct loader_generic_list *)&layer_list->list[i]
697 .instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700698 dev_ext_list = &layer_list->list[i].device_extension_list;
Mark Young0153e0b2016-11-03 14:27:13 -0600699 if (dev_ext_list->capacity > 0 && NULL != dev_ext_list->list &&
Jon Ashburn23d36b12016-02-02 17:47:28 -0700700 dev_ext_list->list->entrypoint_count > 0) {
701 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Young0153e0b2016-11-03 14:27:13 -0600702 loader_instance_heap_free(inst,
703 dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700704 }
Mark Young0ad83132016-06-30 13:02:42 -0600705 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700706 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700707 loader_destroy_generic_list(inst,
708 (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600709 }
710 layer_list->count = 0;
711
Jon Ashburnb82c1852015-08-11 14:49:54 -0600712 if (layer_list->capacity > 0) {
713 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600714 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600715 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600716}
717
Mark Young3a587792016-08-19 15:25:08 -0600718static VkResult loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700719 const struct loader_instance *inst,
720 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props,
721 const char *lib_name, struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700722 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600723 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600724 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600725
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600726 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600727 /* No EnumerateInstanceExtensionProperties defined */
Mark Young3a587792016-08-19 15:25:08 -0600728 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600729 }
730
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600731 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600732 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700733 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700734 "Error getting Instance extension count from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600735 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600736 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600737
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600738 if (count == 0) {
739 /* No ExtensionProperties to report */
Mark Young3a587792016-08-19 15:25:08 -0600740 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600741 }
742
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600743 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600744
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600745 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600746 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700747 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700748 "Error getting Instance extensions from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600749 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600750 }
Tony Barbour59a47322015-06-24 16:06:58 -0600751
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600752 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600753 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600754
Jon Ashburncc407a22016-04-15 09:25:03 -0600755 bool ext_unsupported =
756 wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600757 if (!ext_unsupported) {
758 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
759 VK_MAJOR(ext_props[i].specVersion),
760 VK_MINOR(ext_props[i].specVersion),
761 VK_PATCH(ext_props[i].specVersion));
762 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
763 "Instance Extension: %s (%s) version %s",
764 ext_props[i].extensionName, lib_name, spec_version);
Mark Young3a587792016-08-19 15:25:08 -0600765 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
766 if (res != VK_SUCCESS) {
767 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
768 "Failed to add %s to Instance extension list",
769 lib_name);
770 goto out;
771 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600772 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600773 }
Mark Young3a587792016-08-19 15:25:08 -0600774out:
775 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600776}
777
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700778/*
779 * Initialize ext_list with the physical device extensions.
780 * The extension properties are passed as inputs in count and ext_props.
781 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700782static VkResult
783loader_init_device_extensions(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -0600784 struct loader_physical_device_term *phys_dev_term,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700785 uint32_t count, VkExtensionProperties *ext_props,
786 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700787 VkResult res;
788 uint32_t i;
789
Mark Young3a587792016-08-19 15:25:08 -0600790 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
791 sizeof(VkExtensionProperties));
792 if (VK_SUCCESS != res) {
793 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700794 }
795
796 for (i = 0; i < count; i++) {
797 char spec_version[64];
798
Jon Ashburn23d36b12016-02-02 17:47:28 -0700799 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
800 VK_MAJOR(ext_props[i].specVersion),
801 VK_MINOR(ext_props[i].specVersion),
802 VK_PATCH(ext_props[i].specVersion));
Mark Young0153e0b2016-11-03 14:27:13 -0600803 loader_log(
804 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
805 "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
806 phys_dev_term->this_icd_term->scanned_icd->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700807 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
808 if (res != VK_SUCCESS)
809 return res;
810 }
811
812 return VK_SUCCESS;
813}
814
Jon Ashburn1530c342016-02-26 13:14:27 -0700815VkResult loader_add_device_extensions(const struct loader_instance *inst,
Jon Ashburncc407a22016-04-15 09:25:03 -0600816 PFN_vkEnumerateDeviceExtensionProperties
817 fpEnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700818 VkPhysicalDevice physical_device,
819 const char *lib_name,
820 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600821 uint32_t i, count;
822 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600823 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600824
Piers Daniell295fe402016-03-29 11:51:11 -0600825 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count,
826 NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700827 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700828 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Mark Young9a3ddd42016-10-21 16:25:47 -0600829 if (!ext_props) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700830 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young9a3ddd42016-10-21 16:25:47 -0600831 }
Piers Daniell295fe402016-03-29 11:51:11 -0600832 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
833 &count, ext_props);
Mark Young9a3ddd42016-10-21 16:25:47 -0600834 if (res != VK_SUCCESS) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700835 return res;
Mark Young9a3ddd42016-10-21 16:25:47 -0600836 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700837 for (i = 0; i < count; i++) {
838 char spec_version[64];
839
Jon Ashburn23d36b12016-02-02 17:47:28 -0700840 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
841 VK_MAJOR(ext_props[i].specVersion),
842 VK_MINOR(ext_props[i].specVersion),
843 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700844 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700845 "Device Extension: %s (%s) version %s",
846 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700847 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
848 if (res != VK_SUCCESS)
849 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600850 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700851 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700852 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
853 "Error getting physical device extension info count from "
854 "library %s",
855 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700856 return res;
857 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600858
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700859 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600860}
861
Mark Young3a587792016-08-19 15:25:08 -0600862VkResult loader_init_generic_list(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -0600863 struct loader_generic_list *list_info,
864 size_t element_size) {
Mark Young84ba0482016-09-02 11:45:00 -0600865 size_t capacity = 32 * element_size;
866 list_info->count = 0;
867 list_info->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600868 list_info->list = loader_instance_heap_alloc(
Mark Young84ba0482016-09-02 11:45:00 -0600869 inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700870 if (list_info->list == NULL) {
Mark Young3a587792016-08-19 15:25:08 -0600871 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600872 }
Mark Young84ba0482016-09-02 11:45:00 -0600873 memset(list_info->list, 0, capacity);
874 list_info->capacity = capacity;
Mark Young3a587792016-08-19 15:25:08 -0600875 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600876}
877
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700878void loader_destroy_generic_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700879 struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600880 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700881 list->count = 0;
882 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600883}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600884
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600885/*
886 * Append non-duplicate extension properties defined in props
887 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700888 * Return
889 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600890 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700891VkResult loader_add_to_ext_list(const struct loader_instance *inst,
892 struct loader_extension_list *ext_list,
893 uint32_t prop_list_count,
894 const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600895 uint32_t i;
896 const VkExtensionProperties *cur_ext;
897
898 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young0153e0b2016-11-03 14:27:13 -0600899 VkResult res = loader_init_generic_list(
900 inst, (struct loader_generic_list *)ext_list,
901 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600902 if (VK_SUCCESS != res) {
903 return res;
904 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600905 }
906
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600907 for (i = 0; i < prop_list_count; i++) {
908 cur_ext = &props[i];
909
910 // look for duplicates
911 if (has_vk_extension_property(cur_ext, ext_list)) {
912 continue;
913 }
914
915 // add to list at end
916 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700917 if (ext_list->count * sizeof(VkExtensionProperties) >=
918 ext_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -0600919
Mark Young0ad83132016-06-30 13:02:42 -0600920 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700921 inst, ext_list->list, ext_list->capacity,
922 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700923
924 if (ext_list->list == NULL)
925 return VK_ERROR_OUT_OF_HOST_MEMORY;
926
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600927 // double capacity
928 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600929 }
930
Jon Ashburn23d36b12016-02-02 17:47:28 -0700931 memcpy(&ext_list->list[ext_list->count], cur_ext,
932 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600933 ext_list->count++;
934 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700935 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600936}
937
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700938/*
939 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600940 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700941 * Return
942 * Vk_SUCCESS on success
943 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700944VkResult
945loader_add_to_dev_ext_list(const struct loader_instance *inst,
946 struct loader_device_extension_list *ext_list,
947 const VkExtensionProperties *props,
948 uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700949 uint32_t idx;
950 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600951 VkResult res = loader_init_generic_list(
952 inst, (struct loader_generic_list *)ext_list,
953 sizeof(struct loader_dev_ext_props));
954 if (VK_SUCCESS != res) {
955 return res;
956 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700957 }
958
Jon Ashburnb8726962016-04-08 15:03:35 -0600959 // look for duplicates
960 if (has_vk_dev_ext_property(props, ext_list)) {
961 return VK_SUCCESS;
962 }
963
Jon Ashburn23d36b12016-02-02 17:47:28 -0700964 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700965 // add to list at end
966 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700967 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700968
Mark Young0ad83132016-06-30 13:02:42 -0600969 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700970 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
971 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700972
973 if (ext_list->list == NULL)
974 return VK_ERROR_OUT_OF_HOST_MEMORY;
975
976 // double capacity
977 ext_list->capacity *= 2;
978 }
979
Jon Ashburn23d36b12016-02-02 17:47:28 -0700980 memcpy(&ext_list->list[idx].props, props,
981 sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700982 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700983 ext_list->list[idx].entrypoints =
Mark Young0ad83132016-06-30 13:02:42 -0600984 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
985 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
986 if (ext_list->list[idx].entrypoints == NULL) {
987 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700988 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600989 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700990 for (uint32_t i = 0; i < entry_count; i++) {
Mark Young0ad83132016-06-30 13:02:42 -0600991 ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700992 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600993 if (ext_list->list[idx].entrypoints[i] == NULL) {
994 for (uint32_t j = 0; j < i; j++) {
995 loader_instance_heap_free(inst,
996 ext_list->list[idx].entrypoints[j]);
997 }
998 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
999 ext_list->list[idx].entrypoint_count = 0;
1000 ext_list->list[idx].entrypoints = NULL;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001001 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001002 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001003 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
1004 }
1005 ext_list->count++;
1006
1007 return VK_SUCCESS;
1008}
1009
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001010/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001011 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001012 * Add these to the output layer_list. Don't add duplicates to the output
1013 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001014 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001015static VkResult
1016loader_add_layer_names_to_list(const struct loader_instance *inst,
1017 struct loader_layer_list *output_list,
1018 uint32_t name_count, const char *const *names,
1019 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001020 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001021 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001022
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001023 for (uint32_t i = 0; i < name_count; i++) {
1024 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001025 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001026 if (!layer_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001027 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1028 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001029 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001030 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001031 }
1032
Mark Young0ad83132016-06-30 13:02:42 -06001033 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001034 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001035
1036 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001037}
1038
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001039/*
1040 * Manage lists of VkLayerProperties
1041 */
Jon Ashburne39a4f82015-08-28 13:38:21 -06001042static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001043 struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001044 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Young0ad83132016-06-30 13:02:42 -06001045 list->list = loader_instance_heap_alloc(
1046 inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001047 if (list->list == NULL) {
1048 return false;
1049 }
1050 memset(list->list, 0, list->capacity);
1051 list->count = 0;
1052 return true;
1053}
1054
Jon Ashburne39a4f82015-08-28 13:38:21 -06001055void loader_destroy_layer_list(const struct loader_instance *inst,
Mark Young0ad83132016-06-30 13:02:42 -06001056 struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001057 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001058 if (device) {
1059 loader_device_heap_free(device, layer_list->list);
1060 } else {
1061 loader_instance_heap_free(inst, layer_list->list);
1062 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001063 layer_list->count = 0;
1064 layer_list->capacity = 0;
1065}
1066
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001067/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001068 * Search the given layer list for a list
1069 * matching the given VkLayerProperties
1070 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001071bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
1072 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001073 for (uint32_t i = 0; i < list->count; i++) {
1074 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
1075 return true;
1076 }
1077 return false;
1078}
1079
1080/*
1081 * Search the given layer list for a layer
1082 * matching the given name
1083 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001084bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001085 for (uint32_t i = 0; i < list->count; i++) {
1086 if (strcmp(name, list->list[i].info.layerName) == 0)
1087 return true;
1088 }
1089 return false;
1090}
1091
1092/*
1093 * Append non-duplicate layer properties defined in prop_list
1094 * to the given layer_info list
1095 */
Mark Young0ad83132016-06-30 13:02:42 -06001096VkResult loader_add_to_layer_list(const struct loader_instance *inst,
1097 struct loader_layer_list *list,
1098 uint32_t prop_list_count,
1099 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001100 uint32_t i;
1101 struct loader_layer_properties *layer;
1102
1103 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001104 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001105 }
1106
1107 if (list->list == NULL)
Mark Young0ad83132016-06-30 13:02:42 -06001108 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001109
1110 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001111 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001112
1113 // look for duplicates
1114 if (has_vk_layer_property(&layer->info, list)) {
1115 continue;
1116 }
1117
1118 // add to list at end
1119 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001120 if (list->count * sizeof(struct loader_layer_properties) >=
1121 list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001122
Mark Young0ad83132016-06-30 13:02:42 -06001123 list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001124 inst, list->list, list->capacity, list->capacity * 2,
1125 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001126 if (NULL == list->list) {
1127 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1128 "realloc failed for layer list when attempting to "
1129 "add new layer");
1130 return VK_ERROR_OUT_OF_HOST_MEMORY;
1131 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001132 // double capacity
1133 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001134 }
1135
Jon Ashburn23d36b12016-02-02 17:47:28 -07001136 memcpy(&list->list[list->count], layer,
1137 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001138 list->count++;
1139 }
Mark Young0ad83132016-06-30 13:02:42 -06001140
1141 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001142}
1143
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001144/**
1145 * Search the search_list for any layer with a name
1146 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001147 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001148 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001149 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001150 */
Jon Ashburncc407a22016-04-15 09:25:03 -06001151void loader_find_layer_name_add_list(
1152 const struct loader_instance *inst, const char *name,
1153 const enum layer_type type, const struct loader_layer_list *search_list,
1154 struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001155 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001156 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001157 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001158 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001159 (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001160 /* Found a layer with the same name, add to found_list */
Mark Young0153e0b2016-11-03 14:27:13 -06001161 if (VK_SUCCESS ==
1162 loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
Mark Young0ad83132016-06-30 13:02:42 -06001163 found = true;
1164 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001165 }
1166 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001167 if (!found) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001168 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001169 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001170 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001171}
1172
Jon Ashburn23d36b12016-02-02 17:47:28 -07001173static VkExtensionProperties *
1174get_extension_property(const char *name,
1175 const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001176 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001177 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001178 return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001179 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001180 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001181}
1182
Jon Ashburn23d36b12016-02-02 17:47:28 -07001183static VkExtensionProperties *
1184get_dev_extension_property(const char *name,
1185 const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001186 for (uint32_t i = 0; i < list->count; i++) {
1187 if (strcmp(name, list->list[i].props.extensionName) == 0)
1188 return &list->list[i].props;
1189 }
1190 return NULL;
1191}
1192
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001193/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001194 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001195 * the extension must provide two entry points for the loader to use:
1196 * - "trampoline" entry point - this is the address returned by GetProcAddr
1197 * and will always do what's necessary to support a global call.
1198 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001199 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001200 * the extension for the appropriate ICDs that are available.
1201 * There is no generic mechanism for including these functions, the references
1202 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001203 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1204 * GetProcAddr requests
1205 * loader_coalesce_extensions(void) - add extension records to the list of
1206 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001207 * extension available to the app.
1208 * instance_disp - add function pointer for terminator function to this array.
1209 * The extension itself should be in a separate file that will be
1210 * linked directly with the loader.
1211 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001212
Mark Young3a587792016-08-19 15:25:08 -06001213VkResult loader_get_icd_loader_instance_extensions(
Mark Young0153e0b2016-11-03 14:27:13 -06001214 const struct loader_instance *inst,
1215 struct loader_icd_tramp_list *icd_tramp_list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001216 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001217 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001218 VkResult res = VK_SUCCESS;
1219
Jon Ashburn23d36b12016-02-02 17:47:28 -07001220 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1221 "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001222
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001223 // traverse scanned icd list adding non-duplicate extensions to the list
Mark Young0153e0b2016-11-03 14:27:13 -06001224 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
Mark Young3a587792016-08-19 15:25:08 -06001225 res = loader_init_generic_list(inst,
1226 (struct loader_generic_list *)&icd_exts,
1227 sizeof(VkExtensionProperties));
1228 if (VK_SUCCESS != res) {
1229 goto out;
1230 }
1231 res = loader_add_instance_extensions(
Mark Young0153e0b2016-11-03 14:27:13 -06001232 inst, icd_tramp_list->scanned_list[i]
1233 .EnumerateInstanceExtensionProperties,
1234 icd_tramp_list->scanned_list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001235 if (VK_SUCCESS == res) {
1236 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count,
1237 icd_exts.list);
1238 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001239 loader_destroy_generic_list(inst,
1240 (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001241 if (VK_SUCCESS != res) {
1242 goto out;
1243 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001244 };
1245
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001246 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001247 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001248
1249out:
1250 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001251}
1252
Mark Young0153e0b2016-11-03 14:27:13 -06001253struct loader_icd_term *
1254loader_get_icd_and_device(const VkDevice device,
1255 struct loader_device **found_dev,
1256 uint32_t *icd_index) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001257 *found_dev = NULL;
Mark Young16573c72016-06-28 10:52:43 -06001258 uint32_t index = 0;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001259 for (struct loader_instance *inst = loader.instances; inst;
1260 inst = inst->next) {
Mark Young0153e0b2016-11-03 14:27:13 -06001261 for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term;
1262 icd_term = icd_term->next) {
1263 for (struct loader_device *dev = icd_term->logical_device_list; dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001264 dev = dev->next)
Mark Young65cb3662016-11-07 13:27:02 -07001265 // Value comparison of device prevents object wrapping by layers
1266 if (loader_get_dispatch(dev->icd_device) ==
1267 loader_get_dispatch(device) ||
1268 loader_get_dispatch(dev->chain_device) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07001269 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001270 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001271 if (NULL != icd_index) {
1272 *icd_index = index;
1273 }
Mark Young0153e0b2016-11-03 14:27:13 -06001274 return icd_term;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001275 }
Mark Young16573c72016-06-28 10:52:43 -06001276 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001277 }
1278 }
1279 return NULL;
1280}
1281
Mark Young0ad83132016-06-30 13:02:42 -06001282void loader_destroy_logical_device(const struct loader_instance *inst,
1283 struct loader_device *dev,
1284 const VkAllocationCallbacks *pAllocator) {
1285 if (pAllocator) {
1286 dev->alloc_callbacks = *pAllocator;
1287 }
Mark Young0ad83132016-06-30 13:02:42 -06001288 if (NULL != dev->activated_layer_list.list) {
1289 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1290 }
1291 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001292}
1293
Jon Ashburn1530c342016-02-26 13:14:27 -07001294struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001295loader_create_logical_device(const struct loader_instance *inst,
1296 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001297 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001298#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1299 {
1300#else
1301 if (pAllocator) {
1302 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1303 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1304 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1305 } else {
1306#endif
1307 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1308 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001309
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001310 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001311 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1312 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001313 return NULL;
1314 }
1315
1316 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001317 if (pAllocator) {
1318 new_dev->alloc_callbacks = *pAllocator;
1319 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001320
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001321 return new_dev;
1322}
1323
Piers Daniell295fe402016-03-29 11:51:11 -06001324void loader_add_logical_device(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001325 struct loader_icd_term *icd_term,
Piers Daniell295fe402016-03-29 11:51:11 -06001326 struct loader_device *dev) {
Mark Young0153e0b2016-11-03 14:27:13 -06001327 dev->next = icd_term->logical_device_list;
1328 icd_term->logical_device_list = dev;
Piers Daniell295fe402016-03-29 11:51:11 -06001329}
1330
Jon Ashburn23d36b12016-02-02 17:47:28 -07001331void loader_remove_logical_device(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001332 struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001333 struct loader_device *found_dev,
1334 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001335 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001336
Mark Young0153e0b2016-11-03 14:27:13 -06001337 if (!icd_term || !found_dev)
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001338 return;
1339
1340 prev_dev = NULL;
Mark Young0153e0b2016-11-03 14:27:13 -06001341 dev = icd_term->logical_device_list;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001342 while (dev && dev != found_dev) {
1343 prev_dev = dev;
1344 dev = dev->next;
1345 }
1346
1347 if (prev_dev)
1348 prev_dev->next = found_dev->next;
1349 else
Mark Young0153e0b2016-11-03 14:27:13 -06001350 icd_term->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001351 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001352}
1353
Jon Ashburn23d36b12016-02-02 17:47:28 -07001354static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001355 struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001356 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001357 ptr_inst->total_icd_count--;
Mark Young0153e0b2016-11-03 14:27:13 -06001358 for (struct loader_device *dev = icd_term->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001359 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001360 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001361 dev = next_dev;
1362 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001363
Mark Young0153e0b2016-11-03 14:27:13 -06001364 loader_instance_heap_free(ptr_inst, icd_term);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001365}
1366
Mark Young0153e0b2016-11-03 14:27:13 -06001367static struct loader_icd_term *
Jon Ashburn23d36b12016-02-02 17:47:28 -07001368loader_icd_create(const struct loader_instance *inst) {
Mark Young0153e0b2016-11-03 14:27:13 -06001369 struct loader_icd_term *icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001370
Mark Young0153e0b2016-11-03 14:27:13 -06001371 icd_term = loader_instance_heap_alloc(inst, sizeof(struct loader_icd_term),
1372 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1373 if (!icd_term) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001374 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001375 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001376
Mark Young0153e0b2016-11-03 14:27:13 -06001377 memset(icd_term, 0, sizeof(struct loader_icd_term));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001378
Mark Young0153e0b2016-11-03 14:27:13 -06001379 return icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001380}
1381
Mark Young0153e0b2016-11-03 14:27:13 -06001382static struct loader_icd_term *
Jon Ashburn23d36b12016-02-02 17:47:28 -07001383loader_icd_add(struct loader_instance *ptr_inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001384 const struct loader_scanned_icd *scanned_icd) {
1385 struct loader_icd_term *icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001386
Mark Young0153e0b2016-11-03 14:27:13 -06001387 icd_term = loader_icd_create(ptr_inst);
1388 if (!icd_term) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001389 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001390 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001391
Mark Young0153e0b2016-11-03 14:27:13 -06001392 icd_term->scanned_icd = scanned_icd;
1393 icd_term->this_instance = ptr_inst;
Jon Ashburn3d002332015-08-20 16:35:30 -06001394
Chia-I Wu13a61a52014-08-04 11:18:20 +08001395 /* prepend to the list */
Mark Young0153e0b2016-11-03 14:27:13 -06001396 icd_term->next = ptr_inst->icd_terms;
1397 ptr_inst->icd_terms = icd_term;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001398 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001399
Mark Young0153e0b2016-11-03 14:27:13 -06001400 return icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001401}
Mark Young0153e0b2016-11-03 14:27:13 -06001402
Jon Ashburn17b4c862016-04-25 11:09:37 -06001403/**
1404 * Determine the ICD interface version to use.
1405 * @param icd
1406 * @param pVersion Output parameter indicating which version to use or 0 if
1407 * the negotiation API is not supported by the ICD
1408 * @return bool indicating true if the selected interface version is supported
1409 * by the loader, false indicates the version is not supported
1410 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1411 * vk_icdNegotiateLoaderICDInterfaceVersion
1412 * version 1 supports vk_icdGetInstanceProcAddr
1413 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1414 */
1415bool loader_get_icd_interface_version(
Mark Young0153e0b2016-11-03 14:27:13 -06001416 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1417 uint32_t *pVersion) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001418
1419 if (fp_negotiate_icd_version == NULL) {
1420 // ICD does not support the negotiation API, it supports version 0 or 1
1421 // calling code must determine if it is version 0 or 1
1422 *pVersion = 0;
1423 } else {
1424 // ICD supports the negotiation API, so call it with the loader's
1425 // latest version supported
1426 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1427 VkResult result = fp_negotiate_icd_version(pVersion);
1428
1429 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1430 // ICD no longer supports the loader's latest interface version so
1431 // fail loading the ICD
1432 return false;
1433 }
1434 }
1435
1436#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1437 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1438 // Loader no longer supports the ICD's latest interface version so fail
1439 // loading the ICD
1440 return false;
1441 }
1442#endif
1443 return true;
1444}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001445
Jon Ashburn23d36b12016-02-02 17:47:28 -07001446void loader_scanned_icd_clear(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001447 struct loader_icd_tramp_list *icd_tramp_list) {
1448 if (icd_tramp_list->capacity == 0)
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001449 return;
Mark Young0153e0b2016-11-03 14:27:13 -06001450 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
1451 loader_platform_close_library(icd_tramp_list->scanned_list[i].handle);
1452 loader_instance_heap_free(inst,
1453 icd_tramp_list->scanned_list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001454 }
Mark Young0153e0b2016-11-03 14:27:13 -06001455 loader_instance_heap_free(inst, icd_tramp_list->scanned_list);
1456 icd_tramp_list->capacity = 0;
1457 icd_tramp_list->count = 0;
1458 icd_tramp_list->scanned_list = NULL;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001459}
1460
Mark Young0153e0b2016-11-03 14:27:13 -06001461static VkResult
1462loader_scanned_icd_init(const struct loader_instance *inst,
1463 struct loader_icd_tramp_list *icd_tramp_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001464 VkResult err = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06001465 loader_scanned_icd_clear(inst, icd_tramp_list);
1466 icd_tramp_list->capacity = 8 * sizeof(struct loader_scanned_icd);
1467 icd_tramp_list->scanned_list = loader_instance_heap_alloc(
1468 inst, icd_tramp_list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1469 if (NULL == icd_tramp_list->scanned_list) {
1470 loader_log(
1471 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06001472 "realloc failed for layer list when attempting to add new layer");
1473 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1474 }
1475 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001476}
1477
Mark Young0153e0b2016-11-03 14:27:13 -06001478static VkResult
1479loader_scanned_icd_add(const struct loader_instance *inst,
1480 struct loader_icd_tramp_list *icd_tramp_list,
1481 const char *filename, uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001482 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001483 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001484 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001485 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
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);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001494 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001495 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001496 loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001497 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001498 }
1499
Jon Ashburn17b4c862016-04-25 11:09:37 -06001500 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001501 fp_negotiate_icd_version = loader_platform_get_proc_address(
1502 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001503
1504 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001505 &interface_vers)) {
1506 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1507 "ICD (%s) doesn't support interface version compatible"
1508 "with loader, skip this ICD %s",
1509 filename);
Mark Young3a587792016-08-19 15:25:08 -06001510 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001511 }
1512
Jon Ashburn23d36b12016-02-02 17:47:28 -07001513 fp_get_proc_addr =
1514 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001515 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001516 assert(interface_vers == 0);
1517 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001518 fp_get_proc_addr =
1519 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001520 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001521 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1522 loader_platform_get_proc_address_error(
1523 "vk_icdGetInstanceProcAddr"));
Mark Young3a587792016-08-19 15:25:08 -06001524 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001525 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001526 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001527 "Using deprecated ICD interface of "
1528 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001529 "vk_icdGetInstanceProcAddr for ICD %s",
1530 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001531 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001532 fp_create_inst =
1533 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001534 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001535 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1536 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1537 "for ICD %s",
1538 filename);
Mark Young3a587792016-08-19 15:25:08 -06001539 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001540 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001541 fp_get_inst_ext_props = loader_platform_get_proc_address(
1542 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001543 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001544 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1545 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001546 "via dlsym/loadlibrary for ICD %s",
1547 filename);
Mark Young3a587792016-08-19 15:25:08 -06001548 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001549 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001550 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001551 // Use newer interface version 1 or later
1552 if (interface_vers == 0)
1553 interface_vers = 1;
1554
Jon Ashburn23d36b12016-02-02 17:47:28 -07001555 fp_create_inst =
1556 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001557 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001558 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1559 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001560 "vk_icdGetInstanceProcAddr for ICD %s",
1561 filename);
Mark Young3a587792016-08-19 15:25:08 -06001562 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001563 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001564 fp_get_inst_ext_props =
1565 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1566 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001567 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001568 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1569 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001570 "via vk_icdGetInstanceProcAddr for ICD %s",
1571 filename);
Mark Young3a587792016-08-19 15:25:08 -06001572 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001573 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001574 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001575
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001576 // check for enough capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001577 if ((icd_tramp_list->count * sizeof(struct loader_scanned_icd)) >=
1578 icd_tramp_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001579
Mark Young0153e0b2016-11-03 14:27:13 -06001580 icd_tramp_list->scanned_list = loader_instance_heap_realloc(
1581 inst, icd_tramp_list->scanned_list, icd_tramp_list->capacity,
1582 icd_tramp_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1583 if (NULL == icd_tramp_list->scanned_list) {
Mark Young3a587792016-08-19 15:25:08 -06001584 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001585 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1586 "realloc failed on icd library list");
Mark Young3a587792016-08-19 15:25:08 -06001587 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001588 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001589 // double capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001590 icd_tramp_list->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001591 }
Mark Young0153e0b2016-11-03 14:27:13 -06001592 new_scanned_icd = &(icd_tramp_list->scanned_list[icd_tramp_list->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001593
Mark Young0153e0b2016-11-03 14:27:13 -06001594 new_scanned_icd->handle = handle;
1595 new_scanned_icd->api_version = api_version;
1596 new_scanned_icd->GetInstanceProcAddr = fp_get_proc_addr;
1597 new_scanned_icd->EnumerateInstanceExtensionProperties =
1598 fp_get_inst_ext_props;
1599 new_scanned_icd->CreateInstance = fp_create_inst;
1600 new_scanned_icd->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001601
Mark Young0153e0b2016-11-03 14:27:13 -06001602 new_scanned_icd->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001603 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001604 if (NULL == new_scanned_icd->lib_name) {
Mark Young3a587792016-08-19 15:25:08 -06001605 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001606 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001607 "Out of memory can't add icd");
Mark Young3a587792016-08-19 15:25:08 -06001608 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001609 }
Mark Young0153e0b2016-11-03 14:27:13 -06001610 strcpy(new_scanned_icd->lib_name, filename);
1611 icd_tramp_list->count++;
Mark Young3a587792016-08-19 15:25:08 -06001612
1613out:
1614
1615 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001616}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001617
Mark Young0153e0b2016-11-03 14:27:13 -06001618static bool loader_icd_init_entrys(struct loader_icd_term *icd_term,
1619 VkInstance inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001620 const PFN_vkGetInstanceProcAddr fp_gipa) {
1621/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001622
Jon Ashburn23d36b12016-02-02 17:47:28 -07001623#define LOOKUP_GIPA(func, required) \
1624 do { \
Mark Young0153e0b2016-11-03 14:27:13 -06001625 icd_term->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1626 if (!icd_term->func && required) { \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001627 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001628 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001629 loader_platform_get_proc_address_error("vk" #func)); \
1630 return false; \
1631 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001632 } while (0)
1633
Jon Ashburnc624c882015-07-16 10:17:29 -06001634 LOOKUP_GIPA(GetDeviceProcAddr, true);
1635 LOOKUP_GIPA(DestroyInstance, true);
1636 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1637 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1638 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001639 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001640 LOOKUP_GIPA(CreateDevice, true);
1641 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1642 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001643 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001644 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001645 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001646 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1647 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Mark Young65cb3662016-11-07 13:27:02 -07001648 LOOKUP_GIPA(DebugMarkerSetObjectTagEXT, false);
1649 LOOKUP_GIPA(DebugMarkerSetObjectNameEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001650 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001651 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1652 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1653 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001654 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1655 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1656 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1657 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1658 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1659 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1660 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1661 LOOKUP_GIPA(DestroySurfaceKHR, false);
Mark Young16573c72016-06-28 10:52:43 -06001662 LOOKUP_GIPA(CreateSwapchainKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001663#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Young16573c72016-06-28 10:52:43 -06001664 LOOKUP_GIPA(CreateWin32SurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001665 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1666#endif
1667#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001668 LOOKUP_GIPA(CreateXcbSurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001669 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1670#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001671#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001672 LOOKUP_GIPA(CreateXlibSurfaceKHR, false);
Karl Schultz65d20182016-03-08 07:55:27 -07001673 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1674#endif
Mark Younga7c51fd2016-09-16 10:18:42 -06001675#ifdef VK_USE_PLATFORM_MIR_KHR
1676 LOOKUP_GIPA(CreateMirSurfaceKHR, false);
1677 LOOKUP_GIPA(GetPhysicalDeviceMirPresentationSupportKHR, false);
1678#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001679#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Young16573c72016-06-28 10:52:43 -06001680 LOOKUP_GIPA(CreateWaylandSurfaceKHR, false);
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001681 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1682#endif
James Jones389dc0c2016-08-18 23:41:19 +01001683 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001684
Jon Ashburnc624c882015-07-16 10:17:29 -06001685#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001686
Jon Ashburnc624c882015-07-16 10:17:29 -06001687 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001688}
1689
Jon Ashburn23d36b12016-02-02 17:47:28 -07001690static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001691 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001692
1693 if (g_loader_debug > 0)
1694 return;
1695
1696 g_loader_debug = 0;
1697
1698 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001699 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001700 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001701 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001702 size_t len;
1703
1704 if (p)
1705 len = p - env;
1706 else
1707 len = strlen(env);
1708
1709 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001710 if (strncmp(env, "all", len) == 0) {
1711 g_loader_debug = ~0u;
1712 g_loader_log_msgs = ~0u;
1713 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001714 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001715 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001716 } else if (strncmp(env, "info", len) == 0) {
1717 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001718 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001719 } else if (strncmp(env, "perf", len) == 0) {
1720 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001721 g_loader_log_msgs |=
1722 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001723 } else if (strncmp(env, "error", len) == 0) {
1724 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001725 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001726 } else if (strncmp(env, "debug", len) == 0) {
1727 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001728 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001729 }
1730 }
1731
1732 if (!p)
1733 break;
1734
1735 env = p + 1;
1736 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001737
Mark Young0ad83132016-06-30 13:02:42 -06001738 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001739}
1740
Jon Ashburn23d36b12016-02-02 17:47:28 -07001741void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001742 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001743 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001744 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001745
1746 // initialize logging
1747 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001748
1749 // initial cJSON to use alloc callbacks
1750 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001751 .malloc_fn = loader_instance_tls_heap_alloc,
1752 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001753 };
1754 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001755}
1756
Jon Ashburn2077e382015-06-29 11:25:34 -06001757struct loader_manifest_files {
1758 uint32_t count;
1759 char **filename_list;
1760};
1761
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001762/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001763 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001764 *
1765 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001766 * A pointer to first char in the next path.
1767 * The next path (or NULL) in the list is returned in next_path.
1768 * Note: input string is modified in some cases. PASS IN A COPY!
1769 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001770static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001771 uint32_t len;
1772 char *next;
1773
1774 if (path == NULL)
1775 return NULL;
1776 next = strchr(path, PATH_SEPERATOR);
1777 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001778 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001779 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001780 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001781 *next = '\0';
1782 next++;
1783 }
1784
1785 return next;
1786}
1787
1788/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001789 * Given a path which is absolute or relative, expand the path if relative or
1790 * leave the path unmodified if absolute. The base path to prepend to relative
1791 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001792 *
1793 * \returns
1794 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001795 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001796static void loader_expand_path(const char *path, const char *rel_base,
1797 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001798 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001799 // do not prepend a base to an absolute path
1800 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001801 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001802
1803 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001804}
1805
1806/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001807 * Given a filename (file) and a list of paths (dir), try to find an existing
1808 * file in the paths. If filename already is a path then no
1809 * searching in the given paths.
1810 *
1811 * \returns
1812 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001813 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001814static void loader_get_fullpath(const char *file, const char *dirs,
1815 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001816 if (!loader_platform_is_path(file) && *dirs) {
1817 char *dirs_copy, *dir, *next_dir;
1818
1819 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1820 strcpy(dirs_copy, dirs);
1821
Jon Ashburn23d36b12016-02-02 17:47:28 -07001822 // find if file exists after prepending paths in given list
1823 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001824 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001825 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1826 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001827 if (loader_platform_file_exists(out_fullpath)) {
1828 return;
1829 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001830 }
1831 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001832
Jon Ashburn2077e382015-06-29 11:25:34 -06001833 snprintf(out_fullpath, out_size, "%s", file);
1834}
1835
1836/**
1837 * Read a JSON file into a buffer.
1838 *
1839 * \returns
1840 * A pointer to a cJSON object representing the JSON parse tree.
1841 * This returned buffer should be freed by caller.
1842 */
Mark Young3a587792016-08-19 15:25:08 -06001843static VkResult loader_get_json(const struct loader_instance *inst,
1844 const char *filename, cJSON **json) {
1845 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001846 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001847 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001848 VkResult res = VK_SUCCESS;
1849
1850 if (NULL == json) {
1851 res = VK_ERROR_INITIALIZATION_FAILED;
1852 goto out;
1853 }
1854
1855 *json = NULL;
1856
Jon Ashburn23d36b12016-02-02 17:47:28 -07001857 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001858 if (!file) {
Mark Young3a587792016-08-19 15:25:08 -06001859 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001860 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1861 "Couldn't open JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001862 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001863 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001864 fseek(file, 0, SEEK_END);
1865 len = ftell(file);
1866 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001867 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001868 if (json_buf == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06001869 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001870 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1871 "Out of memory can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001872 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001873 }
1874 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Young3a587792016-08-19 15:25:08 -06001875 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001876 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1877 "fread failed can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001878 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001879 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001880 json_buf[len] = '\0';
1881
Jon Ashburn23d36b12016-02-02 17:47:28 -07001882 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001883 *json = cJSON_Parse(json_buf);
1884 if (*json == NULL) {
1885 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001886 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1887 "Can't parse JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001888 goto out;
1889 }
1890
1891out:
1892 if (NULL != file) {
1893 fclose(file);
1894 }
1895
1896 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001897}
1898
1899/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001900 * Do a deep copy of the loader_layer_properties structure.
1901 */
Mark Young0ad83132016-06-30 13:02:42 -06001902VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1903 struct loader_layer_properties *dst,
1904 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001905 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001906 memcpy(dst, src, sizeof(*src));
1907 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001908 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1909 src->instance_extension_list.count,
1910 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1911 if (NULL == dst->instance_extension_list.list) {
1912 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1913 "alloc failed for instance extension list");
1914 return VK_ERROR_OUT_OF_HOST_MEMORY;
1915 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001916 dst->instance_extension_list.capacity =
1917 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001918 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001919 dst->instance_extension_list.capacity);
1920 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001921 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1922 src->device_extension_list.count,
1923 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1924 if (NULL == dst->device_extension_list.list) {
1925 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1926 "alloc failed for device extension list");
1927 return VK_ERROR_OUT_OF_HOST_MEMORY;
1928 }
Mark Young0153e0b2016-11-03 14:27:13 -06001929 memset(dst->device_extension_list.list, 0,
1930 sizeof(struct loader_dev_ext_props) *
1931 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001932
Jon Ashburn23d36b12016-02-02 17:47:28 -07001933 dst->device_extension_list.capacity =
1934 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001935 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001936 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001937 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001938 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001939 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001940 dst->device_extension_list.list->entrypoints =
1941 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1942 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1943 if (NULL == dst->device_extension_list.list->entrypoints) {
1944 loader_log(
1945 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1946 "alloc failed for device extension list entrypoint array");
1947 return VK_ERROR_OUT_OF_HOST_MEMORY;
1948 }
Mark Young0153e0b2016-11-03 14:27:13 -06001949 memset(dst->device_extension_list.list->entrypoints, 0,
1950 sizeof(char *) * cnt);
Mark Young0ad83132016-06-30 13:02:42 -06001951
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001952 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001953 dst->device_extension_list.list->entrypoints[i] =
1954 loader_instance_heap_alloc(
1955 inst,
1956 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1957 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1958 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1959 loader_log(
1960 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1961 "alloc failed for device extension list entrypoint %d", i);
1962 return VK_ERROR_OUT_OF_HOST_MEMORY;
1963 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001964 strcpy(dst->device_extension_list.list->entrypoints[i],
1965 src->device_extension_list.list->entrypoints[i]);
1966 }
1967 }
Mark Young0ad83132016-06-30 13:02:42 -06001968
1969 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001970}
1971
Jon Ashburn86a527a2016-02-10 20:59:26 -07001972static bool
1973loader_find_layer_name_list(const char *name,
1974 const struct loader_layer_list *layer_list) {
1975 if (!layer_list)
1976 return false;
1977 for (uint32_t j = 0; j < layer_list->count; j++)
1978 if (!strcmp(name, layer_list->list[j].info.layerName))
1979 return true;
1980 return false;
1981}
1982
1983static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1984 const char **layer_list) {
1985 if (!layer_list)
1986 return false;
1987 for (uint32_t j = 0; j < layer_count; j++)
1988 if (!strcmp(name, layer_list[j]))
1989 return true;
1990 return false;
1991}
1992
Jon Ashburn491cd042016-05-16 14:01:18 -06001993bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001994 const char *name, uint32_t layer_count,
1995 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1996 if (!layer_list)
1997 return false;
1998 for (uint32_t j = 0; j < layer_count; j++)
1999 if (!strcmp(name, layer_list[j]))
2000 return true;
2001 return false;
2002}
2003
2004/**
2005 * Searches through an array of layer names (ppp_layer_names) looking for a
2006 * layer key_name.
2007 * If not found then simply returns updating nothing.
2008 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12002009 * Any duplicate (pre-existing) expand_names in layer names are removed.
2010 * Order is otherwise preserved, with the layer key_name being replaced by the
2011 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07002012 * @param inst
2013 * @param layer_count
2014 * @param ppp_layer_names
2015 */
Mark Young0ad83132016-06-30 13:02:42 -06002016VkResult loader_expand_layer_names(
2017 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07002018 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06002019 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07002020
Jon Ashburncc407a22016-04-15 09:25:03 -06002021 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002022
Jon Ashburncc407a22016-04-15 09:25:03 -06002023 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06002024 (char const **)pp_src_layers)) {
2025 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06002026 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06002027 }
Jon Ashburn71483442016-02-11 18:59:43 -07002028
Jon Ashburn86a527a2016-02-10 20:59:26 -07002029 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2030 "Found meta layer %s, replacing with actual layer group",
2031 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12002032
Jon Ashburn491cd042016-05-16 14:01:18 -06002033 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06002034 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06002035 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2036 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002037 if (NULL == pp_dst_layers) {
2038 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2039 "alloc failed for dst layer array");
2040 return VK_ERROR_OUT_OF_HOST_MEMORY;
2041 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002042
2043 // copy layers from src to dst, stripping key_name and anything in
2044 // expand_names.
2045 uint32_t src_index, dst_index = 0;
2046 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002047 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
2048 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002049 continue;
2050 }
2051
2052 if (!strcmp(pp_src_layers[src_index], key_name)) {
2053 // insert all expand_names in place of key_name
2054 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06002055 for (expand_index = 0; expand_index < expand_count;
2056 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002057 pp_dst_layers[dst_index++] = expand_names[expand_index];
2058 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002059 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002060 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002061
2062 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002063 }
2064
Chris Forbesbd9de052016-04-06 20:49:02 +12002065 *ppp_layer_names = pp_dst_layers;
2066 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002067
2068 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002069}
2070
Chris Forbesbd9de052016-04-06 20:49:02 +12002071void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
2072 const VkInstanceCreateInfo *orig,
2073 VkInstanceCreateInfo *ours) {
2074 /* Free the layer names array iff we had to reallocate it */
2075 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002076 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002077 }
2078}
2079
Jon Ashburn491cd042016-05-16 14:01:18 -06002080void loader_init_std_validation_props(struct loader_layer_properties *props) {
2081 memset(props, 0, sizeof(struct loader_layer_properties));
2082 props->type = VK_LAYER_TYPE_META_EXPLICT;
2083 strncpy(props->info.description, "LunarG Standard Validation Layer",
Mark Young0153e0b2016-11-03 14:27:13 -06002084 sizeof(props->info.description));
Jon Ashburn491cd042016-05-16 14:01:18 -06002085 props->info.implementationVersion = 1;
2086 strncpy(props->info.layerName, std_validation_str,
Mark Young0153e0b2016-11-03 14:27:13 -06002087 sizeof(props->info.layerName));
Jon Ashburn491cd042016-05-16 14:01:18 -06002088 // TODO what about specVersion? for now insert loader's built version
2089 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2090}
2091
Jon Ashburn86a527a2016-02-10 20:59:26 -07002092/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002093 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002094 * the set of required layer names. If found then it adds a meta property to the
2095 * layer list.
2096 * Assumes the required layers are the same for both instance and device lists.
2097 * @param inst
2098 * @param layer_count number of layers in layer_names
2099 * @param layer_names array of required layer names
2100 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002101 */
2102static void loader_add_layer_property_meta(
2103 const struct loader_instance *inst, uint32_t layer_count,
2104 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002105 struct loader_layer_list *layer_instance_list) {
2106 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002107 bool found;
2108 struct loader_layer_list *layer_list;
2109
Jon Ashburn491cd042016-05-16 14:01:18 -06002110 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002111 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002112 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002113 return;
2114
Jon Ashburn491cd042016-05-16 14:01:18 -06002115 layer_list = layer_instance_list;
2116
2117 found = true;
2118 if (layer_list == NULL)
2119 return;
2120 for (i = 0; i < layer_count; i++) {
2121 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002122 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002123 found = false;
2124 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002125 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002126
2127 struct loader_layer_properties *props;
2128 if (found) {
2129 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002130 if (NULL == props) {
2131 // Error already triggered in loader_get_next_layer_property.
2132 return;
2133 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002134 loader_init_std_validation_props(props);
Jon Ashburn491cd042016-05-16 14:01:18 -06002135 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07002136}
2137
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002138static void loader_read_json_layer(
2139 const struct loader_instance *inst,
2140 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2141 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2142 char *temp;
2143 char *name, *type, *library_path, *api_version;
2144 char *implementation_version, *description;
2145 cJSON *ext_item;
2146 VkExtensionProperties ext_prop;
2147
Mark Young0ad83132016-06-30 13:02:42 -06002148/*
2149 * The following are required in the "layer" object:
2150 * (required) "name"
2151 * (required) "type"
2152 * (required) “library_path”
2153 * (required) “api_version”
2154 * (required) “implementation_version”
2155 * (required) “description”
2156 * (required for implicit layers) “disable_environment”
2157 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002158
Jon Ashburn23d36b12016-02-02 17:47:28 -07002159#define GET_JSON_OBJECT(node, var) \
2160 { \
2161 var = cJSON_GetObjectItem(node, #var); \
2162 if (var == NULL) { \
2163 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002164 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002165 "Didn't find required layer object %s in manifest " \
2166 "JSON file, skipping this layer", \
2167 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002168 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002169 } \
2170 }
2171#define GET_JSON_ITEM(node, var) \
2172 { \
2173 item = cJSON_GetObjectItem(node, #var); \
2174 if (item == NULL) { \
2175 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002176 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002177 "Didn't find required layer value %s in manifest JSON " \
2178 "file, skipping this layer", \
2179 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002180 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002181 } \
2182 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002183 if (temp == NULL) { \
2184 layer_node = layer_node->next; \
2185 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2186 "Problem accessing layer value %s in manifest JSON " \
2187 "file, skipping this layer", \
2188 #var); \
2189 return; \
2190 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002191 temp[strlen(temp) - 1] = '\0'; \
2192 var = loader_stack_alloc(strlen(temp) + 1); \
2193 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002194 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002195 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002196 GET_JSON_ITEM(layer_node, name)
2197 GET_JSON_ITEM(layer_node, type)
2198 GET_JSON_ITEM(layer_node, library_path)
2199 GET_JSON_ITEM(layer_node, api_version)
2200 GET_JSON_ITEM(layer_node, implementation_version)
2201 GET_JSON_ITEM(layer_node, description)
2202 if (is_implicit) {
2203 GET_JSON_OBJECT(layer_node, disable_environment)
2204 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002205#undef GET_JSON_ITEM
2206#undef GET_JSON_OBJECT
2207
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002208 // add list entry
2209 struct loader_layer_properties *props = NULL;
2210 if (!strcmp(type, "DEVICE")) {
2211 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2212 "Device layers are deprecated skipping this layer");
2213 layer_node = layer_node->next;
2214 return;
2215 }
2216 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2217 // layers that must work with older loaders
2218 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2219 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002220 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002221 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002222 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002223 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002224 if (NULL == props) {
2225 // Error already triggered in loader_get_next_layer_property.
2226 return;
2227 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002228 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2229 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2230 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002231
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002232 if (props == NULL) {
2233 layer_node = layer_node->next;
2234 return;
2235 }
Jon Ashburn15315172015-07-07 15:06:25 -06002236
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002237 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2238 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2239
2240 char *fullpath = props->lib_name;
2241 char *rel_base;
2242 if (loader_platform_is_path(library_path)) {
2243 // a relative or absolute path
2244 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2245 strcpy(name_copy, filename);
2246 rel_base = loader_platform_dirname(name_copy);
2247 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2248 } else {
2249 // a filename which is assumed in a system directory
2250 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2251 MAX_STRING_SIZE, fullpath);
2252 }
2253 props->info.specVersion = loader_make_version(api_version);
2254 props->info.implementationVersion = atoi(implementation_version);
2255 strncpy((char *)props->info.description, description,
2256 sizeof(props->info.description));
2257 props->info.description[sizeof(props->info.description) - 1] = '\0';
2258 if (is_implicit) {
2259 if (!disable_environment || !disable_environment->child) {
2260 loader_log(
2261 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2262 "Didn't find required layer child value disable_environment"
2263 "in manifest JSON file, skipping this layer");
2264 layer_node = layer_node->next;
2265 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002266 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002267 strncpy(props->disable_env_var.name, disable_environment->child->string,
2268 sizeof(props->disable_env_var.name));
2269 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2270 '\0';
2271 strncpy(props->disable_env_var.value,
2272 disable_environment->child->valuestring,
2273 sizeof(props->disable_env_var.value));
2274 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2275 '\0';
2276 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002277
Jon Ashburn23d36b12016-02-02 17:47:28 -07002278/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002279* Now get all optional items and objects and put in list:
2280* functions
2281* instance_extensions
2282* device_extensions
2283* enable_environment (implicit layers only)
2284*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002285#define GET_JSON_OBJECT(node, var) \
2286 { var = cJSON_GetObjectItem(node, #var); }
2287#define GET_JSON_ITEM(node, var) \
2288 { \
2289 item = cJSON_GetObjectItem(node, #var); \
2290 if (item != NULL) { \
2291 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002292 if (temp != NULL) { \
2293 temp[strlen(temp) - 1] = '\0'; \
2294 var = loader_stack_alloc(strlen(temp) + 1); \
2295 strcpy(var, &temp[1]); \
2296 cJSON_Free(temp); \
2297 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002298 } \
2299 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002300
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002301 cJSON *instance_extensions, *device_extensions, *functions,
2302 *enable_environment;
2303 cJSON *entrypoints;
2304 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2305 char **entry_array;
2306 vkGetInstanceProcAddr = NULL;
2307 vkGetDeviceProcAddr = NULL;
2308 spec_version = NULL;
2309 entrypoints = NULL;
2310 entry_array = NULL;
2311 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002312
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002313 /**
2314 * functions
2315 * vkGetInstanceProcAddr
2316 * vkGetDeviceProcAddr
2317 */
2318 GET_JSON_OBJECT(layer_node, functions)
2319 if (functions != NULL) {
2320 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2321 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2322 if (vkGetInstanceProcAddr != NULL)
2323 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2324 sizeof(props->functions.str_gipa));
2325 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2326 if (vkGetDeviceProcAddr != NULL)
2327 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2328 sizeof(props->functions.str_gdpa));
2329 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2330 }
2331 /**
2332 * instance_extensions
2333 * array of
2334 * name
2335 * spec_version
2336 */
2337 GET_JSON_OBJECT(layer_node, instance_extensions)
2338 if (instance_extensions != NULL) {
2339 int count = cJSON_GetArraySize(instance_extensions);
2340 for (i = 0; i < count; i++) {
2341 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2342 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002343 if (name != NULL) {
2344 strncpy(ext_prop.extensionName, name,
2345 sizeof(ext_prop.extensionName));
2346 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2347 '\0';
2348 }
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 Youngc3a6d2e2016-06-13 14:49:53 -06002355 bool ext_unsupported =
2356 wsi_unsupported_instance_extension(&ext_prop);
2357 if (!ext_unsupported) {
2358 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2359 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002360 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002361 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002362 }
2363 /**
2364 * device_extensions
2365 * array of
2366 * name
2367 * spec_version
2368 * entrypoints
2369 */
2370 GET_JSON_OBJECT(layer_node, device_extensions)
2371 if (device_extensions != NULL) {
2372 int count = cJSON_GetArraySize(device_extensions);
2373 for (i = 0; i < count; i++) {
2374 ext_item = cJSON_GetArrayItem(device_extensions, i);
2375 GET_JSON_ITEM(ext_item, name)
2376 GET_JSON_ITEM(ext_item, spec_version)
2377 if (name != NULL) {
2378 strncpy(ext_prop.extensionName, name,
2379 sizeof(ext_prop.extensionName));
2380 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2381 '\0';
2382 }
Mark Young0ad83132016-06-30 13:02:42 -06002383 if (NULL != spec_version) {
2384 ext_prop.specVersion = atoi(spec_version);
2385 } else {
2386 ext_prop.specVersion = 0;
2387 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002388 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2389 GET_JSON_OBJECT(ext_item, entrypoints)
2390 int entry_count;
2391 if (entrypoints == NULL) {
2392 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2393 &ext_prop, 0, NULL);
2394 continue;
2395 }
2396 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002397 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002398 entry_array =
2399 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002400 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002401 for (j = 0; j < entry_count; j++) {
2402 ext_item = cJSON_GetArrayItem(entrypoints, j);
2403 if (ext_item != NULL) {
2404 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002405 if (NULL == temp) {
2406 entry_array[j] = NULL;
2407 continue;
2408 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002409 temp[strlen(temp) - 1] = '\0';
2410 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2411 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002412 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002413 }
2414 }
2415 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2416 &ext_prop, entry_count, entry_array);
2417 }
2418 }
2419 if (is_implicit) {
2420 GET_JSON_OBJECT(layer_node, enable_environment)
2421
2422 // enable_environment is optional
2423 if (enable_environment) {
2424 strncpy(props->enable_env_var.name,
2425 enable_environment->child->string,
2426 sizeof(props->enable_env_var.name));
2427 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2428 '\0';
2429 strncpy(props->enable_env_var.value,
2430 enable_environment->child->valuestring,
2431 sizeof(props->enable_env_var.value));
2432 props->enable_env_var
2433 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2434 }
2435 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002436#undef GET_JSON_ITEM
2437#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002438}
2439
2440/**
2441 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2442 * file, add entry to the layer_list. Fill out the layer_properties in this list
2443 * entry from the input cJSON object.
2444 *
2445 * \returns
2446 * void
2447 * layer_list has a new entry and initialized accordingly.
2448 * If the json input object does not have all the required fields no entry
2449 * is added to the list.
2450 */
2451static void
2452loader_add_layer_properties(const struct loader_instance *inst,
2453 struct loader_layer_list *layer_instance_list,
2454 cJSON *json, bool is_implicit, char *filename) {
2455 /* Fields in layer manifest file that are required:
2456 * (required) “file_format_version”
2457 *
Mark Young0ad83132016-06-30 13:02:42 -06002458 * If more than one "layer" object are to be used, use the "layers" array
2459 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002460 *
2461 * First get all required items and if any missing abort
2462 */
2463
2464 cJSON *item, *layers_node, *layer_node;
2465 uint16_t file_major_vers = 0;
2466 uint16_t file_minor_vers = 0;
2467 uint16_t file_patch_vers = 0;
2468 char *vers_tok;
2469 cJSON *disable_environment = NULL;
2470 item = cJSON_GetObjectItem(json, "file_format_version");
2471 if (item == NULL) {
2472 return;
2473 }
2474 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002475 if (NULL == file_vers) {
2476 return;
2477 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002478 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2479 "Found manifest file %s, version %s", filename, file_vers);
2480 // Get the major/minor/and patch as integers for easier comparison
2481 vers_tok = strtok(file_vers, ".\"\n\r");
2482 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002483 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002484 vers_tok = strtok(NULL, ".\"\n\r");
2485 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002486 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002487 vers_tok = strtok(NULL, ".\"\n\r");
2488 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002489 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002490 }
2491 }
2492 }
2493 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2494 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2495 "%s Unexpected manifest file version (expected 1.0.0 or "
2496 "1.0.1), may cause errors",
2497 filename);
2498 }
Mark Young0ad83132016-06-30 13:02:42 -06002499 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002500 // If "layers" is present, read in the array of layer objects
2501 layers_node = cJSON_GetObjectItem(json, "layers");
2502 if (layers_node != NULL) {
2503 int numItems = cJSON_GetArraySize(layers_node);
2504 if (file_major_vers == 1 && file_minor_vers == 0 &&
2505 file_patch_vers == 0) {
2506 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2507 "\"layers\" tag not officially added until file version "
2508 "1.0.1, but %s is reporting version %s",
2509 filename, file_vers);
2510 }
2511 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2512 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2513 if (layer_node == NULL) {
2514 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2515 "Can't find \"layers\" array element %d object in "
2516 "manifest JSON file %s, skipping this file",
2517 curLayer, filename);
2518 return;
2519 }
2520 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2521 disable_environment, is_implicit, filename);
2522 }
2523 } else {
2524 // Otherwise, try to read in individual layers
2525 layer_node = cJSON_GetObjectItem(json, "layer");
2526 if (layer_node == NULL) {
2527 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2528 "Can't find \"layer\" object in manifest JSON file %s, "
2529 "skipping this file",
2530 filename);
2531 return;
2532 }
2533 // Loop through all "layer" objects in the file to get a count of them
2534 // first.
2535 uint16_t layer_count = 0;
2536 cJSON *tempNode = layer_node;
2537 do {
2538 tempNode = tempNode->next;
2539 layer_count++;
2540 } while (tempNode != NULL);
2541 /*
2542 * Throw a warning if we encounter multiple "layer" objects in file
2543 * versions newer than 1.0.0. Having multiple objects with the same
2544 * name at the same level is actually a JSON standard violation.
2545 */
2546 if (layer_count > 1 &&
2547 (file_major_vers > 1 ||
2548 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2549 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2550 "Multiple \"layer\" nodes are deprecated starting in "
2551 "file version \"1.0.1\". Please use \"layers\" : [] "
2552 "array instead in %s.",
2553 filename);
2554 } else {
2555 do {
2556 loader_read_json_layer(inst, layer_instance_list, layer_node,
2557 item, disable_environment, is_implicit,
2558 filename);
2559 layer_node = layer_node->next;
2560 } while (layer_node != NULL);
2561 }
2562 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002563 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002564}
2565
2566/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002567 * Find the Vulkan library manifest files.
2568 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002569 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002570 * for a list of JSON manifest files. If env_override is non-NULL
2571 * and has a valid value. Then the location is ignored. Otherwise
2572 * location is used to look for manifest files. The location
2573 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002574 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002575 * directory to look at. It is expanded into the dir path
2576 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2577 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002578 *
2579 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002580 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002581 * A string list of manifest files to be opened in out_files param.
2582 * List has a pointer to string for each manifest filename.
2583 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002584 * Location or override string lists can be either files or directories as
2585 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002586 * | location | override
2587 * --------------------------------
2588 * Win ICD | files | files
2589 * Win Layer | files | dirs
2590 * Linux ICD | dirs | files
2591 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002592 */
Mark Youngf8c20102016-11-07 16:26:17 -07002593static VkResult
2594loader_get_manifest_files(const struct loader_instance *inst,
2595 const char *env_override, char *source_override,
2596 bool is_layer, bool warn_if_not_present,
2597 const char *location, const char *home_location,
2598 struct loader_manifest_files *out_files) {
Mark Young0ad83132016-06-30 13:02:42 -06002599 char * override = NULL;
2600 char *loc, *orig_loc = NULL;
2601 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002602 char *file, *next_file, *name;
2603 size_t alloced_count = 64;
2604 char full_path[2048];
2605 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002606 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002607 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002608 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002609
2610 out_files->count = 0;
2611 out_files->filename_list = NULL;
2612
Jamie Madill00c3c912016-04-06 18:26:46 -04002613 if (source_override != NULL) {
2614 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002615 } else if (env_override != NULL &&
2616 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002617#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002618 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002619 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002620 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002621 override = NULL;
2622 }
2623#endif
2624 }
2625
Jon Ashburnb6822212016-02-16 15:34:16 -07002626#if !defined(_WIN32)
2627 if (location == NULL && home_location == NULL) {
2628#else
2629 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002630 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002631#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002632 loader_log(
2633 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002634 "Can't get manifest files with NULL location, env_override=%s",
2635 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002636 res = VK_ERROR_INITIALIZATION_FAILED;
2637 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002638 }
2639
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002640#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002641 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002642#else
2643 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002644#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002645 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002646 // Also handle getting the location(s) from registry on Windows
2647 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002648 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002649 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002650 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2651 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002652 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2653 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002654 }
2655 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002656#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002657 reg = loader_get_registry_files(inst, loc);
2658 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002659 if (!is_layer) {
2660 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2661 "Registry lookup failed can't get ICD manifest "
2662 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002663 // This typically only fails when out of memory, which is
2664 // critical
2665 // if this is for the loader.
2666 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002667 } else {
Mark Youngf8c20102016-11-07 16:26:17 -07002668 if (warn_if_not_present) {
2669 // warning only for layers
2670 loader_log(
2671 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2672 "Registry lookup failed can't get layer manifest files");
2673 }
Mark Young0ad83132016-06-30 13:02:42 -06002674 // Return success for now since it's not critical for layers
2675 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002676 }
Mark Young0ad83132016-06-30 13:02:42 -06002677 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002678 }
Mark Young0ad83132016-06-30 13:02:42 -06002679 orig_loc = loc;
2680 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002681#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002682 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002683 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002684 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002685 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2686 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002687 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2688 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002689 }
2690 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002691 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002692 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002693 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002694 }
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
Jon Ashburn23d36b12016-02-02 17:47:28 -07002697 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2698 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002699
Jon Ashburn2077e382015-06-29 11:25:34 -06002700 file = loc;
2701 while (*file) {
2702 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002703 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002704 sysdir = opendir(file);
2705 name = NULL;
2706 if (sysdir) {
2707 dent = readdir(sysdir);
2708 if (dent == NULL)
2709 break;
2710 name = &(dent->d_name[0]);
2711 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2712 name = full_path;
2713 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002714 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002715#if defined(_WIN32)
2716 name = file;
2717#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002718 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002719 char *dir;
2720 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002721 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002722 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002723 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2724 "Out of memory can't get manifest files");
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 Young0ad83132016-06-30 13:02:42 -06002740 out_files->filename_list = loader_instance_heap_alloc(
2741 inst, alloced_count * sizeof(char *),
2742 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002743 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002744 out_files->filename_list = loader_instance_heap_realloc(
2745 inst, out_files->filename_list,
2746 alloced_count * sizeof(char *),
2747 alloced_count * sizeof(char *) * 2,
2748 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002749 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002750 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002751 if (out_files->filename_list == NULL) {
2752 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2753 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002754 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2755 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002756 }
Mark Young0ad83132016-06-30 13:02:42 -06002757 out_files->filename_list[out_files->count] =
2758 loader_instance_heap_alloc(
2759 inst, strlen(name) + 1,
2760 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002761 if (out_files->filename_list[out_files->count] == NULL) {
2762 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2763 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002764 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2765 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002766 }
2767 strcpy(out_files->filename_list[out_files->count], name);
2768 out_files->count++;
2769 } else if (!list_is_dirs) {
2770 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002771 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002772 "Skipping manifest file %s, file name must end in .json",
2773 name);
2774 }
2775 if (list_is_dirs) {
2776 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002777 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002778 break;
Mark Young0ad83132016-06-30 13:02:42 -06002779 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002780 name = &(dent->d_name[0]);
2781 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2782 name = full_path;
2783 } else {
2784 break;
2785 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002786 }
Mark Young0ad83132016-06-30 13:02:42 -06002787 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002788 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002789 sysdir = NULL;
2790 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002791 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002792#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002793 if (home_location != NULL &&
2794 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002795 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2796 size_t len;
2797 if (xdgdatahome != NULL) {
2798
2799 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002800 strlen(home_location));
2801 if (home_loc == NULL) {
2802 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002803 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002804 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2805 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002806 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002807 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002808 // Add directory separator if needed
2809 if (home_location[0] != DIRECTORY_SYMBOL) {
2810 len = strlen(home_loc);
2811 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002812 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002813 }
2814 strcat(home_loc, home_location);
2815 file = home_loc;
2816 next_file = loader_get_next_path(file);
2817 home_location = NULL;
2818
Jon Ashburn1530c342016-02-26 13:14:27 -07002819 loader_log(
2820 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002821 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002822 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002823 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002824
2825 } else {
2826
2827 char *home = secure_getenv("HOME");
2828 if (home != NULL) {
2829 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2830 strlen(home_location));
2831 if (home_loc == NULL) {
2832 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young0153e0b2016-11-03 14:27:13 -06002833 "Out of memory can't get manifest files");
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002834 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2835 goto out;
2836 }
2837 strcpy(home_loc, home);
2838
2839 len = strlen(home);
2840 if (home[len] != DIRECTORY_SYMBOL) {
2841 home_loc[len] = DIRECTORY_SYMBOL;
2842 home_loc[len + 1] = '\0';
2843 }
2844 strcat(home_loc, ".local/share");
2845
2846 if (home_location[0] != DIRECTORY_SYMBOL) {
2847 len = strlen(home_loc);
2848 home_loc[len] = DIRECTORY_SYMBOL;
2849 home_loc[len + 1] = '\0';
2850 }
2851 strcat(home_loc, home_location);
2852 file = home_loc;
2853 next_file = loader_get_next_path(file);
2854 home_location = NULL;
2855
2856 loader_log(
2857 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2858 "Searching the following path for manifest files: %s\n",
2859 home_loc);
2860 list_is_dirs = true;
2861 } else {
2862 // without knowing HOME, we just.. give up
2863 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002864 }
2865 }
2866#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002867 }
Mark Young0ad83132016-06-30 13:02:42 -06002868
2869out:
2870 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2871 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2872 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2873 }
2874 loader_instance_heap_free(inst, out_files->filename_list);
2875 out_files->count = 0;
2876 out_files->filename_list = NULL;
2877 }
2878
2879 if (NULL != sysdir) {
2880 closedir(sysdir);
2881 }
2882
2883 if (NULL != reg && reg != orig_loc) {
2884 loader_instance_heap_free(inst, reg);
2885 }
2886 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002887}
2888
Jon Ashburn23d36b12016-02-02 17:47:28 -07002889void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002890
Jon Ashburn23d36b12016-02-02 17:47:28 -07002891void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002892/**
2893 * Try to find the Vulkan ICD driver(s).
2894 *
2895 * This function scans the default system loader path(s) or path
2896 * specified by the \c VK_ICD_FILENAMES environment variable in
2897 * order to find loadable VK ICDs manifest files. From these
2898 * manifest files it finds the ICD libraries.
2899 *
2900 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002901 * Vulkan result
2902 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002903 */
Mark Young0ad83132016-06-30 13:02:42 -06002904VkResult loader_icd_scan(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06002905 struct loader_icd_tramp_list *icd_tramp_list) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002906 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002907 uint16_t file_major_vers = 0;
2908 uint16_t file_minor_vers = 0;
2909 uint16_t file_patch_vers = 0;
2910 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002911 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002912 VkResult res = VK_SUCCESS;
2913 bool lockedMutex = false;
2914 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002915 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002916
Mark Young0ad83132016-06-30 13:02:42 -06002917 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2918
Mark Young0153e0b2016-11-03 14:27:13 -06002919 res = loader_scanned_icd_init(inst, icd_tramp_list);
Mark Young0ad83132016-06-30 13:02:42 -06002920 if (VK_SUCCESS != res) {
2921 goto out;
2922 }
2923
Jon Ashburn2077e382015-06-29 11:25:34 -06002924 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002925 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
Mark Youngf8c20102016-11-07 16:26:17 -07002926 true, DEFAULT_VK_DRIVERS_INFO,
Mark Young0ad83132016-06-30 13:02:42 -06002927 HOME_VK_DRIVERS_INFO, &manifest_files);
2928 if (VK_SUCCESS != res || manifest_files.count == 0) {
2929 goto out;
2930 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002931 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002932 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002933 for (uint32_t i = 0; i < manifest_files.count; i++) {
2934 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002935 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002936 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002937 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002938
Mark Young3a587792016-08-19 15:25:08 -06002939 res = loader_get_json(inst, file_str, &json);
2940 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2941 break;
2942 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002943 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002944 }
Mark Young3a587792016-08-19 15:25:08 -06002945
Jon Ashburn005617f2015-11-17 17:35:40 -07002946 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002947 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002948 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002949 if (num_good_icds == 0) {
2950 res = VK_ERROR_INITIALIZATION_FAILED;
2951 }
Derrick Owens62e16ef2016-09-09 15:49:07 -04002952 cJSON_Delete(json);
2953 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002954 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002955 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002956 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002957 if (NULL == file_vers) {
2958 // Only reason the print can fail is if there was an allocation
2959 // issue
Mark Young3a587792016-08-19 15:25:08 -06002960 if (num_good_icds == 0) {
2961 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2962 }
Derrick Owenscd92b8b2016-09-09 15:45:13 -04002963 cJSON_Delete(json);
2964 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002965 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002966 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002967 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2968 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002969 // Get the major/minor/and patch as integers for easier comparison
2970 vers_tok = strtok(file_vers, ".\"\n\r");
2971 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002972 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002973 vers_tok = strtok(NULL, ".\"\n\r");
2974 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002975 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002976 vers_tok = strtok(NULL, ".\"\n\r");
2977 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002978 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002979 }
2980 }
2981 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002982 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002983 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002984 "Unexpected manifest file version (expected 1.0.0 or "
2985 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002986 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002987 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002988 itemICD = cJSON_GetObjectItem(json, "ICD");
2989 if (itemICD != NULL) {
2990 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002991 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002992 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002993 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002994 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002995 "Can't find \"library_path\" in ICD JSON file "
2996 "%s, skipping",
2997 file_str);
Mark Young3a587792016-08-19 15:25:08 -06002998 if (num_good_icds == 0) {
2999 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3000 }
Mark Young0ad83132016-06-30 13:02:42 -06003001 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06003002 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003003 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003004 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06003005 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003006 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06003007 temp[strlen(temp) - 1] = '\0';
3008 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06003009 if (NULL == library_path) {
3010 loader_log(
3011 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3012 "Can't allocate space for \"library_path\" in ICD "
3013 "JSON file %s, skipping",
3014 file_str);
3015 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3016 cJSON_Free(temp);
3017 cJSON_Delete(json);
3018 json = NULL;
3019 goto out;
3020 }
Jon Ashburn86251302015-08-25 16:48:24 -06003021 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003022 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003023 if (strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003024 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003025 "Can't find \"library_path\" in ICD JSON file "
3026 "%s, skipping",
3027 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003028 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003029 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003030 continue;
3031 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003032 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003033 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07003034 loader_log(
3035 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04003036 "Searching for ICD drivers named %s default dir %s\n",
3037 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003038 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003039 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003040 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3041 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003042 strcpy(name_copy, file_str);
3043 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003044 loader_expand_path(library_path, rel_base, sizeof(fullpath),
3045 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003046 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003047 // a filename which is assumed in a system directory
3048 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
3049 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003050 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003051
3052 uint32_t vers = 0;
3053 item = cJSON_GetObjectItem(itemICD, "api_version");
3054 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003055 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003056 if (NULL == temp) {
3057 // Only reason the print can fail is if there was an
3058 // allocation issue
3059 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3060 goto out;
3061 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003062 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003063 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07003064 }
Mark Young0153e0b2016-11-03 14:27:13 -06003065 res = loader_scanned_icd_add(inst, icd_tramp_list, fullpath,
3066 vers);
Mark Young3a587792016-08-19 15:25:08 -06003067 if (VK_SUCCESS != res) {
3068 goto out;
3069 }
3070 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003071 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003072 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003073 "Can't find \"library_path\" object in ICD JSON "
3074 "file %s, skipping",
3075 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003076 }
3077 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003078 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003079 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003080 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
3081 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003082 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003083
Mark Young0ad83132016-06-30 13:02:42 -06003084 cJSON_Delete(json);
3085 json = NULL;
3086 }
3087
3088out:
3089 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003090 cJSON_Delete(json);
3091 }
Mark Young0ad83132016-06-30 13:02:42 -06003092 if (NULL != manifest_files.filename_list) {
3093 for (uint32_t i = 0; i < manifest_files.count; i++) {
3094 if (NULL != manifest_files.filename_list[i]) {
3095 loader_instance_heap_free(inst,
3096 manifest_files.filename_list[i]);
3097 }
3098 }
3099 loader_instance_heap_free(inst, manifest_files.filename_list);
3100 }
3101 if (lockedMutex) {
3102 loader_platform_thread_unlock_mutex(&loader_json_lock);
3103 }
3104 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003105}
3106
Jon Ashburn23d36b12016-02-02 17:47:28 -07003107void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003108 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003109 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003110 struct loader_manifest_files
3111 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003112 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003113 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003114 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003115
Mark Young0ad83132016-06-30 13:02:42 -06003116 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3117
3118 // Get a list of manifest files for explicit layers
3119 if (VK_SUCCESS !=
3120 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
Mark Youngf8c20102016-11-07 16:26:17 -07003121 true, true, DEFAULT_VK_ELAYERS_INFO,
Mark Young0ad83132016-06-30 13:02:42 -06003122 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3123 goto out;
3124 }
3125
3126 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003127 // Pass NULL for environment variable override - implicit layers are not
3128 // overridden by LAYERS_PATH_ENV
Mark Youngf8c20102016-11-07 16:26:17 -07003129 if (VK_SUCCESS != loader_get_manifest_files(inst, NULL, NULL, true, false,
3130 DEFAULT_VK_ILAYERS_INFO,
3131 HOME_VK_ILAYERS_INFO,
3132 &manifest_files[1])) {
Mark Young0ad83132016-06-30 13:02:42 -06003133 goto out;
3134 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003135
Mark Young0ad83132016-06-30 13:02:42 -06003136 // Make sure we have at least one layer, if not, go ahead and return
3137 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3138 goto out;
3139 }
3140
3141 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003142 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003143
Jon Ashburn6461ef22015-09-22 13:11:00 -06003144 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003145 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003146 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003147 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003148 file_str = manifest_files[implicit].filename_list[i];
3149 if (file_str == NULL)
3150 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003151
Jon Ashburn075ce432015-12-17 17:38:24 -07003152 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003153 VkResult res = loader_get_json(inst, file_str, &json);
3154 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3155 break;
3156 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003157 continue;
3158 }
3159
Jon Ashburn491cd042016-05-16 14:01:18 -06003160 loader_add_layer_properties(inst, instance_layers, json,
3161 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003162 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003163 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003164 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003165
3166 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003167 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3168 sizeof(std_validation_names[0]),
3169 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003170
Mark Young0ad83132016-06-30 13:02:42 -06003171out:
3172
3173 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3174 if (NULL != manifest_files[manFile].filename_list) {
3175 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3176 if (NULL != manifest_files[manFile].filename_list[i]) {
3177 loader_instance_heap_free(
3178 inst, manifest_files[manFile].filename_list[i]);
3179 }
3180 }
3181 loader_instance_heap_free(inst,
3182 manifest_files[manFile].filename_list);
3183 }
3184 }
3185 if (lockedMutex) {
3186 loader_platform_thread_unlock_mutex(&loader_json_lock);
3187 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003188}
3189
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003190void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003191 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003192 char *file_str;
3193 struct loader_manifest_files manifest_files;
3194 cJSON *json;
3195 uint32_t i;
3196
3197 // Pass NULL for environment variable override - implicit layers are not
3198 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003199 VkResult res = loader_get_manifest_files(
Mark Youngf8c20102016-11-07 16:26:17 -07003200 inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO,
3201 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 Young0ad83132016-06-30 13:02:42 -06003225 loader_add_layer_properties(inst, instance_layers, json, true,
3226 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003227
Mark Young0ad83132016-06-30 13:02:42 -06003228 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003229 cJSON_Delete(json);
3230 }
Mark Young0ad83132016-06-30 13:02:42 -06003231 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003232
3233 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003234 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3235 sizeof(std_validation_names[0]),
3236 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003237
3238 loader_platform_thread_unlock_mutex(&loader_json_lock);
3239}
3240
Jon Ashburn23d36b12016-02-02 17:47:28 -07003241static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3242loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003243 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003244 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003245 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003246 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003247 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003248 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003249
Jon Ashburn27cd5842015-05-12 17:26:48 -06003250 // inst is not wrapped
3251 if (inst == VK_NULL_HANDLE) {
3252 return NULL;
3253 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003254 VkLayerInstanceDispatchTable *disp_table =
3255 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003256 void *addr;
3257
3258 if (disp_table == NULL)
3259 return NULL;
3260
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003261 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003262 addr =
3263 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003264 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003265 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003266 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003267
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003268 // Don't call down the chain, this would be an infinite loop
3269 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003270 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003271 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003272}
3273
Piers Daniellf9262be2016-09-14 11:24:36 -06003274VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003275loader_gpa_device_internal(VkDevice device, const char *pName) {
3276 struct loader_device *dev;
Mark Young0153e0b2016-11-03 14:27:13 -06003277 struct loader_icd_term *icd_term =
3278 loader_get_icd_and_device(device, &dev, NULL);
Mark Young16573c72016-06-28 10:52:43 -06003279
Mark Young65cb3662016-11-07 13:27:02 -07003280 // NOTE: Device Funcs needing Trampoline/Terminator.
3281 // Overrides for device functions needing a trampoline and
3282 // a terminator because certain device entry-points still need to go
3283 // through a terminator before hitting the ICD. This could be for
3284 // several reasons, but the main one is currently unwrapping an
3285 // object before passing the appropriate info along to the ICD.
3286 // This is why we also have to override the direct ICD call to
3287 // vkGetDeviceProcAddr to intercept those calls.
3288 if (!strcmp(pName, "vkGetDeviceProcAddr")) {
3289 return (PFN_vkVoidFunction)loader_gpa_device_internal;
3290 } else if (!strcmp(pName, "vkCreateSwapchainKHR")) {
Mark Young16573c72016-06-28 10:52:43 -06003291 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
Mark Young65cb3662016-11-07 13:27:02 -07003292 } else if (!strcmp(pName, "vkDebugMarkerSetObjectTagEXT")) {
3293 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectTagEXT;
3294 } else if (!strcmp(pName, "vkDebugMarkerSetObjectNameEXT")) {
3295 return (PFN_vkVoidFunction)terminator_DebugMarkerSetObjectNameEXT;
Mark Young16573c72016-06-28 10:52:43 -06003296 }
3297
Mark Young0153e0b2016-11-03 14:27:13 -06003298 return icd_term->GetDeviceProcAddr(device, pName);
Piers Daniell295fe402016-03-29 11:51:11 -06003299}
3300
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003301/**
3302 * Initialize device_ext dispatch table entry as follows:
3303 * If dev == NULL find all logical devices created within this instance and
3304 * init the entry (given by idx) in the ext dispatch table.
3305 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003306 * The initialization value is gotten by calling down the device chain with
3307 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003308 * If GDPA returns NULL then don't initialize the dispatch table entry.
3309 */
3310static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003311 struct loader_device *dev,
3312 uint32_t idx,
3313 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003314
Jon Ashburn23d36b12016-02-02 17:47:28 -07003315{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003316 void *gdpa_value;
3317 if (dev != NULL) {
3318 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Mark Young65cb3662016-11-07 13:27:02 -07003319 dev->chain_device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003320 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003321 dev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003322 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003323 } else {
Mark Young0153e0b2016-11-03 14:27:13 -06003324 for (struct loader_icd_term *icd_term = inst->icd_terms;
3325 icd_term != NULL; icd_term = icd_term->next) {
3326 struct loader_device *ldev = icd_term->logical_device_list;
Karl Schultz2558bd32016-02-24 14:39:39 -07003327 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003328 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003329 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Mark Young65cb3662016-11-07 13:27:02 -07003330 ldev->chain_device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003331 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003332 ldev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003333 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003334 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003335 }
3336 }
3337 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003338}
3339
3340/**
3341 * Find all dev extension in the hash table and initialize the dispatch table
3342 * for dev for each of those extension entrypoints found in hash table.
3343
3344 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003345void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3346 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003347 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3348 if (inst->disp_hash[i].func_name != NULL)
3349 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3350 inst->disp_hash[i].func_name);
3351 }
3352}
3353
3354static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003355 const char *funcName) {
Mark Young0153e0b2016-11-03 14:27:13 -06003356 struct loader_icd_term *icd_term;
3357 icd_term = inst->icd_terms;
3358 while (NULL != icd_term) {
3359 if (icd_term->scanned_icd->GetInstanceProcAddr(icd_term->instance,
3360 funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003361 // this icd supports funcName
3362 return true;
Mark Young0153e0b2016-11-03 14:27:13 -06003363 icd_term = icd_term->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003364 }
3365
3366 return false;
3367}
3368
Jon Ashburncc407a22016-04-15 09:25:03 -06003369static bool loader_check_layer_list_for_address(
3370 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003371 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003372 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003373 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003374 const struct loader_device_extension_list *const extensions =
3375 &(layers->list[layer].device_extension_list);
3376 for (uint32_t extension = 0; extension < extensions->count;
3377 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003378 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003379 const struct loader_dev_ext_props *const property =
3380 &(extensions->list[extension]);
3381 for (uint32_t entry = 0; entry < property->entrypoint_count;
3382 ++entry) {
3383 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003384 return true;
3385 }
3386 }
3387 }
3388 }
3389
3390 return false;
3391}
3392
Jon Ashburn23d36b12016-02-02 17:47:28 -07003393static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003394 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003395 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3396 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003397 }
3398 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3399}
3400
3401static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003402 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003403 uint32_t i;
3404 uint32_t idx = *ptr_idx;
3405 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3406
3407 if (!inst->disp_hash[idx].func_name) {
3408 // no entry here at this idx, so use it
3409 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003410 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003411 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003412 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003413 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003414 "loader_add_dev_ext_table() can't allocate memory for "
3415 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003416 return false;
3417 }
3418 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3419 return true;
3420 }
3421
3422 // check for enough capacity
3423 if (list->capacity == 0) {
Mark Young0153e0b2016-11-03 14:27:13 -06003424 list->index =
3425 loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
3426 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003427 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003428 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003429 "loader_add_dev_ext_table() can't allocate list memory");
3430 return false;
3431 }
3432 list->capacity = 8 * sizeof(*(list->index));
3433 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0153e0b2016-11-03 14:27:13 -06003434 list->index = loader_instance_heap_realloc(
3435 inst, list->index, list->capacity, list->capacity * 2,
3436 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003437 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003438 loader_log(
3439 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3440 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003441 return false;
3442 }
3443 list->capacity *= 2;
3444 }
3445
Jon Ashburn23d36b12016-02-02 17:47:28 -07003446 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003447 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3448 do {
3449 if (!inst->disp_hash[i].func_name) {
3450 assert(inst->disp_hash[i].list.capacity == 0);
Mark Young0153e0b2016-11-03 14:27:13 -06003451 inst->disp_hash[i].func_name = (char *)loader_instance_heap_alloc(
3452 inst, strlen(funcName) + 1,
3453 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003454 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003455 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003456 "loader_add_dev_ext_table() can't rallocate "
3457 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003458 return false;
3459 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003460 strncpy(inst->disp_hash[i].func_name, funcName,
3461 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003462 list->index[list->count] = i;
3463 list->count++;
3464 *ptr_idx = i;
3465 return true;
3466 }
3467 i = (i + 1) % MAX_NUM_DEV_EXTS;
3468 } while (i != idx);
3469
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003470 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003471 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3472 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003473 return false;
3474}
3475
3476static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003477 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003478 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003479 if (inst->disp_hash[*idx].func_name &&
3480 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003481 return true;
3482
3483 // funcName wasn't at the primary spot in the hash table
3484 // search the list of secondary locations (shallow search, not deep search)
3485 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3486 alt_idx = inst->disp_hash[*idx].list.index[i];
3487 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3488 *idx = alt_idx;
3489 return true;
3490 }
3491 }
3492
3493 return false;
3494}
3495
3496/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003497 * This function returns generic trampoline code address for unknown entry
3498 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003499 * Presumably, these unknown entry points (as given by funcName) are device
3500 * extension entrypoints. A hash table is used to keep a list of unknown entry
3501 * points and their mapping to the device extension dispatch table
3502 * (struct loader_dev_ext_dispatch_table).
3503 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003504 * For a given entry point string (funcName), if an existing mapping is found
3505 * the
3506 * trampoline address for that mapping is returned. Otherwise, this unknown
3507 * entry point
3508 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3509 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003510 * new entry in the hash table is initialized and that trampoline address for
3511 * the new entry is returned. Null is returned if the hash table is full or
3512 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3513 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003514void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003515 uint32_t idx;
3516 uint32_t seed = 0;
3517
3518 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3519
3520 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3521 // found funcName already in hash
3522 return loader_get_dev_ext_trampoline(idx);
3523
3524 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003525 if (!loader_check_icds_for_address(inst, funcName) &&
Mark Young0153e0b2016-11-03 14:27:13 -06003526 !loader_check_layer_list_for_address(&inst->instance_layer_list,
3527 funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003528 // if support found in layers continue on
3529 return NULL;
3530 }
3531
3532 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3533 // successfully added new table entry
3534 // init any dev dispatch table entrys as needed
3535 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3536 return loader_get_dev_ext_trampoline(idx);
3537 }
3538
3539 return NULL;
3540}
3541
Jon Ashburn23d36b12016-02-02 17:47:28 -07003542struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003543 /* look up the loader_instance in our list by comparing dispatch tables, as
3544 * there is no guarantee the instance is still a loader_instance* after any
3545 * layers which wrap the instance object.
3546 */
3547 const VkLayerInstanceDispatchTable *disp;
3548 struct loader_instance *ptr_instance = NULL;
3549 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003550 for (struct loader_instance *inst = loader.instances; inst;
3551 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003552 if (inst->disp == disp) {
3553 ptr_instance = inst;
3554 break;
3555 }
3556 }
3557 return ptr_instance;
3558}
3559
Jon Ashburn23d36b12016-02-02 17:47:28 -07003560static loader_platform_dl_handle
Mark Young0153e0b2016-11-03 14:27:13 -06003561loader_open_layer_lib(const struct loader_instance *inst,
3562 const char *chain_type,
3563 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003564
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003565 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003566 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003567 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003568 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003569 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003570 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003571 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003572 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003573 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003574
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003575 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003576}
3577
Mark Young0153e0b2016-11-03 14:27:13 -06003578static void loader_close_layer_lib(const struct loader_instance *inst,
3579 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003580
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003581 if (prop->lib_handle) {
3582 loader_platform_close_library(prop->lib_handle);
3583 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3584 "Unloading layer library %s", prop->lib_name);
3585 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003586 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003587}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003588
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003589void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003590 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003591 struct loader_layer_list *list) {
3592 /* delete instance list of enabled layers and close any layer libraries */
3593 for (uint32_t i = 0; i < list->count; i++) {
3594 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003595
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003596 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003597 }
Mark Young0ad83132016-06-30 13:02:42 -06003598 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003599}
3600
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003601/**
3602 * Go through the search_list and find any layers which match type. If layer
3603 * type match is found in then add it to ext_list.
3604 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003605static void
3606loader_add_layer_implicit(const struct loader_instance *inst,
3607 const enum layer_type type,
3608 struct loader_layer_list *list,
3609 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003610 bool enable;
3611 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003612 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003613 for (i = 0; i < search_list->count; i++) {
3614 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003615 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003616 /* Found an implicit layer, see if it should be enabled */
3617 enable = false;
3618
Jon Ashburn23d36b12016-02-02 17:47:28 -07003619 // if no enable_environment variable is specified, this implicit
3620 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003621 // should always be enabled. Otherwise check if the variable is set
3622 if (prop->enable_env_var.name[0] == 0) {
3623 enable = true;
3624 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003625 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003626 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3627 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003628 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003629 }
3630
3631 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003632 // environment variables are set, the layer is disabled. Implicit
3633 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003634 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003635 env_value = loader_getenv(prop->disable_env_var.name, inst);
3636 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003637 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003638 }
3639 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003640
Mark Young0ad83132016-06-30 13:02:42 -06003641 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003642 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003643 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003644 }
3645 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003646}
3647
3648/**
3649 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003650 * is found in search_list then add it to layer_list. But only add it to
3651 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003652 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003653static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003654 const enum layer_type type,
3655 const char *env_name,
3656 struct loader_layer_list *layer_list,
3657 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003658 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003659 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003660
Mark Young0ad83132016-06-30 13:02:42 -06003661 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003662 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003663 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003664 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003665 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003666 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003667 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003668 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003669 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003670
Mark Young0ad83132016-06-30 13:02:42 -06003671 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003672
Jon Ashburn23d36b12016-02-02 17:47:28 -07003673 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003674 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003675 if (!strcmp(std_validation_str, name)) {
3676 /* add meta list of layers
3677 don't attempt to remove duplicate layers already added by app or
3678 env var
3679 */
3680 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3681 "Expanding meta layer %s found in environment variable",
3682 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003683 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3684 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003685 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3686 sizeof(std_validation_names[0]);
3687 i++) {
3688 loader_find_layer_name_add_list(inst, std_validation_names[i],
3689 type, search_list, layer_list);
3690 }
3691 } else {
3692 loader_find_layer_name_add_list(inst, name, type, search_list,
3693 layer_list);
3694 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003695 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003696 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003697
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003698 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003699}
3700
Jon Ashburn23d36b12016-02-02 17:47:28 -07003701VkResult
3702loader_enable_instance_layers(struct loader_instance *inst,
3703 const VkInstanceCreateInfo *pCreateInfo,
3704 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003705 VkResult err;
3706
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003707 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003708
Jon Ashburne39a4f82015-08-28 13:38:21 -06003709 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003710 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3711 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003712 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003713 }
3714
Jon Ashburn0c26e712015-07-02 16:10:32 -06003715 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003716 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3717 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003718
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003719 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003720 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3721 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003722 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003723
3724 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003725 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003726 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3727 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003728
3729 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003730}
3731
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003732/*
3733 * Given the list of layers to activate in the loader_instance
3734 * structure. This function will add a VkLayerInstanceCreateInfo
3735 * structure to the VkInstanceCreateInfo.pNext pointer.
3736 * Each activated layer will have it's own VkLayerInstanceLink
3737 * structure that tells the layer what Get*ProcAddr to call to
3738 * get function pointers to the next layer down.
3739 * Once the chain info has been created this function will
3740 * execute the CreateInstance call chain. Each layer will
3741 * then have an opportunity in it's CreateInstance function
3742 * to setup it's dispatch table when the lower layer returns
3743 * successfully.
3744 * Each layer can wrap or not-wrap the returned VkInstance object
3745 * as it sees fit.
3746 * The instance chain is terminated by a loader function
3747 * that will call CreateInstance on all available ICD's and
3748 * cache those VkInstance objects for future use.
3749 */
3750VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003751 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003752 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003753 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003754 uint32_t activated_layers = 0;
3755 VkLayerInstanceCreateInfo chain_info;
3756 VkLayerInstanceLink *layer_instance_link_info = NULL;
3757 VkInstanceCreateInfo loader_create_info;
3758 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003759
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003760 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3761 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003762
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003763 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003764
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003765 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003766
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003767 chain_info.u.pLayerInfo = NULL;
3768 chain_info.pNext = pCreateInfo->pNext;
3769 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3770 chain_info.function = VK_LAYER_LINK_INFO;
3771 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003772
Jon Ashburn23d36b12016-02-02 17:47:28 -07003773 layer_instance_link_info = loader_stack_alloc(
3774 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003775 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003776 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3777 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003778 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003779 }
3780
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003781 /* Create instance chain of enabled layers */
3782 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003783 struct loader_layer_properties *layer_prop =
3784 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003785 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003786
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003787 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003788 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003789 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003790 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3791 NULL) {
3792 if (layer_prop->functions.str_gipa == NULL ||
3793 strlen(layer_prop->functions.str_gipa) == 0) {
3794 fpGIPA = (PFN_vkGetInstanceProcAddr)
3795 loader_platform_get_proc_address(
3796 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003797 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3798 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003799 fpGIPA = (PFN_vkGetInstanceProcAddr)
3800 loader_platform_get_proc_address(
3801 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003802 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003803 loader_log(
3804 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3805 "Failed to find vkGetInstanceProcAddr in layer %s",
3806 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003807 continue;
3808 }
3809 }
3810
Jon Ashburn23d36b12016-02-02 17:47:28 -07003811 layer_instance_link_info[activated_layers].pNext =
3812 chain_info.u.pLayerInfo;
3813 layer_instance_link_info[activated_layers]
3814 .pfnNextGetInstanceProcAddr = nextGIPA;
3815 chain_info.u.pLayerInfo =
3816 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003817 nextGIPA = fpGIPA;
3818
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003819 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003820 "Insert instance layer %s (%s)",
3821 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003822
3823 activated_layers++;
3824 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003825 }
3826
Jon Ashburn23d36b12016-02-02 17:47:28 -07003827 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003828 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003829 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003830 VkLayerInstanceCreateInfo create_info_disp;
3831
Jon Ashburncc407a22016-04-15 09:25:03 -06003832 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003833 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003834
3835 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3836
3837 create_info_disp.pNext = loader_create_info.pNext;
3838 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003839 res =
3840 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003841 } else {
3842 // Couldn't find CreateInstance function!
3843 res = VK_ERROR_INITIALIZATION_FAILED;
3844 }
3845
3846 if (res != VK_SUCCESS) {
3847 // TODO: Need to clean up here
3848 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003849 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003850 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003851 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003852 }
3853
3854 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003855}
3856
Jon Ashburn23d36b12016-02-02 17:47:28 -07003857void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3858 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003859
Jon Ashburn23d36b12016-02-02 17:47:28 -07003860 loader_init_instance_extension_dispatch_table(
3861 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003862}
3863
Jon Ashburn1530c342016-02-26 13:14:27 -07003864VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003865loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3866 const VkDeviceCreateInfo *pCreateInfo,
3867 const VkAllocationCallbacks *pAllocator,
3868 const struct loader_instance *inst,
3869 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003870 uint32_t activated_layers = 0;
3871 VkLayerDeviceLink *layer_device_link_info;
3872 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003873 VkDeviceCreateInfo loader_create_info;
3874 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003875
Piers Daniell295fe402016-03-29 11:51:11 -06003876 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003877 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003878
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003879 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3880
Jon Ashburn23d36b12016-02-02 17:47:28 -07003881 layer_device_link_info = loader_stack_alloc(
3882 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003883 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003884 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3885 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003886 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003887 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003888
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003889 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003890 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3891 chain_info.function = VK_LAYER_LINK_INFO;
3892 chain_info.u.pLayerInfo = NULL;
3893 chain_info.pNext = pCreateInfo->pNext;
3894 loader_create_info.pNext = &chain_info;
3895
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003896 /* Create instance chain of enabled layers */
3897 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003898 struct loader_layer_properties *layer_prop =
3899 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003900 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003901
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003902 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003903 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003904 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003905 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3906 NULL) {
3907 if (layer_prop->functions.str_gipa == NULL ||
3908 strlen(layer_prop->functions.str_gipa) == 0) {
3909 fpGIPA = (PFN_vkGetInstanceProcAddr)
3910 loader_platform_get_proc_address(
3911 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003912 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3913 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003914 fpGIPA = (PFN_vkGetInstanceProcAddr)
3915 loader_platform_get_proc_address(
3916 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003917 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003918 loader_log(
3919 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3920 "Failed to find vkGetInstanceProcAddr in layer %s",
3921 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003922 continue;
3923 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003924 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003925 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003926 if (layer_prop->functions.str_gdpa == NULL ||
3927 strlen(layer_prop->functions.str_gdpa) == 0) {
3928 fpGDPA = (PFN_vkGetDeviceProcAddr)
3929 loader_platform_get_proc_address(lib_handle,
3930 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003931 layer_prop->functions.get_device_proc_addr = fpGDPA;
3932 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003933 fpGDPA = (PFN_vkGetDeviceProcAddr)
3934 loader_platform_get_proc_address(
3935 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003936 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003937 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003938 "Failed to find vkGetDeviceProcAddr in layer %s",
3939 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003940 continue;
3941 }
3942 }
3943
Jon Ashburn23d36b12016-02-02 17:47:28 -07003944 layer_device_link_info[activated_layers].pNext =
3945 chain_info.u.pLayerInfo;
3946 layer_device_link_info[activated_layers]
3947 .pfnNextGetInstanceProcAddr = nextGIPA;
3948 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3949 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003950 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3951 nextGIPA = fpGIPA;
3952 nextGDPA = fpGDPA;
3953
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003954 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003955 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003956 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003957
3958 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003959 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003960 }
3961
Jon Ashburncc407a22016-04-15 09:25:03 -06003962 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003963 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003964 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003965 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003966 VkLayerDeviceCreateInfo create_info_disp;
3967
Jon Ashburncc407a22016-04-15 09:25:03 -06003968 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003969 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3970
3971 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3972
3973 create_info_disp.pNext = loader_create_info.pNext;
3974 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003975 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003976 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003977 if (res != VK_SUCCESS) {
3978 return res;
3979 }
Mark Young65cb3662016-11-07 13:27:02 -07003980 dev->chain_device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003981 } else {
3982 // Couldn't find CreateDevice function!
3983 return VK_ERROR_INITIALIZATION_FAILED;
3984 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003985
Mark Young65cb3662016-11-07 13:27:02 -07003986 // Initialize device dispatch table
Jon Ashburn23d36b12016-02-02 17:47:28 -07003987 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Mark Young65cb3662016-11-07 13:27:02 -07003988 dev->chain_device);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003989
3990 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003991}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003992
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003993VkResult loader_validate_layers(const struct loader_instance *inst,
3994 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003995 const char *const *ppEnabledLayerNames,
3996 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003997 struct loader_layer_properties *prop;
3998
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003999 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004000 VkStringErrorFlags result =
4001 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004002 if (result != VK_STRING_ERROR_NONE) {
4003 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004004 "Loader: Device ppEnabledLayerNames contains string "
4005 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004006 return VK_ERROR_LAYER_NOT_PRESENT;
4007 }
4008
Jon Ashburn23d36b12016-02-02 17:47:28 -07004009 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004010 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004011 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004012 }
4013 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004014 return VK_SUCCESS;
4015}
4016
4017VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004018 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004019 const struct loader_extension_list *icd_exts,
4020 const struct loader_layer_list *instance_layer,
4021 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004022
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004023 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004024 struct loader_layer_properties *layer_prop;
4025
Jon Ashburnf19916e2016-01-11 13:12:43 -07004026 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004027 VkStringErrorFlags result = vk_string_validate(
4028 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004029 if (result != VK_STRING_ERROR_NONE) {
4030 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004031 "Loader: Instance ppEnabledExtensionNames contains "
4032 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004033 return VK_ERROR_EXTENSION_NOT_PRESENT;
4034 }
4035
Jon Ashburn23d36b12016-02-02 17:47:28 -07004036 extension_prop = get_extension_property(
4037 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004038
4039 if (extension_prop) {
4040 continue;
4041 }
4042
4043 extension_prop = NULL;
4044
4045 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004046 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004047 layer_prop = loader_get_layer_property(
Jan-Harald Fredriksene812a642016-09-28 12:10:24 +02004048 pCreateInfo->ppEnabledLayerNames[j], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004049 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004050 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004051 * should have already filtered this case out.
4052 */
4053 continue;
4054 }
4055
Jon Ashburn23d36b12016-02-02 17:47:28 -07004056 extension_prop =
4057 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
4058 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004059 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004060 /* Found the extension in one of the layers enabled by the app.
4061 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004062 break;
4063 }
4064 }
4065
4066 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004067 /* Didn't find extension name in any of the global layers, error out
4068 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004069 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004070 }
4071 }
4072 return VK_SUCCESS;
4073}
4074
4075VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06004076 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004077 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07004078 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004079 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004080 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004081 struct loader_layer_properties *layer_prop;
4082
Jon Ashburnf19916e2016-01-11 13:12:43 -07004083 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004084
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004085 VkStringErrorFlags result = vk_string_validate(
4086 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004087 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06004088 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4089 0, "Loader: Device ppEnabledExtensionNames contains "
4090 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004091 return VK_ERROR_EXTENSION_NOT_PRESENT;
4092 }
4093
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004094 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004095 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004096
4097 if (extension_prop) {
4098 continue;
4099 }
4100
Jon Ashburn471f44c2016-01-13 12:51:43 -07004101 /* Not in global list, search activated layer extension lists */
4102 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4103 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004104
Jon Ashburn23d36b12016-02-02 17:47:28 -07004105 extension_prop = get_dev_extension_property(
4106 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004107 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004108 /* Found the extension in one of the layers enabled by the app.
4109 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004110 break;
4111 }
4112 }
4113
4114 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004115 /* Didn't find extension name in any of the device layers, error out
4116 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004117 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004118 }
4119 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004120 return VK_SUCCESS;
4121}
4122
Jon Ashburn1530c342016-02-26 13:14:27 -07004123/**
4124 * Terminator functions for the Instance chain
4125 * All named terminator_<Vulakn API name>
4126 */
Mark Young0ad83132016-06-30 13:02:42 -06004127VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
4128 const VkInstanceCreateInfo *pCreateInfo,
4129 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Mark Young0153e0b2016-11-03 14:27:13 -06004130 struct loader_icd_term *icd_term;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004131 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004132 char **filtered_extension_names = NULL;
4133 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004134 VkResult res = VK_SUCCESS;
Mark Young8b4edb52016-11-11 09:31:55 -07004135 bool one_icd_successful = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004136
Jon Ashburncc407a22016-04-15 09:25:03 -06004137 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004138 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4139
Jon Ashburnf19916e2016-01-11 13:12:43 -07004140 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004141 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004142
4143 /*
4144 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004145 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004146 * No ICD will advertise support for layers. An ICD
4147 * library could support a layer, but it would be
4148 * independent of the actual ICD, just in the same library.
4149 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004150 filtered_extension_names =
4151 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004152 if (!filtered_extension_names) {
Mark Young3a587792016-08-19 15:25:08 -06004153 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4154 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004155 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004156 icd_create_info.ppEnabledExtensionNames =
4157 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004158
Mark Young0153e0b2016-11-03 14:27:13 -06004159 for (uint32_t i = 0; i < ptr_instance->icd_tramp_list.count; i++) {
4160 icd_term = loader_icd_add(
4161 ptr_instance, &ptr_instance->icd_tramp_list.scanned_list[i]);
4162 if (NULL == icd_term) {
Mark Young3a587792016-08-19 15:25:08 -06004163 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4164 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004165 }
4166 icd_create_info.enabledExtensionCount = 0;
4167 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004168
Mark Young0ad83132016-06-30 13:02:42 -06004169 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4170 "Build ICD instance extension list");
4171 // traverse scanned icd list adding non-duplicate extensions to the
4172 // list
Mark Young3a587792016-08-19 15:25:08 -06004173 res = loader_init_generic_list(ptr_instance,
Mark Young0153e0b2016-11-03 14:27:13 -06004174 (struct loader_generic_list *)&icd_exts,
4175 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004176 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4177 // If out of memory, bail immediately.
4178 goto out;
4179 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004180 // Something bad happened with this ICD, so free it and try the
4181 // next.
Mark Young0153e0b2016-11-03 14:27:13 -06004182 ptr_instance->icd_terms = icd_term->next;
4183 icd_term->next = NULL;
4184 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004185 continue;
4186 }
4187
4188 res = loader_add_instance_extensions(
Mark Young0ad83132016-06-30 13:02:42 -06004189 ptr_instance,
Mark Young0153e0b2016-11-03 14:27:13 -06004190 icd_term->scanned_icd->EnumerateInstanceExtensionProperties,
4191 icd_term->scanned_icd->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004192 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004193 loader_destroy_generic_list(
4194 ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004195 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4196 // If out of memory, bail immediately.
4197 goto out;
4198 } else {
4199 // Something bad happened with this ICD, so free it and try
4200 // the next.
Mark Young0153e0b2016-11-03 14:27:13 -06004201 ptr_instance->icd_terms = icd_term->next;
4202 icd_term->next = NULL;
4203 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004204 continue;
4205 }
Mark Young3a587792016-08-19 15:25:08 -06004206 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004207
Mark Young0ad83132016-06-30 13:02:42 -06004208 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4209 prop = get_extension_property(
4210 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4211 if (prop) {
4212 filtered_extension_names[icd_create_info
4213 .enabledExtensionCount] =
4214 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4215 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004216 }
4217 }
Mark Young0ad83132016-06-30 13:02:42 -06004218
4219 loader_destroy_generic_list(ptr_instance,
4220 (struct loader_generic_list *)&icd_exts);
4221
Mark Young8b4edb52016-11-11 09:31:55 -07004222 VkResult icd_result =
4223 ptr_instance->icd_tramp_list.scanned_list[i].CreateInstance(
4224 &icd_create_info, pAllocator, &(icd_term->instance));
4225 if (VK_ERROR_OUT_OF_HOST_MEMORY == icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004226 // If out of memory, bail immediately.
Mark Young8b4edb52016-11-11 09:31:55 -07004227 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06004228 goto out;
Mark Young8b4edb52016-11-11 09:31:55 -07004229 } else if (VK_SUCCESS != icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004230 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4231 "ICD ignored: failed to CreateInstance in ICD %d", i);
Mark Young0153e0b2016-11-03 14:27:13 -06004232 ptr_instance->icd_terms = icd_term->next;
4233 icd_term->next = NULL;
4234 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004235 continue;
4236 }
Mark Young0ad83132016-06-30 13:02:42 -06004237
Mark Young0153e0b2016-11-03 14:27:13 -06004238 if (!loader_icd_init_entrys(icd_term, icd_term->instance,
4239 ptr_instance->icd_tramp_list.scanned_list[i]
4240 .GetInstanceProcAddr)) {
Mark Young3a587792016-08-19 15:25:08 -06004241 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06004242 "ICD ignored: failed to CreateInstance and find "
4243 "entrypoints with ICD");
Mark Young3a587792016-08-19 15:25:08 -06004244 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004245 }
Mark Young8b4edb52016-11-11 09:31:55 -07004246
4247 // If we made it this far, at least one ICD was successful
4248 one_icd_successful = true;
Jon Ashburn46888392015-01-29 15:45:51 -07004249 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004250
Mark Young8b4edb52016-11-11 09:31:55 -07004251 // If no ICDs were added to instance list and res is unchanged
4252 // from it's initial value, the loader was unable to find
4253 // a suitable ICD.
4254 if (VK_SUCCESS == res &&
4255 (ptr_instance->icd_terms == NULL || !one_icd_successful)) {
Mark Young3a587792016-08-19 15:25:08 -06004256 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4257 }
4258
4259out:
4260
4261 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004262 while (NULL != ptr_instance->icd_terms) {
4263 icd_term = ptr_instance->icd_terms;
4264 ptr_instance->icd_terms = icd_term->next;
4265 if (NULL != icd_term->instance) {
4266 icd_term->DestroyInstance(icd_term->instance, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004267 }
Mark Young0153e0b2016-11-03 14:27:13 -06004268 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004269 }
Ian Elliotteb450762015-02-05 15:19:15 -07004270 }
Jon Ashburn46888392015-01-29 15:45:51 -07004271
Mark Young3a587792016-08-19 15:25:08 -06004272 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004273}
4274
Mark Young0ad83132016-06-30 13:02:42 -06004275VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4276 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004277 struct loader_instance *ptr_instance = loader_instance(instance);
Mark Young0153e0b2016-11-03 14:27:13 -06004278 struct loader_icd_term *icd_terms = ptr_instance->icd_terms;
4279 struct loader_icd_term *next_icd_term;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004280
4281 // Remove this instance from the list of instances:
4282 struct loader_instance *prev = NULL;
4283 struct loader_instance *next = loader.instances;
4284 while (next != NULL) {
4285 if (next == ptr_instance) {
4286 // Remove this instance from the list:
4287 if (prev)
4288 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004289 else
4290 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004291 break;
4292 }
4293 prev = next;
4294 next = next->next;
4295 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004296
Mark Young0153e0b2016-11-03 14:27:13 -06004297 while (NULL != icd_terms) {
4298 if (icd_terms->instance) {
4299 icd_terms->DestroyInstance(icd_terms->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004300 }
Mark Young0153e0b2016-11-03 14:27:13 -06004301 next_icd_term = icd_terms->next;
4302 icd_terms->instance = VK_NULL_HANDLE;
4303 loader_icd_destroy(ptr_instance, icd_terms, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004304
Mark Young0153e0b2016-11-03 14:27:13 -06004305 icd_terms = next_icd_term;
Jon Ashburn46888392015-01-29 15:45:51 -07004306 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004307
Jon Ashburn23d36b12016-02-02 17:47:28 -07004308 loader_delete_layer_properties(ptr_instance,
4309 &ptr_instance->instance_layer_list);
Mark Young0153e0b2016-11-03 14:27:13 -06004310 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_tramp_list);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004311 loader_destroy_generic_list(
4312 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004313 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004314 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004315 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004316}
4317
Mark Young0ad83132016-06-30 13:02:42 -06004318VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4319 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4320 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4321 VkResult res = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06004322 struct loader_physical_device_term *phys_dev_term;
4323 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
4324 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004325
Jon Ashburncc407a22016-04-15 09:25:03 -06004326 struct loader_device *dev = (struct loader_device *)*pDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06004327 PFN_vkCreateDevice fpCreateDevice = icd_term->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004328 struct loader_extension_list icd_exts;
4329
Mark Young65cb3662016-11-07 13:27:02 -07004330 dev->phys_dev_term = phys_dev_term;
4331
Mark Young0ad83132016-06-30 13:02:42 -06004332 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004333
Jon Ashburn1530c342016-02-26 13:14:27 -07004334 if (fpCreateDevice == NULL) {
Mark Young0153e0b2016-11-03 14:27:13 -06004335 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young9a3ddd42016-10-21 16:25:47 -06004336 "No vkCreateDevice command exposed by ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004337 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004338 res = VK_ERROR_INITIALIZATION_FAILED;
4339 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004340 }
4341
Jon Ashburn1530c342016-02-26 13:14:27 -07004342 VkDeviceCreateInfo localCreateInfo;
4343 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004344
4345 /*
4346 * NOTE: Need to filter the extensions to only those
4347 * supported by the ICD.
4348 * No ICD will advertise support for layers. An ICD
4349 * library could support a layer, but it would be
4350 * independent of the actual ICD, just in the same library.
4351 */
4352 char **filtered_extension_names = NULL;
4353 filtered_extension_names =
4354 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4355 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004356 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004357 }
4358
Jon Ashburn1530c342016-02-26 13:14:27 -07004359 localCreateInfo.enabledLayerCount = 0;
4360 localCreateInfo.ppEnabledLayerNames = NULL;
4361
4362 localCreateInfo.enabledExtensionCount = 0;
4363 localCreateInfo.ppEnabledExtensionNames =
4364 (const char *const *)filtered_extension_names;
4365
Jon Ashburn014438f2016-03-01 19:51:07 -07004366 /* Get the physical device (ICD) extensions */
Mark Young0153e0b2016-11-03 14:27:13 -06004367 res = loader_init_generic_list(icd_term->this_instance,
4368 (struct loader_generic_list *)&icd_exts,
4369 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004370 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004371 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004372 }
4373
4374 res = loader_add_device_extensions(
Mark Young0153e0b2016-11-03 14:27:13 -06004375 icd_term->this_instance, icd_term->EnumerateDeviceExtensionProperties,
4376 phys_dev_term->phys_dev, icd_term->scanned_icd->lib_name, &icd_exts);
Jon Ashburn014438f2016-03-01 19:51:07 -07004377 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004378 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004379 }
4380
Jon Ashburn1530c342016-02-26 13:14:27 -07004381 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4382 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004383 VkExtensionProperties *prop =
4384 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004385 if (prop) {
4386 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4387 (char *)extension_name;
4388 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06004389 } else {
Mark Young0153e0b2016-11-03 14:27:13 -06004390 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT,
4391 0, "vkCreateDevice extension %s not available for "
4392 "devices associated with ICD %s",
4393 extension_name, icd_term->scanned_icd->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07004394 }
4395 }
4396
Mark Young0153e0b2016-11-03 14:27:13 -06004397 res = fpCreateDevice(phys_dev_term->phys_dev, &localCreateInfo, pAllocator,
Mark Young65cb3662016-11-07 13:27:02 -07004398 &dev->icd_device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004399 if (res != VK_SUCCESS) {
Mark Young0153e0b2016-11-03 14:27:13 -06004400 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young9a3ddd42016-10-21 16:25:47 -06004401 "vkCreateDevice call failed in ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004402 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004403 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004404 }
4405
Mark Young65cb3662016-11-07 13:27:02 -07004406 *pDevice = dev->icd_device;
Mark Young0153e0b2016-11-03 14:27:13 -06004407 loader_add_logical_device(icd_term->this_instance, icd_term, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004408
4409 /* Init dispatch pointer in new device object */
4410 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4411
Mark Young0ad83132016-06-30 13:02:42 -06004412out:
4413 if (NULL != icd_exts.list) {
Mark Young0153e0b2016-11-03 14:27:13 -06004414 loader_destroy_generic_list(icd_term->this_instance,
Mark Young0ad83132016-06-30 13:02:42 -06004415 (struct loader_generic_list *)&icd_exts);
4416 }
4417
Jon Ashburn1530c342016-02-26 13:14:27 -07004418 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004419}
4420
Mark Young0153e0b2016-11-03 14:27:13 -06004421VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumeratePhysicalDevices(
4422 VkInstance instance, uint32_t *pPhysicalDeviceCount,
4423 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004424 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004425 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004426 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004427
Mark Young0153e0b2016-11-03 14:27:13 -06004428 struct loader_icd_term *icd_term;
Jon Ashburn014438f2016-03-01 19:51:07 -07004429 struct loader_phys_dev_per_icd *phys_devs;
4430
4431 inst->total_gpu_count = 0;
4432 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4433 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4434 if (!phys_devs)
4435 return VK_ERROR_OUT_OF_HOST_MEMORY;
4436
Mark Young0153e0b2016-11-03 14:27:13 -06004437 icd_term = inst->icd_terms;
Jon Ashburn014438f2016-03-01 19:51:07 -07004438 for (i = 0; i < inst->total_icd_count; i++) {
Mark Young0153e0b2016-11-03 14:27:13 -06004439 assert(icd_term);
4440 res = icd_term->EnumeratePhysicalDevices(icd_term->instance,
4441 &phys_devs[i].count, NULL);
Jon Ashburn014438f2016-03-01 19:51:07 -07004442 if (res != VK_SUCCESS)
4443 return res;
Mark Young0153e0b2016-11-03 14:27:13 -06004444 icd_term = icd_term->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004445 }
4446
Mark Young0153e0b2016-11-03 14:27:13 -06004447 icd_term = inst->icd_terms;
Jon Ashburn014438f2016-03-01 19:51:07 -07004448 for (i = 0; i < inst->total_icd_count; i++) {
Mark Young0153e0b2016-11-03 14:27:13 -06004449 assert(icd_term);
Jon Ashburn014438f2016-03-01 19:51:07 -07004450 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4451 phys_devs[i].count * sizeof(VkPhysicalDevice));
4452 if (!phys_devs[i].phys_devs) {
4453 return VK_ERROR_OUT_OF_HOST_MEMORY;
4454 }
Mark Young0153e0b2016-11-03 14:27:13 -06004455 res = icd_term->EnumeratePhysicalDevices(
4456 icd_term->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
Jon Ashburn014438f2016-03-01 19:51:07 -07004457 if ((res == VK_SUCCESS)) {
4458 inst->total_gpu_count += phys_devs[i].count;
4459 } else {
4460 return res;
4461 }
Mark Young0153e0b2016-11-03 14:27:13 -06004462 phys_devs[i].this_icd_term = icd_term;
4463 icd_term = icd_term->next;
Jon Ashburn014438f2016-03-01 19:51:07 -07004464 }
Mark Young76a24632016-10-27 15:32:08 -06004465 if (inst->total_gpu_count == 0) {
4466 return VK_ERROR_INITIALIZATION_FAILED;
4467 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004468
Mark Young559d7502016-09-26 11:38:46 -06004469 uint32_t copy_count = inst->total_gpu_count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004470
Mark Young559d7502016-09-26 11:38:46 -06004471 if (NULL != pPhysicalDevices) {
4472 // Initialize the output pPhysicalDevices with wrapped loader
4473 // terminator physicalDevice objects; save this list of
4474 // wrapped objects in instance struct for later cleanup and
4475 // use by trampoline code
4476 uint32_t j, idx = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004477
Mark Young559d7502016-09-26 11:38:46 -06004478 if (copy_count > *pPhysicalDeviceCount) {
4479 copy_count = *pPhysicalDeviceCount;
4480 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004481
Mark Young76a24632016-10-27 15:32:08 -06004482 if (NULL == inst->phys_devs_term) {
Mark Young559d7502016-09-26 11:38:46 -06004483 inst->phys_devs_term = loader_instance_heap_alloc(
Mark Young0153e0b2016-11-03 14:27:13 -06004484 inst, sizeof(struct loader_physical_device_term) *
4485 inst->total_gpu_count,
Mark Young559d7502016-09-26 11:38:46 -06004486 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young76a24632016-10-27 15:32:08 -06004487 if (NULL == inst->phys_devs_term) {
Mark Young559d7502016-09-26 11:38:46 -06004488 return VK_ERROR_OUT_OF_HOST_MEMORY;
4489 }
4490 }
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004491
Mark Young76a24632016-10-27 15:32:08 -06004492 for (i = 0; idx < inst->total_gpu_count && i < inst->total_icd_count;
4493 i++) {
4494 for (j = 0; j < phys_devs[i].count && idx < inst->total_gpu_count;
4495 j++) {
Mark Young559d7502016-09-26 11:38:46 -06004496 loader_set_dispatch((void *)&inst->phys_devs_term[idx],
4497 inst->disp);
Mark Young0153e0b2016-11-03 14:27:13 -06004498 inst->phys_devs_term[idx].this_icd_term =
4499 phys_devs[i].this_icd_term;
Jamie Madilld29fc092016-10-24 16:54:40 -04004500 inst->phys_devs_term[idx].icd_index = (uint8_t)(i);
Mark Young559d7502016-09-26 11:38:46 -06004501 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
Slawomir Cyganeb610df2016-10-05 18:38:02 +02004502 if (idx < copy_count) {
4503 pPhysicalDevices[idx] =
4504 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4505 }
Mark Young559d7502016-09-26 11:38:46 -06004506 idx++;
4507 }
4508 }
4509
4510 if (copy_count < inst->total_gpu_count) {
Mark Young559d7502016-09-26 11:38:46 -06004511 res = VK_INCOMPLETE;
Jon Ashburn014438f2016-03-01 19:51:07 -07004512 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004513 }
Mark Young559d7502016-09-26 11:38:46 -06004514
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004515 *pPhysicalDeviceCount = copy_count;
4516
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004517 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004518}
4519
Jon Ashburn1530c342016-02-26 13:14:27 -07004520VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4521 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004522 struct loader_physical_device_term *phys_dev_term =
4523 (struct loader_physical_device_term *)physicalDevice;
4524 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004525
Mark Young0153e0b2016-11-03 14:27:13 -06004526 if (icd_term->GetPhysicalDeviceProperties)
4527 icd_term->GetPhysicalDeviceProperties(phys_dev_term->phys_dev,
4528 pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004529}
4530
Jon Ashburn1530c342016-02-26 13:14:27 -07004531VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004532 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4533 VkQueueFamilyProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004534 struct loader_physical_device_term *phys_dev_term =
4535 (struct loader_physical_device_term *)physicalDevice;
4536 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Tony Barbour59a47322015-06-24 16:06:58 -06004537
Mark Young0153e0b2016-11-03 14:27:13 -06004538 if (icd_term->GetPhysicalDeviceQueueFamilyProperties)
4539 icd_term->GetPhysicalDeviceQueueFamilyProperties(
4540 phys_dev_term->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004541}
4542
Jon Ashburn1530c342016-02-26 13:14:27 -07004543VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004544 VkPhysicalDevice physicalDevice,
4545 VkPhysicalDeviceMemoryProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004546 struct loader_physical_device_term *phys_dev_term =
4547 (struct loader_physical_device_term *)physicalDevice;
4548 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Tony Barbour59a47322015-06-24 16:06:58 -06004549
Mark Young0153e0b2016-11-03 14:27:13 -06004550 if (icd_term->GetPhysicalDeviceMemoryProperties)
4551 icd_term->GetPhysicalDeviceMemoryProperties(phys_dev_term->phys_dev,
4552 pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004553}
4554
Mark Young0153e0b2016-11-03 14:27:13 -06004555VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFeatures(
4556 VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures *pFeatures) {
4557 struct loader_physical_device_term *phys_dev_term =
4558 (struct loader_physical_device_term *)physicalDevice;
4559 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004560
Mark Young0153e0b2016-11-03 14:27:13 -06004561 if (icd_term->GetPhysicalDeviceFeatures)
4562 icd_term->GetPhysicalDeviceFeatures(phys_dev_term->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004563}
4564
Mark Young0153e0b2016-11-03 14:27:13 -06004565VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFormatProperties(
4566 VkPhysicalDevice physicalDevice, VkFormat format,
4567 VkFormatProperties *pFormatInfo) {
4568 struct loader_physical_device_term *phys_dev_term =
4569 (struct loader_physical_device_term *)physicalDevice;
4570 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004571
Mark Young0153e0b2016-11-03 14:27:13 -06004572 if (icd_term->GetPhysicalDeviceFormatProperties)
4573 icd_term->GetPhysicalDeviceFormatProperties(phys_dev_term->phys_dev,
4574 format, pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004575}
4576
Jon Ashburn1530c342016-02-26 13:14:27 -07004577VKAPI_ATTR VkResult VKAPI_CALL
4578terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004579 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4580 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4581 VkImageFormatProperties *pImageFormatProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004582 struct loader_physical_device_term *phys_dev_term =
4583 (struct loader_physical_device_term *)physicalDevice;
4584 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn754864f2015-07-23 18:49:07 -06004585
Mark Young0153e0b2016-11-03 14:27:13 -06004586 if (!icd_term->GetPhysicalDeviceImageFormatProperties)
Chia-I Wu17241042015-10-31 00:31:16 +08004587 return VK_ERROR_INITIALIZATION_FAILED;
4588
Mark Young0153e0b2016-11-03 14:27:13 -06004589 return icd_term->GetPhysicalDeviceImageFormatProperties(
4590 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004591 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004592}
4593
Jon Ashburn1530c342016-02-26 13:14:27 -07004594VKAPI_ATTR void VKAPI_CALL
4595terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004596 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4597 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4598 VkImageTiling tiling, uint32_t *pNumProperties,
4599 VkSparseImageFormatProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004600 struct loader_physical_device_term *phys_dev_term =
4601 (struct loader_physical_device_term *)physicalDevice;
4602 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004603
Mark Young0153e0b2016-11-03 14:27:13 -06004604 if (icd_term->GetPhysicalDeviceSparseImageFormatProperties)
4605 icd_term->GetPhysicalDeviceSparseImageFormatProperties(
4606 phys_dev_term->phys_dev, format, type, samples, usage, tiling,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004607 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004608}
4609
Jon Ashburn1530c342016-02-26 13:14:27 -07004610VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4611 VkPhysicalDevice physicalDevice, const char *pLayerName,
4612 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004613 struct loader_physical_device_term *phys_dev_term;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004614
Mark Young3a587792016-08-19 15:25:08 -06004615 struct loader_layer_list implicit_layer_list = {0};
4616 struct loader_extension_list all_exts = {0};
4617 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07004618
Jon Ashburndc5d9202016-02-29 13:00:51 -07004619 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004620
Jon Ashburndc5d9202016-02-29 13:00:51 -07004621 /* Any layer or trampoline wrapping should be removed at this point in time
4622 * can just cast to the expected type for VkPhysicalDevice. */
Mark Young0153e0b2016-11-03 14:27:13 -06004623 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004624
Jon Ashburndc5d9202016-02-29 13:00:51 -07004625 /* this case is during the call down the instance chain with pLayerName
4626 * == NULL*/
Mark Young0153e0b2016-11-03 14:27:13 -06004627 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004628 uint32_t icd_ext_count = *pPropertyCount;
4629 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004630
Jon Ashburndc5d9202016-02-29 13:00:51 -07004631 /* get device extensions */
Mark Young0153e0b2016-11-03 14:27:13 -06004632 res = icd_term->EnumerateDeviceExtensionProperties(
4633 phys_dev_term->phys_dev, NULL, &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06004634 if (res != VK_SUCCESS) {
4635 goto out;
4636 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004637
Mark Young0153e0b2016-11-03 14:27:13 -06004638 if (!loader_init_layer_list(icd_term->this_instance,
4639 &implicit_layer_list)) {
Mark Young3a587792016-08-19 15:25:08 -06004640 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4641 goto out;
4642 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004643
Jon Ashburndc5d9202016-02-29 13:00:51 -07004644 loader_add_layer_implicit(
Mark Young0153e0b2016-11-03 14:27:13 -06004645 icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4646 &implicit_layer_list, &icd_term->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004647 /* we need to determine which implicit layers are active,
4648 * and then add their extensions. This can't be cached as
4649 * it depends on results of environment variables (which can change).
4650 */
4651 if (pProperties != NULL) {
4652 /* initialize dev_extension list within the physicalDevice object */
Mark Young0153e0b2016-11-03 14:27:13 -06004653 res = loader_init_device_extensions(icd_term->this_instance,
4654 phys_dev_term, icd_ext_count,
4655 pProperties, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004656 if (res != VK_SUCCESS) {
4657 goto out;
4658 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004659
Jon Ashburndc5d9202016-02-29 13:00:51 -07004660 /* we need to determine which implicit layers are active,
4661 * and then add their extensions. This can't be cached as
4662 * it depends on results of environment variables (which can
4663 * change).
4664 */
Mark Young0153e0b2016-11-03 14:27:13 -06004665 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts,
Mark Young3a587792016-08-19 15:25:08 -06004666 icd_exts.count, icd_exts.list);
4667 if (res != VK_SUCCESS) {
4668 goto out;
4669 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004670
Jon Ashburndc5d9202016-02-29 13:00:51 -07004671 loader_add_layer_implicit(
Mark Young0153e0b2016-11-03 14:27:13 -06004672 icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4673 &implicit_layer_list,
4674 &icd_term->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004675
Jon Ashburndc5d9202016-02-29 13:00:51 -07004676 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4677 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004678 j < implicit_layer_list.list[i].device_extension_list.count;
4679 j++) {
Mark Young0153e0b2016-11-03 14:27:13 -06004680 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts,
4681 1,
Mark Young3a587792016-08-19 15:25:08 -06004682 &implicit_layer_list.list[i]
4683 .device_extension_list.list[j]
4684 .props);
4685 if (res != VK_SUCCESS) {
4686 goto out;
4687 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004688 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004689 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004690 uint32_t capacity = *pPropertyCount;
4691 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004692
Jon Ashburndc5d9202016-02-29 13:00:51 -07004693 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4694 props[i] = all_exts.list[i];
4695 }
4696 /* wasn't enough space for the extensions, we did partial copy now
4697 * return VK_INCOMPLETE */
4698 if (capacity < all_exts.count) {
4699 res = VK_INCOMPLETE;
4700 } else {
4701 *pPropertyCount = all_exts.count;
4702 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004703 } else {
4704 /* just return the count; need to add in the count of implicit layer
4705 * extensions
4706 * don't worry about duplicates being added in the count */
4707 *pPropertyCount = icd_ext_count;
4708
4709 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4710 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004711 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004712 }
4713 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004714 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004715
Mark Young3a587792016-08-19 15:25:08 -06004716out:
4717
4718 if (NULL != implicit_layer_list.list) {
4719 loader_destroy_generic_list(
Mark Young0153e0b2016-11-03 14:27:13 -06004720 icd_term->this_instance,
Mark Young3a587792016-08-19 15:25:08 -06004721 (struct loader_generic_list *)&implicit_layer_list);
4722 }
4723 if (NULL != all_exts.list) {
Mark Young0153e0b2016-11-03 14:27:13 -06004724 loader_destroy_generic_list(icd_term->this_instance,
Mark Young3a587792016-08-19 15:25:08 -06004725 (struct loader_generic_list *)&all_exts);
4726 }
4727 if (NULL != icd_exts.list) {
Mark Young0153e0b2016-11-03 14:27:13 -06004728 loader_destroy_generic_list(icd_term->this_instance,
Mark Young3a587792016-08-19 15:25:08 -06004729 (struct loader_generic_list *)&icd_exts);
4730 }
4731
Jon Ashburndc5d9202016-02-29 13:00:51 -07004732 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004733}
4734
Mark Young0153e0b2016-11-03 14:27:13 -06004735VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceLayerProperties(
4736 VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
4737 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004738
Jon Ashburndc5d9202016-02-29 13:00:51 -07004739 // should never get here this call isn't dispatched down the chain
4740 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004741}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004742
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004743VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004744 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004745 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004746 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004747
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004748 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004749 if (utf8[i] == 0) {
4750 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004751 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004752 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004753 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004754 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004755 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004756 num_char_bytes = 2;
4757 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4758 num_char_bytes = 3;
4759 } else {
4760 result = VK_STRING_ERROR_BAD_DATA;
4761 }
4762
4763 // Validate the following num_char_bytes of data
4764 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4765 if (++i == max_length) {
4766 result |= VK_STRING_ERROR_LENGTH;
4767 break;
4768 }
4769 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4770 result |= VK_STRING_ERROR_BAD_DATA;
4771 }
4772 }
4773 }
4774 return result;
4775}