• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_per_submit = debug_get_bool_option("MESA_VK_TRACE_PER_SUBMIT", false);
204    if (!instance->trace_per_submit) {
205       instance->trace_frame = (uint32_t)debug_get_num_option("MESA_VK_TRACE_FRAME", 0xFFFFFFFF);
206       instance->trace_trigger_file = secure_getenv("MESA_VK_TRACE_TRIGGER");
207    }
208 
209 #if !VK_LITE_RUNTIME_INSTANCE
210    glsl_type_singleton_init_or_ref();
211 #endif
212 
213    return VK_SUCCESS;
214 }
215 
216 static void
destroy_physical_devices(struct vk_instance * instance)217 destroy_physical_devices(struct vk_instance *instance)
218 {
219    list_for_each_entry_safe(struct vk_physical_device, pdevice,
220                             &instance->physical_devices.list, link) {
221       list_del(&pdevice->link);
222       instance->physical_devices.destroy(pdevice);
223    }
224 }
225 
226 void
vk_instance_finish(struct vk_instance * instance)227 vk_instance_finish(struct vk_instance *instance)
228 {
229    destroy_physical_devices(instance);
230 
231 #if !VK_LITE_RUNTIME_INSTANCE
232    glsl_type_singleton_decref();
233 #endif
234 
235    if (unlikely(!list_is_empty(&instance->debug_utils.callbacks))) {
236       list_for_each_entry_safe(struct vk_debug_utils_messenger, messenger,
237                                &instance->debug_utils.callbacks, link) {
238          list_del(&messenger->link);
239          vk_object_base_finish(&messenger->base);
240          vk_free2(&instance->alloc, &messenger->alloc, messenger);
241       }
242    }
243    if (unlikely(!list_is_empty(&instance->debug_utils.instance_callbacks))) {
244       list_for_each_entry_safe(struct vk_debug_utils_messenger, messenger,
245                                &instance->debug_utils.instance_callbacks,
246                                link) {
247          list_del(&messenger->link);
248          vk_object_base_finish(&messenger->base);
249          vk_free2(&instance->alloc, &messenger->alloc, messenger);
250       }
251    }
252    mtx_destroy(&instance->debug_report.callbacks_mutex);
253    mtx_destroy(&instance->debug_utils.callbacks_mutex);
254    mtx_destroy(&instance->physical_devices.mutex);
255    vk_free(&instance->alloc, (char *)instance->app_info.app_name);
256    vk_free(&instance->alloc, (char *)instance->app_info.engine_name);
257    vk_object_base_finish(&instance->base);
258 }
259 
260 VkResult
vk_enumerate_instance_extension_properties(const struct vk_instance_extension_table * supported_extensions,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)261 vk_enumerate_instance_extension_properties(
262     const struct vk_instance_extension_table *supported_extensions,
263     uint32_t *pPropertyCount,
264     VkExtensionProperties *pProperties)
265 {
266    VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
267 
268    for (int i = 0; i < VK_INSTANCE_EXTENSION_COUNT; i++) {
269       if (!supported_extensions->extensions[i])
270          continue;
271 
272 #ifdef ANDROID_STRICT
273       if (!vk_android_allowed_instance_extensions.extensions[i])
274          continue;
275 #endif
276 
277       vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
278          *prop = vk_instance_extensions[i];
279       }
280    }
281 
282    return vk_outarray_status(&out);
283 }
284 
285 PFN_vkVoidFunction
vk_instance_get_proc_addr(const struct vk_instance * instance,const struct vk_instance_entrypoint_table * entrypoints,const char * name)286 vk_instance_get_proc_addr(const struct vk_instance *instance,
287                           const struct vk_instance_entrypoint_table *entrypoints,
288                           const char *name)
289 {
290    PFN_vkVoidFunction func;
291 
292    /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
293     * when we have to return valid function pointers, NULL, or it's left
294     * undefined.  See the table for exact details.
295     */
296    if (name == NULL)
297       return NULL;
298 
299 #define LOOKUP_VK_ENTRYPOINT(entrypoint) \
300    if (strcmp(name, "vk" #entrypoint) == 0) \
301       return (PFN_vkVoidFunction)entrypoints->entrypoint
302 
303    LOOKUP_VK_ENTRYPOINT(EnumerateInstanceExtensionProperties);
304    LOOKUP_VK_ENTRYPOINT(EnumerateInstanceLayerProperties);
305    LOOKUP_VK_ENTRYPOINT(EnumerateInstanceVersion);
306    LOOKUP_VK_ENTRYPOINT(CreateInstance);
307 
308    /* GetInstanceProcAddr() can also be called with a NULL instance.
309     * See https://gitlab.khronos.org/vulkan/vulkan/issues/2057
310     */
311    LOOKUP_VK_ENTRYPOINT(GetInstanceProcAddr);
312 
313 #undef LOOKUP_VK_ENTRYPOINT
314 
315    /* Beginning with ICD interface v7, the following functions can also be
316     * retrieved via vk_icdGetInstanceProcAddr.
317     */
318 
319    if (strcmp(name, "vk_icdNegotiateLoaderICDInterfaceVersion") == 0)
320       return (PFN_vkVoidFunction)vk_icdNegotiateLoaderICDInterfaceVersion;
321    if (strcmp(name, "vk_icdGetPhysicalDeviceProcAddr") == 0)
322       return (PFN_vkVoidFunction)vk_icdGetPhysicalDeviceProcAddr;
323 #ifdef _WIN32
324    if (strcmp(name, "vk_icdEnumerateAdapterPhysicalDevices") == 0)
325       return (PFN_vkVoidFunction)vk_icdEnumerateAdapterPhysicalDevices;
326 #endif
327 
328    if (instance == NULL)
329       return NULL;
330 
331    func = vk_instance_dispatch_table_get_if_supported(&instance->dispatch_table,
332                                                       name,
333                                                       instance->app_info.api_version,
334                                                       &instance->enabled_extensions);
335    if (func != NULL)
336       return func;
337 
338    func = vk_physical_device_dispatch_table_get_if_supported(&vk_physical_device_trampolines,
339                                                              name,
340                                                              instance->app_info.api_version,
341                                                              &instance->enabled_extensions);
342    if (func != NULL)
343       return func;
344 
345    func = vk_device_dispatch_table_get_if_supported(&vk_device_trampolines,
346                                                     name,
347                                                     instance->app_info.api_version,
348                                                     &instance->enabled_extensions,
349                                                     NULL);
350    if (func != NULL)
351       return func;
352 
353    return NULL;
354 }
355 
356 PFN_vkVoidFunction
vk_instance_get_proc_addr_unchecked(const struct vk_instance * instance,const char * name)357 vk_instance_get_proc_addr_unchecked(const struct vk_instance *instance,
358                                     const char *name)
359 {
360    PFN_vkVoidFunction func;
361 
362    if (instance == NULL || name == NULL)
363       return NULL;
364 
365    func = vk_instance_dispatch_table_get(&instance->dispatch_table, name);
366    if (func != NULL)
367       return func;
368 
369    func = vk_physical_device_dispatch_table_get(
370       &vk_physical_device_trampolines, name);
371    if (func != NULL)
372       return func;
373 
374    func = vk_device_dispatch_table_get(&vk_device_trampolines, name);
375    if (func != NULL)
376       return func;
377 
378    return NULL;
379 }
380 
381 PFN_vkVoidFunction
vk_instance_get_physical_device_proc_addr(const struct vk_instance * instance,const char * name)382 vk_instance_get_physical_device_proc_addr(const struct vk_instance *instance,
383                                           const char *name)
384 {
385    if (instance == NULL || name == NULL)
386       return NULL;
387 
388    return vk_physical_device_dispatch_table_get_if_supported(&vk_physical_device_trampolines,
389                                                              name,
390                                                              instance->app_info.api_version,
391                                                              &instance->enabled_extensions);
392 }
393 
394 void
vk_instance_add_driver_trace_modes(struct vk_instance * instance,const struct debug_control * modes)395 vk_instance_add_driver_trace_modes(struct vk_instance *instance,
396                                    const struct debug_control *modes)
397 {
398    instance->trace_mode |= parse_debug_string(getenv("MESA_VK_TRACE"), modes);
399 }
400 
401 static VkResult
enumerate_drm_physical_devices_locked(struct vk_instance * instance)402 enumerate_drm_physical_devices_locked(struct vk_instance *instance)
403 {
404    /* libdrm returns a maximum of 256 devices (see MAX_DRM_NODES in libdrm) */
405    drmDevicePtr devices[256];
406    int max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
407 
408    if (max_devices < 1)
409       return VK_SUCCESS;
410 
411    VkResult result;
412    for (uint32_t i = 0; i < (uint32_t)max_devices; i++) {
413       struct vk_physical_device *pdevice;
414       result = instance->physical_devices.try_create_for_drm(instance, devices[i], &pdevice);
415 
416       /* Incompatible DRM device, skip. */
417       if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
418          result = VK_SUCCESS;
419          continue;
420       }
421 
422       /* Error creating the physical device, report the error. */
423       if (result != VK_SUCCESS)
424          break;
425 
426       list_addtail(&pdevice->link, &instance->physical_devices.list);
427    }
428 
429    drmFreeDevices(devices, max_devices);
430    return result;
431 }
432 
433 static VkResult
enumerate_physical_devices_locked(struct vk_instance * instance)434 enumerate_physical_devices_locked(struct vk_instance *instance)
435 {
436    if (instance->physical_devices.enumerate) {
437       VkResult result = instance->physical_devices.enumerate(instance);
438       if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
439          return result;
440    }
441 
442    VkResult result = VK_SUCCESS;
443 
444    if (instance->physical_devices.try_create_for_drm) {
445       result = enumerate_drm_physical_devices_locked(instance);
446       if (result != VK_SUCCESS) {
447          destroy_physical_devices(instance);
448          return result;
449       }
450    }
451 
452    return result;
453 }
454 
455 static VkResult
enumerate_physical_devices(struct vk_instance * instance)456 enumerate_physical_devices(struct vk_instance *instance)
457 {
458    VkResult result = VK_SUCCESS;
459 
460    mtx_lock(&instance->physical_devices.mutex);
461    if (!instance->physical_devices.enumerated) {
462       result = enumerate_physical_devices_locked(instance);
463       if (result == VK_SUCCESS)
464          instance->physical_devices.enumerated = true;
465    }
466    mtx_unlock(&instance->physical_devices.mutex);
467 
468    return result;
469 }
470 
471 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_EnumeratePhysicalDevices(VkInstance _instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)472 vk_common_EnumeratePhysicalDevices(VkInstance _instance, uint32_t *pPhysicalDeviceCount,
473                                    VkPhysicalDevice *pPhysicalDevices)
474 {
475    VK_FROM_HANDLE(vk_instance, instance, _instance);
476    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
477 
478    VkResult result = enumerate_physical_devices(instance);
479    if (result != VK_SUCCESS)
480       return result;
481 
482    list_for_each_entry(struct vk_physical_device, pdevice,
483                        &instance->physical_devices.list, link) {
484       vk_outarray_append_typed(VkPhysicalDevice, &out, element) {
485          *element = vk_physical_device_to_handle(pdevice);
486       }
487    }
488 
489    return vk_outarray_status(&out);
490 }
491 
492 #ifdef _WIN32
493 /* Note: This entrypoint is not exported from ICD DLLs, and is only exposed via
494  * vk_icdGetInstanceProcAddr for loaders with interface v7. This is to avoid
495  * a design flaw in the original loader implementation, which prevented enumeration
496  * of physical devices that didn't have a LUID. This flaw was fixed prior to the
497  * implementation of v7, so v7 loaders are unaffected, and it's safe to support this.
498  */
499 VKAPI_ATTR VkResult VKAPI_CALL
vk_icdEnumerateAdapterPhysicalDevices(VkInstance _instance,LUID adapterLUID,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)500 vk_icdEnumerateAdapterPhysicalDevices(VkInstance _instance, LUID adapterLUID,
501                                       uint32_t *pPhysicalDeviceCount,
502                                       VkPhysicalDevice *pPhysicalDevices)
503 {
504    VK_FROM_HANDLE(vk_instance, instance, _instance);
505    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
506 
507    VkResult result = enumerate_physical_devices(instance);
508    if (result != VK_SUCCESS)
509       return result;
510 
511    list_for_each_entry(struct vk_physical_device, pdevice,
512                        &instance->physical_devices.list, link) {
513       if (pdevice->properties.deviceLUIDValid &&
514           memcmp(pdevice->properties.deviceLUID, &adapterLUID, sizeof(adapterLUID)) == 0) {
515          vk_outarray_append_typed(VkPhysicalDevice, &out, element) {
516             *element = vk_physical_device_to_handle(pdevice);
517          }
518       }
519    }
520 
521    return vk_outarray_status(&out);
522 }
523 #endif
524 
525 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_EnumeratePhysicalDeviceGroups(VkInstance _instance,uint32_t * pGroupCount,VkPhysicalDeviceGroupProperties * pGroupProperties)526 vk_common_EnumeratePhysicalDeviceGroups(VkInstance _instance, uint32_t *pGroupCount,
527                                         VkPhysicalDeviceGroupProperties *pGroupProperties)
528 {
529    VK_FROM_HANDLE(vk_instance, instance, _instance);
530    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, pGroupProperties,
531                           pGroupCount);
532 
533    VkResult result = enumerate_physical_devices(instance);
534    if (result != VK_SUCCESS)
535       return result;
536 
537    list_for_each_entry(struct vk_physical_device, pdevice,
538                        &instance->physical_devices.list, link) {
539       vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, p) {
540          p->physicalDeviceCount = 1;
541          memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
542          p->physicalDevices[0] = vk_physical_device_to_handle(pdevice);
543          p->subsetAllocation = false;
544       }
545    }
546 
547    return vk_outarray_status(&out);
548 }
549 
550 /* For Windows, PUBLIC is default-defined to __declspec(dllexport) to automatically export the
551  * public entrypoints from a DLL. However, this declspec needs to match between declaration and
552  * definition, and this attribute is not present on the prototypes specified in vk_icd.h. Instead,
553  * we'll use a .def file to manually export these entrypoints on Windows.
554  */
555 #ifdef _WIN32
556 #undef PUBLIC
557 #define PUBLIC
558 #endif
559 
560 /* With version 4+ of the loader interface the ICD should expose
561  * vk_icdGetPhysicalDeviceProcAddr()
562  */
563 PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetPhysicalDeviceProcAddr(VkInstance _instance,const char * pName)564 vk_icdGetPhysicalDeviceProcAddr(VkInstance  _instance,
565                                 const char *pName)
566 {
567    VK_FROM_HANDLE(vk_instance, instance, _instance);
568    return vk_instance_get_physical_device_proc_addr(instance, pName);
569 }
570 
571 static uint32_t vk_icd_version = 7;
572 
573 uint32_t
vk_get_negotiated_icd_version(void)574 vk_get_negotiated_icd_version(void)
575 {
576    return vk_icd_version;
577 }
578 
579 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t * pSupportedVersion)580 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
581 {
582    /* For the full details on loader interface versioning, see
583     * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
584     * What follows is a condensed summary, to help you navigate the large and
585     * confusing official doc.
586     *
587     *   - Loader interface v0 is incompatible with later versions. We don't
588     *     support it.
589     *
590     *   - In loader interface v1:
591     *       - The first ICD entrypoint called by the loader is
592     *         vk_icdGetInstanceProcAddr(). The ICD must statically expose this
593     *         entrypoint.
594     *       - The ICD must statically expose no other Vulkan symbol unless it is
595     *         linked with -Bsymbolic.
596     *       - Each dispatchable Vulkan handle created by the ICD must be
597     *         a pointer to a struct whose first member is VK_LOADER_DATA. The
598     *         ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
599     *       - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
600     *         vkDestroySurfaceKHR(). The ICD must be capable of working with
601     *         such loader-managed surfaces.
602     *
603     *    - Loader interface v2 differs from v1 in:
604     *       - The first ICD entrypoint called by the loader is
605     *         vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
606     *         statically expose this entrypoint.
607     *
608     *    - Loader interface v3 differs from v2 in:
609     *        - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
610     *          vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
611     *          because the loader no longer does so.
612     *
613     *    - Loader interface v4 differs from v3 in:
614     *        - The ICD must implement vk_icdGetPhysicalDeviceProcAddr().
615     *
616     *    - Loader interface v5 differs from v4 in:
617     *        - The ICD must support Vulkan API version 1.1 and must not return
618     *          VK_ERROR_INCOMPATIBLE_DRIVER from vkCreateInstance() unless a
619     *          Vulkan Loader with interface v4 or smaller is being used and the
620     *          application provides an API version that is greater than 1.0.
621     *
622     *    - Loader interface v6 differs from v5 in:
623     *        - Windows ICDs may export vk_icdEnumerateAdapterPhysicalDevices,
624     *          to tie a physical device to a WDDM adapter LUID. This allows the
625     *          loader to sort physical devices according to the same policy as other
626     *          graphics APIs.
627     *        - Note: A design flaw in the loader implementation of v6 means we do
628     *          not actually support returning this function to v6 loaders. See the
629     *          comments around the implementation above. It's still fine to report
630     *          version number 6 without this method being implemented, however.
631     *
632     *    - Loader interface v7 differs from v6 in:
633     *        - If implemented, the ICD must return the following functions via
634     *          vk_icdGetInstanceProcAddr:
635     *            - vk_icdNegotiateLoaderICDInterfaceVersion
636     *            - vk_icdGetPhysicalDeviceProcAddr
637     *            - vk_icdEnumerateAdapterPhysicalDevices
638     *          Exporting these functions from the ICD is optional. If
639     *          vk_icdNegotiateLoaderICDInterfaceVersion is not exported from the
640     *          module, or if VK_LUNARG_direct_driver_loading is being used, then
641     *          vk_icdGetInstanceProcAddr will be the first method called, to query
642     *          for vk_icdNegotiateLoaderICDInterfaceVersion.
643     */
644    vk_icd_version = MIN2(vk_icd_version, *pSupportedVersion);
645    *pSupportedVersion = vk_icd_version;
646    return VK_SUCCESS;
647 }
648