blob: d0cdc00baf3c810b6a0ec19ae69ce4ef9a1d8075 [file] [log] [blame]
Mark Young0ad83132016-06-30 13:02:42 -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"
David Pinedo9316d3b2015-11-06 12:54:48 -070045#include "vulkan/vk_icd.h"
Jon Ashburn2077e382015-06-29 11:25:34 -060046#include "cJSON.h"
Jon Ashburnfc1031e2015-11-17 15:31:02 -070047#include "murmurhash.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080048
Jon Ashburnd8ed7992016-04-04 13:52:53 -060049#if defined(__GNUC__)
Jon Ashburn10b3f832016-05-09 11:31:40 -060050#if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 17))
Jon Ashburncc407a22016-04-15 09:25:03 -060051#define secure_getenv __secure_getenv
52#endif
Jon Ashburnd8ed7992016-04-04 13:52:53 -060053#endif
54
Jon Ashburn27cd5842015-05-12 17:26:48 -060055struct loader_struct loader = {0};
Jon Ashburn87d6aa92015-08-28 15:19:27 -060056// TLS for instance for alloc/free callbacks
57THREAD_LOCAL_DECL struct loader_instance *tls_instance;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080058
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -070059static size_t loader_platform_combine_path(char *dest, size_t len, ...);
Daniel Dadap00b4aba2015-09-30 11:50:51 -050060
Jon Ashburn24cd4be2015-11-01 14:04:06 -070061struct loader_phys_dev_per_icd {
62 uint32_t count;
63 VkPhysicalDevice *phys_devs;
Jon Ashburn014438f2016-03-01 19:51:07 -070064 struct loader_icd *this_icd;
Jon Ashburn24cd4be2015-11-01 14:04:06 -070065};
66
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060067enum loader_debug {
Jon Ashburn23d36b12016-02-02 17:47:28 -070068 LOADER_INFO_BIT = 0x01,
69 LOADER_WARN_BIT = 0x02,
70 LOADER_PERF_BIT = 0x04,
71 LOADER_ERROR_BIT = 0x08,
72 LOADER_DEBUG_BIT = 0x10,
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060073};
74
75uint32_t g_loader_debug = 0;
76uint32_t g_loader_log_msgs = 0;
77
Jon Ashburn23d36b12016-02-02 17:47:28 -070078// thread safety lock for accessing global data structures such as "loader"
Jon Ashburn6301a0f2015-05-29 13:15:39 -060079// all entrypoints on the instance chain need to be locked except GPA
Jon Ashburn2077e382015-06-29 11:25:34 -060080// additionally CreateDevice and DestroyDevice needs to be locked
Jon Ashburn6301a0f2015-05-29 13:15:39 -060081loader_platform_thread_mutex loader_lock;
Jon Ashburn6461ef22015-09-22 13:11:00 -060082loader_platform_thread_mutex loader_json_lock;
Jon Ashburn6301a0f2015-05-29 13:15:39 -060083
Jon Ashburn86a527a2016-02-10 20:59:26 -070084const char *std_validation_str = "VK_LAYER_LUNARG_standard_validation";
85
Ian Elliottd3ef02f2015-07-06 14:36:13 -060086// This table contains the loader's instance dispatch table, which contains
87// default functions if no instance layers are activated. This contains
88// pointers to "terminator functions".
Jon Ashburn6301a0f2015-05-29 13:15:39 -060089const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn69e9ea22015-09-28 16:15:00 -060090 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn1530c342016-02-26 13:14:27 -070091 .DestroyInstance = terminator_DestroyInstance,
92 .EnumeratePhysicalDevices = terminator_EnumeratePhysicalDevices,
93 .GetPhysicalDeviceFeatures = terminator_GetPhysicalDeviceFeatures,
Jon Ashburn23d36b12016-02-02 17:47:28 -070094 .GetPhysicalDeviceFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -070095 terminator_GetPhysicalDeviceFormatProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -070096 .GetPhysicalDeviceImageFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -070097 terminator_GetPhysicalDeviceImageFormatProperties,
98 .GetPhysicalDeviceProperties = terminator_GetPhysicalDeviceProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -070099 .GetPhysicalDeviceQueueFamilyProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700100 terminator_GetPhysicalDeviceQueueFamilyProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700101 .GetPhysicalDeviceMemoryProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700102 terminator_GetPhysicalDeviceMemoryProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700103 .EnumerateDeviceExtensionProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700104 terminator_EnumerateDeviceExtensionProperties,
105 .EnumerateDeviceLayerProperties = terminator_EnumerateDeviceLayerProperties,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700106 .GetPhysicalDeviceSparseImageFormatProperties =
Jon Ashburn1530c342016-02-26 13:14:27 -0700107 terminator_GetPhysicalDeviceSparseImageFormatProperties,
108 .DestroySurfaceKHR = terminator_DestroySurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700109 .GetPhysicalDeviceSurfaceSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700110 terminator_GetPhysicalDeviceSurfaceSupportKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700111 .GetPhysicalDeviceSurfaceCapabilitiesKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700112 terminator_GetPhysicalDeviceSurfaceCapabilitiesKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700113 .GetPhysicalDeviceSurfaceFormatsKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700114 terminator_GetPhysicalDeviceSurfaceFormatsKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700115 .GetPhysicalDeviceSurfacePresentModesKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700116 terminator_GetPhysicalDeviceSurfacePresentModesKHR,
117 .CreateDebugReportCallbackEXT = terminator_CreateDebugReportCallback,
118 .DestroyDebugReportCallbackEXT = terminator_DestroyDebugReportCallback,
119 .DebugReportMessageEXT = terminator_DebugReportMessage,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700120#ifdef VK_USE_PLATFORM_MIR_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700121 .CreateMirSurfaceKHR = terminator_CreateMirSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700122 .GetPhysicalDeviceMirPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700123 terminator_GetPhysicalDeviceMirPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700124#endif
125#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700126 .CreateWaylandSurfaceKHR = terminator_CreateWaylandSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700127 .GetPhysicalDeviceWaylandPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700128 terminator_GetPhysicalDeviceWaylandPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700129#endif
130#ifdef VK_USE_PLATFORM_WIN32_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700131 .CreateWin32SurfaceKHR = terminator_CreateWin32SurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700132 .GetPhysicalDeviceWin32PresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700133 terminator_GetPhysicalDeviceWin32PresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700134#endif
135#ifdef VK_USE_PLATFORM_XCB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700136 .CreateXcbSurfaceKHR = terminator_CreateXcbSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700137 .GetPhysicalDeviceXcbPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700138 terminator_GetPhysicalDeviceXcbPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700139#endif
140#ifdef VK_USE_PLATFORM_XLIB_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700141 .CreateXlibSurfaceKHR = terminator_CreateXlibSurfaceKHR,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700142 .GetPhysicalDeviceXlibPresentationSupportKHR =
Jon Ashburn1530c342016-02-26 13:14:27 -0700143 terminator_GetPhysicalDeviceXlibPresentationSupportKHR,
Ian Elliottdb4300a2015-11-23 10:17:23 -0700144#endif
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700145#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jon Ashburn1530c342016-02-26 13:14:27 -0700146 .CreateAndroidSurfaceKHR = terminator_CreateAndroidSurfaceKHR,
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700147#endif
Jon Ashburnc7d3e732016-03-08 09:30:30 -0700148 .GetPhysicalDeviceDisplayPropertiesKHR =
149 terminator_GetPhysicalDeviceDisplayPropertiesKHR,
150 .GetPhysicalDeviceDisplayPlanePropertiesKHR =
151 terminator_GetPhysicalDeviceDisplayPlanePropertiesKHR,
152 .GetDisplayPlaneSupportedDisplaysKHR =
153 terminator_GetDisplayPlaneSupportedDisplaysKHR,
Jon Ashburncc407a22016-04-15 09:25:03 -0600154 .GetDisplayModePropertiesKHR = terminator_GetDisplayModePropertiesKHR,
155 .CreateDisplayModeKHR = terminator_CreateDisplayModeKHR,
156 .GetDisplayPlaneCapabilitiesKHR = terminator_GetDisplayPlaneCapabilitiesKHR,
157 .CreateDisplayPlaneSurfaceKHR = terminator_CreateDisplayPlaneSurfaceKHR,
Jon Ashburn27cd5842015-05-12 17:26:48 -0600158};
159
Jon Ashburn8810c5f2015-08-18 18:04:47 -0600160LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700161
Mark Young0ad83132016-06-30 13:02:42 -0600162void *loader_instance_heap_alloc(const struct loader_instance *instance,
163 size_t size,
164 VkSystemAllocationScope alloc_scope) {
165 void *pMemory = NULL;
166#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
167 {
168#else
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800169 if (instance && instance->alloc_callbacks.pfnAllocation) {
Mark Young0ad83132016-06-30 13:02:42 -0600170 /* These are internal structures, so it's best to align everything to
171 * the largest unit size which is the size of a uint64_t.
172 */
173 pMemory = instance->alloc_callbacks.pfnAllocation(
174 instance->alloc_callbacks.pUserData, size, sizeof(uint64_t),
Jon Ashburn23d36b12016-02-02 17:47:28 -0700175 alloc_scope);
Mark Young0ad83132016-06-30 13:02:42 -0600176 } else {
177#endif
178 pMemory = malloc(size);
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600179 }
Mark Young0ad83132016-06-30 13:02:42 -0600180 return pMemory;
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -0600181}
182
Mark Young0ad83132016-06-30 13:02:42 -0600183void loader_instance_heap_free(const struct loader_instance *instance,
184 void *pMemory) {
185 if (pMemory != NULL) {
186#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
187 {
188#else
189 if (instance && instance->alloc_callbacks.pfnFree) {
190 instance->alloc_callbacks.pfnFree(
191 instance->alloc_callbacks.pUserData, pMemory);
192 } else {
193#endif
194 free(pMemory);
Mark Youngd077f992016-06-30 11:03:59 -0600195 }
Mark Young4b0b9222016-06-29 18:33:53 -0600196 }
Mark Young4b0b9222016-06-29 18:33:53 -0600197}
198
Mark Young0ad83132016-06-30 13:02:42 -0600199void *loader_instance_heap_realloc(const struct loader_instance *instance,
200 void *pMemory, size_t orig_size, size_t size,
201 VkSystemAllocationScope alloc_scope) {
202 void *pNewMem = NULL;
203 if (pMemory == NULL || orig_size == 0) {
204 pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
205 } else if (size == 0) {
206 loader_instance_heap_free(instance, pMemory);
207#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
208#else
209 } else if (instance && instance->alloc_callbacks.pfnReallocation) {
210 /* These are internal structures, so it's best to align everything to
211 * the largest unit size which is the size of a uint64_t.
212 */
213 pNewMem = instance->alloc_callbacks.pfnReallocation(
214 instance->alloc_callbacks.pUserData, pMemory, size,
215 sizeof(uint64_t), alloc_scope);
216#endif
217 } else {
218 pNewMem = realloc(pMemory, size);
219 }
220 return pNewMem;
Mark Young4b0b9222016-06-29 18:33:53 -0600221}
222
Mark Young0ad83132016-06-30 13:02:42 -0600223void *loader_instance_tls_heap_alloc(size_t size) {
224 return loader_instance_heap_alloc(tls_instance, size,
225 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young4b0b9222016-06-29 18:33:53 -0600226}
Mark Young4b0b9222016-06-29 18:33:53 -0600227
Mark Young0ad83132016-06-30 13:02:42 -0600228void loader_instance_tls_heap_free(void *pMemory) {
229 loader_instance_heap_free(tls_instance, pMemory);
230}
231
232void *loader_device_heap_alloc(const struct loader_device *device, size_t size,
233 VkSystemAllocationScope alloc_scope) {
234 void *pMemory = NULL;
235#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
236 {
237#else
238 if (device && device->alloc_callbacks.pfnAllocation) {
239 /* These are internal structures, so it's best to align everything to
240 * the largest unit size which is the size of a uint64_t.
241 */
242 pMemory = device->alloc_callbacks.pfnAllocation(
243 device->alloc_callbacks.pUserData, size, sizeof(uint64_t),
244 alloc_scope);
245 } else {
246#endif
247 pMemory = malloc(size);
248 }
249 return pMemory;
250}
251
252void loader_device_heap_free(const struct loader_device *device,
253 void *pMemory) {
254 if (pMemory != NULL) {
255#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
256 {
257#else
258 if (device && device->alloc_callbacks.pfnFree) {
259 device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData,
260 pMemory);
261 } else {
262#endif
263 free(pMemory);
264 }
265 }
266}
267
268void *loader_device_heap_realloc(const struct loader_device *device,
269 void *pMemory, size_t orig_size, size_t size,
270 VkSystemAllocationScope alloc_scope) {
271 void *pNewMem = NULL;
272 if (pMemory == NULL || orig_size == 0) {
273 pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
274 } else if (size == 0) {
275 loader_device_heap_free(device, pMemory);
276#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
277#else
278 } else if (device && device->alloc_callbacks.pfnReallocation) {
279 /* These are internal structures, so it's best to align everything to
280 * the largest unit size which is the size of a uint64_t.
281 */
282 pNewMem = device->alloc_callbacks.pfnReallocation(
283 device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
284 alloc_scope);
285#endif
286 } else {
287 pNewMem = realloc(pMemory, size);
288 }
289 return pNewMem;
290}
291
292// Environment variables
293#if defined(__linux__)
294
295static inline char *loader_getenv(const char *name,
296 const struct loader_instance *inst) {
297 // No allocation of memory necessary for Linux, but we should at least touch
298 // the inst pointer to get rid of compiler warnings.
299 (void)inst;
300 return getenv(name);
301}
302static inline void loader_free_getenv(const char *val,
303 const struct loader_instance *inst) {
304 // No freeing of memory necessary for Linux, but we should at least touch
305 // the val and inst pointers to get rid of compiler warnings.
306 (void)val;
307 (void)inst;
308}
309
310#elif defined(WIN32)
311
312static inline char *loader_getenv(const char *name,
313 const struct loader_instance *inst) {
314 char *retVal;
315 DWORD valSize;
316
317 valSize = GetEnvironmentVariableA(name, NULL, 0);
318
319 // valSize DOES include the null terminator, so for any set variable
320 // will always be at least 1. If it's 0, the variable wasn't set.
321 if (valSize == 0)
322 return NULL;
323
324 // Allocate the space necessary for the registry entry
325 if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
326 retVal = (char *)inst->alloc_callbacks.pfnAllocation(
327 inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
328 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
329 } else {
330 retVal = (char *)malloc(valSize);
331 }
332
333 if (NULL != retVal) {
334 GetEnvironmentVariableA(name, retVal, valSize);
335 }
336
337 return retVal;
338}
339
340static inline void loader_free_getenv(char *val,
341 const struct loader_instance *inst) {
342 if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
343 inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
344 } else {
345 free((void *)val);
346 }
347}
348
349#else
350
351static inline char *loader_getenv(const char *name,
352 const struct loader_instance *inst) {
353 // stub func
354 (void)inst;
355 (void)name;
356 return NULL;
357}
358static inline void loader_free_getenv(const char *val,
359 const struct loader_instance *inst) {
360 // stub func
361 (void)val;
362 (void)inst;
363}
364
365#endif
366
Jon Ashburn2e37d752016-02-12 08:20:06 -0700367void loader_log(const struct loader_instance *inst, VkFlags msg_type,
Jon Ashburn1530c342016-02-26 13:14:27 -0700368 int32_t msg_code, const char *format, ...) {
Jon Ashburn86723b02015-07-31 15:47:59 -0600369 char msg[512];
Jon Ashburnffad94d2015-06-30 14:46:22 -0700370 va_list ap;
371 int ret;
372
Jon Ashburnffad94d2015-06-30 14:46:22 -0700373 va_start(ap, format);
374 ret = vsnprintf(msg, sizeof(msg), format, ap);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700375 if ((ret >= (int)sizeof(msg)) || ret < 0) {
376 msg[sizeof(msg) - 1] = '\0';
Jon Ashburnffad94d2015-06-30 14:46:22 -0700377 }
378 va_end(ap);
379
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700380 if (inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700381 util_DebugReportMessage(inst, msg_type,
382 VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
383 (uint64_t)inst, 0, msg_code, "loader", msg);
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -0700384 }
385
386 if (!(msg_type & g_loader_log_msgs)) {
387 return;
388 }
389
Ian Elliott4470a302015-02-17 10:33:47 -0700390#if defined(WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -0700391 OutputDebugString(msg);
mschottb9cdb782015-07-22 14:11:29 +0200392 OutputDebugString("\n");
Jon Ashburnffad94d2015-06-30 14:46:22 -0700393#endif
394 fputs(msg, stderr);
395 fputc('\n', stderr);
396}
397
Jon Ashburncc407a22016-04-15 09:25:03 -0600398VKAPI_ATTR VkResult VKAPI_CALL
399vkSetInstanceDispatch(VkInstance instance, void *object) {
Jon Ashburnc3c58772016-03-29 11:16:01 -0600400
401 struct loader_instance *inst = loader_get_instance(instance);
402 if (!inst) {
403 return VK_ERROR_INITIALIZATION_FAILED;
404 }
405 loader_set_dispatch(object, inst->disp);
406 return VK_SUCCESS;
407}
408
Jon Ashburncc407a22016-04-15 09:25:03 -0600409VKAPI_ATTR VkResult VKAPI_CALL
410vkSetDeviceDispatch(VkDevice device, void *object) {
Jon Ashburned8f2312016-03-31 10:52:22 -0600411 struct loader_device *dev;
412 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
413
414 if (!icd) {
415 return VK_ERROR_INITIALIZATION_FAILED;
416 }
417 loader_set_dispatch(object, &dev->loader_dispatch);
418 return VK_SUCCESS;
419}
420
Jon Ashburnffad94d2015-06-30 14:46:22 -0700421#if defined(WIN32)
Tony Barbourea968902015-07-29 14:26:21 -0600422static char *loader_get_next_path(char *path);
Jon Ashburnffad94d2015-06-30 14:46:22 -0700423/**
424* Find the list of registry files (names within a key) in key "location".
425*
Jon Ashburn23d36b12016-02-02 17:47:28 -0700426* This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
427*given in "location"
428* for a list or name/values which are added to a returned list (function return
429*value).
Jon Ashburnffad94d2015-06-30 14:46:22 -0700430* The DWORD values within the key must be 0 or they are skipped.
Jon Ashburne39a4f82015-08-28 13:38:21 -0600431* Function return is a string with a ';' separated list of filenames.
Jon Ashburnffad94d2015-06-30 14:46:22 -0700432* Function return is NULL if no valid name/value pairs are found in the key,
433* or the key is not found.
434*
435* \returns
436* A string list of filenames as pointer.
437* When done using the returned string list, pointer should be freed.
438*/
Jon Ashburn23d36b12016-02-02 17:47:28 -0700439static char *loader_get_registry_files(const struct loader_instance *inst,
440 char *location) {
Jon Ashburnffad94d2015-06-30 14:46:22 -0700441 LONG rtn_value;
442 HKEY hive, key;
Piers Daniell524ec732015-11-05 16:58:26 -0700443 DWORD access_flags;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700444 char name[2048];
445 char *out = NULL;
Tony Barbourea968902015-07-29 14:26:21 -0600446 char *loc = location;
447 char *next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700448 DWORD idx = 0;
449 DWORD name_size = sizeof(name);
450 DWORD value;
451 DWORD total_size = 4096;
452 DWORD value_size = sizeof(value);
Tony Barbourea968902015-07-29 14:26:21 -0600453
Jon Ashburn23d36b12016-02-02 17:47:28 -0700454 while (*loc) {
Tony Barbourea968902015-07-29 14:26:21 -0600455 next = loader_get_next_path(loc);
456 hive = DEFAULT_VK_REGISTRY_HIVE;
Piers Daniell524ec732015-11-05 16:58:26 -0700457 access_flags = KEY_QUERY_VALUE;
Tony Barbourea968902015-07-29 14:26:21 -0600458 rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
459 if (rtn_value != ERROR_SUCCESS) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700460 // We still couldn't find the key, so give up:
461 loc = next;
462 continue;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700463 }
Tony Barbourea968902015-07-29 14:26:21 -0600464
Jon Ashburn23d36b12016-02-02 17:47:28 -0700465 while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL,
466 NULL, (LPBYTE)&value, &value_size)) ==
467 ERROR_SUCCESS) {
Tony Barbourea968902015-07-29 14:26:21 -0600468 if (value_size == sizeof(value) && value == 0) {
469 if (out == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -0600470 out = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700471 inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600472 if (NULL == out) {
473 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
474 "Out of memory can't alloc space for registry data");
475 return NULL;
476 }
Tony Barbourea968902015-07-29 14:26:21 -0600477 out[0] = '\0';
Jon Ashburn23d36b12016-02-02 17:47:28 -0700478 } else if (strlen(out) + name_size + 1 > total_size) {
Mark Young0ad83132016-06-30 13:02:42 -0600479 out = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700480 inst, out, total_size, total_size * 2,
481 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600482 if (NULL == out) {
483 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
484 "Out of memory can't realloc space for registry data");
485 return NULL;
486 }
Tony Barbourea968902015-07-29 14:26:21 -0600487 total_size *= 2;
488 }
Tony Barbourea968902015-07-29 14:26:21 -0600489 if (strlen(out) == 0)
Jon Ashburn23d36b12016-02-02 17:47:28 -0700490 snprintf(out, name_size + 1, "%s", name);
Tony Barbourea968902015-07-29 14:26:21 -0600491 else
Jon Ashburn23d36b12016-02-02 17:47:28 -0700492 snprintf(out + strlen(out), name_size + 2, "%c%s",
493 PATH_SEPERATOR, name);
Tony Barbourea968902015-07-29 14:26:21 -0600494 }
495 name_size = 2048;
496 }
497 loc = next;
Jon Ashburnffad94d2015-06-30 14:46:22 -0700498 }
Tony Barbourea968902015-07-29 14:26:21 -0600499
Jon Ashburnffad94d2015-06-30 14:46:22 -0700500 return out;
501}
502
Ian Elliott4470a302015-02-17 10:33:47 -0700503#endif // WIN32
504
Jon Ashburnc7237a72015-08-03 09:08:46 -0600505/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500506 * Combine path elements, separating each element with the platform-specific
507 * directory separator, and save the combined string to a destination buffer,
508 * not exceeding the given length. Path elements are given as variadic args,
509 * with a NULL element terminating the list.
510 *
511 * \returns the total length of the combined string, not including an ASCII
512 * NUL termination character. This length may exceed the available storage:
513 * in this case, the written string will be truncated to avoid a buffer
514 * overrun, and the return value will greater than or equal to the storage
515 * size. A NULL argument may be provided as the destination buffer in order
516 * to determine the required string length without actually writing a string.
517 */
518
Jon Ashburn23d36b12016-02-02 17:47:28 -0700519static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
Courtney Goeltzenleuchter0ef13a02015-12-16 16:19:46 -0700520 size_t required_len = 0;
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500521 va_list ap;
522 const char *component;
523
524 va_start(ap, len);
525
Jon Ashburn23d36b12016-02-02 17:47:28 -0700526 while ((component = va_arg(ap, const char *))) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500527 if (required_len > 0) {
528 // This path element is not the first non-empty element; prepend
529 // a directory separator if space allows
530 if (dest && required_len + 1 < len) {
531 snprintf(dest + required_len, len - required_len, "%c",
532 DIRECTORY_SYMBOL);
533 }
534 required_len++;
535 }
536
537 if (dest && required_len < len) {
538 strncpy(dest + required_len, component, len - required_len);
539 }
540 required_len += strlen(component);
541 }
542
543 va_end(ap);
544
545 // strncpy(3) won't add a NUL terminating byte in the event of truncation.
546 if (dest && required_len >= len) {
547 dest[len - 1] = '\0';
548 }
549
550 return required_len;
551}
552
Daniel Dadap00b4aba2015-09-30 11:50:51 -0500553/**
Jon Ashburnc7237a72015-08-03 09:08:46 -0600554 * Given string of three part form "maj.min.pat" convert to a vulkan version
555 * number.
556 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700557static uint32_t loader_make_version(const char *vers_str) {
558 uint32_t vers = 0, major = 0, minor = 0, patch = 0;
559 char *minor_str = NULL;
Jon Ashburnc7237a72015-08-03 09:08:46 -0600560 char *patch_str = NULL;
561 char *cstr;
562 char *str;
563
564 if (!vers_str)
565 return vers;
566 cstr = loader_stack_alloc(strlen(vers_str) + 1);
567 strcpy(cstr, vers_str);
568 while ((str = strchr(cstr, '.')) != NULL) {
569 if (minor_str == NULL) {
570 minor_str = str + 1;
571 *str = '\0';
572 major = atoi(cstr);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700573 } else if (patch_str == NULL) {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600574 patch_str = str + 1;
575 *str = '\0';
576 minor = atoi(minor_str);
Jon Ashburn23d36b12016-02-02 17:47:28 -0700577 } else {
Jon Ashburnc7237a72015-08-03 09:08:46 -0600578 return vers;
579 }
580 cstr = str + 1;
581 }
582 patch = atoi(patch_str);
583
584 return VK_MAKE_VERSION(major, minor, patch);
Jon Ashburnc7237a72015-08-03 09:08:46 -0600585}
586
Jon Ashburn23d36b12016-02-02 17:47:28 -0700587bool compare_vk_extension_properties(const VkExtensionProperties *op1,
588 const VkExtensionProperties *op2) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800589 return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600590}
591
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600592/**
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600593 * Search the given ext_array for an extension
594 * matching the given vk_ext_prop
595 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700596bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
597 const uint32_t count,
598 const VkExtensionProperties *ext_array) {
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600599 for (uint32_t i = 0; i < count; i++) {
600 if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i]))
601 return true;
602 }
603 return false;
604}
605
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600606/**
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600607 * Search the given ext_list for an extension
608 * matching the given vk_ext_prop
609 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700610bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop,
611 const struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600612 for (uint32_t i = 0; i < ext_list->count; i++) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600613 if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop))
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600614 return true;
615 }
616 return false;
617}
618
Jon Ashburnb8726962016-04-08 15:03:35 -0600619/**
620 * Search the given ext_list for a device extension matching the given ext_prop
621 */
Jon Ashburncc407a22016-04-15 09:25:03 -0600622bool has_vk_dev_ext_property(
623 const VkExtensionProperties *ext_prop,
624 const struct loader_device_extension_list *ext_list) {
Jon Ashburnb8726962016-04-08 15:03:35 -0600625 for (uint32_t i = 0; i < ext_list->count; i++) {
626 if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop))
627 return true;
628 }
629 return false;
630}
631
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600632/*
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -0600633 * Search the given layer list for a layer matching the given layer name
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600634 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700635static struct loader_layer_properties *
636loader_get_layer_property(const char *name,
637 const struct loader_layer_list *layer_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600638 for (uint32_t i = 0; i < layer_list->count; i++) {
639 const VkLayerProperties *item = &layer_list->list[i].info;
640 if (strcmp(name, item->layerName) == 0)
641 return &layer_list->list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600642 }
643 return NULL;
644}
645
Jon Ashburne13ecc92015-08-03 17:19:30 -0600646/**
647 * Get the next unused layer property in the list. Init the property to zero.
648 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700649static struct loader_layer_properties *
650loader_get_next_layer_property(const struct loader_instance *inst,
651 struct loader_layer_list *layer_list) {
Jon Ashburne13ecc92015-08-03 17:19:30 -0600652 if (layer_list->capacity == 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700653 layer_list->list =
Mark Young0ad83132016-06-30 13:02:42 -0600654 loader_instance_heap_alloc(
655 inst, sizeof(struct loader_layer_properties) * 64,
656 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600657 if (layer_list->list == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700658 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
659 "Out of memory can't add any layer properties to list");
Jon Ashburne13ecc92015-08-03 17:19:30 -0600660 return NULL;
661 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700662 memset(layer_list->list, 0,
663 sizeof(struct loader_layer_properties) * 64);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600664 layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
665 }
666
667 // ensure enough room to add an entry
Jon Ashburn23d36b12016-02-02 17:47:28 -0700668 if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
669 layer_list->capacity) {
Mark Young0ad83132016-06-30 13:02:42 -0600670 layer_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700671 inst, layer_list->list, layer_list->capacity,
672 layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600673 if (layer_list->list == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700674 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700675 "realloc failed for layer list");
Mark Young0ad83132016-06-30 13:02:42 -0600676 return NULL;
Jon Ashburne13ecc92015-08-03 17:19:30 -0600677 }
678 layer_list->capacity *= 2;
679 }
680
681 layer_list->count++;
682 return &(layer_list->list[layer_list->count - 1]);
683}
684
685/**
686 * Remove all layer properties entrys from the list
687 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700688void loader_delete_layer_properties(const struct loader_instance *inst,
689 struct loader_layer_list *layer_list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700690 uint32_t i, j;
691 struct loader_device_extension_list *dev_ext_list;
Jon Ashburnb82c1852015-08-11 14:49:54 -0600692 if (!layer_list)
693 return;
694
Jon Ashburne13ecc92015-08-03 17:19:30 -0600695 for (i = 0; i < layer_list->count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700696 loader_destroy_generic_list(
697 inst, (struct loader_generic_list *)&layer_list->list[i]
698 .instance_extension_list);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700699 dev_ext_list = &layer_list->list[i].device_extension_list;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700700 if (dev_ext_list->capacity > 0 &&
Mark Young0ad83132016-06-30 13:02:42 -0600701 NULL != dev_ext_list->list &&
Jon Ashburn23d36b12016-02-02 17:47:28 -0700702 dev_ext_list->list->entrypoint_count > 0) {
703 for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
Mark Young0ad83132016-06-30 13:02:42 -0600704 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700705 }
Mark Young0ad83132016-06-30 13:02:42 -0600706 loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700707 }
Jon Ashburn23d36b12016-02-02 17:47:28 -0700708 loader_destroy_generic_list(inst,
709 (struct loader_generic_list *)dev_ext_list);
Jon Ashburne13ecc92015-08-03 17:19:30 -0600710 }
711 layer_list->count = 0;
712
Jon Ashburnb82c1852015-08-11 14:49:54 -0600713 if (layer_list->capacity > 0) {
714 layer_list->capacity = 0;
Mark Young0ad83132016-06-30 13:02:42 -0600715 loader_instance_heap_free(inst, layer_list->list);
Jon Ashburnb82c1852015-08-11 14:49:54 -0600716 }
Jon Ashburne13ecc92015-08-03 17:19:30 -0600717}
718
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -0700719static void loader_add_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700720 const struct loader_instance *inst,
721 const PFN_vkEnumerateInstanceExtensionProperties fp_get_props,
722 const char *lib_name, struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -0700723 uint32_t i, count = 0;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600724 VkExtensionProperties *ext_props;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600725 VkResult res;
726
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600727 if (!fp_get_props) {
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600728 /* No EnumerateInstanceExtensionProperties defined */
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600729 return;
730 }
731
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600732 res = fp_get_props(NULL, &count, NULL);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600733 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700734 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700735 "Error getting Instance extension count from %s", lib_name);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600736 return;
737 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600738
Courtney Goeltzenleuchter5c6cf472015-07-06 22:28:18 -0600739 if (count == 0) {
740 /* No ExtensionProperties to report */
741 return;
742 }
743
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600744 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600745
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600746 res = fp_get_props(NULL, &count, ext_props);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600747 if (res != VK_SUCCESS) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700748 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700749 "Error getting Instance extensions from %s", lib_name);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600750 return;
751 }
Tony Barbour59a47322015-06-24 16:06:58 -0600752
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600753 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter53043732015-07-12 13:20:05 -0600754 char spec_version[64];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600755
Jon Ashburncc407a22016-04-15 09:25:03 -0600756 bool ext_unsupported =
757 wsi_unsupported_instance_extension(&ext_props[i]);
Jon Ashburn6fa520f2016-03-25 12:49:35 -0600758 if (!ext_unsupported) {
759 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
760 VK_MAJOR(ext_props[i].specVersion),
761 VK_MINOR(ext_props[i].specVersion),
762 VK_PATCH(ext_props[i].specVersion));
763 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
764 "Instance Extension: %s (%s) version %s",
765 ext_props[i].extensionName, lib_name, spec_version);
766 loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
767 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600768 }
769
770 return;
771}
772
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700773/*
774 * Initialize ext_list with the physical device extensions.
775 * The extension properties are passed as inputs in count and ext_props.
776 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700777static VkResult
778loader_init_device_extensions(const struct loader_instance *inst,
779 struct loader_physical_device *phys_dev,
780 uint32_t count, VkExtensionProperties *ext_props,
781 struct loader_extension_list *ext_list) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700782 VkResult res;
783 uint32_t i;
784
Jon Ashburn23d36b12016-02-02 17:47:28 -0700785 if (!loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700786 sizeof(VkExtensionProperties))) {
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700787 return VK_ERROR_OUT_OF_HOST_MEMORY;
788 }
789
790 for (i = 0; i < count; i++) {
791 char spec_version[64];
792
Jon Ashburn23d36b12016-02-02 17:47:28 -0700793 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
794 VK_MAJOR(ext_props[i].specVersion),
795 VK_MINOR(ext_props[i].specVersion),
796 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700797 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700798 "Device Extension: %s (%s) version %s",
799 ext_props[i].extensionName,
800 phys_dev->this_icd->this_icd_lib->lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700801 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
802 if (res != VK_SUCCESS)
803 return res;
804 }
805
806 return VK_SUCCESS;
807}
808
Jon Ashburn1530c342016-02-26 13:14:27 -0700809VkResult loader_add_device_extensions(const struct loader_instance *inst,
Jon Ashburncc407a22016-04-15 09:25:03 -0600810 PFN_vkEnumerateDeviceExtensionProperties
811 fpEnumerateDeviceExtensionProperties,
Jon Ashburn1530c342016-02-26 13:14:27 -0700812 VkPhysicalDevice physical_device,
813 const char *lib_name,
814 struct loader_extension_list *ext_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600815 uint32_t i, count;
816 VkResult res;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600817 VkExtensionProperties *ext_props;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600818
Piers Daniell295fe402016-03-29 11:51:11 -0600819 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count,
820 NULL);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700821 if (res == VK_SUCCESS && count > 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700822 ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700823 if (!ext_props)
824 return VK_ERROR_OUT_OF_HOST_MEMORY;
Piers Daniell295fe402016-03-29 11:51:11 -0600825 res = fpEnumerateDeviceExtensionProperties(physical_device, NULL,
826 &count, ext_props);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700827 if (res != VK_SUCCESS)
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700828 return res;
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700829 for (i = 0; i < count; i++) {
830 char spec_version[64];
831
Jon Ashburn23d36b12016-02-02 17:47:28 -0700832 snprintf(spec_version, sizeof(spec_version), "%d.%d.%d",
833 VK_MAJOR(ext_props[i].specVersion),
834 VK_MINOR(ext_props[i].specVersion),
835 VK_PATCH(ext_props[i].specVersion));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700836 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700837 "Device Extension: %s (%s) version %s",
838 ext_props[i].extensionName, lib_name, spec_version);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700839 res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
840 if (res != VK_SUCCESS)
841 return res;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600842 }
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700843 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700844 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
845 "Error getting physical device extension info count from "
846 "library %s",
847 lib_name);
Jon Ashburn00eb6c02015-11-02 17:40:01 -0700848 return res;
849 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600850
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700851 return VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600852}
853
Jon Ashburn1530c342016-02-26 13:14:27 -0700854bool loader_init_generic_list(const struct loader_instance *inst,
855 struct loader_generic_list *list_info,
856 size_t element_size) {
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700857 list_info->capacity = 32 * element_size;
Mark Young0ad83132016-06-30 13:02:42 -0600858 list_info->list = loader_instance_heap_alloc(
859 inst, list_info->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700860 if (list_info->list == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600861 return false;
862 }
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700863 memset(list_info->list, 0, list_info->capacity);
864 list_info->count = 0;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600865 return true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600866}
867
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700868void loader_destroy_generic_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -0700869 struct loader_generic_list *list) {
Mark Young0ad83132016-06-30 13:02:42 -0600870 loader_instance_heap_free(inst, list->list);
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700871 list->count = 0;
872 list->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600873}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600874
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600875/*
876 * Append non-duplicate extension properties defined in props
877 * to the given ext_list.
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700878 * Return
879 * Vk_SUCCESS on success
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600880 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700881VkResult loader_add_to_ext_list(const struct loader_instance *inst,
882 struct loader_extension_list *ext_list,
883 uint32_t prop_list_count,
884 const VkExtensionProperties *props) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600885 uint32_t i;
886 const VkExtensionProperties *cur_ext;
887
888 if (ext_list->list == NULL || ext_list->capacity == 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700889 loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn6e6a2162015-12-10 08:51:10 -0700890 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600891 }
892
893 if (ext_list->list == NULL)
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700894 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600895
896 for (i = 0; i < prop_list_count; i++) {
897 cur_ext = &props[i];
898
899 // look for duplicates
900 if (has_vk_extension_property(cur_ext, ext_list)) {
901 continue;
902 }
903
904 // add to list at end
905 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700906 if (ext_list->count * sizeof(VkExtensionProperties) >=
907 ext_list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -0600908
Mark Young0ad83132016-06-30 13:02:42 -0600909 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700910 inst, ext_list->list, ext_list->capacity,
911 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700912
913 if (ext_list->list == NULL)
914 return VK_ERROR_OUT_OF_HOST_MEMORY;
915
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600916 // double capacity
917 ext_list->capacity *= 2;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600918 }
919
Jon Ashburn23d36b12016-02-02 17:47:28 -0700920 memcpy(&ext_list->list[ext_list->count], cur_ext,
921 sizeof(VkExtensionProperties));
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600922 ext_list->count++;
923 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -0700924 return VK_SUCCESS;
Jon Ashburn5c042ea2015-08-04 11:14:18 -0600925}
926
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700927/*
928 * Append one extension property defined in props with entrypoints
Jon Ashburnb8726962016-04-08 15:03:35 -0600929 * defined in entrys to the given ext_list. Do not append if a duplicate
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700930 * Return
931 * Vk_SUCCESS on success
932 */
Jon Ashburn23d36b12016-02-02 17:47:28 -0700933VkResult
934loader_add_to_dev_ext_list(const struct loader_instance *inst,
935 struct loader_device_extension_list *ext_list,
936 const VkExtensionProperties *props,
937 uint32_t entry_count, char **entrys) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700938 uint32_t idx;
939 if (ext_list->list == NULL || ext_list->capacity == 0) {
Jon Ashburn23d36b12016-02-02 17:47:28 -0700940 loader_init_generic_list(inst, (struct loader_generic_list *)ext_list,
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700941 sizeof(struct loader_dev_ext_props));
942 }
943
944 if (ext_list->list == NULL)
945 return VK_ERROR_OUT_OF_HOST_MEMORY;
946
Jon Ashburnb8726962016-04-08 15:03:35 -0600947 // look for duplicates
948 if (has_vk_dev_ext_property(props, ext_list)) {
949 return VK_SUCCESS;
950 }
951
Jon Ashburn23d36b12016-02-02 17:47:28 -0700952 idx = ext_list->count;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700953 // add to list at end
954 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -0700955 if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700956
Mark Young0ad83132016-06-30 13:02:42 -0600957 ext_list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700958 inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
959 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700960
961 if (ext_list->list == NULL)
962 return VK_ERROR_OUT_OF_HOST_MEMORY;
963
964 // double capacity
965 ext_list->capacity *= 2;
966 }
967
Jon Ashburn23d36b12016-02-02 17:47:28 -0700968 memcpy(&ext_list->list[idx].props, props,
969 sizeof(struct loader_dev_ext_props));
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700970 ext_list->list[idx].entrypoint_count = entry_count;
Jon Ashburn23d36b12016-02-02 17:47:28 -0700971 ext_list->list[idx].entrypoints =
Mark Young0ad83132016-06-30 13:02:42 -0600972 loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
973 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
974 if (ext_list->list[idx].entrypoints == NULL) {
975 ext_list->list[idx].entrypoint_count = 0;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700976 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600977 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700978 for (uint32_t i = 0; i < entry_count; i++) {
Mark Young0ad83132016-06-30 13:02:42 -0600979 ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -0700980 inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -0600981 if (ext_list->list[idx].entrypoints[i] == NULL) {
982 for (uint32_t j = 0; j < i; j++) {
983 loader_instance_heap_free(inst,
984 ext_list->list[idx].entrypoints[j]);
985 }
986 loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
987 ext_list->list[idx].entrypoint_count = 0;
988 ext_list->list[idx].entrypoints = NULL;
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700989 return VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Young0ad83132016-06-30 13:02:42 -0600990 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -0700991 strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
992 }
993 ext_list->count++;
994
995 return VK_SUCCESS;
996}
997
Jon Ashburnbd6c4882015-07-02 12:59:25 -0600998/**
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600999 * Search the given search_list for any layers in the props list.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001000 * Add these to the output layer_list. Don't add duplicates to the output
1001 * layer_list.
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001002 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001003static VkResult
1004loader_add_layer_names_to_list(const struct loader_instance *inst,
1005 struct loader_layer_list *output_list,
1006 uint32_t name_count, const char *const *names,
1007 const struct loader_layer_list *search_list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001008 struct loader_layer_properties *layer_prop;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001009 VkResult err = VK_SUCCESS;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001010
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001011 for (uint32_t i = 0; i < name_count; i++) {
1012 const char *search_target = names[i];
Jon Ashburne13ecc92015-08-03 17:19:30 -06001013 layer_prop = loader_get_layer_property(search_target, search_list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001014 if (!layer_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001015 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1016 "Unable to find layer %s", search_target);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001017 err = VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001018 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001019 }
1020
Mark Young0ad83132016-06-30 13:02:42 -06001021 err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001022 }
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06001023
1024 return err;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001025}
1026
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06001027/*
1028 * Manage lists of VkLayerProperties
1029 */
Jon Ashburne39a4f82015-08-28 13:38:21 -06001030static bool loader_init_layer_list(const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001031 struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001032 list->capacity = 32 * sizeof(struct loader_layer_properties);
Mark Young0ad83132016-06-30 13:02:42 -06001033 list->list = loader_instance_heap_alloc(
1034 inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001035 if (list->list == NULL) {
1036 return false;
1037 }
1038 memset(list->list, 0, list->capacity);
1039 list->count = 0;
1040 return true;
1041}
1042
Jon Ashburne39a4f82015-08-28 13:38:21 -06001043void loader_destroy_layer_list(const struct loader_instance *inst,
Mark Young0ad83132016-06-30 13:02:42 -06001044 struct loader_device *device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001045 struct loader_layer_list *layer_list) {
Mark Young0ad83132016-06-30 13:02:42 -06001046 if (device) {
1047 loader_device_heap_free(device, layer_list->list);
1048 } else {
1049 loader_instance_heap_free(inst, layer_list->list);
1050 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001051 layer_list->count = 0;
1052 layer_list->capacity = 0;
1053}
1054
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001055/*
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001056 * Search the given layer list for a list
1057 * matching the given VkLayerProperties
1058 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001059bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop,
1060 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001061 for (uint32_t i = 0; i < list->count; i++) {
1062 if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0)
1063 return true;
1064 }
1065 return false;
1066}
1067
1068/*
1069 * Search the given layer list for a layer
1070 * matching the given name
1071 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001072bool has_layer_name(const char *name, const struct loader_layer_list *list) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001073 for (uint32_t i = 0; i < list->count; i++) {
1074 if (strcmp(name, list->list[i].info.layerName) == 0)
1075 return true;
1076 }
1077 return false;
1078}
1079
1080/*
1081 * Append non-duplicate layer properties defined in prop_list
1082 * to the given layer_info list
1083 */
Mark Young0ad83132016-06-30 13:02:42 -06001084VkResult loader_add_to_layer_list(const struct loader_instance *inst,
1085 struct loader_layer_list *list,
1086 uint32_t prop_list_count,
1087 const struct loader_layer_properties *props) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001088 uint32_t i;
1089 struct loader_layer_properties *layer;
1090
1091 if (list->list == NULL || list->capacity == 0) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001092 loader_init_layer_list(inst, list);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001093 }
1094
1095 if (list->list == NULL)
Mark Young0ad83132016-06-30 13:02:42 -06001096 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001097
1098 for (i = 0; i < prop_list_count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001099 layer = (struct loader_layer_properties *)&props[i];
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001100
1101 // look for duplicates
1102 if (has_vk_layer_property(&layer->info, list)) {
1103 continue;
1104 }
1105
1106 // add to list at end
1107 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001108 if (list->count * sizeof(struct loader_layer_properties) >=
1109 list->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001110
Mark Young0ad83132016-06-30 13:02:42 -06001111 list->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001112 inst, list->list, list->capacity, list->capacity * 2,
1113 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001114 if (NULL == list->list) {
1115 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1116 "realloc failed for layer list when attempting to "
1117 "add new layer");
1118 return VK_ERROR_OUT_OF_HOST_MEMORY;
1119 }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001120 // double capacity
1121 list->capacity *= 2;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001122 }
1123
Jon Ashburn23d36b12016-02-02 17:47:28 -07001124 memcpy(&list->list[list->count], layer,
1125 sizeof(struct loader_layer_properties));
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001126 list->count++;
1127 }
Mark Young0ad83132016-06-30 13:02:42 -06001128
1129 return VK_SUCCESS;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001130}
1131
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001132/**
1133 * Search the search_list for any layer with a name
1134 * that matches the given name and a type that matches the given type
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001135 * Add all matching layers to the found_list
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001136 * Do not add if found loader_layer_properties is already
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001137 * on the found_list.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001138 */
Jon Ashburncc407a22016-04-15 09:25:03 -06001139void loader_find_layer_name_add_list(
1140 const struct loader_instance *inst, const char *name,
1141 const enum layer_type type, const struct loader_layer_list *search_list,
1142 struct loader_layer_list *found_list) {
Jon Ashburn56151d62015-10-05 09:03:21 -06001143 bool found = false;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001144 for (uint32_t i = 0; i < search_list->count; i++) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001145 struct loader_layer_properties *layer_prop = &search_list->list[i];
Jon Ashburnbd332cc2015-07-07 10:27:45 -06001146 if (0 == strcmp(layer_prop->info.layerName, name) &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001147 (layer_prop->type & type)) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06001148 /* Found a layer with the same name, add to found_list */
Mark Young0ad83132016-06-30 13:02:42 -06001149 if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
1150 found = true;
1151 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001152 }
1153 }
Jon Ashburn56151d62015-10-05 09:03:21 -06001154 if (!found) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001155 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001156 "Warning, couldn't find layer name %s to activate", name);
Jon Ashburn56151d62015-10-05 09:03:21 -06001157 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001158}
1159
Jon Ashburn23d36b12016-02-02 17:47:28 -07001160static VkExtensionProperties *
1161get_extension_property(const char *name,
1162 const struct loader_extension_list *list) {
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001163 for (uint32_t i = 0; i < list->count; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001164 if (strcmp(name, list->list[i].extensionName) == 0)
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001165 return &list->list[i];
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001166 }
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06001167 return NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001168}
1169
Jon Ashburn23d36b12016-02-02 17:47:28 -07001170static VkExtensionProperties *
1171get_dev_extension_property(const char *name,
1172 const struct loader_device_extension_list *list) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001173 for (uint32_t i = 0; i < list->count; i++) {
1174 if (strcmp(name, list->list[i].props.extensionName) == 0)
1175 return &list->list[i].props;
1176 }
1177 return NULL;
1178}
1179
Courtney Goeltzenleuchterb39ccd52016-01-15 14:15:00 -07001180
1181/*
Courtney Goeltzenleuchterf538ef72015-12-02 14:00:19 -07001182 * For Instance extensions implemented within the loader (i.e. DEBUG_REPORT
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001183 * the extension must provide two entry points for the loader to use:
1184 * - "trampoline" entry point - this is the address returned by GetProcAddr
1185 * and will always do what's necessary to support a global call.
1186 * - "terminator" function - this function will be put at the end of the
Jon Ashburn232e3af2015-11-30 17:21:25 -07001187 * instance chain and will contain the necessary logic to call / process
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001188 * the extension for the appropriate ICDs that are available.
1189 * There is no generic mechanism for including these functions, the references
1190 * must be placed into the appropriate loader entry points.
Jon Ashburn23d36b12016-02-02 17:47:28 -07001191 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for
1192 * GetProcAddr requests
1193 * loader_coalesce_extensions(void) - add extension records to the list of
1194 * global
Jon Ashburnbd6c4882015-07-02 12:59:25 -06001195 * extension available to the app.
1196 * instance_disp - add function pointer for terminator function to this array.
1197 * The extension itself should be in a separate file that will be
1198 * linked directly with the loader.
1199 */
Jon Ashburn9a4c6aa2015-08-14 11:57:54 -06001200
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001201void loader_get_icd_loader_instance_extensions(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001202 const struct loader_instance *inst, struct loader_icd_libs *icd_libs,
1203 struct loader_extension_list *inst_exts) {
Jon Ashburn5c6a46f2015-08-14 14:49:22 -06001204 struct loader_extension_list icd_exts;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001205 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
1206 "Build ICD instance extension list");
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001207 // traverse scanned icd list adding non-duplicate extensions to the list
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001208 for (uint32_t i = 0; i < icd_libs->count; i++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001209 loader_init_generic_list(inst, (struct loader_generic_list *)&icd_exts,
Jon Ashburn6e6a2162015-12-10 08:51:10 -07001210 sizeof(VkExtensionProperties));
Jon Ashburn23d36b12016-02-02 17:47:28 -07001211 loader_add_instance_extensions(
1212 inst, icd_libs->list[i].EnumerateInstanceExtensionProperties,
1213 icd_libs->list[i].lib_name, &icd_exts);
1214 loader_add_to_ext_list(inst, inst_exts, icd_exts.count, icd_exts.list);
1215 loader_destroy_generic_list(inst,
1216 (struct loader_generic_list *)&icd_exts);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001217 };
1218
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001219 // Traverse loader's extensions, adding non-duplicate extensions to the list
Jon Ashburne39a4f82015-08-28 13:38:21 -06001220 debug_report_add_instance_extensions(inst, inst_exts);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001221}
1222
Jon Ashburne39a4f82015-08-28 13:38:21 -06001223struct loader_icd *loader_get_icd_and_device(const VkDevice device,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001224 struct loader_device **found_dev) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001225 *found_dev = NULL;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001226 for (struct loader_instance *inst = loader.instances; inst;
1227 inst = inst->next) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001228 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001229 for (struct loader_device *dev = icd->logical_device_list; dev;
1230 dev = dev->next)
1231 /* Value comparison of device prevents object wrapping by layers
1232 */
1233 if (loader_get_dispatch(dev->device) ==
1234 loader_get_dispatch(device)) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001235 *found_dev = dev;
1236 return icd;
1237 }
1238 }
1239 }
1240 return NULL;
1241}
1242
Mark Young0ad83132016-06-30 13:02:42 -06001243void loader_destroy_logical_device(const struct loader_instance *inst,
1244 struct loader_device *dev,
1245 const VkAllocationCallbacks *pAllocator) {
1246 if (pAllocator) {
1247 dev->alloc_callbacks = *pAllocator;
1248 }
1249 if (NULL != dev->app_extension_props) {
1250 loader_device_heap_free(dev, dev->app_extension_props);
1251 }
1252 if (NULL != dev->activated_layer_list.list) {
1253 loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
1254 }
1255 loader_device_heap_free(dev, dev);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001256}
1257
Jon Ashburn1530c342016-02-26 13:14:27 -07001258struct loader_device *
Mark Young0ad83132016-06-30 13:02:42 -06001259loader_create_logical_device(const struct loader_instance *inst,
1260 const VkAllocationCallbacks *pAllocator) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001261 struct loader_device *new_dev;
Mark Young0ad83132016-06-30 13:02:42 -06001262#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
1263 {
1264#else
1265 if (pAllocator) {
1266 new_dev = (struct loader_device *)pAllocator->pfnAllocation(
1267 pAllocator->pUserData, sizeof(struct loader_device), sizeof(int *),
1268 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1269 } else {
1270#endif
1271 new_dev = (struct loader_device *)malloc(sizeof(struct loader_device));
1272 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001273
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001274 if (!new_dev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001275 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1276 "Failed to alloc struct loader-device");
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001277 return NULL;
1278 }
1279
1280 memset(new_dev, 0, sizeof(struct loader_device));
Mark Young0ad83132016-06-30 13:02:42 -06001281 if (pAllocator) {
1282 new_dev->alloc_callbacks = *pAllocator;
1283 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001284
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001285 return new_dev;
1286}
1287
Piers Daniell295fe402016-03-29 11:51:11 -06001288void loader_add_logical_device(const struct loader_instance *inst,
1289 struct loader_icd *icd,
1290 struct loader_device *dev) {
1291 dev->next = icd->logical_device_list;
1292 icd->logical_device_list = dev;
1293}
1294
Jon Ashburn23d36b12016-02-02 17:47:28 -07001295void loader_remove_logical_device(const struct loader_instance *inst,
1296 struct loader_icd *icd,
Mark Young0ad83132016-06-30 13:02:42 -06001297 struct loader_device *found_dev,
1298 const VkAllocationCallbacks *pAllocator) {
Jon Ashburn781a7ae2015-11-19 15:43:26 -07001299 struct loader_device *dev, *prev_dev;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001300
1301 if (!icd || !found_dev)
1302 return;
1303
1304 prev_dev = NULL;
1305 dev = icd->logical_device_list;
1306 while (dev && dev != found_dev) {
1307 prev_dev = dev;
1308 dev = dev->next;
1309 }
1310
1311 if (prev_dev)
1312 prev_dev->next = found_dev->next;
1313 else
1314 icd->logical_device_list = found_dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001315 loader_destroy_logical_device(inst, found_dev, pAllocator);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001316}
1317
Jon Ashburn23d36b12016-02-02 17:47:28 -07001318static void loader_icd_destroy(struct loader_instance *ptr_inst,
Mark Young0ad83132016-06-30 13:02:42 -06001319 struct loader_icd *icd,
1320 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001321 ptr_inst->total_icd_count--;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001322 for (struct loader_device *dev = icd->logical_device_list; dev;) {
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001323 struct loader_device *next_dev = dev->next;
Mark Young0ad83132016-06-30 13:02:42 -06001324 loader_destroy_logical_device(ptr_inst, dev, pAllocator);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -06001325 dev = next_dev;
1326 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001327
Mark Young0ad83132016-06-30 13:02:42 -06001328 loader_instance_heap_free(ptr_inst, icd);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001329}
1330
Jon Ashburn23d36b12016-02-02 17:47:28 -07001331static struct loader_icd *
1332loader_icd_create(const struct loader_instance *inst) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001333 struct loader_icd *icd;
1334
Mark Young0ad83132016-06-30 13:02:42 -06001335 icd = loader_instance_heap_alloc(inst, sizeof(*icd),
1336 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001337 if (!icd)
1338 return NULL;
1339
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -06001340 memset(icd, 0, sizeof(*icd));
1341
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001342 return icd;
1343}
1344
Jon Ashburn23d36b12016-02-02 17:47:28 -07001345static struct loader_icd *
1346loader_icd_add(struct loader_instance *ptr_inst,
1347 const struct loader_scanned_icds *icd_lib) {
Chia-I Wu13a61a52014-08-04 11:18:20 +08001348 struct loader_icd *icd;
1349
Jon Ashburne39a4f82015-08-28 13:38:21 -06001350 icd = loader_icd_create(ptr_inst);
Chia-I Wu13a61a52014-08-04 11:18:20 +08001351 if (!icd)
1352 return NULL;
1353
Jon Ashburn3d002332015-08-20 16:35:30 -06001354 icd->this_icd_lib = icd_lib;
1355 icd->this_instance = ptr_inst;
1356
Chia-I Wu13a61a52014-08-04 11:18:20 +08001357 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -07001358 icd->next = ptr_inst->icds;
1359 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001360 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +08001361
1362 return icd;
1363}
Jon Ashburn17b4c862016-04-25 11:09:37 -06001364/**
1365 * Determine the ICD interface version to use.
1366 * @param icd
1367 * @param pVersion Output parameter indicating which version to use or 0 if
1368 * the negotiation API is not supported by the ICD
1369 * @return bool indicating true if the selected interface version is supported
1370 * by the loader, false indicates the version is not supported
1371 * version 0 doesn't support vk_icdGetInstanceProcAddr nor
1372 * vk_icdNegotiateLoaderICDInterfaceVersion
1373 * version 1 supports vk_icdGetInstanceProcAddr
1374 * version 2 supports vk_icdNegotiateLoaderICDInterfaceVersion
1375 */
1376bool loader_get_icd_interface_version(
1377 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version,
1378 uint32_t *pVersion) {
1379
1380 if (fp_negotiate_icd_version == NULL) {
1381 // ICD does not support the negotiation API, it supports version 0 or 1
1382 // calling code must determine if it is version 0 or 1
1383 *pVersion = 0;
1384 } else {
1385 // ICD supports the negotiation API, so call it with the loader's
1386 // latest version supported
1387 *pVersion = CURRENT_LOADER_ICD_INTERFACE_VERSION;
1388 VkResult result = fp_negotiate_icd_version(pVersion);
1389
1390 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
1391 // ICD no longer supports the loader's latest interface version so
1392 // fail loading the ICD
1393 return false;
1394 }
1395 }
1396
1397#if MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION > 0
1398 if (*pVersion < MIN_SUPPORTED_LOADER_ICD_INTERFACE_VERSION) {
1399 // Loader no longer supports the ICD's latest interface version so fail
1400 // loading the ICD
1401 return false;
1402 }
1403#endif
1404 return true;
1405}
Chia-I Wu13a61a52014-08-04 11:18:20 +08001406
Jon Ashburn23d36b12016-02-02 17:47:28 -07001407void loader_scanned_icd_clear(const struct loader_instance *inst,
1408 struct loader_icd_libs *icd_libs) {
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001409 if (icd_libs->capacity == 0)
1410 return;
1411 for (uint32_t i = 0; i < icd_libs->count; i++) {
1412 loader_platform_close_library(icd_libs->list[i].handle);
Mark Young0ad83132016-06-30 13:02:42 -06001413 loader_instance_heap_free(inst, icd_libs->list[i].lib_name);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001414 }
Mark Young0ad83132016-06-30 13:02:42 -06001415 loader_instance_heap_free(inst, icd_libs->list);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001416 icd_libs->capacity = 0;
1417 icd_libs->count = 0;
1418 icd_libs->list = NULL;
1419}
1420
Mark Young0ad83132016-06-30 13:02:42 -06001421static VkResult loader_scanned_icd_init(const struct loader_instance *inst,
1422 struct loader_icd_libs *icd_libs) {
1423 VkResult err = VK_SUCCESS;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001424 loader_scanned_icd_clear(inst, icd_libs);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001425 icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
Mark Young0ad83132016-06-30 13:02:42 -06001426 icd_libs->list = loader_instance_heap_alloc(
1427 inst, icd_libs->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1428 if (NULL == icd_libs->list) {
1429 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1430 "realloc failed for layer list when attempting to add new layer");
1431 err = VK_ERROR_OUT_OF_HOST_MEMORY;
1432 }
1433 return err;
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001434}
1435
Jon Ashburn23d36b12016-02-02 17:47:28 -07001436static void loader_scanned_icd_add(const struct loader_instance *inst,
1437 struct loader_icd_libs *icd_libs,
1438 const char *filename, uint32_t api_version) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001439 loader_platform_dl_handle handle;
Tony Barbour1d2cd3f2015-07-03 10:33:54 -06001440 PFN_vkCreateInstance fp_create_inst;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001441 PFN_vkEnumerateInstanceExtensionProperties fp_get_inst_ext_props;
Jon Ashburnc624c882015-07-16 10:17:29 -06001442 PFN_vkGetInstanceProcAddr fp_get_proc_addr;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001443 PFN_vkNegotiateLoaderICDInterfaceVersion fp_negotiate_icd_version;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001444 struct loader_scanned_icds *new_node;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001445 uint32_t interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001446
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06001447 /* TODO implement smarter opening/closing of libraries. For now this
1448 * function leaves libraries open and the scanned_icd_clear closes them */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001449 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001450 if (!handle) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001451 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001452 loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -07001453 return;
1454 }
1455
Jon Ashburn17b4c862016-04-25 11:09:37 -06001456 // Get and settle on an ICD interface version
Mark Young0ad83132016-06-30 13:02:42 -06001457 fp_negotiate_icd_version = loader_platform_get_proc_address(
1458 handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
Jon Ashburn17b4c862016-04-25 11:09:37 -06001459
1460 if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
Mark Young0ad83132016-06-30 13:02:42 -06001461 &interface_vers)) {
1462 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1463 "ICD (%s) doesn't support interface version compatible"
1464 "with loader, skip this ICD %s",
1465 filename);
1466 return;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001467 }
1468
Jon Ashburn23d36b12016-02-02 17:47:28 -07001469 fp_get_proc_addr =
1470 loader_platform_get_proc_address(handle, "vk_icdGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001471 if (!fp_get_proc_addr) {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001472 assert(interface_vers == 0);
1473 // Use deprecated interface from version 0
Jon Ashburn23d36b12016-02-02 17:47:28 -07001474 fp_get_proc_addr =
1475 loader_platform_get_proc_address(handle, "vkGetInstanceProcAddr");
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001476 if (!fp_get_proc_addr) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001477 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1478 loader_platform_get_proc_address_error(
1479 "vk_icdGetInstanceProcAddr"));
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001480 return;
1481 } else {
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 "Using deprecated ICD interface of "
1484 "vkGetInstanceProcAddr instead of "
Mark Young0ad83132016-06-30 13:02:42 -06001485 "vk_icdGetInstanceProcAddr for ICD %s",
1486 filename);
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001487 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001488 fp_create_inst =
1489 loader_platform_get_proc_address(handle, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001490 if (!fp_create_inst) {
Mark Young0ad83132016-06-30 13:02:42 -06001491 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1492 "Couldn't get vkCreateInstance via dlsym/loadlibrary "
1493 "for ICD %s",
1494 filename);
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001495 return;
1496 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001497 fp_get_inst_ext_props = loader_platform_get_proc_address(
1498 handle, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001499 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001500 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1501 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001502 "via dlsym/loadlibrary for ICD %s",
1503 filename);
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001504 return;
1505 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001506 } else {
Jon Ashburn17b4c862016-04-25 11:09:37 -06001507 // Use newer interface version 1 or later
1508 if (interface_vers == 0)
1509 interface_vers = 1;
1510
Jon Ashburn23d36b12016-02-02 17:47:28 -07001511 fp_create_inst =
1512 (PFN_vkCreateInstance)fp_get_proc_addr(NULL, "vkCreateInstance");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001513 if (!fp_create_inst) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001514 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1515 "Couldn't get vkCreateInstance via "
Mark Young0ad83132016-06-30 13:02:42 -06001516 "vk_icdGetInstanceProcAddr for ICD %s",
1517 filename);
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001518 return;
1519 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001520 fp_get_inst_ext_props =
1521 (PFN_vkEnumerateInstanceExtensionProperties)fp_get_proc_addr(
1522 NULL, "vkEnumerateInstanceExtensionProperties");
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001523 if (!fp_get_inst_ext_props) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001524 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1525 "Couldn't get vkEnumerateInstanceExtensionProperties "
Mark Young0ad83132016-06-30 13:02:42 -06001526 "via vk_icdGetInstanceProcAddr for ICD %s",
1527 filename);
Jon Ashburn69a5f7a2016-01-11 14:41:35 -07001528 return;
1529 }
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001530 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001531
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001532 // check for enough capacity
Jon Ashburn23d36b12016-02-02 17:47:28 -07001533 if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
1534 icd_libs->capacity) {
Jon Ashburne39a4f82015-08-28 13:38:21 -06001535
Mark Young0ad83132016-06-30 13:02:42 -06001536 icd_libs->list = loader_instance_heap_realloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001537 inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
1538 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001539 if (NULL == icd_libs->list) {
1540 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1541 "realloc failed on icd library list");
1542 return;
1543 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001544 // double capacity
1545 icd_libs->capacity *= 2;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001546 }
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001547 new_node = &(icd_libs->list[icd_libs->count]);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001548
1549 new_node->handle = handle;
Jon Ashburn005617f2015-11-17 17:35:40 -07001550 new_node->api_version = api_version;
Jon Ashburnc624c882015-07-16 10:17:29 -06001551 new_node->GetInstanceProcAddr = fp_get_proc_addr;
Jon Ashburnfd4d09d2016-01-07 09:44:27 -07001552 new_node->EnumerateInstanceExtensionProperties = fp_get_inst_ext_props;
Jon Ashburn46888392015-01-29 15:45:51 -07001553 new_node->CreateInstance = fp_create_inst;
Jon Ashburn17b4c862016-04-25 11:09:37 -06001554 new_node->interface_version = interface_vers;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001555
Mark Young0ad83132016-06-30 13:02:42 -06001556 new_node->lib_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07001557 inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Mark Young0ad83132016-06-30 13:02:42 -06001558 if (NULL == new_node->lib_name) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001559 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001560 "Out of memory can't add icd");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001561 return;
1562 }
1563 strcpy(new_node->lib_name, filename);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001564 icd_libs->count++;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001565}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001566
Jon Ashburn23d36b12016-02-02 17:47:28 -07001567static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
1568 const PFN_vkGetInstanceProcAddr fp_gipa) {
1569/* initialize entrypoint function pointers */
Jon Ashburn3da71f22015-05-14 12:43:38 -06001570
Jon Ashburn23d36b12016-02-02 17:47:28 -07001571#define LOOKUP_GIPA(func, required) \
1572 do { \
1573 icd->func = (PFN_vk##func)fp_gipa(inst, "vk" #func); \
1574 if (!icd->func && required) { \
1575 loader_log((struct loader_instance *)inst, \
Jon Ashburn1530c342016-02-26 13:14:27 -07001576 VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07001577 loader_platform_get_proc_address_error("vk" #func)); \
1578 return false; \
1579 } \
Jon Ashburn3da71f22015-05-14 12:43:38 -06001580 } while (0)
1581
Jon Ashburnc624c882015-07-16 10:17:29 -06001582 LOOKUP_GIPA(GetDeviceProcAddr, true);
1583 LOOKUP_GIPA(DestroyInstance, true);
1584 LOOKUP_GIPA(EnumeratePhysicalDevices, true);
1585 LOOKUP_GIPA(GetPhysicalDeviceFeatures, true);
1586 LOOKUP_GIPA(GetPhysicalDeviceFormatProperties, true);
Jon Ashburn754864f2015-07-23 18:49:07 -06001587 LOOKUP_GIPA(GetPhysicalDeviceImageFormatProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001588 LOOKUP_GIPA(CreateDevice, true);
1589 LOOKUP_GIPA(GetPhysicalDeviceProperties, true);
1590 LOOKUP_GIPA(GetPhysicalDeviceMemoryProperties, true);
Cody Northropd0802882015-08-03 17:04:53 -06001591 LOOKUP_GIPA(GetPhysicalDeviceQueueFamilyProperties, true);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001592 LOOKUP_GIPA(EnumerateDeviceExtensionProperties, true);
Jon Ashburnc624c882015-07-16 10:17:29 -06001593 LOOKUP_GIPA(GetPhysicalDeviceSparseImageFormatProperties, true);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001594 LOOKUP_GIPA(CreateDebugReportCallbackEXT, false);
1595 LOOKUP_GIPA(DestroyDebugReportCallbackEXT, false);
Ian Elliott7e40db92015-08-21 15:09:33 -06001596 LOOKUP_GIPA(GetPhysicalDeviceSurfaceSupportKHR, false);
Ian Elliott486c5502015-11-19 16:05:09 -07001597 LOOKUP_GIPA(GetPhysicalDeviceSurfaceCapabilitiesKHR, false);
1598 LOOKUP_GIPA(GetPhysicalDeviceSurfaceFormatsKHR, false);
1599 LOOKUP_GIPA(GetPhysicalDeviceSurfacePresentModesKHR, false);
Petros Bantolas25d27fe2016-04-14 12:50:42 +01001600 LOOKUP_GIPA(GetPhysicalDeviceDisplayPropertiesKHR, false);
1601 LOOKUP_GIPA(GetDisplayModePropertiesKHR, false);
1602 LOOKUP_GIPA(CreateDisplayPlaneSurfaceKHR, false);
1603 LOOKUP_GIPA(GetPhysicalDeviceDisplayPlanePropertiesKHR, false);
1604 LOOKUP_GIPA(GetDisplayPlaneSupportedDisplaysKHR, false);
1605 LOOKUP_GIPA(CreateDisplayModeKHR, false);
1606 LOOKUP_GIPA(GetDisplayPlaneCapabilitiesKHR, false);
1607 LOOKUP_GIPA(DestroySurfaceKHR, false);
Ian Elliott919fa302015-11-24 15:39:10 -07001608#ifdef VK_USE_PLATFORM_WIN32_KHR
1609 LOOKUP_GIPA(GetPhysicalDeviceWin32PresentationSupportKHR, false);
1610#endif
1611#ifdef VK_USE_PLATFORM_XCB_KHR
1612 LOOKUP_GIPA(GetPhysicalDeviceXcbPresentationSupportKHR, false);
1613#endif
Karl Schultz65d20182016-03-08 07:55:27 -07001614#ifdef VK_USE_PLATFORM_XLIB_KHR
1615 LOOKUP_GIPA(GetPhysicalDeviceXlibPresentationSupportKHR, false);
1616#endif
Jason Ekstranda5ebe8a2016-02-12 17:25:03 -08001617#ifdef VK_USE_PLATFORM_WAYLAND_KHR
1618 LOOKUP_GIPA(GetPhysicalDeviceWaylandPresentationSupportKHR, false);
1619#endif
Jon Ashburn3da71f22015-05-14 12:43:38 -06001620
Jon Ashburnc624c882015-07-16 10:17:29 -06001621#undef LOOKUP_GIPA
Ian Elliottd3ef02f2015-07-06 14:36:13 -06001622
Jon Ashburnc624c882015-07-16 10:17:29 -06001623 return true;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001624}
1625
Jon Ashburn23d36b12016-02-02 17:47:28 -07001626static void loader_debug_init(void) {
Mark Young0ad83132016-06-30 13:02:42 -06001627 char *env, *orig;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001628
1629 if (g_loader_debug > 0)
1630 return;
1631
1632 g_loader_debug = 0;
1633
1634 /* parse comma-separated debug options */
Mark Young0ad83132016-06-30 13:02:42 -06001635 orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001636 while (env) {
Mark Young0ad83132016-06-30 13:02:42 -06001637 char *p = strchr(env, ',');
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001638 size_t len;
1639
1640 if (p)
1641 len = p - env;
1642 else
1643 len = strlen(env);
1644
1645 if (len > 0) {
Michael Worcester25c73e72015-12-10 18:06:24 +00001646 if (strncmp(env, "all", len) == 0) {
1647 g_loader_debug = ~0u;
1648 g_loader_log_msgs = ~0u;
1649 } else if (strncmp(env, "warn", len) == 0) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001650 g_loader_debug |= LOADER_WARN_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001651 g_loader_log_msgs |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001652 } else if (strncmp(env, "info", len) == 0) {
1653 g_loader_debug |= LOADER_INFO_BIT;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07001654 g_loader_log_msgs |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001655 } else if (strncmp(env, "perf", len) == 0) {
1656 g_loader_debug |= LOADER_PERF_BIT;
Jon Ashburn1530c342016-02-26 13:14:27 -07001657 g_loader_log_msgs |=
1658 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001659 } else if (strncmp(env, "error", len) == 0) {
1660 g_loader_debug |= LOADER_ERROR_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001661 g_loader_log_msgs |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001662 } else if (strncmp(env, "debug", len) == 0) {
1663 g_loader_debug |= LOADER_DEBUG_BIT;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001664 g_loader_log_msgs |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001665 }
1666 }
1667
1668 if (!p)
1669 break;
1670
1671 env = p + 1;
1672 }
Jon Ashburn38a497f2016-01-04 14:01:38 -07001673
Mark Young0ad83132016-06-30 13:02:42 -06001674 loader_free_getenv(orig, NULL);
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -06001675}
1676
Jon Ashburn23d36b12016-02-02 17:47:28 -07001677void loader_initialize(void) {
Jon Ashburn6461ef22015-09-22 13:11:00 -06001678 // initialize mutexs
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001679 loader_platform_thread_create_mutex(&loader_lock);
Jon Ashburn6461ef22015-09-22 13:11:00 -06001680 loader_platform_thread_create_mutex(&loader_json_lock);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001681
1682 // initialize logging
1683 loader_debug_init();
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001684
1685 // initial cJSON to use alloc callbacks
1686 cJSON_Hooks alloc_fns = {
Mark Young0ad83132016-06-30 13:02:42 -06001687 .malloc_fn = loader_instance_tls_heap_alloc,
1688 .free_fn = loader_instance_tls_heap_free,
Jon Ashburn87d6aa92015-08-28 15:19:27 -06001689 };
1690 cJSON_InitHooks(&alloc_fns);
Jon Ashburn8810c5f2015-08-18 18:04:47 -06001691}
1692
Jon Ashburn2077e382015-06-29 11:25:34 -06001693struct loader_manifest_files {
1694 uint32_t count;
1695 char **filename_list;
1696};
1697
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001698/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001699 * Get next file or dirname given a string list or registry key path
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06001700 *
1701 * \returns
Jon Ashburn2077e382015-06-29 11:25:34 -06001702 * A pointer to first char in the next path.
1703 * The next path (or NULL) in the list is returned in next_path.
1704 * Note: input string is modified in some cases. PASS IN A COPY!
1705 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001706static char *loader_get_next_path(char *path) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001707 uint32_t len;
1708 char *next;
1709
1710 if (path == NULL)
1711 return NULL;
1712 next = strchr(path, PATH_SEPERATOR);
1713 if (next == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001714 len = (uint32_t)strlen(path);
Jon Ashburn2077e382015-06-29 11:25:34 -06001715 next = path + len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001716 } else {
Jon Ashburn2077e382015-06-29 11:25:34 -06001717 *next = '\0';
1718 next++;
1719 }
1720
1721 return next;
1722}
1723
1724/**
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001725 * Given a path which is absolute or relative, expand the path if relative or
1726 * leave the path unmodified if absolute. The base path to prepend to relative
1727 * paths is given in rel_base.
Jon Ashburn15315172015-07-07 15:06:25 -06001728 *
1729 * \returns
1730 * A string in out_fullpath of the full absolute path
Jon Ashburn15315172015-07-07 15:06:25 -06001731 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001732static void loader_expand_path(const char *path, const char *rel_base,
1733 size_t out_size, char *out_fullpath) {
Jon Ashburn15315172015-07-07 15:06:25 -06001734 if (loader_platform_is_path_absolute(path)) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001735 // do not prepend a base to an absolute path
1736 rel_base = "";
Jon Ashburn15315172015-07-07 15:06:25 -06001737 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001738
1739 loader_platform_combine_path(out_fullpath, out_size, rel_base, path, NULL);
Jon Ashburn15315172015-07-07 15:06:25 -06001740}
1741
1742/**
Jon Ashburn2077e382015-06-29 11:25:34 -06001743 * Given a filename (file) and a list of paths (dir), try to find an existing
1744 * file in the paths. If filename already is a path then no
1745 * searching in the given paths.
1746 *
1747 * \returns
1748 * A string in out_fullpath of either the full path or file.
Jon Ashburn2077e382015-06-29 11:25:34 -06001749 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001750static void loader_get_fullpath(const char *file, const char *dirs,
1751 size_t out_size, char *out_fullpath) {
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001752 if (!loader_platform_is_path(file) && *dirs) {
1753 char *dirs_copy, *dir, *next_dir;
1754
1755 dirs_copy = loader_stack_alloc(strlen(dirs) + 1);
1756 strcpy(dirs_copy, dirs);
1757
Jon Ashburn23d36b12016-02-02 17:47:28 -07001758 // find if file exists after prepending paths in given list
1759 for (dir = dirs_copy; *dir && (next_dir = loader_get_next_path(dir));
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001760 dir = next_dir) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001761 loader_platform_combine_path(out_fullpath, out_size, dir, file,
1762 NULL);
Jon Ashburn2077e382015-06-29 11:25:34 -06001763 if (loader_platform_file_exists(out_fullpath)) {
1764 return;
1765 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001766 }
1767 }
Daniel Dadap00b4aba2015-09-30 11:50:51 -05001768
Jon Ashburn2077e382015-06-29 11:25:34 -06001769 snprintf(out_fullpath, out_size, "%s", file);
1770}
1771
1772/**
1773 * Read a JSON file into a buffer.
1774 *
1775 * \returns
1776 * A pointer to a cJSON object representing the JSON parse tree.
1777 * This returned buffer should be freed by caller.
1778 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07001779static cJSON *loader_get_json(const struct loader_instance *inst,
1780 const char *filename) {
Jon Ashburn2077e382015-06-29 11:25:34 -06001781 FILE *file;
1782 char *json_buf;
1783 cJSON *json;
Mark Young93ecb1d2016-01-13 13:47:16 -07001784 size_t len;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001785 file = fopen(filename, "rb");
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001786 if (!file) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001787 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1788 "Couldn't open JSON file %s", filename);
Jon Ashburnaa4ea472015-08-27 08:30:50 -06001789 return NULL;
1790 }
Jon Ashburn2077e382015-06-29 11:25:34 -06001791 fseek(file, 0, SEEK_END);
1792 len = ftell(file);
1793 fseek(file, 0, SEEK_SET);
Jon Ashburn23d36b12016-02-02 17:47:28 -07001794 json_buf = (char *)loader_stack_alloc(len + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06001795 if (json_buf == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001796 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1797 "Out of memory can't get JSON file");
Jon Ashburn2077e382015-06-29 11:25:34 -06001798 fclose(file);
1799 return NULL;
1800 }
1801 if (fread(json_buf, sizeof(char), len, file) != len) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07001802 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1803 "fread failed can't get JSON file");
Jon Ashburn2077e382015-06-29 11:25:34 -06001804 fclose(file);
1805 return NULL;
1806 }
1807 fclose(file);
1808 json_buf[len] = '\0';
1809
Jon Ashburn23d36b12016-02-02 17:47:28 -07001810 // parse text from file
Jon Ashburn2077e382015-06-29 11:25:34 -06001811 json = cJSON_Parse(json_buf);
1812 if (json == NULL)
Jon Ashburn23d36b12016-02-02 17:47:28 -07001813 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1814 "Can't parse JSON file %s", filename);
Jon Ashburn2077e382015-06-29 11:25:34 -06001815 return json;
1816}
1817
1818/**
Jon Ashburn3d002332015-08-20 16:35:30 -06001819 * Do a deep copy of the loader_layer_properties structure.
1820 */
Mark Young0ad83132016-06-30 13:02:42 -06001821VkResult loader_copy_layer_properties(const struct loader_instance *inst,
1822 struct loader_layer_properties *dst,
1823 struct loader_layer_properties *src) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001824 uint32_t cnt, i;
Jon Ashburn23d36b12016-02-02 17:47:28 -07001825 memcpy(dst, src, sizeof(*src));
1826 dst->instance_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001827 loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
1828 src->instance_extension_list.count,
1829 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1830 if (NULL == dst->instance_extension_list.list) {
1831 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1832 "alloc failed for instance extension list");
1833 return VK_ERROR_OUT_OF_HOST_MEMORY;
1834 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07001835 dst->instance_extension_list.capacity =
1836 sizeof(VkExtensionProperties) * src->instance_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001837 memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001838 dst->instance_extension_list.capacity);
1839 dst->device_extension_list.list =
Mark Young0ad83132016-06-30 13:02:42 -06001840 loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
1841 src->device_extension_list.count,
1842 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1843 if (NULL == dst->device_extension_list.list) {
1844 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1845 "alloc failed for device extension list");
1846 return VK_ERROR_OUT_OF_HOST_MEMORY;
1847 }
1848 memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) *
1849 src->device_extension_list.count);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001850
Jon Ashburn23d36b12016-02-02 17:47:28 -07001851 dst->device_extension_list.capacity =
1852 sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
Jon Ashburne39a4f82015-08-28 13:38:21 -06001853 memcpy(dst->device_extension_list.list, src->device_extension_list.list,
Jon Ashburn23d36b12016-02-02 17:47:28 -07001854 dst->device_extension_list.capacity);
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001855 if (src->device_extension_list.count > 0 &&
Jon Ashburn23d36b12016-02-02 17:47:28 -07001856 src->device_extension_list.list->entrypoint_count > 0) {
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001857 cnt = src->device_extension_list.list->entrypoint_count;
Mark Young0ad83132016-06-30 13:02:42 -06001858 dst->device_extension_list.list->entrypoints =
1859 loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
1860 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1861 if (NULL == dst->device_extension_list.list->entrypoints) {
1862 loader_log(
1863 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1864 "alloc failed for device extension list entrypoint array");
1865 return VK_ERROR_OUT_OF_HOST_MEMORY;
1866 }
1867 memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
1868
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001869 for (i = 0; i < cnt; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06001870 dst->device_extension_list.list->entrypoints[i] =
1871 loader_instance_heap_alloc(
1872 inst,
1873 strlen(src->device_extension_list.list->entrypoints[i]) + 1,
1874 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1875 if (NULL == dst->device_extension_list.list->entrypoints[i]) {
1876 loader_log(
1877 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1878 "alloc failed for device extension list entrypoint %d", i);
1879 return VK_ERROR_OUT_OF_HOST_MEMORY;
1880 }
Jon Ashburn39fbd4e2015-12-10 18:17:34 -07001881 strcpy(dst->device_extension_list.list->entrypoints[i],
1882 src->device_extension_list.list->entrypoints[i]);
1883 }
1884 }
Mark Young0ad83132016-06-30 13:02:42 -06001885
1886 return VK_SUCCESS;
Jon Ashburn3d002332015-08-20 16:35:30 -06001887}
1888
Jon Ashburn86a527a2016-02-10 20:59:26 -07001889static bool
1890loader_find_layer_name_list(const char *name,
1891 const struct loader_layer_list *layer_list) {
1892 if (!layer_list)
1893 return false;
1894 for (uint32_t j = 0; j < layer_list->count; j++)
1895 if (!strcmp(name, layer_list->list[j].info.layerName))
1896 return true;
1897 return false;
1898}
1899
1900static bool loader_find_layer_name(const char *name, uint32_t layer_count,
1901 const char **layer_list) {
1902 if (!layer_list)
1903 return false;
1904 for (uint32_t j = 0; j < layer_count; j++)
1905 if (!strcmp(name, layer_list[j]))
1906 return true;
1907 return false;
1908}
1909
Jon Ashburn491cd042016-05-16 14:01:18 -06001910bool loader_find_layer_name_array(
Jon Ashburn86a527a2016-02-10 20:59:26 -07001911 const char *name, uint32_t layer_count,
1912 const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]) {
1913 if (!layer_list)
1914 return false;
1915 for (uint32_t j = 0; j < layer_count; j++)
1916 if (!strcmp(name, layer_list[j]))
1917 return true;
1918 return false;
1919}
1920
1921/**
1922 * Searches through an array of layer names (ppp_layer_names) looking for a
1923 * layer key_name.
1924 * If not found then simply returns updating nothing.
1925 * Otherwise, it uses expand_count, expand_names adding them to layer names.
Chris Forbes69366472016-04-07 09:04:49 +12001926 * Any duplicate (pre-existing) expand_names in layer names are removed.
1927 * Order is otherwise preserved, with the layer key_name being replaced by the
1928 * expand_names.
Jon Ashburn86a527a2016-02-10 20:59:26 -07001929 * @param inst
1930 * @param layer_count
1931 * @param ppp_layer_names
1932 */
Mark Young0ad83132016-06-30 13:02:42 -06001933VkResult loader_expand_layer_names(
1934 struct loader_instance *inst, const char *key_name, uint32_t expand_count,
Jon Ashburn86a527a2016-02-10 20:59:26 -07001935 const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburncc407a22016-04-15 09:25:03 -06001936 uint32_t *layer_count, char const *const **ppp_layer_names) {
Jon Ashburn71483442016-02-11 18:59:43 -07001937
Jon Ashburncc407a22016-04-15 09:25:03 -06001938 char const *const *pp_src_layers = *ppp_layer_names;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001939
Jon Ashburncc407a22016-04-15 09:25:03 -06001940 if (!loader_find_layer_name(key_name, *layer_count,
Jon Ashburn491cd042016-05-16 14:01:18 -06001941 (char const **)pp_src_layers)) {
1942 inst->activated_layers_are_std_val = false;
Mark Young0ad83132016-06-30 13:02:42 -06001943 return VK_SUCCESS; // didn't find the key_name in the list.
Jon Ashburn491cd042016-05-16 14:01:18 -06001944 }
Jon Ashburn71483442016-02-11 18:59:43 -07001945
Jon Ashburn86a527a2016-02-10 20:59:26 -07001946 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
1947 "Found meta layer %s, replacing with actual layer group",
1948 key_name);
Chris Forbesbd9de052016-04-06 20:49:02 +12001949
Jon Ashburn491cd042016-05-16 14:01:18 -06001950 inst->activated_layers_are_std_val = true;
Mark Young0ad83132016-06-30 13:02:42 -06001951 char const **pp_dst_layers = loader_instance_heap_alloc(
Jon Ashburncc407a22016-04-15 09:25:03 -06001952 inst, (expand_count + *layer_count - 1) * sizeof(char const *),
1953 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Mark Young0ad83132016-06-30 13:02:42 -06001954 if (NULL == pp_dst_layers) {
1955 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
1956 "alloc failed for dst layer array");
1957 return VK_ERROR_OUT_OF_HOST_MEMORY;
1958 }
Chris Forbesbd9de052016-04-06 20:49:02 +12001959
1960 // copy layers from src to dst, stripping key_name and anything in
1961 // expand_names.
1962 uint32_t src_index, dst_index = 0;
1963 for (src_index = 0; src_index < *layer_count; src_index++) {
Jon Ashburncc407a22016-04-15 09:25:03 -06001964 if (loader_find_layer_name_array(pp_src_layers[src_index], expand_count,
1965 expand_names)) {
Chris Forbes69366472016-04-07 09:04:49 +12001966 continue;
1967 }
1968
1969 if (!strcmp(pp_src_layers[src_index], key_name)) {
1970 // insert all expand_names in place of key_name
1971 uint32_t expand_index;
Jon Ashburncc407a22016-04-15 09:25:03 -06001972 for (expand_index = 0; expand_index < expand_count;
1973 expand_index++) {
Chris Forbes69366472016-04-07 09:04:49 +12001974 pp_dst_layers[dst_index++] = expand_names[expand_index];
1975 }
Chris Forbesbd9de052016-04-06 20:49:02 +12001976 continue;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001977 }
Chris Forbesbd9de052016-04-06 20:49:02 +12001978
1979 pp_dst_layers[dst_index++] = pp_src_layers[src_index];
Jon Ashburn86a527a2016-02-10 20:59:26 -07001980 }
1981
Chris Forbesbd9de052016-04-06 20:49:02 +12001982 *ppp_layer_names = pp_dst_layers;
1983 *layer_count = dst_index;
Mark Young0ad83132016-06-30 13:02:42 -06001984
1985 return VK_SUCCESS;
Jon Ashburn86a527a2016-02-10 20:59:26 -07001986}
1987
Chris Forbesbd9de052016-04-06 20:49:02 +12001988void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
1989 const VkInstanceCreateInfo *orig,
1990 VkInstanceCreateInfo *ours) {
1991 /* Free the layer names array iff we had to reallocate it */
1992 if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
Mark Young0ad83132016-06-30 13:02:42 -06001993 loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
Jon Ashburn86a527a2016-02-10 20:59:26 -07001994 }
1995}
1996
Jon Ashburn491cd042016-05-16 14:01:18 -06001997void loader_init_std_validation_props(struct loader_layer_properties *props) {
1998 memset(props, 0, sizeof(struct loader_layer_properties));
1999 props->type = VK_LAYER_TYPE_META_EXPLICT;
2000 strncpy(props->info.description, "LunarG Standard Validation Layer",
2001 sizeof (props->info.description));
2002 props->info.implementationVersion = 1;
2003 strncpy(props->info.layerName, std_validation_str,
2004 sizeof (props->info.layerName));
2005 // TODO what about specVersion? for now insert loader's built version
2006 props->info.specVersion = VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION);
2007}
2008
Jon Ashburn86a527a2016-02-10 20:59:26 -07002009/**
Jon Ashburn491cd042016-05-16 14:01:18 -06002010 * Searches through the existing instance layer lists looking for
Jon Ashburn86a527a2016-02-10 20:59:26 -07002011 * the set of required layer names. If found then it adds a meta property to the
2012 * layer list.
2013 * Assumes the required layers are the same for both instance and device lists.
2014 * @param inst
2015 * @param layer_count number of layers in layer_names
2016 * @param layer_names array of required layer names
2017 * @param layer_instance_list
Jon Ashburn86a527a2016-02-10 20:59:26 -07002018 */
2019static void loader_add_layer_property_meta(
2020 const struct loader_instance *inst, uint32_t layer_count,
2021 const char layer_names[][VK_MAX_EXTENSION_NAME_SIZE],
Jon Ashburn491cd042016-05-16 14:01:18 -06002022 struct loader_layer_list *layer_instance_list) {
2023 uint32_t i;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002024 bool found;
2025 struct loader_layer_list *layer_list;
2026
Jon Ashburn491cd042016-05-16 14:01:18 -06002027 if (0 == layer_count || (!layer_instance_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002028 return;
Jon Ashburn491cd042016-05-16 14:01:18 -06002029 if (layer_instance_list && (layer_count > layer_instance_list->count))
Jon Ashburn86a527a2016-02-10 20:59:26 -07002030 return;
2031
Jon Ashburn491cd042016-05-16 14:01:18 -06002032
2033 layer_list = layer_instance_list;
2034
2035 found = true;
2036 if (layer_list == NULL)
2037 return;
2038 for (i = 0; i < layer_count; i++) {
2039 if (loader_find_layer_name_list(layer_names[i], layer_list))
Jon Ashburn888c0502016-02-19 15:22:10 -07002040 continue;
Jon Ashburn491cd042016-05-16 14:01:18 -06002041 found = false;
2042 break;
Jon Ashburn86a527a2016-02-10 20:59:26 -07002043 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002044
2045 struct loader_layer_properties *props;
2046 if (found) {
2047 props = loader_get_next_layer_property(inst, layer_list);
Mark Young0ad83132016-06-30 13:02:42 -06002048 if (NULL == props) {
2049 // Error already triggered in loader_get_next_layer_property.
2050 return;
2051 }
Jon Ashburn491cd042016-05-16 14:01:18 -06002052 loader_init_std_validation_props(props);
2053
2054 }
2055
Jon Ashburn86a527a2016-02-10 20:59:26 -07002056}
2057
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002058static void loader_read_json_layer(
2059 const struct loader_instance *inst,
2060 struct loader_layer_list *layer_instance_list, cJSON *layer_node,
2061 cJSON *item, cJSON *disable_environment, bool is_implicit, char *filename) {
2062 char *temp;
2063 char *name, *type, *library_path, *api_version;
2064 char *implementation_version, *description;
2065 cJSON *ext_item;
2066 VkExtensionProperties ext_prop;
2067
Mark Young0ad83132016-06-30 13:02:42 -06002068/*
2069 * The following are required in the "layer" object:
2070 * (required) "name"
2071 * (required) "type"
2072 * (required) “library_path”
2073 * (required) “api_version”
2074 * (required) “implementation_version”
2075 * (required) “description”
2076 * (required for implicit layers) “disable_environment”
2077 */
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002078
Jon Ashburn23d36b12016-02-02 17:47:28 -07002079#define GET_JSON_OBJECT(node, var) \
2080 { \
2081 var = cJSON_GetObjectItem(node, #var); \
2082 if (var == NULL) { \
2083 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002084 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002085 "Didn't find required layer object %s in manifest " \
2086 "JSON file, skipping this layer", \
2087 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002088 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002089 } \
2090 }
2091#define GET_JSON_ITEM(node, var) \
2092 { \
2093 item = cJSON_GetObjectItem(node, #var); \
2094 if (item == NULL) { \
2095 layer_node = layer_node->next; \
Jon Ashburn1530c342016-02-26 13:14:27 -07002096 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002097 "Didn't find required layer value %s in manifest JSON " \
2098 "file, skipping this layer", \
2099 #var); \
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002100 return; \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002101 } \
2102 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002103 if (temp == NULL) { \
2104 layer_node = layer_node->next; \
2105 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0, \
2106 "Problem accessing layer value %s in manifest JSON " \
2107 "file, skipping this layer", \
2108 #var); \
2109 return; \
2110 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002111 temp[strlen(temp) - 1] = '\0'; \
2112 var = loader_stack_alloc(strlen(temp) + 1); \
2113 strcpy(var, &temp[1]); \
Mark Young0ad83132016-06-30 13:02:42 -06002114 cJSON_Free(temp); \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002115 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002116 GET_JSON_ITEM(layer_node, name)
2117 GET_JSON_ITEM(layer_node, type)
2118 GET_JSON_ITEM(layer_node, library_path)
2119 GET_JSON_ITEM(layer_node, api_version)
2120 GET_JSON_ITEM(layer_node, implementation_version)
2121 GET_JSON_ITEM(layer_node, description)
2122 if (is_implicit) {
2123 GET_JSON_OBJECT(layer_node, disable_environment)
2124 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002125#undef GET_JSON_ITEM
2126#undef GET_JSON_OBJECT
2127
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002128 // add list entry
2129 struct loader_layer_properties *props = NULL;
2130 if (!strcmp(type, "DEVICE")) {
2131 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2132 "Device layers are deprecated skipping this layer");
2133 layer_node = layer_node->next;
2134 return;
2135 }
2136 // Allow either GLOBAL or INSTANCE type interchangeably to handle
2137 // layers that must work with older loaders
2138 if (!strcmp(type, "INSTANCE") || !strcmp(type, "GLOBAL")) {
2139 if (layer_instance_list == NULL) {
Jon Ashburn432d2762015-09-18 12:53:16 -06002140 layer_node = layer_node->next;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002141 return;
Jon Ashburn432d2762015-09-18 12:53:16 -06002142 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002143 props = loader_get_next_layer_property(inst, layer_instance_list);
Mark Young0ad83132016-06-30 13:02:42 -06002144 if (NULL == props) {
2145 // Error already triggered in loader_get_next_layer_property.
2146 return;
2147 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002148 props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
2149 : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
2150 }
Jon Ashburn432d2762015-09-18 12:53:16 -06002151
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002152 if (props == NULL) {
2153 layer_node = layer_node->next;
2154 return;
2155 }
Jon Ashburn15315172015-07-07 15:06:25 -06002156
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002157 strncpy(props->info.layerName, name, sizeof(props->info.layerName));
2158 props->info.layerName[sizeof(props->info.layerName) - 1] = '\0';
2159
2160 char *fullpath = props->lib_name;
2161 char *rel_base;
2162 if (loader_platform_is_path(library_path)) {
2163 // a relative or absolute path
2164 char *name_copy = loader_stack_alloc(strlen(filename) + 1);
2165 strcpy(name_copy, filename);
2166 rel_base = loader_platform_dirname(name_copy);
2167 loader_expand_path(library_path, rel_base, MAX_STRING_SIZE, fullpath);
2168 } else {
2169 // a filename which is assumed in a system directory
2170 loader_get_fullpath(library_path, DEFAULT_VK_LAYERS_PATH,
2171 MAX_STRING_SIZE, fullpath);
2172 }
2173 props->info.specVersion = loader_make_version(api_version);
2174 props->info.implementationVersion = atoi(implementation_version);
2175 strncpy((char *)props->info.description, description,
2176 sizeof(props->info.description));
2177 props->info.description[sizeof(props->info.description) - 1] = '\0';
2178 if (is_implicit) {
2179 if (!disable_environment || !disable_environment->child) {
2180 loader_log(
2181 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2182 "Didn't find required layer child value disable_environment"
2183 "in manifest JSON file, skipping this layer");
2184 layer_node = layer_node->next;
2185 return;
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002186 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002187 strncpy(props->disable_env_var.name, disable_environment->child->string,
2188 sizeof(props->disable_env_var.name));
2189 props->disable_env_var.name[sizeof(props->disable_env_var.name) - 1] =
2190 '\0';
2191 strncpy(props->disable_env_var.value,
2192 disable_environment->child->valuestring,
2193 sizeof(props->disable_env_var.value));
2194 props->disable_env_var.value[sizeof(props->disable_env_var.value) - 1] =
2195 '\0';
2196 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002197
Jon Ashburn23d36b12016-02-02 17:47:28 -07002198/**
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002199* Now get all optional items and objects and put in list:
2200* functions
2201* instance_extensions
2202* device_extensions
2203* enable_environment (implicit layers only)
2204*/
Jon Ashburn23d36b12016-02-02 17:47:28 -07002205#define GET_JSON_OBJECT(node, var) \
2206 { var = cJSON_GetObjectItem(node, #var); }
2207#define GET_JSON_ITEM(node, var) \
2208 { \
2209 item = cJSON_GetObjectItem(node, #var); \
2210 if (item != NULL) { \
2211 temp = cJSON_Print(item); \
Mark Young0ad83132016-06-30 13:02:42 -06002212 if (temp != NULL) { \
2213 temp[strlen(temp) - 1] = '\0'; \
2214 var = loader_stack_alloc(strlen(temp) + 1); \
2215 strcpy(var, &temp[1]); \
2216 cJSON_Free(temp); \
2217 } \
Jon Ashburn23d36b12016-02-02 17:47:28 -07002218 } \
2219 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002220
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002221 cJSON *instance_extensions, *device_extensions, *functions,
2222 *enable_environment;
2223 cJSON *entrypoints;
2224 char *vkGetInstanceProcAddr, *vkGetDeviceProcAddr, *spec_version;
2225 char **entry_array;
2226 vkGetInstanceProcAddr = NULL;
2227 vkGetDeviceProcAddr = NULL;
2228 spec_version = NULL;
2229 entrypoints = NULL;
2230 entry_array = NULL;
2231 int i, j;
Jon Ashburn075ce432015-12-17 17:38:24 -07002232
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002233 /**
2234 * functions
2235 * vkGetInstanceProcAddr
2236 * vkGetDeviceProcAddr
2237 */
2238 GET_JSON_OBJECT(layer_node, functions)
2239 if (functions != NULL) {
2240 GET_JSON_ITEM(functions, vkGetInstanceProcAddr)
2241 GET_JSON_ITEM(functions, vkGetDeviceProcAddr)
2242 if (vkGetInstanceProcAddr != NULL)
2243 strncpy(props->functions.str_gipa, vkGetInstanceProcAddr,
2244 sizeof(props->functions.str_gipa));
2245 props->functions.str_gipa[sizeof(props->functions.str_gipa) - 1] = '\0';
2246 if (vkGetDeviceProcAddr != NULL)
2247 strncpy(props->functions.str_gdpa, vkGetDeviceProcAddr,
2248 sizeof(props->functions.str_gdpa));
2249 props->functions.str_gdpa[sizeof(props->functions.str_gdpa) - 1] = '\0';
2250 }
2251 /**
2252 * instance_extensions
2253 * array of
2254 * name
2255 * spec_version
2256 */
2257 GET_JSON_OBJECT(layer_node, instance_extensions)
2258 if (instance_extensions != NULL) {
2259 int count = cJSON_GetArraySize(instance_extensions);
2260 for (i = 0; i < count; i++) {
2261 ext_item = cJSON_GetArrayItem(instance_extensions, i);
2262 GET_JSON_ITEM(ext_item, name)
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002263 if (name != NULL) {
2264 strncpy(ext_prop.extensionName, name,
2265 sizeof(ext_prop.extensionName));
2266 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2267 '\0';
2268 }
Mark Young0ad83132016-06-30 13:02:42 -06002269 GET_JSON_ITEM(ext_item, spec_version)
2270 if (NULL != spec_version) {
2271 ext_prop.specVersion = atoi(spec_version);
2272 } else {
2273 ext_prop.specVersion = 0;
2274 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002275 bool ext_unsupported =
2276 wsi_unsupported_instance_extension(&ext_prop);
2277 if (!ext_unsupported) {
2278 loader_add_to_ext_list(inst, &props->instance_extension_list, 1,
2279 &ext_prop);
Jon Ashburn075ce432015-12-17 17:38:24 -07002280 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002281 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002282 }
2283 /**
2284 * device_extensions
2285 * array of
2286 * name
2287 * spec_version
2288 * entrypoints
2289 */
2290 GET_JSON_OBJECT(layer_node, device_extensions)
2291 if (device_extensions != NULL) {
2292 int count = cJSON_GetArraySize(device_extensions);
2293 for (i = 0; i < count; i++) {
2294 ext_item = cJSON_GetArrayItem(device_extensions, i);
2295 GET_JSON_ITEM(ext_item, name)
2296 GET_JSON_ITEM(ext_item, spec_version)
2297 if (name != NULL) {
2298 strncpy(ext_prop.extensionName, name,
2299 sizeof(ext_prop.extensionName));
2300 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
2301 '\0';
2302 }
Mark Young0ad83132016-06-30 13:02:42 -06002303 if (NULL != spec_version) {
2304 ext_prop.specVersion = atoi(spec_version);
2305 } else {
2306 ext_prop.specVersion = 0;
2307 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002308 // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
2309 GET_JSON_OBJECT(ext_item, entrypoints)
2310 int entry_count;
2311 if (entrypoints == NULL) {
2312 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2313 &ext_prop, 0, NULL);
2314 continue;
2315 }
2316 entry_count = cJSON_GetArraySize(entrypoints);
Mark Young0ad83132016-06-30 13:02:42 -06002317 if (entry_count) {
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002318 entry_array =
2319 (char **)loader_stack_alloc(sizeof(char *) * entry_count);
Mark Young0ad83132016-06-30 13:02:42 -06002320 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002321 for (j = 0; j < entry_count; j++) {
2322 ext_item = cJSON_GetArrayItem(entrypoints, j);
2323 if (ext_item != NULL) {
2324 temp = cJSON_Print(ext_item);
Mark Young0ad83132016-06-30 13:02:42 -06002325 if (NULL == temp) {
2326 entry_array[j] = NULL;
2327 continue;
2328 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002329 temp[strlen(temp) - 1] = '\0';
2330 entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
2331 strcpy(entry_array[j], &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002332 cJSON_Free(temp);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002333 }
2334 }
2335 loader_add_to_dev_ext_list(inst, &props->device_extension_list,
2336 &ext_prop, entry_count, entry_array);
2337 }
2338 }
2339 if (is_implicit) {
2340 GET_JSON_OBJECT(layer_node, enable_environment)
2341
2342 // enable_environment is optional
2343 if (enable_environment) {
2344 strncpy(props->enable_env_var.name,
2345 enable_environment->child->string,
2346 sizeof(props->enable_env_var.name));
2347 props->enable_env_var.name[sizeof(props->enable_env_var.name) - 1] =
2348 '\0';
2349 strncpy(props->enable_env_var.value,
2350 enable_environment->child->valuestring,
2351 sizeof(props->enable_env_var.value));
2352 props->enable_env_var
2353 .value[sizeof(props->enable_env_var.value) - 1] = '\0';
2354 }
2355 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002356#undef GET_JSON_ITEM
2357#undef GET_JSON_OBJECT
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002358}
2359
2360/**
2361 * Given a cJSON struct (json) of the top level JSON object from layer manifest
2362 * file, add entry to the layer_list. Fill out the layer_properties in this list
2363 * entry from the input cJSON object.
2364 *
2365 * \returns
2366 * void
2367 * layer_list has a new entry and initialized accordingly.
2368 * If the json input object does not have all the required fields no entry
2369 * is added to the list.
2370 */
2371static void
2372loader_add_layer_properties(const struct loader_instance *inst,
2373 struct loader_layer_list *layer_instance_list,
2374 cJSON *json, bool is_implicit, char *filename) {
2375 /* Fields in layer manifest file that are required:
2376 * (required) “file_format_version”
2377 *
Mark Young0ad83132016-06-30 13:02:42 -06002378 * If more than one "layer" object are to be used, use the "layers" array
2379 * instead.
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002380 *
2381 * First get all required items and if any missing abort
2382 */
2383
2384 cJSON *item, *layers_node, *layer_node;
2385 uint16_t file_major_vers = 0;
2386 uint16_t file_minor_vers = 0;
2387 uint16_t file_patch_vers = 0;
2388 char *vers_tok;
2389 cJSON *disable_environment = NULL;
2390 item = cJSON_GetObjectItem(json, "file_format_version");
2391 if (item == NULL) {
2392 return;
2393 }
2394 char *file_vers = cJSON_PrintUnformatted(item);
Mark Young0ad83132016-06-30 13:02:42 -06002395 if (NULL == file_vers) {
2396 return;
2397 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002398 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2399 "Found manifest file %s, version %s", filename, file_vers);
2400 // Get the major/minor/and patch as integers for easier comparison
2401 vers_tok = strtok(file_vers, ".\"\n\r");
2402 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002403 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002404 vers_tok = strtok(NULL, ".\"\n\r");
2405 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002406 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002407 vers_tok = strtok(NULL, ".\"\n\r");
2408 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002409 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002410 }
2411 }
2412 }
2413 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1) {
2414 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2415 "%s Unexpected manifest file version (expected 1.0.0 or "
2416 "1.0.1), may cause errors",
2417 filename);
2418 }
Mark Young0ad83132016-06-30 13:02:42 -06002419 cJSON_Free(file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002420 // If "layers" is present, read in the array of layer objects
2421 layers_node = cJSON_GetObjectItem(json, "layers");
2422 if (layers_node != NULL) {
2423 int numItems = cJSON_GetArraySize(layers_node);
2424 if (file_major_vers == 1 && file_minor_vers == 0 &&
2425 file_patch_vers == 0) {
2426 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2427 "\"layers\" tag not officially added until file version "
2428 "1.0.1, but %s is reporting version %s",
2429 filename, file_vers);
2430 }
2431 for (int curLayer = 0; curLayer < numItems; curLayer++) {
2432 layer_node = cJSON_GetArrayItem(layers_node, curLayer);
2433 if (layer_node == NULL) {
2434 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2435 "Can't find \"layers\" array element %d object in "
2436 "manifest JSON file %s, skipping this file",
2437 curLayer, filename);
2438 return;
2439 }
2440 loader_read_json_layer(inst, layer_instance_list, layer_node, item,
2441 disable_environment, is_implicit, filename);
2442 }
2443 } else {
2444 // Otherwise, try to read in individual layers
2445 layer_node = cJSON_GetObjectItem(json, "layer");
2446 if (layer_node == NULL) {
2447 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2448 "Can't find \"layer\" object in manifest JSON file %s, "
2449 "skipping this file",
2450 filename);
2451 return;
2452 }
2453 // Loop through all "layer" objects in the file to get a count of them
2454 // first.
2455 uint16_t layer_count = 0;
2456 cJSON *tempNode = layer_node;
2457 do {
2458 tempNode = tempNode->next;
2459 layer_count++;
2460 } while (tempNode != NULL);
2461 /*
2462 * Throw a warning if we encounter multiple "layer" objects in file
2463 * versions newer than 1.0.0. Having multiple objects with the same
2464 * name at the same level is actually a JSON standard violation.
2465 */
2466 if (layer_count > 1 &&
2467 (file_major_vers > 1 ||
2468 !(file_minor_vers == 0 && file_patch_vers == 0))) {
2469 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2470 "Multiple \"layer\" nodes are deprecated starting in "
2471 "file version \"1.0.1\". Please use \"layers\" : [] "
2472 "array instead in %s.",
2473 filename);
2474 } else {
2475 do {
2476 loader_read_json_layer(inst, layer_instance_list, layer_node,
2477 item, disable_environment, is_implicit,
2478 filename);
2479 layer_node = layer_node->next;
2480 } while (layer_node != NULL);
2481 }
2482 }
Jon Ashburnfb8ac012015-08-12 16:39:32 -06002483 return;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002484}
2485
2486/**
Jon Ashburn2077e382015-06-29 11:25:34 -06002487 * Find the Vulkan library manifest files.
2488 *
Jon Ashburnb6822212016-02-16 15:34:16 -07002489 * This function scans the "location" or "env_override" directories/files
Jon Ashburn2077e382015-06-29 11:25:34 -06002490 * for a list of JSON manifest files. If env_override is non-NULL
2491 * and has a valid value. Then the location is ignored. Otherwise
2492 * location is used to look for manifest files. The location
2493 * is interpreted as Registry path on Windows and a directory path(s)
Jon Ashburnb6822212016-02-16 15:34:16 -07002494 * on Linux. "home_location" is an additional directory in the users home
2495 * directory to look at. It is exapanded into the dir path $HOME/home_location.
2496 * This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002497 *
2498 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002499 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002500 * A string list of manifest files to be opened in out_files param.
2501 * List has a pointer to string for each manifest filename.
2502 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002503 * Location or override string lists can be either files or directories as
2504 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002505 * | location | override
2506 * --------------------------------
2507 * Win ICD | files | files
2508 * Win Layer | files | dirs
2509 * Linux ICD | dirs | files
2510 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002511 */
Mark Young0ad83132016-06-30 13:02:42 -06002512static VkResult loader_get_manifest_files(
2513 const struct loader_instance *inst, const char *env_override,
2514 char *source_override, bool is_layer, const char *location,
2515 const char *home_location, struct loader_manifest_files *out_files) {
2516 char * override = NULL;
2517 char *loc, *orig_loc = NULL;
2518 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002519 char *file, *next_file, *name;
2520 size_t alloced_count = 64;
2521 char full_path[2048];
2522 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002523 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002524 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002525 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002526
2527 out_files->count = 0;
2528 out_files->filename_list = NULL;
2529
Jamie Madill00c3c912016-04-06 18:26:46 -04002530 if (source_override != NULL) {
2531 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002532 } else if (env_override != NULL &&
2533 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002534#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002535 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002536 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002537 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002538 override = NULL;
2539 }
2540#endif
2541 }
2542
Jon Ashburnb6822212016-02-16 15:34:16 -07002543#if !defined(_WIN32)
2544 if (location == NULL && home_location == NULL) {
2545#else
2546 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002547 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002548#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002549 loader_log(
2550 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002551 "Can't get manifest files with NULL location, env_override=%s",
2552 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002553 res = VK_ERROR_INITIALIZATION_FAILED;
2554 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002555 }
2556
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002557#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002558 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002559#else
2560 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002561#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002562 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002563 // Also handle getting the location(s) from registry on Windows
2564 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002565 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002566 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002567 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2568 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002569 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2570 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002571 }
2572 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002573#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002574 reg = loader_get_registry_files(inst, loc);
2575 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002576 if (!is_layer) {
2577 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2578 "Registry lookup failed can't get ICD manifest "
2579 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002580 // This typically only fails when out of memory, which is
2581 // critical
2582 // if this is for the loader.
2583 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002584 } else {
2585 // warning only for layers
2586 loader_log(
2587 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2588 "Registry lookup failed can't get layer manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002589 // Return success for now since it's not critical for layers
2590 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002591 }
Mark Young0ad83132016-06-30 13:02:42 -06002592 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002593 }
Mark Young0ad83132016-06-30 13:02:42 -06002594 orig_loc = loc;
2595 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002596#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002597 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002598 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002599 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002600 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2601 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002602 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2603 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002604 }
2605 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002606 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002607 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002608 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002609 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002610
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002611 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002612 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2613 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002614
Jon Ashburn2077e382015-06-29 11:25:34 -06002615 file = loc;
2616 while (*file) {
2617 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002618 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002619 sysdir = opendir(file);
2620 name = NULL;
2621 if (sysdir) {
2622 dent = readdir(sysdir);
2623 if (dent == NULL)
2624 break;
2625 name = &(dent->d_name[0]);
2626 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2627 name = full_path;
2628 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002629 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002630#if defined(_WIN32)
2631 name = file;
2632#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002633 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002634 char *dir;
2635 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002636 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002637 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002638 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2639 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002640 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002641 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002642 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002643
2644 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2645
2646 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002647#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002648 }
2649 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002650 /* Look for files ending with ".json" suffix */
2651 uint32_t nlen = (uint32_t)strlen(name);
2652 const char *suf = name + nlen - 5;
2653 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2654 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002655 out_files->filename_list = loader_instance_heap_alloc(
2656 inst, alloced_count * sizeof(char *),
2657 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002658 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002659 out_files->filename_list = loader_instance_heap_realloc(
2660 inst, out_files->filename_list,
2661 alloced_count * sizeof(char *),
2662 alloced_count * sizeof(char *) * 2,
2663 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002664 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002665 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002666 if (out_files->filename_list == NULL) {
2667 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2668 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002669 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2670 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002671 }
Mark Young0ad83132016-06-30 13:02:42 -06002672 out_files->filename_list[out_files->count] =
2673 loader_instance_heap_alloc(
2674 inst, strlen(name) + 1,
2675 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002676 if (out_files->filename_list[out_files->count] == NULL) {
2677 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2678 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002679 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2680 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002681 }
2682 strcpy(out_files->filename_list[out_files->count], name);
2683 out_files->count++;
2684 } else if (!list_is_dirs) {
2685 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002686 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002687 "Skipping manifest file %s, file name must end in .json",
2688 name);
2689 }
2690 if (list_is_dirs) {
2691 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002692 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002693 break;
Mark Young0ad83132016-06-30 13:02:42 -06002694 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002695 name = &(dent->d_name[0]);
2696 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2697 name = full_path;
2698 } else {
2699 break;
2700 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002701 }
Mark Young0ad83132016-06-30 13:02:42 -06002702 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002703 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002704 sysdir = NULL;
2705 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002706 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002707#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002708 if (home_location != NULL &&
2709 (next_file == NULL || *next_file == '\0') && override == NULL) {
Jon Ashburn67e262e2016-02-18 12:45:39 -07002710 char *home = secure_getenv("HOME");
2711 if (home != NULL) {
2712 size_t len;
2713 char *home_loc = loader_stack_alloc(strlen(home) + 2 +
2714 strlen(home_location));
2715 if (home_loc == NULL) {
2716 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002717 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002718 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2719 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002720 }
2721 strcpy(home_loc, home);
2722 // Add directory separator if needed
2723 if (home_location[0] != DIRECTORY_SYMBOL) {
2724 len = strlen(home_loc);
2725 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002726 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002727 }
2728 strcat(home_loc, home_location);
2729 file = home_loc;
2730 next_file = loader_get_next_path(file);
2731 home_location = NULL;
2732
Jon Ashburn1530c342016-02-26 13:14:27 -07002733 loader_log(
2734 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2735 "Searching the following paths for manifest files: %s\n",
2736 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002737 list_is_dirs = true;
2738 }
2739 }
2740#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002741 }
Mark Young0ad83132016-06-30 13:02:42 -06002742
2743out:
2744 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2745 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2746 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2747 }
2748 loader_instance_heap_free(inst, out_files->filename_list);
2749 out_files->count = 0;
2750 out_files->filename_list = NULL;
2751 }
2752
2753 if (NULL != sysdir) {
2754 closedir(sysdir);
2755 }
2756
2757 if (NULL != reg && reg != orig_loc) {
2758 loader_instance_heap_free(inst, reg);
2759 }
2760 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002761}
2762
Jon Ashburn23d36b12016-02-02 17:47:28 -07002763void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002764
Jon Ashburn23d36b12016-02-02 17:47:28 -07002765void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002766/**
2767 * Try to find the Vulkan ICD driver(s).
2768 *
2769 * This function scans the default system loader path(s) or path
2770 * specified by the \c VK_ICD_FILENAMES environment variable in
2771 * order to find loadable VK ICDs manifest files. From these
2772 * manifest files it finds the ICD libraries.
2773 *
2774 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002775 * Vulkan result
2776 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002777 */
Mark Young0ad83132016-06-30 13:02:42 -06002778VkResult loader_icd_scan(const struct loader_instance *inst,
2779 struct loader_icd_libs *icds) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002780 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002781 uint16_t file_major_vers = 0;
2782 uint16_t file_minor_vers = 0;
2783 uint16_t file_patch_vers = 0;
2784 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002785 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002786 VkResult res = VK_SUCCESS;
2787 bool lockedMutex = false;
2788 cJSON *json = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002789
Mark Young0ad83132016-06-30 13:02:42 -06002790 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2791
2792 res = loader_scanned_icd_init(inst, icds);
2793 if (VK_SUCCESS != res) {
2794 goto out;
2795 }
2796
Jon Ashburn2077e382015-06-29 11:25:34 -06002797 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002798 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
2799 DEFAULT_VK_DRIVERS_INFO,
2800 HOME_VK_DRIVERS_INFO, &manifest_files);
2801 if (VK_SUCCESS != res || manifest_files.count == 0) {
2802 goto out;
2803 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002804 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002805 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002806 for (uint32_t i = 0; i < manifest_files.count; i++) {
2807 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002808 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002809 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002810 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002811
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -07002812 json = loader_get_json(inst, file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002813 if (!json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002814 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002815 }
Jon Ashburn005617f2015-11-17 17:35:40 -07002816 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002817 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002818 if (item == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002819 res = VK_ERROR_INITIALIZATION_FAILED;
2820 goto out;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002821 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002822 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002823 if (NULL == file_vers) {
2824 // Only reason the print can fail is if there was an allocation
2825 // issue
2826 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2827 goto out;
2828 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002829 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2830 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002831 // Get the major/minor/and patch as integers for easier comparison
2832 vers_tok = strtok(file_vers, ".\"\n\r");
2833 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002834 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002835 vers_tok = strtok(NULL, ".\"\n\r");
2836 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002837 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002838 vers_tok = strtok(NULL, ".\"\n\r");
2839 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002840 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002841 }
2842 }
2843 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002844 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002845 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002846 "Unexpected manifest file version (expected 1.0.0 or "
2847 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002848 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002849 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002850 itemICD = cJSON_GetObjectItem(json, "ICD");
2851 if (itemICD != NULL) {
2852 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002853 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002854 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002855 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002856 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002857 "Can't find \"library_path\" in ICD JSON file "
2858 "%s, skipping",
2859 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002860 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002861 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002862 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002863 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06002864 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002865 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06002866 temp[strlen(temp) - 1] = '\0';
2867 char *library_path = loader_stack_alloc(strlen(temp) + 1);
2868 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002869 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002870 if (!library_path || strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002871 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002872 "Can't find \"library_path\" in ICD JSON file "
2873 "%s, skipping",
2874 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06002875 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002876 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002877 continue;
2878 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04002879 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06002880 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002881 loader_log(
2882 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04002883 "Searching for ICD drivers named %s default dir %s\n",
2884 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002885 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06002886 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002887 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
2888 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06002889 strcpy(name_copy, file_str);
2890 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002891 loader_expand_path(library_path, rel_base, sizeof(fullpath),
2892 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002893 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04002894 // a filename which is assumed in a system directory
2895 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
2896 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06002897 }
Jon Ashburn005617f2015-11-17 17:35:40 -07002898
2899 uint32_t vers = 0;
2900 item = cJSON_GetObjectItem(itemICD, "api_version");
2901 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002902 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002903 if (NULL == temp) {
2904 // Only reason the print can fail is if there was an
2905 // allocation issue
2906 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2907 goto out;
2908 }
Jon Ashburn005617f2015-11-17 17:35:40 -07002909 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06002910 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07002911 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04002912 loader_scanned_icd_add(inst, icds, fullpath, vers);
Mark Young0ad83132016-06-30 13:02:42 -06002913 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002914 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002915 "Can't find \"library_path\" object in ICD JSON "
2916 "file %s, skipping",
2917 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002918 }
2919 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002920 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002921 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002922 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
2923 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002924 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002925
Mark Young0ad83132016-06-30 13:02:42 -06002926 cJSON_Delete(json);
2927 json = NULL;
2928 }
2929
2930out:
2931 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002932 cJSON_Delete(json);
2933 }
Mark Young0ad83132016-06-30 13:02:42 -06002934 if (NULL != manifest_files.filename_list) {
2935 for (uint32_t i = 0; i < manifest_files.count; i++) {
2936 if (NULL != manifest_files.filename_list[i]) {
2937 loader_instance_heap_free(inst,
2938 manifest_files.filename_list[i]);
2939 }
2940 }
2941 loader_instance_heap_free(inst, manifest_files.filename_list);
2942 }
2943 if (lockedMutex) {
2944 loader_platform_thread_unlock_mutex(&loader_json_lock);
2945 }
2946 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002947}
2948
Jon Ashburn23d36b12016-02-02 17:47:28 -07002949void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06002950 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002951 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002952 struct loader_manifest_files
2953 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002954 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07002955 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06002956 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06002957
Mark Young0ad83132016-06-30 13:02:42 -06002958 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
2959
2960 // Get a list of manifest files for explicit layers
2961 if (VK_SUCCESS !=
2962 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
2963 true, DEFAULT_VK_ELAYERS_INFO,
2964 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
2965 goto out;
2966 }
2967
2968 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07002969 // Pass NULL for environment variable override - implicit layers are not
2970 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06002971 if (VK_SUCCESS != loader_get_manifest_files(
2972 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
2973 HOME_VK_ILAYERS_INFO, &manifest_files[1])) {
2974 goto out;
2975 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06002976
Mark Young0ad83132016-06-30 13:02:42 -06002977 // Make sure we have at least one layer, if not, go ahead and return
2978 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
2979 goto out;
2980 }
2981
2982 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06002983 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06002984
Jon Ashburn6461ef22015-09-22 13:11:00 -06002985 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002986 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07002987 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002988 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07002989 file_str = manifest_files[implicit].filename_list[i];
2990 if (file_str == NULL)
2991 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06002992
Jon Ashburn075ce432015-12-17 17:38:24 -07002993 // parse file into JSON struct
2994 json = loader_get_json(inst, file_str);
2995 if (!json) {
2996 continue;
2997 }
2998
Jon Ashburn491cd042016-05-16 14:01:18 -06002999 loader_add_layer_properties(inst, instance_layers, json,
3000 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003001 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003002 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003003 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003004
3005 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003006 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3007 sizeof(std_validation_names[0]),
3008 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003009
Mark Young0ad83132016-06-30 13:02:42 -06003010out:
3011
3012 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3013 if (NULL != manifest_files[manFile].filename_list) {
3014 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3015 if (NULL != manifest_files[manFile].filename_list[i]) {
3016 loader_instance_heap_free(
3017 inst, manifest_files[manFile].filename_list[i]);
3018 }
3019 }
3020 loader_instance_heap_free(inst,
3021 manifest_files[manFile].filename_list);
3022 }
3023 }
3024 if (lockedMutex) {
3025 loader_platform_thread_unlock_mutex(&loader_json_lock);
3026 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003027}
3028
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003029void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003030 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003031 char *file_str;
3032 struct loader_manifest_files manifest_files;
3033 cJSON *json;
3034 uint32_t i;
3035
3036 // Pass NULL for environment variable override - implicit layers are not
3037 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003038 VkResult res = loader_get_manifest_files(
3039 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
3040 &manifest_files);
3041 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003042 return;
3043 }
3044
3045 /* cleanup any previously scanned libraries */
3046 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003047
3048 loader_platform_thread_lock_mutex(&loader_json_lock);
3049
3050 for (i = 0; i < manifest_files.count; i++) {
3051 file_str = manifest_files.filename_list[i];
3052 if (file_str == NULL) {
3053 continue;
3054 }
3055
3056 // parse file into JSON struct
3057 json = loader_get_json(inst, file_str);
3058 if (!json) {
3059 continue;
3060 }
3061
Mark Young0ad83132016-06-30 13:02:42 -06003062 loader_add_layer_properties(inst, instance_layers, json, true,
3063 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003064
Mark Young0ad83132016-06-30 13:02:42 -06003065 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003066 cJSON_Delete(json);
3067 }
Mark Young0ad83132016-06-30 13:02:42 -06003068 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003069
3070 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003071 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3072 sizeof(std_validation_names[0]),
3073 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003074
3075 loader_platform_thread_unlock_mutex(&loader_json_lock);
3076}
3077
Jon Ashburn23d36b12016-02-02 17:47:28 -07003078static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3079loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003080 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003081 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003082 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003083 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003084 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003085 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003086
Jon Ashburn27cd5842015-05-12 17:26:48 -06003087 // inst is not wrapped
3088 if (inst == VK_NULL_HANDLE) {
3089 return NULL;
3090 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003091 VkLayerInstanceDispatchTable *disp_table =
3092 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003093 void *addr;
3094
3095 if (disp_table == NULL)
3096 return NULL;
3097
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003098 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003099 addr =
3100 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003101 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003102 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003103 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003104
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003105 // Don't call down the chain, this would be an infinite loop
3106 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003107 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003108 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003109}
3110
Piers Daniell295fe402016-03-29 11:51:11 -06003111static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003112loader_gpa_device_internal(VkDevice device, const char *pName) {
3113 struct loader_device *dev;
3114 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
Piers Daniell295fe402016-03-29 11:51:11 -06003115 return icd->GetDeviceProcAddr(device, pName);
3116}
3117
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003118/**
3119 * Initialize device_ext dispatch table entry as follows:
3120 * If dev == NULL find all logical devices created within this instance and
3121 * init the entry (given by idx) in the ext dispatch table.
3122 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003123 * The initialization value is gotten by calling down the device chain with
3124 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003125 * If GDPA returns NULL then don't initialize the dispatch table entry.
3126 */
3127static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003128 struct loader_device *dev,
3129 uint32_t idx,
3130 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003131
Jon Ashburn23d36b12016-02-02 17:47:28 -07003132{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003133 void *gdpa_value;
3134 if (dev != NULL) {
3135 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003136 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003137 if (gdpa_value != NULL)
Jon Ashburn23d36b12016-02-02 17:47:28 -07003138 dev->loader_dispatch.ext_dispatch.DevExt[idx] =
3139 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003140 } else {
3141 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
3142 struct loader_icd *icd = &inst->icds[i];
Karl Schultz2558bd32016-02-24 14:39:39 -07003143 struct loader_device *ldev = icd->logical_device_list;
3144 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003145 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003146 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3147 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003148 if (gdpa_value != NULL)
Karl Schultz2558bd32016-02-24 14:39:39 -07003149 ldev->loader_dispatch.ext_dispatch.DevExt[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003150 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003151 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003152 }
3153 }
3154 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003155}
3156
3157/**
3158 * Find all dev extension in the hash table and initialize the dispatch table
3159 * for dev for each of those extension entrypoints found in hash table.
3160
3161 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003162void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3163 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003164 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3165 if (inst->disp_hash[i].func_name != NULL)
3166 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3167 inst->disp_hash[i].func_name);
3168 }
3169}
3170
3171static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003172 const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003173 struct loader_icd *icd;
3174 icd = inst->icds;
3175 while (icd) {
3176 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
3177 // this icd supports funcName
3178 return true;
3179 icd = icd->next;
3180 }
3181
3182 return false;
3183}
3184
Jon Ashburncc407a22016-04-15 09:25:03 -06003185static bool loader_check_layer_list_for_address(
3186 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003187 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003188 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003189 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003190 const struct loader_device_extension_list *const extensions =
3191 &(layers->list[layer].device_extension_list);
3192 for (uint32_t extension = 0; extension < extensions->count;
3193 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003194 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003195 const struct loader_dev_ext_props *const property =
3196 &(extensions->list[extension]);
3197 for (uint32_t entry = 0; entry < property->entrypoint_count;
3198 ++entry) {
3199 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003200 return true;
3201 }
3202 }
3203 }
3204 }
3205
3206 return false;
3207}
3208
Jon Ashburncc407a22016-04-15 09:25:03 -06003209static bool
3210loader_check_layers_for_address(const struct loader_instance *const inst,
3211 const char *funcName) {
3212 if (loader_check_layer_list_for_address(&inst->instance_layer_list,
3213 funcName)) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003214 return true;
3215 }
3216
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003217 return false;
3218}
3219
Jon Ashburn23d36b12016-02-02 17:47:28 -07003220static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003221 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003222 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3223 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003224 }
3225 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3226}
3227
3228static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003229 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003230 uint32_t i;
3231 uint32_t idx = *ptr_idx;
3232 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3233
3234 if (!inst->disp_hash[idx].func_name) {
3235 // no entry here at this idx, so use it
3236 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003237 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003238 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003239 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003240 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003241 "loader_add_dev_ext_table() can't allocate memory for "
3242 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003243 return false;
3244 }
3245 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3246 return true;
3247 }
3248
3249 // check for enough capacity
3250 if (list->capacity == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06003251 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003252 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3253 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003254 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003255 "loader_add_dev_ext_table() can't allocate list memory");
3256 return false;
3257 }
3258 list->capacity = 8 * sizeof(*(list->index));
3259 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0ad83132016-06-30 13:02:42 -06003260 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003261 list->capacity * 2,
3262 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003263 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003264 loader_log(
3265 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3266 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003267 return false;
3268 }
3269 list->capacity *= 2;
3270 }
3271
Jon Ashburn23d36b12016-02-02 17:47:28 -07003272 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003273 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3274 do {
3275 if (!inst->disp_hash[i].func_name) {
3276 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003277 inst->disp_hash[i].func_name =
Mark Young0ad83132016-06-30 13:02:42 -06003278 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003279 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003280 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003281 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003282 "loader_add_dev_ext_table() can't rallocate "
3283 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003284 return false;
3285 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003286 strncpy(inst->disp_hash[i].func_name, funcName,
3287 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003288 list->index[list->count] = i;
3289 list->count++;
3290 *ptr_idx = i;
3291 return true;
3292 }
3293 i = (i + 1) % MAX_NUM_DEV_EXTS;
3294 } while (i != idx);
3295
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003296 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003297 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3298 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003299 return false;
3300}
3301
3302static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003303 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003304 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003305 if (inst->disp_hash[*idx].func_name &&
3306 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003307 return true;
3308
3309 // funcName wasn't at the primary spot in the hash table
3310 // search the list of secondary locations (shallow search, not deep search)
3311 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3312 alt_idx = inst->disp_hash[*idx].list.index[i];
3313 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3314 *idx = alt_idx;
3315 return true;
3316 }
3317 }
3318
3319 return false;
3320}
3321
3322/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003323 * This function returns generic trampoline code address for unknown entry
3324 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003325 * Presumably, these unknown entry points (as given by funcName) are device
3326 * extension entrypoints. A hash table is used to keep a list of unknown entry
3327 * points and their mapping to the device extension dispatch table
3328 * (struct loader_dev_ext_dispatch_table).
3329 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003330 * For a given entry point string (funcName), if an existing mapping is found
3331 * the
3332 * trampoline address for that mapping is returned. Otherwise, this unknown
3333 * entry point
3334 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3335 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003336 * new entry in the hash table is initialized and that trampoline address for
3337 * the new entry is returned. Null is returned if the hash table is full or
3338 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3339 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003340void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003341 uint32_t idx;
3342 uint32_t seed = 0;
3343
3344 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3345
3346 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3347 // found funcName already in hash
3348 return loader_get_dev_ext_trampoline(idx);
3349
3350 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003351 if (!loader_check_icds_for_address(inst, funcName) &&
3352 !loader_check_layers_for_address(inst, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003353 // if support found in layers continue on
3354 return NULL;
3355 }
3356
3357 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3358 // successfully added new table entry
3359 // init any dev dispatch table entrys as needed
3360 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3361 return loader_get_dev_ext_trampoline(idx);
3362 }
3363
3364 return NULL;
3365}
3366
Jon Ashburn23d36b12016-02-02 17:47:28 -07003367struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003368 /* look up the loader_instance in our list by comparing dispatch tables, as
3369 * there is no guarantee the instance is still a loader_instance* after any
3370 * layers which wrap the instance object.
3371 */
3372 const VkLayerInstanceDispatchTable *disp;
3373 struct loader_instance *ptr_instance = NULL;
3374 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003375 for (struct loader_instance *inst = loader.instances; inst;
3376 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003377 if (inst->disp == disp) {
3378 ptr_instance = inst;
3379 break;
3380 }
3381 }
3382 return ptr_instance;
3383}
3384
Jon Ashburn23d36b12016-02-02 17:47:28 -07003385static loader_platform_dl_handle
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003386loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3387 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003388
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003389 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003390 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003391 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003392 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003393 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003394 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003395 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003396 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003397 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003398
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003399 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003400}
3401
Jon Ashburn23d36b12016-02-02 17:47:28 -07003402static void
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003403loader_close_layer_lib(const struct loader_instance *inst,
3404 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003405
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003406 if (prop->lib_handle) {
3407 loader_platform_close_library(prop->lib_handle);
3408 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3409 "Unloading layer library %s", prop->lib_name);
3410 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003411 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003412}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003413
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003414void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003415 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003416 struct loader_layer_list *list) {
3417 /* delete instance list of enabled layers and close any layer libraries */
3418 for (uint32_t i = 0; i < list->count; i++) {
3419 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003420
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003421 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003422 }
Mark Young0ad83132016-06-30 13:02:42 -06003423 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003424}
3425
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003426/**
3427 * Go through the search_list and find any layers which match type. If layer
3428 * type match is found in then add it to ext_list.
3429 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003430static void
3431loader_add_layer_implicit(const struct loader_instance *inst,
3432 const enum layer_type type,
3433 struct loader_layer_list *list,
3434 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003435 bool enable;
3436 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003437 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003438 for (i = 0; i < search_list->count; i++) {
3439 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003440 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003441 /* Found an implicit layer, see if it should be enabled */
3442 enable = false;
3443
Jon Ashburn23d36b12016-02-02 17:47:28 -07003444 // if no enable_environment variable is specified, this implicit
3445 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003446 // should always be enabled. Otherwise check if the variable is set
3447 if (prop->enable_env_var.name[0] == 0) {
3448 enable = true;
3449 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003450 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003451 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3452 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003453 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003454 }
3455
3456 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003457 // environment variables are set, the layer is disabled. Implicit
3458 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003459 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003460 env_value = loader_getenv(prop->disable_env_var.name, inst);
3461 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003462 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003463 }
3464 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003465
Mark Young0ad83132016-06-30 13:02:42 -06003466 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003467 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003468 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003469 }
3470 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003471}
3472
3473/**
3474 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003475 * is found in search_list then add it to layer_list. But only add it to
3476 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003477 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003478static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003479 const enum layer_type type,
3480 const char *env_name,
3481 struct loader_layer_list *layer_list,
3482 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003483 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003484 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003485
Mark Young0ad83132016-06-30 13:02:42 -06003486 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003487 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003488 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003489 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003490 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003491 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003492 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003493 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003494 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003495
Mark Young0ad83132016-06-30 13:02:42 -06003496 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003497
Jon Ashburn23d36b12016-02-02 17:47:28 -07003498 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003499 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003500 if (!strcmp(std_validation_str, name)) {
3501 /* add meta list of layers
3502 don't attempt to remove duplicate layers already added by app or
3503 env var
3504 */
3505 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3506 "Expanding meta layer %s found in environment variable",
3507 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003508 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3509 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003510 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3511 sizeof(std_validation_names[0]);
3512 i++) {
3513 loader_find_layer_name_add_list(inst, std_validation_names[i],
3514 type, search_list, layer_list);
3515 }
3516 } else {
3517 loader_find_layer_name_add_list(inst, name, type, search_list,
3518 layer_list);
3519 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003520 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003521 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003522
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003523 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003524}
3525
Jon Ashburn23d36b12016-02-02 17:47:28 -07003526VkResult
3527loader_enable_instance_layers(struct loader_instance *inst,
3528 const VkInstanceCreateInfo *pCreateInfo,
3529 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003530 VkResult err;
3531
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003532 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003533
Jon Ashburne39a4f82015-08-28 13:38:21 -06003534 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003535 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3536 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003537 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003538 }
3539
Jon Ashburn0c26e712015-07-02 16:10:32 -06003540 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003541 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3542 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003543
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003544 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003545 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3546 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003547 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003548
3549 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003550 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003551 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3552 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003553
3554 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003555}
3556
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003557/*
3558 * Given the list of layers to activate in the loader_instance
3559 * structure. This function will add a VkLayerInstanceCreateInfo
3560 * structure to the VkInstanceCreateInfo.pNext pointer.
3561 * Each activated layer will have it's own VkLayerInstanceLink
3562 * structure that tells the layer what Get*ProcAddr to call to
3563 * get function pointers to the next layer down.
3564 * Once the chain info has been created this function will
3565 * execute the CreateInstance call chain. Each layer will
3566 * then have an opportunity in it's CreateInstance function
3567 * to setup it's dispatch table when the lower layer returns
3568 * successfully.
3569 * Each layer can wrap or not-wrap the returned VkInstance object
3570 * as it sees fit.
3571 * The instance chain is terminated by a loader function
3572 * that will call CreateInstance on all available ICD's and
3573 * cache those VkInstance objects for future use.
3574 */
3575VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003576 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003577 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003578 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003579 uint32_t activated_layers = 0;
3580 VkLayerInstanceCreateInfo chain_info;
3581 VkLayerInstanceLink *layer_instance_link_info = NULL;
3582 VkInstanceCreateInfo loader_create_info;
3583 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003584
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003585 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3586 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003587
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003588 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003589
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003590 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003591
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003592 chain_info.u.pLayerInfo = NULL;
3593 chain_info.pNext = pCreateInfo->pNext;
3594 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3595 chain_info.function = VK_LAYER_LINK_INFO;
3596 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003597
Jon Ashburn23d36b12016-02-02 17:47:28 -07003598 layer_instance_link_info = loader_stack_alloc(
3599 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003600 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003601 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3602 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003603 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003604 }
3605
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003606 /* Create instance chain of enabled layers */
3607 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003608 struct loader_layer_properties *layer_prop =
3609 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003610 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003611
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003612 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003613 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003614 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003615 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3616 NULL) {
3617 if (layer_prop->functions.str_gipa == NULL ||
3618 strlen(layer_prop->functions.str_gipa) == 0) {
3619 fpGIPA = (PFN_vkGetInstanceProcAddr)
3620 loader_platform_get_proc_address(
3621 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003622 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3623 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003624 fpGIPA = (PFN_vkGetInstanceProcAddr)
3625 loader_platform_get_proc_address(
3626 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003627 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003628 loader_log(
3629 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3630 "Failed to find vkGetInstanceProcAddr in layer %s",
3631 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003632 continue;
3633 }
3634 }
3635
Jon Ashburn23d36b12016-02-02 17:47:28 -07003636 layer_instance_link_info[activated_layers].pNext =
3637 chain_info.u.pLayerInfo;
3638 layer_instance_link_info[activated_layers]
3639 .pfnNextGetInstanceProcAddr = nextGIPA;
3640 chain_info.u.pLayerInfo =
3641 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003642 nextGIPA = fpGIPA;
3643
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003644 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003645 "Insert instance layer %s (%s)",
3646 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003647
3648 activated_layers++;
3649 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003650 }
3651
Jon Ashburn23d36b12016-02-02 17:47:28 -07003652 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003653 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003654 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003655 VkLayerInstanceCreateInfo create_info_disp;
3656
Jon Ashburncc407a22016-04-15 09:25:03 -06003657 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003658 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003659
3660 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3661
3662 create_info_disp.pNext = loader_create_info.pNext;
3663 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003664 res =
3665 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003666 } else {
3667 // Couldn't find CreateInstance function!
3668 res = VK_ERROR_INITIALIZATION_FAILED;
3669 }
3670
3671 if (res != VK_SUCCESS) {
3672 // TODO: Need to clean up here
3673 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003674 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003675 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003676 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003677 }
3678
3679 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003680}
3681
Jon Ashburn23d36b12016-02-02 17:47:28 -07003682void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3683 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003684
Jon Ashburn23d36b12016-02-02 17:47:28 -07003685 loader_init_instance_extension_dispatch_table(
3686 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003687}
3688
Jon Ashburn1530c342016-02-26 13:14:27 -07003689VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003690loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3691 const VkDeviceCreateInfo *pCreateInfo,
3692 const VkAllocationCallbacks *pAllocator,
3693 const struct loader_instance *inst,
3694 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003695 uint32_t activated_layers = 0;
3696 VkLayerDeviceLink *layer_device_link_info;
3697 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003698 VkDeviceCreateInfo loader_create_info;
3699 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003700
Piers Daniell295fe402016-03-29 11:51:11 -06003701 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003702 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003703
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003704 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3705
Jon Ashburn23d36b12016-02-02 17:47:28 -07003706 layer_device_link_info = loader_stack_alloc(
3707 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003708 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003709 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3710 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003711 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003712 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003713
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003714 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003715 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3716 chain_info.function = VK_LAYER_LINK_INFO;
3717 chain_info.u.pLayerInfo = NULL;
3718 chain_info.pNext = pCreateInfo->pNext;
3719 loader_create_info.pNext = &chain_info;
3720
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003721 /* Create instance chain of enabled layers */
3722 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003723 struct loader_layer_properties *layer_prop =
3724 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003725 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003726
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003727 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003728 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003729 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003730 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3731 NULL) {
3732 if (layer_prop->functions.str_gipa == NULL ||
3733 strlen(layer_prop->functions.str_gipa) == 0) {
3734 fpGIPA = (PFN_vkGetInstanceProcAddr)
3735 loader_platform_get_proc_address(
3736 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003737 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3738 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003739 fpGIPA = (PFN_vkGetInstanceProcAddr)
3740 loader_platform_get_proc_address(
3741 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003742 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003743 loader_log(
3744 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3745 "Failed to find vkGetInstanceProcAddr in layer %s",
3746 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003747 continue;
3748 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003749 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003750 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003751 if (layer_prop->functions.str_gdpa == NULL ||
3752 strlen(layer_prop->functions.str_gdpa) == 0) {
3753 fpGDPA = (PFN_vkGetDeviceProcAddr)
3754 loader_platform_get_proc_address(lib_handle,
3755 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003756 layer_prop->functions.get_device_proc_addr = fpGDPA;
3757 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003758 fpGDPA = (PFN_vkGetDeviceProcAddr)
3759 loader_platform_get_proc_address(
3760 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003761 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003762 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003763 "Failed to find vkGetDeviceProcAddr in layer %s",
3764 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003765 continue;
3766 }
3767 }
3768
Jon Ashburn23d36b12016-02-02 17:47:28 -07003769 layer_device_link_info[activated_layers].pNext =
3770 chain_info.u.pLayerInfo;
3771 layer_device_link_info[activated_layers]
3772 .pfnNextGetInstanceProcAddr = nextGIPA;
3773 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3774 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003775 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3776 nextGIPA = fpGIPA;
3777 nextGDPA = fpGDPA;
3778
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003779 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003780 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003781 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003782
3783 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003784 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003785 }
3786
Jon Ashburncc407a22016-04-15 09:25:03 -06003787 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003788 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003789 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003790 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003791 VkLayerDeviceCreateInfo create_info_disp;
3792
Jon Ashburncc407a22016-04-15 09:25:03 -06003793 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003794 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3795
3796 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3797
3798 create_info_disp.pNext = loader_create_info.pNext;
3799 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003800 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003801 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003802 if (res != VK_SUCCESS) {
3803 return res;
3804 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003805 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003806 } else {
3807 // Couldn't find CreateDevice function!
3808 return VK_ERROR_INITIALIZATION_FAILED;
3809 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003810
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003811 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003812 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003813 dev->device);
3814
3815 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003816}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003817
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003818VkResult loader_validate_layers(const struct loader_instance *inst,
3819 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003820 const char *const *ppEnabledLayerNames,
3821 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003822 struct loader_layer_properties *prop;
3823
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003824 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003825 VkStringErrorFlags result =
3826 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003827 if (result != VK_STRING_ERROR_NONE) {
3828 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003829 "Loader: Device ppEnabledLayerNames contains string "
3830 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003831 return VK_ERROR_LAYER_NOT_PRESENT;
3832 }
3833
Jon Ashburn23d36b12016-02-02 17:47:28 -07003834 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003835 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003836 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003837 }
3838 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003839 return VK_SUCCESS;
3840}
3841
3842VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003843 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003844 const struct loader_extension_list *icd_exts,
3845 const struct loader_layer_list *instance_layer,
3846 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003847
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003848 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003849 struct loader_layer_properties *layer_prop;
3850
Jon Ashburnf19916e2016-01-11 13:12:43 -07003851 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003852 VkStringErrorFlags result = vk_string_validate(
3853 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003854 if (result != VK_STRING_ERROR_NONE) {
3855 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003856 "Loader: Instance ppEnabledExtensionNames contains "
3857 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003858 return VK_ERROR_EXTENSION_NOT_PRESENT;
3859 }
3860
Jon Ashburn23d36b12016-02-02 17:47:28 -07003861 extension_prop = get_extension_property(
3862 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003863
3864 if (extension_prop) {
3865 continue;
3866 }
3867
3868 extension_prop = NULL;
3869
3870 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07003871 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003872 layer_prop = loader_get_layer_property(
3873 pCreateInfo->ppEnabledLayerNames[i], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003874 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06003875 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003876 * should have already filtered this case out.
3877 */
3878 continue;
3879 }
3880
Jon Ashburn23d36b12016-02-02 17:47:28 -07003881 extension_prop =
3882 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
3883 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003884 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003885 /* Found the extension in one of the layers enabled by the app.
3886 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003887 break;
3888 }
3889 }
3890
3891 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003892 /* Didn't find extension name in any of the global layers, error out
3893 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003894 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003895 }
3896 }
3897 return VK_SUCCESS;
3898}
3899
3900VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06003901 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003902 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07003903 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003904 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003905 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003906 struct loader_layer_properties *layer_prop;
3907
Jon Ashburnf19916e2016-01-11 13:12:43 -07003908 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003909
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003910 VkStringErrorFlags result = vk_string_validate(
3911 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003912 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003913 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
3914 0, "Loader: Device ppEnabledExtensionNames contains "
3915 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003916 return VK_ERROR_EXTENSION_NOT_PRESENT;
3917 }
3918
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003919 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07003920 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003921
3922 if (extension_prop) {
3923 continue;
3924 }
3925
Jon Ashburn471f44c2016-01-13 12:51:43 -07003926 /* Not in global list, search activated layer extension lists */
3927 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
3928 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003929
Jon Ashburn23d36b12016-02-02 17:47:28 -07003930 extension_prop = get_dev_extension_property(
3931 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003932 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003933 /* Found the extension in one of the layers enabled by the app.
3934 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003935 break;
3936 }
3937 }
3938
3939 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003940 /* Didn't find extension name in any of the device layers, error out
3941 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003942 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003943 }
3944 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003945 return VK_SUCCESS;
3946}
3947
Jon Ashburn1530c342016-02-26 13:14:27 -07003948/**
3949 * Terminator functions for the Instance chain
3950 * All named terminator_<Vulakn API name>
3951 */
Mark Young0ad83132016-06-30 13:02:42 -06003952VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
3953 const VkInstanceCreateInfo *pCreateInfo,
3954 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Jon Ashburn46888392015-01-29 15:45:51 -07003955 struct loader_icd *icd;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003956 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003957 char **filtered_extension_names = NULL;
3958 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003959 VkResult res = VK_SUCCESS;
Jon Ashburn7e18de02015-11-19 09:29:51 -07003960 bool success = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07003961
Jon Ashburncc407a22016-04-15 09:25:03 -06003962 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07003963 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
3964
Jon Ashburnf19916e2016-01-11 13:12:43 -07003965 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003966 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003967
3968 /*
3969 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003970 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003971 * No ICD will advertise support for layers. An ICD
3972 * library could support a layer, but it would be
3973 * independent of the actual ICD, just in the same library.
3974 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003975 filtered_extension_names =
3976 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003977 if (!filtered_extension_names) {
3978 return VK_ERROR_OUT_OF_HOST_MEMORY;
3979 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003980 icd_create_info.ppEnabledExtensionNames =
3981 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003982
Jon Ashburn8810c5f2015-08-18 18:04:47 -06003983 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
3984 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06003985 if (NULL == icd) {
3986 while (NULL != ptr_instance->icds) {
3987 icd = ptr_instance->icds;
3988 ptr_instance->icds = icd->next;
3989 icd->DestroyInstance(icd->instance, pAllocator);
3990 loader_icd_destroy(ptr_instance, icd, pAllocator);
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003991 }
Mark Young0ad83132016-06-30 13:02:42 -06003992 return VK_ERROR_OUT_OF_HOST_MEMORY;
3993 }
3994 icd_create_info.enabledExtensionCount = 0;
3995 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003996
Mark Young0ad83132016-06-30 13:02:42 -06003997 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3998 "Build ICD instance extension list");
3999 // traverse scanned icd list adding non-duplicate extensions to the
4000 // list
4001 loader_init_generic_list(ptr_instance,
4002 (struct loader_generic_list *)&icd_exts,
4003 sizeof(VkExtensionProperties));
4004 loader_add_instance_extensions(
4005 ptr_instance,
4006 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
4007 icd->this_icd_lib->lib_name, &icd_exts);
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004008
Mark Young0ad83132016-06-30 13:02:42 -06004009 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4010 prop = get_extension_property(
4011 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4012 if (prop) {
4013 filtered_extension_names[icd_create_info
4014 .enabledExtensionCount] =
4015 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4016 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004017 }
4018 }
Mark Young0ad83132016-06-30 13:02:42 -06004019
4020 loader_destroy_generic_list(ptr_instance,
4021 (struct loader_generic_list *)&icd_exts);
4022
4023 res = ptr_instance->icd_libs.list[i].CreateInstance(
4024 &icd_create_info, pAllocator, &(icd->instance));
4025 if (res == VK_SUCCESS)
4026 success = loader_icd_init_entrys(
4027 icd, icd->instance,
4028 ptr_instance->icd_libs.list[i].GetInstanceProcAddr);
4029
4030 if (res != VK_SUCCESS || !success) {
4031 ptr_instance->icds = ptr_instance->icds->next;
4032 loader_icd_destroy(ptr_instance, icd, pAllocator);
4033 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4034 "ICD ignored: failed to CreateInstance and find "
4035 "entrypoints with ICD");
4036 }
Jon Ashburn46888392015-01-29 15:45:51 -07004037 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004038
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004039 /*
4040 * If no ICDs were added to instance list and res is unchanged
4041 * from it's initial value, the loader was unable to find
4042 * a suitable ICD.
4043 */
Ian Elliotteb450762015-02-05 15:19:15 -07004044 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004045 if (res == VK_SUCCESS) {
4046 return VK_ERROR_INCOMPATIBLE_DRIVER;
4047 } else {
4048 return res;
4049 }
Ian Elliotteb450762015-02-05 15:19:15 -07004050 }
Jon Ashburn46888392015-01-29 15:45:51 -07004051
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004052 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004053}
4054
Mark Young0ad83132016-06-30 13:02:42 -06004055VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4056 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004057 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004058 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06004059 struct loader_icd *next_icd;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004060
4061 // Remove this instance from the list of instances:
4062 struct loader_instance *prev = NULL;
4063 struct loader_instance *next = loader.instances;
4064 while (next != NULL) {
4065 if (next == ptr_instance) {
4066 // Remove this instance from the list:
4067 if (prev)
4068 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004069 else
4070 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004071 break;
4072 }
4073 prev = next;
4074 next = next->next;
4075 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004076
Jon Ashburn3da71f22015-05-14 12:43:38 -06004077 while (icds) {
4078 if (icds->instance) {
Chia-I Wuf7458c52015-10-26 21:10:41 +08004079 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004080 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06004081 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004082 icds->instance = VK_NULL_HANDLE;
Mark Young0ad83132016-06-30 13:02:42 -06004083 loader_icd_destroy(ptr_instance, icds, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004084
4085 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07004086 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004087
Jon Ashburn23d36b12016-02-02 17:47:28 -07004088 loader_delete_layer_properties(ptr_instance,
4089 &ptr_instance->instance_layer_list);
Jon Ashburne39a4f82015-08-28 13:38:21 -06004090 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004091 loader_destroy_generic_list(
4092 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004093 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004094 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004095 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004096}
4097
Mark Young0ad83132016-06-30 13:02:42 -06004098VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4099 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4100 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4101 VkResult res = VK_SUCCESS;
Jon Ashburn1530c342016-02-26 13:14:27 -07004102 struct loader_physical_device *phys_dev;
Jon Ashburn014438f2016-03-01 19:51:07 -07004103 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004104
Jon Ashburncc407a22016-04-15 09:25:03 -06004105 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn72690f22016-03-29 12:52:13 -06004106 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004107 struct loader_extension_list icd_exts;
4108
4109 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004110
Jon Ashburn1530c342016-02-26 13:14:27 -07004111 if (fpCreateDevice == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06004112 res = VK_ERROR_INITIALIZATION_FAILED;
4113 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004114 }
4115
Jon Ashburn1530c342016-02-26 13:14:27 -07004116 VkDeviceCreateInfo localCreateInfo;
4117 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004118
4119 /*
4120 * NOTE: Need to filter the extensions to only those
4121 * supported by the ICD.
4122 * No ICD will advertise support for layers. An ICD
4123 * library could support a layer, but it would be
4124 * independent of the actual ICD, just in the same library.
4125 */
4126 char **filtered_extension_names = NULL;
4127 filtered_extension_names =
4128 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4129 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004130 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004131 }
4132
Jon Ashburn1530c342016-02-26 13:14:27 -07004133 localCreateInfo.enabledLayerCount = 0;
4134 localCreateInfo.ppEnabledLayerNames = NULL;
4135
4136 localCreateInfo.enabledExtensionCount = 0;
4137 localCreateInfo.ppEnabledExtensionNames =
4138 (const char *const *)filtered_extension_names;
4139
Jon Ashburn014438f2016-03-01 19:51:07 -07004140 /* Get the physical device (ICD) extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004141 if (!loader_init_generic_list(phys_dev->this_icd->this_instance,
4142 (struct loader_generic_list *)&icd_exts,
4143 sizeof(VkExtensionProperties))) {
Mark Young0ad83132016-06-30 13:02:42 -06004144 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4145 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004146 }
4147
4148 res = loader_add_device_extensions(
Jon Ashburncc407a22016-04-15 09:25:03 -06004149 phys_dev->this_icd->this_instance,
4150 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn014438f2016-03-01 19:51:07 -07004151 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
4152 &icd_exts);
4153 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004154 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004155 }
4156
Jon Ashburn1530c342016-02-26 13:14:27 -07004157 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4158 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004159 VkExtensionProperties *prop =
4160 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004161 if (prop) {
4162 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4163 (char *)extension_name;
4164 localCreateInfo.enabledExtensionCount++;
4165 }
4166 }
4167
Jon Ashburn1530c342016-02-26 13:14:27 -07004168 // TODO: Why does fpCreateDevice behave differently than
4169 // this_icd->CreateDevice?
4170 // VkResult res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo,
4171 // pAllocator, &localDevice);
Jon Ashburn014438f2016-03-01 19:51:07 -07004172 res = phys_dev->this_icd->CreateDevice(phys_dev->phys_dev, &localCreateInfo,
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004173 pAllocator, &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004174
4175 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004176 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004177 }
4178
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004179 *pDevice = dev->device;
Jon Ashburncc407a22016-04-15 09:25:03 -06004180 loader_add_logical_device(phys_dev->this_icd->this_instance,
4181 phys_dev->this_icd, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004182
4183 /* Init dispatch pointer in new device object */
4184 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4185
Mark Young0ad83132016-06-30 13:02:42 -06004186out:
4187 if (NULL != icd_exts.list) {
4188 loader_destroy_generic_list(phys_dev->this_icd->this_instance,
4189 (struct loader_generic_list *)&icd_exts);
4190 }
4191
Jon Ashburn1530c342016-02-26 13:14:27 -07004192 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004193}
4194
Jon Ashburn23d36b12016-02-02 17:47:28 -07004195VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004196terminator_EnumeratePhysicalDevices(VkInstance instance,
4197 uint32_t *pPhysicalDeviceCount,
4198 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004199 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004200 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004201 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004202
Jon Ashburn014438f2016-03-01 19:51:07 -07004203 struct loader_icd *icd;
4204 struct loader_phys_dev_per_icd *phys_devs;
4205
4206 inst->total_gpu_count = 0;
4207 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4208 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4209 if (!phys_devs)
4210 return VK_ERROR_OUT_OF_HOST_MEMORY;
4211
4212 icd = inst->icds;
4213 for (i = 0; i < inst->total_icd_count; i++) {
4214 assert(icd);
4215 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
4216 NULL);
4217 if (res != VK_SUCCESS)
4218 return res;
4219 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004220 }
4221
Jon Ashburn014438f2016-03-01 19:51:07 -07004222 icd = inst->icds;
4223 for (i = 0; i < inst->total_icd_count; i++) {
4224 assert(icd);
4225 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4226 phys_devs[i].count * sizeof(VkPhysicalDevice));
4227 if (!phys_devs[i].phys_devs) {
4228 return VK_ERROR_OUT_OF_HOST_MEMORY;
4229 }
4230 res = icd->EnumeratePhysicalDevices(
4231 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
4232 if ((res == VK_SUCCESS)) {
4233 inst->total_gpu_count += phys_devs[i].count;
4234 } else {
4235 return res;
4236 }
4237 phys_devs[i].this_icd = icd;
4238 icd = icd->next;
4239 }
4240
4241 *pPhysicalDeviceCount = inst->total_gpu_count;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004242 if (!pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004243 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004244 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004245
Jon Ashburn014438f2016-03-01 19:51:07 -07004246 /* Initialize the output pPhysicalDevices with wrapped loader terminator
4247 * physicalDevice objects; save this list of wrapped objects in instance
4248 * struct for later cleanup and use by trampoline code */
4249 uint32_t j, idx = 0;
4250 uint32_t copy_count = 0;
4251
4252 copy_count = (inst->total_gpu_count < *pPhysicalDeviceCount)
4253 ? inst->total_gpu_count
Jon Ashburn23d36b12016-02-02 17:47:28 -07004254 : *pPhysicalDeviceCount;
Jon Ashburn014438f2016-03-01 19:51:07 -07004255
Jon Ashburn014438f2016-03-01 19:51:07 -07004256 if (inst->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004257 loader_instance_heap_free(inst, inst->phys_devs_term);
4258 inst->phys_devs_term = loader_instance_heap_alloc(
Jon Ashburn014438f2016-03-01 19:51:07 -07004259 inst, sizeof(struct loader_physical_device) * copy_count,
4260 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4261 if (!inst->phys_devs_term)
4262 return VK_ERROR_OUT_OF_HOST_MEMORY;
4263
4264 for (i = 0; idx < copy_count && i < inst->total_icd_count; i++) {
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004265
Jon Ashburn014438f2016-03-01 19:51:07 -07004266 for (j = 0; j < phys_devs[i].count && idx < copy_count; j++) {
4267 loader_set_dispatch((void *)&inst->phys_devs_term[idx], inst->disp);
4268 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
4269 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
4270 pPhysicalDevices[idx] =
4271 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4272 idx++;
4273 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004274 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004275 *pPhysicalDeviceCount = copy_count;
4276
Piers Daniell295fe402016-03-29 11:51:11 -06004277 // TODO: Is phys_devs being leaked?
4278
Jon Ashburn014438f2016-03-01 19:51:07 -07004279 if (copy_count < inst->total_gpu_count) {
4280 inst->total_gpu_count = copy_count;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004281 return VK_INCOMPLETE;
4282 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004283 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004284}
4285
Jon Ashburn1530c342016-02-26 13:14:27 -07004286VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4287 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004288 struct loader_physical_device *phys_dev =
4289 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004290 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004291
Tony Barbour59a47322015-06-24 16:06:58 -06004292 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004293 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004294}
4295
Jon Ashburn1530c342016-02-26 13:14:27 -07004296VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004297 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4298 VkQueueFamilyProperties *pProperties) {
4299 struct loader_physical_device *phys_dev =
4300 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004301 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004302
Cody Northropd0802882015-08-03 17:04:53 -06004303 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004304 icd->GetPhysicalDeviceQueueFamilyProperties(
4305 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004306}
4307
Jon Ashburn1530c342016-02-26 13:14:27 -07004308VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004309 VkPhysicalDevice physicalDevice,
4310 VkPhysicalDeviceMemoryProperties *pProperties) {
4311 struct loader_physical_device *phys_dev =
4312 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004313 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004314
4315 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004316 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004317}
4318
Jon Ashburn23d36b12016-02-02 17:47:28 -07004319VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004320terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4321 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004322 struct loader_physical_device *phys_dev =
4323 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004324 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004325
4326 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004327 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004328}
4329
Jon Ashburn23d36b12016-02-02 17:47:28 -07004330VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004331terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4332 VkFormat format,
4333 VkFormatProperties *pFormatInfo) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004334 struct loader_physical_device *phys_dev =
4335 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004336 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004337
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -06004338 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004339 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4340 pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004341}
4342
Jon Ashburn1530c342016-02-26 13:14:27 -07004343VKAPI_ATTR VkResult VKAPI_CALL
4344terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004345 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4346 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4347 VkImageFormatProperties *pImageFormatProperties) {
4348 struct loader_physical_device *phys_dev =
4349 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004350 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn754864f2015-07-23 18:49:07 -06004351
Chia-I Wu17241042015-10-31 00:31:16 +08004352 if (!icd->GetPhysicalDeviceImageFormatProperties)
4353 return VK_ERROR_INITIALIZATION_FAILED;
4354
Jon Ashburn23d36b12016-02-02 17:47:28 -07004355 return icd->GetPhysicalDeviceImageFormatProperties(
4356 phys_dev->phys_dev, format, type, tiling, usage, flags,
4357 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004358}
4359
Jon Ashburn1530c342016-02-26 13:14:27 -07004360VKAPI_ATTR void VKAPI_CALL
4361terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004362 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4363 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4364 VkImageTiling tiling, uint32_t *pNumProperties,
4365 VkSparseImageFormatProperties *pProperties) {
4366 struct loader_physical_device *phys_dev =
4367 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004368 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004369
4370 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004371 icd->GetPhysicalDeviceSparseImageFormatProperties(
4372 phys_dev->phys_dev, format, type, samples, usage, tiling,
4373 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004374}
4375
Jon Ashburn1530c342016-02-26 13:14:27 -07004376VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4377 VkPhysicalDevice physicalDevice, const char *pLayerName,
4378 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburna760a512015-12-14 08:52:14 -07004379 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004380
Jon Ashburn471f44c2016-01-13 12:51:43 -07004381 struct loader_layer_list implicit_layer_list;
4382
Jon Ashburndc5d9202016-02-29 13:00:51 -07004383 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004384
Jon Ashburndc5d9202016-02-29 13:00:51 -07004385 /* Any layer or trampoline wrapping should be removed at this point in time
4386 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn014438f2016-03-01 19:51:07 -07004387 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004388
Jon Ashburndc5d9202016-02-29 13:00:51 -07004389 /* this case is during the call down the instance chain with pLayerName
4390 * == NULL*/
4391 struct loader_icd *icd = phys_dev->this_icd;
4392 uint32_t icd_ext_count = *pPropertyCount;
4393 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004394
Jon Ashburndc5d9202016-02-29 13:00:51 -07004395 /* get device extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004396 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4397 &icd_ext_count, pProperties);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004398 if (res != VK_SUCCESS)
4399 return res;
Jon Ashburn23d36b12016-02-02 17:47:28 -07004400
Jon Ashburn014438f2016-03-01 19:51:07 -07004401 loader_init_layer_list(icd->this_instance, &implicit_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004402
Jon Ashburndc5d9202016-02-29 13:00:51 -07004403 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004404 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4405 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004406 /* we need to determine which implicit layers are active,
4407 * and then add their extensions. This can't be cached as
4408 * it depends on results of environment variables (which can change).
4409 */
4410 if (pProperties != NULL) {
Jon Ashburn014438f2016-03-01 19:51:07 -07004411 struct loader_extension_list icd_exts;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004412 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn014438f2016-03-01 19:51:07 -07004413 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4414 icd_ext_count, pProperties,
4415 &icd_exts);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004416 if (res != VK_SUCCESS)
4417 return res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004418
Jon Ashburndc5d9202016-02-29 13:00:51 -07004419 /* we need to determine which implicit layers are active,
4420 * and then add their extensions. This can't be cached as
4421 * it depends on results of environment variables (which can
4422 * change).
4423 */
4424 struct loader_extension_list all_exts = {0};
Jon Ashburn014438f2016-03-01 19:51:07 -07004425 loader_add_to_ext_list(icd->this_instance, &all_exts, icd_exts.count,
4426 icd_exts.list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004427
Jon Ashburndc5d9202016-02-29 13:00:51 -07004428 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004429 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4430 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004431
Jon Ashburndc5d9202016-02-29 13:00:51 -07004432 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4433 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004434 j < implicit_layer_list.list[i].device_extension_list.count;
4435 j++) {
4436 loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4437 &implicit_layer_list.list[i]
4438 .device_extension_list.list[j]
4439 .props);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004440 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004441 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004442 uint32_t capacity = *pPropertyCount;
4443 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004444
Jon Ashburndc5d9202016-02-29 13:00:51 -07004445 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4446 props[i] = all_exts.list[i];
4447 }
4448 /* wasn't enough space for the extensions, we did partial copy now
4449 * return VK_INCOMPLETE */
4450 if (capacity < all_exts.count) {
4451 res = VK_INCOMPLETE;
4452 } else {
4453 *pPropertyCount = all_exts.count;
4454 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004455 loader_destroy_generic_list(icd->this_instance,
4456 (struct loader_generic_list *)&all_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004457 loader_destroy_generic_list(icd->this_instance,
4458 (struct loader_generic_list *)&icd_exts);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004459 } else {
4460 /* just return the count; need to add in the count of implicit layer
4461 * extensions
4462 * don't worry about duplicates being added in the count */
4463 *pPropertyCount = icd_ext_count;
4464
4465 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4466 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004467 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004468 }
4469 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004470 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004471
4472 loader_destroy_generic_list(
Jon Ashburn014438f2016-03-01 19:51:07 -07004473 icd->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004474 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004475}
4476
Jon Ashburn23d36b12016-02-02 17:47:28 -07004477VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004478terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4479 uint32_t *pPropertyCount,
4480 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004481
Jon Ashburndc5d9202016-02-29 13:00:51 -07004482 // should never get here this call isn't dispatched down the chain
4483 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004484}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004485
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004486VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004487 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004488 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004489 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004490
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004491 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004492 if (utf8[i] == 0) {
4493 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004494 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004495 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004496 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004497 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004498 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004499 num_char_bytes = 2;
4500 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4501 num_char_bytes = 3;
4502 } else {
4503 result = VK_STRING_ERROR_BAD_DATA;
4504 }
4505
4506 // Validate the following num_char_bytes of data
4507 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4508 if (++i == max_length) {
4509 result |= VK_STRING_ERROR_LENGTH;
4510 break;
4511 }
4512 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4513 result |= VK_STRING_ERROR_BAD_DATA;
4514 }
4515 }
4516 }
4517 return result;
4518}