2
* Copyright © 2021 Intel Corporation
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:
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
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
24
#include "vk_instance.h"
27
#include "vk_common_entrypoints.h"
28
#include "vk_dispatch_trampolines.h"
31
#include "vk_debug_utils.h"
33
#include "compiler/glsl_types.h"
35
#define VERSION_IS_1_0(version) \
36
(VK_API_VERSION_MAJOR(version) == 1 && VK_API_VERSION_MINOR(version) == 0)
39
vk_instance_init(struct vk_instance *instance,
40
const struct vk_instance_extension_table *supported_extensions,
41
const struct vk_instance_dispatch_table *dispatch_table,
42
const VkInstanceCreateInfo *pCreateInfo,
43
const VkAllocationCallbacks *alloc)
45
memset(instance, 0, sizeof(*instance));
46
vk_object_base_init(NULL, &instance->base, VK_OBJECT_TYPE_INSTANCE);
47
instance->alloc = *alloc;
49
/* VK_EXT_debug_utils */
50
/* These messengers will only be used during vkCreateInstance or
51
* vkDestroyInstance calls. We do this first so that it's safe to use
52
* vk_errorf and friends.
54
list_inithead(&instance->debug_utils.instance_callbacks);
55
vk_foreach_struct_const(ext, pCreateInfo->pNext) {
57
VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
58
const VkDebugUtilsMessengerCreateInfoEXT *debugMessengerCreateInfo =
59
(const VkDebugUtilsMessengerCreateInfoEXT *)ext;
60
struct vk_debug_utils_messenger *messenger =
61
vk_alloc2(alloc, alloc, sizeof(struct vk_debug_utils_messenger), 8,
62
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
65
return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
67
vk_object_base_init(NULL, &messenger->base,
68
VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT);
70
messenger->alloc = *alloc;
71
messenger->severity = debugMessengerCreateInfo->messageSeverity;
72
messenger->type = debugMessengerCreateInfo->messageType;
73
messenger->callback = debugMessengerCreateInfo->pfnUserCallback;
74
messenger->data = debugMessengerCreateInfo->pUserData;
76
list_addtail(&messenger->link,
77
&instance->debug_utils.instance_callbacks);
81
uint32_t instance_version = VK_API_VERSION_1_0;
82
if (dispatch_table->EnumerateInstanceVersion)
83
dispatch_table->EnumerateInstanceVersion(&instance_version);
85
instance->app_info = (struct vk_app_info) { .api_version = 0 };
86
if (pCreateInfo->pApplicationInfo) {
87
const VkApplicationInfo *app = pCreateInfo->pApplicationInfo;
89
instance->app_info.app_name =
90
vk_strdup(&instance->alloc, app->pApplicationName,
91
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
92
instance->app_info.app_version = app->applicationVersion;
94
instance->app_info.engine_name =
95
vk_strdup(&instance->alloc, app->pEngineName,
96
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
97
instance->app_info.engine_version = app->engineVersion;
99
instance->app_info.api_version = app->apiVersion;
102
/* From the Vulkan 1.2.199 spec:
106
* Providing a NULL VkInstanceCreateInfo::pApplicationInfo or providing
107
* an apiVersion of 0 is equivalent to providing an apiVersion of
108
* VK_MAKE_API_VERSION(0,1,0,0)."
110
if (instance->app_info.api_version == 0)
111
instance->app_info.api_version = VK_API_VERSION_1_0;
113
/* From the Vulkan 1.2.199 spec:
115
* VUID-VkApplicationInfo-apiVersion-04010
117
* "If apiVersion is not 0, then it must be greater than or equal to
118
* VK_API_VERSION_1_0"
120
assert(instance->app_info.api_version >= VK_API_VERSION_1_0);
122
/* From the Vulkan 1.2.199 spec:
124
* "Vulkan 1.0 implementations were required to return
125
* VK_ERROR_INCOMPATIBLE_DRIVER if apiVersion was larger than 1.0.
126
* Implementations that support Vulkan 1.1 or later must not return
127
* VK_ERROR_INCOMPATIBLE_DRIVER for any value of apiVersion."
129
if (VERSION_IS_1_0(instance_version) &&
130
!VERSION_IS_1_0(instance->app_info.api_version))
131
return VK_ERROR_INCOMPATIBLE_DRIVER;
133
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
135
for (idx = 0; idx < VK_INSTANCE_EXTENSION_COUNT; idx++) {
136
if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
137
vk_instance_extensions[idx].extensionName) == 0)
141
if (idx >= VK_INSTANCE_EXTENSION_COUNT)
142
return vk_errorf(instance, VK_ERROR_EXTENSION_NOT_PRESENT,
144
pCreateInfo->ppEnabledExtensionNames[i]);
146
if (!supported_extensions->extensions[idx])
147
return vk_errorf(instance, VK_ERROR_EXTENSION_NOT_PRESENT,
149
pCreateInfo->ppEnabledExtensionNames[i]);
152
if (!vk_android_allowed_instance_extensions.extensions[idx])
153
return vk_errorf(instance, VK_ERROR_EXTENSION_NOT_PRESENT,
155
pCreateInfo->ppEnabledExtensionNames[i]);
158
instance->enabled_extensions.extensions[idx] = true;
161
instance->dispatch_table = *dispatch_table;
163
/* Add common entrypoints without overwriting driver-provided ones. */
164
vk_instance_dispatch_table_from_entrypoints(
165
&instance->dispatch_table, &vk_common_instance_entrypoints, false);
167
if (mtx_init(&instance->debug_report.callbacks_mutex, mtx_plain) != 0)
168
return vk_error(instance, VK_ERROR_INITIALIZATION_FAILED);
170
list_inithead(&instance->debug_report.callbacks);
172
if (mtx_init(&instance->debug_utils.callbacks_mutex, mtx_plain) != 0) {
173
mtx_destroy(&instance->debug_report.callbacks_mutex);
174
return vk_error(instance, VK_ERROR_INITIALIZATION_FAILED);
177
list_inithead(&instance->debug_utils.callbacks);
179
glsl_type_singleton_init_or_ref();
185
vk_instance_finish(struct vk_instance *instance)
187
glsl_type_singleton_decref();
188
if (unlikely(!list_is_empty(&instance->debug_utils.callbacks))) {
189
list_for_each_entry_safe(struct vk_debug_utils_messenger, messenger,
190
&instance->debug_utils.callbacks, link) {
191
list_del(&messenger->link);
192
vk_object_base_finish(&messenger->base);
193
vk_free2(&instance->alloc, &messenger->alloc, messenger);
196
if (unlikely(!list_is_empty(&instance->debug_utils.instance_callbacks))) {
197
list_for_each_entry_safe(struct vk_debug_utils_messenger, messenger,
198
&instance->debug_utils.instance_callbacks,
200
list_del(&messenger->link);
201
vk_object_base_finish(&messenger->base);
202
vk_free2(&instance->alloc, &messenger->alloc, messenger);
205
mtx_destroy(&instance->debug_report.callbacks_mutex);
206
mtx_destroy(&instance->debug_utils.callbacks_mutex);
207
vk_free(&instance->alloc, (char *)instance->app_info.app_name);
208
vk_free(&instance->alloc, (char *)instance->app_info.engine_name);
209
vk_object_base_finish(&instance->base);
213
vk_enumerate_instance_extension_properties(
214
const struct vk_instance_extension_table *supported_extensions,
215
uint32_t *pPropertyCount,
216
VkExtensionProperties *pProperties)
218
VK_OUTARRAY_MAKE_TYPED(VkExtensionProperties, out, pProperties, pPropertyCount);
220
for (int i = 0; i < VK_INSTANCE_EXTENSION_COUNT; i++) {
221
if (!supported_extensions->extensions[i])
225
if (!vk_android_allowed_instance_extensions.extensions[i])
229
vk_outarray_append_typed(VkExtensionProperties, &out, prop) {
230
*prop = vk_instance_extensions[i];
234
return vk_outarray_status(&out);
238
vk_instance_get_proc_addr(const struct vk_instance *instance,
239
const struct vk_instance_entrypoint_table *entrypoints,
242
PFN_vkVoidFunction func;
244
/* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
245
* when we have to return valid function pointers, NULL, or it's left
246
* undefined. See the table for exact details.
251
#define LOOKUP_VK_ENTRYPOINT(entrypoint) \
252
if (strcmp(name, "vk" #entrypoint) == 0) \
253
return (PFN_vkVoidFunction)entrypoints->entrypoint
255
LOOKUP_VK_ENTRYPOINT(EnumerateInstanceExtensionProperties);
256
LOOKUP_VK_ENTRYPOINT(EnumerateInstanceLayerProperties);
257
LOOKUP_VK_ENTRYPOINT(EnumerateInstanceVersion);
258
LOOKUP_VK_ENTRYPOINT(CreateInstance);
260
/* GetInstanceProcAddr() can also be called with a NULL instance.
261
* See https://gitlab.khronos.org/vulkan/vulkan/issues/2057
263
LOOKUP_VK_ENTRYPOINT(GetInstanceProcAddr);
265
#undef LOOKUP_VK_ENTRYPOINT
267
if (instance == NULL)
270
func = vk_instance_dispatch_table_get_if_supported(&instance->dispatch_table,
272
instance->app_info.api_version,
273
&instance->enabled_extensions);
277
func = vk_physical_device_dispatch_table_get_if_supported(&vk_physical_device_trampolines,
279
instance->app_info.api_version,
280
&instance->enabled_extensions);
284
func = vk_device_dispatch_table_get_if_supported(&vk_device_trampolines,
286
instance->app_info.api_version,
287
&instance->enabled_extensions,
296
vk_instance_get_proc_addr_unchecked(const struct vk_instance *instance,
299
PFN_vkVoidFunction func;
301
if (instance == NULL || name == NULL)
304
func = vk_instance_dispatch_table_get(&instance->dispatch_table, name);
308
func = vk_physical_device_dispatch_table_get(
309
&vk_physical_device_trampolines, name);
313
func = vk_device_dispatch_table_get(&vk_device_trampolines, name);
321
vk_instance_get_physical_device_proc_addr(const struct vk_instance *instance,
324
if (instance == NULL || name == NULL)
327
return vk_physical_device_dispatch_table_get_if_supported(&vk_physical_device_trampolines,
329
instance->app_info.api_version,
330
&instance->enabled_extensions);