Debian Codegraph @

src/asahi/vulkan/hk_instance.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_instance.h"
8
9 #include "hk_entrypoints.h"
10 #include "hk_physical_device.h"
11
12 #include "vulkan/wsi/wsi_common.h"
13
14 #include "util/build_id.h"
15 #include "util/driconf.h"
16 #include "util/mesa-blake3.h"
17
18 VKAPI_ATTR VkResult VKAPI_CALL
19 hk_EnumerateInstanceVersion(uint32_t *pApiVersion)
20 {
21 uint32_t version_override = vk_get_version_override();
22 *pApiVersion = version_override ? version_override
23 : VK_MAKE_VERSION(1, 4, VK_HEADER_VERSION);
24
25 return VK_SUCCESS;
26 }
27
28 static const struct vk_instance_extension_table instance_extensions = {
29 #ifdef HK_USE_WSI_PLATFORM
30 .KHR_get_surface_capabilities2 = true,
31 .KHR_surface = true,
32 .KHR_surface_maintenance1 = true,
33 .KHR_surface_protected_capabilities = true,
34 .EXT_surface_maintenance1 = true,
35 .EXT_swapchain_colorspace = true,
36 #endif
37 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
38 .KHR_wayland_surface = true,
39 #endif
40 #ifdef VK_USE_PLATFORM_XCB_KHR
41 .KHR_xcb_surface = true,
42 #endif
43 #ifdef VK_USE_PLATFORM_XLIB_KHR
44 .KHR_xlib_surface = true,
45 #endif
46 #ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
47 .EXT_acquire_xlib_display = true,
48 #endif
49 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
50 .KHR_display = true,
51 .KHR_get_display_properties2 = true,
52 .EXT_direct_mode_display = true,
53 .EXT_display_surface_counter = true,
54 .EXT_acquire_drm_display = true,
55 #endif
56 #ifndef VK_USE_PLATFORM_WIN32_KHR
57 .EXT_headless_surface = true,
58 #endif
59 .KHR_device_group_creation = true,
60 .KHR_external_fence_capabilities = true,
61 .KHR_external_memory_capabilities = true,
62 .KHR_external_semaphore_capabilities = true,
63 .KHR_get_physical_device_properties2 = true,
64 .EXT_debug_report = true,
65 .EXT_debug_utils = true,
66 };
67
68 VKAPI_ATTR VkResult VKAPI_CALL
69 hk_EnumerateInstanceExtensionProperties(const char *pLayerName,
70 uint32_t *pPropertyCount,
71 VkExtensionProperties *pProperties)
72 {
73 if (pLayerName)
74 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
75
76 return vk_enumerate_instance_extension_properties(
77 &instance_extensions, pPropertyCount, pProperties);
78 }
79
80 /* clang-format off */
81 static const driOptionDescription hk_dri_options[] = {
82 DRI_CONF_SECTION_PERFORMANCE
83 DRI_CONF_ADAPTIVE_SYNC(true)
84 DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0)
85 DRI_CONF_VK_X11_STRICT_IMAGE_COUNT(false)
86 DRI_CONF_VK_X11_ENSURE_MIN_IMAGE_COUNT(false)
87 DRI_CONF_VK_XWAYLAND_WAIT_READY(false)
88 DRI_CONF_SECTION_END
89
90 DRI_CONF_SECTION_DEBUG
91 DRI_CONF_FORCE_VK_VENDOR()
92 DRI_CONF_VK_WSI_FORCE_SWAPCHAIN_TO_CURRENT_EXTENT(false)
93 DRI_CONF_VK_X11_IGNORE_SUBOPTIMAL(false)
94 DRI_CONF_SECTION_END
95
96 DRI_CONF_SECTION_MISCELLANEOUS
97 DRI_CONF_HK_DISABLE_BORDER_EMULATION(false)
98 DRI_CONF_HK_FAKE_MINMAX(false)
99 DRI_CONF_HK_IMAGE_VIEW_MIN_LOD(false)
100 DRI_CONF_HK_ENABLE_VERTEX_PIPELINE_STORES_ATOMICS(false)
101 DRI_CONF_SECTION_END
102 };
103 /* clang-format on */
104
105 static void
106 hk_init_dri_options(struct hk_instance *instance)
107 {
108 driParseOptionInfo(&instance->available_dri_options, hk_dri_options,
109 ARRAY_SIZE(hk_dri_options));
110 driParseConfigFiles(
111 &instance->dri_options, &instance->available_dri_options, 0, "hk", NULL,
112 NULL, instance->vk.app_info.app_name, instance->vk.app_info.app_version,
113 instance->vk.app_info.engine_name, instance->vk.app_info.engine_version);
114
115 instance->force_vk_vendor =
116 driQueryOptioni(&instance->dri_options, "force_vk_vendor");
117
118 instance->no_border =
119 driQueryOptionb(&instance->dri_options, "hk_disable_border_emulation");
120
121 instance->fake_minmax =
122 driQueryOptionb(&instance->dri_options, "hk_fake_minmax");
123
124 instance->image_view_min_lod =
125 driQueryOptionb(&instance->dri_options, "hk_image_view_min_lod");
126
127 instance->vertex_stores = driQueryOptionb(
128 &instance->dri_options, "hk_enable_vertex_pipeline_stores_atomics");
129 }
130
131 VKAPI_ATTR VkResult VKAPI_CALL
132 hk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
133 const VkAllocationCallbacks *pAllocator,
134 VkInstance *pInstance)
135 {
136 struct hk_instance *instance;
137 VkResult result;
138
139 if (pAllocator == NULL)
140 pAllocator = vk_default_allocator();
141
142 instance = vk_alloc(pAllocator, sizeof(*instance), 8,
143 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
144 if (!instance)
145 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
146
147 struct vk_instance_dispatch_table dispatch_table;
148 vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
149 &hk_instance_entrypoints, true);
150 vk_instance_dispatch_table_from_entrypoints(
151 &dispatch_table, &wsi_instance_entrypoints, false);
152
153 result = vk_instance_init(&instance->vk, &instance_extensions,
154 &dispatch_table, pCreateInfo, pAllocator);
155 if (result != VK_SUCCESS)
156 goto fail_alloc;
157
158 hk_init_dri_options(instance);
159
160 instance->vk.physical_devices.try_create_for_drm =
161 hk_create_drm_physical_device;
162 instance->vk.physical_devices.destroy = hk_physical_device_destroy;
163
164 const struct build_id_note *note =
165 build_id_find_nhdr_for_addr(hk_CreateInstance);
166 if (!note) {
167 result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
168 "Failed to find build-id");
169 goto fail_init;
170 }
171
172 unsigned build_id_len = build_id_length(note);
173 if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) {
174 result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
175 "build-id too short. It needs to be a SHA");
176 goto fail_init;
177 }
178
179 static_assert(sizeof(instance->driver_build_sha) == BLAKE3_KEY_LEN);
180 copy_build_id_to_sha1(instance->driver_build_sha, note);
181
182 *pInstance = hk_instance_to_handle(instance);
183 return VK_SUCCESS;
184
185 fail_init:
186 vk_instance_finish(&instance->vk);
187 fail_alloc:
188 vk_free(pAllocator, instance);
189
190 return result;
191 }
192
193 VKAPI_ATTR void VKAPI_CALL
194 hk_DestroyInstance(VkInstance _instance,
195 const VkAllocationCallbacks *pAllocator)
196 {
197 VK_FROM_HANDLE(hk_instance, instance, _instance);
198
199 if (!instance)
200 return;
201
202 driDestroyOptionCache(&instance->dri_options);
203 driDestroyOptionInfo(&instance->available_dri_options);
204
205 vk_instance_finish(&instance->vk);
206 vk_free(&instance->vk.alloc, instance);
207 }
208
209 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
210 hk_GetInstanceProcAddr(VkInstance _instance, const char *pName)
211 {
212 VK_FROM_HANDLE(hk_instance, instance, _instance);
213 return vk_instance_get_proc_addr(&instance->vk, &hk_instance_entrypoints,
214 pName);
215 }
216
217 PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
218 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
219 {
220 return hk_GetInstanceProcAddr(instance, pName);
221 }