xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_instance.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "vk_instance.h"
25 
26 #include "util/libdrm.h"
27 #include "util/perf/cpu_trace.h"
28 
29 #include "vk_alloc.h"
30 #include "vk_common_entrypoints.h"
31 #include "vk_dispatch_trampolines.h"
32 #include "vk_log.h"
33 #include "vk_util.h"
34 #include "vk_debug_utils.h"
35 #include "vk_physical_device.h"
36 
37 #if !VK_LITE_RUNTIME_INSTANCE
38 #include "compiler/glsl_types.h"
39 #endif
40 
41 #define VERSION_IS_1_0(version) \
42    (VK_API_VERSION_MAJOR(version) == 1 && VK_API_VERSION_MINOR(version) == 0)
43 
44 static const struct debug_control trace_options[] = {
45    {"rmv", VK_TRACE_MODE_RMV},
46    {NULL, 0},
47 };
48 
49 VkResult
vk_instance_init(struct vk_instance * instance,const struct vk_instance_extension_table * supported_extensions,const struct vk_instance_dispatch_table * dispatch_table,const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc)50 vk_instance_init(struct vk_instance *instance,
51                  const struct vk_instance_extension_table *supported_extensions,
52                  const struct vk_instance_dispatch_table *dispatch_table,
53                  const VkInstanceCreateInfo *pCreateInfo,
54                  const VkAllocationCallbacks *alloc)
55 {
56    memset(instance, 0, sizeof(*instance));
57    vk_object_base_instance_init(instance, &instance->base, VK_OBJECT_TYPE_INSTANCE);
58    instance->alloc = *alloc;
59 
60    util_cpu_trace_init();
61 
62    /* VK_EXT_debug_utils */
63    /* These messengers will only be used during vkCreateInstance or
64     * vkDestroyInstance calls.  We do this first so that it's safe to use
65     * vk_errorf and friends.
66     */
67    list_inithead(&instance->debug_utils.instance_callbacks);
68    vk_foreach_struct_const(ext, pCreateInfo->pNext) {
69       if (ext->sType ==
70           VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
71          const VkDebugUtilsMessengerCreateInfoEXT *debugMessengerCreateInfo =
72             (const VkDebugUtilsMessengerCreateInfoEXT *)ext;
73          struct vk_debug_utils_messenger *messenger =
74             vk_alloc2(alloc, alloc, sizeof(struct vk_debug_utils_messenger), 8,
75                       VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
76 
77          if (!messenger)
78             return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
79 
80          vk_object_base_instance_init(instance, &messenger->base,
81                                       VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT);
82 
83          messenger->alloc = *alloc;
84          messenger->severity = debugMessengerCreateInfo->messageSeverity;
85          messenger->type = debugMessengerCreateInfo->messageType;
86          messenger->callback = debugMessengerCreateInfo->pfnUserCallback;
87          messenger->data = debugMessengerCreateInfo->pUserData;
88 
89          list_addtail(&messenger->link,
90                       &instance->debug_utils.instance_callbacks);
91       }
92    }
93 
94    uint32_t instance_version = VK_API_VERSION_1_0;
95    if (dispatch_table->EnumerateInstanceVersion)
96       dispatch_table->EnumerateInstanceVersion(&instance_version);
97 
98    instance->app_info = (struct vk_app_info) { .api_version = 0 };
99    if (pCreateInfo->pApplicationInfo) {
100       const VkApplicationInfo *app = pCreateInfo->pApplicationInfo;
101 
102       instance->app_info.app_name =
103          vk_strdup(&instance->alloc, app->pApplicationName,
104                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
105       instance->app_info.app_version = app->applicationVersion;
106 
107       instance->app_info.engine_name =
108          vk_strdup(&instance->alloc, app->pEngineName,
109                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
110       instance->app_info.engine_version = app->engineVersion;
111 
112       instance->app_info.api_version = app->apiVersion;
113    }
114 
115    /* From the Vulkan 1.2.199 spec:
116     *
117     *    "Note:
118     *
119     *    Providing a NULL VkInstanceCreateInfo::pApplicationInfo or providing
120     *    an apiVersion of 0 is equivalent to providing an apiVersion of
121     *    VK_MAKE_API_VERSION(0,1,0,0)."
122     */
123    if (instance->app_info.api_version == 0)
124       instance->app_info.api_version = VK_API_VERSION_1_0;
125 
126    /* From the Vulkan 1.2.199 spec:
127     *
128     *    VUID-VkApplicationInfo-apiVersion-04010
129     *
130     *    "If apiVersion is not 0, then it must be greater than or equal to
131     *    VK_API_VERSION_1_0"
132     */
133    assert(instance->app_info.api_version >= VK_API_VERSION_1_0);
134 
135    /* From the Vulkan 1.2.199 spec:
136     *
137     *    "Vulkan 1.0 implementations were required to return
138     *    VK_ERROR_INCOMPATIBLE_DRIVER if apiVersion was larger than 1.0.
139     *    Implementations that support Vulkan 1.1 or later must not return
140     *    VK_ERROR_INCOMPATIBLE_DRIVER for any value of apiVersion."
141     */
142    if (VERSION_IS_1_0(instance_version) &&
143        !VERSION_IS_1_0(instance->app_info.api_version))
144       return VK_ERROR_INCOMPATIBLE_DRIVER;
145 
146    instance->supported_extensions = supported_extensions;
147 
148    for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
149       int idx;
150       for (idx = 0; idx < VK_INSTANCE_EXTENSION_COUNT; idx++) {
151          if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
152                     vk_instance_extensions[idx].extensionName) == 0)
153             break;
154       }
155 
156       if (idx >= VK_INSTANCE_EXTENSION_COUNT)
157          return vk_errorf(instance, VK_ERROR_EXTENSION_NOT_PRESENT,
158                           "%s not supported",
159                           pCreateInfo->ppEnabledExtensionNames[i]);
160 
161       if (!supported_extensions->extensions[idx])
162          return vk_errorf(instance, VK_ERROR_EXTENSION_NOT_PRESENT,
163                           "%s not supported",
164                           pCreateInfo->ppEnabledExtensionNames[i]);
165 
166 #ifdef ANDROID_STRICT
167       if (!vk_android_allowed_instance_extensions.extensions[idx])
168          return vk_errorf(instance, VK_ERROR_EXTENSION_NOT_PRESENT,
169                           "%s not supported",
170                           pCreateInfo->ppEnabledExtensionNames[i]);
171 #endif
172 
173       instance->enabled_extensions.extensions[idx] = true;
174    }
175 
176    instance->dispatch_table = *dispatch_table;
177 
178    /* Add common entrypoints without overwriting driver-provided ones. */
179    vk_instance_dispatch_table_from_entrypoints(
180       &instance->dispatch_table, &vk_common_instance_entrypoints, false);
181 
182    if (mtx_init(&instance->debug_report.callbacks_mutex, mtx_plain) != 0)
183       return vk_error(instance, VK_ERROR_INITIALIZATION_FAILED);
184 
185    list_inithead(&instance->debug_report.callbacks);
186 
187    if (mtx_init(&instance->debug_utils.callbacks_mutex, mtx_plain) != 0) {
188       mtx_destroy(&instance->debug_report.callbacks_mutex);
189       return vk_error(instance, VK_ERROR_INITIALIZATION_FAILED);
190    }
191 
192    list_inithead(&instance->debug_utils.callbacks);
193 
194    list_inithead(&instance->physical_devices.list);
195 
196    if (mtx_init(&instance->physical_devices.mutex, mtx_plain) != 0) {
197       mtx_destroy(&instance->debug_report.callbacks_mutex);
198       mtx_destroy(&instance->debug_utils.callbacks_mutex);
199       return vk_error(instance, VK_ERROR_INITIALIZATION_FAILED);
200    }
201 
202    instance->trace_mode = parse_debug_string(getenv("MESA_VK_TRACE"), trace_options);
203    instance->trace_frame = (uint32_t)debug_get_num_option("MESA_VK_TRACE_FRAME", 0xFFFFFFFF);
204    instance->trace_trigger_file = secure_getenv("MESA_VK_TRACE_TRIGGER");
205 
206 #if !VK_LITE_RUNTIME_INSTANCE
207    glsl_type_singleton_init_or_ref();
208 #endif
209 
210    return VK_SUCCESS;
211 }
212 
213 static void
destroy_physical_devices(struct vk_instance * instance)214 destroy_physical_devices(struct vk_instance *instance)
215 {
216    list_for_each_entry_safe(struct vk_physical_device, pdevice,
217                             &instance->physical_devices.list, link) {
218       list_del(&pdevice->link);
219       instance->physical_devices.destroy(pdevice);
220    }
221 }
222 
223 void
vk_instance_finish(struct vk_instance * instance)224 vk_instance_finish(struct vk_instance *instance)
225 {
226    destroy_physical_devices(instance);
227 
228 #if !VK_LITE_RUNTIME_INSTANCE
229    glsl_type_singleton_decref();
230 #endif
231 
232    if (unlikely(!list_is_empty(&instance->debug_utils.callbacks))) {
233       list_for_each_entry_safe(struct vk_debug_utils_messenger, messenger,
234                                &instance->debug_utils.callbacks, link) {
235          list_del(&messenger->link);
236          vk_object_base_finish(&messenger->base);
237          vk_free2(&instance->alloc, &messenger->alloc, messenger);
238       }
239    }
240    if (unlikely(!list_is_empty(&instance->debug_utils.instance_callbacks))) {
241       list_for_each_entry_safe(struct vk_debug_utils_messenger, messenger,
242                                &instance->debug_utils.instance_callbacks,
243                                link) {
244          list_del(&messenger->link);
245          vk_object_base_finish(&messenger->base);
246          vk_free2(&instance->alloc, &messenger->alloc, messenger);
247       }
248    }
249    mtx_destroy(&instance->debug_report.callbacks_mutex);
250    mtx_destroy(&instance->debug_utils.callbacks_mutex);
251    mtx_destroy(&instance->physical_devices.mutex);
252    vk_free(&instance->alloc, (char *)instance->app_info.app_name);
253    vk_free(&instance->alloc, (char *)instance->app_info.engine_name);
254    vk_object_base_finish(&instance->base);
255 }
256 
257 VkResult
vk_enumerate_instance_extension_properties(const struct vk_instance_extension_table * supported_extensions,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)258 vk_enumerate_instance_extension_properties(
259     const struct vk_instance_extension_table *supported_extensions,
260     uint32_t *pPropertyCount,
261     VkExtensionProperties *pProperties)
262 {
263    VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
264 
265    for (int i = 0; i < VK_INSTANCE_EXTENSION_COUNT; i++) {
266       if (!supported_extensions->extensions[i])
267          continue;
268 
269 #ifdef ANDROID_STRICT
270       if (!vk_android_allowed_instance_extensions.extensions[i])
271          continue;
272 #endif
273 
274       vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
275          *prop = vk_instance_extensions[i];
276       }
277    }
278 
279    return vk_outarray_status(&out);
280 }
281 
282 PFN_vkVoidFunction
vk_instance_get_proc_addr(const struct vk_instance * instance,const struct vk_instance_entrypoint_table * entrypoints,const char * name)283 vk_instance_get_proc_addr(const struct vk_instance *instance,
284                           const struct vk_instance_entrypoint_table *entrypoints,
285                           const char *name)
286 {
287    PFN_vkVoidFunction func;
288 
289    /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
290     * when we have to return valid function pointers, NULL, or it's left
291     * undefined.  See the table for exact details.
292     */
293    if (name == NULL)
294       return NULL;
295 
296 #define LOOKUP_VK_ENTRYPOINT(entrypoint) \
297    if (strcmp(name, "vk" #entrypoint) == 0) \
298       return (PFN_vkVoidFunction)entrypoints->entrypoint
299 
300    LOOKUP_VK_ENTRYPOINT(EnumerateInstanceExtensionProperties);
301    LOOKUP_VK_ENTRYPOINT(EnumerateInstanceLayerProperties);
302    LOOKUP_VK_ENTRYPOINT(EnumerateInstanceVersion);
303    LOOKUP_VK_ENTRYPOINT(CreateInstance);
304 
305    /* GetInstanceProcAddr() can also be called with a NULL instance.
306     * See https://gitlab.khronos.org/vulkan/vulkan/issues/2057
307     */
308    LOOKUP_VK_ENTRYPOINT(GetInstanceProcAddr);
309 
310 #undef LOOKUP_VK_ENTRYPOINT
311 
312    /* Beginning with ICD interface v7, the following functions can also be
313     * retrieved via vk_icdGetInstanceProcAddr.
314     */
315 
316    if (strcmp(name, "vk_icdNegotiateLoaderICDInterfaceVersion") == 0)
317       return (PFN_vkVoidFunction)vk_icdNegotiateLoaderICDInterfaceVersion;
318    if (strcmp(name, "vk_icdGetPhysicalDeviceProcAddr") == 0)
319       return (PFN_vkVoidFunction)vk_icdGetPhysicalDeviceProcAddr;
320 #ifdef _WIN32
321    if (strcmp(name, "vk_icdEnumerateAdapterPhysicalDevices") == 0)
322       return (PFN_vkVoidFunction)vk_icdEnumerateAdapterPhysicalDevices;
323 #endif
324 
325    if (instance == NULL)
326       return NULL;
327 
328    func = vk_instance_dispatch_table_get_if_supported(&instance->dispatch_table,
329                                                       name,
330                                                       instance->app_info.api_version,
331                                                       &instance->enabled_extensions);
332    if (func != NULL)
333       return func;
334 
335    func = vk_physical_device_dispatch_table_get_if_supported(&vk_physical_device_trampolines,
336                                                              name,
337                                                              instance->app_info.api_version,
338                                                              &instance->enabled_extensions);
339    if (func != NULL)
340       return func;
341 
342    func = vk_device_dispatch_table_get_if_supported(&vk_device_trampolines,
343                                                     name,
344                                                     instance->app_info.api_version,
345                                                     &instance->enabled_extensions,
346                                                     NULL);
347    if (func != NULL)
348       return func;
349 
350    return NULL;
351 }
352 
353 PFN_vkVoidFunction
vk_instance_get_proc_addr_unchecked(const struct vk_instance * instance,const char * name)354 vk_instance_get_proc_addr_unchecked(const struct vk_instance *instance,
355                                     const char *name)
356 {
357    PFN_vkVoidFunction func;
358 
359    if (instance == NULL || name == NULL)
360       return NULL;
361 
362    func = vk_instance_dispatch_table_get(&instance->dispatch_table, name);
363    if (func != NULL)
364       return func;
365 
366    func = vk_physical_device_dispatch_table_get(
367       &vk_physical_device_trampolines, name);
368    if (func != NULL)
369       return func;
370 
371    func = vk_device_dispatch_table_get(&vk_device_trampolines, name);
372    if (func != NULL)
373       return func;
374 
375    return NULL;
376 }
377 
378 PFN_vkVoidFunction
vk_instance_get_physical_device_proc_addr(const struct vk_instance * instance,const char * name)379 vk_instance_get_physical_device_proc_addr(const struct vk_instance *instance,
380                                           const char *name)
381 {
382    if (instance == NULL || name == NULL)
383       return NULL;
384 
385    return vk_physical_device_dispatch_table_get_if_supported(&vk_physical_device_trampolines,
386                                                              name,
387                                                              instance->app_info.api_version,
388                                                              &instance->enabled_extensions);
389 }
390 
391 void
vk_instance_add_driver_trace_modes(struct vk_instance * instance,const struct debug_control * modes)392 vk_instance_add_driver_trace_modes(struct vk_instance *instance,
393                                    const struct debug_control *modes)
394 {
395    instance->trace_mode |= parse_debug_string(getenv("MESA_VK_TRACE"), modes);
396 }
397 
398 static VkResult
enumerate_drm_physical_devices_locked(struct vk_instance * instance)399 enumerate_drm_physical_devices_locked(struct vk_instance *instance)
400 {
401    /* libdrm returns a maximum of 256 devices (see MAX_DRM_NODES in libdrm) */
402    drmDevicePtr devices[256];
403    int max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
404 
405    if (max_devices < 1)
406       return VK_SUCCESS;
407 
408    VkResult result;
409    for (uint32_t i = 0; i < (uint32_t)max_devices; i++) {
410       struct vk_physical_device *pdevice;
411       result = instance->physical_devices.try_create_for_drm(instance, devices[i], &pdevice);
412 
413       /* Incompatible DRM device, skip. */
414       if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
415          result = VK_SUCCESS;
416          continue;
417       }
418 
419       /* Error creating the physical device, report the error. */
420       if (result != VK_SUCCESS)
421          break;
422 
423       list_addtail(&pdevice->link, &instance->physical_devices.list);
424    }
425 
426    drmFreeDevices(devices, max_devices);
427    return result;
428 }
429 
430 static VkResult
enumerate_physical_devices_locked(struct vk_instance * instance)431 enumerate_physical_devices_locked(struct vk_instance *instance)
432 {
433    if (instance->physical_devices.enumerate) {
434       VkResult result = instance->physical_devices.enumerate(instance);
435       if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
436          return result;
437    }
438 
439    VkResult result = VK_SUCCESS;
440 
441    if (instance->physical_devices.try_create_for_drm) {
442       result = enumerate_drm_physical_devices_locked(instance);
443       if (result != VK_SUCCESS) {
444          destroy_physical_devices(instance);
445          return result;
446       }
447    }
448 
449    return result;
450 }
451 
452 static VkResult
enumerate_physical_devices(struct vk_instance * instance)453 enumerate_physical_devices(struct vk_instance *instance)
454 {
455    VkResult result = VK_SUCCESS;
456 
457    mtx_lock(&instance->physical_devices.mutex);
458    if (!instance->physical_devices.enumerated) {
459       result = enumerate_physical_devices_locked(instance);
460       if (result == VK_SUCCESS)
461          instance->physical_devices.enumerated = true;
462    }
463    mtx_unlock(&instance->physical_devices.mutex);
464 
465    return result;
466 }
467 
468 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_EnumeratePhysicalDevices(VkInstance _instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)469 vk_common_EnumeratePhysicalDevices(VkInstance _instance, uint32_t *pPhysicalDeviceCount,
470                                    VkPhysicalDevice *pPhysicalDevices)
471 {
472    VK_FROM_HANDLE(vk_instance, instance, _instance);
473    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
474 
475    VkResult result = enumerate_physical_devices(instance);
476    if (result != VK_SUCCESS)
477       return result;
478 
479    list_for_each_entry(struct vk_physical_device, pdevice,
480                        &instance->physical_devices.list, link) {
481       vk_outarray_append_typed(VkPhysicalDevice, &out, element) {
482          *element = vk_physical_device_to_handle(pdevice);
483       }
484    }
485 
486    return vk_outarray_status(&out);
487 }
488 
489 #ifdef _WIN32
490 /* Note: This entrypoint is not exported from ICD DLLs, and is only exposed via
491  * vk_icdGetInstanceProcAddr for loaders with interface v7. This is to avoid
492  * a design flaw in the original loader implementation, which prevented enumeration
493  * of physical devices that didn't have a LUID. This flaw was fixed prior to the
494  * implementation of v7, so v7 loaders are unaffected, and it's safe to support this.
495  */
496 VKAPI_ATTR VkResult VKAPI_CALL
vk_icdEnumerateAdapterPhysicalDevices(VkInstance _instance,LUID adapterLUID,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)497 vk_icdEnumerateAdapterPhysicalDevices(VkInstance _instance, LUID adapterLUID,
498                                       uint32_t *pPhysicalDeviceCount,
499                                       VkPhysicalDevice *pPhysicalDevices)
500 {
501    VK_FROM_HANDLE(vk_instance, instance, _instance);
502    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
503 
504    VkResult result = enumerate_physical_devices(instance);
505    if (result != VK_SUCCESS)
506       return result;
507 
508    list_for_each_entry(struct vk_physical_device, pdevice,
509                        &instance->physical_devices.list, link) {
510       if (pdevice->properties.deviceLUIDValid &&
511           memcmp(pdevice->properties.deviceLUID, &adapterLUID, sizeof(adapterLUID)) == 0) {
512          vk_outarray_append_typed(VkPhysicalDevice, &out, element) {
513             *element = vk_physical_device_to_handle(pdevice);
514          }
515       }
516    }
517 
518    return vk_outarray_status(&out);
519 }
520 #endif
521 
522 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_EnumeratePhysicalDeviceGroups(VkInstance _instance,uint32_t * pGroupCount,VkPhysicalDeviceGroupProperties * pGroupProperties)523 vk_common_EnumeratePhysicalDeviceGroups(VkInstance _instance, uint32_t *pGroupCount,
524                                         VkPhysicalDeviceGroupProperties *pGroupProperties)
525 {
526    VK_FROM_HANDLE(vk_instance, instance, _instance);
527    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, pGroupProperties,
528                           pGroupCount);
529 
530    VkResult result = enumerate_physical_devices(instance);
531    if (result != VK_SUCCESS)
532       return result;
533 
534    list_for_each_entry(struct vk_physical_device, pdevice,
535                        &instance->physical_devices.list, link) {
536       vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, p) {
537          p->physicalDeviceCount = 1;
538          memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
539          p->physicalDevices[0] = vk_physical_device_to_handle(pdevice);
540          p->subsetAllocation = false;
541       }
542    }
543 
544    return vk_outarray_status(&out);
545 }
546 
547 /* For Windows, PUBLIC is default-defined to __declspec(dllexport) to automatically export the
548  * public entrypoints from a DLL. However, this declspec needs to match between declaration and
549  * definition, and this attribute is not present on the prototypes specified in vk_icd.h. Instead,
550  * we'll use a .def file to manually export these entrypoints on Windows.
551  */
552 #ifdef _WIN32
553 #undef PUBLIC
554 #define PUBLIC
555 #endif
556 
557 /* With version 4+ of the loader interface the ICD should expose
558  * vk_icdGetPhysicalDeviceProcAddr()
559  */
560 PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetPhysicalDeviceProcAddr(VkInstance _instance,const char * pName)561 vk_icdGetPhysicalDeviceProcAddr(VkInstance  _instance,
562                                 const char *pName)
563 {
564    VK_FROM_HANDLE(vk_instance, instance, _instance);
565    return vk_instance_get_physical_device_proc_addr(instance, pName);
566 }
567 
568 static uint32_t vk_icd_version = 7;
569 
570 uint32_t
vk_get_negotiated_icd_version(void)571 vk_get_negotiated_icd_version(void)
572 {
573    return vk_icd_version;
574 }
575 
576 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t * pSupportedVersion)577 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
578 {
579    /* For the full details on loader interface versioning, see
580     * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
581     * What follows is a condensed summary, to help you navigate the large and
582     * confusing official doc.
583     *
584     *   - Loader interface v0 is incompatible with later versions. We don't
585     *     support it.
586     *
587     *   - In loader interface v1:
588     *       - The first ICD entrypoint called by the loader is
589     *         vk_icdGetInstanceProcAddr(). The ICD must statically expose this
590     *         entrypoint.
591     *       - The ICD must statically expose no other Vulkan symbol unless it is
592     *         linked with -Bsymbolic.
593     *       - Each dispatchable Vulkan handle created by the ICD must be
594     *         a pointer to a struct whose first member is VK_LOADER_DATA. The
595     *         ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
596     *       - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
597     *         vkDestroySurfaceKHR(). The ICD must be capable of working with
598     *         such loader-managed surfaces.
599     *
600     *    - Loader interface v2 differs from v1 in:
601     *       - The first ICD entrypoint called by the loader is
602     *         vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
603     *         statically expose this entrypoint.
604     *
605     *    - Loader interface v3 differs from v2 in:
606     *        - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
607     *          vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
608     *          because the loader no longer does so.
609     *
610     *    - Loader interface v4 differs from v3 in:
611     *        - The ICD must implement vk_icdGetPhysicalDeviceProcAddr().
612     *
613     *    - Loader interface v5 differs from v4 in:
614     *        - The ICD must support Vulkan API version 1.1 and must not return
615     *          VK_ERROR_INCOMPATIBLE_DRIVER from vkCreateInstance() unless a
616     *          Vulkan Loader with interface v4 or smaller is being used and the
617     *          application provides an API version that is greater than 1.0.
618     *
619     *    - Loader interface v6 differs from v5 in:
620     *        - Windows ICDs may export vk_icdEnumerateAdapterPhysicalDevices,
621     *          to tie a physical device to a WDDM adapter LUID. This allows the
622     *          loader to sort physical devices according to the same policy as other
623     *          graphics APIs.
624     *        - Note: A design flaw in the loader implementation of v6 means we do
625     *          not actually support returning this function to v6 loaders. See the
626     *          comments around the implementation above. It's still fine to report
627     *          version number 6 without this method being implemented, however.
628     *
629     *    - Loader interface v7 differs from v6 in:
630     *        - If implemented, the ICD must return the following functions via
631     *          vk_icdGetInstanceProcAddr:
632     *            - vk_icdNegotiateLoaderICDInterfaceVersion
633     *            - vk_icdGetPhysicalDeviceProcAddr
634     *            - vk_icdEnumerateAdapterPhysicalDevices
635     *          Exporting these functions from the ICD is optional. If
636     *          vk_icdNegotiateLoaderICDInterfaceVersion is not exported from the
637     *          module, or if VK_LUNARG_direct_driver_loading is being used, then
638     *          vk_icdGetInstanceProcAddr will be the first method called, to query
639     *          for vk_icdNegotiateLoaderICDInterfaceVersion.
640     */
641    vk_icd_version = MIN2(vk_icd_version, *pSupportedVersion);
642    *pSupportedVersion = vk_icd_version;
643    return VK_SUCCESS;
644 }
645