blob: 3a4b6d6742474471e43f25b81a5fd54caf6438fd [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 */
Mark Young60861ac2016-09-02 11:39:26 -0600560static uint32_t loader_make_version(char *vers_str) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700561 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
Mark Young60861ac2016-09-02 11:39:26 -0600562 char *vers_tok;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600563
Mark Young60861ac2016-09-02 11:39:26 -0600564 if (!vers_str) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600565 return vers;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600566 }
Mark Young60861ac2016-09-02 11:39:26 -0600567
568 vers_tok = strtok(vers_str, ".\"\n\r");
569 if (NULL != vers_tok) {
570 major = (uint16_t)atoi(vers_tok);
571 vers_tok = strtok(NULL, ".\"\n\r");
572 if (NULL != vers_tok) {
573 minor = (uint16_t)atoi(vers_tok);
574 vers_tok = strtok(NULL, ".\"\n\r");
575 if (NULL != vers_tok) {
576 patch = (uint16_t)atoi(vers_tok);
577 }
578 }
579 }
Jon Ashburnc7237a72015-08-03 09:08:46 -0600580
581 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600582}
583
Jon Ashburn23d36b12016-02-02 17:47:28 -0700584bool compare_vk_extension_properties(const VkExtensionProperties *op1,
585 const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800586 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600587}
588
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600589/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600590 * Search the given ext_array for an extension
591 * matching the given vk_ext_prop
592 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700593bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
594 const uint32_t count,
595 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600596 for (uint32_t i = 0; i < count; i++) {
597 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i]))
598 return true;
599 }
600 return false;
601}
602
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600603/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600604 * Search the given ext_list for an extension
605 * matching the given vk_ext_prop
606 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700607bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop,
608 const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600609 for (uint32_t i = 0; i < ext_list->count; i++) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600610 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop))
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600611 return true;
612 }
613 return false;
614}
615
Jon Ashburnb8726962016-04-08 15:03:35 -0600616/**
617 * Search the given ext_list for a device extension matching the given ext_prop
618 */
Jon Ashburncc407a22016-04-15 09:25:03 -0600619bool has_vk_dev_ext_property(
620 const VkExtensionProperties *ext_prop,
621 const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600622 for (uint32_t i = 0; i < ext_list->count; i++) {
623 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop))
624 return true;
625 }
626 return false;
627}
628
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600629/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600630 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600631 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700632static struct loader_layer_properties *
633loader_get_layer_property(const char *name,
634 const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600635 for (uint32_t i = 0; i < layer_list->count; i++) {
636 const VkLayerProperties *item = &layer_list->list[i].info;
637 if (strcmp(name, item->layerName) == 0)
638 return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600639 }
640 return NULL;
641}
642
Jon Ashburne13ecc92015-08-03 17:19:30 -0600643/**
644 * Get the next unused layer property in the list. Init the property to zero.
645 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700646static struct loader_layer_properties *
647loader_get_next_layer_property(const struct loader_instance *inst,
648 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600649 if (layer_list->capacity == 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700650 layer_list->list =
Mark Young0ad83132016-06-30 13:02:42 -0600651 loader_instance_heap_alloc(
652 inst, sizeof(struct loader_layer_properties) * 64,
653 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600654 if (layer_list->list == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700655 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
656 "Out of memory can't add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600657 return NULL;
658 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700659 memset(layer_list->list, 0,
660 sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600661 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
662 }
663
664 // ensure enough room to add an entry
Jon Ashburn23d36b12016-02-02 17:47:28 -0700665 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
666 layer_list->capacity) {
Mark Young0ad83132016-06-30 13:02:42 -0600667 layer_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700668 inst, layer_list->list, layer_list->capacity,
669 layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600670 if (layer_list->list == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700671 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700672 "realloc failed for layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600673 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600674 }
675 layer_list->capacity *= 2;
676 }
677
678 layer_list->count++;
679 return &(layer_list->list[layer_list->count - 1]);
680}
681
682/**
683 * Remove all layer properties entrys from the list
684 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700685void loader_delete_layer_properties(const struct loader_instance *inst,
686 struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700687 uint32_t i, j;
688 struct loader_device_extension_list *dev_ext_list;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600689 if (!layer_list)
690 return;
691
Jon Ashburne13ecc92015-08-03 17:19:30 -0600692 for (i = 0; i < layer_list->count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700693 loader_destroy_generic_list(
694 inst, (struct loader_generic_list *)&layer_list->list[i]
695 .instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700696 dev_ext_list = &layer_list->list[i].device_extension_list;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700697 if (dev_ext_list->capacity > 0 &&
Mark Young0ad83132016-06-30 13:02:42 -0600698 NULL != dev_ext_list->list &&
Jon Ashburn23d36b12016-02-02 17:47:28 -0700699 dev_ext_list->list->entrypoint_count > 0) {
700 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Young0ad83132016-06-30 13:02:42 -0600701 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700702 }
Mark Young0ad83132016-06-30 13:02:42 -0600703 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700704 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700705 loader_destroy_generic_list(inst,
706 (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600707 }
708 layer_list->count = 0;
709
Jon Ashburnb82c1852015-08-11 14:49:54 -0600710 if (layer_list->capacity > 0) {
711 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600712 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600713 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600714}
715
Mark Young3a587792016-08-19 15:25:08 -0600716static VkResult loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700717 const struct loader_instance *inst,
718 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props,
719 const char *lib_name, struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700720 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600721 VkExtensionProperties *ext_props;
Mark Young3a587792016-08-19 15:25:08 -0600722 VkResult res = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600723
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600724 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600725 /* No EnumerateInstanceExtensionProperties defined */
Mark Young3a587792016-08-19 15:25:08 -0600726 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600727 }
728
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600729 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600730 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700731 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700732 "Error getting Instance extension count from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600733 goto out;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600734 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600735
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600736 if (count == 0) {
737 /* No ExtensionProperties to report */
Mark Young3a587792016-08-19 15:25:08 -0600738 goto out;
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600739 }
740
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600741 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600742
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600743 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600744 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700745 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700746 "Error getting Instance extensions from %s", lib_name);
Mark Young3a587792016-08-19 15:25:08 -0600747 goto out;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600748 }
Tony Barbour59a47322015-06-24 16:06:58 -0600749
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600750 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600751 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600752
Jon Ashburncc407a22016-04-15 09:25:03 -0600753 bool ext_unsupported =
754 wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600755 if (!ext_unsupported) {
756 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
757 VK_MAJOR(ext_props[i].specVersion),
758 VK_MINOR(ext_props[i].specVersion),
759 VK_PATCH(ext_props[i].specVersion));
760 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
761 "Instance Extension: %s (%s) version %s",
762 ext_props[i].extensionName, lib_name, spec_version);
Mark Young3a587792016-08-19 15:25:08 -0600763 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
764 if (res != VK_SUCCESS) {
765 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
766 "Failed to add %s to Instance extension list",
767 lib_name);
768 goto out;
769 }
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600770 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600771 }
Mark Young3a587792016-08-19 15:25:08 -0600772out:
773 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600774}
775
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700776/*
777 * Initialize ext_list with the physical device extensions.
778 * The extension properties are passed as inputs in count and ext_props.
779 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700780static VkResult
781loader_init_device_extensions(const struct loader_instance *inst,
782 struct loader_physical_device *phys_dev,
783 uint32_t count, VkExtensionProperties *ext_props,
784 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700785 VkResult res;
786 uint32_t i;
787
Mark Young3a587792016-08-19 15:25:08 -0600788 res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
789 sizeof(VkExtensionProperties));
790 if (VK_SUCCESS != res) {
791 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700792 }
793
794 for (i = 0; i < count; i++) {
795 char spec_version[64];
796
Jon Ashburn23d36b12016-02-02 17:47:28 -0700797 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
798 VK_MAJOR(ext_props[i].specVersion),
799 VK_MINOR(ext_props[i].specVersion),
800 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700801 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700802 "Device Extension: %s (%s) version %s",
803 ext_props[i].extensionName,
804 phys_dev->this_icd->this_icd_lib->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700805 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
806 if (res != VK_SUCCESS)
807 return res;
808 }
809
810 return VK_SUCCESS;
811}
812
Jon Ashburn1530c342016-02-26 13:14:27 -0700813VkResult loader_add_device_extensions(const struct loader_instance *inst,
Jon Ashburncc407a22016-04-15 09:25:03 -0600814 PFN_vkEnumerateDeviceExtensionProperties
815 fpEnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700816 VkPhysicalDevice physical_device,
817 const char *lib_name,
818 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600819 uint32_t i, count;
820 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600821 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600822
Piers Daniell295fe402016-03-29 11:51:11 -0600823 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count,
824 NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700825 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700826 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700827 if (!ext_props)
828 return VK_ERROR_OUT_OF_HOST_MEMORY;
Piers Daniell295fe402016-03-29 11:51:11 -0600829 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
830 &count, ext_props);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700831 if (res != VK_SUCCESS)
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700832 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700833 for (i = 0; i < count; i++) {
834 char spec_version[64];
835
Jon Ashburn23d36b12016-02-02 17:47:28 -0700836 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
837 VK_MAJOR(ext_props[i].specVersion),
838 VK_MINOR(ext_props[i].specVersion),
839 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700840 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700841 "Device Extension: %s (%s) version %s",
842 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700843 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
844 if (res != VK_SUCCESS)
845 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600846 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700847 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700848 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
849 "Error getting physical device extension info count from "
850 "library %s",
851 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700852 return res;
853 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600854
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700855 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600856}
857
Mark Young3a587792016-08-19 15:25:08 -0600858VkResult loader_init_generic_list(const struct loader_instance *inst,
Jon Ashburn1530c342016-02-26 13:14:27 -0700859 struct loader_generic_list *list_info,
860 size_t element_size) {
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700861 list_info->capacity = 32 * element_size;
Mark Young0ad83132016-06-30 13:02:42 -0600862 list_info->list = loader_instance_heap_alloc(
863 inst, list_info->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700864 if (list_info->list == NULL) {
Mark Young3a587792016-08-19 15:25:08 -0600865 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600866 }
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700867 memset(list_info->list, 0, list_info->capacity);
868 list_info->count = 0;
Mark Young3a587792016-08-19 15:25:08 -0600869 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600870}
871
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700872void loader_destroy_generic_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700873 struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600874 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700875 list->count = 0;
876 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600877}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600878
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600879/*
880 * Append non-duplicate extension properties defined in props
881 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700882 * Return
883 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600884 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700885VkResult loader_add_to_ext_list(const struct loader_instance *inst,
886 struct loader_extension_list *ext_list,
887 uint32_t prop_list_count,
888 const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600889 uint32_t i;
890 const VkExtensionProperties *cur_ext;
891
892 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600893 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700894 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600895 if (VK_SUCCESS != res) {
896 return res;
897 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600898 }
899
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600900 for (i = 0; i < prop_list_count; i++) {
901 cur_ext = &props[i];
902
903 // look for duplicates
904 if (has_vk_extension_property(cur_ext, ext_list)) {
905 continue;
906 }
907
908 // add to list at end
909 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700910 if (ext_list->count * sizeof(VkExtensionProperties) >=
911 ext_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -0600912
Mark Young0ad83132016-06-30 13:02:42 -0600913 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700914 inst, ext_list->list, ext_list->capacity,
915 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700916
917 if (ext_list->list == NULL)
918 return VK_ERROR_OUT_OF_HOST_MEMORY;
919
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600920 // double capacity
921 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600922 }
923
Jon Ashburn23d36b12016-02-02 17:47:28 -0700924 memcpy(&ext_list->list[ext_list->count], cur_ext,
925 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600926 ext_list->count++;
927 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700928 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600929}
930
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700931/*
932 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600933 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700934 * Return
935 * Vk_SUCCESS on success
936 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700937VkResult
938loader_add_to_dev_ext_list(const struct loader_instance *inst,
939 struct loader_device_extension_list *ext_list,
940 const VkExtensionProperties *props,
941 uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700942 uint32_t idx;
943 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600944 VkResult res = loader_init_generic_list(
945 inst, (struct loader_generic_list *)ext_list,
946 sizeof(struct loader_dev_ext_props));
947 if (VK_SUCCESS != res) {
948 return res;
949 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700950 }
951
Jon Ashburnb8726962016-04-08 15:03:35 -0600952 // look for duplicates
953 if (has_vk_dev_ext_property(props, ext_list)) {
954 return VK_SUCCESS;
955 }
956
Jon Ashburn23d36b12016-02-02 17:47:28 -0700957 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700958 // add to list at end
959 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700960 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700961
Mark Young0ad83132016-06-30 13:02:42 -0600962 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700963 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
964 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700965
966 if (ext_list->list == NULL)
967 return VK_ERROR_OUT_OF_HOST_MEMORY;
968
969 // double capacity
970 ext_list->capacity *= 2;
971 }
972
Jon Ashburn23d36b12016-02-02 17:47:28 -0700973 memcpy(&ext_list->list[idx].props, props,
974 sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700975 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700976 ext_list->list[idx].entrypoints =
Mark Young0ad83132016-06-30 13:02:42 -0600977 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
978 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
979 if (ext_list->list[idx].entrypoints == NULL) {
980 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700981 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600982 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700983 for (uint32_t i = 0; i < entry_count; i++) {
Mark Young0ad83132016-06-30 13:02:42 -0600984 ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700985 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600986 if (ext_list->list[idx].entrypoints[i] == NULL) {
987 for (uint32_t j = 0; j < i; j++) {
988 loader_instance_heap_free(inst,
989 ext_list->list[idx].entrypoints[j]);
990 }
991 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
992 ext_list->list[idx].entrypoint_count = 0;
993 ext_list->list[idx].entrypoints = NULL;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700994 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600995 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700996 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
997 }
998 ext_list->count++;
999
1000 return VK_SUCCESS;
1001}
1002
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001003/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001004 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001005 * Add these to the output layer_list. Don't add duplicates to the output
1006 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001007 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001008static VkResult
1009loader_add_layer_names_to_list(const struct loader_instance *inst,
1010 struct loader_layer_list *output_list,
1011 uint32_t name_count, const char *const *names,
1012 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001013 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001014 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001015
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001016 for (uint32_t i = 0; i < name_count; i++) {
1017 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001018 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001019 if (!layer_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001020 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1021 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001022 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001023 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001024 }
1025
Mark Young0ad83132016-06-30 13:02:42 -06001026 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001027 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001028
1029 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001030}
1031
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001032/*
1033 * Manage lists of VkLayerProperties
1034 */
Jon Ashburne39a4f82015-08-28 13:38:21 -06001035static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001036 struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001037 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Young0ad83132016-06-30 13:02:42 -06001038 list->list = loader_instance_heap_alloc(
1039 inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001040 if (list->list == NULL) {
1041 return false;
1042 }
1043 memset(list->list, 0, list->capacity);
1044 list->count = 0;
1045 return true;
1046}
1047
Jon Ashburne39a4f82015-08-28 13:38:21 -06001048void loader_destroy_layer_list(const struct loader_instance *inst,
Mark Young0ad83132016-06-30 13:02:42 -06001049 struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001050 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001051 if (device) {
1052 loader_device_heap_free(device, layer_list->list);
1053 } else {
1054 loader_instance_heap_free(inst, layer_list->list);
1055 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001056 layer_list->count = 0;
1057 layer_list->capacity = 0;
1058}
1059
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001060/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001061 * Search the given layer list for a list
1062 * matching the given VkLayerProperties
1063 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001064bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
1065 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001066 for (uint32_t i = 0; i < list->count; i++) {
1067 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
1068 return true;
1069 }
1070 return false;
1071}
1072
1073/*
1074 * Search the given layer list for a layer
1075 * matching the given name
1076 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001077bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001078 for (uint32_t i = 0; i < list->count; i++) {
1079 if (strcmp(name, list->list[i].info.layerName) == 0)
1080 return true;
1081 }
1082 return false;
1083}
1084
1085/*
1086 * Append non-duplicate layer properties defined in prop_list
1087 * to the given layer_info list
1088 */
Mark Young0ad83132016-06-30 13:02:42 -06001089VkResult loader_add_to_layer_list(const struct loader_instance *inst,
1090 struct loader_layer_list *list,
1091 uint32_t prop_list_count,
1092 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001093 uint32_t i;
1094 struct loader_layer_properties *layer;
1095
1096 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001097 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001098 }
1099
1100 if (list->list == NULL)
Mark Young0ad83132016-06-30 13:02:42 -06001101 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001102
1103 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001104 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001105
1106 // look for duplicates
1107 if (has_vk_layer_property(&layer->info, list)) {
1108 continue;
1109 }
1110
1111 // add to list at end
1112 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001113 if (list->count * sizeof(struct loader_layer_properties) >=
1114 list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001115
Mark Young0ad83132016-06-30 13:02:42 -06001116 list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001117 inst, list->list, list->capacity, list->capacity * 2,
1118 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001119 if (NULL == list->list) {
1120 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1121 "realloc failed for layer list when attempting to "
1122 "add new layer");
1123 return VK_ERROR_OUT_OF_HOST_MEMORY;
1124 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001125 // double capacity
1126 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001127 }
1128
Jon Ashburn23d36b12016-02-02 17:47:28 -07001129 memcpy(&list->list[list->count], layer,
1130 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001131 list->count++;
1132 }
Mark Young0ad83132016-06-30 13:02:42 -06001133
1134 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001135}
1136
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001137/**
1138 * Search the search_list for any layer with a name
1139 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001140 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001141 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001142 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001143 */
Jon Ashburncc407a22016-04-15 09:25:03 -06001144void loader_find_layer_name_add_list(
1145 const struct loader_instance *inst, const char *name,
1146 const enum layer_type type, const struct loader_layer_list *search_list,
1147 struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001148 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001149 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001150 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001151 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001152 (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001153 /* Found a layer with the same name, add to found_list */
Mark Young0ad83132016-06-30 13:02:42 -06001154 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
1155 found = true;
1156 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001157 }
1158 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001159 if (!found) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001160 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001161 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001162 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001163}
1164
Jon Ashburn23d36b12016-02-02 17:47:28 -07001165static VkExtensionProperties *
1166get_extension_property(const char *name,
1167 const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001168 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001169 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001170 return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001171 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001172 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001173}
1174
Jon Ashburn23d36b12016-02-02 17:47:28 -07001175static VkExtensionProperties *
1176get_dev_extension_property(const char *name,
1177 const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001178 for (uint32_t i = 0; i < list->count; i++) {
1179 if (strcmp(name, list->list[i].props.extensionName) == 0)
1180 return &list->list[i].props;
1181 }
1182 return NULL;
1183}
1184
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001185/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001186 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001187 * the extension must provide two entry points for the loader to use:
1188 * - "trampoline" entry point - this is the address returned by GetProcAddr
1189 * and will always do what's necessary to support a global call.
1190 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001191 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001192 * the extension for the appropriate ICDs that are available.
1193 * There is no generic mechanism for including these functions, the references
1194 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001195 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1196 * GetProcAddr requests
1197 * loader_coalesce_extensions(void) - add extension records to the list of
1198 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001199 * extension available to the app.
1200 * instance_disp - add function pointer for terminator function to this array.
1201 * The extension itself should be in a separate file that will be
1202 * linked directly with the loader.
1203 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001204
Mark Young3a587792016-08-19 15:25:08 -06001205VkResult loader_get_icd_loader_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001206 const struct loader_instance *inst, struct loader_icd_libs *icd_libs,
1207 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001208 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001209 VkResult res = VK_SUCCESS;
1210
Jon Ashburn23d36b12016-02-02 17:47:28 -07001211 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1212 "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001213
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001214 // traverse scanned icd list adding non-duplicate extensions to the list
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001215 for (uint32_t i = 0; i < icd_libs->count; i++) {
Mark Young3a587792016-08-19 15:25:08 -06001216 res = loader_init_generic_list(inst,
1217 (struct loader_generic_list *)&icd_exts,
1218 sizeof(VkExtensionProperties));
1219 if (VK_SUCCESS != res) {
1220 goto out;
1221 }
1222 res = loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001223 inst, icd_libs->list[i].EnumerateInstanceExtensionProperties,
1224 icd_libs->list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001225 if (VK_SUCCESS == res) {
1226 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count,
1227 icd_exts.list);
1228 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001229 loader_destroy_generic_list(inst,
1230 (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001231 if (VK_SUCCESS != res) {
1232 goto out;
1233 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001234 };
1235
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001236 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001237 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001238
1239out:
1240 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001241}
1242
Jon Ashburne39a4f82015-08-28 13:38:21 -06001243struct loader_icd *loader_get_icd_and_device(const VkDevice device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001244 struct loader_device **found_dev) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001245 *found_dev = NULL;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001246 for (struct loader_instance *inst = loader.instances; inst;
1247 inst = inst->next) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001248 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001249 for (struct loader_device *dev = icd->logical_device_list; dev;
1250 dev = dev->next)
1251 /* Value comparison of device prevents object wrapping by layers
1252 */
1253 if (loader_get_dispatch(dev->device) ==
1254 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001255 *found_dev = dev;
1256 return icd;
1257 }
1258 }
1259 }
1260 return NULL;
1261}
1262
Mark Young0ad83132016-06-30 13:02:42 -06001263void loader_destroy_logical_device(const struct loader_instance *inst,
1264 struct loader_device *dev,
1265 const VkAllocationCallbacks *pAllocator) {
1266 if (pAllocator) {
1267 dev->alloc_callbacks = *pAllocator;
1268 }
1269 if (NULL != dev->app_extension_props) {
1270 loader_device_heap_free(dev, dev->app_extension_props);
1271 }
1272 if (NULL != dev->activated_layer_list.list) {
1273 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1274 }
1275 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001276}
1277
Jon Ashburn1530c342016-02-26 13:14:27 -07001278struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001279loader_create_logical_device(const struct loader_instance *inst,
1280 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001281 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001282#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1283 {
1284#else
1285 if (pAllocator) {
1286 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1287 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1288 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1289 } else {
1290#endif
1291 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1292 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001293
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001294 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001295 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1296 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001297 return NULL;
1298 }
1299
1300 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001301 if (pAllocator) {
1302 new_dev->alloc_callbacks = *pAllocator;
1303 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001304
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001305 return new_dev;
1306}
1307
Piers Daniell295fe402016-03-29 11:51:11 -06001308void loader_add_logical_device(const struct loader_instance *inst,
1309 struct loader_icd *icd,
1310 struct loader_device *dev) {
1311 dev->next = icd->logical_device_list;
1312 icd->logical_device_list = dev;
1313}
1314
Jon Ashburn23d36b12016-02-02 17:47:28 -07001315void loader_remove_logical_device(const struct loader_instance *inst,
1316 struct loader_icd *icd,
Mark Young0ad83132016-06-30 13:02:42 -06001317 struct loader_device *found_dev,
1318 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001319 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001320
1321 if (!icd || !found_dev)
1322 return;
1323
1324 prev_dev = NULL;
1325 dev = icd->logical_device_list;
1326 while (dev && dev != found_dev) {
1327 prev_dev = dev;
1328 dev = dev->next;
1329 }
1330
1331 if (prev_dev)
1332 prev_dev->next = found_dev->next;
1333 else
1334 icd->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001335 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001336}
1337
Jon Ashburn23d36b12016-02-02 17:47:28 -07001338static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0ad83132016-06-30 13:02:42 -06001339 struct loader_icd *icd,
1340 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001341 ptr_inst->total_icd_count--;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001342 for (struct loader_device *dev = icd->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001343 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001344 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001345 dev = next_dev;
1346 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001347
Mark Young0ad83132016-06-30 13:02:42 -06001348 loader_instance_heap_free(ptr_inst, icd);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001349}
1350
Jon Ashburn23d36b12016-02-02 17:47:28 -07001351static struct loader_icd *
1352loader_icd_create(const struct loader_instance *inst) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001353 struct loader_icd *icd;
1354
Mark Young0ad83132016-06-30 13:02:42 -06001355 icd = loader_instance_heap_alloc(inst, sizeof(*icd),
1356 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001357 if (!icd)
1358 return NULL;
1359
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001360 memset(icd, 0, sizeof(*icd));
1361
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001362 return icd;
1363}
1364
Jon Ashburn23d36b12016-02-02 17:47:28 -07001365static struct loader_icd *
1366loader_icd_add(struct loader_instance *ptr_inst,
1367 const struct loader_scanned_icds *icd_lib) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001368 struct loader_icd *icd;
1369
Jon Ashburne39a4f82015-08-28 13:38:21 -06001370 icd = loader_icd_create(ptr_inst);
Chia-I Wu13a61a52014-08-04 11:18:20 +08001371 if (!icd)
1372 return NULL;
1373
Jon Ashburn3d002332015-08-20 16:35:30 -06001374 icd->this_icd_lib = icd_lib;
1375 icd->this_instance = ptr_inst;
1376
Chia-I Wu13a61a52014-08-04 11:18:20 +08001377 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -07001378 icd->next = ptr_inst->icds;
1379 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001380 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001381
1382 return icd;
1383}
Jon Ashburn17b4c862016-04-25 11:09:37 -06001384/**
1385 * Determine the ICD interface version to use.
1386 * @param icd
1387 * @param pVersion Output parameter indicating which version to use or 0 if
1388 * the negotiation API is not supported by the ICD
1389 * @return bool indicating true if the selected interface version is supported
1390 * by the loader, false indicates the version is not supported
1391 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1392 * vk_icdNegotiateLoaderICDInterfaceVersion
1393 * version 1 supports vk_icdGetInstanceProcAddr
1394 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1395 */
1396bool loader_get_icd_interface_version(
1397 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1398 uint32_t *pVersion) {
1399
1400 if (fp_negotiate_icd_version == NULL) {
1401 // ICD does not support the negotiation API, it supports version 0 or 1
1402 // calling code must determine if it is version 0 or 1
1403 *pVersion = 0;
1404 } else {
1405 // ICD supports the negotiation API, so call it with the loader's
1406 // latest version supported
1407 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1408 VkResult result = fp_negotiate_icd_version(pVersion);
1409
1410 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1411 // ICD no longer supports the loader's latest interface version so
1412 // fail loading the ICD
1413 return false;
1414 }
1415 }
1416
1417#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1418 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1419 // Loader no longer supports the ICD's latest interface version so fail
1420 // loading the ICD
1421 return false;
1422 }
1423#endif
1424 return true;
1425}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001426
Jon Ashburn23d36b12016-02-02 17:47:28 -07001427void loader_scanned_icd_clear(const struct loader_instance *inst,
1428 struct loader_icd_libs *icd_libs) {
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001429 if (icd_libs->capacity == 0)
1430 return;
1431 for (uint32_t i = 0; i < icd_libs->count; i++) {
1432 loader_platform_close_library(icd_libs->list[i].handle);
Mark Young0ad83132016-06-30 13:02:42 -06001433 loader_instance_heap_free(inst, icd_libs->list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001434 }
Mark Young0ad83132016-06-30 13:02:42 -06001435 loader_instance_heap_free(inst, icd_libs->list);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001436 icd_libs->capacity = 0;
1437 icd_libs->count = 0;
1438 icd_libs->list = NULL;
1439}
1440
Mark Young0ad83132016-06-30 13:02:42 -06001441static VkResult loader_scanned_icd_init(const struct loader_instance *inst,
1442 struct loader_icd_libs *icd_libs) {
1443 VkResult err = VK_SUCCESS;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001444 loader_scanned_icd_clear(inst, icd_libs);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001445 icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
Mark Young0ad83132016-06-30 13:02:42 -06001446 icd_libs->list = loader_instance_heap_alloc(
1447 inst, icd_libs->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1448 if (NULL == icd_libs->list) {
1449 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1450 "realloc failed for layer list when attempting to add new layer");
1451 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1452 }
1453 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001454}
1455
Mark Young3a587792016-08-19 15:25:08 -06001456static VkResult loader_scanned_icd_add(const struct loader_instance *inst,
1457 struct loader_icd_libs *icd_libs,
1458 const char *filename,
1459 uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001460 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001461 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001462 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001463 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001464 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001465 struct loader_scanned_icds *new_node;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001466 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001467 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001468
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001469 /* TODO implement smarter opening/closing of libraries. For now this
1470 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001471 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001472 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001473 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001474 loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001475 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001476 }
1477
Jon Ashburn17b4c862016-04-25 11:09:37 -06001478 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001479 fp_negotiate_icd_version = loader_platform_get_proc_address(
1480 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001481
1482 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001483 &interface_vers)) {
1484 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1485 "ICD (%s) doesn't support interface version compatible"
1486 "with loader, skip this ICD %s",
1487 filename);
Mark Young3a587792016-08-19 15:25:08 -06001488 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001489 }
1490
Jon Ashburn23d36b12016-02-02 17:47:28 -07001491 fp_get_proc_addr =
1492 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001493 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001494 assert(interface_vers == 0);
1495 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001496 fp_get_proc_addr =
1497 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001498 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001499 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1500 loader_platform_get_proc_address_error(
1501 "vk_icdGetInstanceProcAddr"));
Mark Young3a587792016-08-19 15:25:08 -06001502 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001503 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001504 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001505 "Using deprecated ICD interface of "
1506 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001507 "vk_icdGetInstanceProcAddr for ICD %s",
1508 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001509 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001510 fp_create_inst =
1511 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001512 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001513 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1514 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1515 "for ICD %s",
1516 filename);
Mark Young3a587792016-08-19 15:25:08 -06001517 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001518 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001519 fp_get_inst_ext_props = loader_platform_get_proc_address(
1520 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001521 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001522 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1523 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001524 "via dlsym/loadlibrary for ICD %s",
1525 filename);
Mark Young3a587792016-08-19 15:25:08 -06001526 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001527 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001528 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001529 // Use newer interface version 1 or later
1530 if (interface_vers == 0)
1531 interface_vers = 1;
1532
Jon Ashburn23d36b12016-02-02 17:47:28 -07001533 fp_create_inst =
1534 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001535 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001536 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1537 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001538 "vk_icdGetInstanceProcAddr for ICD %s",
1539 filename);
Mark Young3a587792016-08-19 15:25:08 -06001540 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001541 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001542 fp_get_inst_ext_props =
1543 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1544 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001545 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001546 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1547 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001548 "via vk_icdGetInstanceProcAddr for ICD %s",
1549 filename);
Mark Young3a587792016-08-19 15:25:08 -06001550 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001551 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001552 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001553
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001554 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001555 if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
1556 icd_libs->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001557
Mark Young0ad83132016-06-30 13:02:42 -06001558 icd_libs->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001559 inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
1560 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001561 if (NULL == icd_libs->list) {
Mark Young3a587792016-08-19 15:25:08 -06001562 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001563 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1564 "realloc failed on icd library list");
Mark Young3a587792016-08-19 15:25:08 -06001565 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001566 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001567 // double capacity
1568 icd_libs->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001569 }
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001570 new_node = &(icd_libs->list[icd_libs->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001571
1572 new_node->handle = handle;
Jon Ashburn005617f2015-11-17 17:35:40 -07001573 new_node->api_version = api_version;
Jon Ashburnc624c882015-07-16 10:17:29 -06001574 new_node->GetInstanceProcAddr = fp_get_proc_addr;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001575 new_node->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Jon Ashburn46888392015-01-29 15:45:51 -07001576 new_node->CreateInstance = fp_create_inst;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001577 new_node->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001578
Mark Young0ad83132016-06-30 13:02:42 -06001579 new_node->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001580 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001581 if (NULL == new_node->lib_name) {
Mark Young3a587792016-08-19 15:25:08 -06001582 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001583 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001584 "Out of memory can't add icd");
Mark Young3a587792016-08-19 15:25:08 -06001585 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001586 }
1587 strcpy(new_node->lib_name, filename);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001588 icd_libs->count++;
Mark Young3a587792016-08-19 15:25:08 -06001589
1590out:
1591
1592 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001593}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001594
Jon Ashburn23d36b12016-02-02 17:47:28 -07001595static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
1596 const PFN_vkGetInstanceProcAddr fp_gipa) {
1597/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001598
Jon Ashburn23d36b12016-02-02 17:47:28 -07001599#define LOOKUP_GIPA(func, required) \
1600 do { \
1601 icd->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1602 if (!icd->func && required) { \
1603 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001604 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001605 loader_platform_get_proc_address_error("vk" #func)); \
1606 return false; \
1607 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001608 } while (0)
1609
Jon Ashburnc624c882015-07-16 10:17:29 -06001610 LOOKUP_GIPA(GetDeviceProcAddr, true);
1611 LOOKUP_GIPA(DestroyInstance, true);
1612 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1613 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1614 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001615 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001616 LOOKUP_GIPA(CreateDevice, true);
1617 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1618 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001619 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001620 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001621 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001622 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1623 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001624 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001625 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1626 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1627 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001628 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1629 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1630 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1631 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1632 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1633 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1634 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1635 LOOKUP_GIPA(DestroySurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001636#ifdef VK_USE_PLATFORM_WIN32_KHR
1637 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1638#endif
1639#ifdef VK_USE_PLATFORM_XCB_KHR
1640 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1641#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001642#ifdef VK_USE_PLATFORM_XLIB_KHR
1643 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1644#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001645#ifdef VK_USE_PLATFORM_WAYLAND_KHR
1646 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1647#endif
James Jones389dc0c2016-08-18 23:41:19 +01001648 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001649
Jon Ashburnc624c882015-07-16 10:17:29 -06001650#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001651
Jon Ashburnc624c882015-07-16 10:17:29 -06001652 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001653}
1654
Jon Ashburn23d36b12016-02-02 17:47:28 -07001655static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001656 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001657
1658 if (g_loader_debug > 0)
1659 return;
1660
1661 g_loader_debug = 0;
1662
1663 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001664 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001665 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001666 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001667 size_t len;
1668
1669 if (p)
1670 len = p - env;
1671 else
1672 len = strlen(env);
1673
1674 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001675 if (strncmp(env, "all", len) == 0) {
1676 g_loader_debug = ~0u;
1677 g_loader_log_msgs = ~0u;
1678 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001679 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001680 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001681 } else if (strncmp(env, "info", len) == 0) {
1682 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001683 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001684 } else if (strncmp(env, "perf", len) == 0) {
1685 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001686 g_loader_log_msgs |=
1687 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001688 } else if (strncmp(env, "error", len) == 0) {
1689 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001690 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001691 } else if (strncmp(env, "debug", len) == 0) {
1692 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001693 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001694 }
1695 }
1696
1697 if (!p)
1698 break;
1699
1700 env = p + 1;
1701 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001702
Mark Young0ad83132016-06-30 13:02:42 -06001703 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001704}
1705
Jon Ashburn23d36b12016-02-02 17:47:28 -07001706void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001707 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001708 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001709 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001710
1711 // initialize logging
1712 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001713
1714 // initial cJSON to use alloc callbacks
1715 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001716 .malloc_fn = loader_instance_tls_heap_alloc,
1717 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001718 };
1719 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001720}
1721
Jon Ashburn2077e382015-06-29 11:25:34 -06001722struct loader_manifest_files {
1723 uint32_t count;
1724 char **filename_list;
1725};
1726
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001727/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001728 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001729 *
1730 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001731 * A pointer to first char in the next path.
1732 * The next path (or NULL) in the list is returned in next_path.
1733 * Note: input string is modified in some cases. PASS IN A COPY!
1734 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001735static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001736 uint32_t len;
1737 char *next;
1738
1739 if (path == NULL)
1740 return NULL;
1741 next = strchr(path, PATH_SEPERATOR);
1742 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001743 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001744 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001745 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001746 *next = '\0';
1747 next++;
1748 }
1749
1750 return next;
1751}
1752
1753/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001754 * Given a path which is absolute or relative, expand the path if relative or
1755 * leave the path unmodified if absolute. The base path to prepend to relative
1756 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001757 *
1758 * \returns
1759 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001760 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001761static void loader_expand_path(const char *path, const char *rel_base,
1762 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001763 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001764 // do not prepend a base to an absolute path
1765 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001766 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001767
1768 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001769}
1770
1771/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001772 * Given a filename (file) and a list of paths (dir), try to find an existing
1773 * file in the paths. If filename already is a path then no
1774 * searching in the given paths.
1775 *
1776 * \returns
1777 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001778 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001779static void loader_get_fullpath(const char *file, const char *dirs,
1780 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001781 if (!loader_platform_is_path(file) && *dirs) {
1782 char *dirs_copy, *dir, *next_dir;
1783
1784 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1785 strcpy(dirs_copy, dirs);
1786
Jon Ashburn23d36b12016-02-02 17:47:28 -07001787 // find if file exists after prepending paths in given list
1788 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001789 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001790 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1791 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001792 if (loader_platform_file_exists(out_fullpath)) {
1793 return;
1794 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001795 }
1796 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001797
Jon Ashburn2077e382015-06-29 11:25:34 -06001798 snprintf(out_fullpath, out_size, "%s", file);
1799}
1800
1801/**
1802 * Read a JSON file into a buffer.
1803 *
1804 * \returns
1805 * A pointer to a cJSON object representing the JSON parse tree.
1806 * This returned buffer should be freed by caller.
1807 */
Mark Young3a587792016-08-19 15:25:08 -06001808static VkResult loader_get_json(const struct loader_instance *inst,
1809 const char *filename, cJSON **json) {
1810 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001811 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001812 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001813 VkResult res = VK_SUCCESS;
1814
1815 if (NULL == json) {
1816 res = VK_ERROR_INITIALIZATION_FAILED;
1817 goto out;
1818 }
1819
1820 *json = NULL;
1821
Jon Ashburn23d36b12016-02-02 17:47:28 -07001822 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001823 if (!file) {
Mark Young3a587792016-08-19 15:25:08 -06001824 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001825 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1826 "Couldn't open JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001827 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001828 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001829 fseek(file, 0, SEEK_END);
1830 len = ftell(file);
1831 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001832 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001833 if (json_buf == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06001834 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001835 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1836 "Out of memory can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001837 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001838 }
1839 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Young3a587792016-08-19 15:25:08 -06001840 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001841 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1842 "fread failed can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001843 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001844 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001845 json_buf[len] = '\0';
1846
Jon Ashburn23d36b12016-02-02 17:47:28 -07001847 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001848 *json = cJSON_Parse(json_buf);
1849 if (*json == NULL) {
1850 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001851 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1852 "Can't parse JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001853 goto out;
1854 }
1855
1856out:
1857 if (NULL != file) {
1858 fclose(file);
1859 }
1860
1861 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001862}
1863
1864/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001865 * Do a deep copy of the loader_layer_properties structure.
1866 */
Mark Young0ad83132016-06-30 13:02:42 -06001867VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1868 struct loader_layer_properties *dst,
1869 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001870 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001871 memcpy(dst, src, sizeof(*src));
1872 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001873 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1874 src->instance_extension_list.count,
1875 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1876 if (NULL == dst->instance_extension_list.list) {
1877 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1878 "alloc failed for instance extension list");
1879 return VK_ERROR_OUT_OF_HOST_MEMORY;
1880 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001881 dst->instance_extension_list.capacity =
1882 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001883 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001884 dst->instance_extension_list.capacity);
1885 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001886 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1887 src->device_extension_list.count,
1888 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1889 if (NULL == dst->device_extension_list.list) {
1890 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1891 "alloc failed for device extension list");
1892 return VK_ERROR_OUT_OF_HOST_MEMORY;
1893 }
1894 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) *
1895 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001896
Jon Ashburn23d36b12016-02-02 17:47:28 -07001897 dst->device_extension_list.capacity =
1898 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001899 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001900 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001901 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001902 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001903 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001904 dst->device_extension_list.list->entrypoints =
1905 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1906 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1907 if (NULL == dst->device_extension_list.list->entrypoints) {
1908 loader_log(
1909 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1910 "alloc failed for device extension list entrypoint array");
1911 return VK_ERROR_OUT_OF_HOST_MEMORY;
1912 }
1913 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
1914
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001915 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001916 dst->device_extension_list.list->entrypoints[i] =
1917 loader_instance_heap_alloc(
1918 inst,
1919 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1920 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1921 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1922 loader_log(
1923 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1924 "alloc failed for device extension list entrypoint %d", i);
1925 return VK_ERROR_OUT_OF_HOST_MEMORY;
1926 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001927 strcpy(dst->device_extension_list.list->entrypoints[i],
1928 src->device_extension_list.list->entrypoints[i]);
1929 }
1930 }
Mark Young0ad83132016-06-30 13:02:42 -06001931
1932 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001933}
1934
Jon Ashburn86a527a2016-02-10 20:59:26 -07001935static bool
1936loader_find_layer_name_list(const char *name,
1937 const struct loader_layer_list *layer_list) {
1938 if (!layer_list)
1939 return false;
1940 for (uint32_t j = 0; j < layer_list->count; j++)
1941 if (!strcmp(name, layer_list->list[j].info.layerName))
1942 return true;
1943 return false;
1944}
1945
1946static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1947 const char **layer_list) {
1948 if (!layer_list)
1949 return false;
1950 for (uint32_t j = 0; j < layer_count; j++)
1951 if (!strcmp(name, layer_list[j]))
1952 return true;
1953 return false;
1954}
1955
Jon Ashburn491cd042016-05-16 14:01:18 -06001956bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001957 const char *name, uint32_t layer_count,
1958 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1959 if (!layer_list)
1960 return false;
1961 for (uint32_t j = 0; j < layer_count; j++)
1962 if (!strcmp(name, layer_list[j]))
1963 return true;
1964 return false;
1965}
1966
1967/**
1968 * Searches through an array of layer names (ppp_layer_names) looking for a
1969 * layer key_name.
1970 * If not found then simply returns updating nothing.
1971 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12001972 * Any duplicate (pre-existing) expand_names in layer names are removed.
1973 * Order is otherwise preserved, with the layer key_name being replaced by the
1974 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07001975 * @param inst
1976 * @param layer_count
1977 * @param ppp_layer_names
1978 */
Mark Young0ad83132016-06-30 13:02:42 -06001979VkResult loader_expand_layer_names(
1980 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07001981 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06001982 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07001983
Jon Ashburncc407a22016-04-15 09:25:03 -06001984 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001985
Jon Ashburncc407a22016-04-15 09:25:03 -06001986 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06001987 (char const **)pp_src_layers)) {
1988 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06001989 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06001990 }
Jon Ashburn71483442016-02-11 18:59:43 -07001991
Jon Ashburn86a527a2016-02-10 20:59:26 -07001992 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
1993 "Found meta layer %s, replacing with actual layer group",
1994 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12001995
Jon Ashburn491cd042016-05-16 14:01:18 -06001996 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06001997 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06001998 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
1999 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002000 if (NULL == pp_dst_layers) {
2001 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2002 "alloc failed for dst layer array");
2003 return VK_ERROR_OUT_OF_HOST_MEMORY;
2004 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002005
2006 // copy layers from src to dst, stripping key_name and anything in
2007 // expand_names.
2008 uint32_t src_index, dst_index = 0;
2009 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002010 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
2011 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002012 continue;
2013 }
2014
2015 if (!strcmp(pp_src_layers[src_index], key_name)) {
2016 // insert all expand_names in place of key_name
2017 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06002018 for (expand_index = 0; expand_index < expand_count;
2019 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002020 pp_dst_layers[dst_index++] = expand_names[expand_index];
2021 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002022 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002023 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002024
2025 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002026 }
2027
Chris Forbesbd9de052016-04-06 20:49:02 +12002028 *ppp_layer_names = pp_dst_layers;
2029 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002030
2031 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002032}
2033
Chris Forbesbd9de052016-04-06 20:49:02 +12002034void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
2035 const VkInstanceCreateInfo *orig,
2036 VkInstanceCreateInfo *ours) {
2037 /* Free the layer names array iff we had to reallocate it */
2038 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002039 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002040 }
2041}
2042
Jon Ashburn491cd042016-05-16 14:01:18 -06002043void loader_init_std_validation_props(struct loader_layer_properties *props) {
2044 memset(props, 0, sizeof(struct loader_layer_properties));
2045 props->type = VK_LAYER_TYPE_META_EXPLICT;
2046 strncpy(props->info.description, "LunarG Standard Validation Layer",
2047 sizeof (props->info.description));
2048 props->info.implementationVersion = 1;
2049 strncpy(props->info.layerName, std_validation_str,
2050 sizeof (props->info.layerName));
2051 // TODO what about specVersion? for now insert loader's built version
2052 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2053}
2054
Jon Ashburn86a527a2016-02-10 20:59:26 -07002055/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002056 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002057 * the set of required layer names. If found then it adds a meta property to the
2058 * layer list.
2059 * Assumes the required layers are the same for both instance and device lists.
2060 * @param inst
2061 * @param layer_count number of layers in layer_names
2062 * @param layer_names array of required layer names
2063 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002064 */
2065static void loader_add_layer_property_meta(
2066 const struct loader_instance *inst, uint32_t layer_count,
2067 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002068 struct loader_layer_list *layer_instance_list) {
2069 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002070 bool found;
2071 struct loader_layer_list *layer_list;
2072
Jon Ashburn491cd042016-05-16 14:01:18 -06002073 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002074 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002075 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002076 return;
2077
Jon Ashburn491cd042016-05-16 14:01:18 -06002078
2079 layer_list = layer_instance_list;
2080
2081 found = true;
2082 if (layer_list == NULL)
2083 return;
2084 for (i = 0; i < layer_count; i++) {
2085 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002086 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002087 found = false;
2088 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002089 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002090
2091 struct loader_layer_properties *props;
2092 if (found) {
2093 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002094 if (NULL == props) {
2095 // Error already triggered in loader_get_next_layer_property.
2096 return;
2097 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002098 loader_init_std_validation_props(props);
2099
2100 }
2101
Jon Ashburn86a527a2016-02-10 20:59:26 -07002102}
2103
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002104static void loader_read_json_layer(
2105 const struct loader_instance *inst,
2106 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2107 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2108 char *temp;
2109 char *name, *type, *library_path, *api_version;
2110 char *implementation_version, *description;
2111 cJSON *ext_item;
2112 VkExtensionProperties ext_prop;
2113
Mark Young0ad83132016-06-30 13:02:42 -06002114/*
2115 * The following are required in the "layer" object:
2116 * (required) "name"
2117 * (required) "type"
2118 * (required) “library_path”
2119 * (required) “api_version”
2120 * (required) “implementation_version”
2121 * (required) “description”
2122 * (required for implicit layers) “disable_environment”
2123 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002124
Jon Ashburn23d36b12016-02-02 17:47:28 -07002125#define GET_JSON_OBJECT(node, var) \
2126 { \
2127 var = cJSON_GetObjectItem(node, #var); \
2128 if (var == NULL) { \
2129 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002130 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002131 "Didn't find required layer object %s in manifest " \
2132 "JSON file, skipping this layer", \
2133 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002134 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002135 } \
2136 }
2137#define GET_JSON_ITEM(node, var) \
2138 { \
2139 item = cJSON_GetObjectItem(node, #var); \
2140 if (item == NULL) { \
2141 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002142 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002143 "Didn't find required layer value %s in manifest JSON " \
2144 "file, skipping this layer", \
2145 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002146 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002147 } \
2148 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002149 if (temp == NULL) { \
2150 layer_node = layer_node->next; \
2151 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2152 "Problem accessing layer value %s in manifest JSON " \
2153 "file, skipping this layer", \
2154 #var); \
2155 return; \
2156 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002157 temp[strlen(temp) - 1] = '\0'; \
2158 var = loader_stack_alloc(strlen(temp) + 1); \
2159 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002160 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002161 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002162 GET_JSON_ITEM(layer_node, name)
2163 GET_JSON_ITEM(layer_node, type)
2164 GET_JSON_ITEM(layer_node, library_path)
2165 GET_JSON_ITEM(layer_node, api_version)
2166 GET_JSON_ITEM(layer_node, implementation_version)
2167 GET_JSON_ITEM(layer_node, description)
2168 if (is_implicit) {
2169 GET_JSON_OBJECT(layer_node, disable_environment)
2170 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002171#undef GET_JSON_ITEM
2172#undef GET_JSON_OBJECT
2173
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002174 // add list entry
2175 struct loader_layer_properties *props = NULL;
2176 if (!strcmp(type, "DEVICE")) {
2177 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2178 "Device layers are deprecated skipping this layer");
2179 layer_node = layer_node->next;
2180 return;
2181 }
2182 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2183 // layers that must work with older loaders
2184 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2185 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002186 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002187 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002188 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002189 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002190 if (NULL == props) {
2191 // Error already triggered in loader_get_next_layer_property.
2192 return;
2193 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002194 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2195 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2196 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002197
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002198 if (props == NULL) {
2199 layer_node = layer_node->next;
2200 return;
2201 }
Jon Ashburn15315172015-07-07 15:06:25 -06002202
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002203 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2204 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2205
2206 char *fullpath = props->lib_name;
2207 char *rel_base;
2208 if (loader_platform_is_path(library_path)) {
2209 // a relative or absolute path
2210 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2211 strcpy(name_copy, filename);
2212 rel_base = loader_platform_dirname(name_copy);
2213 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2214 } else {
2215 // a filename which is assumed in a system directory
2216 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2217 MAX_STRING_SIZE, fullpath);
2218 }
2219 props->info.specVersion = loader_make_version(api_version);
2220 props->info.implementationVersion = atoi(implementation_version);
2221 strncpy((char *)props->info.description, description,
2222 sizeof(props->info.description));
2223 props->info.description[sizeof(props->info.description) - 1] = '\0';
2224 if (is_implicit) {
2225 if (!disable_environment || !disable_environment->child) {
2226 loader_log(
2227 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2228 "Didn't find required layer child value disable_environment"
2229 "in manifest JSON file, skipping this layer");
2230 layer_node = layer_node->next;
2231 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002232 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002233 strncpy(props->disable_env_var.name, disable_environment->child->string,
2234 sizeof(props->disable_env_var.name));
2235 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2236 '\0';
2237 strncpy(props->disable_env_var.value,
2238 disable_environment->child->valuestring,
2239 sizeof(props->disable_env_var.value));
2240 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2241 '\0';
2242 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002243
Jon Ashburn23d36b12016-02-02 17:47:28 -07002244/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002245* Now get all optional items and objects and put in list:
2246* functions
2247* instance_extensions
2248* device_extensions
2249* enable_environment (implicit layers only)
2250*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002251#define GET_JSON_OBJECT(node, var) \
2252 { var = cJSON_GetObjectItem(node, #var); }
2253#define GET_JSON_ITEM(node, var) \
2254 { \
2255 item = cJSON_GetObjectItem(node, #var); \
2256 if (item != NULL) { \
2257 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002258 if (temp != NULL) { \
2259 temp[strlen(temp) - 1] = '\0'; \
2260 var = loader_stack_alloc(strlen(temp) + 1); \
2261 strcpy(var, &temp[1]); \
2262 cJSON_Free(temp); \
2263 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002264 } \
2265 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002266
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002267 cJSON *instance_extensions, *device_extensions, *functions,
2268 *enable_environment;
2269 cJSON *entrypoints;
2270 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2271 char **entry_array;
2272 vkGetInstanceProcAddr = NULL;
2273 vkGetDeviceProcAddr = NULL;
2274 spec_version = NULL;
2275 entrypoints = NULL;
2276 entry_array = NULL;
2277 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002278
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002279 /**
2280 * functions
2281 * vkGetInstanceProcAddr
2282 * vkGetDeviceProcAddr
2283 */
2284 GET_JSON_OBJECT(layer_node, functions)
2285 if (functions != NULL) {
2286 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2287 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2288 if (vkGetInstanceProcAddr != NULL)
2289 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2290 sizeof(props->functions.str_gipa));
2291 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2292 if (vkGetDeviceProcAddr != NULL)
2293 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2294 sizeof(props->functions.str_gdpa));
2295 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2296 }
2297 /**
2298 * instance_extensions
2299 * array of
2300 * name
2301 * spec_version
2302 */
2303 GET_JSON_OBJECT(layer_node, instance_extensions)
2304 if (instance_extensions != NULL) {
2305 int count = cJSON_GetArraySize(instance_extensions);
2306 for (i = 0; i < count; i++) {
2307 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2308 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002309 if (name != NULL) {
2310 strncpy(ext_prop.extensionName, name,
2311 sizeof(ext_prop.extensionName));
2312 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2313 '\0';
2314 }
Mark Young0ad83132016-06-30 13:02:42 -06002315 GET_JSON_ITEM(ext_item, spec_version)
2316 if (NULL != spec_version) {
2317 ext_prop.specVersion = atoi(spec_version);
2318 } else {
2319 ext_prop.specVersion = 0;
2320 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002321 bool ext_unsupported =
2322 wsi_unsupported_instance_extension(&ext_prop);
2323 if (!ext_unsupported) {
2324 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2325 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002326 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002327 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002328 }
2329 /**
2330 * device_extensions
2331 * array of
2332 * name
2333 * spec_version
2334 * entrypoints
2335 */
2336 GET_JSON_OBJECT(layer_node, device_extensions)
2337 if (device_extensions != NULL) {
2338 int count = cJSON_GetArraySize(device_extensions);
2339 for (i = 0; i < count; i++) {
2340 ext_item = cJSON_GetArrayItem(device_extensions, i);
2341 GET_JSON_ITEM(ext_item, name)
2342 GET_JSON_ITEM(ext_item, spec_version)
2343 if (name != NULL) {
2344 strncpy(ext_prop.extensionName, name,
2345 sizeof(ext_prop.extensionName));
2346 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2347 '\0';
2348 }
Mark Young0ad83132016-06-30 13:02:42 -06002349 if (NULL != spec_version) {
2350 ext_prop.specVersion = atoi(spec_version);
2351 } else {
2352 ext_prop.specVersion = 0;
2353 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002354 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2355 GET_JSON_OBJECT(ext_item, entrypoints)
2356 int entry_count;
2357 if (entrypoints == NULL) {
2358 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2359 &ext_prop, 0, NULL);
2360 continue;
2361 }
2362 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002363 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002364 entry_array =
2365 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002366 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002367 for (j = 0; j < entry_count; j++) {
2368 ext_item = cJSON_GetArrayItem(entrypoints, j);
2369 if (ext_item != NULL) {
2370 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002371 if (NULL == temp) {
2372 entry_array[j] = NULL;
2373 continue;
2374 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002375 temp[strlen(temp) - 1] = '\0';
2376 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2377 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002378 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002379 }
2380 }
2381 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2382 &ext_prop, entry_count, entry_array);
2383 }
2384 }
2385 if (is_implicit) {
2386 GET_JSON_OBJECT(layer_node, enable_environment)
2387
2388 // enable_environment is optional
2389 if (enable_environment) {
2390 strncpy(props->enable_env_var.name,
2391 enable_environment->child->string,
2392 sizeof(props->enable_env_var.name));
2393 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2394 '\0';
2395 strncpy(props->enable_env_var.value,
2396 enable_environment->child->valuestring,
2397 sizeof(props->enable_env_var.value));
2398 props->enable_env_var
2399 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2400 }
2401 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002402#undef GET_JSON_ITEM
2403#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002404}
2405
2406/**
2407 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2408 * file, add entry to the layer_list. Fill out the layer_properties in this list
2409 * entry from the input cJSON object.
2410 *
2411 * \returns
2412 * void
2413 * layer_list has a new entry and initialized accordingly.
2414 * If the json input object does not have all the required fields no entry
2415 * is added to the list.
2416 */
2417static void
2418loader_add_layer_properties(const struct loader_instance *inst,
2419 struct loader_layer_list *layer_instance_list,
2420 cJSON *json, bool is_implicit, char *filename) {
2421 /* Fields in layer manifest file that are required:
2422 * (required) “file_format_version”
2423 *
Mark Young0ad83132016-06-30 13:02:42 -06002424 * If more than one "layer" object are to be used, use the "layers" array
2425 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002426 *
2427 * First get all required items and if any missing abort
2428 */
2429
2430 cJSON *item, *layers_node, *layer_node;
2431 uint16_t file_major_vers = 0;
2432 uint16_t file_minor_vers = 0;
2433 uint16_t file_patch_vers = 0;
2434 char *vers_tok;
2435 cJSON *disable_environment = NULL;
2436 item = cJSON_GetObjectItem(json, "file_format_version");
2437 if (item == NULL) {
2438 return;
2439 }
2440 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002441 if (NULL == file_vers) {
2442 return;
2443 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002444 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2445 "Found manifest file %s, version %s", filename, file_vers);
2446 // Get the major/minor/and patch as integers for easier comparison
2447 vers_tok = strtok(file_vers, ".\"\n\r");
2448 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002449 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002450 vers_tok = strtok(NULL, ".\"\n\r");
2451 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002452 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002453 vers_tok = strtok(NULL, ".\"\n\r");
2454 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002455 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002456 }
2457 }
2458 }
2459 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2460 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2461 "%s Unexpected manifest file version (expected 1.0.0 or "
2462 "1.0.1), may cause errors",
2463 filename);
2464 }
Mark Young0ad83132016-06-30 13:02:42 -06002465 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002466 // If "layers" is present, read in the array of layer objects
2467 layers_node = cJSON_GetObjectItem(json, "layers");
2468 if (layers_node != NULL) {
2469 int numItems = cJSON_GetArraySize(layers_node);
2470 if (file_major_vers == 1 && file_minor_vers == 0 &&
2471 file_patch_vers == 0) {
2472 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2473 "\"layers\" tag not officially added until file version "
2474 "1.0.1, but %s is reporting version %s",
2475 filename, file_vers);
2476 }
2477 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2478 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2479 if (layer_node == NULL) {
2480 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2481 "Can't find \"layers\" array element %d object in "
2482 "manifest JSON file %s, skipping this file",
2483 curLayer, filename);
2484 return;
2485 }
2486 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2487 disable_environment, is_implicit, filename);
2488 }
2489 } else {
2490 // Otherwise, try to read in individual layers
2491 layer_node = cJSON_GetObjectItem(json, "layer");
2492 if (layer_node == NULL) {
2493 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2494 "Can't find \"layer\" object in manifest JSON file %s, "
2495 "skipping this file",
2496 filename);
2497 return;
2498 }
2499 // Loop through all "layer" objects in the file to get a count of them
2500 // first.
2501 uint16_t layer_count = 0;
2502 cJSON *tempNode = layer_node;
2503 do {
2504 tempNode = tempNode->next;
2505 layer_count++;
2506 } while (tempNode != NULL);
2507 /*
2508 * Throw a warning if we encounter multiple "layer" objects in file
2509 * versions newer than 1.0.0. Having multiple objects with the same
2510 * name at the same level is actually a JSON standard violation.
2511 */
2512 if (layer_count > 1 &&
2513 (file_major_vers > 1 ||
2514 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2515 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2516 "Multiple \"layer\" nodes are deprecated starting in "
2517 "file version \"1.0.1\". Please use \"layers\" : [] "
2518 "array instead in %s.",
2519 filename);
2520 } else {
2521 do {
2522 loader_read_json_layer(inst, layer_instance_list, layer_node,
2523 item, disable_environment, is_implicit,
2524 filename);
2525 layer_node = layer_node->next;
2526 } while (layer_node != NULL);
2527 }
2528 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002529 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002530}
2531
2532/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002533 * Find the Vulkan library manifest files.
2534 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002535 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002536 * for a list of JSON manifest files. If env_override is non-NULL
2537 * and has a valid value. Then the location is ignored. Otherwise
2538 * location is used to look for manifest files. The location
2539 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002540 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002541 * directory to look at. It is expanded into the dir path
2542 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2543 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002544 *
2545 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002546 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002547 * A string list of manifest files to be opened in out_files param.
2548 * List has a pointer to string for each manifest filename.
2549 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002550 * Location or override string lists can be either files or directories as
2551 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002552 * | location | override
2553 * --------------------------------
2554 * Win ICD | files | files
2555 * Win Layer | files | dirs
2556 * Linux ICD | dirs | files
2557 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002558 */
Mark Young0ad83132016-06-30 13:02:42 -06002559static VkResult loader_get_manifest_files(
2560 const struct loader_instance *inst, const char *env_override,
2561 char *source_override, bool is_layer, const char *location,
2562 const char *home_location, struct loader_manifest_files *out_files) {
2563 char * override = NULL;
2564 char *loc, *orig_loc = NULL;
2565 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002566 char *file, *next_file, *name;
2567 size_t alloced_count = 64;
2568 char full_path[2048];
2569 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002570 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002571 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002572 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002573
2574 out_files->count = 0;
2575 out_files->filename_list = NULL;
2576
Jamie Madill00c3c912016-04-06 18:26:46 -04002577 if (source_override != NULL) {
2578 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002579 } else if (env_override != NULL &&
2580 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002581#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002582 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002583 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002584 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002585 override = NULL;
2586 }
2587#endif
2588 }
2589
Jon Ashburnb6822212016-02-16 15:34:16 -07002590#if !defined(_WIN32)
2591 if (location == NULL && home_location == NULL) {
2592#else
2593 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002594 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002595#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002596 loader_log(
2597 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002598 "Can't get manifest files with NULL location, env_override=%s",
2599 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002600 res = VK_ERROR_INITIALIZATION_FAILED;
2601 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002602 }
2603
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002604#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002605 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002606#else
2607 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002608#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002609 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002610 // Also handle getting the location(s) from registry on Windows
2611 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002612 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002613 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002614 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2615 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002616 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2617 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002618 }
2619 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002620#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002621 reg = loader_get_registry_files(inst, loc);
2622 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002623 if (!is_layer) {
2624 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2625 "Registry lookup failed can't get ICD manifest "
2626 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002627 // This typically only fails when out of memory, which is
2628 // critical
2629 // if this is for the loader.
2630 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002631 } else {
2632 // warning only for layers
2633 loader_log(
2634 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2635 "Registry lookup failed can't get layer manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002636 // Return success for now since it's not critical for layers
2637 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002638 }
Mark Young0ad83132016-06-30 13:02:42 -06002639 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002640 }
Mark Young0ad83132016-06-30 13:02:42 -06002641 orig_loc = loc;
2642 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002643#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002644 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002645 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002646 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002647 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2648 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002649 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2650 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002651 }
2652 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002653 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002654 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002655 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002656 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002657
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002658 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002659 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2660 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002661
Jon Ashburn2077e382015-06-29 11:25:34 -06002662 file = loc;
2663 while (*file) {
2664 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002665 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002666 sysdir = opendir(file);
2667 name = NULL;
2668 if (sysdir) {
2669 dent = readdir(sysdir);
2670 if (dent == NULL)
2671 break;
2672 name = &(dent->d_name[0]);
2673 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2674 name = full_path;
2675 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002676 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002677#if defined(_WIN32)
2678 name = file;
2679#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002680 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002681 char *dir;
2682 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002683 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002684 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002685 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2686 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002687 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002688 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002689 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002690
2691 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2692
2693 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002694#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002695 }
2696 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002697 /* Look for files ending with ".json" suffix */
2698 uint32_t nlen = (uint32_t)strlen(name);
2699 const char *suf = name + nlen - 5;
2700 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2701 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002702 out_files->filename_list = loader_instance_heap_alloc(
2703 inst, alloced_count * sizeof(char *),
2704 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002705 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002706 out_files->filename_list = loader_instance_heap_realloc(
2707 inst, out_files->filename_list,
2708 alloced_count * sizeof(char *),
2709 alloced_count * sizeof(char *) * 2,
2710 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002711 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002712 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002713 if (out_files->filename_list == NULL) {
2714 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2715 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002716 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2717 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002718 }
Mark Young0ad83132016-06-30 13:02:42 -06002719 out_files->filename_list[out_files->count] =
2720 loader_instance_heap_alloc(
2721 inst, strlen(name) + 1,
2722 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002723 if (out_files->filename_list[out_files->count] == NULL) {
2724 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2725 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002726 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2727 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002728 }
2729 strcpy(out_files->filename_list[out_files->count], name);
2730 out_files->count++;
2731 } else if (!list_is_dirs) {
2732 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002733 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002734 "Skipping manifest file %s, file name must end in .json",
2735 name);
2736 }
2737 if (list_is_dirs) {
2738 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002739 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002740 break;
Mark Young0ad83132016-06-30 13:02:42 -06002741 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002742 name = &(dent->d_name[0]);
2743 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2744 name = full_path;
2745 } else {
2746 break;
2747 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002748 }
Mark Young0ad83132016-06-30 13:02:42 -06002749 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002750 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002751 sysdir = NULL;
2752 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002753 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002754#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002755 if (home_location != NULL &&
2756 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002757 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2758 size_t len;
2759 if (xdgdatahome != NULL) {
2760
2761 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002762 strlen(home_location));
2763 if (home_loc == NULL) {
2764 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002765 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002766 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2767 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002768 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002769 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002770 // Add directory separator if needed
2771 if (home_location[0] != DIRECTORY_SYMBOL) {
2772 len = strlen(home_loc);
2773 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002774 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002775 }
2776 strcat(home_loc, home_location);
2777 file = home_loc;
2778 next_file = loader_get_next_path(file);
2779 home_location = NULL;
2780
Jon Ashburn1530c342016-02-26 13:14:27 -07002781 loader_log(
2782 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002783 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002784 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002785 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002786
2787 } else {
2788
2789 char *home = secure_getenv("HOME");
2790 if (home != NULL) {
2791 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2792 strlen(home_location));
2793 if (home_loc == NULL) {
2794 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2795 "Out of memory can't get manifest files");
2796 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2797 goto out;
2798 }
2799 strcpy(home_loc, home);
2800
2801 len = strlen(home);
2802 if (home[len] != DIRECTORY_SYMBOL) {
2803 home_loc[len] = DIRECTORY_SYMBOL;
2804 home_loc[len + 1] = '\0';
2805 }
2806 strcat(home_loc, ".local/share");
2807
2808 if (home_location[0] != DIRECTORY_SYMBOL) {
2809 len = strlen(home_loc);
2810 home_loc[len] = DIRECTORY_SYMBOL;
2811 home_loc[len + 1] = '\0';
2812 }
2813 strcat(home_loc, home_location);
2814 file = home_loc;
2815 next_file = loader_get_next_path(file);
2816 home_location = NULL;
2817
2818 loader_log(
2819 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2820 "Searching the following path for manifest files: %s\n",
2821 home_loc);
2822 list_is_dirs = true;
2823 } else {
2824 // without knowing HOME, we just.. give up
2825 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002826 }
2827 }
2828#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002829 }
Mark Young0ad83132016-06-30 13:02:42 -06002830
2831out:
2832 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2833 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2834 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2835 }
2836 loader_instance_heap_free(inst, out_files->filename_list);
2837 out_files->count = 0;
2838 out_files->filename_list = NULL;
2839 }
2840
2841 if (NULL != sysdir) {
2842 closedir(sysdir);
2843 }
2844
2845 if (NULL != reg && reg != orig_loc) {
2846 loader_instance_heap_free(inst, reg);
2847 }
2848 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002849}
2850
Jon Ashburn23d36b12016-02-02 17:47:28 -07002851void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002852
Jon Ashburn23d36b12016-02-02 17:47:28 -07002853void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002854/**
2855 * Try to find the Vulkan ICD driver(s).
2856 *
2857 * This function scans the default system loader path(s) or path
2858 * specified by the \c VK_ICD_FILENAMES environment variable in
2859 * order to find loadable VK ICDs manifest files. From these
2860 * manifest files it finds the ICD libraries.
2861 *
2862 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002863 * Vulkan result
2864 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002865 */
Mark Young0ad83132016-06-30 13:02:42 -06002866VkResult loader_icd_scan(const struct loader_instance *inst,
2867 struct loader_icd_libs *icds) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002868 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002869 uint16_t file_major_vers = 0;
2870 uint16_t file_minor_vers = 0;
2871 uint16_t file_patch_vers = 0;
2872 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002873 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002874 VkResult res = VK_SUCCESS;
2875 bool lockedMutex = false;
2876 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002877 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002878
Mark Young0ad83132016-06-30 13:02:42 -06002879 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2880
2881 res = loader_scanned_icd_init(inst, icds);
2882 if (VK_SUCCESS != res) {
2883 goto out;
2884 }
2885
Jon Ashburn2077e382015-06-29 11:25:34 -06002886 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002887 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
2888 DEFAULT_VK_DRIVERS_INFO,
2889 HOME_VK_DRIVERS_INFO, &manifest_files);
2890 if (VK_SUCCESS != res || manifest_files.count == 0) {
2891 goto out;
2892 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002893 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002894 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002895 for (uint32_t i = 0; i < manifest_files.count; i++) {
2896 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002897 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002898 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002899 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002900
Mark Young3a587792016-08-19 15:25:08 -06002901 res = loader_get_json(inst, file_str, &json);
2902 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2903 break;
2904 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002905 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002906 }
Mark Young3a587792016-08-19 15:25:08 -06002907
Jon Ashburn005617f2015-11-17 17:35:40 -07002908 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002909 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002910 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002911 if (num_good_icds == 0) {
2912 res = VK_ERROR_INITIALIZATION_FAILED;
2913 }
2914 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002915 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002916 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002917 if (NULL == file_vers) {
2918 // Only reason the print can fail is if there was an allocation
2919 // issue
Mark Young3a587792016-08-19 15:25:08 -06002920 if (num_good_icds == 0) {
2921 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2922 }
2923 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002924 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002925 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2926 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002927 // Get the major/minor/and patch as integers for easier comparison
2928 vers_tok = strtok(file_vers, ".\"\n\r");
2929 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002930 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002931 vers_tok = strtok(NULL, ".\"\n\r");
2932 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002933 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002934 vers_tok = strtok(NULL, ".\"\n\r");
2935 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002936 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002937 }
2938 }
2939 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002940 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002941 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002942 "Unexpected manifest file version (expected 1.0.0 or "
2943 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002944 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002945 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002946 itemICD = cJSON_GetObjectItem(json, "ICD");
2947 if (itemICD != NULL) {
2948 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002949 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002950 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002951 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002952 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002953 "Can't find \"library_path\" in ICD JSON file "
2954 "%s, skipping",
2955 file_str);
Mark Young3a587792016-08-19 15:25:08 -06002956 if (num_good_icds == 0) {
2957 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2958 }
Mark Young0ad83132016-06-30 13:02:42 -06002959 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002960 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002961 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002962 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06002963 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002964 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06002965 temp[strlen(temp) - 1] = '\0';
2966 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06002967 if (NULL == library_path) {
2968 loader_log(
2969 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2970 "Can't allocate space for \"library_path\" in ICD "
2971 "JSON file %s, skipping",
2972 file_str);
2973 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2974 cJSON_Free(temp);
2975 cJSON_Delete(json);
2976 json = NULL;
2977 goto out;
2978 }
Jon Ashburn86251302015-08-25 16:48:24 -06002979 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002980 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06002981 if (strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002982 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002983 "Can't find \"library_path\" in ICD JSON file "
2984 "%s, skipping",
2985 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06002986 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002987 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002988 continue;
2989 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04002990 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06002991 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002992 loader_log(
2993 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04002994 "Searching for ICD drivers named %s default dir %s\n",
2995 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002996 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06002997 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002998 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
2999 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003000 strcpy(name_copy, file_str);
3001 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003002 loader_expand_path(library_path, rel_base, sizeof(fullpath),
3003 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003004 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003005 // a filename which is assumed in a system directory
3006 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
3007 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003008 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003009
3010 uint32_t vers = 0;
3011 item = cJSON_GetObjectItem(itemICD, "api_version");
3012 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003013 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003014 if (NULL == temp) {
3015 // Only reason the print can fail is if there was an
3016 // allocation issue
3017 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3018 goto out;
3019 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003020 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003021 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07003022 }
Mark Young3a587792016-08-19 15:25:08 -06003023 res = loader_scanned_icd_add(inst, icds, fullpath, vers);
3024 if (VK_SUCCESS != res) {
3025 goto out;
3026 }
3027 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003028 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003029 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003030 "Can't find \"library_path\" object in ICD JSON "
3031 "file %s, skipping",
3032 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003033 }
3034 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003035 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003036 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003037 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
3038 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003039 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003040
Mark Young0ad83132016-06-30 13:02:42 -06003041 cJSON_Delete(json);
3042 json = NULL;
3043 }
3044
3045out:
3046 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003047 cJSON_Delete(json);
3048 }
Mark Young0ad83132016-06-30 13:02:42 -06003049 if (NULL != manifest_files.filename_list) {
3050 for (uint32_t i = 0; i < manifest_files.count; i++) {
3051 if (NULL != manifest_files.filename_list[i]) {
3052 loader_instance_heap_free(inst,
3053 manifest_files.filename_list[i]);
3054 }
3055 }
3056 loader_instance_heap_free(inst, manifest_files.filename_list);
3057 }
3058 if (lockedMutex) {
3059 loader_platform_thread_unlock_mutex(&loader_json_lock);
3060 }
3061 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003062}
3063
Jon Ashburn23d36b12016-02-02 17:47:28 -07003064void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003065 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003066 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003067 struct loader_manifest_files
3068 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003069 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003070 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003071 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003072
Mark Young0ad83132016-06-30 13:02:42 -06003073 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3074
3075 // Get a list of manifest files for explicit layers
3076 if (VK_SUCCESS !=
3077 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
3078 true, DEFAULT_VK_ELAYERS_INFO,
3079 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3080 goto out;
3081 }
3082
3083 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003084 // Pass NULL for environment variable override - implicit layers are not
3085 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003086 if (VK_SUCCESS != loader_get_manifest_files(
3087 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
3088 HOME_VK_ILAYERS_INFO, &manifest_files[1])) {
3089 goto out;
3090 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003091
Mark Young0ad83132016-06-30 13:02:42 -06003092 // Make sure we have at least one layer, if not, go ahead and return
3093 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3094 goto out;
3095 }
3096
3097 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003098 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003099
Jon Ashburn6461ef22015-09-22 13:11:00 -06003100 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003101 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003102 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003103 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003104 file_str = manifest_files[implicit].filename_list[i];
3105 if (file_str == NULL)
3106 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003107
Jon Ashburn075ce432015-12-17 17:38:24 -07003108 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003109 VkResult res = loader_get_json(inst, file_str, &json);
3110 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3111 break;
3112 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003113 continue;
3114 }
3115
Jon Ashburn491cd042016-05-16 14:01:18 -06003116 loader_add_layer_properties(inst, instance_layers, json,
3117 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003118 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003119 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003120 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003121
3122 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003123 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3124 sizeof(std_validation_names[0]),
3125 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003126
Mark Young0ad83132016-06-30 13:02:42 -06003127out:
3128
3129 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3130 if (NULL != manifest_files[manFile].filename_list) {
3131 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3132 if (NULL != manifest_files[manFile].filename_list[i]) {
3133 loader_instance_heap_free(
3134 inst, manifest_files[manFile].filename_list[i]);
3135 }
3136 }
3137 loader_instance_heap_free(inst,
3138 manifest_files[manFile].filename_list);
3139 }
3140 }
3141 if (lockedMutex) {
3142 loader_platform_thread_unlock_mutex(&loader_json_lock);
3143 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003144}
3145
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003146void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003147 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003148 char *file_str;
3149 struct loader_manifest_files manifest_files;
3150 cJSON *json;
3151 uint32_t i;
3152
3153 // Pass NULL for environment variable override - implicit layers are not
3154 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003155 VkResult res = loader_get_manifest_files(
3156 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
3157 &manifest_files);
3158 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003159 return;
3160 }
3161
3162 /* cleanup any previously scanned libraries */
3163 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003164
3165 loader_platform_thread_lock_mutex(&loader_json_lock);
3166
3167 for (i = 0; i < manifest_files.count; i++) {
3168 file_str = manifest_files.filename_list[i];
3169 if (file_str == NULL) {
3170 continue;
3171 }
3172
3173 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003174 res = loader_get_json(inst, file_str, &json);
3175 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3176 break;
3177 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003178 continue;
3179 }
3180
Mark Young0ad83132016-06-30 13:02:42 -06003181 loader_add_layer_properties(inst, instance_layers, json, true,
3182 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003183
Mark Young0ad83132016-06-30 13:02:42 -06003184 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003185 cJSON_Delete(json);
3186 }
Mark Young0ad83132016-06-30 13:02:42 -06003187 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003188
3189 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003190 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3191 sizeof(std_validation_names[0]),
3192 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003193
3194 loader_platform_thread_unlock_mutex(&loader_json_lock);
3195}
3196
Jon Ashburn23d36b12016-02-02 17:47:28 -07003197static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3198loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003199 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003200 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003201 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003202 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003203 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003204 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003205
Jon Ashburn27cd5842015-05-12 17:26:48 -06003206 // inst is not wrapped
3207 if (inst == VK_NULL_HANDLE) {
3208 return NULL;
3209 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003210 VkLayerInstanceDispatchTable *disp_table =
3211 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003212 void *addr;
3213
3214 if (disp_table == NULL)
3215 return NULL;
3216
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003217 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003218 addr =
3219 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003220 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003221 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003222 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003223
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003224 // Don't call down the chain, this would be an infinite loop
3225 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003226 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003227 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003228}
3229
Piers Daniell295fe402016-03-29 11:51:11 -06003230static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003231loader_gpa_device_internal(VkDevice device, const char *pName) {
3232 struct loader_device *dev;
3233 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
Piers Daniell295fe402016-03-29 11:51:11 -06003234 return icd->GetDeviceProcAddr(device, pName);
3235}
3236
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003237/**
3238 * Initialize device_ext dispatch table entry as follows:
3239 * If dev == NULL find all logical devices created within this instance and
3240 * init the entry (given by idx) in the ext dispatch table.
3241 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003242 * The initialization value is gotten by calling down the device chain with
3243 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003244 * If GDPA returns NULL then don't initialize the dispatch table entry.
3245 */
3246static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003247 struct loader_device *dev,
3248 uint32_t idx,
3249 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003250
Jon Ashburn23d36b12016-02-02 17:47:28 -07003251{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003252 void *gdpa_value;
3253 if (dev != NULL) {
3254 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003255 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003256 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003257 dev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003258 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003259 } else {
3260 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
3261 struct loader_icd *icd = &inst->icds[i];
Karl Schultz2558bd32016-02-24 14:39:39 -07003262 struct loader_device *ldev = icd->logical_device_list;
3263 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003264 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003265 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3266 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003267 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003268 ldev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003269 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003270 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003271 }
3272 }
3273 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003274}
3275
3276/**
3277 * Find all dev extension in the hash table and initialize the dispatch table
3278 * for dev for each of those extension entrypoints found in hash table.
3279
3280 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003281void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3282 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003283 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3284 if (inst->disp_hash[i].func_name != NULL)
3285 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3286 inst->disp_hash[i].func_name);
3287 }
3288}
3289
3290static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003291 const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003292 struct loader_icd *icd;
3293 icd = inst->icds;
3294 while (icd) {
3295 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
3296 // this icd supports funcName
3297 return true;
3298 icd = icd->next;
3299 }
3300
3301 return false;
3302}
3303
Jon Ashburncc407a22016-04-15 09:25:03 -06003304static bool loader_check_layer_list_for_address(
3305 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003306 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003307 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003308 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003309 const struct loader_device_extension_list *const extensions =
3310 &(layers->list[layer].device_extension_list);
3311 for (uint32_t extension = 0; extension < extensions->count;
3312 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003313 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003314 const struct loader_dev_ext_props *const property =
3315 &(extensions->list[extension]);
3316 for (uint32_t entry = 0; entry < property->entrypoint_count;
3317 ++entry) {
3318 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003319 return true;
3320 }
3321 }
3322 }
3323 }
3324
3325 return false;
3326}
3327
Jon Ashburncc407a22016-04-15 09:25:03 -06003328static bool
3329loader_check_layers_for_address(const struct loader_instance *const inst,
3330 const char *funcName) {
3331 if (loader_check_layer_list_for_address(&inst->instance_layer_list,
3332 funcName)) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003333 return true;
3334 }
3335
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003336 return false;
3337}
3338
Jon Ashburn23d36b12016-02-02 17:47:28 -07003339static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003340 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003341 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3342 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003343 }
3344 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3345}
3346
3347static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003348 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003349 uint32_t i;
3350 uint32_t idx = *ptr_idx;
3351 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3352
3353 if (!inst->disp_hash[idx].func_name) {
3354 // no entry here at this idx, so use it
3355 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003356 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003357 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003358 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003359 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003360 "loader_add_dev_ext_table() can't allocate memory for "
3361 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003362 return false;
3363 }
3364 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3365 return true;
3366 }
3367
3368 // check for enough capacity
3369 if (list->capacity == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06003370 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003371 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3372 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003373 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003374 "loader_add_dev_ext_table() can't allocate list memory");
3375 return false;
3376 }
3377 list->capacity = 8 * sizeof(*(list->index));
3378 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0ad83132016-06-30 13:02:42 -06003379 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003380 list->capacity * 2,
3381 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003382 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003383 loader_log(
3384 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3385 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003386 return false;
3387 }
3388 list->capacity *= 2;
3389 }
3390
Jon Ashburn23d36b12016-02-02 17:47:28 -07003391 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003392 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3393 do {
3394 if (!inst->disp_hash[i].func_name) {
3395 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003396 inst->disp_hash[i].func_name =
Mark Young0ad83132016-06-30 13:02:42 -06003397 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003398 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003399 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003400 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003401 "loader_add_dev_ext_table() can't rallocate "
3402 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003403 return false;
3404 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003405 strncpy(inst->disp_hash[i].func_name, funcName,
3406 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003407 list->index[list->count] = i;
3408 list->count++;
3409 *ptr_idx = i;
3410 return true;
3411 }
3412 i = (i + 1) % MAX_NUM_DEV_EXTS;
3413 } while (i != idx);
3414
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003415 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003416 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3417 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003418 return false;
3419}
3420
3421static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003422 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003423 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003424 if (inst->disp_hash[*idx].func_name &&
3425 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003426 return true;
3427
3428 // funcName wasn't at the primary spot in the hash table
3429 // search the list of secondary locations (shallow search, not deep search)
3430 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3431 alt_idx = inst->disp_hash[*idx].list.index[i];
3432 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3433 *idx = alt_idx;
3434 return true;
3435 }
3436 }
3437
3438 return false;
3439}
3440
3441/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003442 * This function returns generic trampoline code address for unknown entry
3443 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003444 * Presumably, these unknown entry points (as given by funcName) are device
3445 * extension entrypoints. A hash table is used to keep a list of unknown entry
3446 * points and their mapping to the device extension dispatch table
3447 * (struct loader_dev_ext_dispatch_table).
3448 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003449 * For a given entry point string (funcName), if an existing mapping is found
3450 * the
3451 * trampoline address for that mapping is returned. Otherwise, this unknown
3452 * entry point
3453 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3454 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003455 * new entry in the hash table is initialized and that trampoline address for
3456 * the new entry is returned. Null is returned if the hash table is full or
3457 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3458 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003459void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003460 uint32_t idx;
3461 uint32_t seed = 0;
3462
3463 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3464
3465 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3466 // found funcName already in hash
3467 return loader_get_dev_ext_trampoline(idx);
3468
3469 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003470 if (!loader_check_icds_for_address(inst, funcName) &&
3471 !loader_check_layers_for_address(inst, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003472 // if support found in layers continue on
3473 return NULL;
3474 }
3475
3476 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3477 // successfully added new table entry
3478 // init any dev dispatch table entrys as needed
3479 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3480 return loader_get_dev_ext_trampoline(idx);
3481 }
3482
3483 return NULL;
3484}
3485
Jon Ashburn23d36b12016-02-02 17:47:28 -07003486struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003487 /* look up the loader_instance in our list by comparing dispatch tables, as
3488 * there is no guarantee the instance is still a loader_instance* after any
3489 * layers which wrap the instance object.
3490 */
3491 const VkLayerInstanceDispatchTable *disp;
3492 struct loader_instance *ptr_instance = NULL;
3493 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003494 for (struct loader_instance *inst = loader.instances; inst;
3495 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003496 if (inst->disp == disp) {
3497 ptr_instance = inst;
3498 break;
3499 }
3500 }
3501 return ptr_instance;
3502}
3503
Jon Ashburn23d36b12016-02-02 17:47:28 -07003504static loader_platform_dl_handle
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003505loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3506 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003507
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003508 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003509 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003510 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003511 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003512 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003513 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003514 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003515 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003516 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003517
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003518 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003519}
3520
Jon Ashburn23d36b12016-02-02 17:47:28 -07003521static void
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003522loader_close_layer_lib(const struct loader_instance *inst,
3523 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003524
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003525 if (prop->lib_handle) {
3526 loader_platform_close_library(prop->lib_handle);
3527 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3528 "Unloading layer library %s", prop->lib_name);
3529 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003530 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003531}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003532
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003533void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003534 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003535 struct loader_layer_list *list) {
3536 /* delete instance list of enabled layers and close any layer libraries */
3537 for (uint32_t i = 0; i < list->count; i++) {
3538 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003539
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003540 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003541 }
Mark Young0ad83132016-06-30 13:02:42 -06003542 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003543}
3544
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003545/**
3546 * Go through the search_list and find any layers which match type. If layer
3547 * type match is found in then add it to ext_list.
3548 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003549static void
3550loader_add_layer_implicit(const struct loader_instance *inst,
3551 const enum layer_type type,
3552 struct loader_layer_list *list,
3553 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003554 bool enable;
3555 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003556 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003557 for (i = 0; i < search_list->count; i++) {
3558 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003559 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003560 /* Found an implicit layer, see if it should be enabled */
3561 enable = false;
3562
Jon Ashburn23d36b12016-02-02 17:47:28 -07003563 // if no enable_environment variable is specified, this implicit
3564 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003565 // should always be enabled. Otherwise check if the variable is set
3566 if (prop->enable_env_var.name[0] == 0) {
3567 enable = true;
3568 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003569 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003570 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3571 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003572 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003573 }
3574
3575 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003576 // environment variables are set, the layer is disabled. Implicit
3577 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003578 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003579 env_value = loader_getenv(prop->disable_env_var.name, inst);
3580 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003581 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003582 }
3583 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003584
Mark Young0ad83132016-06-30 13:02:42 -06003585 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003586 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003587 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003588 }
3589 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003590}
3591
3592/**
3593 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003594 * is found in search_list then add it to layer_list. But only add it to
3595 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003596 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003597static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003598 const enum layer_type type,
3599 const char *env_name,
3600 struct loader_layer_list *layer_list,
3601 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003602 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003603 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003604
Mark Young0ad83132016-06-30 13:02:42 -06003605 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003606 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003607 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003608 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003609 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003610 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003611 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003612 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003613 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003614
Mark Young0ad83132016-06-30 13:02:42 -06003615 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003616
Jon Ashburn23d36b12016-02-02 17:47:28 -07003617 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003618 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003619 if (!strcmp(std_validation_str, name)) {
3620 /* add meta list of layers
3621 don't attempt to remove duplicate layers already added by app or
3622 env var
3623 */
3624 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3625 "Expanding meta layer %s found in environment variable",
3626 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003627 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3628 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003629 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3630 sizeof(std_validation_names[0]);
3631 i++) {
3632 loader_find_layer_name_add_list(inst, std_validation_names[i],
3633 type, search_list, layer_list);
3634 }
3635 } else {
3636 loader_find_layer_name_add_list(inst, name, type, search_list,
3637 layer_list);
3638 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003639 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003640 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003641
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003642 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003643}
3644
Jon Ashburn23d36b12016-02-02 17:47:28 -07003645VkResult
3646loader_enable_instance_layers(struct loader_instance *inst,
3647 const VkInstanceCreateInfo *pCreateInfo,
3648 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003649 VkResult err;
3650
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003651 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003652
Jon Ashburne39a4f82015-08-28 13:38:21 -06003653 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003654 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3655 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003656 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003657 }
3658
Jon Ashburn0c26e712015-07-02 16:10:32 -06003659 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003660 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3661 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003662
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003663 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003664 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3665 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003666 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003667
3668 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003669 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003670 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3671 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003672
3673 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003674}
3675
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003676/*
3677 * Given the list of layers to activate in the loader_instance
3678 * structure. This function will add a VkLayerInstanceCreateInfo
3679 * structure to the VkInstanceCreateInfo.pNext pointer.
3680 * Each activated layer will have it's own VkLayerInstanceLink
3681 * structure that tells the layer what Get*ProcAddr to call to
3682 * get function pointers to the next layer down.
3683 * Once the chain info has been created this function will
3684 * execute the CreateInstance call chain. Each layer will
3685 * then have an opportunity in it's CreateInstance function
3686 * to setup it's dispatch table when the lower layer returns
3687 * successfully.
3688 * Each layer can wrap or not-wrap the returned VkInstance object
3689 * as it sees fit.
3690 * The instance chain is terminated by a loader function
3691 * that will call CreateInstance on all available ICD's and
3692 * cache those VkInstance objects for future use.
3693 */
3694VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003695 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003696 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003697 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003698 uint32_t activated_layers = 0;
3699 VkLayerInstanceCreateInfo chain_info;
3700 VkLayerInstanceLink *layer_instance_link_info = NULL;
3701 VkInstanceCreateInfo loader_create_info;
3702 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003703
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003704 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3705 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003706
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003707 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003708
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003709 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003710
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003711 chain_info.u.pLayerInfo = NULL;
3712 chain_info.pNext = pCreateInfo->pNext;
3713 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3714 chain_info.function = VK_LAYER_LINK_INFO;
3715 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003716
Jon Ashburn23d36b12016-02-02 17:47:28 -07003717 layer_instance_link_info = loader_stack_alloc(
3718 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003719 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003720 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3721 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003722 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003723 }
3724
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003725 /* Create instance chain of enabled layers */
3726 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003727 struct loader_layer_properties *layer_prop =
3728 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003729 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003730
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003731 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003732 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003733 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003734 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3735 NULL) {
3736 if (layer_prop->functions.str_gipa == NULL ||
3737 strlen(layer_prop->functions.str_gipa) == 0) {
3738 fpGIPA = (PFN_vkGetInstanceProcAddr)
3739 loader_platform_get_proc_address(
3740 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003741 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3742 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003743 fpGIPA = (PFN_vkGetInstanceProcAddr)
3744 loader_platform_get_proc_address(
3745 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003746 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003747 loader_log(
3748 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3749 "Failed to find vkGetInstanceProcAddr in layer %s",
3750 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003751 continue;
3752 }
3753 }
3754
Jon Ashburn23d36b12016-02-02 17:47:28 -07003755 layer_instance_link_info[activated_layers].pNext =
3756 chain_info.u.pLayerInfo;
3757 layer_instance_link_info[activated_layers]
3758 .pfnNextGetInstanceProcAddr = nextGIPA;
3759 chain_info.u.pLayerInfo =
3760 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003761 nextGIPA = fpGIPA;
3762
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003763 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003764 "Insert instance layer %s (%s)",
3765 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003766
3767 activated_layers++;
3768 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003769 }
3770
Jon Ashburn23d36b12016-02-02 17:47:28 -07003771 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003772 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003773 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003774 VkLayerInstanceCreateInfo create_info_disp;
3775
Jon Ashburncc407a22016-04-15 09:25:03 -06003776 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003777 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003778
3779 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3780
3781 create_info_disp.pNext = loader_create_info.pNext;
3782 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003783 res =
3784 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003785 } else {
3786 // Couldn't find CreateInstance function!
3787 res = VK_ERROR_INITIALIZATION_FAILED;
3788 }
3789
3790 if (res != VK_SUCCESS) {
3791 // TODO: Need to clean up here
3792 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003793 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003794 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003795 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003796 }
3797
3798 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003799}
3800
Jon Ashburn23d36b12016-02-02 17:47:28 -07003801void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3802 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003803
Jon Ashburn23d36b12016-02-02 17:47:28 -07003804 loader_init_instance_extension_dispatch_table(
3805 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003806}
3807
Jon Ashburn1530c342016-02-26 13:14:27 -07003808VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003809loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3810 const VkDeviceCreateInfo *pCreateInfo,
3811 const VkAllocationCallbacks *pAllocator,
3812 const struct loader_instance *inst,
3813 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003814 uint32_t activated_layers = 0;
3815 VkLayerDeviceLink *layer_device_link_info;
3816 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003817 VkDeviceCreateInfo loader_create_info;
3818 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003819
Piers Daniell295fe402016-03-29 11:51:11 -06003820 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003821 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003822
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003823 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3824
Jon Ashburn23d36b12016-02-02 17:47:28 -07003825 layer_device_link_info = loader_stack_alloc(
3826 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003827 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003828 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3829 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003830 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003831 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003832
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003833 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003834 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3835 chain_info.function = VK_LAYER_LINK_INFO;
3836 chain_info.u.pLayerInfo = NULL;
3837 chain_info.pNext = pCreateInfo->pNext;
3838 loader_create_info.pNext = &chain_info;
3839
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003840 /* Create instance chain of enabled layers */
3841 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003842 struct loader_layer_properties *layer_prop =
3843 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003844 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003845
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003846 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003847 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003848 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003849 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3850 NULL) {
3851 if (layer_prop->functions.str_gipa == NULL ||
3852 strlen(layer_prop->functions.str_gipa) == 0) {
3853 fpGIPA = (PFN_vkGetInstanceProcAddr)
3854 loader_platform_get_proc_address(
3855 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003856 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3857 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003858 fpGIPA = (PFN_vkGetInstanceProcAddr)
3859 loader_platform_get_proc_address(
3860 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003861 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003862 loader_log(
3863 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3864 "Failed to find vkGetInstanceProcAddr in layer %s",
3865 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003866 continue;
3867 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003868 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003869 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003870 if (layer_prop->functions.str_gdpa == NULL ||
3871 strlen(layer_prop->functions.str_gdpa) == 0) {
3872 fpGDPA = (PFN_vkGetDeviceProcAddr)
3873 loader_platform_get_proc_address(lib_handle,
3874 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003875 layer_prop->functions.get_device_proc_addr = fpGDPA;
3876 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003877 fpGDPA = (PFN_vkGetDeviceProcAddr)
3878 loader_platform_get_proc_address(
3879 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003880 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003881 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003882 "Failed to find vkGetDeviceProcAddr in layer %s",
3883 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003884 continue;
3885 }
3886 }
3887
Jon Ashburn23d36b12016-02-02 17:47:28 -07003888 layer_device_link_info[activated_layers].pNext =
3889 chain_info.u.pLayerInfo;
3890 layer_device_link_info[activated_layers]
3891 .pfnNextGetInstanceProcAddr = nextGIPA;
3892 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3893 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003894 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3895 nextGIPA = fpGIPA;
3896 nextGDPA = fpGDPA;
3897
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003898 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003899 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003900 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003901
3902 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003903 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003904 }
3905
Jon Ashburncc407a22016-04-15 09:25:03 -06003906 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003907 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003908 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003909 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003910 VkLayerDeviceCreateInfo create_info_disp;
3911
Jon Ashburncc407a22016-04-15 09:25:03 -06003912 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003913 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3914
3915 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3916
3917 create_info_disp.pNext = loader_create_info.pNext;
3918 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003919 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003920 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003921 if (res != VK_SUCCESS) {
3922 return res;
3923 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003924 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003925 } else {
3926 // Couldn't find CreateDevice function!
3927 return VK_ERROR_INITIALIZATION_FAILED;
3928 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003929
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003930 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003931 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003932 dev->device);
3933
3934 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003935}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003936
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003937VkResult loader_validate_layers(const struct loader_instance *inst,
3938 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003939 const char *const *ppEnabledLayerNames,
3940 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003941 struct loader_layer_properties *prop;
3942
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003943 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003944 VkStringErrorFlags result =
3945 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003946 if (result != VK_STRING_ERROR_NONE) {
3947 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003948 "Loader: Device ppEnabledLayerNames contains string "
3949 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003950 return VK_ERROR_LAYER_NOT_PRESENT;
3951 }
3952
Jon Ashburn23d36b12016-02-02 17:47:28 -07003953 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003954 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003955 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003956 }
3957 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003958 return VK_SUCCESS;
3959}
3960
3961VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003962 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003963 const struct loader_extension_list *icd_exts,
3964 const struct loader_layer_list *instance_layer,
3965 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003966
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003967 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003968 struct loader_layer_properties *layer_prop;
3969
Jon Ashburnf19916e2016-01-11 13:12:43 -07003970 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003971 VkStringErrorFlags result = vk_string_validate(
3972 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003973 if (result != VK_STRING_ERROR_NONE) {
3974 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003975 "Loader: Instance ppEnabledExtensionNames contains "
3976 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003977 return VK_ERROR_EXTENSION_NOT_PRESENT;
3978 }
3979
Jon Ashburn23d36b12016-02-02 17:47:28 -07003980 extension_prop = get_extension_property(
3981 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003982
3983 if (extension_prop) {
3984 continue;
3985 }
3986
3987 extension_prop = NULL;
3988
3989 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07003990 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003991 layer_prop = loader_get_layer_property(
3992 pCreateInfo->ppEnabledLayerNames[i], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003993 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06003994 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003995 * should have already filtered this case out.
3996 */
3997 continue;
3998 }
3999
Jon Ashburn23d36b12016-02-02 17:47:28 -07004000 extension_prop =
4001 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
4002 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004003 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004004 /* Found the extension in one of the layers enabled by the app.
4005 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004006 break;
4007 }
4008 }
4009
4010 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004011 /* Didn't find extension name in any of the global layers, error out
4012 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004013 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004014 }
4015 }
4016 return VK_SUCCESS;
4017}
4018
4019VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06004020 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004021 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07004022 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004023 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004024 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004025 struct loader_layer_properties *layer_prop;
4026
Jon Ashburnf19916e2016-01-11 13:12:43 -07004027 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004028
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004029 VkStringErrorFlags result = vk_string_validate(
4030 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004031 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06004032 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4033 0, "Loader: Device ppEnabledExtensionNames contains "
4034 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004035 return VK_ERROR_EXTENSION_NOT_PRESENT;
4036 }
4037
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004038 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004039 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004040
4041 if (extension_prop) {
4042 continue;
4043 }
4044
Jon Ashburn471f44c2016-01-13 12:51:43 -07004045 /* Not in global list, search activated layer extension lists */
4046 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4047 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004048
Jon Ashburn23d36b12016-02-02 17:47:28 -07004049 extension_prop = get_dev_extension_property(
4050 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004051 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004052 /* Found the extension in one of the layers enabled by the app.
4053 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004054 break;
4055 }
4056 }
4057
4058 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004059 /* Didn't find extension name in any of the device layers, error out
4060 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004061 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004062 }
4063 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004064 return VK_SUCCESS;
4065}
4066
Jon Ashburn1530c342016-02-26 13:14:27 -07004067/**
4068 * Terminator functions for the Instance chain
4069 * All named terminator_<Vulakn API name>
4070 */
Mark Young0ad83132016-06-30 13:02:42 -06004071VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
4072 const VkInstanceCreateInfo *pCreateInfo,
4073 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Jon Ashburn46888392015-01-29 15:45:51 -07004074 struct loader_icd *icd;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004075 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004076 char **filtered_extension_names = NULL;
4077 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004078 VkResult res = VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004079
Jon Ashburncc407a22016-04-15 09:25:03 -06004080 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004081 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4082
Jon Ashburnf19916e2016-01-11 13:12:43 -07004083 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004084 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004085
4086 /*
4087 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004088 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004089 * No ICD will advertise support for layers. An ICD
4090 * library could support a layer, but it would be
4091 * independent of the actual ICD, just in the same library.
4092 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004093 filtered_extension_names =
4094 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004095 if (!filtered_extension_names) {
Mark Young3a587792016-08-19 15:25:08 -06004096 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4097 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004098 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004099 icd_create_info.ppEnabledExtensionNames =
4100 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004101
Jon Ashburn8810c5f2015-08-18 18:04:47 -06004102 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
4103 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06004104 if (NULL == icd) {
Mark Young3a587792016-08-19 15:25:08 -06004105 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4106 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004107 }
4108 icd_create_info.enabledExtensionCount = 0;
4109 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004110
Mark Young0ad83132016-06-30 13:02:42 -06004111 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4112 "Build ICD instance extension list");
4113 // traverse scanned icd list adding non-duplicate extensions to the
4114 // list
Mark Young3a587792016-08-19 15:25:08 -06004115 res = loader_init_generic_list(ptr_instance,
Mark Young0ad83132016-06-30 13:02:42 -06004116 (struct loader_generic_list *)&icd_exts,
4117 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004118 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4119 // If out of memory, bail immediately.
4120 goto out;
4121 } else if (VK_SUCCESS != res) {
4122 // Keep trying if there was some other error.
4123 continue;
4124 }
4125
4126 res = loader_add_instance_extensions(
Mark Young0ad83132016-06-30 13:02:42 -06004127 ptr_instance,
4128 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
4129 icd->this_icd_lib->lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004130 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4131 // If out of memory, bail immediately.
4132 goto out;
4133 } else if (VK_SUCCESS != res) {
4134 // Keep trying if there was some other error.
4135 continue;
4136 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004137
Mark Young0ad83132016-06-30 13:02:42 -06004138 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4139 prop = get_extension_property(
4140 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4141 if (prop) {
4142 filtered_extension_names[icd_create_info
4143 .enabledExtensionCount] =
4144 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4145 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004146 }
4147 }
Mark Young0ad83132016-06-30 13:02:42 -06004148
4149 loader_destroy_generic_list(ptr_instance,
4150 (struct loader_generic_list *)&icd_exts);
4151
4152 res = ptr_instance->icd_libs.list[i].CreateInstance(
4153 &icd_create_info, pAllocator, &(icd->instance));
Mark Young3a587792016-08-19 15:25:08 -06004154 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4155 // If out of memory, bail immediately.
4156 goto out;
4157 } else if (VK_SUCCESS != res) {
4158 // Keep trying if there was some other error.
4159 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4160 "ICD ignored: failed to CreateInstance in ICD %d", i);
4161 continue;
4162 }
Mark Young0ad83132016-06-30 13:02:42 -06004163
Mark Young3a587792016-08-19 15:25:08 -06004164 if (!loader_icd_init_entrys(
4165 icd, icd->instance,
4166 ptr_instance->icd_libs.list[i].GetInstanceProcAddr)) {
4167 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06004168 "ICD ignored: failed to CreateInstance and find "
4169 "entrypoints with ICD");
Mark Young3a587792016-08-19 15:25:08 -06004170 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004171 }
Jon Ashburn46888392015-01-29 15:45:51 -07004172 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004173
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004174 /*
4175 * If no ICDs were added to instance list and res is unchanged
4176 * from it's initial value, the loader was unable to find
4177 * a suitable ICD.
4178 */
Mark Young3a587792016-08-19 15:25:08 -06004179 if (VK_SUCCESS == res && ptr_instance->icds == NULL) {
4180 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4181 }
4182
4183out:
4184
4185 if (VK_SUCCESS != res) {
4186 while (NULL != ptr_instance->icds) {
4187 icd = ptr_instance->icds;
4188 ptr_instance->icds = icd->next;
4189 if (NULL != icd->instance) {
4190 icd->DestroyInstance(icd->instance, pAllocator);
4191 }
4192 loader_icd_destroy(ptr_instance, icd, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004193 }
Ian Elliotteb450762015-02-05 15:19:15 -07004194 }
Jon Ashburn46888392015-01-29 15:45:51 -07004195
Mark Young3a587792016-08-19 15:25:08 -06004196 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004197}
4198
Mark Young0ad83132016-06-30 13:02:42 -06004199VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4200 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004201 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004202 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06004203 struct loader_icd *next_icd;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004204
4205 // Remove this instance from the list of instances:
4206 struct loader_instance *prev = NULL;
4207 struct loader_instance *next = loader.instances;
4208 while (next != NULL) {
4209 if (next == ptr_instance) {
4210 // Remove this instance from the list:
4211 if (prev)
4212 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004213 else
4214 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004215 break;
4216 }
4217 prev = next;
4218 next = next->next;
4219 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004220
Jon Ashburn3da71f22015-05-14 12:43:38 -06004221 while (icds) {
4222 if (icds->instance) {
Chia-I Wuf7458c52015-10-26 21:10:41 +08004223 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004224 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06004225 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004226 icds->instance = VK_NULL_HANDLE;
Mark Young0ad83132016-06-30 13:02:42 -06004227 loader_icd_destroy(ptr_instance, icds, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004228
4229 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07004230 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004231
Jon Ashburn23d36b12016-02-02 17:47:28 -07004232 loader_delete_layer_properties(ptr_instance,
4233 &ptr_instance->instance_layer_list);
Jon Ashburne39a4f82015-08-28 13:38:21 -06004234 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004235 loader_destroy_generic_list(
4236 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004237 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004238 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004239 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004240}
4241
Mark Young0ad83132016-06-30 13:02:42 -06004242VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4243 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4244 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4245 VkResult res = VK_SUCCESS;
Jon Ashburn1530c342016-02-26 13:14:27 -07004246 struct loader_physical_device *phys_dev;
Jon Ashburn014438f2016-03-01 19:51:07 -07004247 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004248
Jon Ashburncc407a22016-04-15 09:25:03 -06004249 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn72690f22016-03-29 12:52:13 -06004250 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004251 struct loader_extension_list icd_exts;
4252
4253 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004254
Jon Ashburn1530c342016-02-26 13:14:27 -07004255 if (fpCreateDevice == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06004256 res = VK_ERROR_INITIALIZATION_FAILED;
4257 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004258 }
4259
Jon Ashburn1530c342016-02-26 13:14:27 -07004260 VkDeviceCreateInfo localCreateInfo;
4261 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004262
4263 /*
4264 * NOTE: Need to filter the extensions to only those
4265 * supported by the ICD.
4266 * No ICD will advertise support for layers. An ICD
4267 * library could support a layer, but it would be
4268 * independent of the actual ICD, just in the same library.
4269 */
4270 char **filtered_extension_names = NULL;
4271 filtered_extension_names =
4272 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4273 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004274 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004275 }
4276
Jon Ashburn1530c342016-02-26 13:14:27 -07004277 localCreateInfo.enabledLayerCount = 0;
4278 localCreateInfo.ppEnabledLayerNames = NULL;
4279
4280 localCreateInfo.enabledExtensionCount = 0;
4281 localCreateInfo.ppEnabledExtensionNames =
4282 (const char *const *)filtered_extension_names;
4283
Jon Ashburn014438f2016-03-01 19:51:07 -07004284 /* Get the physical device (ICD) extensions */
Mark Young3a587792016-08-19 15:25:08 -06004285 res = loader_init_generic_list(phys_dev->this_icd->this_instance,
Jon Ashburn014438f2016-03-01 19:51:07 -07004286 (struct loader_generic_list *)&icd_exts,
Mark Young3a587792016-08-19 15:25:08 -06004287 sizeof(VkExtensionProperties));
4288 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004289 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004290 }
4291
4292 res = loader_add_device_extensions(
Jon Ashburncc407a22016-04-15 09:25:03 -06004293 phys_dev->this_icd->this_instance,
4294 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn014438f2016-03-01 19:51:07 -07004295 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
4296 &icd_exts);
4297 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004298 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004299 }
4300
Jon Ashburn1530c342016-02-26 13:14:27 -07004301 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4302 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004303 VkExtensionProperties *prop =
4304 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004305 if (prop) {
4306 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4307 (char *)extension_name;
4308 localCreateInfo.enabledExtensionCount++;
4309 }
4310 }
4311
Jon Ashburn1530c342016-02-26 13:14:27 -07004312 // TODO: Why does fpCreateDevice behave differently than
4313 // this_icd->CreateDevice?
4314 // VkResult res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo,
4315 // pAllocator, &localDevice);
Jon Ashburn014438f2016-03-01 19:51:07 -07004316 res = phys_dev->this_icd->CreateDevice(phys_dev->phys_dev, &localCreateInfo,
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004317 pAllocator, &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004318
4319 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004320 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004321 }
4322
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004323 *pDevice = dev->device;
Jon Ashburncc407a22016-04-15 09:25:03 -06004324 loader_add_logical_device(phys_dev->this_icd->this_instance,
4325 phys_dev->this_icd, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004326
4327 /* Init dispatch pointer in new device object */
4328 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4329
Mark Young0ad83132016-06-30 13:02:42 -06004330out:
4331 if (NULL != icd_exts.list) {
4332 loader_destroy_generic_list(phys_dev->this_icd->this_instance,
4333 (struct loader_generic_list *)&icd_exts);
4334 }
4335
Jon Ashburn1530c342016-02-26 13:14:27 -07004336 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004337}
4338
Jon Ashburn23d36b12016-02-02 17:47:28 -07004339VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004340terminator_EnumeratePhysicalDevices(VkInstance instance,
4341 uint32_t *pPhysicalDeviceCount,
4342 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004343 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004344 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004345 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004346
Jon Ashburn014438f2016-03-01 19:51:07 -07004347 struct loader_icd *icd;
4348 struct loader_phys_dev_per_icd *phys_devs;
4349
4350 inst->total_gpu_count = 0;
4351 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4352 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4353 if (!phys_devs)
4354 return VK_ERROR_OUT_OF_HOST_MEMORY;
4355
4356 icd = inst->icds;
4357 for (i = 0; i < inst->total_icd_count; i++) {
4358 assert(icd);
4359 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
4360 NULL);
4361 if (res != VK_SUCCESS)
4362 return res;
4363 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004364 }
4365
Jon Ashburn014438f2016-03-01 19:51:07 -07004366 icd = inst->icds;
4367 for (i = 0; i < inst->total_icd_count; i++) {
4368 assert(icd);
4369 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4370 phys_devs[i].count * sizeof(VkPhysicalDevice));
4371 if (!phys_devs[i].phys_devs) {
4372 return VK_ERROR_OUT_OF_HOST_MEMORY;
4373 }
4374 res = icd->EnumeratePhysicalDevices(
4375 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
4376 if ((res == VK_SUCCESS)) {
4377 inst->total_gpu_count += phys_devs[i].count;
4378 } else {
4379 return res;
4380 }
4381 phys_devs[i].this_icd = icd;
4382 icd = icd->next;
4383 }
4384
4385 *pPhysicalDeviceCount = inst->total_gpu_count;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004386 if (!pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004387 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004388 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004389
Jon Ashburn014438f2016-03-01 19:51:07 -07004390 /* Initialize the output pPhysicalDevices with wrapped loader terminator
4391 * physicalDevice objects; save this list of wrapped objects in instance
4392 * struct for later cleanup and use by trampoline code */
4393 uint32_t j, idx = 0;
4394 uint32_t copy_count = 0;
4395
4396 copy_count = (inst->total_gpu_count < *pPhysicalDeviceCount)
4397 ? inst->total_gpu_count
Jon Ashburn23d36b12016-02-02 17:47:28 -07004398 : *pPhysicalDeviceCount;
Jon Ashburn014438f2016-03-01 19:51:07 -07004399
Jon Ashburn014438f2016-03-01 19:51:07 -07004400 if (inst->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004401 loader_instance_heap_free(inst, inst->phys_devs_term);
4402 inst->phys_devs_term = loader_instance_heap_alloc(
Jon Ashburn014438f2016-03-01 19:51:07 -07004403 inst, sizeof(struct loader_physical_device) * copy_count,
4404 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4405 if (!inst->phys_devs_term)
4406 return VK_ERROR_OUT_OF_HOST_MEMORY;
4407
4408 for (i = 0; idx < copy_count && i < inst->total_icd_count; i++) {
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004409
Jon Ashburn014438f2016-03-01 19:51:07 -07004410 for (j = 0; j < phys_devs[i].count && idx < copy_count; j++) {
4411 loader_set_dispatch((void *)&inst->phys_devs_term[idx], inst->disp);
4412 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
4413 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
4414 pPhysicalDevices[idx] =
4415 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4416 idx++;
4417 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004418 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004419 *pPhysicalDeviceCount = copy_count;
4420
Piers Daniell295fe402016-03-29 11:51:11 -06004421 // TODO: Is phys_devs being leaked?
4422
Jon Ashburn014438f2016-03-01 19:51:07 -07004423 if (copy_count < inst->total_gpu_count) {
4424 inst->total_gpu_count = copy_count;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004425 return VK_INCOMPLETE;
4426 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004427 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004428}
4429
Jon Ashburn1530c342016-02-26 13:14:27 -07004430VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4431 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004432 struct loader_physical_device *phys_dev =
4433 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004434 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004435
Tony Barbour59a47322015-06-24 16:06:58 -06004436 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004437 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004438}
4439
Jon Ashburn1530c342016-02-26 13:14:27 -07004440VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004441 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4442 VkQueueFamilyProperties *pProperties) {
4443 struct loader_physical_device *phys_dev =
4444 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004445 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004446
Cody Northropd0802882015-08-03 17:04:53 -06004447 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004448 icd->GetPhysicalDeviceQueueFamilyProperties(
4449 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004450}
4451
Jon Ashburn1530c342016-02-26 13:14:27 -07004452VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004453 VkPhysicalDevice physicalDevice,
4454 VkPhysicalDeviceMemoryProperties *pProperties) {
4455 struct loader_physical_device *phys_dev =
4456 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004457 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004458
4459 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004460 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004461}
4462
Jon Ashburn23d36b12016-02-02 17:47:28 -07004463VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004464terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4465 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004466 struct loader_physical_device *phys_dev =
4467 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004468 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004469
4470 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004471 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004472}
4473
Jon Ashburn23d36b12016-02-02 17:47:28 -07004474VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004475terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4476 VkFormat format,
4477 VkFormatProperties *pFormatInfo) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004478 struct loader_physical_device *phys_dev =
4479 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004480 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004481
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -06004482 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004483 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4484 pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004485}
4486
Jon Ashburn1530c342016-02-26 13:14:27 -07004487VKAPI_ATTR VkResult VKAPI_CALL
4488terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004489 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4490 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4491 VkImageFormatProperties *pImageFormatProperties) {
4492 struct loader_physical_device *phys_dev =
4493 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004494 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn754864f2015-07-23 18:49:07 -06004495
Chia-I Wu17241042015-10-31 00:31:16 +08004496 if (!icd->GetPhysicalDeviceImageFormatProperties)
4497 return VK_ERROR_INITIALIZATION_FAILED;
4498
Jon Ashburn23d36b12016-02-02 17:47:28 -07004499 return icd->GetPhysicalDeviceImageFormatProperties(
4500 phys_dev->phys_dev, format, type, tiling, usage, flags,
4501 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004502}
4503
Jon Ashburn1530c342016-02-26 13:14:27 -07004504VKAPI_ATTR void VKAPI_CALL
4505terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004506 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4507 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4508 VkImageTiling tiling, uint32_t *pNumProperties,
4509 VkSparseImageFormatProperties *pProperties) {
4510 struct loader_physical_device *phys_dev =
4511 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004512 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004513
4514 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004515 icd->GetPhysicalDeviceSparseImageFormatProperties(
4516 phys_dev->phys_dev, format, type, samples, usage, tiling,
4517 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004518}
4519
Jon Ashburn1530c342016-02-26 13:14:27 -07004520VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4521 VkPhysicalDevice physicalDevice, const char *pLayerName,
4522 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburna760a512015-12-14 08:52:14 -07004523 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004524
Mark Young3a587792016-08-19 15:25:08 -06004525 struct loader_layer_list implicit_layer_list = {0};
4526 struct loader_extension_list all_exts = {0};
4527 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07004528
Jon Ashburndc5d9202016-02-29 13:00:51 -07004529 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004530
Jon Ashburndc5d9202016-02-29 13:00:51 -07004531 /* Any layer or trampoline wrapping should be removed at this point in time
4532 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn014438f2016-03-01 19:51:07 -07004533 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004534
Jon Ashburndc5d9202016-02-29 13:00:51 -07004535 /* this case is during the call down the instance chain with pLayerName
4536 * == NULL*/
4537 struct loader_icd *icd = phys_dev->this_icd;
4538 uint32_t icd_ext_count = *pPropertyCount;
4539 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004540
Jon Ashburndc5d9202016-02-29 13:00:51 -07004541 /* get device extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004542 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4543 &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06004544 if (res != VK_SUCCESS) {
4545 goto out;
4546 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004547
Mark Young3a587792016-08-19 15:25:08 -06004548 if (!loader_init_layer_list(icd->this_instance, &implicit_layer_list)) {
4549 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4550 goto out;
4551 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004552
Jon Ashburndc5d9202016-02-29 13:00:51 -07004553 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004554 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4555 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004556 /* we need to determine which implicit layers are active,
4557 * and then add their extensions. This can't be cached as
4558 * it depends on results of environment variables (which can change).
4559 */
4560 if (pProperties != NULL) {
4561 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn014438f2016-03-01 19:51:07 -07004562 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4563 icd_ext_count, pProperties,
4564 &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004565 if (res != VK_SUCCESS) {
4566 goto out;
4567 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004568
Jon Ashburndc5d9202016-02-29 13:00:51 -07004569 /* we need to determine which implicit layers are active,
4570 * and then add their extensions. This can't be cached as
4571 * it depends on results of environment variables (which can
4572 * change).
4573 */
Mark Young3a587792016-08-19 15:25:08 -06004574 res = loader_add_to_ext_list(icd->this_instance, &all_exts,
4575 icd_exts.count, icd_exts.list);
4576 if (res != VK_SUCCESS) {
4577 goto out;
4578 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004579
Jon Ashburndc5d9202016-02-29 13:00:51 -07004580 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004581 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4582 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004583
Jon Ashburndc5d9202016-02-29 13:00:51 -07004584 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4585 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004586 j < implicit_layer_list.list[i].device_extension_list.count;
4587 j++) {
Mark Young3a587792016-08-19 15:25:08 -06004588 res = loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4589 &implicit_layer_list.list[i]
4590 .device_extension_list.list[j]
4591 .props);
4592 if (res != VK_SUCCESS) {
4593 goto out;
4594 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004595 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004596 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004597 uint32_t capacity = *pPropertyCount;
4598 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004599
Jon Ashburndc5d9202016-02-29 13:00:51 -07004600 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4601 props[i] = all_exts.list[i];
4602 }
4603 /* wasn't enough space for the extensions, we did partial copy now
4604 * return VK_INCOMPLETE */
4605 if (capacity < all_exts.count) {
4606 res = VK_INCOMPLETE;
4607 } else {
4608 *pPropertyCount = all_exts.count;
4609 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004610 } else {
4611 /* just return the count; need to add in the count of implicit layer
4612 * extensions
4613 * don't worry about duplicates being added in the count */
4614 *pPropertyCount = icd_ext_count;
4615
4616 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4617 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004618 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004619 }
4620 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004621 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004622
Mark Young3a587792016-08-19 15:25:08 -06004623out:
4624
4625 if (NULL != implicit_layer_list.list) {
4626 loader_destroy_generic_list(
4627 icd->this_instance,
4628 (struct loader_generic_list *)&implicit_layer_list);
4629 }
4630 if (NULL != all_exts.list) {
4631 loader_destroy_generic_list(icd->this_instance,
4632 (struct loader_generic_list *)&all_exts);
4633 }
4634 if (NULL != icd_exts.list) {
4635 loader_destroy_generic_list(icd->this_instance,
4636 (struct loader_generic_list *)&icd_exts);
4637 }
4638
Jon Ashburndc5d9202016-02-29 13:00:51 -07004639 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004640}
4641
Jon Ashburn23d36b12016-02-02 17:47:28 -07004642VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004643terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4644 uint32_t *pPropertyCount,
4645 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004646
Jon Ashburndc5d9202016-02-29 13:00:51 -07004647 // should never get here this call isn't dispatched down the chain
4648 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004649}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004650
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004651VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004652 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004653 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004654 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004655
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004656 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004657 if (utf8[i] == 0) {
4658 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004659 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004660 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004661 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004662 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004663 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004664 num_char_bytes = 2;
4665 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4666 num_char_bytes = 3;
4667 } else {
4668 result = VK_STRING_ERROR_BAD_DATA;
4669 }
4670
4671 // Validate the following num_char_bytes of data
4672 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4673 if (++i == max_length) {
4674 result |= VK_STRING_ERROR_LENGTH;
4675 break;
4676 }
4677 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4678 result |= VK_STRING_ERROR_BAD_DATA;
4679 }
4680 }
4681 }
4682 return result;
4683}