blob: b433237adae7b0aef6863f26bee1a225631f0760 [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) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700384 util_DebugReportMessage(inst, msg_type,
385 VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
386 (uint64_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)
1265 /* Value comparison of device prevents object wrapping by layers
1266 */
1267 if (loader_get_dispatch(dev->device) ==
1268 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001269 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001270 if (NULL != icd_index) {
1271 *icd_index = index;
1272 }
Mark Young0153e0b2016-11-03 14:27:13 -06001273 return icd_term;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001274 }
Mark Young16573c72016-06-28 10:52:43 -06001275 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001276 }
1277 }
1278 return NULL;
1279}
1280
Mark Young0ad83132016-06-30 13:02:42 -06001281void loader_destroy_logical_device(const struct loader_instance *inst,
1282 struct loader_device *dev,
1283 const VkAllocationCallbacks *pAllocator) {
1284 if (pAllocator) {
1285 dev->alloc_callbacks = *pAllocator;
1286 }
Mark Young0ad83132016-06-30 13:02:42 -06001287 if (NULL != dev->activated_layer_list.list) {
1288 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1289 }
1290 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001291}
1292
Jon Ashburn1530c342016-02-26 13:14:27 -07001293struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001294loader_create_logical_device(const struct loader_instance *inst,
1295 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001296 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001297#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1298 {
1299#else
1300 if (pAllocator) {
1301 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1302 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1303 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1304 } else {
1305#endif
1306 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1307 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001308
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001309 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001310 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1311 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001312 return NULL;
1313 }
1314
1315 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001316 if (pAllocator) {
1317 new_dev->alloc_callbacks = *pAllocator;
1318 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001319
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001320 return new_dev;
1321}
1322
Piers Daniell295fe402016-03-29 11:51:11 -06001323void loader_add_logical_device(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001324 struct loader_icd_term *icd_term,
Piers Daniell295fe402016-03-29 11:51:11 -06001325 struct loader_device *dev) {
Mark Young0153e0b2016-11-03 14:27:13 -06001326 dev->next = icd_term->logical_device_list;
1327 icd_term->logical_device_list = dev;
Piers Daniell295fe402016-03-29 11:51:11 -06001328}
1329
Jon Ashburn23d36b12016-02-02 17:47:28 -07001330void loader_remove_logical_device(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001331 struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001332 struct loader_device *found_dev,
1333 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001334 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001335
Mark Young0153e0b2016-11-03 14:27:13 -06001336 if (!icd_term || !found_dev)
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001337 return;
1338
1339 prev_dev = NULL;
Mark Young0153e0b2016-11-03 14:27:13 -06001340 dev = icd_term->logical_device_list;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001341 while (dev && dev != found_dev) {
1342 prev_dev = dev;
1343 dev = dev->next;
1344 }
1345
1346 if (prev_dev)
1347 prev_dev->next = found_dev->next;
1348 else
Mark Young0153e0b2016-11-03 14:27:13 -06001349 icd_term->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001350 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001351}
1352
Jon Ashburn23d36b12016-02-02 17:47:28 -07001353static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001354 struct loader_icd_term *icd_term,
Mark Young0ad83132016-06-30 13:02:42 -06001355 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001356 ptr_inst->total_icd_count--;
Mark Young0153e0b2016-11-03 14:27:13 -06001357 for (struct loader_device *dev = icd_term->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001358 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001359 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001360 dev = next_dev;
1361 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001362
Mark Young0153e0b2016-11-03 14:27:13 -06001363 loader_instance_heap_free(ptr_inst, icd_term);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001364}
1365
Mark Young0153e0b2016-11-03 14:27:13 -06001366static struct loader_icd_term *
Jon Ashburn23d36b12016-02-02 17:47:28 -07001367loader_icd_create(const struct loader_instance *inst) {
Mark Young0153e0b2016-11-03 14:27:13 -06001368 struct loader_icd_term *icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001369
Mark Young0153e0b2016-11-03 14:27:13 -06001370 icd_term = loader_instance_heap_alloc(inst, sizeof(struct loader_icd_term),
1371 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1372 if (!icd_term) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001373 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001374 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001375
Mark Young0153e0b2016-11-03 14:27:13 -06001376 memset(icd_term, 0, sizeof(struct loader_icd_term));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001377
Mark Young0153e0b2016-11-03 14:27:13 -06001378 return icd_term;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001379}
1380
Mark Young0153e0b2016-11-03 14:27:13 -06001381static struct loader_icd_term *
Jon Ashburn23d36b12016-02-02 17:47:28 -07001382loader_icd_add(struct loader_instance *ptr_inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001383 const struct loader_scanned_icd *scanned_icd) {
1384 struct loader_icd_term *icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001385
Mark Young0153e0b2016-11-03 14:27:13 -06001386 icd_term = loader_icd_create(ptr_inst);
1387 if (!icd_term) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001388 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001389 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001390
Mark Young0153e0b2016-11-03 14:27:13 -06001391 icd_term->scanned_icd = scanned_icd;
1392 icd_term->this_instance = ptr_inst;
Jon Ashburn3d002332015-08-20 16:35:30 -06001393
Chia-I Wu13a61a52014-08-04 11:18:20 +08001394 /* prepend to the list */
Mark Young0153e0b2016-11-03 14:27:13 -06001395 icd_term->next = ptr_inst->icd_terms;
1396 ptr_inst->icd_terms = icd_term;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001397 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001398
Mark Young0153e0b2016-11-03 14:27:13 -06001399 return icd_term;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001400}
Mark Young0153e0b2016-11-03 14:27:13 -06001401
Jon Ashburn17b4c862016-04-25 11:09:37 -06001402/**
1403 * Determine the ICD interface version to use.
1404 * @param icd
1405 * @param pVersion Output parameter indicating which version to use or 0 if
1406 * the negotiation API is not supported by the ICD
1407 * @return bool indicating true if the selected interface version is supported
1408 * by the loader, false indicates the version is not supported
1409 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1410 * vk_icdNegotiateLoaderICDInterfaceVersion
1411 * version 1 supports vk_icdGetInstanceProcAddr
1412 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1413 */
1414bool loader_get_icd_interface_version(
Mark Young0153e0b2016-11-03 14:27:13 -06001415 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1416 uint32_t *pVersion) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001417
1418 if (fp_negotiate_icd_version == NULL) {
1419 // ICD does not support the negotiation API, it supports version 0 or 1
1420 // calling code must determine if it is version 0 or 1
1421 *pVersion = 0;
1422 } else {
1423 // ICD supports the negotiation API, so call it with the loader's
1424 // latest version supported
1425 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1426 VkResult result = fp_negotiate_icd_version(pVersion);
1427
1428 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1429 // ICD no longer supports the loader's latest interface version so
1430 // fail loading the ICD
1431 return false;
1432 }
1433 }
1434
1435#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1436 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1437 // Loader no longer supports the ICD's latest interface version so fail
1438 // loading the ICD
1439 return false;
1440 }
1441#endif
1442 return true;
1443}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001444
Jon Ashburn23d36b12016-02-02 17:47:28 -07001445void loader_scanned_icd_clear(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06001446 struct loader_icd_tramp_list *icd_tramp_list) {
1447 if (icd_tramp_list->capacity == 0)
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001448 return;
Mark Young0153e0b2016-11-03 14:27:13 -06001449 for (uint32_t i = 0; i < icd_tramp_list->count; i++) {
1450 loader_platform_close_library(icd_tramp_list->scanned_list[i].handle);
1451 loader_instance_heap_free(inst,
1452 icd_tramp_list->scanned_list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001453 }
Mark Young0153e0b2016-11-03 14:27:13 -06001454 loader_instance_heap_free(inst, icd_tramp_list->scanned_list);
1455 icd_tramp_list->capacity = 0;
1456 icd_tramp_list->count = 0;
1457 icd_tramp_list->scanned_list = NULL;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001458}
1459
Mark Young0153e0b2016-11-03 14:27:13 -06001460static VkResult
1461loader_scanned_icd_init(const struct loader_instance *inst,
1462 struct loader_icd_tramp_list *icd_tramp_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001463 VkResult err = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06001464 loader_scanned_icd_clear(inst, icd_tramp_list);
1465 icd_tramp_list->capacity = 8 * sizeof(struct loader_scanned_icd);
1466 icd_tramp_list->scanned_list = loader_instance_heap_alloc(
1467 inst, icd_tramp_list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1468 if (NULL == icd_tramp_list->scanned_list) {
1469 loader_log(
1470 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06001471 "realloc failed for layer list when attempting to add new layer");
1472 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1473 }
1474 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001475}
1476
Mark Young0153e0b2016-11-03 14:27:13 -06001477static VkResult
1478loader_scanned_icd_add(const struct loader_instance *inst,
1479 struct loader_icd_tramp_list *icd_tramp_list,
1480 const char *filename, uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001481 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001482 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001483 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001484 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001485 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Mark Young0153e0b2016-11-03 14:27:13 -06001486 struct loader_scanned_icd *new_scanned_icd;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001487 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001488 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001489
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001490 /* TODO implement smarter opening/closing of libraries. For now this
1491 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001492 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001493 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001494 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001495 loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001496 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001497 }
1498
Jon Ashburn17b4c862016-04-25 11:09:37 -06001499 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001500 fp_negotiate_icd_version = loader_platform_get_proc_address(
1501 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001502
1503 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001504 &interface_vers)) {
1505 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1506 "ICD (%s) doesn't support interface version compatible"
1507 "with loader, skip this ICD %s",
1508 filename);
Mark Young3a587792016-08-19 15:25:08 -06001509 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001510 }
1511
Jon Ashburn23d36b12016-02-02 17:47:28 -07001512 fp_get_proc_addr =
1513 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001514 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001515 assert(interface_vers == 0);
1516 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001517 fp_get_proc_addr =
1518 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001519 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001520 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1521 loader_platform_get_proc_address_error(
1522 "vk_icdGetInstanceProcAddr"));
Mark Young3a587792016-08-19 15:25:08 -06001523 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001524 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001525 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001526 "Using deprecated ICD interface of "
1527 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001528 "vk_icdGetInstanceProcAddr for ICD %s",
1529 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001530 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001531 fp_create_inst =
1532 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001533 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001534 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1535 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1536 "for ICD %s",
1537 filename);
Mark Young3a587792016-08-19 15:25:08 -06001538 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001539 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001540 fp_get_inst_ext_props = loader_platform_get_proc_address(
1541 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001542 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001543 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1544 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001545 "via dlsym/loadlibrary for ICD %s",
1546 filename);
Mark Young3a587792016-08-19 15:25:08 -06001547 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001548 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001549 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001550 // Use newer interface version 1 or later
1551 if (interface_vers == 0)
1552 interface_vers = 1;
1553
Jon Ashburn23d36b12016-02-02 17:47:28 -07001554 fp_create_inst =
1555 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001556 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001557 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1558 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001559 "vk_icdGetInstanceProcAddr for ICD %s",
1560 filename);
Mark Young3a587792016-08-19 15:25:08 -06001561 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001562 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001563 fp_get_inst_ext_props =
1564 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1565 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001566 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001567 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1568 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001569 "via vk_icdGetInstanceProcAddr for ICD %s",
1570 filename);
Mark Young3a587792016-08-19 15:25:08 -06001571 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001572 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001573 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001574
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001575 // check for enough capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001576 if ((icd_tramp_list->count * sizeof(struct loader_scanned_icd)) >=
1577 icd_tramp_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001578
Mark Young0153e0b2016-11-03 14:27:13 -06001579 icd_tramp_list->scanned_list = loader_instance_heap_realloc(
1580 inst, icd_tramp_list->scanned_list, icd_tramp_list->capacity,
1581 icd_tramp_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1582 if (NULL == icd_tramp_list->scanned_list) {
Mark Young3a587792016-08-19 15:25:08 -06001583 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001584 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1585 "realloc failed on icd library list");
Mark Young3a587792016-08-19 15:25:08 -06001586 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001587 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001588 // double capacity
Mark Young0153e0b2016-11-03 14:27:13 -06001589 icd_tramp_list->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001590 }
Mark Young0153e0b2016-11-03 14:27:13 -06001591 new_scanned_icd = &(icd_tramp_list->scanned_list[icd_tramp_list->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001592
Mark Young0153e0b2016-11-03 14:27:13 -06001593 new_scanned_icd->handle = handle;
1594 new_scanned_icd->api_version = api_version;
1595 new_scanned_icd->GetInstanceProcAddr = fp_get_proc_addr;
1596 new_scanned_icd->EnumerateInstanceExtensionProperties =
1597 fp_get_inst_ext_props;
1598 new_scanned_icd->CreateInstance = fp_create_inst;
1599 new_scanned_icd->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001600
Mark Young0153e0b2016-11-03 14:27:13 -06001601 new_scanned_icd->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001602 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0153e0b2016-11-03 14:27:13 -06001603 if (NULL == new_scanned_icd->lib_name) {
Mark Young3a587792016-08-19 15:25:08 -06001604 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001605 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001606 "Out of memory can't add icd");
Mark Young3a587792016-08-19 15:25:08 -06001607 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001608 }
Mark Young0153e0b2016-11-03 14:27:13 -06001609 strcpy(new_scanned_icd->lib_name, filename);
1610 icd_tramp_list->count++;
Mark Young3a587792016-08-19 15:25:08 -06001611
1612out:
1613
1614 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001615}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001616
Mark Young0153e0b2016-11-03 14:27:13 -06001617static bool loader_icd_init_entrys(struct loader_icd_term *icd_term,
1618 VkInstance inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001619 const PFN_vkGetInstanceProcAddr fp_gipa) {
1620/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001621
Jon Ashburn23d36b12016-02-02 17:47:28 -07001622#define LOOKUP_GIPA(func, required) \
1623 do { \
Mark Young0153e0b2016-11-03 14:27:13 -06001624 icd_term->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1625 if (!icd_term->func && required) { \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001626 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001627 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001628 loader_platform_get_proc_address_error("vk" #func)); \
1629 return false; \
1630 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001631 } while (0)
1632
Jon Ashburnc624c882015-07-16 10:17:29 -06001633 LOOKUP_GIPA(GetDeviceProcAddr, true);
1634 LOOKUP_GIPA(DestroyInstance, true);
1635 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1636 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1637 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001638 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001639 LOOKUP_GIPA(CreateDevice, true);
1640 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1641 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001642 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001643 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001644 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001645 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1646 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001647 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001648 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1649 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1650 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001651 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1652 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1653 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1654 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1655 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1656 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1657 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1658 LOOKUP_GIPA(DestroySurfaceKHR, false);
Mark Young16573c72016-06-28 10:52:43 -06001659 LOOKUP_GIPA(CreateSwapchainKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001660#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Young16573c72016-06-28 10:52:43 -06001661 LOOKUP_GIPA(CreateWin32SurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001662 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1663#endif
1664#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001665 LOOKUP_GIPA(CreateXcbSurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001666 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1667#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001668#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001669 LOOKUP_GIPA(CreateXlibSurfaceKHR, false);
Karl Schultz65d20182016-03-08 07:55:27 -07001670 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1671#endif
Mark Younga7c51fd2016-09-16 10:18:42 -06001672#ifdef VK_USE_PLATFORM_MIR_KHR
1673 LOOKUP_GIPA(CreateMirSurfaceKHR, false);
1674 LOOKUP_GIPA(GetPhysicalDeviceMirPresentationSupportKHR, false);
1675#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001676#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Young16573c72016-06-28 10:52:43 -06001677 LOOKUP_GIPA(CreateWaylandSurfaceKHR, false);
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001678 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1679#endif
James Jones389dc0c2016-08-18 23:41:19 +01001680 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001681
Jon Ashburnc624c882015-07-16 10:17:29 -06001682#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001683
Jon Ashburnc624c882015-07-16 10:17:29 -06001684 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001685}
1686
Jon Ashburn23d36b12016-02-02 17:47:28 -07001687static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001688 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001689
1690 if (g_loader_debug > 0)
1691 return;
1692
1693 g_loader_debug = 0;
1694
1695 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001696 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001697 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001698 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001699 size_t len;
1700
1701 if (p)
1702 len = p - env;
1703 else
1704 len = strlen(env);
1705
1706 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001707 if (strncmp(env, "all", len) == 0) {
1708 g_loader_debug = ~0u;
1709 g_loader_log_msgs = ~0u;
1710 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001711 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001712 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001713 } else if (strncmp(env, "info", len) == 0) {
1714 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001715 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001716 } else if (strncmp(env, "perf", len) == 0) {
1717 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001718 g_loader_log_msgs |=
1719 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001720 } else if (strncmp(env, "error", len) == 0) {
1721 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001722 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001723 } else if (strncmp(env, "debug", len) == 0) {
1724 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001725 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001726 }
1727 }
1728
1729 if (!p)
1730 break;
1731
1732 env = p + 1;
1733 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001734
Mark Young0ad83132016-06-30 13:02:42 -06001735 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001736}
1737
Jon Ashburn23d36b12016-02-02 17:47:28 -07001738void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001739 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001740 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001741 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001742
1743 // initialize logging
1744 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001745
1746 // initial cJSON to use alloc callbacks
1747 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001748 .malloc_fn = loader_instance_tls_heap_alloc,
1749 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001750 };
1751 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001752}
1753
Jon Ashburn2077e382015-06-29 11:25:34 -06001754struct loader_manifest_files {
1755 uint32_t count;
1756 char **filename_list;
1757};
1758
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001759/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001760 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001761 *
1762 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001763 * A pointer to first char in the next path.
1764 * The next path (or NULL) in the list is returned in next_path.
1765 * Note: input string is modified in some cases. PASS IN A COPY!
1766 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001767static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001768 uint32_t len;
1769 char *next;
1770
1771 if (path == NULL)
1772 return NULL;
1773 next = strchr(path, PATH_SEPERATOR);
1774 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001775 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001776 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001777 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001778 *next = '\0';
1779 next++;
1780 }
1781
1782 return next;
1783}
1784
1785/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001786 * Given a path which is absolute or relative, expand the path if relative or
1787 * leave the path unmodified if absolute. The base path to prepend to relative
1788 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001789 *
1790 * \returns
1791 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001792 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001793static void loader_expand_path(const char *path, const char *rel_base,
1794 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001795 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001796 // do not prepend a base to an absolute path
1797 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001798 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001799
1800 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001801}
1802
1803/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001804 * Given a filename (file) and a list of paths (dir), try to find an existing
1805 * file in the paths. If filename already is a path then no
1806 * searching in the given paths.
1807 *
1808 * \returns
1809 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001810 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001811static void loader_get_fullpath(const char *file, const char *dirs,
1812 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001813 if (!loader_platform_is_path(file) && *dirs) {
1814 char *dirs_copy, *dir, *next_dir;
1815
1816 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1817 strcpy(dirs_copy, dirs);
1818
Jon Ashburn23d36b12016-02-02 17:47:28 -07001819 // find if file exists after prepending paths in given list
1820 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001821 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001822 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1823 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001824 if (loader_platform_file_exists(out_fullpath)) {
1825 return;
1826 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001827 }
1828 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001829
Jon Ashburn2077e382015-06-29 11:25:34 -06001830 snprintf(out_fullpath, out_size, "%s", file);
1831}
1832
1833/**
1834 * Read a JSON file into a buffer.
1835 *
1836 * \returns
1837 * A pointer to a cJSON object representing the JSON parse tree.
1838 * This returned buffer should be freed by caller.
1839 */
Mark Young3a587792016-08-19 15:25:08 -06001840static VkResult loader_get_json(const struct loader_instance *inst,
1841 const char *filename, cJSON **json) {
1842 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001843 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001844 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001845 VkResult res = VK_SUCCESS;
1846
1847 if (NULL == json) {
1848 res = VK_ERROR_INITIALIZATION_FAILED;
1849 goto out;
1850 }
1851
1852 *json = NULL;
1853
Jon Ashburn23d36b12016-02-02 17:47:28 -07001854 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001855 if (!file) {
Mark Young3a587792016-08-19 15:25:08 -06001856 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001857 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1858 "Couldn't open JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001859 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001860 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001861 fseek(file, 0, SEEK_END);
1862 len = ftell(file);
1863 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001864 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001865 if (json_buf == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06001866 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001867 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1868 "Out of memory can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001869 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001870 }
1871 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Young3a587792016-08-19 15:25:08 -06001872 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001873 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1874 "fread failed can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001875 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001876 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001877 json_buf[len] = '\0';
1878
Jon Ashburn23d36b12016-02-02 17:47:28 -07001879 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001880 *json = cJSON_Parse(json_buf);
1881 if (*json == NULL) {
1882 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001883 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1884 "Can't parse JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001885 goto out;
1886 }
1887
1888out:
1889 if (NULL != file) {
1890 fclose(file);
1891 }
1892
1893 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001894}
1895
1896/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001897 * Do a deep copy of the loader_layer_properties structure.
1898 */
Mark Young0ad83132016-06-30 13:02:42 -06001899VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1900 struct loader_layer_properties *dst,
1901 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001902 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001903 memcpy(dst, src, sizeof(*src));
1904 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001905 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1906 src->instance_extension_list.count,
1907 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1908 if (NULL == dst->instance_extension_list.list) {
1909 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1910 "alloc failed for instance extension list");
1911 return VK_ERROR_OUT_OF_HOST_MEMORY;
1912 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001913 dst->instance_extension_list.capacity =
1914 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001915 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001916 dst->instance_extension_list.capacity);
1917 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001918 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1919 src->device_extension_list.count,
1920 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1921 if (NULL == dst->device_extension_list.list) {
1922 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1923 "alloc failed for device extension list");
1924 return VK_ERROR_OUT_OF_HOST_MEMORY;
1925 }
Mark Young0153e0b2016-11-03 14:27:13 -06001926 memset(dst->device_extension_list.list, 0,
1927 sizeof(struct loader_dev_ext_props) *
1928 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001929
Jon Ashburn23d36b12016-02-02 17:47:28 -07001930 dst->device_extension_list.capacity =
1931 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001932 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001933 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001934 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001935 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001936 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001937 dst->device_extension_list.list->entrypoints =
1938 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1939 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1940 if (NULL == dst->device_extension_list.list->entrypoints) {
1941 loader_log(
1942 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1943 "alloc failed for device extension list entrypoint array");
1944 return VK_ERROR_OUT_OF_HOST_MEMORY;
1945 }
Mark Young0153e0b2016-11-03 14:27:13 -06001946 memset(dst->device_extension_list.list->entrypoints, 0,
1947 sizeof(char *) * cnt);
Mark Young0ad83132016-06-30 13:02:42 -06001948
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001949 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001950 dst->device_extension_list.list->entrypoints[i] =
1951 loader_instance_heap_alloc(
1952 inst,
1953 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1954 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1955 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1956 loader_log(
1957 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1958 "alloc failed for device extension list entrypoint %d", i);
1959 return VK_ERROR_OUT_OF_HOST_MEMORY;
1960 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001961 strcpy(dst->device_extension_list.list->entrypoints[i],
1962 src->device_extension_list.list->entrypoints[i]);
1963 }
1964 }
Mark Young0ad83132016-06-30 13:02:42 -06001965
1966 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001967}
1968
Jon Ashburn86a527a2016-02-10 20:59:26 -07001969static bool
1970loader_find_layer_name_list(const char *name,
1971 const struct loader_layer_list *layer_list) {
1972 if (!layer_list)
1973 return false;
1974 for (uint32_t j = 0; j < layer_list->count; j++)
1975 if (!strcmp(name, layer_list->list[j].info.layerName))
1976 return true;
1977 return false;
1978}
1979
1980static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1981 const char **layer_list) {
1982 if (!layer_list)
1983 return false;
1984 for (uint32_t j = 0; j < layer_count; j++)
1985 if (!strcmp(name, layer_list[j]))
1986 return true;
1987 return false;
1988}
1989
Jon Ashburn491cd042016-05-16 14:01:18 -06001990bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001991 const char *name, uint32_t layer_count,
1992 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1993 if (!layer_list)
1994 return false;
1995 for (uint32_t j = 0; j < layer_count; j++)
1996 if (!strcmp(name, layer_list[j]))
1997 return true;
1998 return false;
1999}
2000
2001/**
2002 * Searches through an array of layer names (ppp_layer_names) looking for a
2003 * layer key_name.
2004 * If not found then simply returns updating nothing.
2005 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12002006 * Any duplicate (pre-existing) expand_names in layer names are removed.
2007 * Order is otherwise preserved, with the layer key_name being replaced by the
2008 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07002009 * @param inst
2010 * @param layer_count
2011 * @param ppp_layer_names
2012 */
Mark Young0ad83132016-06-30 13:02:42 -06002013VkResult loader_expand_layer_names(
2014 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07002015 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06002016 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07002017
Jon Ashburncc407a22016-04-15 09:25:03 -06002018 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002019
Jon Ashburncc407a22016-04-15 09:25:03 -06002020 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06002021 (char const **)pp_src_layers)) {
2022 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06002023 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06002024 }
Jon Ashburn71483442016-02-11 18:59:43 -07002025
Jon Ashburn86a527a2016-02-10 20:59:26 -07002026 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2027 "Found meta layer %s, replacing with actual layer group",
2028 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12002029
Jon Ashburn491cd042016-05-16 14:01:18 -06002030 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06002031 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06002032 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2033 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002034 if (NULL == pp_dst_layers) {
2035 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2036 "alloc failed for dst layer array");
2037 return VK_ERROR_OUT_OF_HOST_MEMORY;
2038 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002039
2040 // copy layers from src to dst, stripping key_name and anything in
2041 // expand_names.
2042 uint32_t src_index, dst_index = 0;
2043 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002044 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
2045 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002046 continue;
2047 }
2048
2049 if (!strcmp(pp_src_layers[src_index], key_name)) {
2050 // insert all expand_names in place of key_name
2051 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06002052 for (expand_index = 0; expand_index < expand_count;
2053 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002054 pp_dst_layers[dst_index++] = expand_names[expand_index];
2055 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002056 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002057 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002058
2059 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002060 }
2061
Chris Forbesbd9de052016-04-06 20:49:02 +12002062 *ppp_layer_names = pp_dst_layers;
2063 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002064
2065 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002066}
2067
Chris Forbesbd9de052016-04-06 20:49:02 +12002068void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
2069 const VkInstanceCreateInfo *orig,
2070 VkInstanceCreateInfo *ours) {
2071 /* Free the layer names array iff we had to reallocate it */
2072 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002073 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002074 }
2075}
2076
Jon Ashburn491cd042016-05-16 14:01:18 -06002077void loader_init_std_validation_props(struct loader_layer_properties *props) {
2078 memset(props, 0, sizeof(struct loader_layer_properties));
2079 props->type = VK_LAYER_TYPE_META_EXPLICT;
2080 strncpy(props->info.description, "LunarG Standard Validation Layer",
Mark Young0153e0b2016-11-03 14:27:13 -06002081 sizeof(props->info.description));
Jon Ashburn491cd042016-05-16 14:01:18 -06002082 props->info.implementationVersion = 1;
2083 strncpy(props->info.layerName, std_validation_str,
Mark Young0153e0b2016-11-03 14:27:13 -06002084 sizeof(props->info.layerName));
Jon Ashburn491cd042016-05-16 14:01:18 -06002085 // TODO what about specVersion? for now insert loader's built version
2086 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2087}
2088
Jon Ashburn86a527a2016-02-10 20:59:26 -07002089/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002090 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002091 * the set of required layer names. If found then it adds a meta property to the
2092 * layer list.
2093 * Assumes the required layers are the same for both instance and device lists.
2094 * @param inst
2095 * @param layer_count number of layers in layer_names
2096 * @param layer_names array of required layer names
2097 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002098 */
2099static void loader_add_layer_property_meta(
2100 const struct loader_instance *inst, uint32_t layer_count,
2101 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002102 struct loader_layer_list *layer_instance_list) {
2103 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002104 bool found;
2105 struct loader_layer_list *layer_list;
2106
Jon Ashburn491cd042016-05-16 14:01:18 -06002107 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002108 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002109 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002110 return;
2111
Jon Ashburn491cd042016-05-16 14:01:18 -06002112 layer_list = layer_instance_list;
2113
2114 found = true;
2115 if (layer_list == NULL)
2116 return;
2117 for (i = 0; i < layer_count; i++) {
2118 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002119 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002120 found = false;
2121 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002122 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002123
2124 struct loader_layer_properties *props;
2125 if (found) {
2126 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002127 if (NULL == props) {
2128 // Error already triggered in loader_get_next_layer_property.
2129 return;
2130 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002131 loader_init_std_validation_props(props);
Jon Ashburn491cd042016-05-16 14:01:18 -06002132 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07002133}
2134
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002135static void loader_read_json_layer(
2136 const struct loader_instance *inst,
2137 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2138 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2139 char *temp;
2140 char *name, *type, *library_path, *api_version;
2141 char *implementation_version, *description;
2142 cJSON *ext_item;
2143 VkExtensionProperties ext_prop;
2144
Mark Young0ad83132016-06-30 13:02:42 -06002145/*
2146 * The following are required in the "layer" object:
2147 * (required) "name"
2148 * (required) "type"
2149 * (required) “library_path”
2150 * (required) “api_version”
2151 * (required) “implementation_version”
2152 * (required) “description”
2153 * (required for implicit layers) “disable_environment”
2154 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002155
Jon Ashburn23d36b12016-02-02 17:47:28 -07002156#define GET_JSON_OBJECT(node, var) \
2157 { \
2158 var = cJSON_GetObjectItem(node, #var); \
2159 if (var == NULL) { \
2160 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002161 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002162 "Didn't find required layer object %s in manifest " \
2163 "JSON file, skipping this layer", \
2164 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002165 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002166 } \
2167 }
2168#define GET_JSON_ITEM(node, var) \
2169 { \
2170 item = cJSON_GetObjectItem(node, #var); \
2171 if (item == NULL) { \
2172 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002173 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002174 "Didn't find required layer value %s in manifest JSON " \
2175 "file, skipping this layer", \
2176 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002177 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002178 } \
2179 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002180 if (temp == NULL) { \
2181 layer_node = layer_node->next; \
2182 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2183 "Problem accessing layer value %s in manifest JSON " \
2184 "file, skipping this layer", \
2185 #var); \
2186 return; \
2187 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002188 temp[strlen(temp) - 1] = '\0'; \
2189 var = loader_stack_alloc(strlen(temp) + 1); \
2190 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002191 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002192 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002193 GET_JSON_ITEM(layer_node, name)
2194 GET_JSON_ITEM(layer_node, type)
2195 GET_JSON_ITEM(layer_node, library_path)
2196 GET_JSON_ITEM(layer_node, api_version)
2197 GET_JSON_ITEM(layer_node, implementation_version)
2198 GET_JSON_ITEM(layer_node, description)
2199 if (is_implicit) {
2200 GET_JSON_OBJECT(layer_node, disable_environment)
2201 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002202#undef GET_JSON_ITEM
2203#undef GET_JSON_OBJECT
2204
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002205 // add list entry
2206 struct loader_layer_properties *props = NULL;
2207 if (!strcmp(type, "DEVICE")) {
2208 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2209 "Device layers are deprecated skipping this layer");
2210 layer_node = layer_node->next;
2211 return;
2212 }
2213 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2214 // layers that must work with older loaders
2215 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2216 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002217 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002218 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002219 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002220 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002221 if (NULL == props) {
2222 // Error already triggered in loader_get_next_layer_property.
2223 return;
2224 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002225 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2226 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2227 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002228
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002229 if (props == NULL) {
2230 layer_node = layer_node->next;
2231 return;
2232 }
Jon Ashburn15315172015-07-07 15:06:25 -06002233
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002234 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2235 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2236
2237 char *fullpath = props->lib_name;
2238 char *rel_base;
2239 if (loader_platform_is_path(library_path)) {
2240 // a relative or absolute path
2241 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2242 strcpy(name_copy, filename);
2243 rel_base = loader_platform_dirname(name_copy);
2244 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2245 } else {
2246 // a filename which is assumed in a system directory
2247 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2248 MAX_STRING_SIZE, fullpath);
2249 }
2250 props->info.specVersion = loader_make_version(api_version);
2251 props->info.implementationVersion = atoi(implementation_version);
2252 strncpy((char *)props->info.description, description,
2253 sizeof(props->info.description));
2254 props->info.description[sizeof(props->info.description) - 1] = '\0';
2255 if (is_implicit) {
2256 if (!disable_environment || !disable_environment->child) {
2257 loader_log(
2258 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2259 "Didn't find required layer child value disable_environment"
2260 "in manifest JSON file, skipping this layer");
2261 layer_node = layer_node->next;
2262 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002263 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002264 strncpy(props->disable_env_var.name, disable_environment->child->string,
2265 sizeof(props->disable_env_var.name));
2266 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2267 '\0';
2268 strncpy(props->disable_env_var.value,
2269 disable_environment->child->valuestring,
2270 sizeof(props->disable_env_var.value));
2271 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2272 '\0';
2273 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002274
Jon Ashburn23d36b12016-02-02 17:47:28 -07002275/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002276* Now get all optional items and objects and put in list:
2277* functions
2278* instance_extensions
2279* device_extensions
2280* enable_environment (implicit layers only)
2281*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002282#define GET_JSON_OBJECT(node, var) \
2283 { var = cJSON_GetObjectItem(node, #var); }
2284#define GET_JSON_ITEM(node, var) \
2285 { \
2286 item = cJSON_GetObjectItem(node, #var); \
2287 if (item != NULL) { \
2288 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002289 if (temp != NULL) { \
2290 temp[strlen(temp) - 1] = '\0'; \
2291 var = loader_stack_alloc(strlen(temp) + 1); \
2292 strcpy(var, &temp[1]); \
2293 cJSON_Free(temp); \
2294 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002295 } \
2296 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002297
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002298 cJSON *instance_extensions, *device_extensions, *functions,
2299 *enable_environment;
2300 cJSON *entrypoints;
2301 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2302 char **entry_array;
2303 vkGetInstanceProcAddr = NULL;
2304 vkGetDeviceProcAddr = NULL;
2305 spec_version = NULL;
2306 entrypoints = NULL;
2307 entry_array = NULL;
2308 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002309
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002310 /**
2311 * functions
2312 * vkGetInstanceProcAddr
2313 * vkGetDeviceProcAddr
2314 */
2315 GET_JSON_OBJECT(layer_node, functions)
2316 if (functions != NULL) {
2317 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2318 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2319 if (vkGetInstanceProcAddr != NULL)
2320 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2321 sizeof(props->functions.str_gipa));
2322 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2323 if (vkGetDeviceProcAddr != NULL)
2324 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2325 sizeof(props->functions.str_gdpa));
2326 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2327 }
2328 /**
2329 * instance_extensions
2330 * array of
2331 * name
2332 * spec_version
2333 */
2334 GET_JSON_OBJECT(layer_node, instance_extensions)
2335 if (instance_extensions != NULL) {
2336 int count = cJSON_GetArraySize(instance_extensions);
2337 for (i = 0; i < count; i++) {
2338 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2339 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002340 if (name != NULL) {
2341 strncpy(ext_prop.extensionName, name,
2342 sizeof(ext_prop.extensionName));
2343 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2344 '\0';
2345 }
Mark Young0ad83132016-06-30 13:02:42 -06002346 GET_JSON_ITEM(ext_item, spec_version)
2347 if (NULL != spec_version) {
2348 ext_prop.specVersion = atoi(spec_version);
2349 } else {
2350 ext_prop.specVersion = 0;
2351 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002352 bool ext_unsupported =
2353 wsi_unsupported_instance_extension(&ext_prop);
2354 if (!ext_unsupported) {
2355 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2356 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002357 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002358 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002359 }
2360 /**
2361 * device_extensions
2362 * array of
2363 * name
2364 * spec_version
2365 * entrypoints
2366 */
2367 GET_JSON_OBJECT(layer_node, device_extensions)
2368 if (device_extensions != NULL) {
2369 int count = cJSON_GetArraySize(device_extensions);
2370 for (i = 0; i < count; i++) {
2371 ext_item = cJSON_GetArrayItem(device_extensions, i);
2372 GET_JSON_ITEM(ext_item, name)
2373 GET_JSON_ITEM(ext_item, spec_version)
2374 if (name != NULL) {
2375 strncpy(ext_prop.extensionName, name,
2376 sizeof(ext_prop.extensionName));
2377 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2378 '\0';
2379 }
Mark Young0ad83132016-06-30 13:02:42 -06002380 if (NULL != spec_version) {
2381 ext_prop.specVersion = atoi(spec_version);
2382 } else {
2383 ext_prop.specVersion = 0;
2384 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002385 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2386 GET_JSON_OBJECT(ext_item, entrypoints)
2387 int entry_count;
2388 if (entrypoints == NULL) {
2389 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2390 &ext_prop, 0, NULL);
2391 continue;
2392 }
2393 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002394 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002395 entry_array =
2396 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002397 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002398 for (j = 0; j < entry_count; j++) {
2399 ext_item = cJSON_GetArrayItem(entrypoints, j);
2400 if (ext_item != NULL) {
2401 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002402 if (NULL == temp) {
2403 entry_array[j] = NULL;
2404 continue;
2405 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002406 temp[strlen(temp) - 1] = '\0';
2407 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2408 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002409 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002410 }
2411 }
2412 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2413 &ext_prop, entry_count, entry_array);
2414 }
2415 }
2416 if (is_implicit) {
2417 GET_JSON_OBJECT(layer_node, enable_environment)
2418
2419 // enable_environment is optional
2420 if (enable_environment) {
2421 strncpy(props->enable_env_var.name,
2422 enable_environment->child->string,
2423 sizeof(props->enable_env_var.name));
2424 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2425 '\0';
2426 strncpy(props->enable_env_var.value,
2427 enable_environment->child->valuestring,
2428 sizeof(props->enable_env_var.value));
2429 props->enable_env_var
2430 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2431 }
2432 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002433#undef GET_JSON_ITEM
2434#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002435}
2436
2437/**
2438 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2439 * file, add entry to the layer_list. Fill out the layer_properties in this list
2440 * entry from the input cJSON object.
2441 *
2442 * \returns
2443 * void
2444 * layer_list has a new entry and initialized accordingly.
2445 * If the json input object does not have all the required fields no entry
2446 * is added to the list.
2447 */
2448static void
2449loader_add_layer_properties(const struct loader_instance *inst,
2450 struct loader_layer_list *layer_instance_list,
2451 cJSON *json, bool is_implicit, char *filename) {
2452 /* Fields in layer manifest file that are required:
2453 * (required) “file_format_version”
2454 *
Mark Young0ad83132016-06-30 13:02:42 -06002455 * If more than one "layer" object are to be used, use the "layers" array
2456 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002457 *
2458 * First get all required items and if any missing abort
2459 */
2460
2461 cJSON *item, *layers_node, *layer_node;
2462 uint16_t file_major_vers = 0;
2463 uint16_t file_minor_vers = 0;
2464 uint16_t file_patch_vers = 0;
2465 char *vers_tok;
2466 cJSON *disable_environment = NULL;
2467 item = cJSON_GetObjectItem(json, "file_format_version");
2468 if (item == NULL) {
2469 return;
2470 }
2471 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002472 if (NULL == file_vers) {
2473 return;
2474 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002475 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2476 "Found manifest file %s, version %s", filename, file_vers);
2477 // Get the major/minor/and patch as integers for easier comparison
2478 vers_tok = strtok(file_vers, ".\"\n\r");
2479 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002480 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002481 vers_tok = strtok(NULL, ".\"\n\r");
2482 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002483 file_minor_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_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002487 }
2488 }
2489 }
2490 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2491 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2492 "%s Unexpected manifest file version (expected 1.0.0 or "
2493 "1.0.1), may cause errors",
2494 filename);
2495 }
Mark Young0ad83132016-06-30 13:02:42 -06002496 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002497 // If "layers" is present, read in the array of layer objects
2498 layers_node = cJSON_GetObjectItem(json, "layers");
2499 if (layers_node != NULL) {
2500 int numItems = cJSON_GetArraySize(layers_node);
2501 if (file_major_vers == 1 && file_minor_vers == 0 &&
2502 file_patch_vers == 0) {
2503 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2504 "\"layers\" tag not officially added until file version "
2505 "1.0.1, but %s is reporting version %s",
2506 filename, file_vers);
2507 }
2508 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2509 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2510 if (layer_node == NULL) {
2511 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2512 "Can't find \"layers\" array element %d object in "
2513 "manifest JSON file %s, skipping this file",
2514 curLayer, filename);
2515 return;
2516 }
2517 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2518 disable_environment, is_implicit, filename);
2519 }
2520 } else {
2521 // Otherwise, try to read in individual layers
2522 layer_node = cJSON_GetObjectItem(json, "layer");
2523 if (layer_node == NULL) {
2524 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2525 "Can't find \"layer\" object in manifest JSON file %s, "
2526 "skipping this file",
2527 filename);
2528 return;
2529 }
2530 // Loop through all "layer" objects in the file to get a count of them
2531 // first.
2532 uint16_t layer_count = 0;
2533 cJSON *tempNode = layer_node;
2534 do {
2535 tempNode = tempNode->next;
2536 layer_count++;
2537 } while (tempNode != NULL);
2538 /*
2539 * Throw a warning if we encounter multiple "layer" objects in file
2540 * versions newer than 1.0.0. Having multiple objects with the same
2541 * name at the same level is actually a JSON standard violation.
2542 */
2543 if (layer_count > 1 &&
2544 (file_major_vers > 1 ||
2545 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2546 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2547 "Multiple \"layer\" nodes are deprecated starting in "
2548 "file version \"1.0.1\". Please use \"layers\" : [] "
2549 "array instead in %s.",
2550 filename);
2551 } else {
2552 do {
2553 loader_read_json_layer(inst, layer_instance_list, layer_node,
2554 item, disable_environment, is_implicit,
2555 filename);
2556 layer_node = layer_node->next;
2557 } while (layer_node != NULL);
2558 }
2559 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002560 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002561}
2562
2563/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002564 * Find the Vulkan library manifest files.
2565 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002566 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002567 * for a list of JSON manifest files. If env_override is non-NULL
2568 * and has a valid value. Then the location is ignored. Otherwise
2569 * location is used to look for manifest files. The location
2570 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002571 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002572 * directory to look at. It is expanded into the dir path
2573 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2574 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002575 *
2576 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002577 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002578 * A string list of manifest files to be opened in out_files param.
2579 * List has a pointer to string for each manifest filename.
2580 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002581 * Location or override string lists can be either files or directories as
2582 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002583 * | location | override
2584 * --------------------------------
2585 * Win ICD | files | files
2586 * Win Layer | files | dirs
2587 * Linux ICD | dirs | files
2588 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002589 */
Mark Youngf8c20102016-11-07 16:26:17 -07002590static VkResult
2591loader_get_manifest_files(const struct loader_instance *inst,
2592 const char *env_override, char *source_override,
2593 bool is_layer, bool warn_if_not_present,
2594 const char *location, const char *home_location,
2595 struct loader_manifest_files *out_files) {
Mark Young0ad83132016-06-30 13:02:42 -06002596 char * override = NULL;
2597 char *loc, *orig_loc = NULL;
2598 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002599 char *file, *next_file, *name;
2600 size_t alloced_count = 64;
2601 char full_path[2048];
2602 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002603 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002604 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002605 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002606
2607 out_files->count = 0;
2608 out_files->filename_list = NULL;
2609
Jamie Madill00c3c912016-04-06 18:26:46 -04002610 if (source_override != NULL) {
2611 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002612 } else if (env_override != NULL &&
2613 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002614#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002615 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002616 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002617 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002618 override = NULL;
2619 }
2620#endif
2621 }
2622
Jon Ashburnb6822212016-02-16 15:34:16 -07002623#if !defined(_WIN32)
2624 if (location == NULL && home_location == NULL) {
2625#else
2626 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002627 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002628#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002629 loader_log(
2630 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002631 "Can't get manifest files with NULL location, env_override=%s",
2632 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002633 res = VK_ERROR_INITIALIZATION_FAILED;
2634 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002635 }
2636
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002637#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002638 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002639#else
2640 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002641#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002642 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002643 // Also handle getting the location(s) from registry on Windows
2644 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002645 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002646 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002647 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2648 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002649 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2650 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002651 }
2652 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002653#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002654 reg = loader_get_registry_files(inst, loc);
2655 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002656 if (!is_layer) {
2657 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2658 "Registry lookup failed can't get ICD manifest "
2659 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002660 // This typically only fails when out of memory, which is
2661 // critical
2662 // if this is for the loader.
2663 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002664 } else {
Mark Youngf8c20102016-11-07 16:26:17 -07002665 if (warn_if_not_present) {
2666 // warning only for layers
2667 loader_log(
2668 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2669 "Registry lookup failed can't get layer manifest files");
2670 }
Mark Young0ad83132016-06-30 13:02:42 -06002671 // Return success for now since it's not critical for layers
2672 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002673 }
Mark Young0ad83132016-06-30 13:02:42 -06002674 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002675 }
Mark Young0ad83132016-06-30 13:02:42 -06002676 orig_loc = loc;
2677 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002678#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002679 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002680 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002681 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002682 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2683 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002684 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2685 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002686 }
2687 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002688 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002689 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002690 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002691 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002692
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002693 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002694 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2695 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002696
Jon Ashburn2077e382015-06-29 11:25:34 -06002697 file = loc;
2698 while (*file) {
2699 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002700 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002701 sysdir = opendir(file);
2702 name = NULL;
2703 if (sysdir) {
2704 dent = readdir(sysdir);
2705 if (dent == NULL)
2706 break;
2707 name = &(dent->d_name[0]);
2708 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2709 name = full_path;
2710 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002711 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002712#if defined(_WIN32)
2713 name = file;
2714#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002715 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002716 char *dir;
2717 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002718 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002719 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002720 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2721 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002722 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002723 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002724 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002725
2726 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2727
2728 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002729#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002730 }
2731 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002732 /* Look for files ending with ".json" suffix */
2733 uint32_t nlen = (uint32_t)strlen(name);
2734 const char *suf = name + nlen - 5;
2735 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2736 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002737 out_files->filename_list = loader_instance_heap_alloc(
2738 inst, alloced_count * sizeof(char *),
2739 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002740 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002741 out_files->filename_list = loader_instance_heap_realloc(
2742 inst, out_files->filename_list,
2743 alloced_count * sizeof(char *),
2744 alloced_count * sizeof(char *) * 2,
2745 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002746 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002747 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002748 if (out_files->filename_list == NULL) {
2749 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2750 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002751 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2752 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002753 }
Mark Young0ad83132016-06-30 13:02:42 -06002754 out_files->filename_list[out_files->count] =
2755 loader_instance_heap_alloc(
2756 inst, strlen(name) + 1,
2757 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002758 if (out_files->filename_list[out_files->count] == NULL) {
2759 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2760 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002761 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2762 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002763 }
2764 strcpy(out_files->filename_list[out_files->count], name);
2765 out_files->count++;
2766 } else if (!list_is_dirs) {
2767 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002768 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002769 "Skipping manifest file %s, file name must end in .json",
2770 name);
2771 }
2772 if (list_is_dirs) {
2773 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002774 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002775 break;
Mark Young0ad83132016-06-30 13:02:42 -06002776 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002777 name = &(dent->d_name[0]);
2778 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2779 name = full_path;
2780 } else {
2781 break;
2782 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002783 }
Mark Young0ad83132016-06-30 13:02:42 -06002784 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002785 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002786 sysdir = NULL;
2787 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002788 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002789#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002790 if (home_location != NULL &&
2791 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002792 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2793 size_t len;
2794 if (xdgdatahome != NULL) {
2795
2796 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002797 strlen(home_location));
2798 if (home_loc == NULL) {
2799 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002800 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002801 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2802 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002803 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002804 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002805 // Add directory separator if needed
2806 if (home_location[0] != DIRECTORY_SYMBOL) {
2807 len = strlen(home_loc);
2808 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002809 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002810 }
2811 strcat(home_loc, home_location);
2812 file = home_loc;
2813 next_file = loader_get_next_path(file);
2814 home_location = NULL;
2815
Jon Ashburn1530c342016-02-26 13:14:27 -07002816 loader_log(
2817 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002818 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002819 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002820 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002821
2822 } else {
2823
2824 char *home = secure_getenv("HOME");
2825 if (home != NULL) {
2826 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2827 strlen(home_location));
2828 if (home_loc == NULL) {
2829 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young0153e0b2016-11-03 14:27:13 -06002830 "Out of memory can't get manifest files");
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002831 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2832 goto out;
2833 }
2834 strcpy(home_loc, home);
2835
2836 len = strlen(home);
2837 if (home[len] != DIRECTORY_SYMBOL) {
2838 home_loc[len] = DIRECTORY_SYMBOL;
2839 home_loc[len + 1] = '\0';
2840 }
2841 strcat(home_loc, ".local/share");
2842
2843 if (home_location[0] != DIRECTORY_SYMBOL) {
2844 len = strlen(home_loc);
2845 home_loc[len] = DIRECTORY_SYMBOL;
2846 home_loc[len + 1] = '\0';
2847 }
2848 strcat(home_loc, home_location);
2849 file = home_loc;
2850 next_file = loader_get_next_path(file);
2851 home_location = NULL;
2852
2853 loader_log(
2854 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2855 "Searching the following path for manifest files: %s\n",
2856 home_loc);
2857 list_is_dirs = true;
2858 } else {
2859 // without knowing HOME, we just.. give up
2860 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002861 }
2862 }
2863#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002864 }
Mark Young0ad83132016-06-30 13:02:42 -06002865
2866out:
2867 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2868 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2869 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2870 }
2871 loader_instance_heap_free(inst, out_files->filename_list);
2872 out_files->count = 0;
2873 out_files->filename_list = NULL;
2874 }
2875
2876 if (NULL != sysdir) {
2877 closedir(sysdir);
2878 }
2879
2880 if (NULL != reg && reg != orig_loc) {
2881 loader_instance_heap_free(inst, reg);
2882 }
2883 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002884}
2885
Jon Ashburn23d36b12016-02-02 17:47:28 -07002886void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002887
Jon Ashburn23d36b12016-02-02 17:47:28 -07002888void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002889/**
2890 * Try to find the Vulkan ICD driver(s).
2891 *
2892 * This function scans the default system loader path(s) or path
2893 * specified by the \c VK_ICD_FILENAMES environment variable in
2894 * order to find loadable VK ICDs manifest files. From these
2895 * manifest files it finds the ICD libraries.
2896 *
2897 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002898 * Vulkan result
2899 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002900 */
Mark Young0ad83132016-06-30 13:02:42 -06002901VkResult loader_icd_scan(const struct loader_instance *inst,
Mark Young0153e0b2016-11-03 14:27:13 -06002902 struct loader_icd_tramp_list *icd_tramp_list) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002903 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002904 uint16_t file_major_vers = 0;
2905 uint16_t file_minor_vers = 0;
2906 uint16_t file_patch_vers = 0;
2907 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002908 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002909 VkResult res = VK_SUCCESS;
2910 bool lockedMutex = false;
2911 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002912 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002913
Mark Young0ad83132016-06-30 13:02:42 -06002914 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2915
Mark Young0153e0b2016-11-03 14:27:13 -06002916 res = loader_scanned_icd_init(inst, icd_tramp_list);
Mark Young0ad83132016-06-30 13:02:42 -06002917 if (VK_SUCCESS != res) {
2918 goto out;
2919 }
2920
Jon Ashburn2077e382015-06-29 11:25:34 -06002921 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002922 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
Mark Youngf8c20102016-11-07 16:26:17 -07002923 true, DEFAULT_VK_DRIVERS_INFO,
Mark Young0ad83132016-06-30 13:02:42 -06002924 HOME_VK_DRIVERS_INFO, &manifest_files);
2925 if (VK_SUCCESS != res || manifest_files.count == 0) {
2926 goto out;
2927 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002928 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002929 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002930 for (uint32_t i = 0; i < manifest_files.count; i++) {
2931 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002932 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002933 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002934 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002935
Mark Young3a587792016-08-19 15:25:08 -06002936 res = loader_get_json(inst, file_str, &json);
2937 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2938 break;
2939 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002940 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002941 }
Mark Young3a587792016-08-19 15:25:08 -06002942
Jon Ashburn005617f2015-11-17 17:35:40 -07002943 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002944 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002945 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002946 if (num_good_icds == 0) {
2947 res = VK_ERROR_INITIALIZATION_FAILED;
2948 }
Derrick Owens62e16ef2016-09-09 15:49:07 -04002949 cJSON_Delete(json);
2950 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002951 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002952 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002953 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002954 if (NULL == file_vers) {
2955 // Only reason the print can fail is if there was an allocation
2956 // issue
Mark Young3a587792016-08-19 15:25:08 -06002957 if (num_good_icds == 0) {
2958 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2959 }
Derrick Owenscd92b8b2016-09-09 15:45:13 -04002960 cJSON_Delete(json);
2961 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002962 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002963 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002964 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2965 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002966 // Get the major/minor/and patch as integers for easier comparison
2967 vers_tok = strtok(file_vers, ".\"\n\r");
2968 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002969 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002970 vers_tok = strtok(NULL, ".\"\n\r");
2971 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002972 file_minor_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_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002976 }
2977 }
2978 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002979 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002980 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002981 "Unexpected manifest file version (expected 1.0.0 or "
2982 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002983 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002984 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002985 itemICD = cJSON_GetObjectItem(json, "ICD");
2986 if (itemICD != NULL) {
2987 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002988 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002989 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002990 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002991 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002992 "Can't find \"library_path\" in ICD JSON file "
2993 "%s, skipping",
2994 file_str);
Mark Young3a587792016-08-19 15:25:08 -06002995 if (num_good_icds == 0) {
2996 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2997 }
Mark Young0ad83132016-06-30 13:02:42 -06002998 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002999 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003000 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003001 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06003002 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003003 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06003004 temp[strlen(temp) - 1] = '\0';
3005 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06003006 if (NULL == library_path) {
3007 loader_log(
3008 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
3009 "Can't allocate space for \"library_path\" in ICD "
3010 "JSON file %s, skipping",
3011 file_str);
3012 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3013 cJSON_Free(temp);
3014 cJSON_Delete(json);
3015 json = NULL;
3016 goto out;
3017 }
Jon Ashburn86251302015-08-25 16:48:24 -06003018 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003019 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003020 if (strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003021 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003022 "Can't find \"library_path\" in ICD JSON file "
3023 "%s, skipping",
3024 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003025 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003026 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003027 continue;
3028 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003029 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003030 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07003031 loader_log(
3032 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04003033 "Searching for ICD drivers named %s default dir %s\n",
3034 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003035 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003036 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003037 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3038 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003039 strcpy(name_copy, file_str);
3040 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003041 loader_expand_path(library_path, rel_base, sizeof(fullpath),
3042 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003043 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003044 // a filename which is assumed in a system directory
3045 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
3046 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003047 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003048
3049 uint32_t vers = 0;
3050 item = cJSON_GetObjectItem(itemICD, "api_version");
3051 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003052 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003053 if (NULL == temp) {
3054 // Only reason the print can fail is if there was an
3055 // allocation issue
3056 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3057 goto out;
3058 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003059 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003060 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07003061 }
Mark Young0153e0b2016-11-03 14:27:13 -06003062 res = loader_scanned_icd_add(inst, icd_tramp_list, fullpath,
3063 vers);
Mark Young3a587792016-08-19 15:25:08 -06003064 if (VK_SUCCESS != res) {
3065 goto out;
3066 }
3067 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003068 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003069 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003070 "Can't find \"library_path\" object in ICD JSON "
3071 "file %s, skipping",
3072 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003073 }
3074 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003075 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003076 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003077 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
3078 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003079 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003080
Mark Young0ad83132016-06-30 13:02:42 -06003081 cJSON_Delete(json);
3082 json = NULL;
3083 }
3084
3085out:
3086 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003087 cJSON_Delete(json);
3088 }
Mark Young0ad83132016-06-30 13:02:42 -06003089 if (NULL != manifest_files.filename_list) {
3090 for (uint32_t i = 0; i < manifest_files.count; i++) {
3091 if (NULL != manifest_files.filename_list[i]) {
3092 loader_instance_heap_free(inst,
3093 manifest_files.filename_list[i]);
3094 }
3095 }
3096 loader_instance_heap_free(inst, manifest_files.filename_list);
3097 }
3098 if (lockedMutex) {
3099 loader_platform_thread_unlock_mutex(&loader_json_lock);
3100 }
3101 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003102}
3103
Jon Ashburn23d36b12016-02-02 17:47:28 -07003104void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003105 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003106 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003107 struct loader_manifest_files
3108 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003109 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003110 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003111 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003112
Mark Young0ad83132016-06-30 13:02:42 -06003113 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3114
3115 // Get a list of manifest files for explicit layers
3116 if (VK_SUCCESS !=
3117 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
Mark Youngf8c20102016-11-07 16:26:17 -07003118 true, true, DEFAULT_VK_ELAYERS_INFO,
Mark Young0ad83132016-06-30 13:02:42 -06003119 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3120 goto out;
3121 }
3122
3123 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003124 // Pass NULL for environment variable override - implicit layers are not
3125 // overridden by LAYERS_PATH_ENV
Mark Youngf8c20102016-11-07 16:26:17 -07003126 if (VK_SUCCESS != loader_get_manifest_files(inst, NULL, NULL, true, false,
3127 DEFAULT_VK_ILAYERS_INFO,
3128 HOME_VK_ILAYERS_INFO,
3129 &manifest_files[1])) {
Mark Young0ad83132016-06-30 13:02:42 -06003130 goto out;
3131 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003132
Mark Young0ad83132016-06-30 13:02:42 -06003133 // Make sure we have at least one layer, if not, go ahead and return
3134 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3135 goto out;
3136 }
3137
3138 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003139 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003140
Jon Ashburn6461ef22015-09-22 13:11:00 -06003141 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003142 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003143 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003144 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003145 file_str = manifest_files[implicit].filename_list[i];
3146 if (file_str == NULL)
3147 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003148
Jon Ashburn075ce432015-12-17 17:38:24 -07003149 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003150 VkResult res = loader_get_json(inst, file_str, &json);
3151 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3152 break;
3153 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003154 continue;
3155 }
3156
Jon Ashburn491cd042016-05-16 14:01:18 -06003157 loader_add_layer_properties(inst, instance_layers, json,
3158 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003159 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003160 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003161 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003162
3163 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003164 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3165 sizeof(std_validation_names[0]),
3166 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003167
Mark Young0ad83132016-06-30 13:02:42 -06003168out:
3169
3170 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3171 if (NULL != manifest_files[manFile].filename_list) {
3172 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3173 if (NULL != manifest_files[manFile].filename_list[i]) {
3174 loader_instance_heap_free(
3175 inst, manifest_files[manFile].filename_list[i]);
3176 }
3177 }
3178 loader_instance_heap_free(inst,
3179 manifest_files[manFile].filename_list);
3180 }
3181 }
3182 if (lockedMutex) {
3183 loader_platform_thread_unlock_mutex(&loader_json_lock);
3184 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003185}
3186
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003187void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003188 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003189 char *file_str;
3190 struct loader_manifest_files manifest_files;
3191 cJSON *json;
3192 uint32_t i;
3193
3194 // Pass NULL for environment variable override - implicit layers are not
3195 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003196 VkResult res = loader_get_manifest_files(
Mark Youngf8c20102016-11-07 16:26:17 -07003197 inst, NULL, NULL, true, false, DEFAULT_VK_ILAYERS_INFO,
3198 HOME_VK_ILAYERS_INFO, &manifest_files);
Mark Young0ad83132016-06-30 13:02:42 -06003199 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003200 return;
3201 }
3202
3203 /* cleanup any previously scanned libraries */
3204 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003205
3206 loader_platform_thread_lock_mutex(&loader_json_lock);
3207
3208 for (i = 0; i < manifest_files.count; i++) {
3209 file_str = manifest_files.filename_list[i];
3210 if (file_str == NULL) {
3211 continue;
3212 }
3213
3214 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003215 res = loader_get_json(inst, file_str, &json);
3216 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3217 break;
3218 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003219 continue;
3220 }
3221
Mark Young0ad83132016-06-30 13:02:42 -06003222 loader_add_layer_properties(inst, instance_layers, json, true,
3223 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003224
Mark Young0ad83132016-06-30 13:02:42 -06003225 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003226 cJSON_Delete(json);
3227 }
Mark Young0ad83132016-06-30 13:02:42 -06003228 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003229
3230 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003231 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3232 sizeof(std_validation_names[0]),
3233 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003234
3235 loader_platform_thread_unlock_mutex(&loader_json_lock);
3236}
3237
Jon Ashburn23d36b12016-02-02 17:47:28 -07003238static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3239loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003240 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003241 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003242 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003243 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003244 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003245 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003246
Jon Ashburn27cd5842015-05-12 17:26:48 -06003247 // inst is not wrapped
3248 if (inst == VK_NULL_HANDLE) {
3249 return NULL;
3250 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003251 VkLayerInstanceDispatchTable *disp_table =
3252 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003253 void *addr;
3254
3255 if (disp_table == NULL)
3256 return NULL;
3257
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003258 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003259 addr =
3260 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003261 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003262 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003263 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003264
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003265 // Don't call down the chain, this would be an infinite loop
3266 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003267 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003268 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003269}
3270
Mark Younga7c51fd2016-09-16 10:18:42 -06003271void loader_override_terminating_device_proc(
3272 VkDevice device, struct loader_dev_dispatch_table *disp_table) {
3273 struct loader_device *dev;
Mark Young0153e0b2016-11-03 14:27:13 -06003274 struct loader_icd_term *icd_term =
3275 loader_get_icd_and_device(device, &dev, NULL);
Mark Younga7c51fd2016-09-16 10:18:42 -06003276
3277 // Certain device entry-points still need to go through a terminator before
3278 // hitting the ICD. This could be for several reasons, but the main one
3279 // is currently unwrapping an object before passing the appropriate info
3280 // along to the ICD.
3281 if ((PFN_vkVoidFunction)disp_table->core_dispatch.CreateSwapchainKHR ==
Mark Young0153e0b2016-11-03 14:27:13 -06003282 (PFN_vkVoidFunction)icd_term->GetDeviceProcAddr(
3283 device, "vkCreateSwapchainKHR")) {
Mark Younga7c51fd2016-09-16 10:18:42 -06003284 disp_table->core_dispatch.CreateSwapchainKHR =
3285 terminator_vkCreateSwapchainKHR;
3286 }
3287}
Mark Young16573c72016-06-28 10:52:43 -06003288
Piers Daniellf9262be2016-09-14 11:24:36 -06003289VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003290loader_gpa_device_internal(VkDevice device, const char *pName) {
3291 struct loader_device *dev;
Mark Young0153e0b2016-11-03 14:27:13 -06003292 struct loader_icd_term *icd_term =
3293 loader_get_icd_and_device(device, &dev, NULL);
Mark Young16573c72016-06-28 10:52:43 -06003294
3295 // Certain device entry-points still need to go through a terminator before
3296 // hitting the ICD. This could be for several reasons, but the main one
3297 // is currently unwrapping an object before passing the appropriate info
3298 // along to the ICD.
3299 if (!strcmp(pName, "vkCreateSwapchainKHR")) {
3300 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
3301 }
3302
Mark Young0153e0b2016-11-03 14:27:13 -06003303 return icd_term->GetDeviceProcAddr(device, pName);
Piers Daniell295fe402016-03-29 11:51:11 -06003304}
3305
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003306/**
3307 * Initialize device_ext dispatch table entry as follows:
3308 * If dev == NULL find all logical devices created within this instance and
3309 * init the entry (given by idx) in the ext dispatch table.
3310 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003311 * The initialization value is gotten by calling down the device chain with
3312 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003313 * If GDPA returns NULL then don't initialize the dispatch table entry.
3314 */
3315static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003316 struct loader_device *dev,
3317 uint32_t idx,
3318 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003319
Jon Ashburn23d36b12016-02-02 17:47:28 -07003320{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003321 void *gdpa_value;
3322 if (dev != NULL) {
3323 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003324 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003325 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003326 dev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003327 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003328 } else {
Mark Young0153e0b2016-11-03 14:27:13 -06003329 for (struct loader_icd_term *icd_term = inst->icd_terms;
3330 icd_term != NULL; icd_term = icd_term->next) {
3331 struct loader_device *ldev = icd_term->logical_device_list;
Karl Schultz2558bd32016-02-24 14:39:39 -07003332 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003333 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003334 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3335 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003336 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003337 ldev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003338 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003339 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003340 }
3341 }
3342 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003343}
3344
3345/**
3346 * Find all dev extension in the hash table and initialize the dispatch table
3347 * for dev for each of those extension entrypoints found in hash table.
3348
3349 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003350void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3351 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003352 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3353 if (inst->disp_hash[i].func_name != NULL)
3354 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3355 inst->disp_hash[i].func_name);
3356 }
3357}
3358
3359static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003360 const char *funcName) {
Mark Young0153e0b2016-11-03 14:27:13 -06003361 struct loader_icd_term *icd_term;
3362 icd_term = inst->icd_terms;
3363 while (NULL != icd_term) {
3364 if (icd_term->scanned_icd->GetInstanceProcAddr(icd_term->instance,
3365 funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003366 // this icd supports funcName
3367 return true;
Mark Young0153e0b2016-11-03 14:27:13 -06003368 icd_term = icd_term->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003369 }
3370
3371 return false;
3372}
3373
Jon Ashburncc407a22016-04-15 09:25:03 -06003374static bool loader_check_layer_list_for_address(
3375 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003376 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003377 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003378 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003379 const struct loader_device_extension_list *const extensions =
3380 &(layers->list[layer].device_extension_list);
3381 for (uint32_t extension = 0; extension < extensions->count;
3382 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003383 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003384 const struct loader_dev_ext_props *const property =
3385 &(extensions->list[extension]);
3386 for (uint32_t entry = 0; entry < property->entrypoint_count;
3387 ++entry) {
3388 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003389 return true;
3390 }
3391 }
3392 }
3393 }
3394
3395 return false;
3396}
3397
Jon Ashburn23d36b12016-02-02 17:47:28 -07003398static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003399 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003400 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3401 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003402 }
3403 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3404}
3405
3406static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003407 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003408 uint32_t i;
3409 uint32_t idx = *ptr_idx;
3410 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3411
3412 if (!inst->disp_hash[idx].func_name) {
3413 // no entry here at this idx, so use it
3414 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003415 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003416 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003417 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003418 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003419 "loader_add_dev_ext_table() can't allocate memory for "
3420 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003421 return false;
3422 }
3423 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3424 return true;
3425 }
3426
3427 // check for enough capacity
3428 if (list->capacity == 0) {
Mark Young0153e0b2016-11-03 14:27:13 -06003429 list->index =
3430 loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
3431 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003432 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003433 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003434 "loader_add_dev_ext_table() can't allocate list memory");
3435 return false;
3436 }
3437 list->capacity = 8 * sizeof(*(list->index));
3438 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0153e0b2016-11-03 14:27:13 -06003439 list->index = loader_instance_heap_realloc(
3440 inst, list->index, list->capacity, list->capacity * 2,
3441 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003442 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003443 loader_log(
3444 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3445 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003446 return false;
3447 }
3448 list->capacity *= 2;
3449 }
3450
Jon Ashburn23d36b12016-02-02 17:47:28 -07003451 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003452 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3453 do {
3454 if (!inst->disp_hash[i].func_name) {
3455 assert(inst->disp_hash[i].list.capacity == 0);
Mark Young0153e0b2016-11-03 14:27:13 -06003456 inst->disp_hash[i].func_name = (char *)loader_instance_heap_alloc(
3457 inst, strlen(funcName) + 1,
3458 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003459 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003460 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003461 "loader_add_dev_ext_table() can't rallocate "
3462 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003463 return false;
3464 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003465 strncpy(inst->disp_hash[i].func_name, funcName,
3466 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003467 list->index[list->count] = i;
3468 list->count++;
3469 *ptr_idx = i;
3470 return true;
3471 }
3472 i = (i + 1) % MAX_NUM_DEV_EXTS;
3473 } while (i != idx);
3474
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003475 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003476 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3477 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003478 return false;
3479}
3480
3481static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003482 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003483 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003484 if (inst->disp_hash[*idx].func_name &&
3485 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003486 return true;
3487
3488 // funcName wasn't at the primary spot in the hash table
3489 // search the list of secondary locations (shallow search, not deep search)
3490 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3491 alt_idx = inst->disp_hash[*idx].list.index[i];
3492 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3493 *idx = alt_idx;
3494 return true;
3495 }
3496 }
3497
3498 return false;
3499}
3500
3501/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003502 * This function returns generic trampoline code address for unknown entry
3503 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003504 * Presumably, these unknown entry points (as given by funcName) are device
3505 * extension entrypoints. A hash table is used to keep a list of unknown entry
3506 * points and their mapping to the device extension dispatch table
3507 * (struct loader_dev_ext_dispatch_table).
3508 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003509 * For a given entry point string (funcName), if an existing mapping is found
3510 * the
3511 * trampoline address for that mapping is returned. Otherwise, this unknown
3512 * entry point
3513 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3514 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003515 * new entry in the hash table is initialized and that trampoline address for
3516 * the new entry is returned. Null is returned if the hash table is full or
3517 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3518 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003519void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003520 uint32_t idx;
3521 uint32_t seed = 0;
3522
3523 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3524
3525 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3526 // found funcName already in hash
3527 return loader_get_dev_ext_trampoline(idx);
3528
3529 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003530 if (!loader_check_icds_for_address(inst, funcName) &&
Mark Young0153e0b2016-11-03 14:27:13 -06003531 !loader_check_layer_list_for_address(&inst->instance_layer_list,
3532 funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003533 // if support found in layers continue on
3534 return NULL;
3535 }
3536
3537 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3538 // successfully added new table entry
3539 // init any dev dispatch table entrys as needed
3540 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3541 return loader_get_dev_ext_trampoline(idx);
3542 }
3543
3544 return NULL;
3545}
3546
Jon Ashburn23d36b12016-02-02 17:47:28 -07003547struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003548 /* look up the loader_instance in our list by comparing dispatch tables, as
3549 * there is no guarantee the instance is still a loader_instance* after any
3550 * layers which wrap the instance object.
3551 */
3552 const VkLayerInstanceDispatchTable *disp;
3553 struct loader_instance *ptr_instance = NULL;
3554 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003555 for (struct loader_instance *inst = loader.instances; inst;
3556 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003557 if (inst->disp == disp) {
3558 ptr_instance = inst;
3559 break;
3560 }
3561 }
3562 return ptr_instance;
3563}
3564
Jon Ashburn23d36b12016-02-02 17:47:28 -07003565static loader_platform_dl_handle
Mark Young0153e0b2016-11-03 14:27:13 -06003566loader_open_layer_lib(const struct loader_instance *inst,
3567 const char *chain_type,
3568 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003569
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003570 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003571 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003572 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003573 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003574 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003575 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003576 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003577 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003578 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003579
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003580 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003581}
3582
Mark Young0153e0b2016-11-03 14:27:13 -06003583static void loader_close_layer_lib(const struct loader_instance *inst,
3584 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003585
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003586 if (prop->lib_handle) {
3587 loader_platform_close_library(prop->lib_handle);
3588 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3589 "Unloading layer library %s", prop->lib_name);
3590 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003591 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003592}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003593
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003594void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003595 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003596 struct loader_layer_list *list) {
3597 /* delete instance list of enabled layers and close any layer libraries */
3598 for (uint32_t i = 0; i < list->count; i++) {
3599 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003600
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003601 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003602 }
Mark Young0ad83132016-06-30 13:02:42 -06003603 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003604}
3605
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003606/**
3607 * Go through the search_list and find any layers which match type. If layer
3608 * type match is found in then add it to ext_list.
3609 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003610static void
3611loader_add_layer_implicit(const struct loader_instance *inst,
3612 const enum layer_type type,
3613 struct loader_layer_list *list,
3614 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003615 bool enable;
3616 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003617 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003618 for (i = 0; i < search_list->count; i++) {
3619 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003620 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003621 /* Found an implicit layer, see if it should be enabled */
3622 enable = false;
3623
Jon Ashburn23d36b12016-02-02 17:47:28 -07003624 // if no enable_environment variable is specified, this implicit
3625 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003626 // should always be enabled. Otherwise check if the variable is set
3627 if (prop->enable_env_var.name[0] == 0) {
3628 enable = true;
3629 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003630 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003631 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3632 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003633 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003634 }
3635
3636 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003637 // environment variables are set, the layer is disabled. Implicit
3638 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003639 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003640 env_value = loader_getenv(prop->disable_env_var.name, inst);
3641 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003642 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003643 }
3644 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003645
Mark Young0ad83132016-06-30 13:02:42 -06003646 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003647 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003648 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003649 }
3650 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003651}
3652
3653/**
3654 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003655 * is found in search_list then add it to layer_list. But only add it to
3656 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003657 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003658static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003659 const enum layer_type type,
3660 const char *env_name,
3661 struct loader_layer_list *layer_list,
3662 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003663 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003664 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003665
Mark Young0ad83132016-06-30 13:02:42 -06003666 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003667 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003668 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003669 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003670 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003671 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003672 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003673 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003674 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003675
Mark Young0ad83132016-06-30 13:02:42 -06003676 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003677
Jon Ashburn23d36b12016-02-02 17:47:28 -07003678 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003679 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003680 if (!strcmp(std_validation_str, name)) {
3681 /* add meta list of layers
3682 don't attempt to remove duplicate layers already added by app or
3683 env var
3684 */
3685 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3686 "Expanding meta layer %s found in environment variable",
3687 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003688 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3689 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003690 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3691 sizeof(std_validation_names[0]);
3692 i++) {
3693 loader_find_layer_name_add_list(inst, std_validation_names[i],
3694 type, search_list, layer_list);
3695 }
3696 } else {
3697 loader_find_layer_name_add_list(inst, name, type, search_list,
3698 layer_list);
3699 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003700 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003701 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003702
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003703 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003704}
3705
Jon Ashburn23d36b12016-02-02 17:47:28 -07003706VkResult
3707loader_enable_instance_layers(struct loader_instance *inst,
3708 const VkInstanceCreateInfo *pCreateInfo,
3709 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003710 VkResult err;
3711
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003712 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003713
Jon Ashburne39a4f82015-08-28 13:38:21 -06003714 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003715 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3716 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003717 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003718 }
3719
Jon Ashburn0c26e712015-07-02 16:10:32 -06003720 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003721 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3722 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003723
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003724 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003725 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3726 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003727 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003728
3729 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003730 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003731 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3732 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003733
3734 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003735}
3736
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003737/*
3738 * Given the list of layers to activate in the loader_instance
3739 * structure. This function will add a VkLayerInstanceCreateInfo
3740 * structure to the VkInstanceCreateInfo.pNext pointer.
3741 * Each activated layer will have it's own VkLayerInstanceLink
3742 * structure that tells the layer what Get*ProcAddr to call to
3743 * get function pointers to the next layer down.
3744 * Once the chain info has been created this function will
3745 * execute the CreateInstance call chain. Each layer will
3746 * then have an opportunity in it's CreateInstance function
3747 * to setup it's dispatch table when the lower layer returns
3748 * successfully.
3749 * Each layer can wrap or not-wrap the returned VkInstance object
3750 * as it sees fit.
3751 * The instance chain is terminated by a loader function
3752 * that will call CreateInstance on all available ICD's and
3753 * cache those VkInstance objects for future use.
3754 */
3755VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003756 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003757 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003758 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003759 uint32_t activated_layers = 0;
3760 VkLayerInstanceCreateInfo chain_info;
3761 VkLayerInstanceLink *layer_instance_link_info = NULL;
3762 VkInstanceCreateInfo loader_create_info;
3763 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003764
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003765 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3766 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003767
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003768 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003769
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003770 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003771
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003772 chain_info.u.pLayerInfo = NULL;
3773 chain_info.pNext = pCreateInfo->pNext;
3774 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3775 chain_info.function = VK_LAYER_LINK_INFO;
3776 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003777
Jon Ashburn23d36b12016-02-02 17:47:28 -07003778 layer_instance_link_info = loader_stack_alloc(
3779 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003780 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003781 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3782 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003783 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003784 }
3785
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003786 /* Create instance chain of enabled layers */
3787 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003788 struct loader_layer_properties *layer_prop =
3789 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003790 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003791
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003792 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003793 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003794 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003795 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3796 NULL) {
3797 if (layer_prop->functions.str_gipa == NULL ||
3798 strlen(layer_prop->functions.str_gipa) == 0) {
3799 fpGIPA = (PFN_vkGetInstanceProcAddr)
3800 loader_platform_get_proc_address(
3801 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003802 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3803 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003804 fpGIPA = (PFN_vkGetInstanceProcAddr)
3805 loader_platform_get_proc_address(
3806 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003807 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003808 loader_log(
3809 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3810 "Failed to find vkGetInstanceProcAddr in layer %s",
3811 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003812 continue;
3813 }
3814 }
3815
Jon Ashburn23d36b12016-02-02 17:47:28 -07003816 layer_instance_link_info[activated_layers].pNext =
3817 chain_info.u.pLayerInfo;
3818 layer_instance_link_info[activated_layers]
3819 .pfnNextGetInstanceProcAddr = nextGIPA;
3820 chain_info.u.pLayerInfo =
3821 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003822 nextGIPA = fpGIPA;
3823
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003824 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003825 "Insert instance layer %s (%s)",
3826 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003827
3828 activated_layers++;
3829 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003830 }
3831
Jon Ashburn23d36b12016-02-02 17:47:28 -07003832 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003833 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003834 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003835 VkLayerInstanceCreateInfo create_info_disp;
3836
Jon Ashburncc407a22016-04-15 09:25:03 -06003837 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003838 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003839
3840 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3841
3842 create_info_disp.pNext = loader_create_info.pNext;
3843 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003844 res =
3845 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003846 } else {
3847 // Couldn't find CreateInstance function!
3848 res = VK_ERROR_INITIALIZATION_FAILED;
3849 }
3850
3851 if (res != VK_SUCCESS) {
3852 // TODO: Need to clean up here
3853 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003854 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003855 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003856 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003857 }
3858
3859 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003860}
3861
Jon Ashburn23d36b12016-02-02 17:47:28 -07003862void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3863 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003864
Jon Ashburn23d36b12016-02-02 17:47:28 -07003865 loader_init_instance_extension_dispatch_table(
3866 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003867}
3868
Jon Ashburn1530c342016-02-26 13:14:27 -07003869VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003870loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3871 const VkDeviceCreateInfo *pCreateInfo,
3872 const VkAllocationCallbacks *pAllocator,
3873 const struct loader_instance *inst,
3874 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003875 uint32_t activated_layers = 0;
3876 VkLayerDeviceLink *layer_device_link_info;
3877 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003878 VkDeviceCreateInfo loader_create_info;
3879 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003880
Piers Daniell295fe402016-03-29 11:51:11 -06003881 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003882 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003883
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003884 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3885
Jon Ashburn23d36b12016-02-02 17:47:28 -07003886 layer_device_link_info = loader_stack_alloc(
3887 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003888 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003889 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3890 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003891 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003892 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003893
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003894 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003895 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3896 chain_info.function = VK_LAYER_LINK_INFO;
3897 chain_info.u.pLayerInfo = NULL;
3898 chain_info.pNext = pCreateInfo->pNext;
3899 loader_create_info.pNext = &chain_info;
3900
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003901 /* Create instance chain of enabled layers */
3902 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003903 struct loader_layer_properties *layer_prop =
3904 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003905 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003906
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003907 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003908 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003909 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003910 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3911 NULL) {
3912 if (layer_prop->functions.str_gipa == NULL ||
3913 strlen(layer_prop->functions.str_gipa) == 0) {
3914 fpGIPA = (PFN_vkGetInstanceProcAddr)
3915 loader_platform_get_proc_address(
3916 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003917 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3918 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003919 fpGIPA = (PFN_vkGetInstanceProcAddr)
3920 loader_platform_get_proc_address(
3921 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003922 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003923 loader_log(
3924 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3925 "Failed to find vkGetInstanceProcAddr in layer %s",
3926 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003927 continue;
3928 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003929 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003930 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003931 if (layer_prop->functions.str_gdpa == NULL ||
3932 strlen(layer_prop->functions.str_gdpa) == 0) {
3933 fpGDPA = (PFN_vkGetDeviceProcAddr)
3934 loader_platform_get_proc_address(lib_handle,
3935 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003936 layer_prop->functions.get_device_proc_addr = fpGDPA;
3937 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003938 fpGDPA = (PFN_vkGetDeviceProcAddr)
3939 loader_platform_get_proc_address(
3940 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003941 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003942 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003943 "Failed to find vkGetDeviceProcAddr in layer %s",
3944 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003945 continue;
3946 }
3947 }
3948
Jon Ashburn23d36b12016-02-02 17:47:28 -07003949 layer_device_link_info[activated_layers].pNext =
3950 chain_info.u.pLayerInfo;
3951 layer_device_link_info[activated_layers]
3952 .pfnNextGetInstanceProcAddr = nextGIPA;
3953 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3954 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003955 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3956 nextGIPA = fpGIPA;
3957 nextGDPA = fpGDPA;
3958
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003959 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003960 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003961 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003962
3963 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003964 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003965 }
3966
Jon Ashburncc407a22016-04-15 09:25:03 -06003967 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003968 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003969 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003970 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003971 VkLayerDeviceCreateInfo create_info_disp;
3972
Jon Ashburncc407a22016-04-15 09:25:03 -06003973 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003974 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3975
3976 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3977
3978 create_info_disp.pNext = loader_create_info.pNext;
3979 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003980 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003981 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003982 if (res != VK_SUCCESS) {
3983 return res;
3984 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003985 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003986 } else {
3987 // Couldn't find CreateDevice function!
3988 return VK_ERROR_INITIALIZATION_FAILED;
3989 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003990
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003991 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003992 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003993 dev->device);
3994
3995 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003996}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003997
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003998VkResult loader_validate_layers(const struct loader_instance *inst,
3999 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004000 const char *const *ppEnabledLayerNames,
4001 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004002 struct loader_layer_properties *prop;
4003
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004004 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004005 VkStringErrorFlags result =
4006 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004007 if (result != VK_STRING_ERROR_NONE) {
4008 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004009 "Loader: Device ppEnabledLayerNames contains string "
4010 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004011 return VK_ERROR_LAYER_NOT_PRESENT;
4012 }
4013
Jon Ashburn23d36b12016-02-02 17:47:28 -07004014 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004015 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004016 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004017 }
4018 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004019 return VK_SUCCESS;
4020}
4021
4022VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004023 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004024 const struct loader_extension_list *icd_exts,
4025 const struct loader_layer_list *instance_layer,
4026 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004027
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004028 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004029 struct loader_layer_properties *layer_prop;
4030
Jon Ashburnf19916e2016-01-11 13:12:43 -07004031 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004032 VkStringErrorFlags result = vk_string_validate(
4033 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004034 if (result != VK_STRING_ERROR_NONE) {
4035 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004036 "Loader: Instance ppEnabledExtensionNames contains "
4037 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004038 return VK_ERROR_EXTENSION_NOT_PRESENT;
4039 }
4040
Jon Ashburn23d36b12016-02-02 17:47:28 -07004041 extension_prop = get_extension_property(
4042 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004043
4044 if (extension_prop) {
4045 continue;
4046 }
4047
4048 extension_prop = NULL;
4049
4050 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004051 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004052 layer_prop = loader_get_layer_property(
Jan-Harald Fredriksene812a642016-09-28 12:10:24 +02004053 pCreateInfo->ppEnabledLayerNames[j], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004054 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004055 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004056 * should have already filtered this case out.
4057 */
4058 continue;
4059 }
4060
Jon Ashburn23d36b12016-02-02 17:47:28 -07004061 extension_prop =
4062 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
4063 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004064 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004065 /* Found the extension in one of the layers enabled by the app.
4066 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004067 break;
4068 }
4069 }
4070
4071 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004072 /* Didn't find extension name in any of the global layers, error out
4073 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004074 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004075 }
4076 }
4077 return VK_SUCCESS;
4078}
4079
4080VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06004081 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004082 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07004083 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004084 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004085 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004086 struct loader_layer_properties *layer_prop;
4087
Jon Ashburnf19916e2016-01-11 13:12:43 -07004088 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004089
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004090 VkStringErrorFlags result = vk_string_validate(
4091 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004092 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06004093 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4094 0, "Loader: Device ppEnabledExtensionNames contains "
4095 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004096 return VK_ERROR_EXTENSION_NOT_PRESENT;
4097 }
4098
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004099 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004100 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004101
4102 if (extension_prop) {
4103 continue;
4104 }
4105
Jon Ashburn471f44c2016-01-13 12:51:43 -07004106 /* Not in global list, search activated layer extension lists */
4107 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4108 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004109
Jon Ashburn23d36b12016-02-02 17:47:28 -07004110 extension_prop = get_dev_extension_property(
4111 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004112 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004113 /* Found the extension in one of the layers enabled by the app.
4114 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004115 break;
4116 }
4117 }
4118
4119 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004120 /* Didn't find extension name in any of the device layers, error out
4121 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004122 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004123 }
4124 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004125 return VK_SUCCESS;
4126}
4127
Jon Ashburn1530c342016-02-26 13:14:27 -07004128/**
4129 * Terminator functions for the Instance chain
4130 * All named terminator_<Vulakn API name>
4131 */
Mark Young0ad83132016-06-30 13:02:42 -06004132VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
4133 const VkInstanceCreateInfo *pCreateInfo,
4134 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Mark Young0153e0b2016-11-03 14:27:13 -06004135 struct loader_icd_term *icd_term;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004136 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004137 char **filtered_extension_names = NULL;
4138 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004139 VkResult res = VK_SUCCESS;
Mark Young8b4edb52016-11-11 09:31:55 -07004140 bool one_icd_successful = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004141
Jon Ashburncc407a22016-04-15 09:25:03 -06004142 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004143 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4144
Jon Ashburnf19916e2016-01-11 13:12:43 -07004145 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004146 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004147
4148 /*
4149 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004150 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004151 * No ICD will advertise support for layers. An ICD
4152 * library could support a layer, but it would be
4153 * independent of the actual ICD, just in the same library.
4154 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004155 filtered_extension_names =
4156 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004157 if (!filtered_extension_names) {
Mark Young3a587792016-08-19 15:25:08 -06004158 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4159 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004160 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004161 icd_create_info.ppEnabledExtensionNames =
4162 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004163
Mark Young0153e0b2016-11-03 14:27:13 -06004164 for (uint32_t i = 0; i < ptr_instance->icd_tramp_list.count; i++) {
4165 icd_term = loader_icd_add(
4166 ptr_instance, &ptr_instance->icd_tramp_list.scanned_list[i]);
4167 if (NULL == icd_term) {
Mark Young3a587792016-08-19 15:25:08 -06004168 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4169 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004170 }
4171 icd_create_info.enabledExtensionCount = 0;
4172 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004173
Mark Young0ad83132016-06-30 13:02:42 -06004174 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4175 "Build ICD instance extension list");
4176 // traverse scanned icd list adding non-duplicate extensions to the
4177 // list
Mark Young3a587792016-08-19 15:25:08 -06004178 res = loader_init_generic_list(ptr_instance,
Mark Young0153e0b2016-11-03 14:27:13 -06004179 (struct loader_generic_list *)&icd_exts,
4180 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004181 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4182 // If out of memory, bail immediately.
4183 goto out;
4184 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004185 // Something bad happened with this ICD, so free it and try the
4186 // next.
Mark Young0153e0b2016-11-03 14:27:13 -06004187 ptr_instance->icd_terms = icd_term->next;
4188 icd_term->next = NULL;
4189 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004190 continue;
4191 }
4192
4193 res = loader_add_instance_extensions(
Mark Young0ad83132016-06-30 13:02:42 -06004194 ptr_instance,
Mark Young0153e0b2016-11-03 14:27:13 -06004195 icd_term->scanned_icd->EnumerateInstanceExtensionProperties,
4196 icd_term->scanned_icd->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004197 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004198 loader_destroy_generic_list(
4199 ptr_instance, (struct loader_generic_list *)&icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004200 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4201 // If out of memory, bail immediately.
4202 goto out;
4203 } else {
4204 // Something bad happened with this ICD, so free it and try
4205 // the next.
Mark Young0153e0b2016-11-03 14:27:13 -06004206 ptr_instance->icd_terms = icd_term->next;
4207 icd_term->next = NULL;
4208 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004209 continue;
4210 }
Mark Young3a587792016-08-19 15:25:08 -06004211 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004212
Mark Young0ad83132016-06-30 13:02:42 -06004213 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4214 prop = get_extension_property(
4215 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4216 if (prop) {
4217 filtered_extension_names[icd_create_info
4218 .enabledExtensionCount] =
4219 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4220 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004221 }
4222 }
Mark Young0ad83132016-06-30 13:02:42 -06004223
4224 loader_destroy_generic_list(ptr_instance,
4225 (struct loader_generic_list *)&icd_exts);
4226
Mark Young8b4edb52016-11-11 09:31:55 -07004227 VkResult icd_result =
4228 ptr_instance->icd_tramp_list.scanned_list[i].CreateInstance(
4229 &icd_create_info, pAllocator, &(icd_term->instance));
4230 if (VK_ERROR_OUT_OF_HOST_MEMORY == icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004231 // If out of memory, bail immediately.
Mark Young8b4edb52016-11-11 09:31:55 -07004232 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young3a587792016-08-19 15:25:08 -06004233 goto out;
Mark Young8b4edb52016-11-11 09:31:55 -07004234 } else if (VK_SUCCESS != icd_result) {
Mark Young3a587792016-08-19 15:25:08 -06004235 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4236 "ICD ignored: failed to CreateInstance in ICD %d", i);
Mark Young0153e0b2016-11-03 14:27:13 -06004237 ptr_instance->icd_terms = icd_term->next;
4238 icd_term->next = NULL;
4239 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004240 continue;
4241 }
Mark Young0ad83132016-06-30 13:02:42 -06004242
Mark Young0153e0b2016-11-03 14:27:13 -06004243 if (!loader_icd_init_entrys(icd_term, icd_term->instance,
4244 ptr_instance->icd_tramp_list.scanned_list[i]
4245 .GetInstanceProcAddr)) {
Mark Young3a587792016-08-19 15:25:08 -06004246 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06004247 "ICD ignored: failed to CreateInstance and find "
4248 "entrypoints with ICD");
Mark Young3a587792016-08-19 15:25:08 -06004249 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004250 }
Mark Young8b4edb52016-11-11 09:31:55 -07004251
4252 // If we made it this far, at least one ICD was successful
4253 one_icd_successful = true;
Jon Ashburn46888392015-01-29 15:45:51 -07004254 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004255
Mark Young8b4edb52016-11-11 09:31:55 -07004256 // If no ICDs were added to instance list and res is unchanged
4257 // from it's initial value, the loader was unable to find
4258 // a suitable ICD.
4259 if (VK_SUCCESS == res &&
4260 (ptr_instance->icd_terms == NULL || !one_icd_successful)) {
Mark Young3a587792016-08-19 15:25:08 -06004261 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4262 }
4263
4264out:
4265
4266 if (VK_SUCCESS != res) {
Mark Young0153e0b2016-11-03 14:27:13 -06004267 while (NULL != ptr_instance->icd_terms) {
4268 icd_term = ptr_instance->icd_terms;
4269 ptr_instance->icd_terms = icd_term->next;
4270 if (NULL != icd_term->instance) {
4271 icd_term->DestroyInstance(icd_term->instance, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004272 }
Mark Young0153e0b2016-11-03 14:27:13 -06004273 loader_icd_destroy(ptr_instance, icd_term, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004274 }
Ian Elliotteb450762015-02-05 15:19:15 -07004275 }
Jon Ashburn46888392015-01-29 15:45:51 -07004276
Mark Young3a587792016-08-19 15:25:08 -06004277 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004278}
4279
Mark Young0ad83132016-06-30 13:02:42 -06004280VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4281 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004282 struct loader_instance *ptr_instance = loader_instance(instance);
Mark Young0153e0b2016-11-03 14:27:13 -06004283 struct loader_icd_term *icd_terms = ptr_instance->icd_terms;
4284 struct loader_icd_term *next_icd_term;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004285
4286 // Remove this instance from the list of instances:
4287 struct loader_instance *prev = NULL;
4288 struct loader_instance *next = loader.instances;
4289 while (next != NULL) {
4290 if (next == ptr_instance) {
4291 // Remove this instance from the list:
4292 if (prev)
4293 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004294 else
4295 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004296 break;
4297 }
4298 prev = next;
4299 next = next->next;
4300 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004301
Mark Young0153e0b2016-11-03 14:27:13 -06004302 while (NULL != icd_terms) {
4303 if (icd_terms->instance) {
4304 icd_terms->DestroyInstance(icd_terms->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004305 }
Mark Young0153e0b2016-11-03 14:27:13 -06004306 next_icd_term = icd_terms->next;
4307 icd_terms->instance = VK_NULL_HANDLE;
4308 loader_icd_destroy(ptr_instance, icd_terms, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004309
Mark Young0153e0b2016-11-03 14:27:13 -06004310 icd_terms = next_icd_term;
Jon Ashburn46888392015-01-29 15:45:51 -07004311 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004312
Jon Ashburn23d36b12016-02-02 17:47:28 -07004313 loader_delete_layer_properties(ptr_instance,
4314 &ptr_instance->instance_layer_list);
Mark Young0153e0b2016-11-03 14:27:13 -06004315 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_tramp_list);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004316 loader_destroy_generic_list(
4317 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004318 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004319 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004320 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004321}
4322
Mark Young0ad83132016-06-30 13:02:42 -06004323VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4324 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4325 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4326 VkResult res = VK_SUCCESS;
Mark Young0153e0b2016-11-03 14:27:13 -06004327 struct loader_physical_device_term *phys_dev_term;
4328 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
4329 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004330
Jon Ashburncc407a22016-04-15 09:25:03 -06004331 struct loader_device *dev = (struct loader_device *)*pDevice;
Mark Young0153e0b2016-11-03 14:27:13 -06004332 PFN_vkCreateDevice fpCreateDevice = icd_term->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004333 struct loader_extension_list icd_exts;
4334
4335 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004336
Jon Ashburn1530c342016-02-26 13:14:27 -07004337 if (fpCreateDevice == NULL) {
Mark Young0153e0b2016-11-03 14:27:13 -06004338 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young9a3ddd42016-10-21 16:25:47 -06004339 "No vkCreateDevice command exposed by ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004340 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004341 res = VK_ERROR_INITIALIZATION_FAILED;
4342 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004343 }
4344
Jon Ashburn1530c342016-02-26 13:14:27 -07004345 VkDeviceCreateInfo localCreateInfo;
4346 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004347
4348 /*
4349 * NOTE: Need to filter the extensions to only those
4350 * supported by the ICD.
4351 * No ICD will advertise support for layers. An ICD
4352 * library could support a layer, but it would be
4353 * independent of the actual ICD, just in the same library.
4354 */
4355 char **filtered_extension_names = NULL;
4356 filtered_extension_names =
4357 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4358 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004359 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004360 }
4361
Jon Ashburn1530c342016-02-26 13:14:27 -07004362 localCreateInfo.enabledLayerCount = 0;
4363 localCreateInfo.ppEnabledLayerNames = NULL;
4364
4365 localCreateInfo.enabledExtensionCount = 0;
4366 localCreateInfo.ppEnabledExtensionNames =
4367 (const char *const *)filtered_extension_names;
4368
Jon Ashburn014438f2016-03-01 19:51:07 -07004369 /* Get the physical device (ICD) extensions */
Mark Young0153e0b2016-11-03 14:27:13 -06004370 res = loader_init_generic_list(icd_term->this_instance,
4371 (struct loader_generic_list *)&icd_exts,
4372 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004373 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004374 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004375 }
4376
4377 res = loader_add_device_extensions(
Mark Young0153e0b2016-11-03 14:27:13 -06004378 icd_term->this_instance, icd_term->EnumerateDeviceExtensionProperties,
4379 phys_dev_term->phys_dev, icd_term->scanned_icd->lib_name, &icd_exts);
Jon Ashburn014438f2016-03-01 19:51:07 -07004380 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004381 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004382 }
4383
Jon Ashburn1530c342016-02-26 13:14:27 -07004384 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4385 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004386 VkExtensionProperties *prop =
4387 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004388 if (prop) {
4389 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4390 (char *)extension_name;
4391 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06004392 } else {
Mark Young0153e0b2016-11-03 14:27:13 -06004393 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT,
4394 0, "vkCreateDevice extension %s not available for "
4395 "devices associated with ICD %s",
4396 extension_name, icd_term->scanned_icd->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07004397 }
4398 }
4399
Mark Young0153e0b2016-11-03 14:27:13 -06004400 res = fpCreateDevice(phys_dev_term->phys_dev, &localCreateInfo, pAllocator,
Mark Young9a3ddd42016-10-21 16:25:47 -06004401 &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004402 if (res != VK_SUCCESS) {
Mark Young0153e0b2016-11-03 14:27:13 -06004403 loader_log(icd_term->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Mark Young9a3ddd42016-10-21 16:25:47 -06004404 "vkCreateDevice call failed in ICD %s",
Mark Young0153e0b2016-11-03 14:27:13 -06004405 icd_term->scanned_icd->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004406 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004407 }
4408
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004409 *pDevice = dev->device;
Mark Young0153e0b2016-11-03 14:27:13 -06004410 loader_add_logical_device(icd_term->this_instance, icd_term, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004411
4412 /* Init dispatch pointer in new device object */
4413 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4414
Mark Young0ad83132016-06-30 13:02:42 -06004415out:
4416 if (NULL != icd_exts.list) {
Mark Young0153e0b2016-11-03 14:27:13 -06004417 loader_destroy_generic_list(icd_term->this_instance,
Mark Young0ad83132016-06-30 13:02:42 -06004418 (struct loader_generic_list *)&icd_exts);
4419 }
4420
Jon Ashburn1530c342016-02-26 13:14:27 -07004421 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004422}
4423
Mark Young0153e0b2016-11-03 14:27:13 -06004424VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumeratePhysicalDevices(
4425 VkInstance instance, uint32_t *pPhysicalDeviceCount,
4426 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004427 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004428 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004429 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004430
Mark Young0153e0b2016-11-03 14:27:13 -06004431 struct loader_icd_term *icd_term;
Jon Ashburn014438f2016-03-01 19:51:07 -07004432 struct loader_phys_dev_per_icd *phys_devs;
4433
4434 inst->total_gpu_count = 0;
4435 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4436 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4437 if (!phys_devs)
4438 return VK_ERROR_OUT_OF_HOST_MEMORY;
4439
Mark Young0153e0b2016-11-03 14:27:13 -06004440 icd_term = inst->icd_terms;
Jon Ashburn014438f2016-03-01 19:51:07 -07004441 for (i = 0; i < inst->total_icd_count; i++) {
Mark Young0153e0b2016-11-03 14:27:13 -06004442 assert(icd_term);
4443 res = icd_term->EnumeratePhysicalDevices(icd_term->instance,
4444 &phys_devs[i].count, NULL);
Jon Ashburn014438f2016-03-01 19:51:07 -07004445 if (res != VK_SUCCESS)
4446 return res;
Mark Young0153e0b2016-11-03 14:27:13 -06004447 icd_term = icd_term->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004448 }
4449
Mark Young0153e0b2016-11-03 14:27:13 -06004450 icd_term = inst->icd_terms;
Jon Ashburn014438f2016-03-01 19:51:07 -07004451 for (i = 0; i < inst->total_icd_count; i++) {
Mark Young0153e0b2016-11-03 14:27:13 -06004452 assert(icd_term);
Jon Ashburn014438f2016-03-01 19:51:07 -07004453 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4454 phys_devs[i].count * sizeof(VkPhysicalDevice));
4455 if (!phys_devs[i].phys_devs) {
4456 return VK_ERROR_OUT_OF_HOST_MEMORY;
4457 }
Mark Young0153e0b2016-11-03 14:27:13 -06004458 res = icd_term->EnumeratePhysicalDevices(
4459 icd_term->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
Jon Ashburn014438f2016-03-01 19:51:07 -07004460 if ((res == VK_SUCCESS)) {
4461 inst->total_gpu_count += phys_devs[i].count;
4462 } else {
4463 return res;
4464 }
Mark Young0153e0b2016-11-03 14:27:13 -06004465 phys_devs[i].this_icd_term = icd_term;
4466 icd_term = icd_term->next;
Jon Ashburn014438f2016-03-01 19:51:07 -07004467 }
Mark Young76a24632016-10-27 15:32:08 -06004468 if (inst->total_gpu_count == 0) {
4469 return VK_ERROR_INITIALIZATION_FAILED;
4470 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004471
Mark Young559d7502016-09-26 11:38:46 -06004472 uint32_t copy_count = inst->total_gpu_count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004473
Mark Young559d7502016-09-26 11:38:46 -06004474 if (NULL != pPhysicalDevices) {
4475 // Initialize the output pPhysicalDevices with wrapped loader
4476 // terminator physicalDevice objects; save this list of
4477 // wrapped objects in instance struct for later cleanup and
4478 // use by trampoline code
4479 uint32_t j, idx = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004480
Mark Young559d7502016-09-26 11:38:46 -06004481 if (copy_count > *pPhysicalDeviceCount) {
4482 copy_count = *pPhysicalDeviceCount;
4483 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004484
Mark Young76a24632016-10-27 15:32:08 -06004485 if (NULL == inst->phys_devs_term) {
Mark Young559d7502016-09-26 11:38:46 -06004486 inst->phys_devs_term = loader_instance_heap_alloc(
Mark Young0153e0b2016-11-03 14:27:13 -06004487 inst, sizeof(struct loader_physical_device_term) *
4488 inst->total_gpu_count,
Mark Young559d7502016-09-26 11:38:46 -06004489 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young76a24632016-10-27 15:32:08 -06004490 if (NULL == inst->phys_devs_term) {
Mark Young559d7502016-09-26 11:38:46 -06004491 return VK_ERROR_OUT_OF_HOST_MEMORY;
4492 }
4493 }
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004494
Mark Young76a24632016-10-27 15:32:08 -06004495 for (i = 0; idx < inst->total_gpu_count && i < inst->total_icd_count;
4496 i++) {
4497 for (j = 0; j < phys_devs[i].count && idx < inst->total_gpu_count;
4498 j++) {
Mark Young559d7502016-09-26 11:38:46 -06004499 loader_set_dispatch((void *)&inst->phys_devs_term[idx],
4500 inst->disp);
Mark Young0153e0b2016-11-03 14:27:13 -06004501 inst->phys_devs_term[idx].this_icd_term =
4502 phys_devs[i].this_icd_term;
Jamie Madilld29fc092016-10-24 16:54:40 -04004503 inst->phys_devs_term[idx].icd_index = (uint8_t)(i);
Mark Young559d7502016-09-26 11:38:46 -06004504 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
Slawomir Cyganeb610df2016-10-05 18:38:02 +02004505 if (idx < copy_count) {
4506 pPhysicalDevices[idx] =
4507 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4508 }
Mark Young559d7502016-09-26 11:38:46 -06004509 idx++;
4510 }
4511 }
4512
4513 if (copy_count < inst->total_gpu_count) {
Mark Young559d7502016-09-26 11:38:46 -06004514 res = VK_INCOMPLETE;
Jon Ashburn014438f2016-03-01 19:51:07 -07004515 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004516 }
Mark Young559d7502016-09-26 11:38:46 -06004517
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004518 *pPhysicalDeviceCount = copy_count;
4519
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004520 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004521}
4522
Jon Ashburn1530c342016-02-26 13:14:27 -07004523VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4524 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004525 struct loader_physical_device_term *phys_dev_term =
4526 (struct loader_physical_device_term *)physicalDevice;
4527 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004528
Mark Young0153e0b2016-11-03 14:27:13 -06004529 if (icd_term->GetPhysicalDeviceProperties)
4530 icd_term->GetPhysicalDeviceProperties(phys_dev_term->phys_dev,
4531 pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004532}
4533
Jon Ashburn1530c342016-02-26 13:14:27 -07004534VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004535 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4536 VkQueueFamilyProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004537 struct loader_physical_device_term *phys_dev_term =
4538 (struct loader_physical_device_term *)physicalDevice;
4539 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Tony Barbour59a47322015-06-24 16:06:58 -06004540
Mark Young0153e0b2016-11-03 14:27:13 -06004541 if (icd_term->GetPhysicalDeviceQueueFamilyProperties)
4542 icd_term->GetPhysicalDeviceQueueFamilyProperties(
4543 phys_dev_term->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004544}
4545
Jon Ashburn1530c342016-02-26 13:14:27 -07004546VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004547 VkPhysicalDevice physicalDevice,
4548 VkPhysicalDeviceMemoryProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004549 struct loader_physical_device_term *phys_dev_term =
4550 (struct loader_physical_device_term *)physicalDevice;
4551 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Tony Barbour59a47322015-06-24 16:06:58 -06004552
Mark Young0153e0b2016-11-03 14:27:13 -06004553 if (icd_term->GetPhysicalDeviceMemoryProperties)
4554 icd_term->GetPhysicalDeviceMemoryProperties(phys_dev_term->phys_dev,
4555 pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004556}
4557
Mark Young0153e0b2016-11-03 14:27:13 -06004558VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFeatures(
4559 VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures *pFeatures) {
4560 struct loader_physical_device_term *phys_dev_term =
4561 (struct loader_physical_device_term *)physicalDevice;
4562 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004563
Mark Young0153e0b2016-11-03 14:27:13 -06004564 if (icd_term->GetPhysicalDeviceFeatures)
4565 icd_term->GetPhysicalDeviceFeatures(phys_dev_term->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004566}
4567
Mark Young0153e0b2016-11-03 14:27:13 -06004568VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceFormatProperties(
4569 VkPhysicalDevice physicalDevice, VkFormat format,
4570 VkFormatProperties *pFormatInfo) {
4571 struct loader_physical_device_term *phys_dev_term =
4572 (struct loader_physical_device_term *)physicalDevice;
4573 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004574
Mark Young0153e0b2016-11-03 14:27:13 -06004575 if (icd_term->GetPhysicalDeviceFormatProperties)
4576 icd_term->GetPhysicalDeviceFormatProperties(phys_dev_term->phys_dev,
4577 format, pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004578}
4579
Jon Ashburn1530c342016-02-26 13:14:27 -07004580VKAPI_ATTR VkResult VKAPI_CALL
4581terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004582 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4583 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4584 VkImageFormatProperties *pImageFormatProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004585 struct loader_physical_device_term *phys_dev_term =
4586 (struct loader_physical_device_term *)physicalDevice;
4587 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburn754864f2015-07-23 18:49:07 -06004588
Mark Young0153e0b2016-11-03 14:27:13 -06004589 if (!icd_term->GetPhysicalDeviceImageFormatProperties)
Chia-I Wu17241042015-10-31 00:31:16 +08004590 return VK_ERROR_INITIALIZATION_FAILED;
4591
Mark Young0153e0b2016-11-03 14:27:13 -06004592 return icd_term->GetPhysicalDeviceImageFormatProperties(
4593 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004594 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004595}
4596
Jon Ashburn1530c342016-02-26 13:14:27 -07004597VKAPI_ATTR void VKAPI_CALL
4598terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004599 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4600 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4601 VkImageTiling tiling, uint32_t *pNumProperties,
4602 VkSparseImageFormatProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004603 struct loader_physical_device_term *phys_dev_term =
4604 (struct loader_physical_device_term *)physicalDevice;
4605 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004606
Mark Young0153e0b2016-11-03 14:27:13 -06004607 if (icd_term->GetPhysicalDeviceSparseImageFormatProperties)
4608 icd_term->GetPhysicalDeviceSparseImageFormatProperties(
4609 phys_dev_term->phys_dev, format, type, samples, usage, tiling,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004610 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004611}
4612
Jon Ashburn1530c342016-02-26 13:14:27 -07004613VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4614 VkPhysicalDevice physicalDevice, const char *pLayerName,
4615 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -06004616 struct loader_physical_device_term *phys_dev_term;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004617
Mark Young3a587792016-08-19 15:25:08 -06004618 struct loader_layer_list implicit_layer_list = {0};
4619 struct loader_extension_list all_exts = {0};
4620 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07004621
Jon Ashburndc5d9202016-02-29 13:00:51 -07004622 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004623
Jon Ashburndc5d9202016-02-29 13:00:51 -07004624 /* Any layer or trampoline wrapping should be removed at this point in time
4625 * can just cast to the expected type for VkPhysicalDevice. */
Mark Young0153e0b2016-11-03 14:27:13 -06004626 phys_dev_term = (struct loader_physical_device_term *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004627
Jon Ashburndc5d9202016-02-29 13:00:51 -07004628 /* this case is during the call down the instance chain with pLayerName
4629 * == NULL*/
Mark Young0153e0b2016-11-03 14:27:13 -06004630 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004631 uint32_t icd_ext_count = *pPropertyCount;
4632 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004633
Jon Ashburndc5d9202016-02-29 13:00:51 -07004634 /* get device extensions */
Mark Young0153e0b2016-11-03 14:27:13 -06004635 res = icd_term->EnumerateDeviceExtensionProperties(
4636 phys_dev_term->phys_dev, NULL, &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06004637 if (res != VK_SUCCESS) {
4638 goto out;
4639 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004640
Mark Young0153e0b2016-11-03 14:27:13 -06004641 if (!loader_init_layer_list(icd_term->this_instance,
4642 &implicit_layer_list)) {
Mark Young3a587792016-08-19 15:25:08 -06004643 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4644 goto out;
4645 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004646
Jon Ashburndc5d9202016-02-29 13:00:51 -07004647 loader_add_layer_implicit(
Mark Young0153e0b2016-11-03 14:27:13 -06004648 icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4649 &implicit_layer_list, &icd_term->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004650 /* we need to determine which implicit layers are active,
4651 * and then add their extensions. This can't be cached as
4652 * it depends on results of environment variables (which can change).
4653 */
4654 if (pProperties != NULL) {
4655 /* initialize dev_extension list within the physicalDevice object */
Mark Young0153e0b2016-11-03 14:27:13 -06004656 res = loader_init_device_extensions(icd_term->this_instance,
4657 phys_dev_term, icd_ext_count,
4658 pProperties, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004659 if (res != VK_SUCCESS) {
4660 goto out;
4661 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004662
Jon Ashburndc5d9202016-02-29 13:00:51 -07004663 /* we need to determine which implicit layers are active,
4664 * and then add their extensions. This can't be cached as
4665 * it depends on results of environment variables (which can
4666 * change).
4667 */
Mark Young0153e0b2016-11-03 14:27:13 -06004668 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts,
Mark Young3a587792016-08-19 15:25:08 -06004669 icd_exts.count, icd_exts.list);
4670 if (res != VK_SUCCESS) {
4671 goto out;
4672 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004673
Jon Ashburndc5d9202016-02-29 13:00:51 -07004674 loader_add_layer_implicit(
Mark Young0153e0b2016-11-03 14:27:13 -06004675 icd_term->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4676 &implicit_layer_list,
4677 &icd_term->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004678
Jon Ashburndc5d9202016-02-29 13:00:51 -07004679 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4680 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004681 j < implicit_layer_list.list[i].device_extension_list.count;
4682 j++) {
Mark Young0153e0b2016-11-03 14:27:13 -06004683 res = loader_add_to_ext_list(icd_term->this_instance, &all_exts,
4684 1,
Mark Young3a587792016-08-19 15:25:08 -06004685 &implicit_layer_list.list[i]
4686 .device_extension_list.list[j]
4687 .props);
4688 if (res != VK_SUCCESS) {
4689 goto out;
4690 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004691 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004692 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004693 uint32_t capacity = *pPropertyCount;
4694 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004695
Jon Ashburndc5d9202016-02-29 13:00:51 -07004696 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4697 props[i] = all_exts.list[i];
4698 }
4699 /* wasn't enough space for the extensions, we did partial copy now
4700 * return VK_INCOMPLETE */
4701 if (capacity < all_exts.count) {
4702 res = VK_INCOMPLETE;
4703 } else {
4704 *pPropertyCount = all_exts.count;
4705 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004706 } else {
4707 /* just return the count; need to add in the count of implicit layer
4708 * extensions
4709 * don't worry about duplicates being added in the count */
4710 *pPropertyCount = icd_ext_count;
4711
4712 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4713 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004714 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004715 }
4716 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004717 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004718
Mark Young3a587792016-08-19 15:25:08 -06004719out:
4720
4721 if (NULL != implicit_layer_list.list) {
4722 loader_destroy_generic_list(
Mark Young0153e0b2016-11-03 14:27:13 -06004723 icd_term->this_instance,
Mark Young3a587792016-08-19 15:25:08 -06004724 (struct loader_generic_list *)&implicit_layer_list);
4725 }
4726 if (NULL != all_exts.list) {
Mark Young0153e0b2016-11-03 14:27:13 -06004727 loader_destroy_generic_list(icd_term->this_instance,
Mark Young3a587792016-08-19 15:25:08 -06004728 (struct loader_generic_list *)&all_exts);
4729 }
4730 if (NULL != icd_exts.list) {
Mark Young0153e0b2016-11-03 14:27:13 -06004731 loader_destroy_generic_list(icd_term->this_instance,
Mark Young3a587792016-08-19 15:25:08 -06004732 (struct loader_generic_list *)&icd_exts);
4733 }
4734
Jon Ashburndc5d9202016-02-29 13:00:51 -07004735 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004736}
4737
Mark Young0153e0b2016-11-03 14:27:13 -06004738VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceLayerProperties(
4739 VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
4740 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004741
Jon Ashburndc5d9202016-02-29 13:00:51 -07004742 // should never get here this call isn't dispatched down the chain
4743 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004744}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004745
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004746VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004747 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004748 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004749 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004750
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004751 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004752 if (utf8[i] == 0) {
4753 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004754 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004755 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004756 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004757 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004758 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004759 num_char_bytes = 2;
4760 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4761 num_char_bytes = 3;
4762 } else {
4763 result = VK_STRING_ERROR_BAD_DATA;
4764 }
4765
4766 // Validate the following num_char_bytes of data
4767 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4768 if (++i == max_length) {
4769 result |= VK_STRING_ERROR_LENGTH;
4770 break;
4771 }
4772 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4773 result |= VK_STRING_ERROR_BAD_DATA;
4774 }
4775 }
4776 }
4777 return result;
4778}