• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3  * SPDX-License-Identifier: MIT
4  */
5 #include "nvk_instance.h"
6 
7 #include "nvk_entrypoints.h"
8 #include "nvk_physical_device.h"
9 
10 #include "vulkan/wsi/wsi_common.h"
11 
12 #include "util/build_id.h"
13 #include "util/detect_os.h"
14 #include "util/driconf.h"
15 #include "util/mesa-sha1.h"
16 #include "util/u_debug.h"
17 
18 #if DETECT_OS_ANDROID
19 #include "util/u_gralloc/u_gralloc.h"
20 #include "vk_android.h"
21 #endif
22 
23 VKAPI_ATTR VkResult VKAPI_CALL
nvk_EnumerateInstanceVersion(uint32_t * pApiVersion)24 nvk_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, 4, VK_HEADER_VERSION);
29 
30    return VK_SUCCESS;
31 }
32 
33 static const struct vk_instance_extension_table instance_extensions = {
34 #ifdef NVK_USE_WSI_PLATFORM
35    .KHR_get_surface_capabilities2 = true,
36    .KHR_surface = true,
37    .KHR_surface_protected_capabilities = true,
38    .EXT_surface_maintenance1 = true,
39    .EXT_swapchain_colorspace = true,
40 #endif
41 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
42    .KHR_wayland_surface = true,
43 #endif
44 #ifdef VK_USE_PLATFORM_XCB_KHR
45    .KHR_xcb_surface = true,
46 #endif
47 #ifdef VK_USE_PLATFORM_XLIB_KHR
48    .KHR_xlib_surface = true,
49 #endif
50 #ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
51    .EXT_acquire_xlib_display = true,
52 #endif
53 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
54    .KHR_display = true,
55    .KHR_get_display_properties2 = true,
56    .EXT_direct_mode_display = true,
57    .EXT_display_surface_counter = true,
58    .EXT_acquire_drm_display = true,
59 #endif
60 #ifndef VK_USE_PLATFORM_WIN32_KHR
61    .EXT_headless_surface = true,
62 #endif
63    .KHR_device_group_creation = true,
64    .KHR_external_fence_capabilities = true,
65    .KHR_external_memory_capabilities = true,
66    .KHR_external_semaphore_capabilities = true,
67    .KHR_get_physical_device_properties2 = true,
68    .EXT_debug_report = true,
69    .EXT_debug_utils = true,
70 };
71 
72 VKAPI_ATTR VkResult VKAPI_CALL
nvk_EnumerateInstanceExtensionProperties(const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)73 nvk_EnumerateInstanceExtensionProperties(const char *pLayerName,
74                                          uint32_t *pPropertyCount,
75                                          VkExtensionProperties *pProperties)
76 {
77    if (pLayerName)
78       return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
79 
80    return vk_enumerate_instance_extension_properties(
81       &instance_extensions, pPropertyCount, pProperties);
82 }
83 
84 static void
nvk_init_debug_flags(struct nvk_instance * instance)85 nvk_init_debug_flags(struct nvk_instance *instance)
86 {
87    const struct debug_control flags[] = {
88       { "push_dump", NVK_DEBUG_PUSH_DUMP },
89       { "push", NVK_DEBUG_PUSH_DUMP },
90       { "push_sync", NVK_DEBUG_PUSH_SYNC },
91       { "zero_memory", NVK_DEBUG_ZERO_MEMORY },
92       { "vm", NVK_DEBUG_VM },
93       { "no_cbuf", NVK_DEBUG_NO_CBUF },
94       { "edb_bview", NVK_DEBUG_FORCE_EDB_BVIEW },
95       { "gart", NVK_DEBUG_FORCE_GART },
96       { NULL, 0 },
97    };
98 
99    instance->debug_flags = parse_debug_string(getenv("NVK_DEBUG"), flags);
100 }
101 
102 static const driOptionDescription nvk_dri_options[] = {
103    DRI_CONF_SECTION_PERFORMANCE
104       DRI_CONF_ADAPTIVE_SYNC(true)
105       DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0)
106       DRI_CONF_VK_X11_STRICT_IMAGE_COUNT(false)
107       DRI_CONF_VK_X11_ENSURE_MIN_IMAGE_COUNT(false)
108       DRI_CONF_VK_KHR_PRESENT_WAIT(false)
109       DRI_CONF_VK_XWAYLAND_WAIT_READY(false)
110    DRI_CONF_SECTION_END
111 
112    DRI_CONF_SECTION_DEBUG
113       DRI_CONF_FORCE_VK_VENDOR()
114       DRI_CONF_VK_WSI_FORCE_SWAPCHAIN_TO_CURRENT_EXTENT(false)
115       DRI_CONF_VK_X11_IGNORE_SUBOPTIMAL(false)
116       DRI_CONF_VK_ZERO_VRAM(false)
117    DRI_CONF_SECTION_END
118 };
119 
120 static void
nvk_init_dri_options(struct nvk_instance * instance)121 nvk_init_dri_options(struct nvk_instance *instance)
122 {
123    driParseOptionInfo(&instance->available_dri_options, nvk_dri_options, ARRAY_SIZE(nvk_dri_options));
124    driParseConfigFiles(&instance->dri_options, &instance->available_dri_options, 0, "nvk", NULL, NULL,
125                        instance->vk.app_info.app_name, instance->vk.app_info.app_version,
126                        instance->vk.app_info.engine_name, instance->vk.app_info.engine_version);
127 
128    instance->force_vk_vendor =
129       driQueryOptioni(&instance->dri_options, "force_vk_vendor");
130 
131    if (driQueryOptionb(&instance->dri_options, "vk_zero_vram"))
132       instance->debug_flags |= NVK_DEBUG_ZERO_MEMORY;
133 }
134 
135 VKAPI_ATTR VkResult VKAPI_CALL
nvk_CreateInstance(const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkInstance * pInstance)136 nvk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
137                    const VkAllocationCallbacks *pAllocator,
138                    VkInstance *pInstance)
139 {
140    struct nvk_instance *instance;
141    VkResult result;
142 
143    if (pAllocator == NULL)
144       pAllocator = vk_default_allocator();
145 
146    instance = vk_alloc(pAllocator, sizeof(*instance), 8,
147                        VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
148    if (!instance)
149       return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
150 
151    struct vk_instance_dispatch_table dispatch_table;
152    vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
153                                                &nvk_instance_entrypoints,
154                                                true);
155    vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
156                                                &wsi_instance_entrypoints,
157                                                false);
158 
159    result = vk_instance_init(&instance->vk, &instance_extensions,
160                              &dispatch_table, pCreateInfo, pAllocator);
161    if (result != VK_SUCCESS)
162       goto fail_alloc;
163 
164    nvk_init_debug_flags(instance);
165    nvk_init_dri_options(instance);
166 
167    instance->vk.physical_devices.try_create_for_drm =
168       nvk_create_drm_physical_device;
169    instance->vk.physical_devices.destroy = nvk_physical_device_destroy;
170 
171    const struct build_id_note *note =
172       build_id_find_nhdr_for_addr(nvk_CreateInstance);
173    if (!note) {
174       result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
175                          "Failed to find build-id");
176       goto fail_init;
177    }
178 
179    unsigned build_id_len = build_id_length(note);
180    if (build_id_len < SHA1_DIGEST_LENGTH) {
181       result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
182                         "build-id too short.  It needs to be a SHA");
183       goto fail_init;
184    }
185 
186    STATIC_ASSERT(sizeof(instance->driver_build_sha) == SHA1_DIGEST_LENGTH);
187    memcpy(instance->driver_build_sha, build_id_data(note), SHA1_DIGEST_LENGTH);
188 
189 #if DETECT_OS_ANDROID
190    struct u_gralloc *u_gralloc = vk_android_init_ugralloc();
191 
192    if (u_gralloc && u_gralloc_get_type(u_gralloc) == U_GRALLOC_TYPE_FALLBACK) {
193       mesa_logw(
194          "nvk: Gralloc is not supported. Android extensions are disabled.");
195       vk_android_destroy_ugralloc();
196    }
197 #endif
198 
199    *pInstance = nvk_instance_to_handle(instance);
200    return VK_SUCCESS;
201 
202 fail_init:
203    vk_instance_finish(&instance->vk);
204 fail_alloc:
205    vk_free(pAllocator, instance);
206 
207    return result;
208 }
209 
210 VKAPI_ATTR void VKAPI_CALL
nvk_DestroyInstance(VkInstance _instance,const VkAllocationCallbacks * pAllocator)211 nvk_DestroyInstance(VkInstance _instance,
212                     const VkAllocationCallbacks *pAllocator)
213 {
214    VK_FROM_HANDLE(nvk_instance, instance, _instance);
215 
216    if (!instance)
217       return;
218 
219 #if DETECT_OS_ANDROID
220    vk_android_destroy_ugralloc();
221 #endif
222 
223    driDestroyOptionCache(&instance->dri_options);
224    driDestroyOptionInfo(&instance->available_dri_options);
225 
226    vk_instance_finish(&instance->vk);
227    vk_free(&instance->vk.alloc, instance);
228 }
229 
230 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
nvk_GetInstanceProcAddr(VkInstance _instance,const char * pName)231 nvk_GetInstanceProcAddr(VkInstance _instance, const char *pName)
232 {
233    VK_FROM_HANDLE(nvk_instance, instance, _instance);
234    return vk_instance_get_proc_addr(&instance->vk,
235                                     &nvk_instance_entrypoints,
236                                     pName);
237 }
238 
239 PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(VkInstance instance,const char * pName)240 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
241 {
242    return nvk_GetInstanceProcAddr(instance, pName);
243 }
244