1 /*
2 *
3 * Copyright (c) 2014-2022 The Khronos Group Inc.
4 * Copyright (c) 2014-2022 Valve Corporation
5 * Copyright (c) 2014-2022 LunarG, Inc.
6 * Copyright (C) 2015 Google Inc.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * Author: Jon Ashburn <jon@lunarg.com>
21 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
22 * Author: Chia-I Wu <olvaffe@gmail.com>
23 * Author: Chia-I Wu <olv@lunarg.com>
24 * Author: Mark Lobodzinski <mark@LunarG.com>
25 * Author: Lenny Komow <lenny@lunarg.com>
26 * Author: Charles Giessen <charles@lunarg.com>
27 *
28 */
29
30 #pragma once
31
32 #include "loader_common.h"
33
34 // Declare the once_init variable
LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION(once_init)35 LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION(once_init)
36
37 static inline VkPhysicalDevice loader_unwrap_physical_device(VkPhysicalDevice physicalDevice) {
38 struct loader_physical_device_tramp *phys_dev = (struct loader_physical_device_tramp *)physicalDevice;
39 if (PHYS_TRAMP_MAGIC_NUMBER != phys_dev->magic) {
40 return VK_NULL_HANDLE;
41 }
42 return phys_dev->phys_dev;
43 }
44
loader_set_dispatch(void * obj,const void * data)45 static inline void loader_set_dispatch(void *obj, const void *data) { *((const void **)obj) = data; }
46
loader_get_dispatch(const void * obj)47 static inline VkLayerDispatchTable *loader_get_dispatch(const void *obj) {
48 if (VK_NULL_HANDLE == obj) {
49 return NULL;
50 }
51 VkLayerDispatchTable *disp = *((VkLayerDispatchTable **)obj);
52 if (VK_NULL_HANDLE == disp || DEVICE_DISP_TABLE_MAGIC_NUMBER != disp->magic) {
53 return NULL;
54 }
55 return disp;
56 }
57
loader_get_dev_dispatch(const void * obj)58 static inline struct loader_dev_dispatch_table *loader_get_dev_dispatch(const void *obj) {
59 return *((struct loader_dev_dispatch_table **)obj);
60 }
61
loader_get_instance_layer_dispatch(const void * obj)62 static inline VkLayerInstanceDispatchTable *loader_get_instance_layer_dispatch(const void *obj) {
63 return *((VkLayerInstanceDispatchTable **)obj);
64 }
65
loader_get_instance_dispatch(const void * obj)66 static inline struct loader_instance_dispatch_table *loader_get_instance_dispatch(const void *obj) {
67 return *((struct loader_instance_dispatch_table **)obj);
68 }
69
loader_init_dispatch(void * obj,const void * data)70 static inline void loader_init_dispatch(void *obj, const void *data) {
71 #ifdef DEBUG
72 assert(valid_loader_magic_value(obj) &&
73 "Incompatible ICD, first dword must be initialized to "
74 "ICD_LOADER_MAGIC. See loader/README.md for details.");
75 #endif
76
77 loader_set_dispatch(obj, data);
78 }
79
80 // Global variables used across files
81 extern struct loader_struct loader;
82 extern loader_platform_thread_mutex loader_lock;
83 extern loader_platform_thread_mutex loader_json_lock;
84 extern loader_platform_thread_mutex loader_preload_icd_lock;
85 extern loader_platform_thread_mutex loader_global_instance_list_lock;
86
87 bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2);
88
89 VkResult loader_validate_layers(const struct loader_instance *inst, const uint32_t layer_count,
90 const char *const *ppEnabledLayerNames, const struct loader_layer_list *list);
91
92 VkResult loader_validate_instance_extensions(struct loader_instance *inst, const struct loader_extension_list *icd_exts,
93 const struct loader_layer_list *instance_layer,
94 const VkInstanceCreateInfo *pCreateInfo);
95
96 void loader_initialize(void);
97 void loader_release(void);
98 void loader_preload_icds(void);
99 void loader_unload_preloaded_icds(void);
100 bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop, const uint32_t count,
101 const VkExtensionProperties *ext_array);
102 bool has_vk_extension_property(const VkExtensionProperties *vk_ext_prop, const struct loader_extension_list *ext_list);
103
104 void loader_free_layer_properties(const struct loader_instance *inst, struct loader_layer_properties *layer_properties);
105 VkResult loader_add_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list,
106 uint32_t prop_list_count, const VkExtensionProperties *props);
107 VkResult loader_add_to_dev_ext_list(const struct loader_instance *inst, struct loader_device_extension_list *ext_list,
108 const VkExtensionProperties *props, uint32_t entry_count, char **entrys);
109 VkResult loader_add_device_extensions(const struct loader_instance *inst,
110 PFN_vkEnumerateDeviceExtensionProperties fpEnumerateDeviceExtensionProperties,
111 VkPhysicalDevice physical_device, const char *lib_name,
112 struct loader_extension_list *ext_list);
113 VkResult loader_init_generic_list(const struct loader_instance *inst, struct loader_generic_list *list_info, size_t element_size);
114 void loader_destroy_generic_list(const struct loader_instance *inst, struct loader_generic_list *list);
115 void loader_destroy_layer_list(const struct loader_instance *inst, struct loader_device *device,
116 struct loader_layer_list *layer_list);
117 void loader_delete_layer_list_and_properties(const struct loader_instance *inst, struct loader_layer_list *layer_list);
118 VkResult loader_add_layer_name_to_list(const struct loader_instance *inst, const char *name, const enum layer_type_flags type_flags,
119 const struct loader_layer_list *source_list, struct loader_layer_list *target_list,
120 struct loader_layer_list *expanded_target_list);
121 void loader_icd_destroy(struct loader_instance *ptr_inst, struct loader_icd_term *icd_term,
122 const VkAllocationCallbacks *pAllocator);
123 void loader_scanned_icd_clear(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list);
124 VkResult loader_icd_scan(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
125 bool *skipped_portability_drivers);
126 void loader_scan_for_layers(struct loader_instance *inst, struct loader_layer_list *instance_layers);
127 void loader_scan_for_implicit_layers(struct loader_instance *inst, struct loader_layer_list *instance_layers);
128 bool loader_implicit_layer_is_enabled(const struct loader_instance *inst, const struct loader_layer_properties *prop);
129 VkResult loader_get_icd_loader_instance_extensions(const struct loader_instance *inst, struct loader_icd_tramp_list *icd_tramp_list,
130 struct loader_extension_list *inst_exts);
131 struct loader_icd_term *loader_get_icd_and_device(const void *device, struct loader_device **found_dev, uint32_t *icd_index);
132 struct loader_instance *loader_get_instance(const VkInstance instance);
133 void loader_deactivate_layers(const struct loader_instance *instance, struct loader_device *device, struct loader_layer_list *list);
134 struct loader_device *loader_create_logical_device(const struct loader_instance *inst, const VkAllocationCallbacks *pAllocator);
135 void loader_add_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term,
136 struct loader_device *found_dev);
137 void loader_remove_logical_device(const struct loader_instance *inst, struct loader_icd_term *icd_term,
138 struct loader_device *found_dev, const VkAllocationCallbacks *pAllocator);
139 // NOTE: Outside of loader, this entry-point is only provided for error
140 // cleanup.
141 void loader_destroy_logical_device(const struct loader_instance *inst, struct loader_device *dev,
142 const VkAllocationCallbacks *pAllocator);
143
144 VkResult loader_enable_instance_layers(struct loader_instance *inst, const VkInstanceCreateInfo *pCreateInfo,
145 const struct loader_layer_list *instance_layers);
146
147 VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
148 struct loader_instance *inst, VkInstance *created_instance);
149
150 void loader_activate_instance_layer_extensions(struct loader_instance *inst, VkInstance created_inst);
151
152 VKAPI_ATTR VkResult VKAPI_CALL loader_layer_create_device(VkInstance instance, VkPhysicalDevice physicalDevice,
153 const VkDeviceCreateInfo *pCreateInfo,
154 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice,
155 PFN_vkGetInstanceProcAddr layerGIPA, PFN_vkGetDeviceProcAddr *nextGDPA);
156 VKAPI_ATTR void VKAPI_CALL loader_layer_destroy_device(VkDevice device, const VkAllocationCallbacks *pAllocator,
157 PFN_vkDestroyDevice destroyFunction);
158
159 VkResult loader_create_device_chain(const VkPhysicalDevice pd, const VkDeviceCreateInfo *pCreateInfo,
160 const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst,
161 struct loader_device *dev, PFN_vkGetInstanceProcAddr callingLayer,
162 PFN_vkGetDeviceProcAddr *layerNextGDPA);
163
164 VkResult loader_validate_device_extensions(struct loader_instance *this_instance,
165 const struct loader_layer_list *activated_device_layers,
166 const struct loader_extension_list *icd_exts, const VkDeviceCreateInfo *pCreateInfo);
167
168 VkResult setup_loader_tramp_phys_devs(struct loader_instance *inst, uint32_t phys_dev_count, VkPhysicalDevice *phys_devs);
169 VkResult setup_loader_tramp_phys_dev_groups(struct loader_instance *inst, uint32_t group_count,
170 VkPhysicalDeviceGroupProperties *groups);
171
172 VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array);
173 char *loader_get_next_path(char *path);
174 VkResult add_data_files(const struct loader_instance *inst, char *search_path, struct loader_data_files *out_files,
175 bool use_first_found_manifest);
176
177 loader_api_version loader_make_version(uint32_t version);
178 loader_api_version loader_combine_version(uint32_t major, uint32_t minor, uint32_t patch);
179
180 // Helper macros for determining if a version is valid or not
181 bool loader_check_version_meets_required(loader_api_version required, loader_api_version version);
182
183 // Convenience macros for common versions
184 #ifndef LOADER_VERSION_1_0_0
185 #define LOADER_VERSION_1_0_0 loader_combine_version(1, 0, 0)
186 #endif
187
188 #ifndef LOADER_VERSION_1_1_0
189 #define LOADER_VERSION_1_1_0 loader_combine_version(1, 1, 0)
190 #endif
191