blob: 6b22893557117f8897bae498ffd917b9816f1cb3 [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
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002495 * directory to look at. It is expanded into the dir path
2496 * $XDG_DATA_HOME/home_location or $HOME/.local/share/home_location depending
2497 * on environment variables. This "home_location" is only used on Linux.
Jon Ashburn2077e382015-06-29 11:25:34 -06002498 *
2499 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002500 * VKResult
Jon Ashburn2077e382015-06-29 11:25:34 -06002501 * A string list of manifest files to be opened in out_files param.
2502 * List has a pointer to string for each manifest filename.
2503 * When done using the list in out_files, pointers should be freed.
Jon Ashburn23d36b12016-02-02 17:47:28 -07002504 * Location or override string lists can be either files or directories as
2505 *follows:
Jon Ashburnffad94d2015-06-30 14:46:22 -07002506 * | location | override
2507 * --------------------------------
2508 * Win ICD | files | files
2509 * Win Layer | files | dirs
2510 * Linux ICD | dirs | files
2511 * Linux Layer| dirs | dirs
Jon Ashburn2077e382015-06-29 11:25:34 -06002512 */
Mark Young0ad83132016-06-30 13:02:42 -06002513static VkResult loader_get_manifest_files(
2514 const struct loader_instance *inst, const char *env_override,
2515 char *source_override, bool is_layer, const char *location,
2516 const char *home_location, struct loader_manifest_files *out_files) {
2517 char * override = NULL;
2518 char *loc, *orig_loc = NULL;
2519 char *reg = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002520 char *file, *next_file, *name;
2521 size_t alloced_count = 64;
2522 char full_path[2048];
2523 DIR *sysdir = NULL;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002524 bool list_is_dirs = false;
Jon Ashburn2077e382015-06-29 11:25:34 -06002525 struct dirent *dent;
Mark Young0ad83132016-06-30 13:02:42 -06002526 VkResult res = VK_SUCCESS;
Jon Ashburn2077e382015-06-29 11:25:34 -06002527
2528 out_files->count = 0;
2529 out_files->filename_list = NULL;
2530
Jamie Madill00c3c912016-04-06 18:26:46 -04002531 if (source_override != NULL) {
2532 override = source_override;
Mark Young0ad83132016-06-30 13:02:42 -06002533 } else if (env_override != NULL &&
2534 (override = loader_getenv(env_override, inst))) {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002535#if !defined(_WIN32)
Jon Ashburncc407a22016-04-15 09:25:03 -06002536 if (geteuid() != getuid() || getegid() != getgid()) {
Jon Ashburnffad94d2015-06-30 14:46:22 -07002537 /* Don't allow setuid apps to use the env var: */
Mark Young0ad83132016-06-30 13:02:42 -06002538 loader_free_getenv(override, inst);
Jon Ashburn2077e382015-06-29 11:25:34 -06002539 override = NULL;
2540 }
2541#endif
2542 }
2543
Jon Ashburnb6822212016-02-16 15:34:16 -07002544#if !defined(_WIN32)
2545 if (location == NULL && home_location == NULL) {
2546#else
2547 home_location = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002548 if (location == NULL) {
Jon Ashburnb6822212016-02-16 15:34:16 -07002549#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002550 loader_log(
2551 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnffad94d2015-06-30 14:46:22 -07002552 "Can't get manifest files with NULL location, env_override=%s",
2553 env_override);
Mark Young0ad83132016-06-30 13:02:42 -06002554 res = VK_ERROR_INITIALIZATION_FAILED;
2555 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002556 }
2557
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002558#if defined(_WIN32)
Jon Ashburnffad94d2015-06-30 14:46:22 -07002559 list_is_dirs = (is_layer && override != NULL) ? true : false;
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002560#else
2561 list_is_dirs = (override == NULL || is_layer) ? true : false;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002562#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002563 // Make a copy of the input we are using so it is not modified
Jon Ashburnffad94d2015-06-30 14:46:22 -07002564 // Also handle getting the location(s) from registry on Windows
2565 if (override == NULL) {
Jon Ashburn3b78e462015-07-31 10:11:24 -06002566 loc = loader_stack_alloc(strlen(location) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002567 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002568 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2569 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002570 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2571 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002572 }
2573 strcpy(loc, location);
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002574#if defined(_WIN32)
Mark Young0ad83132016-06-30 13:02:42 -06002575 reg = loader_get_registry_files(inst, loc);
2576 if (reg == NULL) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002577 if (!is_layer) {
2578 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2579 "Registry lookup failed can't get ICD manifest "
2580 "files, do you have a Vulkan driver installed");
Mark Young0ad83132016-06-30 13:02:42 -06002581 // This typically only fails when out of memory, which is
2582 // critical
2583 // if this is for the loader.
2584 res = VK_ERROR_OUT_OF_HOST_MEMORY;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002585 } else {
2586 // warning only for layers
2587 loader_log(
2588 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
2589 "Registry lookup failed can't get layer manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002590 // Return success for now since it's not critical for layers
2591 res = VK_SUCCESS;
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002592 }
Mark Young0ad83132016-06-30 13:02:42 -06002593 goto out;
Jon Ashburn24265ac2015-07-31 09:33:21 -06002594 }
Mark Young0ad83132016-06-30 13:02:42 -06002595 orig_loc = loc;
2596 loc = reg;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002597#endif
Jon Ashburn23d36b12016-02-02 17:47:28 -07002598 } else {
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06002599 loc = loader_stack_alloc(strlen(override) + 1);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002600 if (loc == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002601 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2602 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002603 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2604 goto out;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002605 }
2606 strcpy(loc, override);
Jamie Madill00c3c912016-04-06 18:26:46 -04002607 if (source_override == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002608 loader_free_getenv(override, inst);
Jamie Madill00c3c912016-04-06 18:26:46 -04002609 }
Jon Ashburnffad94d2015-06-30 14:46:22 -07002610 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002611
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002612 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002613 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2614 "Searching the following paths for manifest files: %s\n", loc);
Liam Middlebrook9b14e892015-07-23 18:32:20 -07002615
Jon Ashburn2077e382015-06-29 11:25:34 -06002616 file = loc;
2617 while (*file) {
2618 next_file = loader_get_next_path(file);
Jon Ashburnffad94d2015-06-30 14:46:22 -07002619 if (list_is_dirs) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002620 sysdir = opendir(file);
2621 name = NULL;
2622 if (sysdir) {
2623 dent = readdir(sysdir);
2624 if (dent == NULL)
2625 break;
2626 name = &(dent->d_name[0]);
2627 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2628 name = full_path;
2629 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002630 } else {
Johannes van Waveren9bd805012015-10-28 11:45:00 -05002631#if defined(_WIN32)
2632 name = file;
2633#else
Jon Ashburnffad94d2015-06-30 14:46:22 -07002634 // only Linux has relative paths
Jon Ashburn2077e382015-06-29 11:25:34 -06002635 char *dir;
2636 // make a copy of location so it isn't modified
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002637 dir = loader_stack_alloc(strlen(loc) + 1);
Jon Ashburn2077e382015-06-29 11:25:34 -06002638 if (dir == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002639 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2640 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002641 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002642 }
Jason Ekstrandcc7550e2015-10-10 08:33:37 -07002643 strcpy(dir, loc);
Jon Ashburn2077e382015-06-29 11:25:34 -06002644
2645 loader_get_fullpath(file, dir, sizeof(full_path), full_path);
2646
2647 name = full_path;
Jon Ashburnffad94d2015-06-30 14:46:22 -07002648#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002649 }
2650 while (name) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002651 /* Look for files ending with ".json" suffix */
2652 uint32_t nlen = (uint32_t)strlen(name);
2653 const char *suf = name + nlen - 5;
2654 if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
2655 if (out_files->count == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06002656 out_files->filename_list = loader_instance_heap_alloc(
2657 inst, alloced_count * sizeof(char *),
2658 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002659 } else if (out_files->count == alloced_count) {
Mark Young0ad83132016-06-30 13:02:42 -06002660 out_files->filename_list = loader_instance_heap_realloc(
2661 inst, out_files->filename_list,
2662 alloced_count * sizeof(char *),
2663 alloced_count * sizeof(char *) * 2,
2664 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002665 alloced_count *= 2;
Jon Ashburn2077e382015-06-29 11:25:34 -06002666 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002667 if (out_files->filename_list == NULL) {
2668 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2669 "Out of memory can't alloc manifest file list");
Mark Young0ad83132016-06-30 13:02:42 -06002670 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2671 goto out;
Jon Ashburn2077e382015-06-29 11:25:34 -06002672 }
Mark Young0ad83132016-06-30 13:02:42 -06002673 out_files->filename_list[out_files->count] =
2674 loader_instance_heap_alloc(
2675 inst, strlen(name) + 1,
2676 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002677 if (out_files->filename_list[out_files->count] == NULL) {
2678 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2679 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002680 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2681 goto out;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002682 }
2683 strcpy(out_files->filename_list[out_files->count], name);
2684 out_files->count++;
2685 } else if (!list_is_dirs) {
2686 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002687 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002688 "Skipping manifest file %s, file name must end in .json",
2689 name);
2690 }
2691 if (list_is_dirs) {
2692 dent = readdir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002693 if (dent == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002694 break;
Mark Young0ad83132016-06-30 13:02:42 -06002695 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002696 name = &(dent->d_name[0]);
2697 loader_get_fullpath(name, file, sizeof(full_path), full_path);
2698 name = full_path;
2699 } else {
2700 break;
2701 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002702 }
Mark Young0ad83132016-06-30 13:02:42 -06002703 if (sysdir) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002704 closedir(sysdir);
Mark Young0ad83132016-06-30 13:02:42 -06002705 sysdir = NULL;
2706 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002707 file = next_file;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002708#if !defined(_WIN32)
Jon Ashburn1530c342016-02-26 13:14:27 -07002709 if (home_location != NULL &&
2710 (next_file == NULL || *next_file == '\0') && override == NULL) {
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002711 char *xdgdatahome = secure_getenv("XDG_DATA_HOME");
2712 size_t len;
2713 if (xdgdatahome != NULL) {
2714
2715 char *home_loc = loader_stack_alloc(strlen(xdgdatahome) + 2 +
Jon Ashburn67e262e2016-02-18 12:45:39 -07002716 strlen(home_location));
2717 if (home_loc == NULL) {
2718 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn1530c342016-02-26 13:14:27 -07002719 "Out of memory can't get manifest files");
Mark Young0ad83132016-06-30 13:02:42 -06002720 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2721 goto out;
Jon Ashburn67e262e2016-02-18 12:45:39 -07002722 }
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002723 strcpy(home_loc, xdgdatahome);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002724 // Add directory separator if needed
2725 if (home_location[0] != DIRECTORY_SYMBOL) {
2726 len = strlen(home_loc);
2727 home_loc[len] = DIRECTORY_SYMBOL;
Jon Ashburn1530c342016-02-26 13:14:27 -07002728 home_loc[len + 1] = '\0';
Jon Ashburn67e262e2016-02-18 12:45:39 -07002729 }
2730 strcat(home_loc, home_location);
2731 file = home_loc;
2732 next_file = loader_get_next_path(file);
2733 home_location = NULL;
2734
Jon Ashburn1530c342016-02-26 13:14:27 -07002735 loader_log(
2736 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002737 "Searching the following path for manifest files: %s\n",
Jon Ashburn1530c342016-02-26 13:14:27 -07002738 home_loc);
Jon Ashburn67e262e2016-02-18 12:45:39 -07002739 list_is_dirs = true;
John Drinkwater9ac3f4f2016-08-01 17:00:00 +01002740
2741 } else {
2742
2743 char *home = secure_getenv("HOME");
2744 if (home != NULL) {
2745 char *home_loc = loader_stack_alloc(strlen(home) + 16 +
2746 strlen(home_location));
2747 if (home_loc == NULL) {
2748 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
2749 "Out of memory can't get manifest files");
2750 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2751 goto out;
2752 }
2753 strcpy(home_loc, home);
2754
2755 len = strlen(home);
2756 if (home[len] != DIRECTORY_SYMBOL) {
2757 home_loc[len] = DIRECTORY_SYMBOL;
2758 home_loc[len + 1] = '\0';
2759 }
2760 strcat(home_loc, ".local/share");
2761
2762 if (home_location[0] != DIRECTORY_SYMBOL) {
2763 len = strlen(home_loc);
2764 home_loc[len] = DIRECTORY_SYMBOL;
2765 home_loc[len + 1] = '\0';
2766 }
2767 strcat(home_loc, home_location);
2768 file = home_loc;
2769 next_file = loader_get_next_path(file);
2770 home_location = NULL;
2771
2772 loader_log(
2773 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
2774 "Searching the following path for manifest files: %s\n",
2775 home_loc);
2776 list_is_dirs = true;
2777 } else {
2778 // without knowing HOME, we just.. give up
2779 }
Jon Ashburn67e262e2016-02-18 12:45:39 -07002780 }
2781 }
2782#endif
Jon Ashburn2077e382015-06-29 11:25:34 -06002783 }
Mark Young0ad83132016-06-30 13:02:42 -06002784
2785out:
2786 if (VK_SUCCESS != res && NULL != out_files->filename_list) {
2787 for (uint32_t remove = 0; remove < out_files->count; remove++) {
2788 loader_instance_heap_free(inst, out_files->filename_list[remove]);
2789 }
2790 loader_instance_heap_free(inst, out_files->filename_list);
2791 out_files->count = 0;
2792 out_files->filename_list = NULL;
2793 }
2794
2795 if (NULL != sysdir) {
2796 closedir(sysdir);
2797 }
2798
2799 if (NULL != reg && reg != orig_loc) {
2800 loader_instance_heap_free(inst, reg);
2801 }
2802 return res;
Jon Ashburn2077e382015-06-29 11:25:34 -06002803}
2804
Jon Ashburn23d36b12016-02-02 17:47:28 -07002805void loader_init_icd_lib_list() {}
Jon Ashburn8810c5f2015-08-18 18:04:47 -06002806
Jon Ashburn23d36b12016-02-02 17:47:28 -07002807void loader_destroy_icd_lib_list() {}
Jon Ashburn2077e382015-06-29 11:25:34 -06002808/**
2809 * Try to find the Vulkan ICD driver(s).
2810 *
2811 * This function scans the default system loader path(s) or path
2812 * specified by the \c VK_ICD_FILENAMES environment variable in
2813 * order to find loadable VK ICDs manifest files. From these
2814 * manifest files it finds the ICD libraries.
2815 *
2816 * \returns
Mark Young0ad83132016-06-30 13:02:42 -06002817 * Vulkan result
2818 * (on result == VK_SUCCESS) a list of icds that were discovered
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -06002819 */
Mark Young0ad83132016-06-30 13:02:42 -06002820VkResult loader_icd_scan(const struct loader_instance *inst,
2821 struct loader_icd_libs *icds) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002822 char *file_str;
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002823 uint16_t file_major_vers = 0;
2824 uint16_t file_minor_vers = 0;
2825 uint16_t file_patch_vers = 0;
2826 char *vers_tok;
Jon Ashburn2077e382015-06-29 11:25:34 -06002827 struct loader_manifest_files manifest_files;
Mark Young0ad83132016-06-30 13:02:42 -06002828 VkResult res = VK_SUCCESS;
2829 bool lockedMutex = false;
2830 cJSON *json = NULL;
Jon Ashburn2077e382015-06-29 11:25:34 -06002831
Mark Young0ad83132016-06-30 13:02:42 -06002832 memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
2833
2834 res = loader_scanned_icd_init(inst, icds);
2835 if (VK_SUCCESS != res) {
2836 goto out;
2837 }
2838
Jon Ashburn2077e382015-06-29 11:25:34 -06002839 // Get a list of manifest files for ICDs
Mark Young0ad83132016-06-30 13:02:42 -06002840 res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
2841 DEFAULT_VK_DRIVERS_INFO,
2842 HOME_VK_DRIVERS_INFO, &manifest_files);
2843 if (VK_SUCCESS != res || manifest_files.count == 0) {
2844 goto out;
2845 }
Jon Ashburn6461ef22015-09-22 13:11:00 -06002846 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06002847 lockedMutex = true;
Jon Ashburn2077e382015-06-29 11:25:34 -06002848 for (uint32_t i = 0; i < manifest_files.count; i++) {
2849 file_str = manifest_files.filename_list[i];
Mark Young0ad83132016-06-30 13:02:42 -06002850 if (file_str == NULL) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002851 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002852 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002853
Courtney Goeltzenleuchter73477392015-12-03 13:48:01 -07002854 json = loader_get_json(inst, file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002855 if (!json) {
Jon Ashburnaa4ea472015-08-27 08:30:50 -06002856 continue;
Mark Young0ad83132016-06-30 13:02:42 -06002857 }
Jon Ashburn005617f2015-11-17 17:35:40 -07002858 cJSON *item, *itemICD;
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002859 item = cJSON_GetObjectItem(json, "file_format_version");
Jon Ashburn6461ef22015-09-22 13:11:00 -06002860 if (item == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06002861 res = VK_ERROR_INITIALIZATION_FAILED;
2862 goto out;
Jon Ashburn6461ef22015-09-22 13:11:00 -06002863 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002864 char *file_vers = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002865 if (NULL == file_vers) {
2866 // Only reason the print can fail is if there was an allocation
2867 // issue
2868 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2869 goto out;
2870 }
Mark Youngcbcbf892016-06-22 15:25:00 -06002871 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
2872 "Found manifest file %s, version %s", file_str, file_vers);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002873 // Get the major/minor/and patch as integers for easier comparison
2874 vers_tok = strtok(file_vers, ".\"\n\r");
2875 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002876 file_major_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002877 vers_tok = strtok(NULL, ".\"\n\r");
2878 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002879 file_minor_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002880 vers_tok = strtok(NULL, ".\"\n\r");
2881 if (NULL != vers_tok) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04002882 file_patch_vers = (uint16_t)atoi(vers_tok);
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002883 }
2884 }
2885 }
Mark Youngc3a6d2e2016-06-13 14:49:53 -06002886 if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002887 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Mark Young0ad83132016-06-30 13:02:42 -06002888 "Unexpected manifest file version (expected 1.0.0 or "
2889 "1.0.1), may "
Jon Ashburn23d36b12016-02-02 17:47:28 -07002890 "cause errors");
Mark Young0ad83132016-06-30 13:02:42 -06002891 cJSON_Free(file_vers);
Jon Ashburn005617f2015-11-17 17:35:40 -07002892 itemICD = cJSON_GetObjectItem(json, "ICD");
2893 if (itemICD != NULL) {
2894 item = cJSON_GetObjectItem(itemICD, "library_path");
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002895 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002896 char *temp = cJSON_Print(item);
Jon Ashburn86251302015-08-25 16:48:24 -06002897 if (!temp || strlen(temp) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002898 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002899 "Can't find \"library_path\" in ICD JSON file "
2900 "%s, skipping",
2901 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002902 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002903 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002904 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002905 continue;
Jon Ashburn2077e382015-06-29 11:25:34 -06002906 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07002907 // strip out extra quotes
Jon Ashburn86251302015-08-25 16:48:24 -06002908 temp[strlen(temp) - 1] = '\0';
2909 char *library_path = loader_stack_alloc(strlen(temp) + 1);
2910 strcpy(library_path, &temp[1]);
Mark Young0ad83132016-06-30 13:02:42 -06002911 cJSON_Free(temp);
Jon Ashburn86251302015-08-25 16:48:24 -06002912 if (!library_path || strlen(library_path) == 0) {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002913 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002914 "Can't find \"library_path\" in ICD JSON file "
2915 "%s, skipping",
2916 file_str);
Jon Ashburn86251302015-08-25 16:48:24 -06002917 cJSON_Delete(json);
Mark Young0ad83132016-06-30 13:02:42 -06002918 json = NULL;
Jon Ashburn86251302015-08-25 16:48:24 -06002919 continue;
2920 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04002921 char fullpath[MAX_STRING_SIZE];
Jon Ashburn86251302015-08-25 16:48:24 -06002922 // Print out the paths being searched if debugging is enabled
Jon Ashburn23d36b12016-02-02 17:47:28 -07002923 loader_log(
2924 inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jamie Madill2fcbd152016-04-27 16:33:23 -04002925 "Searching for ICD drivers named %s default dir %s\n",
2926 library_path, DEFAULT_VK_DRIVERS_PATH);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002927 if (loader_platform_is_path(library_path)) {
Jon Ashburn86251302015-08-25 16:48:24 -06002928 // a relative or absolute path
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002929 char *name_copy = loader_stack_alloc(strlen(file_str) + 1);
2930 char *rel_base;
Jon Ashburn86251302015-08-25 16:48:24 -06002931 strcpy(name_copy, file_str);
2932 rel_base = loader_platform_dirname(name_copy);
Jon Ashburn23d36b12016-02-02 17:47:28 -07002933 loader_expand_path(library_path, rel_base, sizeof(fullpath),
2934 fullpath);
Daniel Dadap00b4aba2015-09-30 11:50:51 -05002935 } else {
Jamie Madill2fcbd152016-04-27 16:33:23 -04002936 // a filename which is assumed in a system directory
2937 loader_get_fullpath(library_path, DEFAULT_VK_DRIVERS_PATH,
2938 sizeof(fullpath), fullpath);
Jon Ashburn86251302015-08-25 16:48:24 -06002939 }
Jon Ashburn005617f2015-11-17 17:35:40 -07002940
2941 uint32_t vers = 0;
2942 item = cJSON_GetObjectItem(itemICD, "api_version");
2943 if (item != NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002944 temp = cJSON_Print(item);
Mark Young0ad83132016-06-30 13:02:42 -06002945 if (NULL == temp) {
2946 // Only reason the print can fail is if there was an
2947 // allocation issue
2948 res = VK_ERROR_OUT_OF_HOST_MEMORY;
2949 goto out;
2950 }
Jon Ashburn005617f2015-11-17 17:35:40 -07002951 vers = loader_make_version(temp);
Mark Young0ad83132016-06-30 13:02:42 -06002952 cJSON_Free(temp);
Jon Ashburn005617f2015-11-17 17:35:40 -07002953 }
Jamie Madill2fcbd152016-04-27 16:33:23 -04002954 loader_scanned_icd_add(inst, icds, fullpath, vers);
Mark Young0ad83132016-06-30 13:02:42 -06002955 } else {
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002956 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002957 "Can't find \"library_path\" object in ICD JSON "
2958 "file %s, skipping",
2959 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002960 }
2961 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07002962 loader_log(
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07002963 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07002964 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
2965 file_str);
Mark Young0ad83132016-06-30 13:02:42 -06002966 }
Jon Ashburn2077e382015-06-29 11:25:34 -06002967
Mark Young0ad83132016-06-30 13:02:42 -06002968 cJSON_Delete(json);
2969 json = NULL;
2970 }
2971
2972out:
2973 if (NULL != json) {
Jon Ashburn2077e382015-06-29 11:25:34 -06002974 cJSON_Delete(json);
2975 }
Mark Young0ad83132016-06-30 13:02:42 -06002976 if (NULL != manifest_files.filename_list) {
2977 for (uint32_t i = 0; i < manifest_files.count; i++) {
2978 if (NULL != manifest_files.filename_list[i]) {
2979 loader_instance_heap_free(inst,
2980 manifest_files.filename_list[i]);
2981 }
2982 }
2983 loader_instance_heap_free(inst, manifest_files.filename_list);
2984 }
2985 if (lockedMutex) {
2986 loader_platform_thread_unlock_mutex(&loader_json_lock);
2987 }
2988 return res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08002989}
2990
Jon Ashburn23d36b12016-02-02 17:47:28 -07002991void loader_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06002992 struct loader_layer_list *instance_layers) {
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002993 char *file_str;
Jon Ashburn23d36b12016-02-02 17:47:28 -07002994 struct loader_manifest_files
2995 manifest_files[2]; // [0] = explicit, [1] = implicit
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06002996 cJSON *json;
Jon Ashburn075ce432015-12-17 17:38:24 -07002997 uint32_t implicit;
Mark Young0ad83132016-06-30 13:02:42 -06002998 bool lockedMutex = false;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06002999
Mark Young0ad83132016-06-30 13:02:42 -06003000 memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
3001
3002 // Get a list of manifest files for explicit layers
3003 if (VK_SUCCESS !=
3004 loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH,
3005 true, DEFAULT_VK_ELAYERS_INFO,
3006 HOME_VK_ELAYERS_INFO, &manifest_files[0])) {
3007 goto out;
3008 }
3009
3010 // Get a list of manifest files for any implicit layers
Jon Ashburn23d36b12016-02-02 17:47:28 -07003011 // Pass NULL for environment variable override - implicit layers are not
3012 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003013 if (VK_SUCCESS != loader_get_manifest_files(
3014 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
3015 HOME_VK_ILAYERS_INFO, &manifest_files[1])) {
3016 goto out;
3017 }
Jon Ashburn90c6a0e2015-06-04 15:30:58 -06003018
Mark Young0ad83132016-06-30 13:02:42 -06003019 // Make sure we have at least one layer, if not, go ahead and return
3020 if (manifest_files[0].count == 0 && manifest_files[1].count == 0) {
3021 goto out;
3022 }
3023
3024 // cleanup any previously scanned libraries
Jon Ashburne39a4f82015-08-28 13:38:21 -06003025 loader_delete_layer_properties(inst, instance_layers);
Jon Ashburnb2ef1372015-07-16 17:19:31 -06003026
Jon Ashburn6461ef22015-09-22 13:11:00 -06003027 loader_platform_thread_lock_mutex(&loader_json_lock);
Mark Young0ad83132016-06-30 13:02:42 -06003028 lockedMutex = true;
Jon Ashburn075ce432015-12-17 17:38:24 -07003029 for (implicit = 0; implicit < 2; implicit++) {
Jamie Madill970ebcf2016-07-06 11:19:42 -04003030 for (uint32_t i = 0; i < manifest_files[implicit].count; i++) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003031 file_str = manifest_files[implicit].filename_list[i];
3032 if (file_str == NULL)
3033 continue;
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06003034
Jon Ashburn075ce432015-12-17 17:38:24 -07003035 // parse file into JSON struct
3036 json = loader_get_json(inst, file_str);
3037 if (!json) {
3038 continue;
3039 }
3040
Jon Ashburn491cd042016-05-16 14:01:18 -06003041 loader_add_layer_properties(inst, instance_layers, json,
3042 (implicit == 1), file_str);
Jon Ashburn075ce432015-12-17 17:38:24 -07003043 cJSON_Delete(json);
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003044 }
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003045 }
Jon Ashburn86a527a2016-02-10 20:59:26 -07003046
3047 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003048 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3049 sizeof(std_validation_names[0]),
3050 std_validation_names, instance_layers);
Jon Ashburn86a527a2016-02-10 20:59:26 -07003051
Mark Young0ad83132016-06-30 13:02:42 -06003052out:
3053
3054 for (uint32_t manFile = 0; manFile < 2; manFile++) {
3055 if (NULL != manifest_files[manFile].filename_list) {
3056 for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
3057 if (NULL != manifest_files[manFile].filename_list[i]) {
3058 loader_instance_heap_free(
3059 inst, manifest_files[manFile].filename_list[i]);
3060 }
3061 }
3062 loader_instance_heap_free(inst,
3063 manifest_files[manFile].filename_list);
3064 }
3065 }
3066 if (lockedMutex) {
3067 loader_platform_thread_unlock_mutex(&loader_json_lock);
3068 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003069}
3070
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003071void loader_implicit_layer_scan(const struct loader_instance *inst,
Jon Ashburn491cd042016-05-16 14:01:18 -06003072 struct loader_layer_list *instance_layers) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003073 char *file_str;
3074 struct loader_manifest_files manifest_files;
3075 cJSON *json;
3076 uint32_t i;
3077
3078 // Pass NULL for environment variable override - implicit layers are not
3079 // overridden by LAYERS_PATH_ENV
Mark Young0ad83132016-06-30 13:02:42 -06003080 VkResult res = loader_get_manifest_files(
3081 inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
3082 &manifest_files);
3083 if (VK_SUCCESS != res || manifest_files.count == 0) {
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003084 return;
3085 }
3086
3087 /* cleanup any previously scanned libraries */
3088 loader_delete_layer_properties(inst, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003089
3090 loader_platform_thread_lock_mutex(&loader_json_lock);
3091
3092 for (i = 0; i < manifest_files.count; i++) {
3093 file_str = manifest_files.filename_list[i];
3094 if (file_str == NULL) {
3095 continue;
3096 }
3097
3098 // parse file into JSON struct
3099 json = loader_get_json(inst, file_str);
3100 if (!json) {
3101 continue;
3102 }
3103
Mark Young0ad83132016-06-30 13:02:42 -06003104 loader_add_layer_properties(inst, instance_layers, json, true,
3105 file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003106
Mark Young0ad83132016-06-30 13:02:42 -06003107 loader_instance_heap_free(inst, file_str);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003108 cJSON_Delete(json);
3109 }
Mark Young0ad83132016-06-30 13:02:42 -06003110 loader_instance_heap_free(inst, manifest_files.filename_list);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003111
3112 // add a meta layer for validation if the validation layers are all present
Mark Young0ad83132016-06-30 13:02:42 -06003113 loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
3114 sizeof(std_validation_names[0]),
3115 std_validation_names, instance_layers);
Jeremy Hayes3e2bd5a2016-04-01 11:40:26 -06003116
3117 loader_platform_thread_unlock_mutex(&loader_json_lock);
3118}
3119
Jon Ashburn23d36b12016-02-02 17:47:28 -07003120static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
3121loader_gpa_instance_internal(VkInstance inst, const char *pName) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003122 if (!strcmp(pName, "vkGetInstanceProcAddr"))
Jon Ashburn23d36b12016-02-02 17:47:28 -07003123 return (void *)loader_gpa_instance_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003124 if (!strcmp(pName, "vkCreateInstance"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003125 return (void *)terminator_CreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003126 if (!strcmp(pName, "vkCreateDevice"))
Jon Ashburn1530c342016-02-26 13:14:27 -07003127 return (void *)terminator_CreateDevice;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003128
Jon Ashburn27cd5842015-05-12 17:26:48 -06003129 // inst is not wrapped
3130 if (inst == VK_NULL_HANDLE) {
3131 return NULL;
3132 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003133 VkLayerInstanceDispatchTable *disp_table =
3134 *(VkLayerInstanceDispatchTable **)inst;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003135 void *addr;
3136
3137 if (disp_table == NULL)
3138 return NULL;
3139
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003140 bool found_name;
Jon Ashburncc407a22016-04-15 09:25:03 -06003141 addr =
3142 loader_lookup_instance_dispatch_table(disp_table, pName, &found_name);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003143 if (found_name) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06003144 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003145 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003146
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003147 // Don't call down the chain, this would be an infinite loop
3148 loader_log(NULL, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
Jon Ashburncc407a22016-04-15 09:25:03 -06003149 "loader_gpa_instance_internal() unrecognized name %s", pName);
Jon Ashburnc7d3e732016-03-08 09:30:30 -07003150 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06003151}
3152
Piers Daniell295fe402016-03-29 11:51:11 -06003153static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
Jon Ashburncc407a22016-04-15 09:25:03 -06003154loader_gpa_device_internal(VkDevice device, const char *pName) {
3155 struct loader_device *dev;
3156 struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
Piers Daniell295fe402016-03-29 11:51:11 -06003157 return icd->GetDeviceProcAddr(device, pName);
3158}
3159
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003160/**
3161 * Initialize device_ext dispatch table entry as follows:
3162 * If dev == NULL find all logical devices created within this instance and
3163 * init the entry (given by idx) in the ext dispatch table.
3164 * If dev != NULL only initialize the entry in the given dev's dispatch table.
Jon Ashburn23d36b12016-02-02 17:47:28 -07003165 * The initialization value is gotten by calling down the device chain with
3166 * GDPA.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003167 * If GDPA returns NULL then don't initialize the dispatch table entry.
3168 */
3169static void loader_init_dispatch_dev_ext_entry(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003170 struct loader_device *dev,
3171 uint32_t idx,
3172 const char *funcName)
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003173
Jon Ashburn23d36b12016-02-02 17:47:28 -07003174{
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003175 void *gdpa_value;
3176 if (dev != NULL) {
3177 gdpa_value = dev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003178 dev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003179 if (gdpa_value != NULL)
Jon Ashburn23d36b12016-02-02 17:47:28 -07003180 dev->loader_dispatch.ext_dispatch.DevExt[idx] =
3181 (PFN_vkDevExt)gdpa_value;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003182 } else {
3183 for (uint32_t i = 0; i < inst->total_icd_count; i++) {
3184 struct loader_icd *icd = &inst->icds[i];
Karl Schultz2558bd32016-02-24 14:39:39 -07003185 struct loader_device *ldev = icd->logical_device_list;
3186 while (ldev) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003187 gdpa_value =
Karl Schultz2558bd32016-02-24 14:39:39 -07003188 ldev->loader_dispatch.core_dispatch.GetDeviceProcAddr(
3189 ldev->device, funcName);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003190 if (gdpa_value != NULL)
Karl Schultz2558bd32016-02-24 14:39:39 -07003191 ldev->loader_dispatch.ext_dispatch.DevExt[idx] =
Jon Ashburn23d36b12016-02-02 17:47:28 -07003192 (PFN_vkDevExt)gdpa_value;
Karl Schultz2558bd32016-02-24 14:39:39 -07003193 ldev = ldev->next;
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003194 }
3195 }
3196 }
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003197}
3198
3199/**
3200 * Find all dev extension in the hash table and initialize the dispatch table
3201 * for dev for each of those extension entrypoints found in hash table.
3202
3203 */
Jon Ashburn1530c342016-02-26 13:14:27 -07003204void loader_init_dispatch_dev_ext(struct loader_instance *inst,
3205 struct loader_device *dev) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003206 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
3207 if (inst->disp_hash[i].func_name != NULL)
3208 loader_init_dispatch_dev_ext_entry(inst, dev, i,
3209 inst->disp_hash[i].func_name);
3210 }
3211}
3212
3213static bool loader_check_icds_for_address(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003214 const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003215 struct loader_icd *icd;
3216 icd = inst->icds;
3217 while (icd) {
3218 if (icd->this_icd_lib->GetInstanceProcAddr(icd->instance, funcName))
3219 // this icd supports funcName
3220 return true;
3221 icd = icd->next;
3222 }
3223
3224 return false;
3225}
3226
Jon Ashburncc407a22016-04-15 09:25:03 -06003227static bool loader_check_layer_list_for_address(
3228 const struct loader_layer_list *const layers, const char *funcName) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003229 // Iterate over the layers.
Jon Ashburncc407a22016-04-15 09:25:03 -06003230 for (uint32_t layer = 0; layer < layers->count; ++layer) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003231 // Iterate over the extensions.
Jon Ashburncc407a22016-04-15 09:25:03 -06003232 const struct loader_device_extension_list *const extensions =
3233 &(layers->list[layer].device_extension_list);
3234 for (uint32_t extension = 0; extension < extensions->count;
3235 ++extension) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003236 // Iterate over the entry points.
Jon Ashburncc407a22016-04-15 09:25:03 -06003237 const struct loader_dev_ext_props *const property =
3238 &(extensions->list[extension]);
3239 for (uint32_t entry = 0; entry < property->entrypoint_count;
3240 ++entry) {
3241 if (strcmp(property->entrypoints[entry], funcName) == 0) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003242 return true;
3243 }
3244 }
3245 }
3246 }
3247
3248 return false;
3249}
3250
Jon Ashburncc407a22016-04-15 09:25:03 -06003251static bool
3252loader_check_layers_for_address(const struct loader_instance *const inst,
3253 const char *funcName) {
3254 if (loader_check_layer_list_for_address(&inst->instance_layer_list,
3255 funcName)) {
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003256 return true;
3257 }
3258
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003259 return false;
3260}
3261
Jon Ashburn23d36b12016-02-02 17:47:28 -07003262static void loader_free_dev_ext_table(struct loader_instance *inst) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003263 for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
Mark Young0ad83132016-06-30 13:02:42 -06003264 loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
3265 loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003266 }
3267 memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
3268}
3269
3270static bool loader_add_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003271 uint32_t *ptr_idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003272 uint32_t i;
3273 uint32_t idx = *ptr_idx;
3274 struct loader_dispatch_hash_list *list = &inst->disp_hash[idx].list;
3275
3276 if (!inst->disp_hash[idx].func_name) {
3277 // no entry here at this idx, so use it
3278 assert(list->capacity == 0);
Mark Young0ad83132016-06-30 13:02:42 -06003279 inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003280 inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003281 if (inst->disp_hash[idx].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003282 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003283 "loader_add_dev_ext_table() can't allocate memory for "
3284 "func_name");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003285 return false;
3286 }
3287 strncpy(inst->disp_hash[idx].func_name, funcName, strlen(funcName) + 1);
3288 return true;
3289 }
3290
3291 // check for enough capacity
3292 if (list->capacity == 0) {
Mark Young0ad83132016-06-30 13:02:42 -06003293 list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003294 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
3295 if (list->index == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003296 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003297 "loader_add_dev_ext_table() can't allocate list memory");
3298 return false;
3299 }
3300 list->capacity = 8 * sizeof(*(list->index));
3301 } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
Mark Young0ad83132016-06-30 13:02:42 -06003302 list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003303 list->capacity * 2,
3304 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003305 if (list->index == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003306 loader_log(
3307 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3308 "loader_add_dev_ext_table() can't reallocate list memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003309 return false;
3310 }
3311 list->capacity *= 2;
3312 }
3313
Jon Ashburn23d36b12016-02-02 17:47:28 -07003314 // find an unused index in the hash table and use it
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003315 i = (idx + 1) % MAX_NUM_DEV_EXTS;
3316 do {
3317 if (!inst->disp_hash[i].func_name) {
3318 assert(inst->disp_hash[i].list.capacity == 0);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003319 inst->disp_hash[i].func_name =
Mark Young0ad83132016-06-30 13:02:42 -06003320 (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003321 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003322 if (inst->disp_hash[i].func_name == NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003323 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003324 "loader_add_dev_ext_table() can't rallocate "
3325 "func_name memory");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003326 return false;
3327 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07003328 strncpy(inst->disp_hash[i].func_name, funcName,
3329 strlen(funcName) + 1);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003330 list->index[list->count] = i;
3331 list->count++;
3332 *ptr_idx = i;
3333 return true;
3334 }
3335 i = (i + 1) % MAX_NUM_DEV_EXTS;
3336 } while (i != idx);
3337
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003338 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003339 "loader_add_dev_ext_table() couldn't insert into hash table; is "
3340 "it full?");
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003341 return false;
3342}
3343
3344static bool loader_name_in_dev_ext_table(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003345 uint32_t *idx, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003346 uint32_t alt_idx;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003347 if (inst->disp_hash[*idx].func_name &&
3348 !strcmp(inst->disp_hash[*idx].func_name, funcName))
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003349 return true;
3350
3351 // funcName wasn't at the primary spot in the hash table
3352 // search the list of secondary locations (shallow search, not deep search)
3353 for (uint32_t i = 0; i < inst->disp_hash[*idx].list.count; i++) {
3354 alt_idx = inst->disp_hash[*idx].list.index[i];
3355 if (!strcmp(inst->disp_hash[*idx].func_name, funcName)) {
3356 *idx = alt_idx;
3357 return true;
3358 }
3359 }
3360
3361 return false;
3362}
3363
3364/**
Jon Ashburn23d36b12016-02-02 17:47:28 -07003365 * This function returns generic trampoline code address for unknown entry
3366 * points.
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003367 * Presumably, these unknown entry points (as given by funcName) are device
3368 * extension entrypoints. A hash table is used to keep a list of unknown entry
3369 * points and their mapping to the device extension dispatch table
3370 * (struct loader_dev_ext_dispatch_table).
3371 * \returns
Jon Ashburn23d36b12016-02-02 17:47:28 -07003372 * For a given entry point string (funcName), if an existing mapping is found
3373 * the
3374 * trampoline address for that mapping is returned. Otherwise, this unknown
3375 * entry point
3376 * has not been seen yet. Next check if a layer or ICD supports it. If so then
3377 * a
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003378 * new entry in the hash table is initialized and that trampoline address for
3379 * the new entry is returned. Null is returned if the hash table is full or
3380 * if no discovered layer or ICD returns a non-NULL GetProcAddr for it.
3381 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003382void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003383 uint32_t idx;
3384 uint32_t seed = 0;
3385
3386 idx = murmurhash(funcName, strlen(funcName), seed) % MAX_NUM_DEV_EXTS;
3387
3388 if (loader_name_in_dev_ext_table(inst, &idx, funcName))
3389 // found funcName already in hash
3390 return loader_get_dev_ext_trampoline(idx);
3391
3392 // Check if funcName is supported in either ICDs or a layer library
Jeremy Hayes1eb1f622016-03-03 16:03:03 -07003393 if (!loader_check_icds_for_address(inst, funcName) &&
3394 !loader_check_layers_for_address(inst, funcName)) {
Jon Ashburnfc1031e2015-11-17 15:31:02 -07003395 // if support found in layers continue on
3396 return NULL;
3397 }
3398
3399 if (loader_add_dev_ext_table(inst, &idx, funcName)) {
3400 // successfully added new table entry
3401 // init any dev dispatch table entrys as needed
3402 loader_init_dispatch_dev_ext_entry(inst, NULL, idx, funcName);
3403 return loader_get_dev_ext_trampoline(idx);
3404 }
3405
3406 return NULL;
3407}
3408
Jon Ashburn23d36b12016-02-02 17:47:28 -07003409struct loader_instance *loader_get_instance(const VkInstance instance) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003410 /* look up the loader_instance in our list by comparing dispatch tables, as
3411 * there is no guarantee the instance is still a loader_instance* after any
3412 * layers which wrap the instance object.
3413 */
3414 const VkLayerInstanceDispatchTable *disp;
3415 struct loader_instance *ptr_instance = NULL;
3416 disp = loader_get_instance_dispatch(instance);
Jon Ashburn23d36b12016-02-02 17:47:28 -07003417 for (struct loader_instance *inst = loader.instances; inst;
3418 inst = inst->next) {
Jon Ashburne0e64572015-09-30 12:56:42 -06003419 if (inst->disp == disp) {
3420 ptr_instance = inst;
3421 break;
3422 }
3423 }
3424 return ptr_instance;
3425}
3426
Jon Ashburn23d36b12016-02-02 17:47:28 -07003427static loader_platform_dl_handle
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003428loader_open_layer_lib(const struct loader_instance *inst, const char *chain_type,
3429 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003430
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003431 if ((prop->lib_handle = loader_platform_open_library(prop->lib_name)) ==
Jon Ashburn23d36b12016-02-02 17:47:28 -07003432 NULL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003433 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003434 loader_platform_open_library_error(prop->lib_name));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003435 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003436 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003437 "Chain: %s: Loading layer library %s", chain_type,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003438 prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003439 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003440
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003441 return prop->lib_handle;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003442}
3443
Jon Ashburn23d36b12016-02-02 17:47:28 -07003444static void
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003445loader_close_layer_lib(const struct loader_instance *inst,
3446 struct loader_layer_properties *prop) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003447
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003448 if (prop->lib_handle) {
3449 loader_platform_close_library(prop->lib_handle);
3450 loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
3451 "Unloading layer library %s", prop->lib_name);
3452 prop->lib_handle = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003453 }
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003454}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003455
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003456void loader_deactivate_layers(const struct loader_instance *instance,
Mark Young0ad83132016-06-30 13:02:42 -06003457 struct loader_device *device,
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003458 struct loader_layer_list *list) {
3459 /* delete instance list of enabled layers and close any layer libraries */
3460 for (uint32_t i = 0; i < list->count; i++) {
3461 struct loader_layer_properties *layer_prop = &list->list[i];
Courtney Goeltzenleuchter80bfd0e2015-12-17 09:51:22 -07003462
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003463 loader_close_layer_lib(instance, layer_prop);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003464 }
Mark Young0ad83132016-06-30 13:02:42 -06003465 loader_destroy_layer_list(instance, device, list);
Jon Ashburnb8358052014-11-18 09:06:04 -07003466}
3467
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003468/**
3469 * Go through the search_list and find any layers which match type. If layer
3470 * type match is found in then add it to ext_list.
3471 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003472static void
3473loader_add_layer_implicit(const struct loader_instance *inst,
3474 const enum layer_type type,
3475 struct loader_layer_list *list,
3476 const struct loader_layer_list *search_list) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003477 bool enable;
3478 char *env_value;
Jon Ashburn0c26e712015-07-02 16:10:32 -06003479 uint32_t i;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06003480 for (i = 0; i < search_list->count; i++) {
3481 const struct loader_layer_properties *prop = &search_list->list[i];
Jon Ashburn0c26e712015-07-02 16:10:32 -06003482 if (prop->type & type) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003483 /* Found an implicit layer, see if it should be enabled */
3484 enable = false;
3485
Jon Ashburn23d36b12016-02-02 17:47:28 -07003486 // if no enable_environment variable is specified, this implicit
3487 // layer
Jon Ashburn075ce432015-12-17 17:38:24 -07003488 // should always be enabled. Otherwise check if the variable is set
3489 if (prop->enable_env_var.name[0] == 0) {
3490 enable = true;
3491 } else {
Mark Young0ad83132016-06-30 13:02:42 -06003492 env_value = loader_getenv(prop->enable_env_var.name, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003493 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
3494 enable = true;
Mark Young0ad83132016-06-30 13:02:42 -06003495 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003496 }
3497
3498 // disable_environment has priority, i.e. if both enable and disable
Jon Ashburn23d36b12016-02-02 17:47:28 -07003499 // environment variables are set, the layer is disabled. Implicit
3500 // layers
Jon Ashburn075ce432015-12-17 17:38:24 -07003501 // are required to have a disable_environment variables
Mark Young0ad83132016-06-30 13:02:42 -06003502 env_value = loader_getenv(prop->disable_env_var.name, inst);
3503 if (env_value) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003504 enable = false;
Mark Young0ad83132016-06-30 13:02:42 -06003505 }
3506 loader_free_getenv(env_value, inst);
Jon Ashburn075ce432015-12-17 17:38:24 -07003507
Mark Young0ad83132016-06-30 13:02:42 -06003508 if (enable) {
Jon Ashburn075ce432015-12-17 17:38:24 -07003509 loader_add_to_layer_list(inst, list, 1, prop);
Mark Young0ad83132016-06-30 13:02:42 -06003510 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003511 }
3512 }
Jon Ashburn0c26e712015-07-02 16:10:32 -06003513}
3514
3515/**
3516 * Get the layer name(s) from the env_name environment variable. If layer
Jon Ashburnbd332cc2015-07-07 10:27:45 -06003517 * is found in search_list then add it to layer_list. But only add it to
3518 * layer_list if type matches.
Jon Ashburn0c26e712015-07-02 16:10:32 -06003519 */
Jon Ashburn491cd042016-05-16 14:01:18 -06003520static void loader_add_layer_env(struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003521 const enum layer_type type,
3522 const char *env_name,
3523 struct loader_layer_list *layer_list,
3524 const struct loader_layer_list *search_list) {
Ian Elliott4470a302015-02-17 10:33:47 -07003525 char *layerEnv;
Jon Ashburneb6d5682015-07-02 14:10:53 -06003526 char *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003527
Mark Young0ad83132016-06-30 13:02:42 -06003528 layerEnv = loader_getenv(env_name, inst);
Ian Elliott4470a302015-02-17 10:33:47 -07003529 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003530 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003531 }
Courtney Goeltzenleuchter7f5aafc2015-07-05 11:28:29 -06003532 name = loader_stack_alloc(strlen(layerEnv) + 1);
Jon Ashburneb6d5682015-07-02 14:10:53 -06003533 if (name == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003534 return;
Ian Elliott4470a302015-02-17 10:33:47 -07003535 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003536 strcpy(name, layerEnv);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003537
Mark Young0ad83132016-06-30 13:02:42 -06003538 loader_free_getenv(layerEnv, inst);
Jon Ashburn38a497f2016-01-04 14:01:38 -07003539
Jon Ashburn23d36b12016-02-02 17:47:28 -07003540 while (name && *name) {
Jon Ashburneb6d5682015-07-02 14:10:53 -06003541 next = loader_get_next_path(name);
Jon Ashburn71483442016-02-11 18:59:43 -07003542 if (!strcmp(std_validation_str, name)) {
3543 /* add meta list of layers
3544 don't attempt to remove duplicate layers already added by app or
3545 env var
3546 */
3547 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
3548 "Expanding meta layer %s found in environment variable",
3549 std_validation_str);
Jon Ashburn491cd042016-05-16 14:01:18 -06003550 if (type == VK_LAYER_TYPE_INSTANCE_EXPLICIT)
3551 inst->activated_layers_are_std_val = true;
Jon Ashburn71483442016-02-11 18:59:43 -07003552 for (uint32_t i = 0; i < sizeof(std_validation_names) /
3553 sizeof(std_validation_names[0]);
3554 i++) {
3555 loader_find_layer_name_add_list(inst, std_validation_names[i],
3556 type, search_list, layer_list);
3557 }
3558 } else {
3559 loader_find_layer_name_add_list(inst, name, type, search_list,
3560 layer_list);
3561 }
Jon Ashburneb6d5682015-07-02 14:10:53 -06003562 name = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07003563 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003564
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003565 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003566}
3567
Jon Ashburn23d36b12016-02-02 17:47:28 -07003568VkResult
3569loader_enable_instance_layers(struct loader_instance *inst,
3570 const VkInstanceCreateInfo *pCreateInfo,
3571 const struct loader_layer_list *instance_layers) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003572 VkResult err;
3573
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003574 assert(inst && "Cannot have null instance");
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003575
Jon Ashburne39a4f82015-08-28 13:38:21 -06003576 if (!loader_init_layer_list(inst, &inst->activated_layer_list)) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003577 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3578 "Failed to alloc Instance activated layer list");
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003579 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003580 }
3581
Jon Ashburn0c26e712015-07-02 16:10:32 -06003582 /* Add any implicit layers first */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003583 loader_add_layer_implicit(inst, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
3584 &inst->activated_layer_list, instance_layers);
Jon Ashburn0c26e712015-07-02 16:10:32 -06003585
Jon Ashburn2d0c4bb2015-07-06 15:40:35 -06003586 /* Add any layers specified via environment variable next */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003587 loader_add_layer_env(inst, VK_LAYER_TYPE_INSTANCE_EXPLICIT,
3588 "VK_INSTANCE_LAYERS", &inst->activated_layer_list,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003589 instance_layers);
Jon Ashburnbd6c4882015-07-02 12:59:25 -06003590
3591 /* Add layers specified by the application */
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003592 err = loader_add_layer_names_to_list(
Jon Ashburn23d36b12016-02-02 17:47:28 -07003593 inst, &inst->activated_layer_list, pCreateInfo->enabledLayerCount,
3594 pCreateInfo->ppEnabledLayerNames, instance_layers);
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06003595
3596 return err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003597}
3598
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003599/*
3600 * Given the list of layers to activate in the loader_instance
3601 * structure. This function will add a VkLayerInstanceCreateInfo
3602 * structure to the VkInstanceCreateInfo.pNext pointer.
3603 * Each activated layer will have it's own VkLayerInstanceLink
3604 * structure that tells the layer what Get*ProcAddr to call to
3605 * get function pointers to the next layer down.
3606 * Once the chain info has been created this function will
3607 * execute the CreateInstance call chain. Each layer will
3608 * then have an opportunity in it's CreateInstance function
3609 * to setup it's dispatch table when the lower layer returns
3610 * successfully.
3611 * Each layer can wrap or not-wrap the returned VkInstance object
3612 * as it sees fit.
3613 * The instance chain is terminated by a loader function
3614 * that will call CreateInstance on all available ICD's and
3615 * cache those VkInstance objects for future use.
3616 */
3617VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003618 const VkAllocationCallbacks *pAllocator,
Jon Ashburn373c1802016-01-20 08:08:25 -07003619 struct loader_instance *inst,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003620 VkInstance *created_instance) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003621 uint32_t activated_layers = 0;
3622 VkLayerInstanceCreateInfo chain_info;
3623 VkLayerInstanceLink *layer_instance_link_info = NULL;
3624 VkInstanceCreateInfo loader_create_info;
3625 VkResult res;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003626
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003627 PFN_vkGetInstanceProcAddr nextGIPA = loader_gpa_instance_internal;
3628 PFN_vkGetInstanceProcAddr fpGIPA = loader_gpa_instance_internal;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003629
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003630 memcpy(&loader_create_info, pCreateInfo, sizeof(VkInstanceCreateInfo));
Jon Ashburn27cd5842015-05-12 17:26:48 -06003631
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003632 if (inst->activated_layer_list.count > 0) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003633
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003634 chain_info.u.pLayerInfo = NULL;
3635 chain_info.pNext = pCreateInfo->pNext;
3636 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
3637 chain_info.function = VK_LAYER_LINK_INFO;
3638 loader_create_info.pNext = &chain_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003639
Jon Ashburn23d36b12016-02-02 17:47:28 -07003640 layer_instance_link_info = loader_stack_alloc(
3641 sizeof(VkLayerInstanceLink) * inst->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003642 if (!layer_instance_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003643 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3644 "Failed to alloc Instance objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003645 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003646 }
3647
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003648 /* Create instance chain of enabled layers */
3649 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003650 struct loader_layer_properties *layer_prop =
3651 &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003652 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003653
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003654 lib_handle = loader_open_layer_lib(inst, "instance", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003655 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003656 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003657 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3658 NULL) {
3659 if (layer_prop->functions.str_gipa == NULL ||
3660 strlen(layer_prop->functions.str_gipa) == 0) {
3661 fpGIPA = (PFN_vkGetInstanceProcAddr)
3662 loader_platform_get_proc_address(
3663 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003664 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3665 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003666 fpGIPA = (PFN_vkGetInstanceProcAddr)
3667 loader_platform_get_proc_address(
3668 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003669 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003670 loader_log(
3671 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3672 "Failed to find vkGetInstanceProcAddr in layer %s",
3673 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003674 continue;
3675 }
3676 }
3677
Jon Ashburn23d36b12016-02-02 17:47:28 -07003678 layer_instance_link_info[activated_layers].pNext =
3679 chain_info.u.pLayerInfo;
3680 layer_instance_link_info[activated_layers]
3681 .pfnNextGetInstanceProcAddr = nextGIPA;
3682 chain_info.u.pLayerInfo =
3683 &layer_instance_link_info[activated_layers];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003684 nextGIPA = fpGIPA;
3685
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003686 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003687 "Insert instance layer %s (%s)",
3688 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003689
3690 activated_layers++;
3691 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06003692 }
3693
Jon Ashburn23d36b12016-02-02 17:47:28 -07003694 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003695 (PFN_vkCreateInstance)nextGIPA(*created_instance, "vkCreateInstance");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003696 if (fpCreateInstance) {
Jon Ashburnc3c58772016-03-29 11:16:01 -06003697 VkLayerInstanceCreateInfo create_info_disp;
3698
Jon Ashburncc407a22016-04-15 09:25:03 -06003699 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003700 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
Jon Ashburnc3c58772016-03-29 11:16:01 -06003701
3702 create_info_disp.u.pfnSetInstanceLoaderData = vkSetInstanceDispatch;
3703
3704 create_info_disp.pNext = loader_create_info.pNext;
3705 loader_create_info.pNext = &create_info_disp;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003706 res =
3707 fpCreateInstance(&loader_create_info, pAllocator, created_instance);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003708 } else {
3709 // Couldn't find CreateInstance function!
3710 res = VK_ERROR_INITIALIZATION_FAILED;
3711 }
3712
3713 if (res != VK_SUCCESS) {
3714 // TODO: Need to clean up here
3715 } else {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003716 loader_init_instance_core_dispatch_table(inst->disp, nextGIPA,
Jon Ashburn6e0a2132016-02-03 12:37:30 -07003717 *created_instance);
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003718 inst->instance = *created_instance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003719 }
3720
3721 return res;
Jon Ashburn27cd5842015-05-12 17:26:48 -06003722}
3723
Jon Ashburn23d36b12016-02-02 17:47:28 -07003724void loader_activate_instance_layer_extensions(struct loader_instance *inst,
3725 VkInstance created_inst) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003726
Jon Ashburn23d36b12016-02-02 17:47:28 -07003727 loader_init_instance_extension_dispatch_table(
3728 inst->disp, inst->disp->GetInstanceProcAddr, created_inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06003729}
3730
Jon Ashburn1530c342016-02-26 13:14:27 -07003731VkResult
Jon Ashburncc407a22016-04-15 09:25:03 -06003732loader_create_device_chain(const struct loader_physical_device_tramp *pd,
3733 const VkDeviceCreateInfo *pCreateInfo,
3734 const VkAllocationCallbacks *pAllocator,
3735 const struct loader_instance *inst,
3736 struct loader_device *dev) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003737 uint32_t activated_layers = 0;
3738 VkLayerDeviceLink *layer_device_link_info;
3739 VkLayerDeviceCreateInfo chain_info;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003740 VkDeviceCreateInfo loader_create_info;
3741 VkResult res;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003742
Piers Daniell295fe402016-03-29 11:51:11 -06003743 PFN_vkGetDeviceProcAddr fpGDPA, nextGDPA = loader_gpa_device_internal;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003744 PFN_vkGetInstanceProcAddr fpGIPA, nextGIPA = loader_gpa_instance_internal;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003745
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003746 memcpy(&loader_create_info, pCreateInfo, sizeof(VkDeviceCreateInfo));
3747
Jon Ashburn23d36b12016-02-02 17:47:28 -07003748 layer_device_link_info = loader_stack_alloc(
3749 sizeof(VkLayerDeviceLink) * dev->activated_layer_list.count);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003750 if (!layer_device_link_info) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003751 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3752 "Failed to alloc Device objects for layer");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003753 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06003754 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003755
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003756 if (dev->activated_layer_list.count > 0) {
Jon Ashburn72690f22016-03-29 12:52:13 -06003757 chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
3758 chain_info.function = VK_LAYER_LINK_INFO;
3759 chain_info.u.pLayerInfo = NULL;
3760 chain_info.pNext = pCreateInfo->pNext;
3761 loader_create_info.pNext = &chain_info;
3762
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003763 /* Create instance chain of enabled layers */
3764 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003765 struct loader_layer_properties *layer_prop =
3766 &dev->activated_layer_list.list[i];
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003767 loader_platform_dl_handle lib_handle;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -06003768
Jon Ashburn4dc1d8a2016-04-20 13:21:17 -06003769 lib_handle = loader_open_layer_lib(inst, "device", layer_prop);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003770 if (!lib_handle)
Courtney Goeltzenleuchter524b7e32016-01-14 16:06:06 -07003771 continue;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003772 if ((fpGIPA = layer_prop->functions.get_instance_proc_addr) ==
3773 NULL) {
3774 if (layer_prop->functions.str_gipa == NULL ||
3775 strlen(layer_prop->functions.str_gipa) == 0) {
3776 fpGIPA = (PFN_vkGetInstanceProcAddr)
3777 loader_platform_get_proc_address(
3778 lib_handle, "vkGetInstanceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003779 layer_prop->functions.get_instance_proc_addr = fpGIPA;
3780 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003781 fpGIPA = (PFN_vkGetInstanceProcAddr)
3782 loader_platform_get_proc_address(
3783 lib_handle, layer_prop->functions.str_gipa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003784 if (!fpGIPA) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003785 loader_log(
3786 inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
3787 "Failed to find vkGetInstanceProcAddr in layer %s",
3788 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003789 continue;
3790 }
Jon Ashburn21c21ee2015-09-09 11:29:24 -06003791 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003792 if ((fpGDPA = layer_prop->functions.get_device_proc_addr) == NULL) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003793 if (layer_prop->functions.str_gdpa == NULL ||
3794 strlen(layer_prop->functions.str_gdpa) == 0) {
3795 fpGDPA = (PFN_vkGetDeviceProcAddr)
3796 loader_platform_get_proc_address(lib_handle,
3797 "vkGetDeviceProcAddr");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003798 layer_prop->functions.get_device_proc_addr = fpGDPA;
3799 } else
Jon Ashburn23d36b12016-02-02 17:47:28 -07003800 fpGDPA = (PFN_vkGetDeviceProcAddr)
3801 loader_platform_get_proc_address(
3802 lib_handle, layer_prop->functions.str_gdpa);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003803 if (!fpGDPA) {
Jon Ashburnc0dc07c2016-05-16 17:35:43 -06003804 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003805 "Failed to find vkGetDeviceProcAddr in layer %s",
3806 layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003807 continue;
3808 }
3809 }
3810
Jon Ashburn23d36b12016-02-02 17:47:28 -07003811 layer_device_link_info[activated_layers].pNext =
3812 chain_info.u.pLayerInfo;
3813 layer_device_link_info[activated_layers]
3814 .pfnNextGetInstanceProcAddr = nextGIPA;
3815 layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
3816 nextGDPA;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003817 chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
3818 nextGIPA = fpGIPA;
3819 nextGDPA = fpGDPA;
3820
Mark Lobodzinski510e20d2016-02-11 09:26:16 -07003821 loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003822 "Insert device layer %s (%s)",
Jon Ashburn23d36b12016-02-02 17:47:28 -07003823 layer_prop->info.layerName, layer_prop->lib_name);
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003824
3825 activated_layers++;
Jon Ashburn94e70492015-06-10 10:13:10 -06003826 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003827 }
3828
Jon Ashburncc407a22016-04-15 09:25:03 -06003829 VkDevice created_device = (VkDevice)dev;
Jon Ashburn23d36b12016-02-02 17:47:28 -07003830 PFN_vkCreateDevice fpCreateDevice =
Jon Ashburn4e8c4162016-03-08 15:21:30 -07003831 (PFN_vkCreateDevice)nextGIPA(inst->instance, "vkCreateDevice");
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003832 if (fpCreateDevice) {
Jon Ashburned8f2312016-03-31 10:52:22 -06003833 VkLayerDeviceCreateInfo create_info_disp;
3834
Jon Ashburncc407a22016-04-15 09:25:03 -06003835 create_info_disp.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
Jon Ashburned8f2312016-03-31 10:52:22 -06003836 create_info_disp.function = VK_LOADER_DATA_CALLBACK;
3837
3838 create_info_disp.u.pfnSetDeviceLoaderData = vkSetDeviceDispatch;
3839
3840 create_info_disp.pNext = loader_create_info.pNext;
3841 loader_create_info.pNext = &create_info_disp;
Jon Ashburn014438f2016-03-01 19:51:07 -07003842 res = fpCreateDevice(pd->phys_dev, &loader_create_info, pAllocator,
Jon Ashburn72690f22016-03-29 12:52:13 -06003843 &created_device);
Piers Daniellefbbfc12016-04-05 17:28:06 -06003844 if (res != VK_SUCCESS) {
3845 return res;
3846 }
Jon Ashburn72690f22016-03-29 12:52:13 -06003847 dev->device = created_device;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003848 } else {
3849 // Couldn't find CreateDevice function!
3850 return VK_ERROR_INITIALIZATION_FAILED;
3851 }
Jon Ashburn94e70492015-06-10 10:13:10 -06003852
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003853 /* Initialize device dispatch table */
Jon Ashburn23d36b12016-02-02 17:47:28 -07003854 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGDPA,
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003855 dev->device);
3856
3857 return res;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06003858}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06003859
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003860VkResult loader_validate_layers(const struct loader_instance *inst,
3861 const uint32_t layer_count,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003862 const char *const *ppEnabledLayerNames,
3863 const struct loader_layer_list *list) {
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003864 struct loader_layer_properties *prop;
3865
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003866 for (uint32_t i = 0; i < layer_count; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003867 VkStringErrorFlags result =
3868 vk_string_validate(MaxLoaderStringLength, ppEnabledLayerNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003869 if (result != VK_STRING_ERROR_NONE) {
3870 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003871 "Loader: Device ppEnabledLayerNames contains string "
3872 "that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003873 return VK_ERROR_LAYER_NOT_PRESENT;
3874 }
3875
Jon Ashburn23d36b12016-02-02 17:47:28 -07003876 prop = loader_get_layer_property(ppEnabledLayerNames[i], list);
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003877 if (!prop) {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003878 return VK_ERROR_LAYER_NOT_PRESENT;
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003879 }
3880 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003881 return VK_SUCCESS;
3882}
3883
3884VkResult loader_validate_instance_extensions(
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003885 const struct loader_instance *inst,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003886 const struct loader_extension_list *icd_exts,
3887 const struct loader_layer_list *instance_layer,
3888 const VkInstanceCreateInfo *pCreateInfo) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003889
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003890 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003891 struct loader_layer_properties *layer_prop;
3892
Jon Ashburnf19916e2016-01-11 13:12:43 -07003893 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003894 VkStringErrorFlags result = vk_string_validate(
3895 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003896 if (result != VK_STRING_ERROR_NONE) {
3897 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003898 "Loader: Instance ppEnabledExtensionNames contains "
3899 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003900 return VK_ERROR_EXTENSION_NOT_PRESENT;
3901 }
3902
Jon Ashburn23d36b12016-02-02 17:47:28 -07003903 extension_prop = get_extension_property(
3904 pCreateInfo->ppEnabledExtensionNames[i], icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003905
3906 if (extension_prop) {
3907 continue;
3908 }
3909
3910 extension_prop = NULL;
3911
3912 /* Not in global list, search layer extension lists */
Jon Ashburnf19916e2016-01-11 13:12:43 -07003913 for (uint32_t j = 0; j < pCreateInfo->enabledLayerCount; j++) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003914 layer_prop = loader_get_layer_property(
3915 pCreateInfo->ppEnabledLayerNames[i], instance_layer);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003916 if (!layer_prop) {
Courtney Goeltzenleuchter6f5b00c2015-07-06 20:46:50 -06003917 /* Should NOT get here, loader_validate_layers
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003918 * should have already filtered this case out.
3919 */
3920 continue;
3921 }
3922
Jon Ashburn23d36b12016-02-02 17:47:28 -07003923 extension_prop =
3924 get_extension_property(pCreateInfo->ppEnabledExtensionNames[i],
3925 &layer_prop->instance_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003926 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003927 /* Found the extension in one of the layers enabled by the app.
3928 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003929 break;
3930 }
3931 }
3932
3933 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003934 /* Didn't find extension name in any of the global layers, error out
3935 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003936 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003937 }
3938 }
3939 return VK_SUCCESS;
3940}
3941
3942VkResult loader_validate_device_extensions(
Jon Ashburn787eb252016-03-24 15:49:57 -06003943 struct loader_physical_device_tramp *phys_dev,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003944 const struct loader_layer_list *activated_device_layers,
Jon Ashburn014438f2016-03-01 19:51:07 -07003945 const struct loader_extension_list *icd_exts,
Jon Ashburn23d36b12016-02-02 17:47:28 -07003946 const VkDeviceCreateInfo *pCreateInfo) {
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003947 VkExtensionProperties *extension_prop;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003948 struct loader_layer_properties *layer_prop;
3949
Jon Ashburnf19916e2016-01-11 13:12:43 -07003950 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003951
Jon Ashburnf2b4e382016-02-10 20:50:19 -07003952 VkStringErrorFlags result = vk_string_validate(
3953 MaxLoaderStringLength, pCreateInfo->ppEnabledExtensionNames[i]);
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003954 if (result != VK_STRING_ERROR_NONE) {
Jon Ashburncc407a22016-04-15 09:25:03 -06003955 loader_log(phys_dev->this_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT,
3956 0, "Loader: Device ppEnabledExtensionNames contains "
3957 "string that is too long or is badly formed");
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07003958 return VK_ERROR_EXTENSION_NOT_PRESENT;
3959 }
3960
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003961 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07003962 extension_prop = get_extension_property(extension_name, icd_exts);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003963
3964 if (extension_prop) {
3965 continue;
3966 }
3967
Jon Ashburn471f44c2016-01-13 12:51:43 -07003968 /* Not in global list, search activated layer extension lists */
3969 for (uint32_t j = 0; j < activated_device_layers->count; j++) {
3970 layer_prop = &activated_device_layers->list[j];
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003971
Jon Ashburn23d36b12016-02-02 17:47:28 -07003972 extension_prop = get_dev_extension_property(
3973 extension_name, &layer_prop->device_extension_list);
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003974 if (extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003975 /* Found the extension in one of the layers enabled by the app.
3976 */
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003977 break;
3978 }
3979 }
3980
3981 if (!extension_prop) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07003982 /* Didn't find extension name in any of the device layers, error out
3983 */
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06003984 return VK_ERROR_EXTENSION_NOT_PRESENT;
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06003985 }
3986 }
Courtney Goeltzenleuchter3b8c5ff2015-07-06 17:45:08 -06003987 return VK_SUCCESS;
3988}
3989
Jon Ashburn1530c342016-02-26 13:14:27 -07003990/**
3991 * Terminator functions for the Instance chain
3992 * All named terminator_<Vulakn API name>
3993 */
Mark Young0ad83132016-06-30 13:02:42 -06003994VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
3995 const VkInstanceCreateInfo *pCreateInfo,
3996 const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
Jon Ashburn46888392015-01-29 15:45:51 -07003997 struct loader_icd *icd;
Jon Ashburn5c042ea2015-08-04 11:14:18 -06003998 VkExtensionProperties *prop;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06003999 char **filtered_extension_names = NULL;
4000 VkInstanceCreateInfo icd_create_info;
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004001 VkResult res = VK_SUCCESS;
Jon Ashburn7e18de02015-11-19 09:29:51 -07004002 bool success = false;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004003
Jon Ashburncc407a22016-04-15 09:25:03 -06004004 struct loader_instance *ptr_instance = (struct loader_instance *)*pInstance;
Tony Barbour3c78ff42015-12-04 13:24:39 -07004005 memcpy(&icd_create_info, pCreateInfo, sizeof(icd_create_info));
4006
Jon Ashburnf19916e2016-01-11 13:12:43 -07004007 icd_create_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004008 icd_create_info.ppEnabledLayerNames = NULL;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004009
4010 /*
4011 * NOTE: Need to filter the extensions to only those
Courtney Goeltzenleuchter366b27a2015-07-06 20:14:18 -06004012 * supported by the ICD.
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004013 * No ICD will advertise support for layers. An ICD
4014 * library could support a layer, but it would be
4015 * independent of the actual ICD, just in the same library.
4016 */
Jon Ashburn23d36b12016-02-02 17:47:28 -07004017 filtered_extension_names =
4018 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004019 if (!filtered_extension_names) {
4020 return VK_ERROR_OUT_OF_HOST_MEMORY;
4021 }
Jon Ashburn23d36b12016-02-02 17:47:28 -07004022 icd_create_info.ppEnabledExtensionNames =
4023 (const char *const *)filtered_extension_names;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004024
Jon Ashburn8810c5f2015-08-18 18:04:47 -06004025 for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
4026 icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
Mark Young0ad83132016-06-30 13:02:42 -06004027 if (NULL == icd) {
4028 while (NULL != ptr_instance->icds) {
4029 icd = ptr_instance->icds;
4030 ptr_instance->icds = icd->next;
4031 icd->DestroyInstance(icd->instance, pAllocator);
4032 loader_icd_destroy(ptr_instance, icd, pAllocator);
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004033 }
Mark Young0ad83132016-06-30 13:02:42 -06004034 return VK_ERROR_OUT_OF_HOST_MEMORY;
4035 }
4036 icd_create_info.enabledExtensionCount = 0;
4037 struct loader_extension_list icd_exts;
Courtney Goeltzenleuchter746db732015-07-06 17:42:01 -06004038
Mark Young0ad83132016-06-30 13:02:42 -06004039 loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
4040 "Build ICD instance extension list");
4041 // traverse scanned icd list adding non-duplicate extensions to the
4042 // list
4043 loader_init_generic_list(ptr_instance,
4044 (struct loader_generic_list *)&icd_exts,
4045 sizeof(VkExtensionProperties));
4046 loader_add_instance_extensions(
4047 ptr_instance,
4048 icd->this_icd_lib->EnumerateInstanceExtensionProperties,
4049 icd->this_icd_lib->lib_name, &icd_exts);
Courtney Goeltzenleuchter36eeb742015-12-21 16:41:47 -07004050
Mark Young0ad83132016-06-30 13:02:42 -06004051 for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
4052 prop = get_extension_property(
4053 pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
4054 if (prop) {
4055 filtered_extension_names[icd_create_info
4056 .enabledExtensionCount] =
4057 (char *)pCreateInfo->ppEnabledExtensionNames[j];
4058 icd_create_info.enabledExtensionCount++;
Jon Ashburn46888392015-01-29 15:45:51 -07004059 }
4060 }
Mark Young0ad83132016-06-30 13:02:42 -06004061
4062 loader_destroy_generic_list(ptr_instance,
4063 (struct loader_generic_list *)&icd_exts);
4064
4065 res = ptr_instance->icd_libs.list[i].CreateInstance(
4066 &icd_create_info, pAllocator, &(icd->instance));
4067 if (res == VK_SUCCESS)
4068 success = loader_icd_init_entrys(
4069 icd, icd->instance,
4070 ptr_instance->icd_libs.list[i].GetInstanceProcAddr);
4071
4072 if (res != VK_SUCCESS || !success) {
4073 ptr_instance->icds = ptr_instance->icds->next;
4074 loader_icd_destroy(ptr_instance, icd, pAllocator);
4075 loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
4076 "ICD ignored: failed to CreateInstance and find "
4077 "entrypoints with ICD");
4078 }
Jon Ashburn46888392015-01-29 15:45:51 -07004079 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004080
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004081 /*
4082 * If no ICDs were added to instance list and res is unchanged
4083 * from it's initial value, the loader was unable to find
4084 * a suitable ICD.
4085 */
Ian Elliotteb450762015-02-05 15:19:15 -07004086 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004087 if (res == VK_SUCCESS) {
4088 return VK_ERROR_INCOMPATIBLE_DRIVER;
4089 } else {
4090 return res;
4091 }
Ian Elliotteb450762015-02-05 15:19:15 -07004092 }
Jon Ashburn46888392015-01-29 15:45:51 -07004093
Courtney Goeltzenleuchter40caf0b2015-07-06 09:06:34 -06004094 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004095}
4096
Mark Young0ad83132016-06-30 13:02:42 -06004097VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
4098 VkInstance instance, const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06004099 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004100 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06004101 struct loader_icd *next_icd;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004102
4103 // Remove this instance from the list of instances:
4104 struct loader_instance *prev = NULL;
4105 struct loader_instance *next = loader.instances;
4106 while (next != NULL) {
4107 if (next == ptr_instance) {
4108 // Remove this instance from the list:
4109 if (prev)
4110 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07004111 else
4112 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004113 break;
4114 }
4115 prev = next;
4116 next = next->next;
4117 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004118
Jon Ashburn3da71f22015-05-14 12:43:38 -06004119 while (icds) {
4120 if (icds->instance) {
Chia-I Wuf7458c52015-10-26 21:10:41 +08004121 icds->DestroyInstance(icds->instance, pAllocator);
Tony Barbourf20f87b2015-04-22 09:02:32 -06004122 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06004123 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004124 icds->instance = VK_NULL_HANDLE;
Mark Young0ad83132016-06-30 13:02:42 -06004125 loader_icd_destroy(ptr_instance, icds, pAllocator);
Jon Ashburna6fd2612015-06-16 14:43:19 -06004126
4127 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07004128 }
Jon Ashburn491cd042016-05-16 14:01:18 -06004129
Jon Ashburn23d36b12016-02-02 17:47:28 -07004130 loader_delete_layer_properties(ptr_instance,
4131 &ptr_instance->instance_layer_list);
Jon Ashburne39a4f82015-08-28 13:38:21 -06004132 loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
Jon Ashburn23d36b12016-02-02 17:47:28 -07004133 loader_destroy_generic_list(
4134 ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
Jon Ashburn014438f2016-03-01 19:51:07 -07004135 if (ptr_instance->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004136 loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
Jon Ashburnfc1031e2015-11-17 15:31:02 -07004137 loader_free_dev_ext_table(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004138}
4139
Mark Young0ad83132016-06-30 13:02:42 -06004140VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
4141 VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
4142 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4143 VkResult res = VK_SUCCESS;
Jon Ashburn1530c342016-02-26 13:14:27 -07004144 struct loader_physical_device *phys_dev;
Jon Ashburn014438f2016-03-01 19:51:07 -07004145 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004146
Jon Ashburncc407a22016-04-15 09:25:03 -06004147 struct loader_device *dev = (struct loader_device *)*pDevice;
Jon Ashburn72690f22016-03-29 12:52:13 -06004148 PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
Mark Young0ad83132016-06-30 13:02:42 -06004149 struct loader_extension_list icd_exts;
4150
4151 icd_exts.list = NULL;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004152
Jon Ashburn1530c342016-02-26 13:14:27 -07004153 if (fpCreateDevice == NULL) {
Mark Young0ad83132016-06-30 13:02:42 -06004154 res = VK_ERROR_INITIALIZATION_FAILED;
4155 goto out;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004156 }
4157
Jon Ashburn1530c342016-02-26 13:14:27 -07004158 VkDeviceCreateInfo localCreateInfo;
4159 memcpy(&localCreateInfo, pCreateInfo, sizeof(localCreateInfo));
Jon Ashburn1530c342016-02-26 13:14:27 -07004160
4161 /*
4162 * NOTE: Need to filter the extensions to only those
4163 * supported by the ICD.
4164 * No ICD will advertise support for layers. An ICD
4165 * library could support a layer, but it would be
4166 * independent of the actual ICD, just in the same library.
4167 */
4168 char **filtered_extension_names = NULL;
4169 filtered_extension_names =
4170 loader_stack_alloc(pCreateInfo->enabledExtensionCount * sizeof(char *));
4171 if (!filtered_extension_names) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004172 return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004173 }
4174
Jon Ashburn1530c342016-02-26 13:14:27 -07004175 localCreateInfo.enabledLayerCount = 0;
4176 localCreateInfo.ppEnabledLayerNames = NULL;
4177
4178 localCreateInfo.enabledExtensionCount = 0;
4179 localCreateInfo.ppEnabledExtensionNames =
4180 (const char *const *)filtered_extension_names;
4181
Jon Ashburn014438f2016-03-01 19:51:07 -07004182 /* Get the physical device (ICD) extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004183 if (!loader_init_generic_list(phys_dev->this_icd->this_instance,
4184 (struct loader_generic_list *)&icd_exts,
4185 sizeof(VkExtensionProperties))) {
Mark Young0ad83132016-06-30 13:02:42 -06004186 res = VK_ERROR_OUT_OF_HOST_MEMORY;
4187 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004188 }
4189
4190 res = loader_add_device_extensions(
Jon Ashburncc407a22016-04-15 09:25:03 -06004191 phys_dev->this_icd->this_instance,
4192 phys_dev->this_icd->EnumerateDeviceExtensionProperties,
Jon Ashburn014438f2016-03-01 19:51:07 -07004193 phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
4194 &icd_exts);
4195 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004196 goto out;
Jon Ashburn014438f2016-03-01 19:51:07 -07004197 }
4198
Jon Ashburn1530c342016-02-26 13:14:27 -07004199 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4200 const char *extension_name = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn014438f2016-03-01 19:51:07 -07004201 VkExtensionProperties *prop =
4202 get_extension_property(extension_name, &icd_exts);
Jon Ashburn1530c342016-02-26 13:14:27 -07004203 if (prop) {
4204 filtered_extension_names[localCreateInfo.enabledExtensionCount] =
4205 (char *)extension_name;
4206 localCreateInfo.enabledExtensionCount++;
4207 }
4208 }
4209
Jon Ashburn1530c342016-02-26 13:14:27 -07004210 // TODO: Why does fpCreateDevice behave differently than
4211 // this_icd->CreateDevice?
4212 // VkResult res = fpCreateDevice(phys_dev->phys_dev, &localCreateInfo,
4213 // pAllocator, &localDevice);
Jon Ashburn014438f2016-03-01 19:51:07 -07004214 res = phys_dev->this_icd->CreateDevice(phys_dev->phys_dev, &localCreateInfo,
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004215 pAllocator, &dev->device);
Jon Ashburn1530c342016-02-26 13:14:27 -07004216
4217 if (res != VK_SUCCESS) {
Mark Young0ad83132016-06-30 13:02:42 -06004218 goto out;
Jon Ashburn1530c342016-02-26 13:14:27 -07004219 }
4220
Jon Ashburn0b8507c2016-04-01 11:49:39 -06004221 *pDevice = dev->device;
Jon Ashburncc407a22016-04-15 09:25:03 -06004222 loader_add_logical_device(phys_dev->this_icd->this_instance,
4223 phys_dev->this_icd, dev);
Jon Ashburn1530c342016-02-26 13:14:27 -07004224
4225 /* Init dispatch pointer in new device object */
4226 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
4227
Mark Young0ad83132016-06-30 13:02:42 -06004228out:
4229 if (NULL != icd_exts.list) {
4230 loader_destroy_generic_list(phys_dev->this_icd->this_instance,
4231 (struct loader_generic_list *)&icd_exts);
4232 }
4233
Jon Ashburn1530c342016-02-26 13:14:27 -07004234 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004235}
4236
Jon Ashburn23d36b12016-02-02 17:47:28 -07004237VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004238terminator_EnumeratePhysicalDevices(VkInstance instance,
4239 uint32_t *pPhysicalDeviceCount,
4240 VkPhysicalDevice *pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004241 uint32_t i;
Jon Ashburn014438f2016-03-01 19:51:07 -07004242 struct loader_instance *inst = (struct loader_instance *)instance;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004243 VkResult res = VK_SUCCESS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004244
Jon Ashburn014438f2016-03-01 19:51:07 -07004245 struct loader_icd *icd;
4246 struct loader_phys_dev_per_icd *phys_devs;
4247
4248 inst->total_gpu_count = 0;
4249 phys_devs = (struct loader_phys_dev_per_icd *)loader_stack_alloc(
4250 sizeof(struct loader_phys_dev_per_icd) * inst->total_icd_count);
4251 if (!phys_devs)
4252 return VK_ERROR_OUT_OF_HOST_MEMORY;
4253
4254 icd = inst->icds;
4255 for (i = 0; i < inst->total_icd_count; i++) {
4256 assert(icd);
4257 res = icd->EnumeratePhysicalDevices(icd->instance, &phys_devs[i].count,
4258 NULL);
4259 if (res != VK_SUCCESS)
4260 return res;
4261 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004262 }
4263
Jon Ashburn014438f2016-03-01 19:51:07 -07004264 icd = inst->icds;
4265 for (i = 0; i < inst->total_icd_count; i++) {
4266 assert(icd);
4267 phys_devs[i].phys_devs = (VkPhysicalDevice *)loader_stack_alloc(
4268 phys_devs[i].count * sizeof(VkPhysicalDevice));
4269 if (!phys_devs[i].phys_devs) {
4270 return VK_ERROR_OUT_OF_HOST_MEMORY;
4271 }
4272 res = icd->EnumeratePhysicalDevices(
4273 icd->instance, &(phys_devs[i].count), phys_devs[i].phys_devs);
4274 if ((res == VK_SUCCESS)) {
4275 inst->total_gpu_count += phys_devs[i].count;
4276 } else {
4277 return res;
4278 }
4279 phys_devs[i].this_icd = icd;
4280 icd = icd->next;
4281 }
4282
4283 *pPhysicalDeviceCount = inst->total_gpu_count;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004284 if (!pPhysicalDevices) {
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004285 return res;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004286 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07004287
Jon Ashburn014438f2016-03-01 19:51:07 -07004288 /* Initialize the output pPhysicalDevices with wrapped loader terminator
4289 * physicalDevice objects; save this list of wrapped objects in instance
4290 * struct for later cleanup and use by trampoline code */
4291 uint32_t j, idx = 0;
4292 uint32_t copy_count = 0;
4293
4294 copy_count = (inst->total_gpu_count < *pPhysicalDeviceCount)
4295 ? inst->total_gpu_count
Jon Ashburn23d36b12016-02-02 17:47:28 -07004296 : *pPhysicalDeviceCount;
Jon Ashburn014438f2016-03-01 19:51:07 -07004297
Jon Ashburn014438f2016-03-01 19:51:07 -07004298 if (inst->phys_devs_term)
Mark Young0ad83132016-06-30 13:02:42 -06004299 loader_instance_heap_free(inst, inst->phys_devs_term);
4300 inst->phys_devs_term = loader_instance_heap_alloc(
Jon Ashburn014438f2016-03-01 19:51:07 -07004301 inst, sizeof(struct loader_physical_device) * copy_count,
4302 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
4303 if (!inst->phys_devs_term)
4304 return VK_ERROR_OUT_OF_HOST_MEMORY;
4305
4306 for (i = 0; idx < copy_count && i < inst->total_icd_count; i++) {
Jon Ashburn1a02c2f2016-03-11 14:43:57 -07004307
Jon Ashburn014438f2016-03-01 19:51:07 -07004308 for (j = 0; j < phys_devs[i].count && idx < copy_count; j++) {
4309 loader_set_dispatch((void *)&inst->phys_devs_term[idx], inst->disp);
4310 inst->phys_devs_term[idx].this_icd = phys_devs[i].this_icd;
4311 inst->phys_devs_term[idx].phys_dev = phys_devs[i].phys_devs[j];
4312 pPhysicalDevices[idx] =
4313 (VkPhysicalDevice)&inst->phys_devs_term[idx];
4314 idx++;
4315 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06004316 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004317 *pPhysicalDeviceCount = copy_count;
4318
Piers Daniell295fe402016-03-29 11:51:11 -06004319 // TODO: Is phys_devs being leaked?
4320
Jon Ashburn014438f2016-03-01 19:51:07 -07004321 if (copy_count < inst->total_gpu_count) {
4322 inst->total_gpu_count = copy_count;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07004323 return VK_INCOMPLETE;
4324 }
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004325 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07004326}
4327
Jon Ashburn1530c342016-02-26 13:14:27 -07004328VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceProperties(
4329 VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004330 struct loader_physical_device *phys_dev =
4331 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004332 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn3da71f22015-05-14 12:43:38 -06004333
Tony Barbour59a47322015-06-24 16:06:58 -06004334 if (icd->GetPhysicalDeviceProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004335 icd->GetPhysicalDeviceProperties(phys_dev->phys_dev, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004336}
4337
Jon Ashburn1530c342016-02-26 13:14:27 -07004338VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceQueueFamilyProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004339 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
4340 VkQueueFamilyProperties *pProperties) {
4341 struct loader_physical_device *phys_dev =
4342 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004343 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004344
Cody Northropd0802882015-08-03 17:04:53 -06004345 if (icd->GetPhysicalDeviceQueueFamilyProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004346 icd->GetPhysicalDeviceQueueFamilyProperties(
4347 phys_dev->phys_dev, pQueueFamilyPropertyCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -06004348}
4349
Jon Ashburn1530c342016-02-26 13:14:27 -07004350VKAPI_ATTR void VKAPI_CALL terminator_GetPhysicalDeviceMemoryProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004351 VkPhysicalDevice physicalDevice,
4352 VkPhysicalDeviceMemoryProperties *pProperties) {
4353 struct loader_physical_device *phys_dev =
4354 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004355 struct loader_icd *icd = phys_dev->this_icd;
Tony Barbour59a47322015-06-24 16:06:58 -06004356
4357 if (icd->GetPhysicalDeviceMemoryProperties)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004358 icd->GetPhysicalDeviceMemoryProperties(phys_dev->phys_dev, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06004359}
4360
Jon Ashburn23d36b12016-02-02 17:47:28 -07004361VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004362terminator_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
4363 VkPhysicalDeviceFeatures *pFeatures) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004364 struct loader_physical_device *phys_dev =
4365 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004366 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004367
4368 if (icd->GetPhysicalDeviceFeatures)
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004369 icd->GetPhysicalDeviceFeatures(phys_dev->phys_dev, pFeatures);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004370}
4371
Jon Ashburn23d36b12016-02-02 17:47:28 -07004372VKAPI_ATTR void VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004373terminator_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,
4374 VkFormat format,
4375 VkFormatProperties *pFormatInfo) {
Jon Ashburn23d36b12016-02-02 17:47:28 -07004376 struct loader_physical_device *phys_dev =
4377 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004378 struct loader_icd *icd = phys_dev->this_icd;
Chris Forbesbc0bb772015-06-21 22:55:02 +12004379
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -06004380 if (icd->GetPhysicalDeviceFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004381 icd->GetPhysicalDeviceFormatProperties(phys_dev->phys_dev, format,
4382 pFormatInfo);
Chris Forbesbc0bb772015-06-21 22:55:02 +12004383}
4384
Jon Ashburn1530c342016-02-26 13:14:27 -07004385VKAPI_ATTR VkResult VKAPI_CALL
4386terminator_GetPhysicalDeviceImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004387 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4388 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
4389 VkImageFormatProperties *pImageFormatProperties) {
4390 struct loader_physical_device *phys_dev =
4391 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004392 struct loader_icd *icd = phys_dev->this_icd;
Jon Ashburn754864f2015-07-23 18:49:07 -06004393
Chia-I Wu17241042015-10-31 00:31:16 +08004394 if (!icd->GetPhysicalDeviceImageFormatProperties)
4395 return VK_ERROR_INITIALIZATION_FAILED;
4396
Jon Ashburn23d36b12016-02-02 17:47:28 -07004397 return icd->GetPhysicalDeviceImageFormatProperties(
4398 phys_dev->phys_dev, format, type, tiling, usage, flags,
4399 pImageFormatProperties);
Jon Ashburn754864f2015-07-23 18:49:07 -06004400}
4401
Jon Ashburn1530c342016-02-26 13:14:27 -07004402VKAPI_ATTR void VKAPI_CALL
4403terminator_GetPhysicalDeviceSparseImageFormatProperties(
Jon Ashburn23d36b12016-02-02 17:47:28 -07004404 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
4405 VkSampleCountFlagBits samples, VkImageUsageFlags usage,
4406 VkImageTiling tiling, uint32_t *pNumProperties,
4407 VkSparseImageFormatProperties *pProperties) {
4408 struct loader_physical_device *phys_dev =
4409 (struct loader_physical_device *)physicalDevice;
Jon Ashburn24cd4be2015-11-01 14:04:06 -07004410 struct loader_icd *icd = phys_dev->this_icd;
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004411
4412 if (icd->GetPhysicalDeviceSparseImageFormatProperties)
Jon Ashburn23d36b12016-02-02 17:47:28 -07004413 icd->GetPhysicalDeviceSparseImageFormatProperties(
4414 phys_dev->phys_dev, format, type, samples, usage, tiling,
4415 pNumProperties, pProperties);
Mark Lobodzinski16e8bef2015-07-03 15:58:09 -06004416}
4417
Jon Ashburn1530c342016-02-26 13:14:27 -07004418VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
4419 VkPhysicalDevice physicalDevice, const char *pLayerName,
4420 uint32_t *pPropertyCount, VkExtensionProperties *pProperties) {
Jon Ashburna760a512015-12-14 08:52:14 -07004421 struct loader_physical_device *phys_dev;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07004422
Jon Ashburn471f44c2016-01-13 12:51:43 -07004423 struct loader_layer_list implicit_layer_list;
4424
Jon Ashburndc5d9202016-02-29 13:00:51 -07004425 assert(pLayerName == NULL || strlen(pLayerName) == 0);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004426
Jon Ashburndc5d9202016-02-29 13:00:51 -07004427 /* Any layer or trampoline wrapping should be removed at this point in time
4428 * can just cast to the expected type for VkPhysicalDevice. */
Jon Ashburn014438f2016-03-01 19:51:07 -07004429 phys_dev = (struct loader_physical_device *)physicalDevice;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004430
Jon Ashburndc5d9202016-02-29 13:00:51 -07004431 /* this case is during the call down the instance chain with pLayerName
4432 * == NULL*/
4433 struct loader_icd *icd = phys_dev->this_icd;
4434 uint32_t icd_ext_count = *pPropertyCount;
4435 VkResult res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004436
Jon Ashburndc5d9202016-02-29 13:00:51 -07004437 /* get device extensions */
Jon Ashburn014438f2016-03-01 19:51:07 -07004438 res = icd->EnumerateDeviceExtensionProperties(phys_dev->phys_dev, NULL,
4439 &icd_ext_count, pProperties);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004440 if (res != VK_SUCCESS)
4441 return res;
Jon Ashburn23d36b12016-02-02 17:47:28 -07004442
Jon Ashburn014438f2016-03-01 19:51:07 -07004443 loader_init_layer_list(icd->this_instance, &implicit_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004444
Jon Ashburndc5d9202016-02-29 13:00:51 -07004445 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004446 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4447 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004448 /* we need to determine which implicit layers are active,
4449 * and then add their extensions. This can't be cached as
4450 * it depends on results of environment variables (which can change).
4451 */
4452 if (pProperties != NULL) {
Jon Ashburn014438f2016-03-01 19:51:07 -07004453 struct loader_extension_list icd_exts;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004454 /* initialize dev_extension list within the physicalDevice object */
Jon Ashburn014438f2016-03-01 19:51:07 -07004455 res = loader_init_device_extensions(icd->this_instance, phys_dev,
4456 icd_ext_count, pProperties,
4457 &icd_exts);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004458 if (res != VK_SUCCESS)
4459 return res;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004460
Jon Ashburndc5d9202016-02-29 13:00:51 -07004461 /* we need to determine which implicit layers are active,
4462 * and then add their extensions. This can't be cached as
4463 * it depends on results of environment variables (which can
4464 * change).
4465 */
4466 struct loader_extension_list all_exts = {0};
Jon Ashburn014438f2016-03-01 19:51:07 -07004467 loader_add_to_ext_list(icd->this_instance, &all_exts, icd_exts.count,
4468 icd_exts.list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004469
Jon Ashburndc5d9202016-02-29 13:00:51 -07004470 loader_add_layer_implicit(
Jon Ashburn014438f2016-03-01 19:51:07 -07004471 icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
4472 &implicit_layer_list, &icd->this_instance->instance_layer_list);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004473
Jon Ashburndc5d9202016-02-29 13:00:51 -07004474 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4475 for (uint32_t j = 0;
Jon Ashburn014438f2016-03-01 19:51:07 -07004476 j < implicit_layer_list.list[i].device_extension_list.count;
4477 j++) {
4478 loader_add_to_ext_list(icd->this_instance, &all_exts, 1,
4479 &implicit_layer_list.list[i]
4480 .device_extension_list.list[j]
4481 .props);
Jon Ashburn471f44c2016-01-13 12:51:43 -07004482 }
Jon Ashburn471f44c2016-01-13 12:51:43 -07004483 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004484 uint32_t capacity = *pPropertyCount;
4485 VkExtensionProperties *props = pProperties;
Jon Ashburn471f44c2016-01-13 12:51:43 -07004486
Jon Ashburndc5d9202016-02-29 13:00:51 -07004487 for (uint32_t i = 0; i < all_exts.count && i < capacity; i++) {
4488 props[i] = all_exts.list[i];
4489 }
4490 /* wasn't enough space for the extensions, we did partial copy now
4491 * return VK_INCOMPLETE */
4492 if (capacity < all_exts.count) {
4493 res = VK_INCOMPLETE;
4494 } else {
4495 *pPropertyCount = all_exts.count;
4496 }
Jon Ashburn014438f2016-03-01 19:51:07 -07004497 loader_destroy_generic_list(icd->this_instance,
4498 (struct loader_generic_list *)&all_exts);
Mark Young0ad83132016-06-30 13:02:42 -06004499 loader_destroy_generic_list(icd->this_instance,
4500 (struct loader_generic_list *)&icd_exts);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004501 } else {
4502 /* just return the count; need to add in the count of implicit layer
4503 * extensions
4504 * don't worry about duplicates being added in the count */
4505 *pPropertyCount = icd_ext_count;
4506
4507 for (uint32_t i = 0; i < implicit_layer_list.count; i++) {
4508 *pPropertyCount +=
Jon Ashburn014438f2016-03-01 19:51:07 -07004509 implicit_layer_list.list[i].device_extension_list.count;
Jon Ashburndc5d9202016-02-29 13:00:51 -07004510 }
4511 res = VK_SUCCESS;
Jon Ashburnb82c1852015-08-11 14:49:54 -06004512 }
Jon Ashburndc5d9202016-02-29 13:00:51 -07004513
4514 loader_destroy_generic_list(
Jon Ashburn014438f2016-03-01 19:51:07 -07004515 icd->this_instance, (struct loader_generic_list *)&implicit_layer_list);
Jon Ashburndc5d9202016-02-29 13:00:51 -07004516 return res;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004517}
4518
Jon Ashburn23d36b12016-02-02 17:47:28 -07004519VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburn1530c342016-02-26 13:14:27 -07004520terminator_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
4521 uint32_t *pPropertyCount,
4522 VkLayerProperties *pProperties) {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -06004523
Jon Ashburndc5d9202016-02-29 13:00:51 -07004524 // should never get here this call isn't dispatched down the chain
4525 return VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06004526}
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004527
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004528VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004529 VkStringErrorFlags result = VK_STRING_ERROR_NONE;
Karl Schultz2558bd32016-02-24 14:39:39 -07004530 int num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004531 int i, j;
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004532
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004533 for (i = 0; i < max_length; i++) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004534 if (utf8[i] == 0) {
4535 break;
Mark Lobodzinski36b4de22016-02-12 11:30:14 -07004536 } else if ((utf8[i] >= 0x20) && (utf8[i] < 0x7f)) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004537 num_char_bytes = 0;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004538 } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004539 num_char_bytes = 1;
Jon Ashburnf2b4e382016-02-10 20:50:19 -07004540 } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
Mark Lobodzinskie9f09ef2016-02-02 18:53:34 -07004541 num_char_bytes = 2;
4542 } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
4543 num_char_bytes = 3;
4544 } else {
4545 result = VK_STRING_ERROR_BAD_DATA;
4546 }
4547
4548 // Validate the following num_char_bytes of data
4549 for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
4550 if (++i == max_length) {
4551 result |= VK_STRING_ERROR_LENGTH;
4552 break;
4553 }
4554 if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
4555 result |= VK_STRING_ERROR_BAD_DATA;
4556 }
4557 }
4558 }
4559 return result;
4560}