Debian Codegraph @

src/kosmickrisp/vulkan/kk_instance.c

raw symbols SCIP
1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * Copyright 2025 LunarG, Inc.
4 * Copyright 2025 Google LLC
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "kk_instance.h"
9
10 #include "kk_debug.h"
11 #include "kk_entrypoints.h"
12 #include "kk_physical_device.h"
13
14 #include "kk_dispatch_trampolines.h"
15
16 #include "vulkan/wsi/wsi_common.h"
17
18 #include "util/build_id.h"
19 #include "util/driconf.h"
20 #include "util/mesa-blake3.h"
21 #include "util/u_debug.h"
22
23 VKAPI_ATTR VkResult VKAPI_CALL
24 kk_EnumerateInstanceVersion(uint32_t *pApiVersion)
25 {
26 uint32_t version_override = vk_get_version_override();
27 *pApiVersion = version_override ? version_override
28 : VK_MAKE_VERSION(1, 3, VK_HEADER_VERSION);
29
30 return VK_SUCCESS;
31 }
32
33 static const struct vk_instance_extension_table instance_extensions = {
34 #ifdef KK_USE_WSI_PLATFORM
35 .KHR_get_surface_capabilities2 = true,
36 .KHR_surface = true,
37 .KHR_surface_maintenance1 = true,
38 .KHR_surface_protected_capabilities = true,
39 .EXT_surface_maintenance1 = true,
40 .EXT_swapchain_colorspace = true,
41 #endif
42 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
43 .KHR_wayland_surface = true,
44 #endif
45 #ifdef VK_USE_PLATFORM_XCB_KHR
46 .KHR_xcb_surface = true,
47 #endif
48 #ifdef VK_USE_PLATFORM_XLIB_KHR
49 .KHR_xlib_surface = true,
50 #endif
51 #ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
52 .EXT_acquire_xlib_display = true,
53 #endif
54 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
55 .KHR_display = true,
56 .KHR_get_display_properties2 = true,
57 .EXT_direct_mode_display = true,
58 .EXT_display_surface_counter = true,
59 .EXT_acquire_drm_display = true,
60 #endif
61 #ifdef VK_USE_PLATFORM_METAL_EXT
62 .EXT_metal_surface = true,
63 #endif
64 #ifndef VK_USE_PLATFORM_METAL_EXT
65 .EXT_headless_surface = true,
66 #endif
67 .KHR_device_group_creation = true,
68 .KHR_external_fence_capabilities = true,
69 .KHR_external_memory_capabilities = true,
70 .KHR_external_semaphore_capabilities = true,
71 .KHR_get_physical_device_properties2 = true,
72 .EXT_debug_report = true,
73 .EXT_debug_utils = true,
74 };
75
76 VKAPI_ATTR VkResult VKAPI_CALL
77 kk_EnumerateInstanceExtensionProperties(const char *pLayerName,
78 uint32_t *pPropertyCount,
79 VkExtensionProperties *pProperties)
80 {
81 if (pLayerName)
82 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
83
84 return vk_enumerate_instance_extension_properties(
85 &instance_extensions, pPropertyCount, pProperties);
86 }
87
88 VKAPI_ATTR VkResult VKAPI_CALL
89 kk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
90 const VkAllocationCallbacks *pAllocator,
91 VkInstance *pInstance)
92 {
93 struct kk_instance *instance;
94 VkResult result;
95
96 if (pAllocator == NULL)
97 pAllocator = vk_default_allocator();
98
99 instance = vk_alloc(pAllocator, sizeof(*instance), 8,
100 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
101 if (!instance)
102 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
103
104 struct vk_instance_dispatch_table dispatch_table;
105 vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
106 &kk_instance_entrypoints, true);
107 vk_instance_dispatch_table_from_entrypoints(
108 &dispatch_table, &wsi_instance_entrypoints, false);
109
110 result = vk_instance_init(&instance->vk, &instance_extensions,
111 &dispatch_table, pCreateInfo, pAllocator);
112 if (result != VK_SUCCESS)
113 goto fail_alloc;
114
115 instance->vk.physical_devices.enumerate = kk_enumerate_physical_devices;
116 instance->vk.physical_devices.destroy = kk_physical_device_destroy;
117
118 const struct build_id_note *note =
119 build_id_find_nhdr_for_addr(kk_CreateInstance);
120 if (!note) {
121 result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
122 "Failed to find build-id");
123 goto fail_init;
124 }
125
126 unsigned build_id_len = build_id_length(note);
127 if (build_id_len != VK_UUID_SIZE) {
128 result = vk_errorf(
129 NULL, VK_ERROR_INITIALIZATION_FAILED,
130 "build-id size does not match expected size (VK_UUID_SIZE which is 16 bytes)");
131 goto fail_init;
132 }
133
134 static_assert(sizeof(instance->driver_build_sha) == VK_UUID_SIZE,
135 "UUID size mismatch");
136 memcpy(instance->driver_build_sha, build_id_data(note), VK_UUID_SIZE);
137
138 kk_process_debug_variable();
139
140 *pInstance = kk_instance_to_handle(instance);
141 return VK_SUCCESS;
142
143 fail_init:
144 vk_instance_finish(&instance->vk);
145 fail_alloc:
146 vk_free(pAllocator, instance);
147
148 return result;
149 }
150
151 VKAPI_ATTR void VKAPI_CALL
152 kk_DestroyInstance(VkInstance _instance,
153 const VkAllocationCallbacks *pAllocator)
154 {
155 VK_FROM_HANDLE(kk_instance, instance, _instance);
156
157 if (!instance)
158 return;
159
160 vk_instance_finish(&instance->vk);
161 vk_free(&instance->vk.alloc, instance);
162 }
163
164 /* We need this to return our own trampoline functions */
165 static PFN_vkVoidFunction
166 kk_instance_get_proc_addr(const struct vk_instance *instance,
167 const struct vk_instance_entrypoint_table *entrypoints,
168 const char *name)
169 {
170 PFN_vkVoidFunction func;
171
172 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
173 * when we have to return valid function pointers, NULL, or it's left
174 * undefined. See the table for exact details.
175 */
176 if (name == NULL)
177 return NULL;
178
179 #define LOOKUP_VK_ENTRYPOINT(entrypoint) \
180 if (strcmp(name, "vk" #entrypoint) == 0) \
181 return (PFN_vkVoidFunction)entrypoints->entrypoint
182
183 LOOKUP_VK_ENTRYPOINT(EnumerateInstanceExtensionProperties);
184 LOOKUP_VK_ENTRYPOINT(EnumerateInstanceLayerProperties);
185 LOOKUP_VK_ENTRYPOINT(EnumerateInstanceVersion);
186 LOOKUP_VK_ENTRYPOINT(CreateInstance);
187
188 /* GetInstanceProcAddr() can also be called with a NULL instance.
189 * See https://gitlab.khronos.org/vulkan/vulkan/issues/2057
190 */
191 LOOKUP_VK_ENTRYPOINT(GetInstanceProcAddr);
192
193 #undef LOOKUP_VK_ENTRYPOINT
194
195 /* Beginning with ICD interface v7, the following functions can also be
196 * retrieved via vk_icdGetInstanceProcAddr.
197 */
198
199 if (strcmp(name, "vk_icdNegotiateLoaderICDInterfaceVersion") == 0)
200 return (PFN_vkVoidFunction)vk_icdNegotiateLoaderICDInterfaceVersion;
201 if (strcmp(name, "vk_icdGetPhysicalDeviceProcAddr") == 0)
202 return (PFN_vkVoidFunction)vk_icdGetPhysicalDeviceProcAddr;
203 #ifdef _WIN32
204 if (strcmp(name, "vk_icdEnumerateAdapterPhysicalDevices") == 0)
205 return (PFN_vkVoidFunction)vk_icdEnumerateAdapterPhysicalDevices;
206 #endif
207
208 if (instance == NULL)
209 return NULL;
210
211 func = vk_instance_dispatch_table_get_if_supported(
212 &instance->dispatch_table, name, instance->app_info.api_version,
213 &instance->enabled_extensions);
214 if (func != NULL)
215 return func;
216
217 func = vk_physical_device_dispatch_table_get_if_supported(
218 &kk_physical_device_trampolines, name, instance->app_info.api_version,
219 &instance->enabled_extensions);
220 if (func != NULL)
221 return func;
222
223 func = vk_device_dispatch_table_get_if_supported(
224 &kk_device_trampolines, name, instance->app_info.api_version,
225 &instance->enabled_extensions, NULL);
226 if (func != NULL)
227 return func;
228
229 return NULL;
230 }
231
232 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
233 kk_GetInstanceProcAddr(VkInstance _instance, const char *pName)
234 {
235 VK_FROM_HANDLE(kk_instance, instance, _instance);
236 return kk_instance_get_proc_addr(&instance->vk, &kk_instance_entrypoints,
237 pName);
238 }
239
240 PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
241 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
242 {
243 return kk_GetInstanceProcAddr(instance, pName);
244 }