forked from KhronosGroup/Vulkan-LoaderAndValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
5535 lines (4908 loc) · 239 KB
/
loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 2014-2017 The Khronos Group Inc.
* Copyright (c) 2014-2017 Valve Corporation
* Copyright (c) 2014-2017 LunarG, Inc.
* Copyright (C) 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Jon Ashburn <jon@lunarg.com>
* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
* Author: Mark Young <marky@lunarg.com>
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <stddef.h>
#include <sys/types.h>
#if defined(_WIN32)
#include "dirent_on_windows.h"
#else // _WIN32
#include <dirent.h>
#endif // _WIN32
#include "vk_loader_platform.h"
#include "loader.h"
#include "gpa_helper.h"
#include "debug_report.h"
#include "wsi.h"
#include "vulkan/vk_icd.h"
#include "cJSON.h"
#include "murmurhash.h"
// This is a CMake generated file with #defines for any functions/includes
// that it found present. This is currently necessary to properly determine
// if secure_getenv or __secure_getenv are present
#if !defined(VULKAN_NON_CMAKE_BUILD)
#include "loader_cmake_config.h"
#endif // !defined(VULKAN_NON_CMAKE_BUILD)
// Generated file containing all the extension data
#include "vk_loader_extensions.c"
struct loader_struct loader = {0};
// TLS for instance for alloc/free callbacks
THREAD_LOCAL_DECL struct loader_instance *tls_instance;
static size_t loader_platform_combine_path(char *dest, size_t len, ...);
struct loader_phys_dev_per_icd {
uint32_t count;
VkPhysicalDevice *phys_devs;
struct loader_icd_term *this_icd_term;
};
enum loader_debug {
LOADER_INFO_BIT = 0x01,
LOADER_WARN_BIT = 0x02,
LOADER_PERF_BIT = 0x04,
LOADER_ERROR_BIT = 0x08,
LOADER_DEBUG_BIT = 0x10,
};
uint32_t g_loader_debug = 0;
uint32_t g_loader_log_msgs = 0;
// thread safety lock for accessing global data structures such as "loader"
// all entrypoints on the instance chain need to be locked except GPA
// additionally CreateDevice and DestroyDevice needs to be locked
loader_platform_thread_mutex loader_lock;
loader_platform_thread_mutex loader_json_lock;
LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope alloc_scope) {
void *pMemory = NULL;
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
{
#else
if (instance && instance->alloc_callbacks.pfnAllocation) {
// These are internal structures, so it's best to align everything to
// the largest unit size which is the size of a uint64_t.
pMemory = instance->alloc_callbacks.pfnAllocation(instance->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
} else {
#endif
pMemory = malloc(size);
}
return pMemory;
}
void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory) {
if (pMemory != NULL) {
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
{
#else
if (instance && instance->alloc_callbacks.pfnFree) {
instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData, pMemory);
} else {
#endif
free(pMemory);
}
}
}
void *loader_instance_heap_realloc(const struct loader_instance *instance, void *pMemory, size_t orig_size, size_t size,
VkSystemAllocationScope alloc_scope) {
void *pNewMem = NULL;
if (pMemory == NULL || orig_size == 0) {
pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope);
} else if (size == 0) {
loader_instance_heap_free(instance, pMemory);
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
#else
} else if (instance && instance->alloc_callbacks.pfnReallocation) {
// These are internal structures, so it's best to align everything to
// the largest unit size which is the size of a uint64_t.
pNewMem = instance->alloc_callbacks.pfnReallocation(instance->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
alloc_scope);
#endif
} else {
pNewMem = realloc(pMemory, size);
}
return pNewMem;
}
void *loader_instance_tls_heap_alloc(size_t size) {
return loader_instance_heap_alloc(tls_instance, size, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
}
void loader_instance_tls_heap_free(void *pMemory) { loader_instance_heap_free(tls_instance, pMemory); }
void *loader_device_heap_alloc(const struct loader_device *device, size_t size, VkSystemAllocationScope alloc_scope) {
void *pMemory = NULL;
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
{
#else
if (device && device->alloc_callbacks.pfnAllocation) {
// These are internal structures, so it's best to align everything to
// the largest unit size which is the size of a uint64_t.
pMemory = device->alloc_callbacks.pfnAllocation(device->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope);
} else {
#endif
pMemory = malloc(size);
}
return pMemory;
}
void loader_device_heap_free(const struct loader_device *device, void *pMemory) {
if (pMemory != NULL) {
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
{
#else
if (device && device->alloc_callbacks.pfnFree) {
device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData, pMemory);
} else {
#endif
free(pMemory);
}
}
}
void *loader_device_heap_realloc(const struct loader_device *device, void *pMemory, size_t orig_size, size_t size,
VkSystemAllocationScope alloc_scope) {
void *pNewMem = NULL;
if (pMemory == NULL || orig_size == 0) {
pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
} else if (size == 0) {
loader_device_heap_free(device, pMemory);
#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
#else
} else if (device && device->alloc_callbacks.pfnReallocation) {
// These are internal structures, so it's best to align everything to
// the largest unit size which is the size of a uint64_t.
pNewMem = device->alloc_callbacks.pfnReallocation(device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
alloc_scope);
#endif
} else {
pNewMem = realloc(pMemory, size);
}
return pNewMem;
}
// Environment variables
#if defined(__linux__)
static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
// No allocation of memory necessary for Linux, but we should at least touch
// the inst pointer to get rid of compiler warnings.
(void)inst;
return getenv(name);
}
static inline char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
// No allocation of memory necessary for Linux, but we should at least touch
// the inst pointer to get rid of compiler warnings.
(void)inst;
#ifdef HAVE_SECURE_GETENV
return secure_getenv(name);
#elif defined(HAVE___SECURE_GETENV)
return __secure_getenv(name);
#else
#pragma message( \
"Warning: Falling back to non-secure getenv for environmental lookups! Consider" \
" updating to a different libc.")
return loader_getenv(name, inst);
#endif
}
static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
// No freeing of memory necessary for Linux, but we should at least touch
// the val and inst pointers to get rid of compiler warnings.
(void)val;
(void)inst;
}
#elif defined(WIN32)
static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
char *retVal;
DWORD valSize;
valSize = GetEnvironmentVariableA(name, NULL, 0);
// valSize DOES include the null terminator, so for any set variable
// will always be at least 1. If it's 0, the variable wasn't set.
if (valSize == 0) return NULL;
// Allocate the space necessary for the registry entry
if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) {
retVal = (char *)inst->alloc_callbacks.pfnAllocation(inst->alloc_callbacks.pUserData, valSize, sizeof(char *),
VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
} else {
retVal = (char *)malloc(valSize);
}
if (NULL != retVal) {
GetEnvironmentVariableA(name, retVal, valSize);
}
return retVal;
}
static inline char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
// No secure version for Windows as far as I know
return loader_getenv(name, inst);
}
static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) {
inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val);
} else {
free((void *)val);
}
}
#else
static inline char *loader_getenv(const char *name, const struct loader_instance *inst) {
// stub func
(void)inst;
(void)name;
return NULL;
}
static inline void loader_free_getenv(char *val, const struct loader_instance *inst) {
// stub func
(void)val;
(void)inst;
}
#endif
void loader_log(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code, const char *format, ...) {
char msg[512];
char cmd_line_msg[512];
size_t cmd_line_size = sizeof(cmd_line_msg);
va_list ap;
int ret;
va_start(ap, format);
ret = vsnprintf(msg, sizeof(msg), format, ap);
if ((ret >= (int)sizeof(msg)) || ret < 0) {
msg[sizeof(msg) - 1] = '\0';
}
va_end(ap);
if (inst) {
util_DebugReportMessage(inst, msg_type, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, (uint64_t)(uintptr_t)inst, 0, msg_code,
"loader", msg);
}
if (!(msg_type & g_loader_log_msgs)) {
return;
}
cmd_line_msg[0] = '\0';
cmd_line_size -= 1;
size_t original_size = cmd_line_size;
va_start(ap, format);
if ((msg_type & LOADER_INFO_BIT) != 0) {
strncat(cmd_line_msg, "INFO", cmd_line_size);
cmd_line_size -= 4;
}
if ((msg_type & LOADER_WARN_BIT) != 0) {
if (cmd_line_size != original_size) {
strncat(cmd_line_msg, " | ", cmd_line_size);
cmd_line_size -= 3;
}
strncat(cmd_line_msg, "WARNING", cmd_line_size);
cmd_line_size -= 7;
}
if ((msg_type & LOADER_PERF_BIT) != 0) {
if (cmd_line_size != original_size) {
strncat(cmd_line_msg, " | ", cmd_line_size);
cmd_line_size -= 3;
}
strncat(cmd_line_msg, "PERF", cmd_line_size);
cmd_line_size -= 4;
}
if ((msg_type & LOADER_ERROR_BIT) != 0) {
if (cmd_line_size != original_size) {
strncat(cmd_line_msg, " | ", cmd_line_size);
cmd_line_size -= 3;
}
strncat(cmd_line_msg, "ERROR", cmd_line_size);
cmd_line_size -= 5;
}
if ((msg_type & LOADER_DEBUG_BIT) != 0) {
if (cmd_line_size != original_size) {
strncat(cmd_line_msg, " | ", cmd_line_size);
cmd_line_size -= 3;
}
strncat(cmd_line_msg, "DEBUG", cmd_line_size);
cmd_line_size -= 5;
}
if (cmd_line_size != original_size) {
strncat(cmd_line_msg, ": ", cmd_line_size);
cmd_line_size -= 2;
}
if (0 < cmd_line_size) {
// If the message is too long, trim it down
if (strlen(msg) > cmd_line_size) {
msg[cmd_line_size - 1] = '\0';
}
strncat(cmd_line_msg, msg, cmd_line_size);
} else {
// Shouldn't get here, but check to make sure if we've already overrun
// the string boundary
assert(false);
}
#if defined(WIN32)
OutputDebugString(cmd_line_msg);
OutputDebugString("\n");
#endif
fputs(cmd_line_msg, stderr);
fputc('\n', stderr);
}
VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceDispatch(VkInstance instance, void *object) {
struct loader_instance *inst = loader_get_instance(instance);
if (!inst) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"vkSetInstanceDispatch: Can not retrieve Instance "
"dispatch table.");
return VK_ERROR_INITIALIZATION_FAILED;
}
loader_set_dispatch(object, inst->disp);
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceDispatch(VkDevice device, void *object) {
struct loader_device *dev;
struct loader_icd_term *icd_term = loader_get_icd_and_device(device, &dev, NULL);
if (NULL == icd_term) {
return VK_ERROR_INITIALIZATION_FAILED;
}
loader_set_dispatch(object, &dev->loader_dispatch);
return VK_SUCCESS;
}
#if defined(WIN32)
static char *loader_get_next_path(char *path);
// Find the list of registry files (names within a key) in key "location".
//
// This function looks in the registry (hive = DEFAULT_VK_REGISTRY_HIVE) key as
// given in "location"
// for a list or name/values which are added to a returned list (function return
// value).
// The DWORD values within the key must be 0 or they are skipped.
// Function return is a string with a ';' separated list of filenames.
// Function return is NULL if no valid name/value pairs are found in the key,
// or the key is not found.
//
// *reg_data contains a string list of filenames as pointer.
// When done using the returned string list, the caller should free the pointer.
VkResult loaderGetRegistryFiles(const struct loader_instance *inst, char *location, bool use_secondary_hive, char **reg_data) {
LONG rtn_value;
HKEY hive = DEFAULT_VK_REGISTRY_HIVE, key;
DWORD access_flags;
char name[2048];
char *loc = location;
char *next;
DWORD idx;
DWORD name_size = sizeof(name);
DWORD value;
DWORD total_size = 4096;
DWORD value_size = sizeof(value);
VkResult result = VK_SUCCESS;
bool found = false;
if (NULL == reg_data) {
result = VK_ERROR_INITIALIZATION_FAILED;
goto out;
}
while (*loc) {
next = loader_get_next_path(loc);
access_flags = KEY_QUERY_VALUE;
rtn_value = RegOpenKeyEx(hive, loc, 0, access_flags, &key);
if (ERROR_SUCCESS == rtn_value) {
idx = 0;
while ((rtn_value = RegEnumValue(key, idx++, name, &name_size, NULL, NULL, (LPBYTE)&value, &value_size)) ==
ERROR_SUCCESS) {
if (value_size == sizeof(value) && value == 0) {
if (NULL == *reg_data) {
*reg_data = loader_instance_heap_alloc(inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (NULL == *reg_data) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loaderGetRegistryFiles: Failed to allocate space for registry data for key %s", name);
result = VK_ERROR_OUT_OF_HOST_MEMORY;
goto out;
}
*reg_data[0] = '\0';
} else if (strlen(*reg_data) + name_size + 1 > total_size) {
void *new_ptr = loader_instance_heap_realloc(inst, *reg_data, total_size, total_size * 2,
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (NULL == new_ptr) {
loader_log(
inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loaderGetRegistryFiles: Failed to reallocate space for registry value of size %d for key %s",
total_size * 2, name);
result = VK_ERROR_OUT_OF_HOST_MEMORY;
goto out;
}
*reg_data = new_ptr;
total_size *= 2;
}
loader_log(
inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0, "Located json file \"%s\" from registry \"%s\\%s\"", name,
hive == DEFAULT_VK_REGISTRY_HIVE ? DEFAULT_VK_REGISTRY_HIVE_STR : SECONDARY_VK_REGISTRY_HIVE_STR, location);
if (strlen(*reg_data) == 0) {
(void)snprintf(*reg_data, name_size + 1, "%s", name);
} else {
(void)snprintf(*reg_data + strlen(*reg_data), name_size + 2, "%c%s", PATH_SEPARATOR, name);
}
found = true;
}
name_size = 2048;
}
}
// Advance the location - if the next location is in the secondary hive, then reset the locations and advance the hive
if (use_secondary_hive && (hive == DEFAULT_VK_REGISTRY_HIVE) && (*next == '\0')) {
loc = location;
hive = SECONDARY_VK_REGISTRY_HIVE;
} else {
loc = next;
}
}
if (!found) {
result = VK_ERROR_INITIALIZATION_FAILED;
}
out:
return result;
}
#endif // WIN32
// Combine path elements, separating each element with the platform-specific
// directory separator, and save the combined string to a destination buffer,
// not exceeding the given length. Path elements are given as variable args,
// with a NULL element terminating the list.
//
// \returns the total length of the combined string, not including an ASCII
// NUL termination character. This length may exceed the available storage:
// in this case, the written string will be truncated to avoid a buffer
// overrun, and the return value will greater than or equal to the storage
// size. A NULL argument may be provided as the destination buffer in order
// to determine the required string length without actually writing a string.
static size_t loader_platform_combine_path(char *dest, size_t len, ...) {
size_t required_len = 0;
va_list ap;
const char *component;
va_start(ap, len);
while ((component = va_arg(ap, const char *))) {
if (required_len > 0) {
// This path element is not the first non-empty element; prepend
// a directory separator if space allows
if (dest && required_len + 1 < len) {
(void)snprintf(dest + required_len, len - required_len, "%c", DIRECTORY_SYMBOL);
}
required_len++;
}
if (dest && required_len < len) {
strncpy(dest + required_len, component, len - required_len);
}
required_len += strlen(component);
}
va_end(ap);
// strncpy(3) won't add a NUL terminating byte in the event of truncation.
if (dest && required_len >= len) {
dest[len - 1] = '\0';
}
return required_len;
}
// Given string of three part form "maj.min.pat" convert to a vulkan version number.
static uint32_t loader_make_version(char *vers_str) {
uint32_t vers = 0, major = 0, minor = 0, patch = 0;
char *vers_tok;
if (!vers_str) {
return vers;
}
vers_tok = strtok(vers_str, ".\"\n\r");
if (NULL != vers_tok) {
major = (uint16_t)atoi(vers_tok);
vers_tok = strtok(NULL, ".\"\n\r");
if (NULL != vers_tok) {
minor = (uint16_t)atoi(vers_tok);
vers_tok = strtok(NULL, ".\"\n\r");
if (NULL != vers_tok) {
patch = (uint16_t)atoi(vers_tok);
}
}
}
return VK_MAKE_VERSION(major, minor, patch);
}
bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2) {
return strcmp(op1->extensionName, op2->extensionName) == 0 ? true : false;
}
// Search the given ext_array for an extension matching the given vk_ext_prop
bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop, const uint32_t count,
const VkExtensionProperties *ext_array) {
for (uint32_t i = 0; i < count; i++) {
if (compare_vk_extension_properties(vk_ext_prop, &ext_array[i])) return true;
}
return false;
}
// Search the given ext_list for an extension matching the given vk_ext_prop
bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop, const struct loader_extension_list *ext_list) {
for (uint32_t i = 0; i < ext_list->count; i++) {
if (compare_vk_extension_properties(&ext_list->list[i], vk_ext_prop)) return true;
}
return false;
}
// Search the given ext_list for a device extension matching the given ext_prop
bool has_vk_dev_ext_property(const VkExtensionProperties *ext_prop, const struct loader_device_extension_list *ext_list) {
for (uint32_t i = 0; i < ext_list->count; i++) {
if (compare_vk_extension_properties(&ext_list->list[i].props, ext_prop)) return true;
}
return false;
}
// Search the given layer list for a layer matching the given layer name
static struct loader_layer_properties *loader_get_layer_property(const char *name, const struct loader_layer_list *layer_list) {
for (uint32_t i = 0; i < layer_list->count; i++) {
const VkLayerProperties *item = &layer_list->list[i].info;
if (strcmp(name, item->layerName) == 0) return &layer_list->list[i];
}
return NULL;
}
// Get the next unused layer property in the list. Init the property to zero.
static struct loader_layer_properties *loader_get_next_layer_property(const struct loader_instance *inst,
struct loader_layer_list *layer_list) {
if (layer_list->capacity == 0) {
layer_list->list =
loader_instance_heap_alloc(inst, sizeof(struct loader_layer_properties) * 64, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (layer_list->list == NULL) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_get_next_layer_property: Out of memory can "
"not add any layer properties to list");
return NULL;
}
memset(layer_list->list, 0, sizeof(struct loader_layer_properties) * 64);
layer_list->capacity = sizeof(struct loader_layer_properties) * 64;
}
// Ensure enough room to add an entry
if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) > layer_list->capacity) {
void *new_ptr = loader_instance_heap_realloc(inst, layer_list->list, layer_list->capacity, layer_list->capacity * 2,
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (NULL == new_ptr) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0, "loader_get_next_layer_property: realloc failed for layer list");
return NULL;
}
layer_list->list = new_ptr;
layer_list->capacity *= 2;
}
layer_list->count++;
return &(layer_list->list[layer_list->count - 1]);
}
// Remove all layer properties entries from the list
void loader_delete_layer_properties(const struct loader_instance *inst, struct loader_layer_list *layer_list) {
uint32_t i, j;
struct loader_device_extension_list *dev_ext_list;
if (!layer_list) return;
for (i = 0; i < layer_list->count; i++) {
if (NULL != layer_list->list[i].component_layer_names) {
loader_instance_heap_free(inst, layer_list->list[i].component_layer_names);
layer_list->list[i].component_layer_names = NULL;
}
loader_destroy_generic_list(inst, (struct loader_generic_list *)&layer_list->list[i].instance_extension_list);
dev_ext_list = &layer_list->list[i].device_extension_list;
if (dev_ext_list->capacity > 0 && NULL != dev_ext_list->list && dev_ext_list->list->entrypoint_count > 0) {
for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
}
loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
}
loader_destroy_generic_list(inst, (struct loader_generic_list *)dev_ext_list);
}
layer_list->count = 0;
if (layer_list->capacity > 0) {
layer_list->capacity = 0;
loader_instance_heap_free(inst, layer_list->list);
}
}
static VkResult loader_add_instance_extensions(const struct loader_instance *inst,
const PFN_vkEnumerateInstanceExtensionProperties fp_get_props, const char *lib_name,
struct loader_extension_list *ext_list) {
uint32_t i, count = 0;
VkExtensionProperties *ext_props;
VkResult res = VK_SUCCESS;
if (!fp_get_props) {
// No EnumerateInstanceExtensionProperties defined
goto out;
}
res = fp_get_props(NULL, &count, NULL);
if (res != VK_SUCCESS) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_instance_extensions: Error getting Instance "
"extension count from %s",
lib_name);
goto out;
}
if (count == 0) {
// No ExtensionProperties to report
goto out;
}
ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
if (NULL == ext_props) {
res = VK_ERROR_OUT_OF_HOST_MEMORY;
goto out;
}
res = fp_get_props(NULL, &count, ext_props);
if (res != VK_SUCCESS) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_instance_extensions: Error getting Instance "
"extensions from %s",
lib_name);
goto out;
}
for (i = 0; i < count; i++) {
char spec_version[64];
bool ext_unsupported = wsi_unsupported_instance_extension(&ext_props[i]);
if (!ext_unsupported) {
(void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_VERSION_MAJOR(ext_props[i].specVersion),
VK_VERSION_MINOR(ext_props[i].specVersion), VK_VERSION_PATCH(ext_props[i].specVersion));
loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Instance Extension: %s (%s) version %s", ext_props[i].extensionName,
lib_name, spec_version);
res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
if (res != VK_SUCCESS) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_instance_extensions: Failed to add %s "
"to Instance extension list",
lib_name);
goto out;
}
}
}
out:
return res;
}
// Initialize ext_list with the physical device extensions.
// The extension properties are passed as inputs in count and ext_props.
static VkResult loader_init_device_extensions(const struct loader_instance *inst, struct loader_physical_device_term *phys_dev_term,
uint32_t count, VkExtensionProperties *ext_props,
struct loader_extension_list *ext_list) {
VkResult res;
uint32_t i;
res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
if (VK_SUCCESS != res) {
return res;
}
for (i = 0; i < count; i++) {
char spec_version[64];
(void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_VERSION_MAJOR(ext_props[i].specVersion),
VK_VERSION_MINOR(ext_props[i].specVersion), VK_VERSION_PATCH(ext_props[i].specVersion));
loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
phys_dev_term->this_icd_term->scanned_icd->lib_name, spec_version);
res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
if (res != VK_SUCCESS) return res;
}
return VK_SUCCESS;
}
VkResult loader_add_device_extensions(const struct loader_instance *inst,
PFN_vkEnumerateDeviceExtensionProperties fpEnumerateDeviceExtensionProperties,
VkPhysicalDevice physical_device, const char *lib_name,
struct loader_extension_list *ext_list) {
uint32_t i, count;
VkResult res;
VkExtensionProperties *ext_props;
res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, NULL);
if (res == VK_SUCCESS && count > 0) {
ext_props = loader_stack_alloc(count * sizeof(VkExtensionProperties));
if (!ext_props) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_device_extensions: Failed to allocate space"
" for device extension properties.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
res = fpEnumerateDeviceExtensionProperties(physical_device, NULL, &count, ext_props);
if (res != VK_SUCCESS) {
return res;
}
for (i = 0; i < count; i++) {
char spec_version[64];
(void)snprintf(spec_version, sizeof(spec_version), "%d.%d.%d", VK_VERSION_MAJOR(ext_props[i].specVersion),
VK_VERSION_MINOR(ext_props[i].specVersion), VK_VERSION_PATCH(ext_props[i].specVersion));
loader_log(inst, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0, "Device Extension: %s (%s) version %s", ext_props[i].extensionName,
lib_name, spec_version);
res = loader_add_to_ext_list(inst, ext_list, 1, &ext_props[i]);
if (res != VK_SUCCESS) {
return res;
}
}
} else {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_device_extensions: Error getting physical "
"device extension info count from library %s",
lib_name);
return res;
}
return VK_SUCCESS;
}
VkResult loader_init_generic_list(const struct loader_instance *inst, struct loader_generic_list *list_info, size_t element_size) {
size_t capacity = 32 * element_size;
list_info->count = 0;
list_info->capacity = 0;
list_info->list = loader_instance_heap_alloc(inst, capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (list_info->list == NULL) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_init_generic_list: Failed to allocate space "
"for generic list");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
memset(list_info->list, 0, capacity);
list_info->capacity = capacity;
return VK_SUCCESS;
}
void loader_destroy_generic_list(const struct loader_instance *inst, struct loader_generic_list *list) {
loader_instance_heap_free(inst, list->list);
list->count = 0;
list->capacity = 0;
}
// Append non-duplicate extension properties defined in props to the given ext_list.
// Return - Vk_SUCCESS on success
VkResult loader_add_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list,
uint32_t prop_list_count, const VkExtensionProperties *props) {
uint32_t i;
const VkExtensionProperties *cur_ext;
if (ext_list->list == NULL || ext_list->capacity == 0) {
VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(VkExtensionProperties));
if (VK_SUCCESS != res) {
return res;
}
}
for (i = 0; i < prop_list_count; i++) {
cur_ext = &props[i];
// look for duplicates
if (has_vk_extension_property(cur_ext, ext_list)) {
continue;
}
// add to list at end
// check for enough capacity
if (ext_list->count * sizeof(VkExtensionProperties) >= ext_list->capacity) {
void *new_ptr = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (new_ptr == NULL) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_to_ext_list: Failed to reallocate "
"space for extension list");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
ext_list->list = new_ptr;
// double capacity
ext_list->capacity *= 2;
}
memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(VkExtensionProperties));
ext_list->count++;
}
return VK_SUCCESS;
}
// Append one extension property defined in props with entrypoints defined in entries to the given
// ext_list. Do not append if a duplicate.
// Return - Vk_SUCCESS on success
VkResult loader_add_to_dev_ext_list(const struct loader_instance *inst, struct loader_device_extension_list *ext_list,
const VkExtensionProperties *props, uint32_t entry_count, char **entrys) {
uint32_t idx;
if (ext_list->list == NULL || ext_list->capacity == 0) {
VkResult res = loader_init_generic_list(inst, (struct loader_generic_list *)ext_list, sizeof(struct loader_dev_ext_props));
if (VK_SUCCESS != res) {
return res;
}
}
// look for duplicates
if (has_vk_dev_ext_property(props, ext_list)) {
return VK_SUCCESS;
}
idx = ext_list->count;
// add to list at end
// check for enough capacity
if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
void *new_ptr = loader_instance_heap_realloc(inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (NULL == new_ptr) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_to_dev_ext_list: Failed to reallocate space for device extension list");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
ext_list->list = new_ptr;
// double capacity
ext_list->capacity *= 2;
}
memcpy(&ext_list->list[idx].props, props, sizeof(*props));
ext_list->list[idx].entrypoint_count = entry_count;
if (entry_count == 0) {
ext_list->list[idx].entrypoints = NULL;
} else {
ext_list->list[idx].entrypoints =
loader_instance_heap_alloc(inst, sizeof(char *) * entry_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (ext_list->list[idx].entrypoints == NULL) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_to_dev_ext_list: Failed to allocate space "
"for device extension entrypoint list in list %d",
idx);
ext_list->list[idx].entrypoint_count = 0;
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
for (uint32_t i = 0; i < entry_count; i++) {
ext_list->list[idx].entrypoints[i] =
loader_instance_heap_alloc(inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (ext_list->list[idx].entrypoints[i] == NULL) {
for (uint32_t j = 0; j < i; j++) {
loader_instance_heap_free(inst, ext_list->list[idx].entrypoints[j]);
}
loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
ext_list->list[idx].entrypoint_count = 0;
ext_list->list[idx].entrypoints = NULL;
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_to_dev_ext_list: Failed to allocate space "
"for device extension entrypoint %d name",
i);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
}
}
ext_list->count++;
return VK_SUCCESS;
}
// Prototype of loader_add_meta_layer function since we use it in the loader_add_implicit_layer, but can also
// call loader_add_implicit_layer from loader_add_meta_layer.
bool loader_add_meta_layer(const struct loader_instance *inst, const struct loader_layer_properties *prop,
struct loader_layer_list *target_list, struct loader_layer_list *expanded_target_list,
const struct loader_layer_list *source_list);
// Search the given layer list for a list matching the given VkLayerProperties
bool has_vk_layer_property(const VkLayerProperties *vk_layer_prop, const struct loader_layer_list *list) {
for (uint32_t i = 0; i < list->count; i++) {
if (strcmp(vk_layer_prop->layerName, list->list[i].info.layerName) == 0) return true;
}
return false;
}
// Search the given layer list for a layer matching the given name
bool has_layer_name(const char *name, const struct loader_layer_list *list) {
for (uint32_t i = 0; i < list->count; i++) {
if (strcmp(name, list->list[i].info.layerName) == 0) return true;
}
return false;
}
// Search the given search_list for any layers in the props list. Add these to the
// output layer_list. Don't add duplicates to the output layer_list.
static VkResult loader_add_layer_names_to_list(const struct loader_instance *inst, struct loader_layer_list *output_list,
struct loader_layer_list *expanded_output_list, uint32_t name_count,
const char *const *names, const struct loader_layer_list *source_list) {
struct loader_layer_properties *layer_prop;
VkResult err = VK_SUCCESS;
for (uint32_t i = 0; i < name_count; i++) {
const char *source_name = names[i];
layer_prop = loader_get_layer_property(source_name, source_list);
if (NULL == layer_prop) {
loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
"loader_add_layer_names_to_list: Unable to find layer"
" %s",
source_name);
err = VK_ERROR_LAYER_NOT_PRESENT;
continue;
}
// If not a meta-layer, simply add it.
if (0 == (layer_prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
if (!has_vk_layer_property(&layer_prop->info, output_list)) {
loader_add_to_layer_list(inst, output_list, 1, layer_prop);
}
if (!has_vk_layer_property(&layer_prop->info, expanded_output_list)) {
loader_add_to_layer_list(inst, expanded_output_list, 1, layer_prop);
}
} else {