blob: 184809b2ccaed5bd26537601aed6a7657e7c257f [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;
Mark Young16573c72016-06-28 10:52:43 -0600415 struct loader_icd *icd = loader_get_icd_and_device(device, &dev, NULL);
Jon Ashburned8f2312016-03-31 10:52:22 -0600416
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));
Mark Young9a3ddd42016-10-21 16:25:47 -0600827 if (!ext_props) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700828 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young9a3ddd42016-10-21 16:25:47 -0600829 }
Piers Daniell295fe402016-03-29 11:51:11 -0600830 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
831 &count, ext_props);
Mark Young9a3ddd42016-10-21 16:25:47 -0600832 if (res != VK_SUCCESS) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700833 return res;
Mark Young9a3ddd42016-10-21 16:25:47 -0600834 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700835 for (i = 0; i < count; i++) {
836 char spec_version[64];
837
Jon Ashburn23d36b12016-02-02 17:47:28 -0700838 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
839 VK_MAJOR(ext_props[i].specVersion),
840 VK_MINOR(ext_props[i].specVersion),
841 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700842 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700843 "Device Extension: %s (%s) version %s",
844 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700845 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
846 if (res != VK_SUCCESS)
847 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600848 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700849 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700850 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
851 "Error getting physical device extension info count from "
852 "library %s",
853 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700854 return res;
855 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600856
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700857 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600858}
859
Mark Young3a587792016-08-19 15:25:08 -0600860VkResult loader_init_generic_list(const struct loader_instance *inst,
Jon Ashburn1530c342016-02-26 13:14:27 -0700861 struct loader_generic_list *list_info,
862 size_t element_size) {
Mark Young84ba0482016-09-02 11:45:00 -0600863 size_t capacity = 32 * element_size;
864 list_info->count = 0;
865 list_info->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600866 list_info->list = loader_instance_heap_alloc(
Mark Young84ba0482016-09-02 11:45:00 -0600867 inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700868 if (list_info->list == NULL) {
Mark Young3a587792016-08-19 15:25:08 -0600869 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600870 }
Mark Young84ba0482016-09-02 11:45:00 -0600871 memset(list_info->list, 0, capacity);
872 list_info->capacity = capacity;
Mark Young3a587792016-08-19 15:25:08 -0600873 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600874}
875
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700876void loader_destroy_generic_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700877 struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600878 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700879 list->count = 0;
880 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600881}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600882
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600883/*
884 * Append non-duplicate extension properties defined in props
885 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700886 * Return
887 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600888 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700889VkResult loader_add_to_ext_list(const struct loader_instance *inst,
890 struct loader_extension_list *ext_list,
891 uint32_t prop_list_count,
892 const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600893 uint32_t i;
894 const VkExtensionProperties *cur_ext;
895
896 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600897 VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700898 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -0600899 if (VK_SUCCESS != res) {
900 return res;
901 }
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600902 }
903
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600904 for (i = 0; i < prop_list_count; i++) {
905 cur_ext = &props[i];
906
907 // look for duplicates
908 if (has_vk_extension_property(cur_ext, ext_list)) {
909 continue;
910 }
911
912 // add to list at end
913 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700914 if (ext_list->count * sizeof(VkExtensionProperties) >=
915 ext_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -0600916
Mark Young0ad83132016-06-30 13:02:42 -0600917 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700918 inst, ext_list->list, ext_list->capacity,
919 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700920
921 if (ext_list->list == NULL)
922 return VK_ERROR_OUT_OF_HOST_MEMORY;
923
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600924 // double capacity
925 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600926 }
927
Jon Ashburn23d36b12016-02-02 17:47:28 -0700928 memcpy(&ext_list->list[ext_list->count], cur_ext,
929 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600930 ext_list->count++;
931 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700932 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600933}
934
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700935/*
936 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600937 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700938 * Return
939 * Vk_SUCCESS on success
940 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700941VkResult
942loader_add_to_dev_ext_list(const struct loader_instance *inst,
943 struct loader_device_extension_list *ext_list,
944 const VkExtensionProperties *props,
945 uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700946 uint32_t idx;
947 if (ext_list->list == NULL || ext_list->capacity == 0) {
Mark Young3a587792016-08-19 15:25:08 -0600948 VkResult res = loader_init_generic_list(
949 inst, (struct loader_generic_list *)ext_list,
950 sizeof(struct loader_dev_ext_props));
951 if (VK_SUCCESS != res) {
952 return res;
953 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700954 }
955
Jon Ashburnb8726962016-04-08 15:03:35 -0600956 // look for duplicates
957 if (has_vk_dev_ext_property(props, ext_list)) {
958 return VK_SUCCESS;
959 }
960
Jon Ashburn23d36b12016-02-02 17:47:28 -0700961 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700962 // add to list at end
963 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700964 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700965
Mark Young0ad83132016-06-30 13:02:42 -0600966 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700967 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
968 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700969
970 if (ext_list->list == NULL)
971 return VK_ERROR_OUT_OF_HOST_MEMORY;
972
973 // double capacity
974 ext_list->capacity *= 2;
975 }
976
Jon Ashburn23d36b12016-02-02 17:47:28 -0700977 memcpy(&ext_list->list[idx].props, props,
978 sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700979 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700980 ext_list->list[idx].entrypoints =
Mark Young0ad83132016-06-30 13:02:42 -0600981 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
982 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
983 if (ext_list->list[idx].entrypoints == NULL) {
984 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700985 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600986 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700987 for (uint32_t i = 0; i < entry_count; i++) {
Mark Young0ad83132016-06-30 13:02:42 -0600988 ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700989 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600990 if (ext_list->list[idx].entrypoints[i] == NULL) {
991 for (uint32_t j = 0; j < i; j++) {
992 loader_instance_heap_free(inst,
993 ext_list->list[idx].entrypoints[j]);
994 }
995 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
996 ext_list->list[idx].entrypoint_count = 0;
997 ext_list->list[idx].entrypoints = NULL;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700998 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600999 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001000 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
1001 }
1002 ext_list->count++;
1003
1004 return VK_SUCCESS;
1005}
1006
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001007/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001008 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001009 * Add these to the output layer_list. Don't add duplicates to the output
1010 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001011 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001012static VkResult
1013loader_add_layer_names_to_list(const struct loader_instance *inst,
1014 struct loader_layer_list *output_list,
1015 uint32_t name_count, const char *const *names,
1016 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001017 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001018 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001019
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001020 for (uint32_t i = 0; i < name_count; i++) {
1021 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001022 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001023 if (!layer_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001024 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1025 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001026 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001027 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001028 }
1029
Mark Young0ad83132016-06-30 13:02:42 -06001030 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001031 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001032
1033 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001034}
1035
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001036/*
1037 * Manage lists of VkLayerProperties
1038 */
Jon Ashburne39a4f82015-08-28 13:38:21 -06001039static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001040 struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001041 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Young0ad83132016-06-30 13:02:42 -06001042 list->list = loader_instance_heap_alloc(
1043 inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001044 if (list->list == NULL) {
1045 return false;
1046 }
1047 memset(list->list, 0, list->capacity);
1048 list->count = 0;
1049 return true;
1050}
1051
Jon Ashburne39a4f82015-08-28 13:38:21 -06001052void loader_destroy_layer_list(const struct loader_instance *inst,
Mark Young0ad83132016-06-30 13:02:42 -06001053 struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001054 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001055 if (device) {
1056 loader_device_heap_free(device, layer_list->list);
1057 } else {
1058 loader_instance_heap_free(inst, layer_list->list);
1059 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001060 layer_list->count = 0;
1061 layer_list->capacity = 0;
1062}
1063
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001064/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001065 * Search the given layer list for a list
1066 * matching the given VkLayerProperties
1067 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001068bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
1069 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001070 for (uint32_t i = 0; i < list->count; i++) {
1071 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
1072 return true;
1073 }
1074 return false;
1075}
1076
1077/*
1078 * Search the given layer list for a layer
1079 * matching the given name
1080 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001081bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001082 for (uint32_t i = 0; i < list->count; i++) {
1083 if (strcmp(name, list->list[i].info.layerName) == 0)
1084 return true;
1085 }
1086 return false;
1087}
1088
1089/*
1090 * Append non-duplicate layer properties defined in prop_list
1091 * to the given layer_info list
1092 */
Mark Young0ad83132016-06-30 13:02:42 -06001093VkResult loader_add_to_layer_list(const struct loader_instance *inst,
1094 struct loader_layer_list *list,
1095 uint32_t prop_list_count,
1096 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001097 uint32_t i;
1098 struct loader_layer_properties *layer;
1099
1100 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001101 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001102 }
1103
1104 if (list->list == NULL)
Mark Young0ad83132016-06-30 13:02:42 -06001105 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001106
1107 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001108 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001109
1110 // look for duplicates
1111 if (has_vk_layer_property(&layer->info, list)) {
1112 continue;
1113 }
1114
1115 // add to list at end
1116 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001117 if (list->count * sizeof(struct loader_layer_properties) >=
1118 list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001119
Mark Young0ad83132016-06-30 13:02:42 -06001120 list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001121 inst, list->list, list->capacity, list->capacity * 2,
1122 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001123 if (NULL == list->list) {
1124 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1125 "realloc failed for layer list when attempting to "
1126 "add new layer");
1127 return VK_ERROR_OUT_OF_HOST_MEMORY;
1128 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001129 // double capacity
1130 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001131 }
1132
Jon Ashburn23d36b12016-02-02 17:47:28 -07001133 memcpy(&list->list[list->count], layer,
1134 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001135 list->count++;
1136 }
Mark Young0ad83132016-06-30 13:02:42 -06001137
1138 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001139}
1140
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001141/**
1142 * Search the search_list for any layer with a name
1143 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001144 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001145 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001146 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001147 */
Jon Ashburncc407a22016-04-15 09:25:03 -06001148void loader_find_layer_name_add_list(
1149 const struct loader_instance *inst, const char *name,
1150 const enum layer_type type, const struct loader_layer_list *search_list,
1151 struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001152 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001153 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001154 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001155 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001156 (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001157 /* Found a layer with the same name, add to found_list */
Mark Young0ad83132016-06-30 13:02:42 -06001158 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
1159 found = true;
1160 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001161 }
1162 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001163 if (!found) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001164 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001165 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001166 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001167}
1168
Jon Ashburn23d36b12016-02-02 17:47:28 -07001169static VkExtensionProperties *
1170get_extension_property(const char *name,
1171 const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001172 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001173 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001174 return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001175 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001176 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001177}
1178
Jon Ashburn23d36b12016-02-02 17:47:28 -07001179static VkExtensionProperties *
1180get_dev_extension_property(const char *name,
1181 const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001182 for (uint32_t i = 0; i < list->count; i++) {
1183 if (strcmp(name, list->list[i].props.extensionName) == 0)
1184 return &list->list[i].props;
1185 }
1186 return NULL;
1187}
1188
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001189/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001190 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001191 * the extension must provide two entry points for the loader to use:
1192 * - "trampoline" entry point - this is the address returned by GetProcAddr
1193 * and will always do what's necessary to support a global call.
1194 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001195 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001196 * the extension for the appropriate ICDs that are available.
1197 * There is no generic mechanism for including these functions, the references
1198 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001199 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1200 * GetProcAddr requests
1201 * loader_coalesce_extensions(void) - add extension records to the list of
1202 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001203 * extension available to the app.
1204 * instance_disp - add function pointer for terminator function to this array.
1205 * The extension itself should be in a separate file that will be
1206 * linked directly with the loader.
1207 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001208
Mark Young3a587792016-08-19 15:25:08 -06001209VkResult loader_get_icd_loader_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001210 const struct loader_instance *inst, struct loader_icd_libs *icd_libs,
1211 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001212 struct loader_extension_list icd_exts;
Mark Young3a587792016-08-19 15:25:08 -06001213 VkResult res = VK_SUCCESS;
1214
Jon Ashburn23d36b12016-02-02 17:47:28 -07001215 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1216 "Build ICD instance extension list");
Mark Young3a587792016-08-19 15:25:08 -06001217
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001218 // traverse scanned icd list adding non-duplicate extensions to the list
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001219 for (uint32_t i = 0; i < icd_libs->count; i++) {
Mark Young3a587792016-08-19 15:25:08 -06001220 res = loader_init_generic_list(inst,
1221 (struct loader_generic_list *)&icd_exts,
1222 sizeof(VkExtensionProperties));
1223 if (VK_SUCCESS != res) {
1224 goto out;
1225 }
1226 res = loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001227 inst, icd_libs->list[i].EnumerateInstanceExtensionProperties,
1228 icd_libs->list[i].lib_name, &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001229 if (VK_SUCCESS == res) {
1230 res = loader_add_to_ext_list(inst, inst_exts, icd_exts.count,
1231 icd_exts.list);
1232 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001233 loader_destroy_generic_list(inst,
1234 (struct loader_generic_list *)&icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06001235 if (VK_SUCCESS != res) {
1236 goto out;
1237 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001238 };
1239
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001240 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001241 debug_report_add_instance_extensions(inst, inst_exts);
Mark Young3a587792016-08-19 15:25:08 -06001242
1243out:
1244 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001245}
1246
Jon Ashburne39a4f82015-08-28 13:38:21 -06001247struct loader_icd *loader_get_icd_and_device(const VkDevice device,
Mark Young16573c72016-06-28 10:52:43 -06001248 struct loader_device **found_dev,
1249 uint32_t *icd_index) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001250 *found_dev = NULL;
Mark Young16573c72016-06-28 10:52:43 -06001251 uint32_t index = 0;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001252 for (struct loader_instance *inst = loader.instances; inst;
1253 inst = inst->next) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001254 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001255 for (struct loader_device *dev = icd->logical_device_list; dev;
1256 dev = dev->next)
1257 /* Value comparison of device prevents object wrapping by layers
1258 */
1259 if (loader_get_dispatch(dev->device) ==
1260 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001261 *found_dev = dev;
Mark Young16573c72016-06-28 10:52:43 -06001262 if (NULL != icd_index) {
1263 *icd_index = index;
1264 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001265 return icd;
1266 }
Mark Young16573c72016-06-28 10:52:43 -06001267 index++;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001268 }
1269 }
1270 return NULL;
1271}
1272
Mark Young0ad83132016-06-30 13:02:42 -06001273void loader_destroy_logical_device(const struct loader_instance *inst,
1274 struct loader_device *dev,
1275 const VkAllocationCallbacks *pAllocator) {
1276 if (pAllocator) {
1277 dev->alloc_callbacks = *pAllocator;
1278 }
Mark Young0ad83132016-06-30 13:02:42 -06001279 if (NULL != dev->activated_layer_list.list) {
1280 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1281 }
1282 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001283}
1284
Jon Ashburn1530c342016-02-26 13:14:27 -07001285struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001286loader_create_logical_device(const struct loader_instance *inst,
1287 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001288 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001289#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1290 {
1291#else
1292 if (pAllocator) {
1293 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1294 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1295 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1296 } else {
1297#endif
1298 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1299 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001300
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001301 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001302 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1303 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001304 return NULL;
1305 }
1306
1307 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001308 if (pAllocator) {
1309 new_dev->alloc_callbacks = *pAllocator;
1310 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001311
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001312 return new_dev;
1313}
1314
Piers Daniell295fe402016-03-29 11:51:11 -06001315void loader_add_logical_device(const struct loader_instance *inst,
1316 struct loader_icd *icd,
1317 struct loader_device *dev) {
1318 dev->next = icd->logical_device_list;
1319 icd->logical_device_list = dev;
1320}
1321
Jon Ashburn23d36b12016-02-02 17:47:28 -07001322void loader_remove_logical_device(const struct loader_instance *inst,
1323 struct loader_icd *icd,
Mark Young0ad83132016-06-30 13:02:42 -06001324 struct loader_device *found_dev,
1325 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001326 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001327
1328 if (!icd || !found_dev)
1329 return;
1330
1331 prev_dev = NULL;
1332 dev = icd->logical_device_list;
1333 while (dev && dev != found_dev) {
1334 prev_dev = dev;
1335 dev = dev->next;
1336 }
1337
1338 if (prev_dev)
1339 prev_dev->next = found_dev->next;
1340 else
1341 icd->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001342 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001343}
1344
Jon Ashburn23d36b12016-02-02 17:47:28 -07001345static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0ad83132016-06-30 13:02:42 -06001346 struct loader_icd *icd,
1347 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001348 ptr_inst->total_icd_count--;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001349 for (struct loader_device *dev = icd->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001350 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001351 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001352 dev = next_dev;
1353 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001354
Mark Young0ad83132016-06-30 13:02:42 -06001355 loader_instance_heap_free(ptr_inst, icd);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001356}
1357
Jon Ashburn23d36b12016-02-02 17:47:28 -07001358static struct loader_icd *
1359loader_icd_create(const struct loader_instance *inst) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001360 struct loader_icd *icd;
1361
Mark Youngdb13a2a2016-09-06 13:53:03 -06001362 icd = loader_instance_heap_alloc(inst, sizeof(struct loader_icd),
Mark Young0ad83132016-06-30 13:02:42 -06001363 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Youngdb13a2a2016-09-06 13:53:03 -06001364 if (!icd) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001365 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001366 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001367
Mark Youngdb13a2a2016-09-06 13:53:03 -06001368 memset(icd, 0, sizeof(struct loader_icd));
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001369
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001370 return icd;
1371}
1372
Jon Ashburn23d36b12016-02-02 17:47:28 -07001373static struct loader_icd *
1374loader_icd_add(struct loader_instance *ptr_inst,
1375 const struct loader_scanned_icds *icd_lib) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001376 struct loader_icd *icd;
1377
Jon Ashburne39a4f82015-08-28 13:38:21 -06001378 icd = loader_icd_create(ptr_inst);
Mark Youngdb13a2a2016-09-06 13:53:03 -06001379 if (!icd) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001380 return NULL;
Mark Youngdb13a2a2016-09-06 13:53:03 -06001381 }
Chia-I Wu13a61a52014-08-04 11:18:20 +08001382
Jon Ashburn3d002332015-08-20 16:35:30 -06001383 icd->this_icd_lib = icd_lib;
1384 icd->this_instance = ptr_inst;
1385
Chia-I Wu13a61a52014-08-04 11:18:20 +08001386 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -07001387 icd->next = ptr_inst->icds;
1388 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001389 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001390
1391 return icd;
1392}
Jon Ashburn17b4c862016-04-25 11:09:37 -06001393/**
1394 * Determine the ICD interface version to use.
1395 * @param icd
1396 * @param pVersion Output parameter indicating which version to use or 0 if
1397 * the negotiation API is not supported by the ICD
1398 * @return bool indicating true if the selected interface version is supported
1399 * by the loader, false indicates the version is not supported
1400 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1401 * vk_icdNegotiateLoaderICDInterfaceVersion
1402 * version 1 supports vk_icdGetInstanceProcAddr
1403 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1404 */
1405bool loader_get_icd_interface_version(
1406 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1407 uint32_t *pVersion) {
1408
1409 if (fp_negotiate_icd_version == NULL) {
1410 // ICD does not support the negotiation API, it supports version 0 or 1
1411 // calling code must determine if it is version 0 or 1
1412 *pVersion = 0;
1413 } else {
1414 // ICD supports the negotiation API, so call it with the loader's
1415 // latest version supported
1416 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1417 VkResult result = fp_negotiate_icd_version(pVersion);
1418
1419 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1420 // ICD no longer supports the loader's latest interface version so
1421 // fail loading the ICD
1422 return false;
1423 }
1424 }
1425
1426#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1427 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1428 // Loader no longer supports the ICD's latest interface version so fail
1429 // loading the ICD
1430 return false;
1431 }
1432#endif
1433 return true;
1434}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001435
Jon Ashburn23d36b12016-02-02 17:47:28 -07001436void loader_scanned_icd_clear(const struct loader_instance *inst,
1437 struct loader_icd_libs *icd_libs) {
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001438 if (icd_libs->capacity == 0)
1439 return;
1440 for (uint32_t i = 0; i < icd_libs->count; i++) {
1441 loader_platform_close_library(icd_libs->list[i].handle);
Mark Young0ad83132016-06-30 13:02:42 -06001442 loader_instance_heap_free(inst, icd_libs->list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001443 }
Mark Young0ad83132016-06-30 13:02:42 -06001444 loader_instance_heap_free(inst, icd_libs->list);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001445 icd_libs->capacity = 0;
1446 icd_libs->count = 0;
1447 icd_libs->list = NULL;
1448}
1449
Mark Young0ad83132016-06-30 13:02:42 -06001450static VkResult loader_scanned_icd_init(const struct loader_instance *inst,
1451 struct loader_icd_libs *icd_libs) {
1452 VkResult err = VK_SUCCESS;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001453 loader_scanned_icd_clear(inst, icd_libs);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001454 icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
Mark Young0ad83132016-06-30 13:02:42 -06001455 icd_libs->list = loader_instance_heap_alloc(
1456 inst, icd_libs->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1457 if (NULL == icd_libs->list) {
1458 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1459 "realloc failed for layer list when attempting to add new layer");
1460 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1461 }
1462 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001463}
1464
Mark Young3a587792016-08-19 15:25:08 -06001465static VkResult loader_scanned_icd_add(const struct loader_instance *inst,
1466 struct loader_icd_libs *icd_libs,
1467 const char *filename,
1468 uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001469 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001470 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001471 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001472 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001473 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001474 struct loader_scanned_icds *new_node;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001475 uint32_t interface_vers;
Mark Young3a587792016-08-19 15:25:08 -06001476 VkResult res = VK_SUCCESS;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001477
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001478 /* TODO implement smarter opening/closing of libraries. For now this
1479 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001480 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001481 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001482 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001483 loader_platform_open_library_error(filename));
Mark Young3a587792016-08-19 15:25:08 -06001484 goto out;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001485 }
1486
Jon Ashburn17b4c862016-04-25 11:09:37 -06001487 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001488 fp_negotiate_icd_version = loader_platform_get_proc_address(
1489 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001490
1491 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001492 &interface_vers)) {
1493 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1494 "ICD (%s) doesn't support interface version compatible"
1495 "with loader, skip this ICD %s",
1496 filename);
Mark Young3a587792016-08-19 15:25:08 -06001497 goto out;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001498 }
1499
Jon Ashburn23d36b12016-02-02 17:47:28 -07001500 fp_get_proc_addr =
1501 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001502 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001503 assert(interface_vers == 0);
1504 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001505 fp_get_proc_addr =
1506 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001507 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001508 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1509 loader_platform_get_proc_address_error(
1510 "vk_icdGetInstanceProcAddr"));
Mark Young3a587792016-08-19 15:25:08 -06001511 goto out;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001512 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001513 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001514 "Using deprecated ICD interface of "
1515 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001516 "vk_icdGetInstanceProcAddr for ICD %s",
1517 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001518 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001519 fp_create_inst =
1520 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001521 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001522 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1523 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1524 "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 fp_get_inst_ext_props = loader_platform_get_proc_address(
1529 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001530 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001531 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1532 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001533 "via dlsym/loadlibrary for ICD %s",
1534 filename);
Mark Young3a587792016-08-19 15:25:08 -06001535 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001536 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001537 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001538 // Use newer interface version 1 or later
1539 if (interface_vers == 0)
1540 interface_vers = 1;
1541
Jon Ashburn23d36b12016-02-02 17:47:28 -07001542 fp_create_inst =
1543 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001544 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001545 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1546 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001547 "vk_icdGetInstanceProcAddr for ICD %s",
1548 filename);
Mark Young3a587792016-08-19 15:25:08 -06001549 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001550 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001551 fp_get_inst_ext_props =
1552 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1553 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001554 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001555 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1556 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001557 "via vk_icdGetInstanceProcAddr for ICD %s",
1558 filename);
Mark Young3a587792016-08-19 15:25:08 -06001559 goto out;
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001560 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001561 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001562
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001563 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001564 if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
1565 icd_libs->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001566
Mark Young0ad83132016-06-30 13:02:42 -06001567 icd_libs->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001568 inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
1569 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001570 if (NULL == icd_libs->list) {
Mark Young3a587792016-08-19 15:25:08 -06001571 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -06001572 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1573 "realloc failed on icd library list");
Mark Young3a587792016-08-19 15:25:08 -06001574 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06001575 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001576 // double capacity
1577 icd_libs->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001578 }
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001579 new_node = &(icd_libs->list[icd_libs->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001580
1581 new_node->handle = handle;
Jon Ashburn005617f2015-11-17 17:35:40 -07001582 new_node->api_version = api_version;
Jon Ashburnc624c882015-07-16 10:17:29 -06001583 new_node->GetInstanceProcAddr = fp_get_proc_addr;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001584 new_node->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Jon Ashburn46888392015-01-29 15:45:51 -07001585 new_node->CreateInstance = fp_create_inst;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001586 new_node->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001587
Mark Young0ad83132016-06-30 13:02:42 -06001588 new_node->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001589 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001590 if (NULL == new_node->lib_name) {
Mark Young3a587792016-08-19 15:25:08 -06001591 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001592 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001593 "Out of memory can't add icd");
Mark Young3a587792016-08-19 15:25:08 -06001594 goto out;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001595 }
1596 strcpy(new_node->lib_name, filename);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001597 icd_libs->count++;
Mark Young3a587792016-08-19 15:25:08 -06001598
1599out:
1600
1601 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001602}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001603
Jon Ashburn23d36b12016-02-02 17:47:28 -07001604static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
1605 const PFN_vkGetInstanceProcAddr fp_gipa) {
1606/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001607
Jon Ashburn23d36b12016-02-02 17:47:28 -07001608#define LOOKUP_GIPA(func, required) \
1609 do { \
1610 icd->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1611 if (!icd->func && required) { \
1612 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001613 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001614 loader_platform_get_proc_address_error("vk" #func)); \
1615 return false; \
1616 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001617 } while (0)
1618
Jon Ashburnc624c882015-07-16 10:17:29 -06001619 LOOKUP_GIPA(GetDeviceProcAddr, true);
1620 LOOKUP_GIPA(DestroyInstance, true);
1621 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1622 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1623 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001624 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001625 LOOKUP_GIPA(CreateDevice, true);
1626 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1627 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001628 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001629 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001630 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001631 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1632 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001633 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001634 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1635 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1636 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001637 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1638 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1639 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1640 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1641 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1642 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1643 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1644 LOOKUP_GIPA(DestroySurfaceKHR, false);
Mark Young16573c72016-06-28 10:52:43 -06001645 LOOKUP_GIPA(CreateSwapchainKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001646#ifdef VK_USE_PLATFORM_WIN32_KHR
Mark Young16573c72016-06-28 10:52:43 -06001647 LOOKUP_GIPA(CreateWin32SurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001648 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1649#endif
1650#ifdef VK_USE_PLATFORM_XCB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001651 LOOKUP_GIPA(CreateXcbSurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001652 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1653#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001654#ifdef VK_USE_PLATFORM_XLIB_KHR
Mark Young16573c72016-06-28 10:52:43 -06001655 LOOKUP_GIPA(CreateXlibSurfaceKHR, false);
Karl Schultz65d20182016-03-08 07:55:27 -07001656 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1657#endif
Mark Younga7c51fd2016-09-16 10:18:42 -06001658#ifdef VK_USE_PLATFORM_MIR_KHR
1659 LOOKUP_GIPA(CreateMirSurfaceKHR, false);
1660 LOOKUP_GIPA(GetPhysicalDeviceMirPresentationSupportKHR, false);
1661#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001662#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Mark Young16573c72016-06-28 10:52:43 -06001663 LOOKUP_GIPA(CreateWaylandSurfaceKHR, false);
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001664 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1665#endif
James Jones389dc0c2016-08-18 23:41:19 +01001666 LOOKUP_GIPA(GetPhysicalDeviceExternalImageFormatPropertiesNV, false);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001667
Jon Ashburnc624c882015-07-16 10:17:29 -06001668#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001669
Jon Ashburnc624c882015-07-16 10:17:29 -06001670 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001671}
1672
Jon Ashburn23d36b12016-02-02 17:47:28 -07001673static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001674 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001675
1676 if (g_loader_debug > 0)
1677 return;
1678
1679 g_loader_debug = 0;
1680
1681 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001682 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001683 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001684 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001685 size_t len;
1686
1687 if (p)
1688 len = p - env;
1689 else
1690 len = strlen(env);
1691
1692 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001693 if (strncmp(env, "all", len) == 0) {
1694 g_loader_debug = ~0u;
1695 g_loader_log_msgs = ~0u;
1696 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001697 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001698 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001699 } else if (strncmp(env, "info", len) == 0) {
1700 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001701 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001702 } else if (strncmp(env, "perf", len) == 0) {
1703 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001704 g_loader_log_msgs |=
1705 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001706 } else if (strncmp(env, "error", len) == 0) {
1707 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001708 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001709 } else if (strncmp(env, "debug", len) == 0) {
1710 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001711 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001712 }
1713 }
1714
1715 if (!p)
1716 break;
1717
1718 env = p + 1;
1719 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001720
Mark Young0ad83132016-06-30 13:02:42 -06001721 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001722}
1723
Jon Ashburn23d36b12016-02-02 17:47:28 -07001724void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001725 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001726 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001727 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001728
1729 // initialize logging
1730 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001731
1732 // initial cJSON to use alloc callbacks
1733 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001734 .malloc_fn = loader_instance_tls_heap_alloc,
1735 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001736 };
1737 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001738}
1739
Jon Ashburn2077e382015-06-29 11:25:34 -06001740struct loader_manifest_files {
1741 uint32_t count;
1742 char **filename_list;
1743};
1744
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001745/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001746 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001747 *
1748 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001749 * A pointer to first char in the next path.
1750 * The next path (or NULL) in the list is returned in next_path.
1751 * Note: input string is modified in some cases. PASS IN A COPY!
1752 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001753static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001754 uint32_t len;
1755 char *next;
1756
1757 if (path == NULL)
1758 return NULL;
1759 next = strchr(path, PATH_SEPERATOR);
1760 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001761 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001762 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001763 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001764 *next = '\0';
1765 next++;
1766 }
1767
1768 return next;
1769}
1770
1771/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001772 * Given a path which is absolute or relative, expand the path if relative or
1773 * leave the path unmodified if absolute. The base path to prepend to relative
1774 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001775 *
1776 * \returns
1777 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001778 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001779static void loader_expand_path(const char *path, const char *rel_base,
1780 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001781 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001782 // do not prepend a base to an absolute path
1783 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001784 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001785
1786 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001787}
1788
1789/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001790 * Given a filename (file) and a list of paths (dir), try to find an existing
1791 * file in the paths. If filename already is a path then no
1792 * searching in the given paths.
1793 *
1794 * \returns
1795 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001796 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001797static void loader_get_fullpath(const char *file, const char *dirs,
1798 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001799 if (!loader_platform_is_path(file) && *dirs) {
1800 char *dirs_copy, *dir, *next_dir;
1801
1802 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1803 strcpy(dirs_copy, dirs);
1804
Jon Ashburn23d36b12016-02-02 17:47:28 -07001805 // find if file exists after prepending paths in given list
1806 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001807 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001808 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1809 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001810 if (loader_platform_file_exists(out_fullpath)) {
1811 return;
1812 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001813 }
1814 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001815
Jon Ashburn2077e382015-06-29 11:25:34 -06001816 snprintf(out_fullpath, out_size, "%s", file);
1817}
1818
1819/**
1820 * Read a JSON file into a buffer.
1821 *
1822 * \returns
1823 * A pointer to a cJSON object representing the JSON parse tree.
1824 * This returned buffer should be freed by caller.
1825 */
Mark Young3a587792016-08-19 15:25:08 -06001826static VkResult loader_get_json(const struct loader_instance *inst,
1827 const char *filename, cJSON **json) {
1828 FILE *file = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06001829 char *json_buf;
Mark Young93ecb1d2016-01-13 13:47:16 -07001830 size_t len;
Mark Young3a587792016-08-19 15:25:08 -06001831 VkResult res = VK_SUCCESS;
1832
1833 if (NULL == json) {
1834 res = VK_ERROR_INITIALIZATION_FAILED;
1835 goto out;
1836 }
1837
1838 *json = NULL;
1839
Jon Ashburn23d36b12016-02-02 17:47:28 -07001840 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001841 if (!file) {
Mark Young3a587792016-08-19 15:25:08 -06001842 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001843 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1844 "Couldn't open JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001845 goto out;
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001846 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001847 fseek(file, 0, SEEK_END);
1848 len = ftell(file);
1849 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001850 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001851 if (json_buf == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06001852 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001853 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1854 "Out of memory can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001855 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001856 }
1857 if (fread(json_buf, sizeof(char), len, file) != len) {
Mark Young3a587792016-08-19 15:25:08 -06001858 res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001859 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1860 "fread failed can't get JSON file");
Mark Young3a587792016-08-19 15:25:08 -06001861 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06001862 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001863 json_buf[len] = '\0';
1864
Jon Ashburn23d36b12016-02-02 17:47:28 -07001865 // parse text from file
Mark Young3a587792016-08-19 15:25:08 -06001866 *json = cJSON_Parse(json_buf);
1867 if (*json == NULL) {
1868 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001869 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1870 "Can't parse JSON file %s", filename);
Mark Young3a587792016-08-19 15:25:08 -06001871 goto out;
1872 }
1873
1874out:
1875 if (NULL != file) {
1876 fclose(file);
1877 }
1878
1879 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06001880}
1881
1882/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001883 * Do a deep copy of the loader_layer_properties structure.
1884 */
Mark Young0ad83132016-06-30 13:02:42 -06001885VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1886 struct loader_layer_properties *dst,
1887 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001888 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001889 memcpy(dst, src, sizeof(*src));
1890 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001891 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1892 src->instance_extension_list.count,
1893 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1894 if (NULL == dst->instance_extension_list.list) {
1895 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1896 "alloc failed for instance extension list");
1897 return VK_ERROR_OUT_OF_HOST_MEMORY;
1898 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001899 dst->instance_extension_list.capacity =
1900 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001901 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001902 dst->instance_extension_list.capacity);
1903 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001904 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1905 src->device_extension_list.count,
1906 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1907 if (NULL == dst->device_extension_list.list) {
1908 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1909 "alloc failed for device extension list");
1910 return VK_ERROR_OUT_OF_HOST_MEMORY;
1911 }
1912 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) *
1913 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001914
Jon Ashburn23d36b12016-02-02 17:47:28 -07001915 dst->device_extension_list.capacity =
1916 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001917 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001918 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001919 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001920 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001921 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001922 dst->device_extension_list.list->entrypoints =
1923 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1924 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1925 if (NULL == dst->device_extension_list.list->entrypoints) {
1926 loader_log(
1927 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1928 "alloc failed for device extension list entrypoint array");
1929 return VK_ERROR_OUT_OF_HOST_MEMORY;
1930 }
1931 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
1932
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001933 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001934 dst->device_extension_list.list->entrypoints[i] =
1935 loader_instance_heap_alloc(
1936 inst,
1937 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1938 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1939 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1940 loader_log(
1941 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1942 "alloc failed for device extension list entrypoint %d", i);
1943 return VK_ERROR_OUT_OF_HOST_MEMORY;
1944 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001945 strcpy(dst->device_extension_list.list->entrypoints[i],
1946 src->device_extension_list.list->entrypoints[i]);
1947 }
1948 }
Mark Young0ad83132016-06-30 13:02:42 -06001949
1950 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001951}
1952
Jon Ashburn86a527a2016-02-10 20:59:26 -07001953static bool
1954loader_find_layer_name_list(const char *name,
1955 const struct loader_layer_list *layer_list) {
1956 if (!layer_list)
1957 return false;
1958 for (uint32_t j = 0; j < layer_list->count; j++)
1959 if (!strcmp(name, layer_list->list[j].info.layerName))
1960 return true;
1961 return false;
1962}
1963
1964static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1965 const char **layer_list) {
1966 if (!layer_list)
1967 return false;
1968 for (uint32_t j = 0; j < layer_count; j++)
1969 if (!strcmp(name, layer_list[j]))
1970 return true;
1971 return false;
1972}
1973
Jon Ashburn491cd042016-05-16 14:01:18 -06001974bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001975 const char *name, uint32_t layer_count,
1976 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1977 if (!layer_list)
1978 return false;
1979 for (uint32_t j = 0; j < layer_count; j++)
1980 if (!strcmp(name, layer_list[j]))
1981 return true;
1982 return false;
1983}
1984
1985/**
1986 * Searches through an array of layer names (ppp_layer_names) looking for a
1987 * layer key_name.
1988 * If not found then simply returns updating nothing.
1989 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12001990 * Any duplicate (pre-existing) expand_names in layer names are removed.
1991 * Order is otherwise preserved, with the layer key_name being replaced by the
1992 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07001993 * @param inst
1994 * @param layer_count
1995 * @param ppp_layer_names
1996 */
Mark Young0ad83132016-06-30 13:02:42 -06001997VkResult loader_expand_layer_names(
1998 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07001999 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06002000 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07002001
Jon Ashburncc407a22016-04-15 09:25:03 -06002002 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002003
Jon Ashburncc407a22016-04-15 09:25:03 -06002004 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06002005 (char const **)pp_src_layers)) {
2006 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06002007 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06002008 }
Jon Ashburn71483442016-02-11 18:59:43 -07002009
Jon Ashburn86a527a2016-02-10 20:59:26 -07002010 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2011 "Found meta layer %s, replacing with actual layer group",
2012 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12002013
Jon Ashburn491cd042016-05-16 14:01:18 -06002014 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06002015 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06002016 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
2017 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06002018 if (NULL == pp_dst_layers) {
2019 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2020 "alloc failed for dst layer array");
2021 return VK_ERROR_OUT_OF_HOST_MEMORY;
2022 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002023
2024 // copy layers from src to dst, stripping key_name and anything in
2025 // expand_names.
2026 uint32_t src_index, dst_index = 0;
2027 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06002028 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
2029 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12002030 continue;
2031 }
2032
2033 if (!strcmp(pp_src_layers[src_index], key_name)) {
2034 // insert all expand_names in place of key_name
2035 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06002036 for (expand_index = 0; expand_index < expand_count;
2037 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12002038 pp_dst_layers[dst_index++] = expand_names[expand_index];
2039 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002040 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002041 }
Chris Forbesbd9de052016-04-06 20:49:02 +12002042
2043 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07002044 }
2045
Chris Forbesbd9de052016-04-06 20:49:02 +12002046 *ppp_layer_names = pp_dst_layers;
2047 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06002048
2049 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002050}
2051
Chris Forbesbd9de052016-04-06 20:49:02 +12002052void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
2053 const VkInstanceCreateInfo *orig,
2054 VkInstanceCreateInfo *ours) {
2055 /* Free the layer names array iff we had to reallocate it */
2056 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06002057 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07002058 }
2059}
2060
Jon Ashburn491cd042016-05-16 14:01:18 -06002061void loader_init_std_validation_props(struct loader_layer_properties *props) {
2062 memset(props, 0, sizeof(struct loader_layer_properties));
2063 props->type = VK_LAYER_TYPE_META_EXPLICT;
2064 strncpy(props->info.description, "LunarG Standard Validation Layer",
2065 sizeof (props->info.description));
2066 props->info.implementationVersion = 1;
2067 strncpy(props->info.layerName, std_validation_str,
2068 sizeof (props->info.layerName));
2069 // TODO what about specVersion? for now insert loader's built version
2070 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2071}
2072
Jon Ashburn86a527a2016-02-10 20:59:26 -07002073/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002074 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002075 * the set of required layer names. If found then it adds a meta property to the
2076 * layer list.
2077 * Assumes the required layers are the same for both instance and device lists.
2078 * @param inst
2079 * @param layer_count number of layers in layer_names
2080 * @param layer_names array of required layer names
2081 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002082 */
2083static void loader_add_layer_property_meta(
2084 const struct loader_instance *inst, uint32_t layer_count,
2085 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002086 struct loader_layer_list *layer_instance_list) {
2087 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002088 bool found;
2089 struct loader_layer_list *layer_list;
2090
Jon Ashburn491cd042016-05-16 14:01:18 -06002091 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002092 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002093 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002094 return;
2095
Jon Ashburn491cd042016-05-16 14:01:18 -06002096
2097 layer_list = layer_instance_list;
2098
2099 found = true;
2100 if (layer_list == NULL)
2101 return;
2102 for (i = 0; i < layer_count; i++) {
2103 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002104 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002105 found = false;
2106 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002107 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002108
2109 struct loader_layer_properties *props;
2110 if (found) {
2111 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002112 if (NULL == props) {
2113 // Error already triggered in loader_get_next_layer_property.
2114 return;
2115 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002116 loader_init_std_validation_props(props);
2117
2118 }
2119
Jon Ashburn86a527a2016-02-10 20:59:26 -07002120}
2121
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002122static void loader_read_json_layer(
2123 const struct loader_instance *inst,
2124 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2125 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2126 char *temp;
2127 char *name, *type, *library_path, *api_version;
2128 char *implementation_version, *description;
2129 cJSON *ext_item;
2130 VkExtensionProperties ext_prop;
2131
Mark Young0ad83132016-06-30 13:02:42 -06002132/*
2133 * The following are required in the "layer" object:
2134 * (required) "name"
2135 * (required) "type"
2136 * (required) “library_path”
2137 * (required) “api_version”
2138 * (required) “implementation_version”
2139 * (required) “description”
2140 * (required for implicit layers) “disable_environment”
2141 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002142
Jon Ashburn23d36b12016-02-02 17:47:28 -07002143#define GET_JSON_OBJECT(node, var) \
2144 { \
2145 var = cJSON_GetObjectItem(node, #var); \
2146 if (var == NULL) { \
2147 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002148 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002149 "Didn't find required layer object %s in manifest " \
2150 "JSON file, skipping this layer", \
2151 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002152 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002153 } \
2154 }
2155#define GET_JSON_ITEM(node, var) \
2156 { \
2157 item = cJSON_GetObjectItem(node, #var); \
2158 if (item == NULL) { \
2159 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002160 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002161 "Didn't find required layer value %s in manifest JSON " \
2162 "file, skipping this layer", \
2163 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002164 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002165 } \
2166 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002167 if (temp == NULL) { \
2168 layer_node = layer_node->next; \
2169 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2170 "Problem accessing layer value %s in manifest JSON " \
2171 "file, skipping this layer", \
2172 #var); \
2173 return; \
2174 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002175 temp[strlen(temp) - 1] = '\0'; \
2176 var = loader_stack_alloc(strlen(temp) + 1); \
2177 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002178 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002179 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002180 GET_JSON_ITEM(layer_node, name)
2181 GET_JSON_ITEM(layer_node, type)
2182 GET_JSON_ITEM(layer_node, library_path)
2183 GET_JSON_ITEM(layer_node, api_version)
2184 GET_JSON_ITEM(layer_node, implementation_version)
2185 GET_JSON_ITEM(layer_node, description)
2186 if (is_implicit) {
2187 GET_JSON_OBJECT(layer_node, disable_environment)
2188 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002189#undef GET_JSON_ITEM
2190#undef GET_JSON_OBJECT
2191
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002192 // add list entry
2193 struct loader_layer_properties *props = NULL;
2194 if (!strcmp(type, "DEVICE")) {
2195 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2196 "Device layers are deprecated skipping this layer");
2197 layer_node = layer_node->next;
2198 return;
2199 }
2200 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2201 // layers that must work with older loaders
2202 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2203 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002204 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002205 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002206 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002207 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002208 if (NULL == props) {
2209 // Error already triggered in loader_get_next_layer_property.
2210 return;
2211 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002212 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2213 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2214 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002215
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002216 if (props == NULL) {
2217 layer_node = layer_node->next;
2218 return;
2219 }
Jon Ashburn15315172015-07-07 15:06:25 -06002220
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002221 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2222 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2223
2224 char *fullpath = props->lib_name;
2225 char *rel_base;
2226 if (loader_platform_is_path(library_path)) {
2227 // a relative or absolute path
2228 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2229 strcpy(name_copy, filename);
2230 rel_base = loader_platform_dirname(name_copy);
2231 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2232 } else {
2233 // a filename which is assumed in a system directory
2234 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2235 MAX_STRING_SIZE, fullpath);
2236 }
2237 props->info.specVersion = loader_make_version(api_version);
2238 props->info.implementationVersion = atoi(implementation_version);
2239 strncpy((char *)props->info.description, description,
2240 sizeof(props->info.description));
2241 props->info.description[sizeof(props->info.description) - 1] = '\0';
2242 if (is_implicit) {
2243 if (!disable_environment || !disable_environment->child) {
2244 loader_log(
2245 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2246 "Didn't find required layer child value disable_environment"
2247 "in manifest JSON file, skipping this layer");
2248 layer_node = layer_node->next;
2249 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002250 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002251 strncpy(props->disable_env_var.name, disable_environment->child->string,
2252 sizeof(props->disable_env_var.name));
2253 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2254 '\0';
2255 strncpy(props->disable_env_var.value,
2256 disable_environment->child->valuestring,
2257 sizeof(props->disable_env_var.value));
2258 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2259 '\0';
2260 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002261
Jon Ashburn23d36b12016-02-02 17:47:28 -07002262/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002263* Now get all optional items and objects and put in list:
2264* functions
2265* instance_extensions
2266* device_extensions
2267* enable_environment (implicit layers only)
2268*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002269#define GET_JSON_OBJECT(node, var) \
2270 { var = cJSON_GetObjectItem(node, #var); }
2271#define GET_JSON_ITEM(node, var) \
2272 { \
2273 item = cJSON_GetObjectItem(node, #var); \
2274 if (item != NULL) { \
2275 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002276 if (temp != NULL) { \
2277 temp[strlen(temp) - 1] = '\0'; \
2278 var = loader_stack_alloc(strlen(temp) + 1); \
2279 strcpy(var, &temp[1]); \
2280 cJSON_Free(temp); \
2281 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002282 } \
2283 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002284
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002285 cJSON *instance_extensions, *device_extensions, *functions,
2286 *enable_environment;
2287 cJSON *entrypoints;
2288 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2289 char **entry_array;
2290 vkGetInstanceProcAddr = NULL;
2291 vkGetDeviceProcAddr = NULL;
2292 spec_version = NULL;
2293 entrypoints = NULL;
2294 entry_array = NULL;
2295 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002296
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002297 /**
2298 * functions
2299 * vkGetInstanceProcAddr
2300 * vkGetDeviceProcAddr
2301 */
2302 GET_JSON_OBJECT(layer_node, functions)
2303 if (functions != NULL) {
2304 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2305 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2306 if (vkGetInstanceProcAddr != NULL)
2307 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2308 sizeof(props->functions.str_gipa));
2309 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2310 if (vkGetDeviceProcAddr != NULL)
2311 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2312 sizeof(props->functions.str_gdpa));
2313 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2314 }
2315 /**
2316 * instance_extensions
2317 * array of
2318 * name
2319 * spec_version
2320 */
2321 GET_JSON_OBJECT(layer_node, instance_extensions)
2322 if (instance_extensions != NULL) {
2323 int count = cJSON_GetArraySize(instance_extensions);
2324 for (i = 0; i < count; i++) {
2325 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2326 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002327 if (name != NULL) {
2328 strncpy(ext_prop.extensionName, name,
2329 sizeof(ext_prop.extensionName));
2330 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2331 '\0';
2332 }
Mark Young0ad83132016-06-30 13:02:42 -06002333 GET_JSON_ITEM(ext_item, spec_version)
2334 if (NULL != spec_version) {
2335 ext_prop.specVersion = atoi(spec_version);
2336 } else {
2337 ext_prop.specVersion = 0;
2338 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002339 bool ext_unsupported =
2340 wsi_unsupported_instance_extension(&ext_prop);
2341 if (!ext_unsupported) {
2342 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2343 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002344 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002345 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002346 }
2347 /**
2348 * device_extensions
2349 * array of
2350 * name
2351 * spec_version
2352 * entrypoints
2353 */
2354 GET_JSON_OBJECT(layer_node, device_extensions)
2355 if (device_extensions != NULL) {
2356 int count = cJSON_GetArraySize(device_extensions);
2357 for (i = 0; i < count; i++) {
2358 ext_item = cJSON_GetArrayItem(device_extensions, i);
2359 GET_JSON_ITEM(ext_item, name)
2360 GET_JSON_ITEM(ext_item, spec_version)
2361 if (name != NULL) {
2362 strncpy(ext_prop.extensionName, name,
2363 sizeof(ext_prop.extensionName));
2364 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2365 '\0';
2366 }
Mark Young0ad83132016-06-30 13:02:42 -06002367 if (NULL != spec_version) {
2368 ext_prop.specVersion = atoi(spec_version);
2369 } else {
2370 ext_prop.specVersion = 0;
2371 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002372 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2373 GET_JSON_OBJECT(ext_item, entrypoints)
2374 int entry_count;
2375 if (entrypoints == NULL) {
2376 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2377 &ext_prop, 0, NULL);
2378 continue;
2379 }
2380 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002381 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002382 entry_array =
2383 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002384 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002385 for (j = 0; j < entry_count; j++) {
2386 ext_item = cJSON_GetArrayItem(entrypoints, j);
2387 if (ext_item != NULL) {
2388 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002389 if (NULL == temp) {
2390 entry_array[j] = NULL;
2391 continue;
2392 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002393 temp[strlen(temp) - 1] = '\0';
2394 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2395 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002396 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002397 }
2398 }
2399 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2400 &ext_prop, entry_count, entry_array);
2401 }
2402 }
2403 if (is_implicit) {
2404 GET_JSON_OBJECT(layer_node, enable_environment)
2405
2406 // enable_environment is optional
2407 if (enable_environment) {
2408 strncpy(props->enable_env_var.name,
2409 enable_environment->child->string,
2410 sizeof(props->enable_env_var.name));
2411 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2412 '\0';
2413 strncpy(props->enable_env_var.value,
2414 enable_environment->child->valuestring,
2415 sizeof(props->enable_env_var.value));
2416 props->enable_env_var
2417 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2418 }
2419 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002420#undef GET_JSON_ITEM
2421#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002422}
2423
2424/**
2425 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2426 * file, add entry to the layer_list. Fill out the layer_properties in this list
2427 * entry from the input cJSON object.
2428 *
2429 * \returns
2430 * void
2431 * layer_list has a new entry and initialized accordingly.
2432 * If the json input object does not have all the required fields no entry
2433 * is added to the list.
2434 */
2435static void
2436loader_add_layer_properties(const struct loader_instance *inst,
2437 struct loader_layer_list *layer_instance_list,
2438 cJSON *json, bool is_implicit, char *filename) {
2439 /* Fields in layer manifest file that are required:
2440 * (required) “file_format_version”
2441 *
Mark Young0ad83132016-06-30 13:02:42 -06002442 * If more than one "layer" object are to be used, use the "layers" array
2443 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002444 *
2445 * First get all required items and if any missing abort
2446 */
2447
2448 cJSON *item, *layers_node, *layer_node;
2449 uint16_t file_major_vers = 0;
2450 uint16_t file_minor_vers = 0;
2451 uint16_t file_patch_vers = 0;
2452 char *vers_tok;
2453 cJSON *disable_environment = NULL;
2454 item = cJSON_GetObjectItem(json, "file_format_version");
2455 if (item == NULL) {
2456 return;
2457 }
2458 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002459 if (NULL == file_vers) {
2460 return;
2461 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002462 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2463 "Found manifest file %s, version %s", filename, file_vers);
2464 // Get the major/minor/and patch as integers for easier comparison
2465 vers_tok = strtok(file_vers, ".\"\n\r");
2466 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002467 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002468 vers_tok = strtok(NULL, ".\"\n\r");
2469 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002470 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002471 vers_tok = strtok(NULL, ".\"\n\r");
2472 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002473 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002474 }
2475 }
2476 }
2477 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2478 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2479 "%s Unexpected manifest file version (expected 1.0.0 or "
2480 "1.0.1), may cause errors",
2481 filename);
2482 }
Mark Young0ad83132016-06-30 13:02:42 -06002483 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002484 // If "layers" is present, read in the array of layer objects
2485 layers_node = cJSON_GetObjectItem(json, "layers");
2486 if (layers_node != NULL) {
2487 int numItems = cJSON_GetArraySize(layers_node);
2488 if (file_major_vers == 1 && file_minor_vers == 0 &&
2489 file_patch_vers == 0) {
2490 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2491 "\"layers\" tag not officially added until file version "
2492 "1.0.1, but %s is reporting version %s",
2493 filename, file_vers);
2494 }
2495 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2496 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2497 if (layer_node == NULL) {
2498 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2499 "Can't find \"layers\" array element %d object in "
2500 "manifest JSON file %s, skipping this file",
2501 curLayer, filename);
2502 return;
2503 }
2504 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2505 disable_environment, is_implicit, filename);
2506 }
2507 } else {
2508 // Otherwise, try to read in individual layers
2509 layer_node = cJSON_GetObjectItem(json, "layer");
2510 if (layer_node == NULL) {
2511 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2512 "Can't find \"layer\" object in manifest JSON file %s, "
2513 "skipping this file",
2514 filename);
2515 return;
2516 }
2517 // Loop through all "layer" objects in the file to get a count of them
2518 // first.
2519 uint16_t layer_count = 0;
2520 cJSON *tempNode = layer_node;
2521 do {
2522 tempNode = tempNode->next;
2523 layer_count++;
2524 } while (tempNode != NULL);
2525 /*
2526 * Throw a warning if we encounter multiple "layer" objects in file
2527 * versions newer than 1.0.0. Having multiple objects with the same
2528 * name at the same level is actually a JSON standard violation.
2529 */
2530 if (layer_count > 1 &&
2531 (file_major_vers > 1 ||
2532 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2533 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2534 "Multiple \"layer\" nodes are deprecated starting in "
2535 "file version \"1.0.1\". Please use \"layers\" : [] "
2536 "array instead in %s.",
2537 filename);
2538 } else {
2539 do {
2540 loader_read_json_layer(inst, layer_instance_list, layer_node,
2541 item, disable_environment, is_implicit,
2542 filename);
2543 layer_node = layer_node->next;
2544 } while (layer_node != NULL);
2545 }
2546 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002547 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002548}
2549
2550/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002551 * Find the Vulkan library manifest files.
2552 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002553 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002554 * for a list of JSON manifest files. If env_override is non-NULL
2555 * and has a valid value. Then the location is ignored. Otherwise
2556 * location is used to look for manifest files. The location
2557 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002558 * on Linux. "home_location" is an additional directory in the users home
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002559 * directory to look at. It is expanded into the dir path
2560 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2561 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002562 *
2563 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002564 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002565 * A string list of manifest files to be opened in out_files param.
2566 * List has a pointer to string for each manifest filename.
2567 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002568 * Location or override string lists can be either files or directories as
2569 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002570 * | location | override
2571 * --------------------------------
2572 * Win ICD | files | files
2573 * Win Layer | files | dirs
2574 * Linux ICD | dirs | files
2575 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002576 */
Mark Young0ad83132016-06-30 13:02:42 -06002577static VkResult loader_get_manifest_files(
2578 const struct loader_instance *inst, const char *env_override,
2579 char *source_override, bool is_layer, const char *location,
2580 const char *home_location, struct loader_manifest_files *out_files) {
2581 char * override = NULL;
2582 char *loc, *orig_loc = NULL;
2583 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002584 char *file, *next_file, *name;
2585 size_t alloced_count = 64;
2586 char full_path[2048];
2587 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002588 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002589 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002590 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002591
2592 out_files->count = 0;
2593 out_files->filename_list = NULL;
2594
Jamie Madill00c3c912016-04-06 18:26:46 -04002595 if (source_override != NULL) {
2596 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002597 } else if (env_override != NULL &&
2598 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002599#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002600 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002601 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002602 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002603 override = NULL;
2604 }
2605#endif
2606 }
2607
Jon Ashburnb6822212016-02-16 15:34:16 -07002608#if !defined(_WIN32)
2609 if (location == NULL && home_location == NULL) {
2610#else
2611 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002612 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002613#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002614 loader_log(
2615 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002616 "Can't get manifest files with NULL location, env_override=%s",
2617 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002618 res = VK_ERROR_INITIALIZATION_FAILED;
2619 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002620 }
2621
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002622#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002623 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002624#else
2625 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002626#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002627 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002628 // Also handle getting the location(s) from registry on Windows
2629 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002630 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002631 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002632 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2633 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002634 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2635 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002636 }
2637 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002638#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002639 reg = loader_get_registry_files(inst, loc);
2640 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002641 if (!is_layer) {
2642 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2643 "Registry lookup failed can't get ICD manifest "
2644 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002645 // This typically only fails when out of memory, which is
2646 // critical
2647 // if this is for the loader.
2648 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002649 } else {
2650 // warning only for layers
2651 loader_log(
2652 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2653 "Registry lookup failed can't get layer manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002654 // Return success for now since it's not critical for layers
2655 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002656 }
Mark Young0ad83132016-06-30 13:02:42 -06002657 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002658 }
Mark Young0ad83132016-06-30 13:02:42 -06002659 orig_loc = loc;
2660 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002661#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002662 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002663 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002664 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002665 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2666 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002667 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2668 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002669 }
2670 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002671 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002672 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002673 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002674 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002675
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002676 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002677 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2678 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002679
Jon Ashburn2077e382015-06-29 11:25:34 -06002680 file = loc;
2681 while (*file) {
2682 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002683 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002684 sysdir = opendir(file);
2685 name = NULL;
2686 if (sysdir) {
2687 dent = readdir(sysdir);
2688 if (dent == NULL)
2689 break;
2690 name = &(dent->d_name[0]);
2691 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2692 name = full_path;
2693 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002694 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002695#if defined(_WIN32)
2696 name = file;
2697#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002698 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002699 char *dir;
2700 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002701 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002702 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002703 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2704 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002705 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002706 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002707 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002708
2709 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2710
2711 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002712#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002713 }
2714 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002715 /* Look for files ending with ".json" suffix */
2716 uint32_t nlen = (uint32_t)strlen(name);
2717 const char *suf = name + nlen - 5;
2718 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2719 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002720 out_files->filename_list = loader_instance_heap_alloc(
2721 inst, alloced_count * sizeof(char *),
2722 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002723 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002724 out_files->filename_list = loader_instance_heap_realloc(
2725 inst, out_files->filename_list,
2726 alloced_count * sizeof(char *),
2727 alloced_count * sizeof(char *) * 2,
2728 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002729 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002730 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002731 if (out_files->filename_list == NULL) {
2732 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2733 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002734 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2735 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002736 }
Mark Young0ad83132016-06-30 13:02:42 -06002737 out_files->filename_list[out_files->count] =
2738 loader_instance_heap_alloc(
2739 inst, strlen(name) + 1,
2740 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002741 if (out_files->filename_list[out_files->count] == NULL) {
2742 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2743 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002744 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2745 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002746 }
2747 strcpy(out_files->filename_list[out_files->count], name);
2748 out_files->count++;
2749 } else if (!list_is_dirs) {
2750 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002751 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002752 "Skipping manifest file %s, file name must end in .json",
2753 name);
2754 }
2755 if (list_is_dirs) {
2756 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002757 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002758 break;
Mark Young0ad83132016-06-30 13:02:42 -06002759 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002760 name = &(dent->d_name[0]);
2761 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2762 name = full_path;
2763 } else {
2764 break;
2765 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002766 }
Mark Young0ad83132016-06-30 13:02:42 -06002767 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002768 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002769 sysdir = NULL;
2770 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002771 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002772#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002773 if (home_location != NULL &&
2774 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002775 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2776 size_t len;
2777 if (xdgdatahome != NULL) {
2778
2779 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002780 strlen(home_location));
2781 if (home_loc == NULL) {
2782 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002783 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002784 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2785 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002786 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002787 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002788 // Add directory separator if needed
2789 if (home_location[0] != DIRECTORY_SYMBOL) {
2790 len = strlen(home_loc);
2791 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002792 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002793 }
2794 strcat(home_loc, home_location);
2795 file = home_loc;
2796 next_file = loader_get_next_path(file);
2797 home_location = NULL;
2798
Jon Ashburn1530c342016-02-26 13:14:27 -07002799 loader_log(
2800 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002801 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002802 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002803 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002804
2805 } else {
2806
2807 char *home = secure_getenv("HOME");
2808 if (home != NULL) {
2809 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2810 strlen(home_location));
2811 if (home_loc == NULL) {
2812 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2813 "Out of memory can't get manifest files");
2814 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2815 goto out;
2816 }
2817 strcpy(home_loc, home);
2818
2819 len = strlen(home);
2820 if (home[len] != DIRECTORY_SYMBOL) {
2821 home_loc[len] = DIRECTORY_SYMBOL;
2822 home_loc[len + 1] = '\0';
2823 }
2824 strcat(home_loc, ".local/share");
2825
2826 if (home_location[0] != DIRECTORY_SYMBOL) {
2827 len = strlen(home_loc);
2828 home_loc[len] = DIRECTORY_SYMBOL;
2829 home_loc[len + 1] = '\0';
2830 }
2831 strcat(home_loc, home_location);
2832 file = home_loc;
2833 next_file = loader_get_next_path(file);
2834 home_location = NULL;
2835
2836 loader_log(
2837 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2838 "Searching the following path for manifest files: %s\n",
2839 home_loc);
2840 list_is_dirs = true;
2841 } else {
2842 // without knowing HOME, we just.. give up
2843 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002844 }
2845 }
2846#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002847 }
Mark Young0ad83132016-06-30 13:02:42 -06002848
2849out:
2850 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2851 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2852 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2853 }
2854 loader_instance_heap_free(inst, out_files->filename_list);
2855 out_files->count = 0;
2856 out_files->filename_list = NULL;
2857 }
2858
2859 if (NULL != sysdir) {
2860 closedir(sysdir);
2861 }
2862
2863 if (NULL != reg && reg != orig_loc) {
2864 loader_instance_heap_free(inst, reg);
2865 }
2866 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002867}
2868
Jon Ashburn23d36b12016-02-02 17:47:28 -07002869void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002870
Jon Ashburn23d36b12016-02-02 17:47:28 -07002871void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002872/**
2873 * Try to find the Vulkan ICD driver(s).
2874 *
2875 * This function scans the default system loader path(s) or path
2876 * specified by the \c VK_ICD_FILENAMES environment variable in
2877 * order to find loadable VK ICDs manifest files. From these
2878 * manifest files it finds the ICD libraries.
2879 *
2880 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002881 * Vulkan result
2882 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002883 */
Mark Young0ad83132016-06-30 13:02:42 -06002884VkResult loader_icd_scan(const struct loader_instance *inst,
2885 struct loader_icd_libs *icds) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002886 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002887 uint16_t file_major_vers = 0;
2888 uint16_t file_minor_vers = 0;
2889 uint16_t file_patch_vers = 0;
2890 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002891 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002892 VkResult res = VK_SUCCESS;
2893 bool lockedMutex = false;
2894 cJSON *json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002895 uint32_t num_good_icds = 0;
Jon Ashburn2077e382015-06-29 11:25:34 -06002896
Mark Young0ad83132016-06-30 13:02:42 -06002897 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2898
2899 res = loader_scanned_icd_init(inst, icds);
2900 if (VK_SUCCESS != res) {
2901 goto out;
2902 }
2903
Jon Ashburn2077e382015-06-29 11:25:34 -06002904 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002905 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
2906 DEFAULT_VK_DRIVERS_INFO,
2907 HOME_VK_DRIVERS_INFO, &manifest_files);
2908 if (VK_SUCCESS != res || manifest_files.count == 0) {
2909 goto out;
2910 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002911 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002912 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002913 for (uint32_t i = 0; i < manifest_files.count; i++) {
2914 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002915 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002916 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002917 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002918
Mark Young3a587792016-08-19 15:25:08 -06002919 res = loader_get_json(inst, file_str, &json);
2920 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
2921 break;
2922 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002923 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002924 }
Mark Young3a587792016-08-19 15:25:08 -06002925
Jon Ashburn005617f2015-11-17 17:35:40 -07002926 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002927 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002928 if (item == NULL) {
Mark Young3a587792016-08-19 15:25:08 -06002929 if (num_good_icds == 0) {
2930 res = VK_ERROR_INITIALIZATION_FAILED;
2931 }
Derrick Owens62e16ef2016-09-09 15:49:07 -04002932 cJSON_Delete(json);
2933 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002934 continue;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002935 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002936 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002937 if (NULL == file_vers) {
2938 // Only reason the print can fail is if there was an allocation
2939 // issue
Mark Young3a587792016-08-19 15:25:08 -06002940 if (num_good_icds == 0) {
2941 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2942 }
Derrick Owenscd92b8b2016-09-09 15:45:13 -04002943 cJSON_Delete(json);
2944 json = NULL;
Mark Young3a587792016-08-19 15:25:08 -06002945 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002946 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002947 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2948 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002949 // Get the major/minor/and patch as integers for easier comparison
2950 vers_tok = strtok(file_vers, ".\"\n\r");
2951 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002952 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002953 vers_tok = strtok(NULL, ".\"\n\r");
2954 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002955 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002956 vers_tok = strtok(NULL, ".\"\n\r");
2957 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002958 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002959 }
2960 }
2961 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002962 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002963 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002964 "Unexpected manifest file version (expected 1.0.0 or "
2965 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002966 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002967 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002968 itemICD = cJSON_GetObjectItem(json, "ICD");
2969 if (itemICD != NULL) {
2970 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002971 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002972 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002973 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002974 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002975 "Can't find \"library_path\" in ICD JSON file "
2976 "%s, skipping",
2977 file_str);
Mark Young3a587792016-08-19 15:25:08 -06002978 if (num_good_icds == 0) {
2979 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2980 }
Mark Young0ad83132016-06-30 13:02:42 -06002981 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002982 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002983 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002984 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06002985 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002986 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06002987 temp[strlen(temp) - 1] = '\0';
2988 char *library_path = loader_stack_alloc(strlen(temp) + 1);
Mark Young3a587792016-08-19 15:25:08 -06002989 if (NULL == library_path) {
2990 loader_log(
2991 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2992 "Can't allocate space for \"library_path\" in ICD "
2993 "JSON file %s, skipping",
2994 file_str);
2995 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2996 cJSON_Free(temp);
2997 cJSON_Delete(json);
2998 json = NULL;
2999 goto out;
3000 }
Jon Ashburn86251302015-08-25 16:48:24 -06003001 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06003002 cJSON_Free(temp);
Mark Young3a587792016-08-19 15:25:08 -06003003 if (strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003004 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003005 "Can't find \"library_path\" in ICD JSON file "
3006 "%s, skipping",
3007 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06003008 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06003009 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06003010 continue;
3011 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04003012 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06003013 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07003014 loader_log(
3015 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04003016 "Searching for ICD drivers named %s default dir %s\n",
3017 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003018 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06003019 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003020 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
3021 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06003022 strcpy(name_copy, file_str);
3023 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003024 loader_expand_path(library_path, rel_base, sizeof(fullpath),
3025 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05003026 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04003027 // a filename which is assumed in a system directory
3028 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
3029 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06003030 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003031
3032 uint32_t vers = 0;
3033 item = cJSON_GetObjectItem(itemICD, "api_version");
3034 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003035 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06003036 if (NULL == temp) {
3037 // Only reason the print can fail is if there was an
3038 // allocation issue
3039 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3040 goto out;
3041 }
Jon Ashburn005617f2015-11-17 17:35:40 -07003042 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06003043 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07003044 }
Mark Young3a587792016-08-19 15:25:08 -06003045 res = loader_scanned_icd_add(inst, icds, fullpath, vers);
3046 if (VK_SUCCESS != res) {
3047 goto out;
3048 }
3049 num_good_icds++;
Mark Young0ad83132016-06-30 13:02:42 -06003050 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003051 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003052 "Can't find \"library_path\" object in ICD JSON "
3053 "file %s, skipping",
3054 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003055 }
3056 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003057 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003058 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003059 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
3060 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06003061 }
Jon Ashburn2077e382015-06-29 11:25:34 -06003062
Mark Young0ad83132016-06-30 13:02:42 -06003063 cJSON_Delete(json);
3064 json = NULL;
3065 }
3066
3067out:
3068 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06003069 cJSON_Delete(json);
3070 }
Mark Young0ad83132016-06-30 13:02:42 -06003071 if (NULL != manifest_files.filename_list) {
3072 for (uint32_t i = 0; i < manifest_files.count; i++) {
3073 if (NULL != manifest_files.filename_list[i]) {
3074 loader_instance_heap_free(inst,
3075 manifest_files.filename_list[i]);
3076 }
3077 }
3078 loader_instance_heap_free(inst, manifest_files.filename_list);
3079 }
3080 if (lockedMutex) {
3081 loader_platform_thread_unlock_mutex(&loader_json_lock);
3082 }
3083 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003084}
3085
Jon Ashburn23d36b12016-02-02 17:47:28 -07003086void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003087 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003088 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003089 struct loader_manifest_files
3090 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003091 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07003092 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06003093 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003094
Mark Young0ad83132016-06-30 13:02:42 -06003095 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3096
3097 // Get a list of manifest files for explicit layers
3098 if (VK_SUCCESS !=
3099 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
3100 true, DEFAULT_VK_ELAYERS_INFO,
3101 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3102 goto out;
3103 }
3104
3105 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003106 // Pass NULL for environment variable override - implicit layers are not
3107 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003108 if (VK_SUCCESS != loader_get_manifest_files(
3109 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
3110 HOME_VK_ILAYERS_INFO, &manifest_files[1])) {
3111 goto out;
3112 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003113
Mark Young0ad83132016-06-30 13:02:42 -06003114 // Make sure we have at least one layer, if not, go ahead and return
3115 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3116 goto out;
3117 }
3118
3119 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003120 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003121
Jon Ashburn6461ef22015-09-22 13:11:00 -06003122 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003123 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003124 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003125 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003126 file_str = manifest_files[implicit].filename_list[i];
3127 if (file_str == NULL)
3128 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003129
Jon Ashburn075ce432015-12-17 17:38:24 -07003130 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003131 VkResult res = loader_get_json(inst, file_str, &json);
3132 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3133 break;
3134 } else if (VK_SUCCESS != res || NULL == json) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003135 continue;
3136 }
3137
Jon Ashburn491cd042016-05-16 14:01:18 -06003138 loader_add_layer_properties(inst, instance_layers, json,
3139 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003140 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003141 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003142 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003143
3144 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003145 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3146 sizeof(std_validation_names[0]),
3147 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003148
Mark Young0ad83132016-06-30 13:02:42 -06003149out:
3150
3151 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3152 if (NULL != manifest_files[manFile].filename_list) {
3153 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3154 if (NULL != manifest_files[manFile].filename_list[i]) {
3155 loader_instance_heap_free(
3156 inst, manifest_files[manFile].filename_list[i]);
3157 }
3158 }
3159 loader_instance_heap_free(inst,
3160 manifest_files[manFile].filename_list);
3161 }
3162 }
3163 if (lockedMutex) {
3164 loader_platform_thread_unlock_mutex(&loader_json_lock);
3165 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003166}
3167
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003168void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003169 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003170 char *file_str;
3171 struct loader_manifest_files manifest_files;
3172 cJSON *json;
3173 uint32_t i;
3174
3175 // Pass NULL for environment variable override - implicit layers are not
3176 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003177 VkResult res = loader_get_manifest_files(
3178 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
3179 &manifest_files);
3180 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003181 return;
3182 }
3183
3184 /* cleanup any previously scanned libraries */
3185 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003186
3187 loader_platform_thread_lock_mutex(&loader_json_lock);
3188
3189 for (i = 0; i < manifest_files.count; i++) {
3190 file_str = manifest_files.filename_list[i];
3191 if (file_str == NULL) {
3192 continue;
3193 }
3194
3195 // parse file into JSON struct
Mark Young3a587792016-08-19 15:25:08 -06003196 res = loader_get_json(inst, file_str, &json);
3197 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
3198 break;
3199 } else if (VK_SUCCESS != res || NULL == json) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003200 continue;
3201 }
3202
Mark Young0ad83132016-06-30 13:02:42 -06003203 loader_add_layer_properties(inst, instance_layers, json, true,
3204 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003205
Mark Young0ad83132016-06-30 13:02:42 -06003206 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003207 cJSON_Delete(json);
3208 }
Mark Young0ad83132016-06-30 13:02:42 -06003209 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003210
3211 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003212 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3213 sizeof(std_validation_names[0]),
3214 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003215
3216 loader_platform_thread_unlock_mutex(&loader_json_lock);
3217}
3218
Jon Ashburn23d36b12016-02-02 17:47:28 -07003219static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3220loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003221 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003222 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003223 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003224 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003225 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003226 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003227
Jon Ashburn27cd5842015-05-12 17:26:48 -06003228 // inst is not wrapped
3229 if (inst == VK_NULL_HANDLE) {
3230 return NULL;
3231 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003232 VkLayerInstanceDispatchTable *disp_table =
3233 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003234 void *addr;
3235
3236 if (disp_table == NULL)
3237 return NULL;
3238
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003239 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003240 addr =
3241 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003242 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003243 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003244 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003245
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003246 // Don't call down the chain, this would be an infinite loop
3247 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003248 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003249 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003250}
3251
Mark Younga7c51fd2016-09-16 10:18:42 -06003252void loader_override_terminating_device_proc(
3253 VkDevice device, struct loader_dev_dispatch_table *disp_table) {
3254 struct loader_device *dev;
3255 struct loader_icd *icd = loader_get_icd_and_device(device, &dev, NULL);
3256
3257 // Certain device entry-points still need to go through a terminator before
3258 // hitting the ICD. This could be for several reasons, but the main one
3259 // is currently unwrapping an object before passing the appropriate info
3260 // along to the ICD.
3261 if ((PFN_vkVoidFunction)disp_table->core_dispatch.CreateSwapchainKHR ==
3262 (PFN_vkVoidFunction)icd->GetDeviceProcAddr(device,
3263 "vkCreateSwapchainKHR")) {
3264 disp_table->core_dispatch.CreateSwapchainKHR =
3265 terminator_vkCreateSwapchainKHR;
3266 }
3267}
Mark Young16573c72016-06-28 10:52:43 -06003268
Piers Daniellf9262be2016-09-14 11:24:36 -06003269VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003270loader_gpa_device_internal(VkDevice device, const char *pName) {
3271 struct loader_device *dev;
Mark Young16573c72016-06-28 10:52:43 -06003272 struct loader_icd *icd = loader_get_icd_and_device(device, &dev, NULL);
3273
3274 // Certain device entry-points still need to go through a terminator before
3275 // hitting the ICD. This could be for several reasons, but the main one
3276 // is currently unwrapping an object before passing the appropriate info
3277 // along to the ICD.
3278 if (!strcmp(pName, "vkCreateSwapchainKHR")) {
3279 return (PFN_vkVoidFunction)terminator_vkCreateSwapchainKHR;
3280 }
3281
Piers Daniell295fe402016-03-29 11:51:11 -06003282 return icd->GetDeviceProcAddr(device, pName);
3283}
3284
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003285/**
3286 * Initialize device_ext dispatch table entry as follows:
3287 * If dev == NULL find all logical devices created within this instance and
3288 * init the entry (given by idx) in the ext dispatch table.
3289 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003290 * The initialization value is gotten by calling down the device chain with
3291 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003292 * If GDPA returns NULL then don't initialize the dispatch table entry.
3293 */
3294static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003295 struct loader_device *dev,
3296 uint32_t idx,
3297 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003298
Jon Ashburn23d36b12016-02-02 17:47:28 -07003299{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003300 void *gdpa_value;
3301 if (dev != NULL) {
3302 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003303 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003304 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003305 dev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003306 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003307 } else {
3308 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
3309 struct loader_icd *icd = &inst->icds[i];
Karl Schultz2558bd32016-02-24 14:39:39 -07003310 struct loader_device *ldev = icd->logical_device_list;
3311 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003312 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003313 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3314 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003315 if (gdpa_value != NULL)
Mark Young8191d9f2016-09-02 11:41:28 -06003316 ldev->loader_dispatch.ext_dispatch.dev_ext[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003317 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003318 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003319 }
3320 }
3321 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003322}
3323
3324/**
3325 * Find all dev extension in the hash table and initialize the dispatch table
3326 * for dev for each of those extension entrypoints found in hash table.
3327
3328 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003329void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3330 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003331 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3332 if (inst->disp_hash[i].func_name != NULL)
3333 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3334 inst->disp_hash[i].func_name);
3335 }
3336}
3337
3338static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003339 const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003340 struct loader_icd *icd;
3341 icd = inst->icds;
3342 while (icd) {
3343 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
3344 // this icd supports funcName
3345 return true;
3346 icd = icd->next;
3347 }
3348
3349 return false;
3350}
3351
Jon Ashburncc407a22016-04-15 09:25:03 -06003352static bool loader_check_layer_list_for_address(
3353 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003354 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003355 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003356 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003357 const struct loader_device_extension_list *const extensions =
3358 &(layers->list[layer].device_extension_list);
3359 for (uint32_t extension = 0; extension < extensions->count;
3360 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003361 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003362 const struct loader_dev_ext_props *const property =
3363 &(extensions->list[extension]);
3364 for (uint32_t entry = 0; entry < property->entrypoint_count;
3365 ++entry) {
3366 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003367 return true;
3368 }
3369 }
3370 }
3371 }
3372
3373 return false;
3374}
3375
Jon Ashburn23d36b12016-02-02 17:47:28 -07003376static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003377 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003378 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3379 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003380 }
3381 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3382}
3383
3384static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003385 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003386 uint32_t i;
3387 uint32_t idx = *ptr_idx;
3388 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3389
3390 if (!inst->disp_hash[idx].func_name) {
3391 // no entry here at this idx, so use it
3392 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003393 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003394 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003395 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003396 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003397 "loader_add_dev_ext_table() can't allocate memory for "
3398 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003399 return false;
3400 }
3401 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3402 return true;
3403 }
3404
3405 // check for enough capacity
3406 if (list->capacity == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06003407 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003408 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3409 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003410 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003411 "loader_add_dev_ext_table() can't allocate list memory");
3412 return false;
3413 }
3414 list->capacity = 8 * sizeof(*(list->index));
3415 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0ad83132016-06-30 13:02:42 -06003416 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003417 list->capacity * 2,
3418 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003419 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003420 loader_log(
3421 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3422 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003423 return false;
3424 }
3425 list->capacity *= 2;
3426 }
3427
Jon Ashburn23d36b12016-02-02 17:47:28 -07003428 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003429 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3430 do {
3431 if (!inst->disp_hash[i].func_name) {
3432 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003433 inst->disp_hash[i].func_name =
Mark Young0ad83132016-06-30 13:02:42 -06003434 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003435 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003436 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003437 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003438 "loader_add_dev_ext_table() can't rallocate "
3439 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003440 return false;
3441 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003442 strncpy(inst->disp_hash[i].func_name, funcName,
3443 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003444 list->index[list->count] = i;
3445 list->count++;
3446 *ptr_idx = i;
3447 return true;
3448 }
3449 i = (i + 1) % MAX_NUM_DEV_EXTS;
3450 } while (i != idx);
3451
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003452 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003453 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3454 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003455 return false;
3456}
3457
3458static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003459 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003460 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003461 if (inst->disp_hash[*idx].func_name &&
3462 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003463 return true;
3464
3465 // funcName wasn't at the primary spot in the hash table
3466 // search the list of secondary locations (shallow search, not deep search)
3467 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3468 alt_idx = inst->disp_hash[*idx].list.index[i];
3469 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3470 *idx = alt_idx;
3471 return true;
3472 }
3473 }
3474
3475 return false;
3476}
3477
3478/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003479 * This function returns generic trampoline code address for unknown entry
3480 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003481 * Presumably, these unknown entry points (as given by funcName) are device
3482 * extension entrypoints. A hash table is used to keep a list of unknown entry
3483 * points and their mapping to the device extension dispatch table
3484 * (struct loader_dev_ext_dispatch_table).
3485 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003486 * For a given entry point string (funcName), if an existing mapping is found
3487 * the
3488 * trampoline address for that mapping is returned. Otherwise, this unknown
3489 * entry point
3490 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3491 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003492 * new entry in the hash table is initialized and that trampoline address for
3493 * the new entry is returned. Null is returned if the hash table is full or
3494 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3495 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003496void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003497 uint32_t idx;
3498 uint32_t seed = 0;
3499
3500 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3501
3502 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3503 // found funcName already in hash
3504 return loader_get_dev_ext_trampoline(idx);
3505
3506 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003507 if (!loader_check_icds_for_address(inst, funcName) &&
Mark Young3fc992b2016-10-04 15:19:30 -06003508 !loader_check_layer_list_for_address(&inst->instance_layer_list, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003509 // if support found in layers continue on
3510 return NULL;
3511 }
3512
3513 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3514 // successfully added new table entry
3515 // init any dev dispatch table entrys as needed
3516 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3517 return loader_get_dev_ext_trampoline(idx);
3518 }
3519
3520 return NULL;
3521}
3522
Jon Ashburn23d36b12016-02-02 17:47:28 -07003523struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003524 /* look up the loader_instance in our list by comparing dispatch tables, as
3525 * there is no guarantee the instance is still a loader_instance* after any
3526 * layers which wrap the instance object.
3527 */
3528 const VkLayerInstanceDispatchTable *disp;
3529 struct loader_instance *ptr_instance = NULL;
3530 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003531 for (struct loader_instance *inst = loader.instances; inst;
3532 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003533 if (inst->disp == disp) {
3534 ptr_instance = inst;
3535 break;
3536 }
3537 }
3538 return ptr_instance;
3539}
3540
Jon Ashburn23d36b12016-02-02 17:47:28 -07003541static loader_platform_dl_handle
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003542loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3543 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003544
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003545 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003546 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003547 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003548 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003549 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003550 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003551 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003552 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003553 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003554
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003555 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003556}
3557
Jon Ashburn23d36b12016-02-02 17:47:28 -07003558static void
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003559loader_close_layer_lib(const struct loader_instance *inst,
3560 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003561
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003562 if (prop->lib_handle) {
3563 loader_platform_close_library(prop->lib_handle);
3564 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3565 "Unloading layer library %s", prop->lib_name);
3566 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003567 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003568}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003569
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003570void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003571 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003572 struct loader_layer_list *list) {
3573 /* delete instance list of enabled layers and close any layer libraries */
3574 for (uint32_t i = 0; i < list->count; i++) {
3575 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003576
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003577 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003578 }
Mark Young0ad83132016-06-30 13:02:42 -06003579 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003580}
3581
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003582/**
3583 * Go through the search_list and find any layers which match type. If layer
3584 * type match is found in then add it to ext_list.
3585 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003586static void
3587loader_add_layer_implicit(const struct loader_instance *inst,
3588 const enum layer_type type,
3589 struct loader_layer_list *list,
3590 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003591 bool enable;
3592 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003593 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003594 for (i = 0; i < search_list->count; i++) {
3595 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003596 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003597 /* Found an implicit layer, see if it should be enabled */
3598 enable = false;
3599
Jon Ashburn23d36b12016-02-02 17:47:28 -07003600 // if no enable_environment variable is specified, this implicit
3601 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003602 // should always be enabled. Otherwise check if the variable is set
3603 if (prop->enable_env_var.name[0] == 0) {
3604 enable = true;
3605 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003606 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003607 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3608 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003609 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003610 }
3611
3612 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003613 // environment variables are set, the layer is disabled. Implicit
3614 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003615 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003616 env_value = loader_getenv(prop->disable_env_var.name, inst);
3617 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003618 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003619 }
3620 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003621
Mark Young0ad83132016-06-30 13:02:42 -06003622 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003623 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003624 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003625 }
3626 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003627}
3628
3629/**
3630 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003631 * is found in search_list then add it to layer_list. But only add it to
3632 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003633 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003634static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003635 const enum layer_type type,
3636 const char *env_name,
3637 struct loader_layer_list *layer_list,
3638 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003639 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003640 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003641
Mark Young0ad83132016-06-30 13:02:42 -06003642 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003643 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003644 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003645 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003646 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003647 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003648 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003649 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003650 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003651
Mark Young0ad83132016-06-30 13:02:42 -06003652 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003653
Jon Ashburn23d36b12016-02-02 17:47:28 -07003654 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003655 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003656 if (!strcmp(std_validation_str, name)) {
3657 /* add meta list of layers
3658 don't attempt to remove duplicate layers already added by app or
3659 env var
3660 */
3661 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3662 "Expanding meta layer %s found in environment variable",
3663 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003664 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3665 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003666 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3667 sizeof(std_validation_names[0]);
3668 i++) {
3669 loader_find_layer_name_add_list(inst, std_validation_names[i],
3670 type, search_list, layer_list);
3671 }
3672 } else {
3673 loader_find_layer_name_add_list(inst, name, type, search_list,
3674 layer_list);
3675 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003676 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003677 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003678
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003679 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003680}
3681
Jon Ashburn23d36b12016-02-02 17:47:28 -07003682VkResult
3683loader_enable_instance_layers(struct loader_instance *inst,
3684 const VkInstanceCreateInfo *pCreateInfo,
3685 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003686 VkResult err;
3687
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003688 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003689
Jon Ashburne39a4f82015-08-28 13:38:21 -06003690 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003691 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3692 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003693 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003694 }
3695
Jon Ashburn0c26e712015-07-02 16:10:32 -06003696 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003697 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3698 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003699
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003700 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003701 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3702 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003703 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003704
3705 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003706 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003707 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3708 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003709
3710 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003711}
3712
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003713/*
3714 * Given the list of layers to activate in the loader_instance
3715 * structure. This function will add a VkLayerInstanceCreateInfo
3716 * structure to the VkInstanceCreateInfo.pNext pointer.
3717 * Each activated layer will have it's own VkLayerInstanceLink
3718 * structure that tells the layer what Get*ProcAddr to call to
3719 * get function pointers to the next layer down.
3720 * Once the chain info has been created this function will
3721 * execute the CreateInstance call chain. Each layer will
3722 * then have an opportunity in it's CreateInstance function
3723 * to setup it's dispatch table when the lower layer returns
3724 * successfully.
3725 * Each layer can wrap or not-wrap the returned VkInstance object
3726 * as it sees fit.
3727 * The instance chain is terminated by a loader function
3728 * that will call CreateInstance on all available ICD's and
3729 * cache those VkInstance objects for future use.
3730 */
3731VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003732 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003733 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003734 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003735 uint32_t activated_layers = 0;
3736 VkLayerInstanceCreateInfo chain_info;
3737 VkLayerInstanceLink *layer_instance_link_info = NULL;
3738 VkInstanceCreateInfo loader_create_info;
3739 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003740
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003741 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3742 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003743
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003744 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003745
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003746 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003747
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003748 chain_info.u.pLayerInfo = NULL;
3749 chain_info.pNext = pCreateInfo->pNext;
3750 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3751 chain_info.function = VK_LAYER_LINK_INFO;
3752 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003753
Jon Ashburn23d36b12016-02-02 17:47:28 -07003754 layer_instance_link_info = loader_stack_alloc(
3755 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003756 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003757 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3758 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003759 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003760 }
3761
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003762 /* Create instance chain of enabled layers */
3763 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003764 struct loader_layer_properties *layer_prop =
3765 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003766 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003767
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003768 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003769 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003770 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003771 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3772 NULL) {
3773 if (layer_prop->functions.str_gipa == NULL ||
3774 strlen(layer_prop->functions.str_gipa) == 0) {
3775 fpGIPA = (PFN_vkGetInstanceProcAddr)
3776 loader_platform_get_proc_address(
3777 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003778 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3779 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003780 fpGIPA = (PFN_vkGetInstanceProcAddr)
3781 loader_platform_get_proc_address(
3782 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003783 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003784 loader_log(
3785 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3786 "Failed to find vkGetInstanceProcAddr in layer %s",
3787 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003788 continue;
3789 }
3790 }
3791
Jon Ashburn23d36b12016-02-02 17:47:28 -07003792 layer_instance_link_info[activated_layers].pNext =
3793 chain_info.u.pLayerInfo;
3794 layer_instance_link_info[activated_layers]
3795 .pfnNextGetInstanceProcAddr = nextGIPA;
3796 chain_info.u.pLayerInfo =
3797 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003798 nextGIPA = fpGIPA;
3799
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003800 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003801 "Insert instance layer %s (%s)",
3802 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003803
3804 activated_layers++;
3805 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003806 }
3807
Jon Ashburn23d36b12016-02-02 17:47:28 -07003808 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003809 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003810 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003811 VkLayerInstanceCreateInfo create_info_disp;
3812
Jon Ashburncc407a22016-04-15 09:25:03 -06003813 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003814 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003815
3816 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3817
3818 create_info_disp.pNext = loader_create_info.pNext;
3819 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003820 res =
3821 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003822 } else {
3823 // Couldn't find CreateInstance function!
3824 res = VK_ERROR_INITIALIZATION_FAILED;
3825 }
3826
3827 if (res != VK_SUCCESS) {
3828 // TODO: Need to clean up here
3829 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003830 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003831 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003832 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003833 }
3834
3835 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003836}
3837
Jon Ashburn23d36b12016-02-02 17:47:28 -07003838void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3839 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003840
Jon Ashburn23d36b12016-02-02 17:47:28 -07003841 loader_init_instance_extension_dispatch_table(
3842 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003843}
3844
Jon Ashburn1530c342016-02-26 13:14:27 -07003845VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003846loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3847 const VkDeviceCreateInfo *pCreateInfo,
3848 const VkAllocationCallbacks *pAllocator,
3849 const struct loader_instance *inst,
3850 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003851 uint32_t activated_layers = 0;
3852 VkLayerDeviceLink *layer_device_link_info;
3853 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003854 VkDeviceCreateInfo loader_create_info;
3855 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003856
Piers Daniell295fe402016-03-29 11:51:11 -06003857 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003858 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003859
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003860 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3861
Jon Ashburn23d36b12016-02-02 17:47:28 -07003862 layer_device_link_info = loader_stack_alloc(
3863 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003864 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003865 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3866 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003867 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003868 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003869
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003870 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003871 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3872 chain_info.function = VK_LAYER_LINK_INFO;
3873 chain_info.u.pLayerInfo = NULL;
3874 chain_info.pNext = pCreateInfo->pNext;
3875 loader_create_info.pNext = &chain_info;
3876
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003877 /* Create instance chain of enabled layers */
3878 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003879 struct loader_layer_properties *layer_prop =
3880 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003881 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003882
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003883 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003884 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003885 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003886 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3887 NULL) {
3888 if (layer_prop->functions.str_gipa == NULL ||
3889 strlen(layer_prop->functions.str_gipa) == 0) {
3890 fpGIPA = (PFN_vkGetInstanceProcAddr)
3891 loader_platform_get_proc_address(
3892 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003893 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3894 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003895 fpGIPA = (PFN_vkGetInstanceProcAddr)
3896 loader_platform_get_proc_address(
3897 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003898 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003899 loader_log(
3900 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3901 "Failed to find vkGetInstanceProcAddr in layer %s",
3902 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003903 continue;
3904 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003905 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003906 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003907 if (layer_prop->functions.str_gdpa == NULL ||
3908 strlen(layer_prop->functions.str_gdpa) == 0) {
3909 fpGDPA = (PFN_vkGetDeviceProcAddr)
3910 loader_platform_get_proc_address(lib_handle,
3911 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003912 layer_prop->functions.get_device_proc_addr = fpGDPA;
3913 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003914 fpGDPA = (PFN_vkGetDeviceProcAddr)
3915 loader_platform_get_proc_address(
3916 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003917 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003918 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003919 "Failed to find vkGetDeviceProcAddr in layer %s",
3920 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003921 continue;
3922 }
3923 }
3924
Jon Ashburn23d36b12016-02-02 17:47:28 -07003925 layer_device_link_info[activated_layers].pNext =
3926 chain_info.u.pLayerInfo;
3927 layer_device_link_info[activated_layers]
3928 .pfnNextGetInstanceProcAddr = nextGIPA;
3929 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3930 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003931 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3932 nextGIPA = fpGIPA;
3933 nextGDPA = fpGDPA;
3934
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003935 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003936 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003937 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003938
3939 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003940 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003941 }
3942
Jon Ashburncc407a22016-04-15 09:25:03 -06003943 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003944 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003945 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003946 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003947 VkLayerDeviceCreateInfo create_info_disp;
3948
Jon Ashburncc407a22016-04-15 09:25:03 -06003949 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003950 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3951
3952 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3953
3954 create_info_disp.pNext = loader_create_info.pNext;
3955 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003956 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003957 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003958 if (res != VK_SUCCESS) {
3959 return res;
3960 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003961 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003962 } else {
3963 // Couldn't find CreateDevice function!
3964 return VK_ERROR_INITIALIZATION_FAILED;
3965 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003966
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003967 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003968 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003969 dev->device);
3970
3971 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003972}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003973
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003974VkResult loader_validate_layers(const struct loader_instance *inst,
3975 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003976 const char *const *ppEnabledLayerNames,
3977 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003978 struct loader_layer_properties *prop;
3979
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003980 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003981 VkStringErrorFlags result =
3982 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003983 if (result != VK_STRING_ERROR_NONE) {
3984 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003985 "Loader: Device ppEnabledLayerNames contains string "
3986 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003987 return VK_ERROR_LAYER_NOT_PRESENT;
3988 }
3989
Jon Ashburn23d36b12016-02-02 17:47:28 -07003990 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003991 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003992 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003993 }
3994 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003995 return VK_SUCCESS;
3996}
3997
3998VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003999 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004000 const struct loader_extension_list *icd_exts,
4001 const struct loader_layer_list *instance_layer,
4002 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004003
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004004 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004005 struct loader_layer_properties *layer_prop;
4006
Jon Ashburnf19916e2016-01-11 13:12:43 -07004007 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004008 VkStringErrorFlags result = vk_string_validate(
4009 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004010 if (result != VK_STRING_ERROR_NONE) {
4011 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004012 "Loader: Instance ppEnabledExtensionNames contains "
4013 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004014 return VK_ERROR_EXTENSION_NOT_PRESENT;
4015 }
4016
Jon Ashburn23d36b12016-02-02 17:47:28 -07004017 extension_prop = get_extension_property(
4018 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004019
4020 if (extension_prop) {
4021 continue;
4022 }
4023
4024 extension_prop = NULL;
4025
4026 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07004027 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004028 layer_prop = loader_get_layer_property(
Jan-Harald Fredriksene812a642016-09-28 12:10:24 +02004029 pCreateInfo->ppEnabledLayerNames[j], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004030 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06004031 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004032 * should have already filtered this case out.
4033 */
4034 continue;
4035 }
4036
Jon Ashburn23d36b12016-02-02 17:47:28 -07004037 extension_prop =
4038 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
4039 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004040 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004041 /* Found the extension in one of the layers enabled by the app.
4042 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004043 break;
4044 }
4045 }
4046
4047 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004048 /* Didn't find extension name in any of the global layers, error out
4049 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004050 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004051 }
4052 }
4053 return VK_SUCCESS;
4054}
4055
4056VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06004057 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004058 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07004059 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07004060 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004061 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004062 struct loader_layer_properties *layer_prop;
4063
Jon Ashburnf19916e2016-01-11 13:12:43 -07004064 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004065
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004066 VkStringErrorFlags result = vk_string_validate(
4067 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004068 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06004069 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4070 0, "Loader: Device ppEnabledExtensionNames contains "
4071 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004072 return VK_ERROR_EXTENSION_NOT_PRESENT;
4073 }
4074
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004075 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004076 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004077
4078 if (extension_prop) {
4079 continue;
4080 }
4081
Jon Ashburn471f44c2016-01-13 12:51:43 -07004082 /* Not in global list, search activated layer extension lists */
4083 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
4084 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004085
Jon Ashburn23d36b12016-02-02 17:47:28 -07004086 extension_prop = get_dev_extension_property(
4087 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004088 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004089 /* Found the extension in one of the layers enabled by the app.
4090 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004091 break;
4092 }
4093 }
4094
4095 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004096 /* Didn't find extension name in any of the device layers, error out
4097 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06004098 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004099 }
4100 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06004101 return VK_SUCCESS;
4102}
4103
Jon Ashburn1530c342016-02-26 13:14:27 -07004104/**
4105 * Terminator functions for the Instance chain
4106 * All named terminator_<Vulakn API name>
4107 */
Mark Young0ad83132016-06-30 13:02:42 -06004108VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
4109 const VkInstanceCreateInfo *pCreateInfo,
4110 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Jon Ashburn46888392015-01-29 15:45:51 -07004111 struct loader_icd *icd;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06004112 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004113 char **filtered_extension_names = NULL;
4114 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004115 VkResult res = VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004116
Jon Ashburncc407a22016-04-15 09:25:03 -06004117 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004118 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4119
Jon Ashburnf19916e2016-01-11 13:12:43 -07004120 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004121 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004122
4123 /*
4124 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004125 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004126 * No ICD will advertise support for layers. An ICD
4127 * library could support a layer, but it would be
4128 * independent of the actual ICD, just in the same library.
4129 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004130 filtered_extension_names =
4131 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004132 if (!filtered_extension_names) {
Mark Young3a587792016-08-19 15:25:08 -06004133 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4134 goto out;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004135 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004136 icd_create_info.ppEnabledExtensionNames =
4137 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004138
Jon Ashburn8810c5f2015-08-18 18:04:47 -06004139 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
4140 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06004141 if (NULL == icd) {
Mark Young3a587792016-08-19 15:25:08 -06004142 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4143 goto out;
Mark Young0ad83132016-06-30 13:02:42 -06004144 }
4145 icd_create_info.enabledExtensionCount = 0;
4146 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004147
Mark Young0ad83132016-06-30 13:02:42 -06004148 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4149 "Build ICD instance extension list");
4150 // traverse scanned icd list adding non-duplicate extensions to the
4151 // list
Mark Young3a587792016-08-19 15:25:08 -06004152 res = loader_init_generic_list(ptr_instance,
Mark Young0ad83132016-06-30 13:02:42 -06004153 (struct loader_generic_list *)&icd_exts,
4154 sizeof(VkExtensionProperties));
Mark Young3a587792016-08-19 15:25:08 -06004155 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4156 // If out of memory, bail immediately.
4157 goto out;
4158 } else if (VK_SUCCESS != res) {
Mark Young7e471292016-09-06 09:53:45 -06004159 // Something bad happened with this ICD, so free it and try the
4160 // next.
4161 ptr_instance->icds = icd->next;
Mark Youngdb13a2a2016-09-06 13:53:03 -06004162 icd->next = NULL;
Mark Young7e471292016-09-06 09:53:45 -06004163 loader_icd_destroy(ptr_instance, icd, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004164 continue;
4165 }
4166
4167 res = loader_add_instance_extensions(
Mark Young0ad83132016-06-30 13:02:42 -06004168 ptr_instance,
4169 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
4170 icd->this_icd_lib->lib_name, &icd_exts);
Mark Youngdb13a2a2016-09-06 13:53:03 -06004171 if (VK_SUCCESS != res) {
4172 loader_destroy_generic_list(ptr_instance,
4173 (struct loader_generic_list *)&icd_exts);
4174 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4175 // If out of memory, bail immediately.
4176 goto out;
4177 } else {
4178 // Something bad happened with this ICD, so free it and try
4179 // the next.
4180 ptr_instance->icds = icd->next;
4181 icd->next = NULL;
4182 loader_icd_destroy(ptr_instance, icd, pAllocator);
4183 continue;
4184 }
Mark Young3a587792016-08-19 15:25:08 -06004185 }
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004186
Mark Young0ad83132016-06-30 13:02:42 -06004187 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4188 prop = get_extension_property(
4189 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4190 if (prop) {
4191 filtered_extension_names[icd_create_info
4192 .enabledExtensionCount] =
4193 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4194 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004195 }
4196 }
Mark Young0ad83132016-06-30 13:02:42 -06004197
4198 loader_destroy_generic_list(ptr_instance,
4199 (struct loader_generic_list *)&icd_exts);
4200
4201 res = ptr_instance->icd_libs.list[i].CreateInstance(
4202 &icd_create_info, pAllocator, &(icd->instance));
Mark Young3a587792016-08-19 15:25:08 -06004203 if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
4204 // If out of memory, bail immediately.
4205 goto out;
4206 } else if (VK_SUCCESS != res) {
Mark Young3a587792016-08-19 15:25:08 -06004207 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4208 "ICD ignored: failed to CreateInstance in ICD %d", i);
Mark Young7e471292016-09-06 09:53:45 -06004209 ptr_instance->icds = icd->next;
Mark Youngdb13a2a2016-09-06 13:53:03 -06004210 icd->next = NULL;
Mark Young7e471292016-09-06 09:53:45 -06004211 loader_icd_destroy(ptr_instance, icd, pAllocator);
Mark Young3a587792016-08-19 15:25:08 -06004212 continue;
4213 }
Mark Young0ad83132016-06-30 13:02:42 -06004214
Mark Young3a587792016-08-19 15:25:08 -06004215 if (!loader_icd_init_entrys(
4216 icd, icd->instance,
4217 ptr_instance->icd_libs.list[i].GetInstanceProcAddr)) {
4218 loader_log(ptr_instance, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06004219 "ICD ignored: failed to CreateInstance and find "
4220 "entrypoints with ICD");
Mark Young3a587792016-08-19 15:25:08 -06004221 continue;
Mark Young0ad83132016-06-30 13:02:42 -06004222 }
Jon Ashburn46888392015-01-29 15:45:51 -07004223 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004224
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004225 /*
4226 * If no ICDs were added to instance list and res is unchanged
4227 * from it's initial value, the loader was unable to find
4228 * a suitable ICD.
4229 */
Mark Young3a587792016-08-19 15:25:08 -06004230 if (VK_SUCCESS == res && ptr_instance->icds == NULL) {
4231 res = VK_ERROR_INCOMPATIBLE_DRIVER;
4232 }
4233
4234out:
4235
4236 if (VK_SUCCESS != res) {
4237 while (NULL != ptr_instance->icds) {
4238 icd = ptr_instance->icds;
4239 ptr_instance->icds = icd->next;
4240 if (NULL != icd->instance) {
4241 icd->DestroyInstance(icd->instance, pAllocator);
4242 }
4243 loader_icd_destroy(ptr_instance, icd, pAllocator);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004244 }
Ian Elliotteb450762015-02-05 15:19:15 -07004245 }
Jon Ashburn46888392015-01-29 15:45:51 -07004246
Mark Young3a587792016-08-19 15:25:08 -06004247 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004248}
4249
Mark Young0ad83132016-06-30 13:02:42 -06004250VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4251 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004252 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004253 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06004254 struct loader_icd *next_icd;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004255
4256 // Remove this instance from the list of instances:
4257 struct loader_instance *prev = NULL;
4258 struct loader_instance *next = loader.instances;
4259 while (next != NULL) {
4260 if (next == ptr_instance) {
4261 // Remove this instance from the list:
4262 if (prev)
4263 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004264 else
4265 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004266 break;
4267 }
4268 prev = next;
4269 next = next->next;
4270 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004271
Jon Ashburn3da71f22015-05-14 12:43:38 -06004272 while (icds) {
4273 if (icds->instance) {
Chia-I Wuf7458c52015-10-26 21:10:41 +08004274 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004275 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06004276 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004277 icds->instance = VK_NULL_HANDLE;
Mark Young0ad83132016-06-30 13:02:42 -06004278 loader_icd_destroy(ptr_instance, icds, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004279
4280 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07004281 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004282
Jon Ashburn23d36b12016-02-02 17:47:28 -07004283 loader_delete_layer_properties(ptr_instance,
4284 &ptr_instance->instance_layer_list);
Jon Ashburne39a4f82015-08-28 13:38:21 -06004285 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004286 loader_destroy_generic_list(
4287 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004288 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004289 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004290 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004291}
4292
Mark Young0ad83132016-06-30 13:02:42 -06004293VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4294 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4295 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4296 VkResult res = VK_SUCCESS;
Jon Ashburn1530c342016-02-26 13:14:27 -07004297 struct loader_physical_device *phys_dev;
Jon Ashburn014438f2016-03-01 19:51:07 -07004298 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004299
Jon Ashburncc407a22016-04-15 09:25:03 -06004300 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn72690f22016-03-29 12:52:13 -06004301 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004302 struct loader_extension_list icd_exts;
4303
4304 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004305
Jon Ashburn1530c342016-02-26 13:14:27 -07004306 if (fpCreateDevice == NULL) {
Mark Young9a3ddd42016-10-21 16:25:47 -06004307 loader_log(phys_dev->this_icd->this_instance,
4308 VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4309 "No vkCreateDevice command exposed by ICD %s",
4310 phys_dev->this_icd->this_icd_lib->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004311 res = VK_ERROR_INITIALIZATION_FAILED;
4312 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004313 }
4314
Jon Ashburn1530c342016-02-26 13:14:27 -07004315 VkDeviceCreateInfo localCreateInfo;
4316 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004317
4318 /*
4319 * NOTE: Need to filter the extensions to only those
4320 * supported by the ICD.
4321 * No ICD will advertise support for layers. An ICD
4322 * library could support a layer, but it would be
4323 * independent of the actual ICD, just in the same library.
4324 */
4325 char **filtered_extension_names = NULL;
4326 filtered_extension_names =
4327 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4328 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004329 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004330 }
4331
Jon Ashburn1530c342016-02-26 13:14:27 -07004332 localCreateInfo.enabledLayerCount = 0;
4333 localCreateInfo.ppEnabledLayerNames = NULL;
4334
4335 localCreateInfo.enabledExtensionCount = 0;
4336 localCreateInfo.ppEnabledExtensionNames =
4337 (const char *const *)filtered_extension_names;
4338
Jon Ashburn014438f2016-03-01 19:51:07 -07004339 /* Get the physical device (ICD) extensions */
Mark Young3a587792016-08-19 15:25:08 -06004340 res = loader_init_generic_list(phys_dev->this_icd->this_instance,
Jon Ashburn014438f2016-03-01 19:51:07 -07004341 (struct loader_generic_list *)&icd_exts,
Mark Young3a587792016-08-19 15:25:08 -06004342 sizeof(VkExtensionProperties));
4343 if (VK_SUCCESS != res) {
Mark Young0ad83132016-06-30 13:02:42 -06004344 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004345 }
4346
4347 res = loader_add_device_extensions(
Jon Ashburncc407a22016-04-15 09:25:03 -06004348 phys_dev->this_icd->this_instance,
4349 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn014438f2016-03-01 19:51:07 -07004350 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
4351 &icd_exts);
4352 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004353 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004354 }
4355
Jon Ashburn1530c342016-02-26 13:14:27 -07004356 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4357 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004358 VkExtensionProperties *prop =
4359 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004360 if (prop) {
4361 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4362 (char *)extension_name;
4363 localCreateInfo.enabledExtensionCount++;
Mark Young9a3ddd42016-10-21 16:25:47 -06004364 } else {
4365 loader_log(phys_dev->this_icd->this_instance,
4366 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
4367 "vkCreateDevice extension %s not available for "
4368 "devices associated with ICD %s",
4369 extension_name,
4370 phys_dev->this_icd->this_icd_lib->lib_name);
Jon Ashburn1530c342016-02-26 13:14:27 -07004371 }
4372 }
4373
Mark Young9a3ddd42016-10-21 16:25:47 -06004374 res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo, pAllocator,
4375 &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004376 if (res != VK_SUCCESS) {
Mark Young9a3ddd42016-10-21 16:25:47 -06004377 loader_log(phys_dev->this_icd->this_instance,
4378 VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4379 "vkCreateDevice call failed in ICD %s",
4380 phys_dev->this_icd->this_icd_lib->lib_name);
Mark Young0ad83132016-06-30 13:02:42 -06004381 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004382 }
4383
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004384 *pDevice = dev->device;
Jon Ashburncc407a22016-04-15 09:25:03 -06004385 loader_add_logical_device(phys_dev->this_icd->this_instance,
4386 phys_dev->this_icd, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004387
4388 /* Init dispatch pointer in new device object */
4389 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4390
Mark Young0ad83132016-06-30 13:02:42 -06004391out:
4392 if (NULL != icd_exts.list) {
4393 loader_destroy_generic_list(phys_dev->this_icd->this_instance,
4394 (struct loader_generic_list *)&icd_exts);
4395 }
4396
Jon Ashburn1530c342016-02-26 13:14:27 -07004397 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004398}
4399
Jon Ashburn23d36b12016-02-02 17:47:28 -07004400VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004401terminator_EnumeratePhysicalDevices(VkInstance instance,
4402 uint32_t *pPhysicalDeviceCount,
4403 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004404 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004405 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004406 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004407
Jon Ashburn014438f2016-03-01 19:51:07 -07004408 struct loader_icd *icd;
4409 struct loader_phys_dev_per_icd *phys_devs;
4410
4411 inst->total_gpu_count = 0;
4412 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4413 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4414 if (!phys_devs)
4415 return VK_ERROR_OUT_OF_HOST_MEMORY;
4416
4417 icd = inst->icds;
4418 for (i = 0; i < inst->total_icd_count; i++) {
4419 assert(icd);
4420 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
4421 NULL);
4422 if (res != VK_SUCCESS)
4423 return res;
4424 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004425 }
4426
Jon Ashburn014438f2016-03-01 19:51:07 -07004427 icd = inst->icds;
4428 for (i = 0; i < inst->total_icd_count; i++) {
4429 assert(icd);
4430 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4431 phys_devs[i].count * sizeof(VkPhysicalDevice));
4432 if (!phys_devs[i].phys_devs) {
4433 return VK_ERROR_OUT_OF_HOST_MEMORY;
4434 }
4435 res = icd->EnumeratePhysicalDevices(
4436 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
4437 if ((res == VK_SUCCESS)) {
4438 inst->total_gpu_count += phys_devs[i].count;
4439 } else {
4440 return res;
4441 }
4442 phys_devs[i].this_icd = icd;
4443 icd = icd->next;
4444 }
4445
Mark Young559d7502016-09-26 11:38:46 -06004446 uint32_t copy_count = inst->total_gpu_count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004447
Mark Young559d7502016-09-26 11:38:46 -06004448 if (NULL != pPhysicalDevices) {
4449 // Initialize the output pPhysicalDevices with wrapped loader
4450 // terminator physicalDevice objects; save this list of
4451 // wrapped objects in instance struct for later cleanup and
4452 // use by trampoline code
4453 uint32_t j, idx = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004454
Mark Young559d7502016-09-26 11:38:46 -06004455 if (copy_count > *pPhysicalDeviceCount) {
4456 copy_count = *pPhysicalDeviceCount;
4457 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004458
Mark Young559d7502016-09-26 11:38:46 -06004459 if (inst->phys_devs_term) {
4460 loader_instance_heap_free(inst, inst->phys_devs_term);
Maciej Jesionowski7c55ba42016-09-30 15:14:52 +02004461 inst->phys_devs_term = NULL;
Mark Young559d7502016-09-26 11:38:46 -06004462 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004463
Slawomir Cyganeb610df2016-10-05 18:38:02 +02004464 if (inst->total_gpu_count > 0) {
Mark Young559d7502016-09-26 11:38:46 -06004465 inst->phys_devs_term = loader_instance_heap_alloc(
Slawomir Cyganeb610df2016-10-05 18:38:02 +02004466 inst, sizeof(struct loader_physical_device) * inst->total_gpu_count,
Mark Young559d7502016-09-26 11:38:46 -06004467 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4468 if (!inst->phys_devs_term) {
4469 return VK_ERROR_OUT_OF_HOST_MEMORY;
4470 }
4471 }
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004472
Slawomir Cyganeb610df2016-10-05 18:38:02 +02004473 for (i = 0; idx < inst->total_gpu_count && i < inst->total_icd_count; i++) {
4474 for (j = 0; j < phys_devs[i].count && idx < inst->total_gpu_count; j++) {
Mark Young559d7502016-09-26 11:38:46 -06004475 loader_set_dispatch((void *)&inst->phys_devs_term[idx],
4476 inst->disp);
4477 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
Jamie Madilld29fc092016-10-24 16:54:40 -04004478 inst->phys_devs_term[idx].icd_index = (uint8_t)(i);
Mark Young559d7502016-09-26 11:38:46 -06004479 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
Slawomir Cyganeb610df2016-10-05 18:38:02 +02004480 if (idx < copy_count) {
4481 pPhysicalDevices[idx] =
4482 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4483 }
Mark Young559d7502016-09-26 11:38:46 -06004484 idx++;
4485 }
4486 }
4487
4488 if (copy_count < inst->total_gpu_count) {
Mark Young559d7502016-09-26 11:38:46 -06004489 res = VK_INCOMPLETE;
Jon Ashburn014438f2016-03-01 19:51:07 -07004490 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004491 }
Mark Young559d7502016-09-26 11:38:46 -06004492
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004493 *pPhysicalDeviceCount = copy_count;
4494
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004495 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004496}
4497
Jon Ashburn1530c342016-02-26 13:14:27 -07004498VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4499 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004500 struct loader_physical_device *phys_dev =
4501 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004502 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004503
Tony Barbour59a47322015-06-24 16:06:58 -06004504 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004505 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004506}
4507
Jon Ashburn1530c342016-02-26 13:14:27 -07004508VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004509 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4510 VkQueueFamilyProperties *pProperties) {
4511 struct loader_physical_device *phys_dev =
4512 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004513 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004514
Cody Northropd0802882015-08-03 17:04:53 -06004515 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004516 icd->GetPhysicalDeviceQueueFamilyProperties(
4517 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004518}
4519
Jon Ashburn1530c342016-02-26 13:14:27 -07004520VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004521 VkPhysicalDevice physicalDevice,
4522 VkPhysicalDeviceMemoryProperties *pProperties) {
4523 struct loader_physical_device *phys_dev =
4524 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004525 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004526
4527 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004528 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004529}
4530
Jon Ashburn23d36b12016-02-02 17:47:28 -07004531VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004532terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4533 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004534 struct loader_physical_device *phys_dev =
4535 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004536 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004537
4538 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004539 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004540}
4541
Jon Ashburn23d36b12016-02-02 17:47:28 -07004542VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004543terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4544 VkFormat format,
4545 VkFormatProperties *pFormatInfo) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004546 struct loader_physical_device *phys_dev =
4547 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004548 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004549
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -06004550 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004551 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4552 pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004553}
4554
Jon Ashburn1530c342016-02-26 13:14:27 -07004555VKAPI_ATTR VkResult VKAPI_CALL
4556terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004557 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4558 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4559 VkImageFormatProperties *pImageFormatProperties) {
4560 struct loader_physical_device *phys_dev =
4561 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004562 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn754864f2015-07-23 18:49:07 -06004563
Chia-I Wu17241042015-10-31 00:31:16 +08004564 if (!icd->GetPhysicalDeviceImageFormatProperties)
4565 return VK_ERROR_INITIALIZATION_FAILED;
4566
Jon Ashburn23d36b12016-02-02 17:47:28 -07004567 return icd->GetPhysicalDeviceImageFormatProperties(
4568 phys_dev->phys_dev, format, type, tiling, usage, flags,
4569 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004570}
4571
Jon Ashburn1530c342016-02-26 13:14:27 -07004572VKAPI_ATTR void VKAPI_CALL
4573terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004574 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4575 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4576 VkImageTiling tiling, uint32_t *pNumProperties,
4577 VkSparseImageFormatProperties *pProperties) {
4578 struct loader_physical_device *phys_dev =
4579 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004580 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004581
4582 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004583 icd->GetPhysicalDeviceSparseImageFormatProperties(
4584 phys_dev->phys_dev, format, type, samples, usage, tiling,
4585 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004586}
4587
Jon Ashburn1530c342016-02-26 13:14:27 -07004588VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4589 VkPhysicalDevice physicalDevice, const char *pLayerName,
4590 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburna760a512015-12-14 08:52:14 -07004591 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004592
Mark Young3a587792016-08-19 15:25:08 -06004593 struct loader_layer_list implicit_layer_list = {0};
4594 struct loader_extension_list all_exts = {0};
4595 struct loader_extension_list icd_exts = {0};
Jon Ashburn471f44c2016-01-13 12:51:43 -07004596
Jon Ashburndc5d9202016-02-29 13:00:51 -07004597 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004598
Jon Ashburndc5d9202016-02-29 13:00:51 -07004599 /* Any layer or trampoline wrapping should be removed at this point in time
4600 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn014438f2016-03-01 19:51:07 -07004601 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004602
Jon Ashburndc5d9202016-02-29 13:00:51 -07004603 /* this case is during the call down the instance chain with pLayerName
4604 * == NULL*/
4605 struct loader_icd *icd = phys_dev->this_icd;
4606 uint32_t icd_ext_count = *pPropertyCount;
4607 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004608
Jon Ashburndc5d9202016-02-29 13:00:51 -07004609 /* get device extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004610 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4611 &icd_ext_count, pProperties);
Mark Young3a587792016-08-19 15:25:08 -06004612 if (res != VK_SUCCESS) {
4613 goto out;
4614 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004615
Mark Young3a587792016-08-19 15:25:08 -06004616 if (!loader_init_layer_list(icd->this_instance, &implicit_layer_list)) {
4617 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4618 goto out;
4619 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004620
Jon Ashburndc5d9202016-02-29 13:00:51 -07004621 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004622 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4623 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004624 /* we need to determine which implicit layers are active,
4625 * and then add their extensions. This can't be cached as
4626 * it depends on results of environment variables (which can change).
4627 */
4628 if (pProperties != NULL) {
4629 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn014438f2016-03-01 19:51:07 -07004630 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4631 icd_ext_count, pProperties,
4632 &icd_exts);
Mark Young3a587792016-08-19 15:25:08 -06004633 if (res != VK_SUCCESS) {
4634 goto out;
4635 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004636
Jon Ashburndc5d9202016-02-29 13:00:51 -07004637 /* we need to determine which implicit layers are active,
4638 * and then add their extensions. This can't be cached as
4639 * it depends on results of environment variables (which can
4640 * change).
4641 */
Mark Young3a587792016-08-19 15:25:08 -06004642 res = loader_add_to_ext_list(icd->this_instance, &all_exts,
4643 icd_exts.count, icd_exts.list);
4644 if (res != VK_SUCCESS) {
4645 goto out;
4646 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004647
Jon Ashburndc5d9202016-02-29 13:00:51 -07004648 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004649 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4650 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004651
Jon Ashburndc5d9202016-02-29 13:00:51 -07004652 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4653 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004654 j < implicit_layer_list.list[i].device_extension_list.count;
4655 j++) {
Mark Young3a587792016-08-19 15:25:08 -06004656 res = loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4657 &implicit_layer_list.list[i]
4658 .device_extension_list.list[j]
4659 .props);
4660 if (res != VK_SUCCESS) {
4661 goto out;
4662 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004663 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004664 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004665 uint32_t capacity = *pPropertyCount;
4666 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004667
Jon Ashburndc5d9202016-02-29 13:00:51 -07004668 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4669 props[i] = all_exts.list[i];
4670 }
4671 /* wasn't enough space for the extensions, we did partial copy now
4672 * return VK_INCOMPLETE */
4673 if (capacity < all_exts.count) {
4674 res = VK_INCOMPLETE;
4675 } else {
4676 *pPropertyCount = all_exts.count;
4677 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004678 } else {
4679 /* just return the count; need to add in the count of implicit layer
4680 * extensions
4681 * don't worry about duplicates being added in the count */
4682 *pPropertyCount = icd_ext_count;
4683
4684 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4685 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004686 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004687 }
4688 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004689 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004690
Mark Young3a587792016-08-19 15:25:08 -06004691out:
4692
4693 if (NULL != implicit_layer_list.list) {
4694 loader_destroy_generic_list(
4695 icd->this_instance,
4696 (struct loader_generic_list *)&implicit_layer_list);
4697 }
4698 if (NULL != all_exts.list) {
4699 loader_destroy_generic_list(icd->this_instance,
4700 (struct loader_generic_list *)&all_exts);
4701 }
4702 if (NULL != icd_exts.list) {
4703 loader_destroy_generic_list(icd->this_instance,
4704 (struct loader_generic_list *)&icd_exts);
4705 }
4706
Jon Ashburndc5d9202016-02-29 13:00:51 -07004707 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004708}
4709
Jon Ashburn23d36b12016-02-02 17:47:28 -07004710VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004711terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4712 uint32_t *pPropertyCount,
4713 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004714
Jon Ashburndc5d9202016-02-29 13:00:51 -07004715 // should never get here this call isn't dispatched down the chain
4716 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004717}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004718
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004719VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004720 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004721 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004722 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004723
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004724 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004725 if (utf8[i] == 0) {
4726 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004727 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004728 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004729 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004730 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004731 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004732 num_char_bytes = 2;
4733 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4734 num_char_bytes = 3;
4735 } else {
4736 result = VK_STRING_ERROR_BAD_DATA;
4737 }
4738
4739 // Validate the following num_char_bytes of data
4740 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4741 if (++i == max_length) {
4742 result |= VK_STRING_ERROR_LENGTH;
4743 break;
4744 }
4745 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4746 result |= VK_STRING_ERROR_BAD_DATA;
4747 }
4748 }
4749 }
4750 return result;
4751}