blob: 7f90ee48d35da10e89ca62cc51897e0439403319 [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;
Jon Ashburn014438f2016-03-01 19:51:07 -070065 struct loader_icd *this_icd;
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,
355 const struct loader_instance *inst) {
356 // stub func
357 (void)inst;
358 (void)name;
359 return NULL;
360}
361static inline void loader_free_getenv(const char *val,
362 const struct loader_instance *inst) {
363 // 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
Jon Ashburncc407a22016-04-15 09:25:03 -0600401VKAPI_ATTR VkResult VKAPI_CALL
402vkSetInstanceDispatch(VkInstance instance, 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
Jon Ashburncc407a22016-04-15 09:25:03 -0600412VKAPI_ATTR VkResult VKAPI_CALL
413vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600414 struct loader_device *dev;
415 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
416
417 if (!icd) {
418 return VK_ERROR_INITIALIZATION_FAILED;
419 }
420 loader_set_dispatch(object, &dev->loader_dispatch);
421 return VK_SUCCESS;
422}
423
Jon Ashburnffad94d2015-06-30 14:46:22 -0700424#if defined(WIN32)
Tony Barbourea968902015-07-29 14:26:21 -0600425static char *loader_get_next_path(char *path);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700426/**
427* Find the list of registry files (names within a key) in key "location".
428*
Jon Ashburn23d36b12016-02-02 17:47:28 -0700429* This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
430*given in "location"
431* for a list or name/values which are added to a returned list (function return
432*value).
Jon Ashburnffad94d2015-06-30 14:46:22 -0700433* The DWORD values within the key must be 0 or they are skipped.
Jon Ashburne39a4f82015-08-28 13:38:21 -0600434* Function return is a string with a ';' separated list of filenames.
Jon Ashburnffad94d2015-06-30 14:46:22 -0700435* Function return is NULL if no valid name/value pairs are found in the key,
436* or the key is not found.
437*
438* \returns
439* A string list of filenames as pointer.
440* When done using the returned string list, pointer should be freed.
441*/
Jon Ashburn23d36b12016-02-02 17:47:28 -0700442static char *loader_get_registry_files(const struct loader_instance *inst,
443 char *location) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700444 LONG rtn_value;
445 HKEY hive, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700446 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700447 char name[2048];
448 char *out = NULL;
Tony Barbourea968902015-07-29 14:26:21 -0600449 char *loc = location;
450 char *next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700451 DWORD idx = 0;
452 DWORD name_size = sizeof(name);
453 DWORD value;
454 DWORD total_size = 4096;
455 DWORD value_size = sizeof(value);
Tony Barbourea968902015-07-29 14:26:21 -0600456
Jon Ashburn23d36b12016-02-02 17:47:28 -0700457 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600458 next = loader_get_next_path(loc);
459 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniell524ec732015-11-05 16:58:26 -0700460 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600461 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
462 if (rtn_value != ERROR_SUCCESS) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700463 // We still couldn't find the key, so give up:
464 loc = next;
465 continue;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700466 }
Tony Barbourea968902015-07-29 14:26:21 -0600467
Jon Ashburn23d36b12016-02-02 17:47:28 -0700468 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL,
469 NULL, (LPBYTE)&value, &value_size)) ==
470 ERROR_SUCCESS) {
Tony Barbourea968902015-07-29 14:26:21 -0600471 if (value_size == sizeof(value) && value == 0) {
472 if (out == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -0600473 out = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700474 inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600475 if (NULL == out) {
476 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
477 "Out of memory can't alloc space for registry data");
478 return NULL;
479 }
Tony Barbourea968902015-07-29 14:26:21 -0600480 out[0] = '\0';
Jon Ashburn23d36b12016-02-02 17:47:28 -0700481 } else if (strlen(out) + name_size + 1 > total_size) {
Mark Young0ad83132016-06-30 13:02:42 -0600482 out = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700483 inst, out, total_size, total_size * 2,
484 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600485 if (NULL == out) {
486 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
487 "Out of memory can't realloc space for registry data");
488 return NULL;
489 }
Tony Barbourea968902015-07-29 14:26:21 -0600490 total_size *= 2;
491 }
Tony Barbourea968902015-07-29 14:26:21 -0600492 if (strlen(out) == 0)
Jon Ashburn23d36b12016-02-02 17:47:28 -0700493 snprintf(out, name_size + 1, "%s", name);
Tony Barbourea968902015-07-29 14:26:21 -0600494 else
Jon Ashburn23d36b12016-02-02 17:47:28 -0700495 snprintf(out + strlen(out), name_size + 2, "%c%s",
496 PATH_SEPERATOR, name);
Tony Barbourea968902015-07-29 14:26:21 -0600497 }
498 name_size = 2048;
499 }
500 loc = next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700501 }
Tony Barbourea968902015-07-29 14:26:21 -0600502
Jon Ashburnffad94d2015-06-30 14:46:22 -0700503 return out;
504}
505
Ian Elliott4470a302015-02-17 10:33:47 -0700506#endif // WIN32
507
Jon Ashburnc7237a72015-08-03 09:08:46 -0600508/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500509 * Combine path elements, separating each element with the platform-specific
510 * directory separator, and save the combined string to a destination buffer,
511 * not exceeding the given length. Path elements are given as variadic args,
512 * with a NULL element terminating the list.
513 *
514 * \returns the total length of the combined string, not including an ASCII
515 * NUL termination character. This length may exceed the available storage:
516 * in this case, the written string will be truncated to avoid a buffer
517 * overrun, and the return value will greater than or equal to the storage
518 * size. A NULL argument may be provided as the destination buffer in order
519 * to determine the required string length without actually writing a string.
520 */
521
Jon Ashburn23d36b12016-02-02 17:47:28 -0700522static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700523 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500524 va_list ap;
525 const char *component;
526
527 va_start(ap, len);
528
Jon Ashburn23d36b12016-02-02 17:47:28 -0700529 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500530 if (required_len > 0) {
531 // This path element is not the first non-empty element; prepend
532 // a directory separator if space allows
533 if (dest && required_len + 1 < len) {
534 snprintf(dest + required_len, len - required_len, "%c",
535 DIRECTORY_SYMBOL);
536 }
537 required_len++;
538 }
539
540 if (dest && required_len < len) {
541 strncpy(dest + required_len, component, len - required_len);
542 }
543 required_len += strlen(component);
544 }
545
546 va_end(ap);
547
548 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
549 if (dest && required_len >= len) {
550 dest[len - 1] = '\0';
551 }
552
553 return required_len;
554}
555
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500556/**
Jon Ashburnc7237a72015-08-03 09:08:46 -0600557 * Given string of three part form "maj.min.pat" convert to a vulkan version
558 * number.
559 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700560static uint32_t loader_make_version(const char *vers_str) {
561 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
562 char *minor_str = NULL;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600563 char *patch_str = NULL;
564 char *cstr;
565 char *str;
566
567 if (!vers_str)
568 return vers;
569 cstr = loader_stack_alloc(strlen(vers_str) + 1);
570 strcpy(cstr, vers_str);
571 while ((str = strchr(cstr, '.')) != NULL) {
572 if (minor_str == NULL) {
573 minor_str = str + 1;
574 *str = '\0';
575 major = atoi(cstr);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700576 } else if (patch_str == NULL) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600577 patch_str = str + 1;
578 *str = '\0';
579 minor = atoi(minor_str);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700580 } else {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600581 return vers;
582 }
583 cstr = str + 1;
584 }
585 patch = atoi(patch_str);
586
587 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600588}
589
Jon Ashburn23d36b12016-02-02 17:47:28 -0700590bool compare_vk_extension_properties(const VkExtensionProperties *op1,
591 const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800592 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600593}
594
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600595/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600596 * Search the given ext_array for an extension
597 * matching the given vk_ext_prop
598 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700599bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
600 const uint32_t count,
601 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600602 for (uint32_t i = 0; i < count; i++) {
603 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i]))
604 return true;
605 }
606 return false;
607}
608
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600609/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600610 * Search the given ext_list for an extension
611 * matching the given vk_ext_prop
612 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700613bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop,
614 const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600615 for (uint32_t i = 0; i < ext_list->count; i++) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600616 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop))
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600617 return true;
618 }
619 return false;
620}
621
Jon Ashburnb8726962016-04-08 15:03:35 -0600622/**
623 * Search the given ext_list for a device extension matching the given ext_prop
624 */
Jon Ashburncc407a22016-04-15 09:25:03 -0600625bool has_vk_dev_ext_property(
626 const VkExtensionProperties *ext_prop,
627 const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600628 for (uint32_t i = 0; i < ext_list->count; i++) {
629 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop))
630 return true;
631 }
632 return false;
633}
634
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600635/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600636 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600637 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700638static struct loader_layer_properties *
639loader_get_layer_property(const char *name,
640 const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600641 for (uint32_t i = 0; i < layer_list->count; i++) {
642 const VkLayerProperties *item = &layer_list->list[i].info;
643 if (strcmp(name, item->layerName) == 0)
644 return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600645 }
646 return NULL;
647}
648
Jon Ashburne13ecc92015-08-03 17:19:30 -0600649/**
650 * Get the next unused layer property in the list. Init the property to zero.
651 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700652static struct loader_layer_properties *
653loader_get_next_layer_property(const struct loader_instance *inst,
654 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600655 if (layer_list->capacity == 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700656 layer_list->list =
Mark Young0ad83132016-06-30 13:02:42 -0600657 loader_instance_heap_alloc(
658 inst, sizeof(struct loader_layer_properties) * 64,
659 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600660 if (layer_list->list == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700661 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
662 "Out of memory can't add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600663 return NULL;
664 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700665 memset(layer_list->list, 0,
666 sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600667 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
668 }
669
670 // ensure enough room to add an entry
Jon Ashburn23d36b12016-02-02 17:47:28 -0700671 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
672 layer_list->capacity) {
Mark Young0ad83132016-06-30 13:02:42 -0600673 layer_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700674 inst, layer_list->list, layer_list->capacity,
675 layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600676 if (layer_list->list == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700677 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700678 "realloc failed for layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600679 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600680 }
681 layer_list->capacity *= 2;
682 }
683
684 layer_list->count++;
685 return &(layer_list->list[layer_list->count - 1]);
686}
687
688/**
689 * Remove all layer properties entrys from the list
690 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700691void loader_delete_layer_properties(const struct loader_instance *inst,
692 struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700693 uint32_t i, j;
694 struct loader_device_extension_list *dev_ext_list;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600695 if (!layer_list)
696 return;
697
Jon Ashburne13ecc92015-08-03 17:19:30 -0600698 for (i = 0; i < layer_list->count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700699 loader_destroy_generic_list(
700 inst, (struct loader_generic_list *)&layer_list->list[i]
701 .instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700702 dev_ext_list = &layer_list->list[i].device_extension_list;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700703 if (dev_ext_list->capacity > 0 &&
Mark Young0ad83132016-06-30 13:02:42 -0600704 NULL != dev_ext_list->list &&
Jon Ashburn23d36b12016-02-02 17:47:28 -0700705 dev_ext_list->list->entrypoint_count > 0) {
706 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Young0ad83132016-06-30 13:02:42 -0600707 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700708 }
Mark Young0ad83132016-06-30 13:02:42 -0600709 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700710 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700711 loader_destroy_generic_list(inst,
712 (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600713 }
714 layer_list->count = 0;
715
Jon Ashburnb82c1852015-08-11 14:49:54 -0600716 if (layer_list->capacity > 0) {
717 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600718 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600719 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600720}
721
Mark Young3a587792016-08-19 15:25:08 -0600722static VkResult loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700723 const struct loader_instance *inst,
724 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props,
725 const char *lib_name, struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700726 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600727 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600728 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600729
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600730 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600731 /* No EnumerateInstanceExtensionProperties defined */
Mark Young3a587792016-08-19 15:25:08 -0600732 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600733 }
734
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600735 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600736 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700737 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700738 "Error getting Instance extension count from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600739 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600740 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600741
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600742 if (count == 0) {
743 /* No ExtensionProperties to report */
Mark Young3a587792016-08-19 15:25:08 -0600744 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600745 }
746
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600747 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600748
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600749 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600750 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700751 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700752 "Error getting Instance extensions from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600753 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600754 }
Tony Barbour59a47322015-06-24 16:06:58 -0600755
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600756 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600757 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600758
Jon Ashburncc407a22016-04-15 09:25:03 -0600759 bool ext_unsupported =
760 wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600761 if (!ext_unsupported) {
762 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
763 VK_MAJOR(ext_props[i].specVersion),
764 VK_MINOR(ext_props[i].specVersion),
765 VK_PATCH(ext_props[i].specVersion));
766 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
767 "Instance Extension: %s (%s) version %s",
768 ext_props[i].extensionName, lib_name, spec_version);
Mark Young3a587792016-08-19 15:25:08 -0600769 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
770 if (res != VK_SUCCESS) {
771 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
772 "Failed to add %s to Instance extension list",
773 lib_name);
774 goto out;
775 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600776 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600777 }
Mark Young3a587792016-08-19 15:25:08 -0600778out:
779 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600780}
781
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700782/*
783 * Initialize ext_list with the physical device extensions.
784 * The extension properties are passed as inputs in count and ext_props.
785 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700786static VkResult
787loader_init_device_extensions(const struct loader_instance *inst,
788 struct loader_physical_device *phys_dev,
789 uint32_t count, VkExtensionProperties *ext_props,
790 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700791 VkResult res;
792 uint32_t i;
793
Mark Young3a587792016-08-19 15:25:08 -0600794 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
795 sizeof(VkExtensionProperties));
796 if (VK_SUCCESS != res) {
797 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700798 }
799
800 for (i = 0; i < count; i++) {
801 char spec_version[64];
802
Jon Ashburn23d36b12016-02-02 17:47:28 -0700803 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
804 VK_MAJOR(ext_props[i].specVersion),
805 VK_MINOR(ext_props[i].specVersion),
806 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700807 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700808 "Device Extension: %s (%s) version %s",
809 ext_props[i].extensionName,
810 phys_dev->this_icd->this_icd_lib->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700811 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
812 if (res != VK_SUCCESS)
813 return res;
814 }
815
816 return VK_SUCCESS;
817}
818
Jon Ashburn1530c342016-02-26 13:14:27 -0700819VkResult loader_add_device_extensions(const struct loader_instance *inst,
Jon Ashburncc407a22016-04-15 09:25:03 -0600820 PFN_vkEnumerateDeviceExtensionProperties
821 fpEnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700822 VkPhysicalDevice physical_device,
823 const char *lib_name,
824 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600825 uint32_t i, count;
826 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600827 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600828
Piers Daniell295fe402016-03-29 11:51:11 -0600829 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count,
830 NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700831 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700832 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700833 if (!ext_props)
834 return VK_ERROR_OUT_OF_HOST_MEMORY;
Piers Daniell295fe402016-03-29 11:51:11 -0600835 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
836 &count, ext_props);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700837 if (res != VK_SUCCESS)
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700838 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700839 for (i = 0; i < count; i++) {
840 char spec_version[64];
841
Jon Ashburn23d36b12016-02-02 17:47:28 -0700842 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
843 VK_MAJOR(ext_props[i].specVersion),
844 VK_MINOR(ext_props[i].specVersion),
845 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700846 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700847 "Device Extension: %s (%s) version %s",
848 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700849 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
850 if (res != VK_SUCCESS)
851 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600852 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700853 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700854 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
855 "Error getting physical device extension info count from "
856 "library %s",
857 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700858 return res;
859 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600860
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700861 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600862}
863
Mark Young3a587792016-08-19 15:25:08 -0600864VkResult loader_init_generic_list(const struct loader_instance *inst,
Jon Ashburn1530c342016-02-26 13:14:27 -0700865 struct loader_generic_list *list_info,
866 size_t element_size) {
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700867 list_info->capacity = 32 * element_size;
Mark Young0ad83132016-06-30 13:02:42 -0600868 list_info->list = loader_instance_heap_alloc(
869 inst, list_info->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 }
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700873 memset(list_info->list, 0, list_info->capacity);
874 list_info->count = 0;
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 Young3a587792016-08-19 15:25:08 -0600899 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700900 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600901 if (VK_SUCCESS != res) {
902 return res;
903 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600904 }
905
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600906 for (i = 0; i < prop_list_count; i++) {
907 cur_ext = &props[i];
908
909 // look for duplicates
910 if (has_vk_extension_property(cur_ext, ext_list)) {
911 continue;
912 }
913
914 // add to list at end
915 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700916 if (ext_list->count * sizeof(VkExtensionProperties) >=
917 ext_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -0600918
Mark Young0ad83132016-06-30 13:02:42 -0600919 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700920 inst, ext_list->list, ext_list->capacity,
921 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700922
923 if (ext_list->list == NULL)
924 return VK_ERROR_OUT_OF_HOST_MEMORY;
925
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600926 // double capacity
927 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600928 }
929
Jon Ashburn23d36b12016-02-02 17:47:28 -0700930 memcpy(&ext_list->list[ext_list->count], cur_ext,
931 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600932 ext_list->count++;
933 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700934 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600935}
936
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700937/*
938 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600939 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700940 * Return
941 * Vk_SUCCESS on success
942 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700943VkResult
944loader_add_to_dev_ext_list(const struct loader_instance *inst,
945 struct loader_device_extension_list *ext_list,
946 const VkExtensionProperties *props,
947 uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700948 uint32_t idx;
949 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600950 VkResult res = loader_init_generic_list(
951 inst, (struct loader_generic_list *)ext_list,
952 sizeof(struct loader_dev_ext_props));
953 if (VK_SUCCESS != res) {
954 return res;
955 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700956 }
957
Jon Ashburnb8726962016-04-08 15:03:35 -0600958 // look for duplicates
959 if (has_vk_dev_ext_property(props, ext_list)) {
960 return VK_SUCCESS;
961 }
962
Jon Ashburn23d36b12016-02-02 17:47:28 -0700963 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700964 // add to list at end
965 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700966 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700967
Mark Young0ad83132016-06-30 13:02:42 -0600968 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700969 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
970 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700971
972 if (ext_list->list == NULL)
973 return VK_ERROR_OUT_OF_HOST_MEMORY;
974
975 // double capacity
976 ext_list->capacity *= 2;
977 }
978
Jon Ashburn23d36b12016-02-02 17:47:28 -0700979 memcpy(&ext_list->list[idx].props, props,
980 sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700981 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700982 ext_list->list[idx].entrypoints =
Mark Young0ad83132016-06-30 13:02:42 -0600983 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
984 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
985 if (ext_list->list[idx].entrypoints == NULL) {
986 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700987 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600988 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700989 for (uint32_t i = 0; i < entry_count; i++) {
Mark Young0ad83132016-06-30 13:02:42 -0600990 ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700991 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600992 if (ext_list->list[idx].entrypoints[i] == NULL) {
993 for (uint32_t j = 0; j < i; j++) {
994 loader_instance_heap_free(inst,
995 ext_list->list[idx].entrypoints[j]);
996 }
997 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
998 ext_list->list[idx].entrypoint_count = 0;
999 ext_list->list[idx].entrypoints = NULL;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001000 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001001 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001002 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
1003 }
1004 ext_list->count++;
1005
1006 return VK_SUCCESS;
1007}
1008
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001009/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001010 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001011 * Add these to the output layer_list. Don't add duplicates to the output
1012 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001013 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001014static VkResult
1015loader_add_layer_names_to_list(const struct loader_instance *inst,
1016 struct loader_layer_list *output_list,
1017 uint32_t name_count, const char *const *names,
1018 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001019 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001020 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001021
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001022 for (uint32_t i = 0; i < name_count; i++) {
1023 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001024 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001025 if (!layer_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001026 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1027 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001028 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001029 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001030 }
1031
Mark Young0ad83132016-06-30 13:02:42 -06001032 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001033 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001034
1035 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001036}
1037
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001038/*
1039 * Manage lists of VkLayerProperties
1040 */
Jon Ashburne39a4f82015-08-28 13:38:21 -06001041static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001042 struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001043 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Young0ad83132016-06-30 13:02:42 -06001044 list->list = loader_instance_heap_alloc(
1045 inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001046 if (list->list == NULL) {
1047 return false;
1048 }
1049 memset(list->list, 0, list->capacity);
1050 list->count = 0;
1051 return true;
1052}
1053
Jon Ashburne39a4f82015-08-28 13:38:21 -06001054void loader_destroy_layer_list(const struct loader_instance *inst,
Mark Young0ad83132016-06-30 13:02:42 -06001055 struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001056 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001057 if (device) {
1058 loader_device_heap_free(device, layer_list->list);
1059 } else {
1060 loader_instance_heap_free(inst, layer_list->list);
1061 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001062 layer_list->count = 0;
1063 layer_list->capacity = 0;
1064}
1065
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001066/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001067 * Search the given layer list for a list
1068 * matching the given VkLayerProperties
1069 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001070bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
1071 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001072 for (uint32_t i = 0; i < list->count; i++) {
1073 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
1074 return true;
1075 }
1076 return false;
1077}
1078
1079/*
1080 * Search the given layer list for a layer
1081 * matching the given name
1082 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001083bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001084 for (uint32_t i = 0; i < list->count; i++) {
1085 if (strcmp(name, list->list[i].info.layerName) == 0)
1086 return true;
1087 }
1088 return false;
1089}
1090
1091/*
1092 * Append non-duplicate layer properties defined in prop_list
1093 * to the given layer_info list
1094 */
Mark Young0ad83132016-06-30 13:02:42 -06001095VkResult loader_add_to_layer_list(const struct loader_instance *inst,
1096 struct loader_layer_list *list,
1097 uint32_t prop_list_count,
1098 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001099 uint32_t i;
1100 struct loader_layer_properties *layer;
1101
1102 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001103 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001104 }
1105
1106 if (list->list == NULL)
Mark Young0ad83132016-06-30 13:02:42 -06001107 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001108
1109 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001110 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001111
1112 // look for duplicates
1113 if (has_vk_layer_property(&layer->info, list)) {
1114 continue;
1115 }
1116
1117 // add to list at end
1118 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001119 if (list->count * sizeof(struct loader_layer_properties) >=
1120 list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001121
Mark Young0ad83132016-06-30 13:02:42 -06001122 list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001123 inst, list->list, list->capacity, list->capacity * 2,
1124 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001125 if (NULL == list->list) {
1126 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1127 "realloc failed for layer list when attempting to "
1128 "add new layer");
1129 return VK_ERROR_OUT_OF_HOST_MEMORY;
1130 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001131 // double capacity
1132 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001133 }
1134
Jon Ashburn23d36b12016-02-02 17:47:28 -07001135 memcpy(&list->list[list->count], layer,
1136 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001137 list->count++;
1138 }
Mark Young0ad83132016-06-30 13:02:42 -06001139
1140 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001141}
1142
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001143/**
1144 * Search the search_list for any layer with a name
1145 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001146 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001147 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001148 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001149 */
Jon Ashburncc407a22016-04-15 09:25:03 -06001150void loader_find_layer_name_add_list(
1151 const struct loader_instance *inst, const char *name,
1152 const enum layer_type type, const struct loader_layer_list *search_list,
1153 struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001154 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001155 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001156 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001157 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001158 (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001159 /* Found a layer with the same name, add to found_list */
Mark Young0ad83132016-06-30 13:02:42 -06001160 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
1161 found = true;
1162 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001163 }
1164 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001165 if (!found) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001166 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001167 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001168 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001169}
1170
Jon Ashburn23d36b12016-02-02 17:47:28 -07001171static VkExtensionProperties *
1172get_extension_property(const char *name,
1173 const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001174 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001175 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001176 return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001177 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001178 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001179}
1180
Jon Ashburn23d36b12016-02-02 17:47:28 -07001181static VkExtensionProperties *
1182get_dev_extension_property(const char *name,
1183 const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001184 for (uint32_t i = 0; i < list->count; i++) {
1185 if (strcmp(name, list->list[i].props.extensionName) == 0)
1186 return &list->list[i].props;
1187 }
1188 return NULL;
1189}
1190
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001191/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001192 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001193 * the extension must provide two entry points for the loader to use:
1194 * - "trampoline" entry point - this is the address returned by GetProcAddr
1195 * and will always do what's necessary to support a global call.
1196 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001197 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001198 * the extension for the appropriate ICDs that are available.
1199 * There is no generic mechanism for including these functions, the references
1200 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001201 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1202 * GetProcAddr requests
1203 * loader_coalesce_extensions(void) - add extension records to the list of
1204 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001205 * extension available to the app.
1206 * instance_disp - add function pointer for terminator function to this array.
1207 * The extension itself should be in a separate file that will be
1208 * linked directly with the loader.
1209 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001210
Mark Young3a587792016-08-19 15:25:08 -06001211VkResult loader_get_icd_loader_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001212 const struct loader_instance *inst, struct loader_icd_libs *icd_libs,
1213 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001214 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001215 VkResult res = VK_SUCCESS;
1216
Jon Ashburn23d36b12016-02-02 17:47:28 -07001217 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1218 "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001219
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001220 // traverse scanned icd list adding non-duplicate extensions to the list
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001221 for (uint32_t i = 0; i < icd_libs->count; i++) {
Mark Young3a587792016-08-19 15:25:08 -06001222 res = loader_init_generic_list(inst,
1223 (struct loader_generic_list *)&icd_exts,
1224 sizeof(VkExtensionProperties));
1225 if (VK_SUCCESS != res) {
1226 goto out;
1227 }
1228 res = loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001229 inst, icd_libs->list[i].EnumerateInstanceExtensionProperties,
1230 icd_libs->list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001231 if (VK_SUCCESS == res) {
1232 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count,
1233 icd_exts.list);
1234 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001235 loader_destroy_generic_list(inst,
1236 (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001237 if (VK_SUCCESS != res) {
1238 goto out;
1239 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001240 };
1241
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001242 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001243 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001244
1245out:
1246 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001247}
1248
Jon Ashburne39a4f82015-08-28 13:38:21 -06001249struct loader_icd *loader_get_icd_and_device(const VkDevice device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001250 struct loader_device **found_dev) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001251 *found_dev = NULL;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001252 for (struct loader_instance *inst = loader.instances; inst;
1253 inst = inst->next) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001254 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001255 for (struct loader_device *dev = icd->logical_device_list; dev;
1256 dev = dev->next)
1257 /* Value comparison of device prevents object wrapping by layers
1258 */
1259 if (loader_get_dispatch(dev->device) ==
1260 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001261 *found_dev = dev;
1262 return icd;
1263 }
1264 }
1265 }
1266 return NULL;
1267}
1268
Mark Young0ad83132016-06-30 13:02:42 -06001269void loader_destroy_logical_device(const struct loader_instance *inst,
1270 struct loader_device *dev,
1271 const VkAllocationCallbacks *pAllocator) {
1272 if (pAllocator) {
1273 dev->alloc_callbacks = *pAllocator;
1274 }
1275 if (NULL != dev->app_extension_props) {
1276 loader_device_heap_free(dev, dev->app_extension_props);
1277 }
1278 if (NULL != dev->activated_layer_list.list) {
1279 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1280 }
1281 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001282}
1283
Jon Ashburn1530c342016-02-26 13:14:27 -07001284struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001285loader_create_logical_device(const struct loader_instance *inst,
1286 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001287 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001288#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1289 {
1290#else
1291 if (pAllocator) {
1292 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1293 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1294 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1295 } else {
1296#endif
1297 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1298 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001299
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001300 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001301 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1302 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001303 return NULL;
1304 }
1305
1306 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001307 if (pAllocator) {
1308 new_dev->alloc_callbacks = *pAllocator;
1309 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001310
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001311 return new_dev;
1312}
1313
Piers Daniell295fe402016-03-29 11:51:11 -06001314void loader_add_logical_device(const struct loader_instance *inst,
1315 struct loader_icd *icd,
1316 struct loader_device *dev) {
1317 dev->next = icd->logical_device_list;
1318 icd->logical_device_list = dev;
1319}
1320
Jon Ashburn23d36b12016-02-02 17:47:28 -07001321void loader_remove_logical_device(const struct loader_instance *inst,
1322 struct loader_icd *icd,
Mark Young0ad83132016-06-30 13:02:42 -06001323 struct loader_device *found_dev,
1324 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001325 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001326
1327 if (!icd || !found_dev)
1328 return;
1329
1330 prev_dev = NULL;
1331 dev = icd->logical_device_list;
1332 while (dev && dev != found_dev) {
1333 prev_dev = dev;
1334 dev = dev->next;
1335 }
1336
1337 if (prev_dev)
1338 prev_dev->next = found_dev->next;
1339 else
1340 icd->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001341 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001342}
1343
Jon Ashburn23d36b12016-02-02 17:47:28 -07001344static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0ad83132016-06-30 13:02:42 -06001345 struct loader_icd *icd,
1346 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001347 ptr_inst->total_icd_count--;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001348 for (struct loader_device *dev = icd->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001349 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001350 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001351 dev = next_dev;
1352 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001353
Mark Young0ad83132016-06-30 13:02:42 -06001354 loader_instance_heap_free(ptr_inst, icd);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001355}
1356
Jon Ashburn23d36b12016-02-02 17:47:28 -07001357static struct loader_icd *
1358loader_icd_create(const struct loader_instance *inst) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001359 struct loader_icd *icd;
1360
Mark Young0ad83132016-06-30 13:02:42 -06001361 icd = loader_instance_heap_alloc(inst, sizeof(*icd),
1362 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001363 if (!icd)
1364 return NULL;
1365
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001366 memset(icd, 0, sizeof(*icd));
1367
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001368 return icd;
1369}
1370
Jon Ashburn23d36b12016-02-02 17:47:28 -07001371static struct loader_icd *
1372loader_icd_add(struct loader_instance *ptr_inst,
1373 const struct loader_scanned_icds *icd_lib) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001374 struct loader_icd *icd;
1375
Jon Ashburne39a4f82015-08-28 13:38:21 -06001376 icd = loader_icd_create(ptr_inst);
Chia-I Wu13a61a52014-08-04 11:18:20 +08001377 if (!icd)
1378 return NULL;
1379
Jon Ashburn3d002332015-08-20 16:35:30 -06001380 icd->this_icd_lib = icd_lib;
1381 icd->this_instance = ptr_inst;
1382
Chia-I Wu13a61a52014-08-04 11:18:20 +08001383 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -07001384 icd->next = ptr_inst->icds;
1385 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001386 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001387
1388 return icd;
1389}
Jon Ashburn17b4c862016-04-25 11:09:37 -06001390/**
1391 * Determine the ICD interface version to use.
1392 * @param icd
1393 * @param pVersion Output parameter indicating which version to use or 0 if
1394 * the negotiation API is not supported by the ICD
1395 * @return bool indicating true if the selected interface version is supported
1396 * by the loader, false indicates the version is not supported
1397 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1398 * vk_icdNegotiateLoaderICDInterfaceVersion
1399 * version 1 supports vk_icdGetInstanceProcAddr
1400 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1401 */
1402bool loader_get_icd_interface_version(
1403 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1404 uint32_t *pVersion) {
1405
1406 if (fp_negotiate_icd_version == NULL) {
1407 // ICD does not support the negotiation API, it supports version 0 or 1
1408 // calling code must determine if it is version 0 or 1
1409 *pVersion = 0;
1410 } else {
1411 // ICD supports the negotiation API, so call it with the loader's
1412 // latest version supported
1413 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1414 VkResult result = fp_negotiate_icd_version(pVersion);
1415
1416 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1417 // ICD no longer supports the loader's latest interface version so
1418 // fail loading the ICD
1419 return false;
1420 }
1421 }
1422
1423#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1424 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1425 // Loader no longer supports the ICD's latest interface version so fail
1426 // loading the ICD
1427 return false;
1428 }
1429#endif
1430 return true;
1431}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001432
Jon Ashburn23d36b12016-02-02 17:47:28 -07001433void loader_scanned_icd_clear(const struct loader_instance *inst,
1434 struct loader_icd_libs *icd_libs) {
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001435 if (icd_libs->capacity == 0)
1436 return;
1437 for (uint32_t i = 0; i < icd_libs->count; i++) {
1438 loader_platform_close_library(icd_libs->list[i].handle);
Mark Young0ad83132016-06-30 13:02:42 -06001439 loader_instance_heap_free(inst, icd_libs->list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001440 }
Mark Young0ad83132016-06-30 13:02:42 -06001441 loader_instance_heap_free(inst, icd_libs->list);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001442 icd_libs->capacity = 0;
1443 icd_libs->count = 0;
1444 icd_libs->list = NULL;
1445}
1446
Mark Young0ad83132016-06-30 13:02:42 -06001447static VkResult loader_scanned_icd_init(const struct loader_instance *inst,
1448 struct loader_icd_libs *icd_libs) {
1449 VkResult err = VK_SUCCESS;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001450 loader_scanned_icd_clear(inst, icd_libs);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001451 icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
Mark Young0ad83132016-06-30 13:02:42 -06001452 icd_libs->list = loader_instance_heap_alloc(
1453 inst, icd_libs->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1454 if (NULL == icd_libs->list) {
1455 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1456 "realloc failed for layer list when attempting to add new layer");
1457 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1458 }
1459 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001460}
1461
Mark Young3a587792016-08-19 15:25:08 -06001462static VkResult loader_scanned_icd_add(const struct loader_instance *inst,
1463 struct loader_icd_libs *icd_libs,
1464 const char *filename,
1465 uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001466 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001467 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001468 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001469 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001470 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001471 struct loader_scanned_icds *new_node;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001472 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001473 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001474
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001475 /* TODO implement smarter opening/closing of libraries. For now this
1476 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001477 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001478 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001479 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001480 loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001481 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001482 }
1483
Jon Ashburn17b4c862016-04-25 11:09:37 -06001484 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001485 fp_negotiate_icd_version = loader_platform_get_proc_address(
1486 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001487
1488 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001489 &interface_vers)) {
1490 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1491 "ICD (%s) doesn't support interface version compatible"
1492 "with loader, skip this ICD %s",
1493 filename);
Mark Young3a587792016-08-19 15:25:08 -06001494 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001495 }
1496
Jon Ashburn23d36b12016-02-02 17:47:28 -07001497 fp_get_proc_addr =
1498 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001499 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001500 assert(interface_vers == 0);
1501 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001502 fp_get_proc_addr =
1503 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001504 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001505 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1506 loader_platform_get_proc_address_error(
1507 "vk_icdGetInstanceProcAddr"));
Mark Young3a587792016-08-19 15:25:08 -06001508 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001509 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001510 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001511 "Using deprecated ICD interface of "
1512 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001513 "vk_icdGetInstanceProcAddr for ICD %s",
1514 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001515 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001516 fp_create_inst =
1517 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001518 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001519 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1520 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1521 "for ICD %s",
1522 filename);
Mark Young3a587792016-08-19 15:25:08 -06001523 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001524 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001525 fp_get_inst_ext_props = loader_platform_get_proc_address(
1526 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001527 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001528 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1529 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001530 "via dlsym/loadlibrary for ICD %s",
1531 filename);
Mark Young3a587792016-08-19 15:25:08 -06001532 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001533 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001534 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001535 // Use newer interface version 1 or later
1536 if (interface_vers == 0)
1537 interface_vers = 1;
1538
Jon Ashburn23d36b12016-02-02 17:47:28 -07001539 fp_create_inst =
1540 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001541 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001542 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1543 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001544 "vk_icdGetInstanceProcAddr for ICD %s",
1545 filename);
Mark Young3a587792016-08-19 15:25:08 -06001546 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001547 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001548 fp_get_inst_ext_props =
1549 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1550 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001551 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001552 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1553 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001554 "via vk_icdGetInstanceProcAddr for ICD %s",
1555 filename);
Mark Young3a587792016-08-19 15:25:08 -06001556 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001557 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001558 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001559
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001560 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001561 if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
1562 icd_libs->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001563
Mark Young0ad83132016-06-30 13:02:42 -06001564 icd_libs->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001565 inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
1566 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001567 if (NULL == icd_libs->list) {
Mark Young3a587792016-08-19 15:25:08 -06001568 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001569 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1570 "realloc failed on icd library list");
Mark Young3a587792016-08-19 15:25:08 -06001571 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001572 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001573 // double capacity
1574 icd_libs->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001575 }
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001576 new_node = &(icd_libs->list[icd_libs->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001577
1578 new_node->handle = handle;
Jon Ashburn005617f2015-11-17 17:35:40 -07001579 new_node->api_version = api_version;
Jon Ashburnc624c882015-07-16 10:17:29 -06001580 new_node->GetInstanceProcAddr = fp_get_proc_addr;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001581 new_node->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Jon Ashburn46888392015-01-29 15:45:51 -07001582 new_node->CreateInstance = fp_create_inst;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001583 new_node->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001584
Mark Young0ad83132016-06-30 13:02:42 -06001585 new_node->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001586 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001587 if (NULL == new_node->lib_name) {
Mark Young3a587792016-08-19 15:25:08 -06001588 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001589 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001590 "Out of memory can't add icd");
Mark Young3a587792016-08-19 15:25:08 -06001591 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001592 }
1593 strcpy(new_node->lib_name, filename);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001594 icd_libs->count++;
Mark Young3a587792016-08-19 15:25:08 -06001595
1596out:
1597
1598 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001599}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001600
Jon Ashburn23d36b12016-02-02 17:47:28 -07001601static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
1602 const PFN_vkGetInstanceProcAddr fp_gipa) {
1603/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001604
Jon Ashburn23d36b12016-02-02 17:47:28 -07001605#define LOOKUP_GIPA(func, required) \
1606 do { \
1607 icd->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1608 if (!icd->func && required) { \
1609 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001610 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001611 loader_platform_get_proc_address_error("vk" #func)); \
1612 return false; \
1613 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001614 } while (0)
1615
Jon Ashburnc624c882015-07-16 10:17:29 -06001616 LOOKUP_GIPA(GetDeviceProcAddr, true);
1617 LOOKUP_GIPA(DestroyInstance, true);
1618 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1619 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1620 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001621 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001622 LOOKUP_GIPA(CreateDevice, true);
1623 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1624 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001625 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001626 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001627 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001628 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1629 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001630 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001631 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1632 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1633 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001634 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1635 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1636 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1637 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1638 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1639 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1640 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1641 LOOKUP_GIPA(DestroySurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001642#ifdef VK_USE_PLATFORM_WIN32_KHR
1643 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1644#endif
1645#ifdef VK_USE_PLATFORM_XCB_KHR
1646 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1647#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001648#ifdef VK_USE_PLATFORM_XLIB_KHR
1649 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1650#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001651#ifdef VK_USE_PLATFORM_WAYLAND_KHR
1652 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1653#endif
James Jones389dc0c2016-08-18 23:41:19 +01001654 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001655
Jon Ashburnc624c882015-07-16 10:17:29 -06001656#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001657
Jon Ashburnc624c882015-07-16 10:17:29 -06001658 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001659}
1660
Jon Ashburn23d36b12016-02-02 17:47:28 -07001661static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001662 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001663
1664 if (g_loader_debug > 0)
1665 return;
1666
1667 g_loader_debug = 0;
1668
1669 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001670 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001671 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001672 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001673 size_t len;
1674
1675 if (p)
1676 len = p - env;
1677 else
1678 len = strlen(env);
1679
1680 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001681 if (strncmp(env, "all", len) == 0) {
1682 g_loader_debug = ~0u;
1683 g_loader_log_msgs = ~0u;
1684 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001685 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001686 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001687 } else if (strncmp(env, "info", len) == 0) {
1688 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001689 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001690 } else if (strncmp(env, "perf", len) == 0) {
1691 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001692 g_loader_log_msgs |=
1693 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001694 } else if (strncmp(env, "error", len) == 0) {
1695 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001696 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001697 } else if (strncmp(env, "debug", len) == 0) {
1698 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001699 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001700 }
1701 }
1702
1703 if (!p)
1704 break;
1705
1706 env = p + 1;
1707 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001708
Mark Young0ad83132016-06-30 13:02:42 -06001709 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001710}
1711
Jon Ashburn23d36b12016-02-02 17:47:28 -07001712void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001713 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001714 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001715 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001716
1717 // initialize logging
1718 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001719
1720 // initial cJSON to use alloc callbacks
1721 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001722 .malloc_fn = loader_instance_tls_heap_alloc,
1723 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001724 };
1725 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001726}
1727
Jon Ashburn2077e382015-06-29 11:25:34 -06001728struct loader_manifest_files {
1729 uint32_t count;
1730 char **filename_list;
1731};
1732
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001733/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001734 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001735 *
1736 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001737 * A pointer to first char in the next path.
1738 * The next path (or NULL) in the list is returned in next_path.
1739 * Note: input string is modified in some cases. PASS IN A COPY!
1740 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001741static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001742 uint32_t len;
1743 char *next;
1744
1745 if (path == NULL)
1746 return NULL;
1747 next = strchr(path, PATH_SEPERATOR);
1748 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001749 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001750 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001751 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001752 *next = '\0';
1753 next++;
1754 }
1755
1756 return next;
1757}
1758
1759/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001760 * Given a path which is absolute or relative, expand the path if relative or
1761 * leave the path unmodified if absolute. The base path to prepend to relative
1762 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001763 *
1764 * \returns
1765 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001766 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001767static void loader_expand_path(const char *path, const char *rel_base,
1768 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001769 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001770 // do not prepend a base to an absolute path
1771 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001772 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001773
1774 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001775}
1776
1777/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001778 * Given a filename (file) and a list of paths (dir), try to find an existing
1779 * file in the paths. If filename already is a path then no
1780 * searching in the given paths.
1781 *
1782 * \returns
1783 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001784 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001785static void loader_get_fullpath(const char *file, const char *dirs,
1786 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001787 if (!loader_platform_is_path(file) && *dirs) {
1788 char *dirs_copy, *dir, *next_dir;
1789
1790 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1791 strcpy(dirs_copy, dirs);
1792
Jon Ashburn23d36b12016-02-02 17:47:28 -07001793 // find if file exists after prepending paths in given list
1794 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001795 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001796 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1797 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001798 if (loader_platform_file_exists(out_fullpath)) {
1799 return;
1800 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001801 }
1802 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001803
Jon Ashburn2077e382015-06-29 11:25:34 -06001804 snprintf(out_fullpath, out_size, "%s", file);
1805}
1806
1807/**
1808 * Read a JSON file into a buffer.
1809 *
1810 * \returns
1811 * A pointer to a cJSON object representing the JSON parse tree.
1812 * This returned buffer should be freed by caller.
1813 */
Mark Young3a587792016-08-19 15:25:08 -06001814static VkResult loader_get_json(const struct loader_instance *inst,
1815 const char *filename, cJSON **json) {
1816 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001817 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001818 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001819 VkResult res = VK_SUCCESS;
1820
1821 if (NULL == json) {
1822 res = VK_ERROR_INITIALIZATION_FAILED;
1823 goto out;
1824 }
1825
1826 *json = NULL;
1827
Jon Ashburn23d36b12016-02-02 17:47:28 -07001828 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001829 if (!file) {
Mark Young3a587792016-08-19 15:25:08 -06001830 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001831 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1832 "Couldn't open JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001833 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001834 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001835 fseek(file, 0, SEEK_END);
1836 len = ftell(file);
1837 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001838 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001839 if (json_buf == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06001840 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001841 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1842 "Out of memory can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001843 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001844 }
1845 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Young3a587792016-08-19 15:25:08 -06001846 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001847 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1848 "fread failed can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001849 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001850 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001851 json_buf[len] = '\0';
1852
Jon Ashburn23d36b12016-02-02 17:47:28 -07001853 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001854 *json = cJSON_Parse(json_buf);
1855 if (*json == NULL) {
1856 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001857 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1858 "Can't parse JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001859 goto out;
1860 }
1861
1862out:
1863 if (NULL != file) {
1864 fclose(file);
1865 }
1866
1867 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001868}
1869
1870/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001871 * Do a deep copy of the loader_layer_properties structure.
1872 */
Mark Young0ad83132016-06-30 13:02:42 -06001873VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1874 struct loader_layer_properties *dst,
1875 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001876 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001877 memcpy(dst, src, sizeof(*src));
1878 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001879 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1880 src->instance_extension_list.count,
1881 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1882 if (NULL == dst->instance_extension_list.list) {
1883 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1884 "alloc failed for instance extension list");
1885 return VK_ERROR_OUT_OF_HOST_MEMORY;
1886 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001887 dst->instance_extension_list.capacity =
1888 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001889 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001890 dst->instance_extension_list.capacity);
1891 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001892 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1893 src->device_extension_list.count,
1894 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1895 if (NULL == dst->device_extension_list.list) {
1896 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1897 "alloc failed for device extension list");
1898 return VK_ERROR_OUT_OF_HOST_MEMORY;
1899 }
1900 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) *
1901 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001902
Jon Ashburn23d36b12016-02-02 17:47:28 -07001903 dst->device_extension_list.capacity =
1904 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001905 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001906 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001907 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001908 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001909 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001910 dst->device_extension_list.list->entrypoints =
1911 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1912 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1913 if (NULL == dst->device_extension_list.list->entrypoints) {
1914 loader_log(
1915 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1916 "alloc failed for device extension list entrypoint array");
1917 return VK_ERROR_OUT_OF_HOST_MEMORY;
1918 }
1919 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
1920
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001921 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001922 dst->device_extension_list.list->entrypoints[i] =
1923 loader_instance_heap_alloc(
1924 inst,
1925 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1926 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1927 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1928 loader_log(
1929 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1930 "alloc failed for device extension list entrypoint %d", i);
1931 return VK_ERROR_OUT_OF_HOST_MEMORY;
1932 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001933 strcpy(dst->device_extension_list.list->entrypoints[i],
1934 src->device_extension_list.list->entrypoints[i]);
1935 }
1936 }
Mark Young0ad83132016-06-30 13:02:42 -06001937
1938 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001939}
1940
Jon Ashburn86a527a2016-02-10 20:59:26 -07001941static bool
1942loader_find_layer_name_list(const char *name,
1943 const struct loader_layer_list *layer_list) {
1944 if (!layer_list)
1945 return false;
1946 for (uint32_t j = 0; j < layer_list->count; j++)
1947 if (!strcmp(name, layer_list->list[j].info.layerName))
1948 return true;
1949 return false;
1950}
1951
1952static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1953 const char **layer_list) {
1954 if (!layer_list)
1955 return false;
1956 for (uint32_t j = 0; j < layer_count; j++)
1957 if (!strcmp(name, layer_list[j]))
1958 return true;
1959 return false;
1960}
1961
Jon Ashburn491cd042016-05-16 14:01:18 -06001962bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001963 const char *name, uint32_t layer_count,
1964 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1965 if (!layer_list)
1966 return false;
1967 for (uint32_t j = 0; j < layer_count; j++)
1968 if (!strcmp(name, layer_list[j]))
1969 return true;
1970 return false;
1971}
1972
1973/**
1974 * Searches through an array of layer names (ppp_layer_names) looking for a
1975 * layer key_name.
1976 * If not found then simply returns updating nothing.
1977 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12001978 * Any duplicate (pre-existing) expand_names in layer names are removed.
1979 * Order is otherwise preserved, with the layer key_name being replaced by the
1980 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07001981 * @param inst
1982 * @param layer_count
1983 * @param ppp_layer_names
1984 */
Mark Young0ad83132016-06-30 13:02:42 -06001985VkResult loader_expand_layer_names(
1986 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07001987 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06001988 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07001989
Jon Ashburncc407a22016-04-15 09:25:03 -06001990 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001991
Jon Ashburncc407a22016-04-15 09:25:03 -06001992 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06001993 (char const **)pp_src_layers)) {
1994 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06001995 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06001996 }
Jon Ashburn71483442016-02-11 18:59:43 -07001997
Jon Ashburn86a527a2016-02-10 20:59:26 -07001998 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
1999 "Found meta layer %s, replacing with actual layer group",
2000 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12002001
Jon Ashburn491cd042016-05-16 14:01:18 -06002002 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06002003 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06002004 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2005 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002006 if (NULL == pp_dst_layers) {
2007 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2008 "alloc failed for dst layer array");
2009 return VK_ERROR_OUT_OF_HOST_MEMORY;
2010 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002011
2012 // copy layers from src to dst, stripping key_name and anything in
2013 // expand_names.
2014 uint32_t src_index, dst_index = 0;
2015 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002016 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
2017 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002018 continue;
2019 }
2020
2021 if (!strcmp(pp_src_layers[src_index], key_name)) {
2022 // insert all expand_names in place of key_name
2023 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06002024 for (expand_index = 0; expand_index < expand_count;
2025 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002026 pp_dst_layers[dst_index++] = expand_names[expand_index];
2027 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002028 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002029 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002030
2031 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002032 }
2033
Chris Forbesbd9de052016-04-06 20:49:02 +12002034 *ppp_layer_names = pp_dst_layers;
2035 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002036
2037 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002038}
2039
Chris Forbesbd9de052016-04-06 20:49:02 +12002040void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
2041 const VkInstanceCreateInfo *orig,
2042 VkInstanceCreateInfo *ours) {
2043 /* Free the layer names array iff we had to reallocate it */
2044 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002045 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002046 }
2047}
2048
Jon Ashburn491cd042016-05-16 14:01:18 -06002049void loader_init_std_validation_props(struct loader_layer_properties *props) {
2050 memset(props, 0, sizeof(struct loader_layer_properties));
2051 props->type = VK_LAYER_TYPE_META_EXPLICT;
2052 strncpy(props->info.description, "LunarG Standard Validation Layer",
2053 sizeof (props->info.description));
2054 props->info.implementationVersion = 1;
2055 strncpy(props->info.layerName, std_validation_str,
2056 sizeof (props->info.layerName));
2057 // TODO what about specVersion? for now insert loader's built version
2058 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2059}
2060
Jon Ashburn86a527a2016-02-10 20:59:26 -07002061/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002062 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002063 * the set of required layer names. If found then it adds a meta property to the
2064 * layer list.
2065 * Assumes the required layers are the same for both instance and device lists.
2066 * @param inst
2067 * @param layer_count number of layers in layer_names
2068 * @param layer_names array of required layer names
2069 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002070 */
2071static void loader_add_layer_property_meta(
2072 const struct loader_instance *inst, uint32_t layer_count,
2073 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002074 struct loader_layer_list *layer_instance_list) {
2075 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002076 bool found;
2077 struct loader_layer_list *layer_list;
2078
Jon Ashburn491cd042016-05-16 14:01:18 -06002079 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002080 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002081 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002082 return;
2083
Jon Ashburn491cd042016-05-16 14:01:18 -06002084
2085 layer_list = layer_instance_list;
2086
2087 found = true;
2088 if (layer_list == NULL)
2089 return;
2090 for (i = 0; i < layer_count; i++) {
2091 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002092 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002093 found = false;
2094 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002095 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002096
2097 struct loader_layer_properties *props;
2098 if (found) {
2099 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002100 if (NULL == props) {
2101 // Error already triggered in loader_get_next_layer_property.
2102 return;
2103 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002104 loader_init_std_validation_props(props);
2105
2106 }
2107
Jon Ashburn86a527a2016-02-10 20:59:26 -07002108}
2109
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002110static void loader_read_json_layer(
2111 const struct loader_instance *inst,
2112 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2113 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2114 char *temp;
2115 char *name, *type, *library_path, *api_version;
2116 char *implementation_version, *description;
2117 cJSON *ext_item;
2118 VkExtensionProperties ext_prop;
2119
Mark Young0ad83132016-06-30 13:02:42 -06002120/*
2121 * The following are required in the "layer" object:
2122 * (required) "name"
2123 * (required) "type"
2124 * (required) “library_path”
2125 * (required) “api_version”
2126 * (required) “implementation_version”
2127 * (required) “description”
2128 * (required for implicit layers) “disable_environment”
2129 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002130
Jon Ashburn23d36b12016-02-02 17:47:28 -07002131#define GET_JSON_OBJECT(node, var) \
2132 { \
2133 var = cJSON_GetObjectItem(node, #var); \
2134 if (var == NULL) { \
2135 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002136 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002137 "Didn't find required layer object %s in manifest " \
2138 "JSON file, skipping this layer", \
2139 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002140 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002141 } \
2142 }
2143#define GET_JSON_ITEM(node, var) \
2144 { \
2145 item = cJSON_GetObjectItem(node, #var); \
2146 if (item == NULL) { \
2147 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002148 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002149 "Didn't find required layer value %s in manifest JSON " \
2150 "file, skipping this layer", \
2151 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002152 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002153 } \
2154 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002155 if (temp == NULL) { \
2156 layer_node = layer_node->next; \
2157 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2158 "Problem accessing layer value %s in manifest JSON " \
2159 "file, skipping this layer", \
2160 #var); \
2161 return; \
2162 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002163 temp[strlen(temp) - 1] = '\0'; \
2164 var = loader_stack_alloc(strlen(temp) + 1); \
2165 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002166 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002167 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002168 GET_JSON_ITEM(layer_node, name)
2169 GET_JSON_ITEM(layer_node, type)
2170 GET_JSON_ITEM(layer_node, library_path)
2171 GET_JSON_ITEM(layer_node, api_version)
2172 GET_JSON_ITEM(layer_node, implementation_version)
2173 GET_JSON_ITEM(layer_node, description)
2174 if (is_implicit) {
2175 GET_JSON_OBJECT(layer_node, disable_environment)
2176 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002177#undef GET_JSON_ITEM
2178#undef GET_JSON_OBJECT
2179
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002180 // add list entry
2181 struct loader_layer_properties *props = NULL;
2182 if (!strcmp(type, "DEVICE")) {
2183 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2184 "Device layers are deprecated skipping this layer");
2185 layer_node = layer_node->next;
2186 return;
2187 }
2188 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2189 // layers that must work with older loaders
2190 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2191 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002192 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002193 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002194 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002195 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002196 if (NULL == props) {
2197 // Error already triggered in loader_get_next_layer_property.
2198 return;
2199 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002200 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2201 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2202 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002203
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002204 if (props == NULL) {
2205 layer_node = layer_node->next;
2206 return;
2207 }
Jon Ashburn15315172015-07-07 15:06:25 -06002208
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002209 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2210 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2211
2212 char *fullpath = props->lib_name;
2213 char *rel_base;
2214 if (loader_platform_is_path(library_path)) {
2215 // a relative or absolute path
2216 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2217 strcpy(name_copy, filename);
2218 rel_base = loader_platform_dirname(name_copy);
2219 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2220 } else {
2221 // a filename which is assumed in a system directory
2222 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2223 MAX_STRING_SIZE, fullpath);
2224 }
2225 props->info.specVersion = loader_make_version(api_version);
2226 props->info.implementationVersion = atoi(implementation_version);
2227 strncpy((char *)props->info.description, description,
2228 sizeof(props->info.description));
2229 props->info.description[sizeof(props->info.description) - 1] = '\0';
2230 if (is_implicit) {
2231 if (!disable_environment || !disable_environment->child) {
2232 loader_log(
2233 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2234 "Didn't find required layer child value disable_environment"
2235 "in manifest JSON file, skipping this layer");
2236 layer_node = layer_node->next;
2237 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002238 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002239 strncpy(props->disable_env_var.name, disable_environment->child->string,
2240 sizeof(props->disable_env_var.name));
2241 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2242 '\0';
2243 strncpy(props->disable_env_var.value,
2244 disable_environment->child->valuestring,
2245 sizeof(props->disable_env_var.value));
2246 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2247 '\0';
2248 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002249
Jon Ashburn23d36b12016-02-02 17:47:28 -07002250/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002251* Now get all optional items and objects and put in list:
2252* functions
2253* instance_extensions
2254* device_extensions
2255* enable_environment (implicit layers only)
2256*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002257#define GET_JSON_OBJECT(node, var) \
2258 { var = cJSON_GetObjectItem(node, #var); }
2259#define GET_JSON_ITEM(node, var) \
2260 { \
2261 item = cJSON_GetObjectItem(node, #var); \
2262 if (item != NULL) { \
2263 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002264 if (temp != NULL) { \
2265 temp[strlen(temp) - 1] = '\0'; \
2266 var = loader_stack_alloc(strlen(temp) + 1); \
2267 strcpy(var, &temp[1]); \
2268 cJSON_Free(temp); \
2269 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002270 } \
2271 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002272
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002273 cJSON *instance_extensions, *device_extensions, *functions,
2274 *enable_environment;
2275 cJSON *entrypoints;
2276 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2277 char **entry_array;
2278 vkGetInstanceProcAddr = NULL;
2279 vkGetDeviceProcAddr = NULL;
2280 spec_version = NULL;
2281 entrypoints = NULL;
2282 entry_array = NULL;
2283 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002284
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002285 /**
2286 * functions
2287 * vkGetInstanceProcAddr
2288 * vkGetDeviceProcAddr
2289 */
2290 GET_JSON_OBJECT(layer_node, functions)
2291 if (functions != NULL) {
2292 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2293 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2294 if (vkGetInstanceProcAddr != NULL)
2295 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2296 sizeof(props->functions.str_gipa));
2297 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2298 if (vkGetDeviceProcAddr != NULL)
2299 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2300 sizeof(props->functions.str_gdpa));
2301 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2302 }
2303 /**
2304 * instance_extensions
2305 * array of
2306 * name
2307 * spec_version
2308 */
2309 GET_JSON_OBJECT(layer_node, instance_extensions)
2310 if (instance_extensions != NULL) {
2311 int count = cJSON_GetArraySize(instance_extensions);
2312 for (i = 0; i < count; i++) {
2313 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2314 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002315 if (name != NULL) {
2316 strncpy(ext_prop.extensionName, name,
2317 sizeof(ext_prop.extensionName));
2318 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2319 '\0';
2320 }
Mark Young0ad83132016-06-30 13:02:42 -06002321 GET_JSON_ITEM(ext_item, spec_version)
2322 if (NULL != spec_version) {
2323 ext_prop.specVersion = atoi(spec_version);
2324 } else {
2325 ext_prop.specVersion = 0;
2326 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002327 bool ext_unsupported =
2328 wsi_unsupported_instance_extension(&ext_prop);
2329 if (!ext_unsupported) {
2330 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2331 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002332 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002333 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002334 }
2335 /**
2336 * device_extensions
2337 * array of
2338 * name
2339 * spec_version
2340 * entrypoints
2341 */
2342 GET_JSON_OBJECT(layer_node, device_extensions)
2343 if (device_extensions != NULL) {
2344 int count = cJSON_GetArraySize(device_extensions);
2345 for (i = 0; i < count; i++) {
2346 ext_item = cJSON_GetArrayItem(device_extensions, i);
2347 GET_JSON_ITEM(ext_item, name)
2348 GET_JSON_ITEM(ext_item, spec_version)
2349 if (name != NULL) {
2350 strncpy(ext_prop.extensionName, name,
2351 sizeof(ext_prop.extensionName));
2352 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2353 '\0';
2354 }
Mark Young0ad83132016-06-30 13:02:42 -06002355 if (NULL != spec_version) {
2356 ext_prop.specVersion = atoi(spec_version);
2357 } else {
2358 ext_prop.specVersion = 0;
2359 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002360 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2361 GET_JSON_OBJECT(ext_item, entrypoints)
2362 int entry_count;
2363 if (entrypoints == NULL) {
2364 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2365 &ext_prop, 0, NULL);
2366 continue;
2367 }
2368 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002369 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002370 entry_array =
2371 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002372 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002373 for (j = 0; j < entry_count; j++) {
2374 ext_item = cJSON_GetArrayItem(entrypoints, j);
2375 if (ext_item != NULL) {
2376 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002377 if (NULL == temp) {
2378 entry_array[j] = NULL;
2379 continue;
2380 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002381 temp[strlen(temp) - 1] = '\0';
2382 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2383 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002384 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002385 }
2386 }
2387 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2388 &ext_prop, entry_count, entry_array);
2389 }
2390 }
2391 if (is_implicit) {
2392 GET_JSON_OBJECT(layer_node, enable_environment)
2393
2394 // enable_environment is optional
2395 if (enable_environment) {
2396 strncpy(props->enable_env_var.name,
2397 enable_environment->child->string,
2398 sizeof(props->enable_env_var.name));
2399 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2400 '\0';
2401 strncpy(props->enable_env_var.value,
2402 enable_environment->child->valuestring,
2403 sizeof(props->enable_env_var.value));
2404 props->enable_env_var
2405 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2406 }
2407 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002408#undef GET_JSON_ITEM
2409#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002410}
2411
2412/**
2413 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2414 * file, add entry to the layer_list. Fill out the layer_properties in this list
2415 * entry from the input cJSON object.
2416 *
2417 * \returns
2418 * void
2419 * layer_list has a new entry and initialized accordingly.
2420 * If the json input object does not have all the required fields no entry
2421 * is added to the list.
2422 */
2423static void
2424loader_add_layer_properties(const struct loader_instance *inst,
2425 struct loader_layer_list *layer_instance_list,
2426 cJSON *json, bool is_implicit, char *filename) {
2427 /* Fields in layer manifest file that are required:
2428 * (required) “file_format_version”
2429 *
Mark Young0ad83132016-06-30 13:02:42 -06002430 * If more than one "layer" object are to be used, use the "layers" array
2431 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002432 *
2433 * First get all required items and if any missing abort
2434 */
2435
2436 cJSON *item, *layers_node, *layer_node;
2437 uint16_t file_major_vers = 0;
2438 uint16_t file_minor_vers = 0;
2439 uint16_t file_patch_vers = 0;
2440 char *vers_tok;
2441 cJSON *disable_environment = NULL;
2442 item = cJSON_GetObjectItem(json, "file_format_version");
2443 if (item == NULL) {
2444 return;
2445 }
2446 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002447 if (NULL == file_vers) {
2448 return;
2449 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002450 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2451 "Found manifest file %s, version %s", filename, file_vers);
2452 // Get the major/minor/and patch as integers for easier comparison
2453 vers_tok = strtok(file_vers, ".\"\n\r");
2454 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002455 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002456 vers_tok = strtok(NULL, ".\"\n\r");
2457 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002458 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002459 vers_tok = strtok(NULL, ".\"\n\r");
2460 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002461 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002462 }
2463 }
2464 }
2465 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2466 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2467 "%s Unexpected manifest file version (expected 1.0.0 or "
2468 "1.0.1), may cause errors",
2469 filename);
2470 }
Mark Young0ad83132016-06-30 13:02:42 -06002471 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002472 // If "layers" is present, read in the array of layer objects
2473 layers_node = cJSON_GetObjectItem(json, "layers");
2474 if (layers_node != NULL) {
2475 int numItems = cJSON_GetArraySize(layers_node);
2476 if (file_major_vers == 1 && file_minor_vers == 0 &&
2477 file_patch_vers == 0) {
2478 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2479 "\"layers\" tag not officially added until file version "
2480 "1.0.1, but %s is reporting version %s",
2481 filename, file_vers);
2482 }
2483 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2484 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2485 if (layer_node == NULL) {
2486 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2487 "Can't find \"layers\" array element %d object in "
2488 "manifest JSON file %s, skipping this file",
2489 curLayer, filename);
2490 return;
2491 }
2492 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2493 disable_environment, is_implicit, filename);
2494 }
2495 } else {
2496 // Otherwise, try to read in individual layers
2497 layer_node = cJSON_GetObjectItem(json, "layer");
2498 if (layer_node == NULL) {
2499 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2500 "Can't find \"layer\" object in manifest JSON file %s, "
2501 "skipping this file",
2502 filename);
2503 return;
2504 }
2505 // Loop through all "layer" objects in the file to get a count of them
2506 // first.
2507 uint16_t layer_count = 0;
2508 cJSON *tempNode = layer_node;
2509 do {
2510 tempNode = tempNode->next;
2511 layer_count++;
2512 } while (tempNode != NULL);
2513 /*
2514 * Throw a warning if we encounter multiple "layer" objects in file
2515 * versions newer than 1.0.0. Having multiple objects with the same
2516 * name at the same level is actually a JSON standard violation.
2517 */
2518 if (layer_count > 1 &&
2519 (file_major_vers > 1 ||
2520 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2521 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2522 "Multiple \"layer\" nodes are deprecated starting in "
2523 "file version \"1.0.1\". Please use \"layers\" : [] "
2524 "array instead in %s.",
2525 filename);
2526 } else {
2527 do {
2528 loader_read_json_layer(inst, layer_instance_list, layer_node,
2529 item, disable_environment, is_implicit,
2530 filename);
2531 layer_node = layer_node->next;
2532 } while (layer_node != NULL);
2533 }
2534 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002535 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002536}
2537
2538/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002539 * Find the Vulkan library manifest files.
2540 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002541 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002542 * for a list of JSON manifest files. If env_override is non-NULL
2543 * and has a valid value. Then the location is ignored. Otherwise
2544 * location is used to look for manifest files. The location
2545 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002546 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002547 * directory to look at. It is expanded into the dir path
2548 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2549 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002550 *
2551 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002552 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002553 * A string list of manifest files to be opened in out_files param.
2554 * List has a pointer to string for each manifest filename.
2555 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002556 * Location or override string lists can be either files or directories as
2557 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002558 * | location | override
2559 * --------------------------------
2560 * Win ICD | files | files
2561 * Win Layer | files | dirs
2562 * Linux ICD | dirs | files
2563 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002564 */
Mark Young0ad83132016-06-30 13:02:42 -06002565static VkResult loader_get_manifest_files(
2566 const struct loader_instance *inst, const char *env_override,
2567 char *source_override, bool is_layer, const char *location,
2568 const char *home_location, struct loader_manifest_files *out_files) {
2569 char * override = NULL;
2570 char *loc, *orig_loc = NULL;
2571 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002572 char *file, *next_file, *name;
2573 size_t alloced_count = 64;
2574 char full_path[2048];
2575 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002576 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002577 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002578 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002579
2580 out_files->count = 0;
2581 out_files->filename_list = NULL;
2582
Jamie Madill00c3c912016-04-06 18:26:46 -04002583 if (source_override != NULL) {
2584 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002585 } else if (env_override != NULL &&
2586 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002587#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002588 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002589 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002590 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002591 override = NULL;
2592 }
2593#endif
2594 }
2595
Jon Ashburnb6822212016-02-16 15:34:16 -07002596#if !defined(_WIN32)
2597 if (location == NULL && home_location == NULL) {
2598#else
2599 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002600 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002601#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002602 loader_log(
2603 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002604 "Can't get manifest files with NULL location, env_override=%s",
2605 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002606 res = VK_ERROR_INITIALIZATION_FAILED;
2607 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002608 }
2609
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002610#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002611 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002612#else
2613 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002614#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002615 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002616 // Also handle getting the location(s) from registry on Windows
2617 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002618 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002619 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002620 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2621 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002622 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2623 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002624 }
2625 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002626#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002627 reg = loader_get_registry_files(inst, loc);
2628 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002629 if (!is_layer) {
2630 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2631 "Registry lookup failed can't get ICD manifest "
2632 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002633 // This typically only fails when out of memory, which is
2634 // critical
2635 // if this is for the loader.
2636 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002637 } else {
2638 // warning only for layers
2639 loader_log(
2640 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2641 "Registry lookup failed can't get layer manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002642 // Return success for now since it's not critical for layers
2643 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002644 }
Mark Young0ad83132016-06-30 13:02:42 -06002645 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002646 }
Mark Young0ad83132016-06-30 13:02:42 -06002647 orig_loc = loc;
2648 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002649#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002650 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002651 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002652 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002653 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2654 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002655 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2656 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002657 }
2658 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002659 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002660 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002661 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002662 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002663
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002664 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002665 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2666 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002667
Jon Ashburn2077e382015-06-29 11:25:34 -06002668 file = loc;
2669 while (*file) {
2670 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002671 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002672 sysdir = opendir(file);
2673 name = NULL;
2674 if (sysdir) {
2675 dent = readdir(sysdir);
2676 if (dent == NULL)
2677 break;
2678 name = &(dent->d_name[0]);
2679 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2680 name = full_path;
2681 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002682 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002683#if defined(_WIN32)
2684 name = file;
2685#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002686 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002687 char *dir;
2688 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002689 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002690 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002691 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2692 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002693 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002694 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002695 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002696
2697 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2698
2699 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002700#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002701 }
2702 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002703 /* Look for files ending with ".json" suffix */
2704 uint32_t nlen = (uint32_t)strlen(name);
2705 const char *suf = name + nlen - 5;
2706 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2707 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002708 out_files->filename_list = loader_instance_heap_alloc(
2709 inst, alloced_count * sizeof(char *),
2710 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002711 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002712 out_files->filename_list = loader_instance_heap_realloc(
2713 inst, out_files->filename_list,
2714 alloced_count * sizeof(char *),
2715 alloced_count * sizeof(char *) * 2,
2716 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002717 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002718 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002719 if (out_files->filename_list == NULL) {
2720 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2721 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002722 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2723 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002724 }
Mark Young0ad83132016-06-30 13:02:42 -06002725 out_files->filename_list[out_files->count] =
2726 loader_instance_heap_alloc(
2727 inst, strlen(name) + 1,
2728 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002729 if (out_files->filename_list[out_files->count] == NULL) {
2730 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2731 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002732 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2733 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002734 }
2735 strcpy(out_files->filename_list[out_files->count], name);
2736 out_files->count++;
2737 } else if (!list_is_dirs) {
2738 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002739 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002740 "Skipping manifest file %s, file name must end in .json",
2741 name);
2742 }
2743 if (list_is_dirs) {
2744 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002745 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002746 break;
Mark Young0ad83132016-06-30 13:02:42 -06002747 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002748 name = &(dent->d_name[0]);
2749 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2750 name = full_path;
2751 } else {
2752 break;
2753 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002754 }
Mark Young0ad83132016-06-30 13:02:42 -06002755 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002756 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002757 sysdir = NULL;
2758 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002759 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002760#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002761 if (home_location != NULL &&
2762 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002763 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2764 size_t len;
2765 if (xdgdatahome != NULL) {
2766
2767 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002768 strlen(home_location));
2769 if (home_loc == NULL) {
2770 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002771 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002772 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2773 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002774 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002775 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002776 // Add directory separator if needed
2777 if (home_location[0] != DIRECTORY_SYMBOL) {
2778 len = strlen(home_loc);
2779 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002780 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002781 }
2782 strcat(home_loc, home_location);
2783 file = home_loc;
2784 next_file = loader_get_next_path(file);
2785 home_location = NULL;
2786
Jon Ashburn1530c342016-02-26 13:14:27 -07002787 loader_log(
2788 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002789 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002790 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002791 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002792
2793 } else {
2794
2795 char *home = secure_getenv("HOME");
2796 if (home != NULL) {
2797 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2798 strlen(home_location));
2799 if (home_loc == NULL) {
2800 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2801 "Out of memory can't get manifest files");
2802 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2803 goto out;
2804 }
2805 strcpy(home_loc, home);
2806
2807 len = strlen(home);
2808 if (home[len] != DIRECTORY_SYMBOL) {
2809 home_loc[len] = DIRECTORY_SYMBOL;
2810 home_loc[len + 1] = '\0';
2811 }
2812 strcat(home_loc, ".local/share");
2813
2814 if (home_location[0] != DIRECTORY_SYMBOL) {
2815 len = strlen(home_loc);
2816 home_loc[len] = DIRECTORY_SYMBOL;
2817 home_loc[len + 1] = '\0';
2818 }
2819 strcat(home_loc, home_location);
2820 file = home_loc;
2821 next_file = loader_get_next_path(file);
2822 home_location = NULL;
2823
2824 loader_log(
2825 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2826 "Searching the following path for manifest files: %s\n",
2827 home_loc);
2828 list_is_dirs = true;
2829 } else {
2830 // without knowing HOME, we just.. give up
2831 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002832 }
2833 }
2834#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002835 }
Mark Young0ad83132016-06-30 13:02:42 -06002836
2837out:
2838 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2839 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2840 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2841 }
2842 loader_instance_heap_free(inst, out_files->filename_list);
2843 out_files->count = 0;
2844 out_files->filename_list = NULL;
2845 }
2846
2847 if (NULL != sysdir) {
2848 closedir(sysdir);
2849 }
2850
2851 if (NULL != reg && reg != orig_loc) {
2852 loader_instance_heap_free(inst, reg);
2853 }
2854 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002855}
2856
Jon Ashburn23d36b12016-02-02 17:47:28 -07002857void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002858
Jon Ashburn23d36b12016-02-02 17:47:28 -07002859void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002860/**
2861 * Try to find the Vulkan ICD driver(s).
2862 *
2863 * This function scans the default system loader path(s) or path
2864 * specified by the \c VK_ICD_FILENAMES environment variable in
2865 * order to find loadable VK ICDs manifest files. From these
2866 * manifest files it finds the ICD libraries.
2867 *
2868 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002869 * Vulkan result
2870 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002871 */
Mark Young0ad83132016-06-30 13:02:42 -06002872VkResult loader_icd_scan(const struct loader_instance *inst,
2873 struct loader_icd_libs *icds) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002874 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002875 uint16_t file_major_vers = 0;
2876 uint16_t file_minor_vers = 0;
2877 uint16_t file_patch_vers = 0;
2878 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002879 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002880 VkResult res = VK_SUCCESS;
2881 bool lockedMutex = false;
2882 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002883 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002884
Mark Young0ad83132016-06-30 13:02:42 -06002885 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2886
2887 res = loader_scanned_icd_init(inst, icds);
2888 if (VK_SUCCESS != res) {
2889 goto out;
2890 }
2891
Jon Ashburn2077e382015-06-29 11:25:34 -06002892 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002893 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
2894 DEFAULT_VK_DRIVERS_INFO,
2895 HOME_VK_DRIVERS_INFO, &manifest_files);
2896 if (VK_SUCCESS != res || manifest_files.count == 0) {
2897 goto out;
2898 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002899 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002900 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002901 for (uint32_t i = 0; i < manifest_files.count; i++) {
2902 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002903 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002904 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002905 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002906
Mark Young3a587792016-08-19 15:25:08 -06002907 res = loader_get_json(inst, file_str, &json);
2908 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2909 break;
2910 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002911 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002912 }
Mark Young3a587792016-08-19 15:25:08 -06002913
Jon Ashburn005617f2015-11-17 17:35:40 -07002914 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002915 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002916 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002917 if (num_good_icds == 0) {
2918 res = VK_ERROR_INITIALIZATION_FAILED;
2919 }
2920 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002921 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002922 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002923 if (NULL == file_vers) {
2924 // Only reason the print can fail is if there was an allocation
2925 // issue
Mark Young3a587792016-08-19 15:25:08 -06002926 if (num_good_icds == 0) {
2927 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2928 }
2929 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002930 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002931 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2932 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002933 // Get the major/minor/and patch as integers for easier comparison
2934 vers_tok = strtok(file_vers, ".\"\n\r");
2935 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002936 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002937 vers_tok = strtok(NULL, ".\"\n\r");
2938 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002939 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002940 vers_tok = strtok(NULL, ".\"\n\r");
2941 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002942 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002943 }
2944 }
2945 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002946 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002947 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002948 "Unexpected manifest file version (expected 1.0.0 or "
2949 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002950 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002951 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002952 itemICD = cJSON_GetObjectItem(json, "ICD");
2953 if (itemICD != NULL) {
2954 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002955 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002956 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002957 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002958 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002959 "Can't find \"library_path\" in ICD JSON file "
2960 "%s, skipping",
2961 file_str);
Mark Young3a587792016-08-19 15:25:08 -06002962 if (num_good_icds == 0) {
2963 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2964 }
Mark Young0ad83132016-06-30 13:02:42 -06002965 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002966 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002967 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002968 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06002969 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002970 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06002971 temp[strlen(temp) - 1] = '\0';
2972 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06002973 if (NULL == library_path) {
2974 loader_log(
2975 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2976 "Can't allocate space for \"library_path\" in ICD "
2977 "JSON file %s, skipping",
2978 file_str);
2979 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2980 cJSON_Free(temp);
2981 cJSON_Delete(json);
2982 json = NULL;
2983 goto out;
2984 }
Jon Ashburn86251302015-08-25 16:48:24 -06002985 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002986 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06002987 if (strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002988 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002989 "Can't find \"library_path\" in ICD JSON file "
2990 "%s, skipping",
2991 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06002992 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002993 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002994 continue;
2995 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04002996 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06002997 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002998 loader_log(
2999 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04003000 "Searching for ICD drivers named %s default dir %s\n",
3001 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003002 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003003 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003004 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3005 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003006 strcpy(name_copy, file_str);
3007 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003008 loader_expand_path(library_path, rel_base, sizeof(fullpath),
3009 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003010 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003011 // a filename which is assumed in a system directory
3012 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
3013 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003014 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003015
3016 uint32_t vers = 0;
3017 item = cJSON_GetObjectItem(itemICD, "api_version");
3018 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003019 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003020 if (NULL == temp) {
3021 // Only reason the print can fail is if there was an
3022 // allocation issue
3023 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3024 goto out;
3025 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003026 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003027 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07003028 }
Mark Young3a587792016-08-19 15:25:08 -06003029 res = loader_scanned_icd_add(inst, icds, fullpath, vers);
3030 if (VK_SUCCESS != res) {
3031 goto out;
3032 }
3033 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003034 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003035 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003036 "Can't find \"library_path\" object in ICD JSON "
3037 "file %s, skipping",
3038 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003039 }
3040 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003041 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003042 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003043 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
3044 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003045 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003046
Mark Young0ad83132016-06-30 13:02:42 -06003047 cJSON_Delete(json);
3048 json = NULL;
3049 }
3050
3051out:
3052 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003053 cJSON_Delete(json);
3054 }
Mark Young0ad83132016-06-30 13:02:42 -06003055 if (NULL != manifest_files.filename_list) {
3056 for (uint32_t i = 0; i < manifest_files.count; i++) {
3057 if (NULL != manifest_files.filename_list[i]) {
3058 loader_instance_heap_free(inst,
3059 manifest_files.filename_list[i]);
3060 }
3061 }
3062 loader_instance_heap_free(inst, manifest_files.filename_list);
3063 }
3064 if (lockedMutex) {
3065 loader_platform_thread_unlock_mutex(&loader_json_lock);
3066 }
3067 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003068}
3069
Jon Ashburn23d36b12016-02-02 17:47:28 -07003070void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003071 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003072 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003073 struct loader_manifest_files
3074 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003075 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003076 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003077 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003078
Mark Young0ad83132016-06-30 13:02:42 -06003079 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3080
3081 // Get a list of manifest files for explicit layers
3082 if (VK_SUCCESS !=
3083 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
3084 true, DEFAULT_VK_ELAYERS_INFO,
3085 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3086 goto out;
3087 }
3088
3089 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003090 // Pass NULL for environment variable override - implicit layers are not
3091 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003092 if (VK_SUCCESS != loader_get_manifest_files(
3093 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
3094 HOME_VK_ILAYERS_INFO, &manifest_files[1])) {
3095 goto out;
3096 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003097
Mark Young0ad83132016-06-30 13:02:42 -06003098 // Make sure we have at least one layer, if not, go ahead and return
3099 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3100 goto out;
3101 }
3102
3103 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003104 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003105
Jon Ashburn6461ef22015-09-22 13:11:00 -06003106 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003107 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003108 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003109 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003110 file_str = manifest_files[implicit].filename_list[i];
3111 if (file_str == NULL)
3112 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003113
Jon Ashburn075ce432015-12-17 17:38:24 -07003114 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003115 VkResult res = loader_get_json(inst, file_str, &json);
3116 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3117 break;
3118 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003119 continue;
3120 }
3121
Jon Ashburn491cd042016-05-16 14:01:18 -06003122 loader_add_layer_properties(inst, instance_layers, json,
3123 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003124 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003125 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003126 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003127
3128 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003129 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3130 sizeof(std_validation_names[0]),
3131 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003132
Mark Young0ad83132016-06-30 13:02:42 -06003133out:
3134
3135 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3136 if (NULL != manifest_files[manFile].filename_list) {
3137 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3138 if (NULL != manifest_files[manFile].filename_list[i]) {
3139 loader_instance_heap_free(
3140 inst, manifest_files[manFile].filename_list[i]);
3141 }
3142 }
3143 loader_instance_heap_free(inst,
3144 manifest_files[manFile].filename_list);
3145 }
3146 }
3147 if (lockedMutex) {
3148 loader_platform_thread_unlock_mutex(&loader_json_lock);
3149 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003150}
3151
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003152void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003153 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003154 char *file_str;
3155 struct loader_manifest_files manifest_files;
3156 cJSON *json;
3157 uint32_t i;
3158
3159 // Pass NULL for environment variable override - implicit layers are not
3160 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003161 VkResult res = loader_get_manifest_files(
3162 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
3163 &manifest_files);
3164 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003165 return;
3166 }
3167
3168 /* cleanup any previously scanned libraries */
3169 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003170
3171 loader_platform_thread_lock_mutex(&loader_json_lock);
3172
3173 for (i = 0; i < manifest_files.count; i++) {
3174 file_str = manifest_files.filename_list[i];
3175 if (file_str == NULL) {
3176 continue;
3177 }
3178
3179 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003180 res = loader_get_json(inst, file_str, &json);
3181 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3182 break;
3183 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003184 continue;
3185 }
3186
Mark Young0ad83132016-06-30 13:02:42 -06003187 loader_add_layer_properties(inst, instance_layers, json, true,
3188 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003189
Mark Young0ad83132016-06-30 13:02:42 -06003190 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003191 cJSON_Delete(json);
3192 }
Mark Young0ad83132016-06-30 13:02:42 -06003193 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003194
3195 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003196 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3197 sizeof(std_validation_names[0]),
3198 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003199
3200 loader_platform_thread_unlock_mutex(&loader_json_lock);
3201}
3202
Jon Ashburn23d36b12016-02-02 17:47:28 -07003203static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3204loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003205 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003206 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003207 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003208 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003209 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003210 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003211
Jon Ashburn27cd5842015-05-12 17:26:48 -06003212 // inst is not wrapped
3213 if (inst == VK_NULL_HANDLE) {
3214 return NULL;
3215 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003216 VkLayerInstanceDispatchTable *disp_table =
3217 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003218 void *addr;
3219
3220 if (disp_table == NULL)
3221 return NULL;
3222
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003223 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003224 addr =
3225 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003226 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003227 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003228 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003229
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003230 // Don't call down the chain, this would be an infinite loop
3231 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003232 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003233 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003234}
3235
Piers Daniell295fe402016-03-29 11:51:11 -06003236static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003237loader_gpa_device_internal(VkDevice device, const char *pName) {
3238 struct loader_device *dev;
3239 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
Piers Daniell295fe402016-03-29 11:51:11 -06003240 return icd->GetDeviceProcAddr(device, pName);
3241}
3242
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003243/**
3244 * Initialize device_ext dispatch table entry as follows:
3245 * If dev == NULL find all logical devices created within this instance and
3246 * init the entry (given by idx) in the ext dispatch table.
3247 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003248 * The initialization value is gotten by calling down the device chain with
3249 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003250 * If GDPA returns NULL then don't initialize the dispatch table entry.
3251 */
3252static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003253 struct loader_device *dev,
3254 uint32_t idx,
3255 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003256
Jon Ashburn23d36b12016-02-02 17:47:28 -07003257{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003258 void *gdpa_value;
3259 if (dev != NULL) {
3260 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003261 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003262 if (gdpa_value != NULL)
Jon Ashburn23d36b12016-02-02 17:47:28 -07003263 dev->loader_dispatch.ext_dispatch.DevExt[idx] =
3264 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003265 } else {
3266 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
3267 struct loader_icd *icd = &inst->icds[i];
Karl Schultz2558bd32016-02-24 14:39:39 -07003268 struct loader_device *ldev = icd->logical_device_list;
3269 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003270 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003271 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3272 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003273 if (gdpa_value != NULL)
Karl Schultz2558bd32016-02-24 14:39:39 -07003274 ldev->loader_dispatch.ext_dispatch.DevExt[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003275 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003276 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003277 }
3278 }
3279 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003280}
3281
3282/**
3283 * Find all dev extension in the hash table and initialize the dispatch table
3284 * for dev for each of those extension entrypoints found in hash table.
3285
3286 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003287void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3288 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003289 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3290 if (inst->disp_hash[i].func_name != NULL)
3291 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3292 inst->disp_hash[i].func_name);
3293 }
3294}
3295
3296static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003297 const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003298 struct loader_icd *icd;
3299 icd = inst->icds;
3300 while (icd) {
3301 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
3302 // this icd supports funcName
3303 return true;
3304 icd = icd->next;
3305 }
3306
3307 return false;
3308}
3309
Jon Ashburncc407a22016-04-15 09:25:03 -06003310static bool loader_check_layer_list_for_address(
3311 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003312 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003313 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003314 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003315 const struct loader_device_extension_list *const extensions =
3316 &(layers->list[layer].device_extension_list);
3317 for (uint32_t extension = 0; extension < extensions->count;
3318 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003319 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003320 const struct loader_dev_ext_props *const property =
3321 &(extensions->list[extension]);
3322 for (uint32_t entry = 0; entry < property->entrypoint_count;
3323 ++entry) {
3324 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003325 return true;
3326 }
3327 }
3328 }
3329 }
3330
3331 return false;
3332}
3333
Jon Ashburncc407a22016-04-15 09:25:03 -06003334static bool
3335loader_check_layers_for_address(const struct loader_instance *const inst,
3336 const char *funcName) {
3337 if (loader_check_layer_list_for_address(&inst->instance_layer_list,
3338 funcName)) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003339 return true;
3340 }
3341
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003342 return false;
3343}
3344
Jon Ashburn23d36b12016-02-02 17:47:28 -07003345static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003346 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003347 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3348 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003349 }
3350 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3351}
3352
3353static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003354 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003355 uint32_t i;
3356 uint32_t idx = *ptr_idx;
3357 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3358
3359 if (!inst->disp_hash[idx].func_name) {
3360 // no entry here at this idx, so use it
3361 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003362 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003363 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003364 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003365 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003366 "loader_add_dev_ext_table() can't allocate memory for "
3367 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003368 return false;
3369 }
3370 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3371 return true;
3372 }
3373
3374 // check for enough capacity
3375 if (list->capacity == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06003376 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003377 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3378 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003379 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003380 "loader_add_dev_ext_table() can't allocate list memory");
3381 return false;
3382 }
3383 list->capacity = 8 * sizeof(*(list->index));
3384 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0ad83132016-06-30 13:02:42 -06003385 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003386 list->capacity * 2,
3387 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003388 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003389 loader_log(
3390 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3391 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003392 return false;
3393 }
3394 list->capacity *= 2;
3395 }
3396
Jon Ashburn23d36b12016-02-02 17:47:28 -07003397 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003398 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3399 do {
3400 if (!inst->disp_hash[i].func_name) {
3401 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003402 inst->disp_hash[i].func_name =
Mark Young0ad83132016-06-30 13:02:42 -06003403 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003404 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003405 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003406 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003407 "loader_add_dev_ext_table() can't rallocate "
3408 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003409 return false;
3410 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003411 strncpy(inst->disp_hash[i].func_name, funcName,
3412 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003413 list->index[list->count] = i;
3414 list->count++;
3415 *ptr_idx = i;
3416 return true;
3417 }
3418 i = (i + 1) % MAX_NUM_DEV_EXTS;
3419 } while (i != idx);
3420
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003421 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003422 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3423 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003424 return false;
3425}
3426
3427static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003428 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003429 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003430 if (inst->disp_hash[*idx].func_name &&
3431 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003432 return true;
3433
3434 // funcName wasn't at the primary spot in the hash table
3435 // search the list of secondary locations (shallow search, not deep search)
3436 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3437 alt_idx = inst->disp_hash[*idx].list.index[i];
3438 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3439 *idx = alt_idx;
3440 return true;
3441 }
3442 }
3443
3444 return false;
3445}
3446
3447/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003448 * This function returns generic trampoline code address for unknown entry
3449 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003450 * Presumably, these unknown entry points (as given by funcName) are device
3451 * extension entrypoints. A hash table is used to keep a list of unknown entry
3452 * points and their mapping to the device extension dispatch table
3453 * (struct loader_dev_ext_dispatch_table).
3454 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003455 * For a given entry point string (funcName), if an existing mapping is found
3456 * the
3457 * trampoline address for that mapping is returned. Otherwise, this unknown
3458 * entry point
3459 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3460 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003461 * new entry in the hash table is initialized and that trampoline address for
3462 * the new entry is returned. Null is returned if the hash table is full or
3463 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3464 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003465void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003466 uint32_t idx;
3467 uint32_t seed = 0;
3468
3469 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3470
3471 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3472 // found funcName already in hash
3473 return loader_get_dev_ext_trampoline(idx);
3474
3475 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003476 if (!loader_check_icds_for_address(inst, funcName) &&
3477 !loader_check_layers_for_address(inst, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003478 // if support found in layers continue on
3479 return NULL;
3480 }
3481
3482 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3483 // successfully added new table entry
3484 // init any dev dispatch table entrys as needed
3485 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3486 return loader_get_dev_ext_trampoline(idx);
3487 }
3488
3489 return NULL;
3490}
3491
Jon Ashburn23d36b12016-02-02 17:47:28 -07003492struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003493 /* look up the loader_instance in our list by comparing dispatch tables, as
3494 * there is no guarantee the instance is still a loader_instance* after any
3495 * layers which wrap the instance object.
3496 */
3497 const VkLayerInstanceDispatchTable *disp;
3498 struct loader_instance *ptr_instance = NULL;
3499 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003500 for (struct loader_instance *inst = loader.instances; inst;
3501 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003502 if (inst->disp == disp) {
3503 ptr_instance = inst;
3504 break;
3505 }
3506 }
3507 return ptr_instance;
3508}
3509
Jon Ashburn23d36b12016-02-02 17:47:28 -07003510static loader_platform_dl_handle
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003511loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3512 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003513
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003514 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003515 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003516 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003517 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003518 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003519 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003520 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003521 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003522 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003523
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003524 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003525}
3526
Jon Ashburn23d36b12016-02-02 17:47:28 -07003527static void
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003528loader_close_layer_lib(const struct loader_instance *inst,
3529 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003530
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003531 if (prop->lib_handle) {
3532 loader_platform_close_library(prop->lib_handle);
3533 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3534 "Unloading layer library %s", prop->lib_name);
3535 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003536 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003537}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003538
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003539void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003540 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003541 struct loader_layer_list *list) {
3542 /* delete instance list of enabled layers and close any layer libraries */
3543 for (uint32_t i = 0; i < list->count; i++) {
3544 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003545
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003546 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003547 }
Mark Young0ad83132016-06-30 13:02:42 -06003548 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003549}
3550
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003551/**
3552 * Go through the search_list and find any layers which match type. If layer
3553 * type match is found in then add it to ext_list.
3554 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003555static void
3556loader_add_layer_implicit(const struct loader_instance *inst,
3557 const enum layer_type type,
3558 struct loader_layer_list *list,
3559 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003560 bool enable;
3561 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003562 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003563 for (i = 0; i < search_list->count; i++) {
3564 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003565 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003566 /* Found an implicit layer, see if it should be enabled */
3567 enable = false;
3568
Jon Ashburn23d36b12016-02-02 17:47:28 -07003569 // if no enable_environment variable is specified, this implicit
3570 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003571 // should always be enabled. Otherwise check if the variable is set
3572 if (prop->enable_env_var.name[0] == 0) {
3573 enable = true;
3574 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003575 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003576 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3577 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003578 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003579 }
3580
3581 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003582 // environment variables are set, the layer is disabled. Implicit
3583 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003584 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003585 env_value = loader_getenv(prop->disable_env_var.name, inst);
3586 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003587 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003588 }
3589 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003590
Mark Young0ad83132016-06-30 13:02:42 -06003591 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003592 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003593 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003594 }
3595 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003596}
3597
3598/**
3599 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003600 * is found in search_list then add it to layer_list. But only add it to
3601 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003602 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003603static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003604 const enum layer_type type,
3605 const char *env_name,
3606 struct loader_layer_list *layer_list,
3607 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003608 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003609 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003610
Mark Young0ad83132016-06-30 13:02:42 -06003611 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003612 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003613 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003614 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003615 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003616 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003617 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003618 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003619 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003620
Mark Young0ad83132016-06-30 13:02:42 -06003621 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003622
Jon Ashburn23d36b12016-02-02 17:47:28 -07003623 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003624 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003625 if (!strcmp(std_validation_str, name)) {
3626 /* add meta list of layers
3627 don't attempt to remove duplicate layers already added by app or
3628 env var
3629 */
3630 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3631 "Expanding meta layer %s found in environment variable",
3632 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003633 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3634 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003635 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3636 sizeof(std_validation_names[0]);
3637 i++) {
3638 loader_find_layer_name_add_list(inst, std_validation_names[i],
3639 type, search_list, layer_list);
3640 }
3641 } else {
3642 loader_find_layer_name_add_list(inst, name, type, search_list,
3643 layer_list);
3644 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003645 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003646 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003647
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003648 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003649}
3650
Jon Ashburn23d36b12016-02-02 17:47:28 -07003651VkResult
3652loader_enable_instance_layers(struct loader_instance *inst,
3653 const VkInstanceCreateInfo *pCreateInfo,
3654 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003655 VkResult err;
3656
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003657 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003658
Jon Ashburne39a4f82015-08-28 13:38:21 -06003659 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003660 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3661 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003662 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003663 }
3664
Jon Ashburn0c26e712015-07-02 16:10:32 -06003665 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003666 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3667 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003668
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003669 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003670 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3671 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003672 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003673
3674 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003675 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003676 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3677 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003678
3679 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003680}
3681
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003682/*
3683 * Given the list of layers to activate in the loader_instance
3684 * structure. This function will add a VkLayerInstanceCreateInfo
3685 * structure to the VkInstanceCreateInfo.pNext pointer.
3686 * Each activated layer will have it's own VkLayerInstanceLink
3687 * structure that tells the layer what Get*ProcAddr to call to
3688 * get function pointers to the next layer down.
3689 * Once the chain info has been created this function will
3690 * execute the CreateInstance call chain. Each layer will
3691 * then have an opportunity in it's CreateInstance function
3692 * to setup it's dispatch table when the lower layer returns
3693 * successfully.
3694 * Each layer can wrap or not-wrap the returned VkInstance object
3695 * as it sees fit.
3696 * The instance chain is terminated by a loader function
3697 * that will call CreateInstance on all available ICD's and
3698 * cache those VkInstance objects for future use.
3699 */
3700VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003701 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003702 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003703 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003704 uint32_t activated_layers = 0;
3705 VkLayerInstanceCreateInfo chain_info;
3706 VkLayerInstanceLink *layer_instance_link_info = NULL;
3707 VkInstanceCreateInfo loader_create_info;
3708 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003709
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003710 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3711 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003712
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003713 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003714
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003715 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003716
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003717 chain_info.u.pLayerInfo = NULL;
3718 chain_info.pNext = pCreateInfo->pNext;
3719 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3720 chain_info.function = VK_LAYER_LINK_INFO;
3721 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003722
Jon Ashburn23d36b12016-02-02 17:47:28 -07003723 layer_instance_link_info = loader_stack_alloc(
3724 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003725 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003726 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3727 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003728 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003729 }
3730
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003731 /* Create instance chain of enabled layers */
3732 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003733 struct loader_layer_properties *layer_prop =
3734 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003735 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003736
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003737 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003738 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003739 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003740 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3741 NULL) {
3742 if (layer_prop->functions.str_gipa == NULL ||
3743 strlen(layer_prop->functions.str_gipa) == 0) {
3744 fpGIPA = (PFN_vkGetInstanceProcAddr)
3745 loader_platform_get_proc_address(
3746 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003747 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3748 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003749 fpGIPA = (PFN_vkGetInstanceProcAddr)
3750 loader_platform_get_proc_address(
3751 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003752 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003753 loader_log(
3754 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3755 "Failed to find vkGetInstanceProcAddr in layer %s",
3756 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003757 continue;
3758 }
3759 }
3760
Jon Ashburn23d36b12016-02-02 17:47:28 -07003761 layer_instance_link_info[activated_layers].pNext =
3762 chain_info.u.pLayerInfo;
3763 layer_instance_link_info[activated_layers]
3764 .pfnNextGetInstanceProcAddr = nextGIPA;
3765 chain_info.u.pLayerInfo =
3766 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003767 nextGIPA = fpGIPA;
3768
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003769 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003770 "Insert instance layer %s (%s)",
3771 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003772
3773 activated_layers++;
3774 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003775 }
3776
Jon Ashburn23d36b12016-02-02 17:47:28 -07003777 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003778 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003779 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003780 VkLayerInstanceCreateInfo create_info_disp;
3781
Jon Ashburncc407a22016-04-15 09:25:03 -06003782 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003783 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003784
3785 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3786
3787 create_info_disp.pNext = loader_create_info.pNext;
3788 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003789 res =
3790 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003791 } else {
3792 // Couldn't find CreateInstance function!
3793 res = VK_ERROR_INITIALIZATION_FAILED;
3794 }
3795
3796 if (res != VK_SUCCESS) {
3797 // TODO: Need to clean up here
3798 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003799 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003800 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003801 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003802 }
3803
3804 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003805}
3806
Jon Ashburn23d36b12016-02-02 17:47:28 -07003807void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3808 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003809
Jon Ashburn23d36b12016-02-02 17:47:28 -07003810 loader_init_instance_extension_dispatch_table(
3811 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003812}
3813
Jon Ashburn1530c342016-02-26 13:14:27 -07003814VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003815loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3816 const VkDeviceCreateInfo *pCreateInfo,
3817 const VkAllocationCallbacks *pAllocator,
3818 const struct loader_instance *inst,
3819 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003820 uint32_t activated_layers = 0;
3821 VkLayerDeviceLink *layer_device_link_info;
3822 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003823 VkDeviceCreateInfo loader_create_info;
3824 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003825
Piers Daniell295fe402016-03-29 11:51:11 -06003826 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003827 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003828
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003829 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3830
Jon Ashburn23d36b12016-02-02 17:47:28 -07003831 layer_device_link_info = loader_stack_alloc(
3832 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003833 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003834 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3835 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003836 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003837 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003838
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003839 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003840 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3841 chain_info.function = VK_LAYER_LINK_INFO;
3842 chain_info.u.pLayerInfo = NULL;
3843 chain_info.pNext = pCreateInfo->pNext;
3844 loader_create_info.pNext = &chain_info;
3845
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003846 /* Create instance chain of enabled layers */
3847 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003848 struct loader_layer_properties *layer_prop =
3849 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003850 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003851
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003852 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003853 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003854 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003855 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3856 NULL) {
3857 if (layer_prop->functions.str_gipa == NULL ||
3858 strlen(layer_prop->functions.str_gipa) == 0) {
3859 fpGIPA = (PFN_vkGetInstanceProcAddr)
3860 loader_platform_get_proc_address(
3861 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003862 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3863 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003864 fpGIPA = (PFN_vkGetInstanceProcAddr)
3865 loader_platform_get_proc_address(
3866 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003867 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003868 loader_log(
3869 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3870 "Failed to find vkGetInstanceProcAddr in layer %s",
3871 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003872 continue;
3873 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003874 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003875 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003876 if (layer_prop->functions.str_gdpa == NULL ||
3877 strlen(layer_prop->functions.str_gdpa) == 0) {
3878 fpGDPA = (PFN_vkGetDeviceProcAddr)
3879 loader_platform_get_proc_address(lib_handle,
3880 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003881 layer_prop->functions.get_device_proc_addr = fpGDPA;
3882 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003883 fpGDPA = (PFN_vkGetDeviceProcAddr)
3884 loader_platform_get_proc_address(
3885 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003886 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003887 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003888 "Failed to find vkGetDeviceProcAddr in layer %s",
3889 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003890 continue;
3891 }
3892 }
3893
Jon Ashburn23d36b12016-02-02 17:47:28 -07003894 layer_device_link_info[activated_layers].pNext =
3895 chain_info.u.pLayerInfo;
3896 layer_device_link_info[activated_layers]
3897 .pfnNextGetInstanceProcAddr = nextGIPA;
3898 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3899 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003900 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3901 nextGIPA = fpGIPA;
3902 nextGDPA = fpGDPA;
3903
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003904 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003905 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003906 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003907
3908 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003909 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003910 }
3911
Jon Ashburncc407a22016-04-15 09:25:03 -06003912 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003913 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003914 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003915 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003916 VkLayerDeviceCreateInfo create_info_disp;
3917
Jon Ashburncc407a22016-04-15 09:25:03 -06003918 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003919 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3920
3921 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3922
3923 create_info_disp.pNext = loader_create_info.pNext;
3924 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003925 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003926 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003927 if (res != VK_SUCCESS) {
3928 return res;
3929 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003930 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003931 } else {
3932 // Couldn't find CreateDevice function!
3933 return VK_ERROR_INITIALIZATION_FAILED;
3934 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003935
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003936 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003937 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003938 dev->device);
3939
3940 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003941}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003942
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003943VkResult loader_validate_layers(const struct loader_instance *inst,
3944 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003945 const char *const *ppEnabledLayerNames,
3946 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003947 struct loader_layer_properties *prop;
3948
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003949 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003950 VkStringErrorFlags result =
3951 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003952 if (result != VK_STRING_ERROR_NONE) {
3953 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003954 "Loader: Device ppEnabledLayerNames contains string "
3955 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003956 return VK_ERROR_LAYER_NOT_PRESENT;
3957 }
3958
Jon Ashburn23d36b12016-02-02 17:47:28 -07003959 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003960 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003961 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003962 }
3963 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003964 return VK_SUCCESS;
3965}
3966
3967VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003968 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003969 const struct loader_extension_list *icd_exts,
3970 const struct loader_layer_list *instance_layer,
3971 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003972
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003973 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003974 struct loader_layer_properties *layer_prop;
3975
Jon Ashburnf19916e2016-01-11 13:12:43 -07003976 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003977 VkStringErrorFlags result = vk_string_validate(
3978 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003979 if (result != VK_STRING_ERROR_NONE) {
3980 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003981 "Loader: Instance ppEnabledExtensionNames contains "
3982 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003983 return VK_ERROR_EXTENSION_NOT_PRESENT;
3984 }
3985
Jon Ashburn23d36b12016-02-02 17:47:28 -07003986 extension_prop = get_extension_property(
3987 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003988
3989 if (extension_prop) {
3990 continue;
3991 }
3992
3993 extension_prop = NULL;
3994
3995 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07003996 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003997 layer_prop = loader_get_layer_property(
3998 pCreateInfo->ppEnabledLayerNames[i], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003999 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004000 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004001 * should have already filtered this case out.
4002 */
4003 continue;
4004 }
4005
Jon Ashburn23d36b12016-02-02 17:47:28 -07004006 extension_prop =
4007 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
4008 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004009 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004010 /* Found the extension in one of the layers enabled by the app.
4011 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004012 break;
4013 }
4014 }
4015
4016 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004017 /* Didn't find extension name in any of the global layers, error out
4018 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004019 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004020 }
4021 }
4022 return VK_SUCCESS;
4023}
4024
4025VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06004026 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004027 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07004028 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004029 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004030 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004031 struct loader_layer_properties *layer_prop;
4032
Jon Ashburnf19916e2016-01-11 13:12:43 -07004033 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004034
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004035 VkStringErrorFlags result = vk_string_validate(
4036 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004037 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06004038 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4039 0, "Loader: Device ppEnabledExtensionNames contains "
4040 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004041 return VK_ERROR_EXTENSION_NOT_PRESENT;
4042 }
4043
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004044 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004045 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004046
4047 if (extension_prop) {
4048 continue;
4049 }
4050
Jon Ashburn471f44c2016-01-13 12:51:43 -07004051 /* Not in global list, search activated layer extension lists */
4052 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4053 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004054
Jon Ashburn23d36b12016-02-02 17:47:28 -07004055 extension_prop = get_dev_extension_property(
4056 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004057 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004058 /* Found the extension in one of the layers enabled by the app.
4059 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004060 break;
4061 }
4062 }
4063
4064 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004065 /* Didn't find extension name in any of the device layers, error out
4066 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004067 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004068 }
4069 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004070 return VK_SUCCESS;
4071}
4072
Jon Ashburn1530c342016-02-26 13:14:27 -07004073/**
4074 * Terminator functions for the Instance chain
4075 * All named terminator_<Vulakn API name>
4076 */
Mark Young0ad83132016-06-30 13:02:42 -06004077VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
4078 const VkInstanceCreateInfo *pCreateInfo,
4079 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Jon Ashburn46888392015-01-29 15:45:51 -07004080 struct loader_icd *icd;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004081 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004082 char **filtered_extension_names = NULL;
4083 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004084 VkResult res = VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004085
Jon Ashburncc407a22016-04-15 09:25:03 -06004086 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004087 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4088
Jon Ashburnf19916e2016-01-11 13:12:43 -07004089 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004090 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004091
4092 /*
4093 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004094 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004095 * No ICD will advertise support for layers. An ICD
4096 * library could support a layer, but it would be
4097 * independent of the actual ICD, just in the same library.
4098 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004099 filtered_extension_names =
4100 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004101 if (!filtered_extension_names) {
Mark Young3a587792016-08-19 15:25:08 -06004102 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4103 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004104 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004105 icd_create_info.ppEnabledExtensionNames =
4106 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004107
Jon Ashburn8810c5f2015-08-18 18:04:47 -06004108 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
4109 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06004110 if (NULL == icd) {
Mark Young3a587792016-08-19 15:25:08 -06004111 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4112 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004113 }
4114 icd_create_info.enabledExtensionCount = 0;
4115 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004116
Mark Young0ad83132016-06-30 13:02:42 -06004117 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4118 "Build ICD instance extension list");
4119 // traverse scanned icd list adding non-duplicate extensions to the
4120 // list
Mark Young3a587792016-08-19 15:25:08 -06004121 res = loader_init_generic_list(ptr_instance,
Mark Young0ad83132016-06-30 13:02:42 -06004122 (struct loader_generic_list *)&icd_exts,
4123 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004124 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4125 // If out of memory, bail immediately.
4126 goto out;
4127 } else if (VK_SUCCESS != res) {
4128 // Keep trying if there was some other error.
4129 continue;
4130 }
4131
4132 res = loader_add_instance_extensions(
Mark Young0ad83132016-06-30 13:02:42 -06004133 ptr_instance,
4134 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
4135 icd->this_icd_lib->lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004136 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4137 // If out of memory, bail immediately.
4138 goto out;
4139 } else if (VK_SUCCESS != res) {
4140 // Keep trying if there was some other error.
4141 continue;
4142 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004143
Mark Young0ad83132016-06-30 13:02:42 -06004144 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4145 prop = get_extension_property(
4146 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4147 if (prop) {
4148 filtered_extension_names[icd_create_info
4149 .enabledExtensionCount] =
4150 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4151 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004152 }
4153 }
Mark Young0ad83132016-06-30 13:02:42 -06004154
4155 loader_destroy_generic_list(ptr_instance,
4156 (struct loader_generic_list *)&icd_exts);
4157
4158 res = ptr_instance->icd_libs.list[i].CreateInstance(
4159 &icd_create_info, pAllocator, &(icd->instance));
Mark Young3a587792016-08-19 15:25:08 -06004160 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4161 // If out of memory, bail immediately.
4162 goto out;
4163 } else if (VK_SUCCESS != res) {
4164 // Keep trying if there was some other error.
4165 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4166 "ICD ignored: failed to CreateInstance in ICD %d", i);
4167 continue;
4168 }
Mark Young0ad83132016-06-30 13:02:42 -06004169
Mark Young3a587792016-08-19 15:25:08 -06004170 if (!loader_icd_init_entrys(
4171 icd, icd->instance,
4172 ptr_instance->icd_libs.list[i].GetInstanceProcAddr)) {
4173 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06004174 "ICD ignored: failed to CreateInstance and find "
4175 "entrypoints with ICD");
Mark Young3a587792016-08-19 15:25:08 -06004176 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004177 }
Jon Ashburn46888392015-01-29 15:45:51 -07004178 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004179
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004180 /*
4181 * If no ICDs were added to instance list and res is unchanged
4182 * from it's initial value, the loader was unable to find
4183 * a suitable ICD.
4184 */
Mark Young3a587792016-08-19 15:25:08 -06004185 if (VK_SUCCESS == res && ptr_instance->icds == NULL) {
4186 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4187 }
4188
4189out:
4190
4191 if (VK_SUCCESS != res) {
4192 while (NULL != ptr_instance->icds) {
4193 icd = ptr_instance->icds;
4194 ptr_instance->icds = icd->next;
4195 if (NULL != icd->instance) {
4196 icd->DestroyInstance(icd->instance, pAllocator);
4197 }
4198 loader_icd_destroy(ptr_instance, icd, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004199 }
Ian Elliotteb450762015-02-05 15:19:15 -07004200 }
Jon Ashburn46888392015-01-29 15:45:51 -07004201
Mark Young3a587792016-08-19 15:25:08 -06004202 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004203}
4204
Mark Young0ad83132016-06-30 13:02:42 -06004205VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4206 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004207 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004208 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06004209 struct loader_icd *next_icd;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004210
4211 // Remove this instance from the list of instances:
4212 struct loader_instance *prev = NULL;
4213 struct loader_instance *next = loader.instances;
4214 while (next != NULL) {
4215 if (next == ptr_instance) {
4216 // Remove this instance from the list:
4217 if (prev)
4218 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004219 else
4220 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004221 break;
4222 }
4223 prev = next;
4224 next = next->next;
4225 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004226
Jon Ashburn3da71f22015-05-14 12:43:38 -06004227 while (icds) {
4228 if (icds->instance) {
Chia-I Wuf7458c52015-10-26 21:10:41 +08004229 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004230 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06004231 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004232 icds->instance = VK_NULL_HANDLE;
Mark Young0ad83132016-06-30 13:02:42 -06004233 loader_icd_destroy(ptr_instance, icds, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004234
4235 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07004236 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004237
Jon Ashburn23d36b12016-02-02 17:47:28 -07004238 loader_delete_layer_properties(ptr_instance,
4239 &ptr_instance->instance_layer_list);
Jon Ashburne39a4f82015-08-28 13:38:21 -06004240 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004241 loader_destroy_generic_list(
4242 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004243 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004244 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004245 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004246}
4247
Mark Young0ad83132016-06-30 13:02:42 -06004248VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4249 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4250 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4251 VkResult res = VK_SUCCESS;
Jon Ashburn1530c342016-02-26 13:14:27 -07004252 struct loader_physical_device *phys_dev;
Jon Ashburn014438f2016-03-01 19:51:07 -07004253 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004254
Jon Ashburncc407a22016-04-15 09:25:03 -06004255 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn72690f22016-03-29 12:52:13 -06004256 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004257 struct loader_extension_list icd_exts;
4258
4259 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004260
Jon Ashburn1530c342016-02-26 13:14:27 -07004261 if (fpCreateDevice == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06004262 res = VK_ERROR_INITIALIZATION_FAILED;
4263 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004264 }
4265
Jon Ashburn1530c342016-02-26 13:14:27 -07004266 VkDeviceCreateInfo localCreateInfo;
4267 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004268
4269 /*
4270 * NOTE: Need to filter the extensions to only those
4271 * supported by the ICD.
4272 * No ICD will advertise support for layers. An ICD
4273 * library could support a layer, but it would be
4274 * independent of the actual ICD, just in the same library.
4275 */
4276 char **filtered_extension_names = NULL;
4277 filtered_extension_names =
4278 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4279 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004280 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004281 }
4282
Jon Ashburn1530c342016-02-26 13:14:27 -07004283 localCreateInfo.enabledLayerCount = 0;
4284 localCreateInfo.ppEnabledLayerNames = NULL;
4285
4286 localCreateInfo.enabledExtensionCount = 0;
4287 localCreateInfo.ppEnabledExtensionNames =
4288 (const char *const *)filtered_extension_names;
4289
Jon Ashburn014438f2016-03-01 19:51:07 -07004290 /* Get the physical device (ICD) extensions */
Mark Young3a587792016-08-19 15:25:08 -06004291 res = loader_init_generic_list(phys_dev->this_icd->this_instance,
Jon Ashburn014438f2016-03-01 19:51:07 -07004292 (struct loader_generic_list *)&icd_exts,
Mark Young3a587792016-08-19 15:25:08 -06004293 sizeof(VkExtensionProperties));
4294 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004295 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004296 }
4297
4298 res = loader_add_device_extensions(
Jon Ashburncc407a22016-04-15 09:25:03 -06004299 phys_dev->this_icd->this_instance,
4300 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn014438f2016-03-01 19:51:07 -07004301 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
4302 &icd_exts);
4303 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004304 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004305 }
4306
Jon Ashburn1530c342016-02-26 13:14:27 -07004307 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4308 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004309 VkExtensionProperties *prop =
4310 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004311 if (prop) {
4312 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4313 (char *)extension_name;
4314 localCreateInfo.enabledExtensionCount++;
4315 }
4316 }
4317
Jon Ashburn1530c342016-02-26 13:14:27 -07004318 // TODO: Why does fpCreateDevice behave differently than
4319 // this_icd->CreateDevice?
4320 // VkResult res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo,
4321 // pAllocator, &localDevice);
Jon Ashburn014438f2016-03-01 19:51:07 -07004322 res = phys_dev->this_icd->CreateDevice(phys_dev->phys_dev, &localCreateInfo,
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004323 pAllocator, &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004324
4325 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004326 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004327 }
4328
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004329 *pDevice = dev->device;
Jon Ashburncc407a22016-04-15 09:25:03 -06004330 loader_add_logical_device(phys_dev->this_icd->this_instance,
4331 phys_dev->this_icd, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004332
4333 /* Init dispatch pointer in new device object */
4334 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4335
Mark Young0ad83132016-06-30 13:02:42 -06004336out:
4337 if (NULL != icd_exts.list) {
4338 loader_destroy_generic_list(phys_dev->this_icd->this_instance,
4339 (struct loader_generic_list *)&icd_exts);
4340 }
4341
Jon Ashburn1530c342016-02-26 13:14:27 -07004342 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004343}
4344
Jon Ashburn23d36b12016-02-02 17:47:28 -07004345VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004346terminator_EnumeratePhysicalDevices(VkInstance instance,
4347 uint32_t *pPhysicalDeviceCount,
4348 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004349 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004350 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004351 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004352
Jon Ashburn014438f2016-03-01 19:51:07 -07004353 struct loader_icd *icd;
4354 struct loader_phys_dev_per_icd *phys_devs;
4355
4356 inst->total_gpu_count = 0;
4357 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4358 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4359 if (!phys_devs)
4360 return VK_ERROR_OUT_OF_HOST_MEMORY;
4361
4362 icd = inst->icds;
4363 for (i = 0; i < inst->total_icd_count; i++) {
4364 assert(icd);
4365 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
4366 NULL);
4367 if (res != VK_SUCCESS)
4368 return res;
4369 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004370 }
4371
Jon Ashburn014438f2016-03-01 19:51:07 -07004372 icd = inst->icds;
4373 for (i = 0; i < inst->total_icd_count; i++) {
4374 assert(icd);
4375 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4376 phys_devs[i].count * sizeof(VkPhysicalDevice));
4377 if (!phys_devs[i].phys_devs) {
4378 return VK_ERROR_OUT_OF_HOST_MEMORY;
4379 }
4380 res = icd->EnumeratePhysicalDevices(
4381 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
4382 if ((res == VK_SUCCESS)) {
4383 inst->total_gpu_count += phys_devs[i].count;
4384 } else {
4385 return res;
4386 }
4387 phys_devs[i].this_icd = icd;
4388 icd = icd->next;
4389 }
4390
4391 *pPhysicalDeviceCount = inst->total_gpu_count;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004392 if (!pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004393 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004394 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004395
Jon Ashburn014438f2016-03-01 19:51:07 -07004396 /* Initialize the output pPhysicalDevices with wrapped loader terminator
4397 * physicalDevice objects; save this list of wrapped objects in instance
4398 * struct for later cleanup and use by trampoline code */
4399 uint32_t j, idx = 0;
4400 uint32_t copy_count = 0;
4401
4402 copy_count = (inst->total_gpu_count < *pPhysicalDeviceCount)
4403 ? inst->total_gpu_count
Jon Ashburn23d36b12016-02-02 17:47:28 -07004404 : *pPhysicalDeviceCount;
Jon Ashburn014438f2016-03-01 19:51:07 -07004405
Jon Ashburn014438f2016-03-01 19:51:07 -07004406 if (inst->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004407 loader_instance_heap_free(inst, inst->phys_devs_term);
4408 inst->phys_devs_term = loader_instance_heap_alloc(
Jon Ashburn014438f2016-03-01 19:51:07 -07004409 inst, sizeof(struct loader_physical_device) * copy_count,
4410 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4411 if (!inst->phys_devs_term)
4412 return VK_ERROR_OUT_OF_HOST_MEMORY;
4413
4414 for (i = 0; idx < copy_count && i < inst->total_icd_count; i++) {
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004415
Jon Ashburn014438f2016-03-01 19:51:07 -07004416 for (j = 0; j < phys_devs[i].count && idx < copy_count; j++) {
4417 loader_set_dispatch((void *)&inst->phys_devs_term[idx], inst->disp);
4418 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
4419 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
4420 pPhysicalDevices[idx] =
4421 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4422 idx++;
4423 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004424 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004425 *pPhysicalDeviceCount = copy_count;
4426
Piers Daniell295fe402016-03-29 11:51:11 -06004427 // TODO: Is phys_devs being leaked?
4428
Jon Ashburn014438f2016-03-01 19:51:07 -07004429 if (copy_count < inst->total_gpu_count) {
4430 inst->total_gpu_count = copy_count;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004431 return VK_INCOMPLETE;
4432 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004433 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004434}
4435
Jon Ashburn1530c342016-02-26 13:14:27 -07004436VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4437 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004438 struct loader_physical_device *phys_dev =
4439 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004440 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004441
Tony Barbour59a47322015-06-24 16:06:58 -06004442 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004443 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004444}
4445
Jon Ashburn1530c342016-02-26 13:14:27 -07004446VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004447 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4448 VkQueueFamilyProperties *pProperties) {
4449 struct loader_physical_device *phys_dev =
4450 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004451 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004452
Cody Northropd0802882015-08-03 17:04:53 -06004453 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004454 icd->GetPhysicalDeviceQueueFamilyProperties(
4455 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004456}
4457
Jon Ashburn1530c342016-02-26 13:14:27 -07004458VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004459 VkPhysicalDevice physicalDevice,
4460 VkPhysicalDeviceMemoryProperties *pProperties) {
4461 struct loader_physical_device *phys_dev =
4462 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004463 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004464
4465 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004466 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004467}
4468
Jon Ashburn23d36b12016-02-02 17:47:28 -07004469VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004470terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4471 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004472 struct loader_physical_device *phys_dev =
4473 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004474 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004475
4476 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004477 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004478}
4479
Jon Ashburn23d36b12016-02-02 17:47:28 -07004480VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004481terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4482 VkFormat format,
4483 VkFormatProperties *pFormatInfo) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004484 struct loader_physical_device *phys_dev =
4485 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004486 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004487
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -06004488 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004489 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4490 pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004491}
4492
Jon Ashburn1530c342016-02-26 13:14:27 -07004493VKAPI_ATTR VkResult VKAPI_CALL
4494terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004495 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4496 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4497 VkImageFormatProperties *pImageFormatProperties) {
4498 struct loader_physical_device *phys_dev =
4499 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004500 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn754864f2015-07-23 18:49:07 -06004501
Chia-I Wu17241042015-10-31 00:31:16 +08004502 if (!icd->GetPhysicalDeviceImageFormatProperties)
4503 return VK_ERROR_INITIALIZATION_FAILED;
4504
Jon Ashburn23d36b12016-02-02 17:47:28 -07004505 return icd->GetPhysicalDeviceImageFormatProperties(
4506 phys_dev->phys_dev, format, type, tiling, usage, flags,
4507 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004508}
4509
Jon Ashburn1530c342016-02-26 13:14:27 -07004510VKAPI_ATTR void VKAPI_CALL
4511terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004512 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4513 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4514 VkImageTiling tiling, uint32_t *pNumProperties,
4515 VkSparseImageFormatProperties *pProperties) {
4516 struct loader_physical_device *phys_dev =
4517 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004518 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004519
4520 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004521 icd->GetPhysicalDeviceSparseImageFormatProperties(
4522 phys_dev->phys_dev, format, type, samples, usage, tiling,
4523 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004524}
4525
Jon Ashburn1530c342016-02-26 13:14:27 -07004526VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4527 VkPhysicalDevice physicalDevice, const char *pLayerName,
4528 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburna760a512015-12-14 08:52:14 -07004529 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004530
Mark Young3a587792016-08-19 15:25:08 -06004531 struct loader_layer_list implicit_layer_list = {0};
4532 struct loader_extension_list all_exts = {0};
4533 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07004534
Jon Ashburndc5d9202016-02-29 13:00:51 -07004535 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004536
Jon Ashburndc5d9202016-02-29 13:00:51 -07004537 /* Any layer or trampoline wrapping should be removed at this point in time
4538 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn014438f2016-03-01 19:51:07 -07004539 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004540
Jon Ashburndc5d9202016-02-29 13:00:51 -07004541 /* this case is during the call down the instance chain with pLayerName
4542 * == NULL*/
4543 struct loader_icd *icd = phys_dev->this_icd;
4544 uint32_t icd_ext_count = *pPropertyCount;
4545 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004546
Jon Ashburndc5d9202016-02-29 13:00:51 -07004547 /* get device extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004548 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4549 &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06004550 if (res != VK_SUCCESS) {
4551 goto out;
4552 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004553
Mark Young3a587792016-08-19 15:25:08 -06004554 if (!loader_init_layer_list(icd->this_instance, &implicit_layer_list)) {
4555 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4556 goto out;
4557 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004558
Jon Ashburndc5d9202016-02-29 13:00:51 -07004559 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004560 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4561 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004562 /* we need to determine which implicit layers are active,
4563 * and then add their extensions. This can't be cached as
4564 * it depends on results of environment variables (which can change).
4565 */
4566 if (pProperties != NULL) {
4567 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn014438f2016-03-01 19:51:07 -07004568 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4569 icd_ext_count, pProperties,
4570 &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004571 if (res != VK_SUCCESS) {
4572 goto out;
4573 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004574
Jon Ashburndc5d9202016-02-29 13:00:51 -07004575 /* we need to determine which implicit layers are active,
4576 * and then add their extensions. This can't be cached as
4577 * it depends on results of environment variables (which can
4578 * change).
4579 */
Mark Young3a587792016-08-19 15:25:08 -06004580 res = loader_add_to_ext_list(icd->this_instance, &all_exts,
4581 icd_exts.count, icd_exts.list);
4582 if (res != VK_SUCCESS) {
4583 goto out;
4584 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004585
Jon Ashburndc5d9202016-02-29 13:00:51 -07004586 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004587 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4588 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004589
Jon Ashburndc5d9202016-02-29 13:00:51 -07004590 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4591 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004592 j < implicit_layer_list.list[i].device_extension_list.count;
4593 j++) {
Mark Young3a587792016-08-19 15:25:08 -06004594 res = loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4595 &implicit_layer_list.list[i]
4596 .device_extension_list.list[j]
4597 .props);
4598 if (res != VK_SUCCESS) {
4599 goto out;
4600 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004601 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004602 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004603 uint32_t capacity = *pPropertyCount;
4604 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004605
Jon Ashburndc5d9202016-02-29 13:00:51 -07004606 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4607 props[i] = all_exts.list[i];
4608 }
4609 /* wasn't enough space for the extensions, we did partial copy now
4610 * return VK_INCOMPLETE */
4611 if (capacity < all_exts.count) {
4612 res = VK_INCOMPLETE;
4613 } else {
4614 *pPropertyCount = all_exts.count;
4615 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004616 } else {
4617 /* just return the count; need to add in the count of implicit layer
4618 * extensions
4619 * don't worry about duplicates being added in the count */
4620 *pPropertyCount = icd_ext_count;
4621
4622 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4623 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004624 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004625 }
4626 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004627 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004628
Mark Young3a587792016-08-19 15:25:08 -06004629out:
4630
4631 if (NULL != implicit_layer_list.list) {
4632 loader_destroy_generic_list(
4633 icd->this_instance,
4634 (struct loader_generic_list *)&implicit_layer_list);
4635 }
4636 if (NULL != all_exts.list) {
4637 loader_destroy_generic_list(icd->this_instance,
4638 (struct loader_generic_list *)&all_exts);
4639 }
4640 if (NULL != icd_exts.list) {
4641 loader_destroy_generic_list(icd->this_instance,
4642 (struct loader_generic_list *)&icd_exts);
4643 }
4644
Jon Ashburndc5d9202016-02-29 13:00:51 -07004645 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004646}
4647
Jon Ashburn23d36b12016-02-02 17:47:28 -07004648VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004649terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4650 uint32_t *pPropertyCount,
4651 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004652
Jon Ashburndc5d9202016-02-29 13:00:51 -07004653 // should never get here this call isn't dispatched down the chain
4654 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004655}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004656
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004657VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004658 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004659 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004660 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004661
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004662 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004663 if (utf8[i] == 0) {
4664 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004665 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004666 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004667 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004668 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004669 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004670 num_char_bytes = 2;
4671 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4672 num_char_bytes = 3;
4673 } else {
4674 result = VK_STRING_ERROR_BAD_DATA;
4675 }
4676
4677 // Validate the following num_char_bytes of data
4678 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4679 if (++i == max_length) {
4680 result |= VK_STRING_ERROR_LENGTH;
4681 break;
4682 }
4683 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4684 result |= VK_STRING_ERROR_BAD_DATA;
4685 }
4686 }
4687 }
4688 return result;
4689}