blob: 0e62ec6ef2440ac7b4ecc3ab554528b267e65660 [file] [log] [blame]
Chia-I Wuc1e0f962014-08-04 08:03:57 +08001/*
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wuc1e0f962014-08-04 08:03:57 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wud8ab1e62014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wuc1e0f962014-08-04 08:03:57 +080026 */
27
28#ifndef LOADER_H
29#define LOADER_H
30
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060031#include <vulkan.h>
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060032#include <vk_debug_report_lunarg.h>
Chia-I Wu73e9db32015-04-16 22:02:10 +080033#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060034#include <vkLayer.h>
35#include <vkIcd.h>
Chia-I Wufe333ed2015-04-11 15:34:07 +080036#include <assert.h>
37
Chia-I Wuc1e0f962014-08-04 08:03:57 +080038#if defined(__GNUC__) && __GNUC__ >= 4
39# define LOADER_EXPORT __attribute__((visibility("default")))
40#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
41# define LOADER_EXPORT __attribute__((visibility("default")))
42#else
43# define LOADER_EXPORT
44#endif
45
Courtney Goeltzenleuchter99ad4482015-06-07 17:28:17 -060046#define MAX_EXTENSION_NAME_SIZE 63
Jon Ashburn13482452015-05-12 17:26:48 -060047#define MAX_LAYER_LIBRARIES 64
48#define MAX_GPUS_FOR_LAYER 16
49
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060050enum extension_origin {
51 VK_EXTENSION_ORIGIN_ICD,
52 VK_EXTENSION_ORIGIN_LAYER,
53 VK_EXTENSION_ORIGIN_LOADER
Jon Ashburn13482452015-05-12 17:26:48 -060054};
55
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060056struct loader_extension_property {
57 VkExtensionProperties info;
58 const char *lib_name;
59 enum extension_origin origin;
Courtney Goeltzenleuchter99ad4482015-06-07 17:28:17 -060060 // An extension library can export the same extension
61 // under different names. Handy to provide a "grouping"
62 // such as Validation. However, the loader requires
63 // that a layer be included only once in a chain.
64 // During layer scanning the loader will check if
65 // the vkGet*ProcAddr is the same as an existing extension
66 // If so, it will link them together via the alias pointer.
67 // At initialization time we'll follow the alias pointer
68 // to the "base" extension and then use that extension
69 // internally to ensure we reject duplicates
70 char get_extension_info_name[MAX_EXTENSION_NAME_SIZE+1];
71 struct loader_extension_property *alias;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060072 bool hosted; // does the extension reside in one driver/layer
73};
74
75struct loader_extension_list {
76 size_t capacity;
77 uint32_t count;
78 struct loader_extension_property *list;
79};
80
81struct loader_scanned_layers {
82 char *lib_name;
83
84 /* cache of global extensions for a specific layer */
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060085 struct loader_extension_list global_extension_list;
86
Courtney Goeltzenleuchter1c512402015-06-17 20:51:59 -060087 bool physical_device_extensions_supported;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060088 /*
89 * cache of device extensions for a specific layer,
90 * filled in at CreateInstance time
91 */
Courtney Goeltzenleuchter1c512402015-06-17 20:51:59 -060092 struct loader_extension_list physical_device_extension_list;
Jon Ashburn13482452015-05-12 17:26:48 -060093};
94
Jon Ashburnebfa25e2015-05-15 15:09:35 -060095struct loader_icd {
96 const struct loader_scanned_icds *scanned_icds;
97
98 VkLayerDispatchTable *loader_dispatch;
99 uint32_t layer_count[MAX_GPUS_FOR_LAYER];
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600100 VkBaseLayerObject *wrappedGpus[MAX_GPUS_FOR_LAYER];
101 uint32_t gpu_count;
102 VkBaseLayerObject *gpus;
103 VkInstance instance; // instance object from the icd
Jon Ashburnc89dd4a2015-05-18 13:20:15 -0600104 PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600105 PFN_vkDestroyInstance DestroyInstance;
106 PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
107 PFN_vkGetPhysicalDeviceInfo GetPhysicalDeviceInfo;
108 PFN_vkCreateDevice CreateDevice;
109 PFN_vkGetPhysicalDeviceExtensionInfo GetPhysicalDeviceExtensionInfo;
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600110 PFN_vkGetMultiDeviceCompatibility GetMultiDeviceCompatibility;
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600111 PFN_vkGetDisplayInfoWSI GetDisplayInfoWSI;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600112 PFN_vkDbgCreateMsgCallback DbgCreateMsgCallback;
113 PFN_vkDbgDestroyMsgCallback DbgDestroyMsgCallback;
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600114 struct loader_icd *next;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600115
116 uint32_t app_extension_count[MAX_GPUS_FOR_LAYER];
117 VkExtensionProperties *app_extension_props[MAX_GPUS_FOR_LAYER];
118
Courtney Goeltzenleuchter1c512402015-06-17 20:51:59 -0600119 /*
120 * Fill in the cache of available extensions from all layers that
121 * operate with this physical device.
122 * This cache will be used to satisfy calls to GetPhysicalDeviceExtensionInfo
123 */
124 struct loader_extension_list device_extension_cache[MAX_GPUS_FOR_LAYER];
125
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600126 struct loader_extension_list enabled_device_extensions[MAX_GPUS_FOR_LAYER];
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600127};
128
Jon Ashburn13482452015-05-12 17:26:48 -0600129struct loader_instance {
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600130 VkLayerInstanceDispatchTable *disp; // must be first entry in structure
131
Jon Ashburn13482452015-05-12 17:26:48 -0600132 uint32_t layer_count;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600133// struct loader_layers layer_libs[MAX_LAYER_LIBRARIES];
Jon Ashburn13482452015-05-12 17:26:48 -0600134 VkBaseLayerObject *wrappedInstance;
135 uint32_t total_gpu_count;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600136 uint32_t total_icd_count;
Jon Ashburn13482452015-05-12 17:26:48 -0600137 struct loader_icd *icds;
138 struct loader_instance *next;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600139
140 /* TODO: Should keep track of application provided allocation functions */
141
142 /*
143 * CreateMsgCallback is global and needs to be
144 * applied to all layers and ICDs.
145 * What happens if a layer is enabled on both the instance chain
146 * as well as the device chain and a call to CreateMsgCallback is made?
147 * Do we need to make sure that each layer / driver only gets called once?
148 * Should a layer implementing support for CreateMsgCallback only be allowed (?)
149 * to live on one chain? Or maybe make it the application's responsibility.
150 * If the app enables DRAW_STATE on at both CreateInstance time and CreateDevice
151 * time, CreateMsgCallback will call the DRAW_STATE layer twice. Once via
152 * the instance chain and once via the device chain.
153 * The loader should only return the DEBUG_REPORT extension as supported
154 * for the GetGlobalExtensionSupport call. That should help eliminate one
155 * duplication.
156 * Since the instance chain requires us iterating over the available ICDs
157 * and each ICD will have it's own unique MsgCallback object we need to
158 * track those objects to give back the right one.
159 * This also implies that the loader has to intercept vkDestroyObject and
160 * if the extension is enabled and the object type is a MsgCallback then
161 * we must translate the object into the proper ICD specific ones.
162 * DestroyObject works on a device chain. Should not be what's destroying
163 * the MsgCallback object. That needs to be an instance thing. So, since
164 * we used an instance to create it, we need a custom Destroy that also
165 * takes an instance. That way we can iterate over the ICDs properly.
166 * Example use:
167 * CreateInstance: DEBUG_REPORT
168 * Loader will create instance chain with enabled extensions.
169 * TODO: Should validation layers be enabled here? If not, they will not be in the instance chain.
170 * fn = GetProcAddr(INSTANCE, "vkCreateMsgCallback") -> point to loader's vkCreateMsgCallback
171 * App creates a callback object: fn(..., &MsgCallbackObject1)
172 * Have only established the instance chain so far. Loader will call the instance chain.
173 * Each layer in the instance chain will call down to the next layer, terminating with
174 * the CreateMsgCallback loader terminator function that creates the actual MsgCallbackObject1 object.
175 * The loader CreateMsgCallback terminator will iterate over the ICDs.
176 * Calling each ICD that supports vkCreateMsgCallback and collect answers in icd_msg_callback_map here.
177 * As result is sent back up the chain each layer has opportunity to record the callback operation and
178 * appropriate MsgCallback object.
179 * ...
180 * Any reports matching the flags set in MsgCallbackObject1 will generate the defined callback behavior
181 * in the layer / ICD that initiated that report.
182 * ...
183 * CreateDevice: MemTracker:...
184 * App does not include DEBUG_REPORT as that is a global extension.
185 * TODO: GetExtensionSupport must not report DEBUG_REPORT when using instance.
186 * App MUST include any desired validation layers or they will not participate in the device call chain.
187 * App creates a callback object: fn(..., &MsgCallbackObject2)
188 * Loader's vkCreateMsgCallback is called.
189 * Loader sends call down instance chain - this is a global extension - any validation layer that was
190 * enabled at CreateInstance will be able to register the callback. Loader will iterate over the ICDs and
191 * will record the ICD's version of the MsgCallback2 object here.
192 * ...
193 * Any report will go to the layer's report function and it will check the flags for MsgCallbackObject1
194 * and MsgCallbackObject2 and take the appropriate action as indicated by the app.
195 * ...
196 * App calls vkDestroyMsgCallback( MsgCallbackObject1 )
197 * Loader's DestroyMsgCallback is where call starts. DestroyMsgCallback will be sent down instance chain
198 * ending in the loader's DestroyMsgCallback terminator which will iterate over the ICD's destroying each
199 * ICD version of that MsgCallback object and then destroy the loader's version of the object.
200 * Any reports generated after this will only have MsgCallbackObject2 available.
201 */
202 struct loader_msg_callback_map_entry *icd_msg_callback_map;
203
204 struct loader_extension_list enabled_instance_extensions;
Courtney Goeltzenleuchter99ad4482015-06-07 17:28:17 -0600205 struct loader_extension_list activated_layer_list;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600206
207 uint32_t app_extension_count;
208 VkExtensionProperties *app_extension_props;
209
210 bool debug_report_enabled;
211 bool wsi_lunarg_enabled;
212 VkLayerDbgFunctionNode *DbgFunctionHead;
213};
214
215struct loader_lib_info {
216 const char *lib_name;
217 uint32_t ref_count;
218 loader_platform_dl_handle lib_handle;
Jon Ashburn13482452015-05-12 17:26:48 -0600219};
220
221struct loader_struct {
222 struct loader_instance *instances;
223 bool icds_scanned;
224 struct loader_scanned_icds *scanned_icd_list;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600225 bool layers_scanned;
226
227 unsigned int loaded_layer_lib_count;
228 struct loader_lib_info *loaded_layer_lib_list;
229
Jon Ashburn13482452015-05-12 17:26:48 -0600230 char *layer_dirs;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600231
232 /* TODO: eliminate fixed limit */
233 unsigned int scanned_layer_count; // indicate number of scanned layers
Jon Ashburn13482452015-05-12 17:26:48 -0600234 size_t scanned_ext_list_capacity;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600235 struct loader_scanned_layers scanned_layers[MAX_LAYER_LIBRARIES];
236
237 /* Keep track of all the extensions available via GetGlobalExtensionInfo */
238 struct loader_extension_list global_extensions;
239};
240
241struct loader_scanned_icds {
242 char *lib_name;
243 loader_platform_dl_handle handle;
244
245 PFN_vkCreateInstance CreateInstance;
246 PFN_vkDestroyInstance DestroyInstance;
247 PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
248 PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
249 PFN_vkGetPhysicalDeviceExtensionInfo GetPhysicalDeviceExtensionInfo;
250 VkInstance instance;
251 struct loader_scanned_icds *next;
252
253 /* cache of global extensions for specific ICD */
254 struct loader_extension_list global_extension_list;
255
256 /*
257 * cache of device extensions for specific ICD,
258 * filled in at CreateInstance time
259 */
Courtney Goeltzenleuchter1c512402015-06-17 20:51:59 -0600260 struct loader_extension_list device_extension_list;
Jon Ashburn13482452015-05-12 17:26:48 -0600261};
262
Jon Ashburn62a54d12015-05-01 18:00:33 -0600263static inline void loader_set_dispatch(VkObject obj, const void *data)
Chia-I Wufe333ed2015-04-11 15:34:07 +0800264{
265 *((const void **) obj) = data;
266}
267
Jon Ashburn62a54d12015-05-01 18:00:33 -0600268static inline VkLayerDispatchTable *loader_get_dispatch(const VkObject obj)
Chia-I Wufe333ed2015-04-11 15:34:07 +0800269{
Jon Ashburn62a54d12015-05-01 18:00:33 -0600270 return *((VkLayerDispatchTable **) obj);
Chia-I Wufe333ed2015-04-11 15:34:07 +0800271}
272
Jon Ashburn13482452015-05-12 17:26:48 -0600273static inline VkLayerInstanceDispatchTable *loader_get_instance_dispatch(const VkObject obj)
274{
275 return *((VkLayerInstanceDispatchTable **) obj);
276}
277
Jon Ashburn62a54d12015-05-01 18:00:33 -0600278static inline void loader_init_dispatch(VkObject obj, const void *data)
Chia-I Wufe333ed2015-04-11 15:34:07 +0800279{
Jon Ashburn167b2b12015-04-15 13:34:33 -0600280#ifdef DEBUG
Chia-I Wufe333ed2015-04-11 15:34:07 +0800281 assert(valid_loader_magic_value(obj) &&
282 "Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.");
Jon Ashburn167b2b12015-04-15 13:34:33 -0600283#endif
Chia-I Wufe333ed2015-04-11 15:34:07 +0800284
Jon Ashburn62a54d12015-05-01 18:00:33 -0600285 loader_set_dispatch(obj, data);
Chia-I Wufe333ed2015-04-11 15:34:07 +0800286}
287
Jon Ashburn13482452015-05-12 17:26:48 -0600288/* global variables used across files */
289extern struct loader_struct loader;
290extern LOADER_PLATFORM_THREAD_ONCE_DEFINITION(once_icd);
291extern LOADER_PLATFORM_THREAD_ONCE_DEFINITION(once_layer);
292extern LOADER_PLATFORM_THREAD_ONCE_DEFINITION(once_exts);
293extern VkLayerInstanceDispatchTable instance_disp;
Jon Ashburn37e7f972015-04-06 10:58:22 -0600294
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600295struct loader_msg_callback_map_entry {
296 VkDbgMsgCallback icd_obj;
297 VkDbgMsgCallback loader_obj;
298};
299
300bool compare_vk_extension_properties(
301 const VkExtensionProperties* op1,
302 const VkExtensionProperties* op2);
303
Jon Ashburn13482452015-05-12 17:26:48 -0600304/* instance layer chain termination entrypoint definitions */
305VkResult loader_CreateInstance(
306 const VkInstanceCreateInfo* pCreateInfo,
307 VkInstance* pInstance);
Chia-I Wufe333ed2015-04-11 15:34:07 +0800308
Jon Ashburn13482452015-05-12 17:26:48 -0600309VkResult loader_DestroyInstance(
310 VkInstance instance);
311
312VkResult loader_EnumeratePhysicalDevices(
313 VkInstance instance,
314 uint32_t* pPhysicalDeviceCount,
315 VkPhysicalDevice* pPhysicalDevices);
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600316VkResult loader_GetPhysicalDeviceInfo(
317 VkPhysicalDevice gpu,
318 VkPhysicalDeviceInfoType infoType,
319 size_t* pDataSize,
320 void* pData);
321
322VkResult loader_CreateDevice(
323 VkPhysicalDevice gpu,
324 const VkDeviceCreateInfo* pCreateInfo,
325 VkDevice* pDevice);
Jon Ashburn13482452015-05-12 17:26:48 -0600326
Jon Ashburne8c90eb2015-05-18 15:28:32 -0600327#if 0
Jon Ashburn13482452015-05-12 17:26:48 -0600328VkResult VKAPI loader_GetGlobalExtensionInfo(
329 VkExtensionInfoType infoType,
330 uint32_t extensionIndex,
331 size_t* pDataSize,
332 void* pData);
333
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600334VkResult loader_GetPhysicalDeviceExtensionInfo(
335 VkPhysicalDevice gpu,
336 VkExtensionInfoType infoType,
337 uint32_t extensionIndex,
338 size_t* pDataSize,
339 void* pData);
Courtney Goeltzenleuchter1c512402015-06-17 20:51:59 -0600340#endif
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600341
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600342VkResult loader_GetMultiDeviceCompatibility(
343 VkPhysicalDevice gpu0,
344 VkPhysicalDevice gpu1,
345 VkPhysicalDeviceCompatibilityInfo* pInfo);
346
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600347/* helper function definitions */
348bool has_vk_extension_property(
349 const VkExtensionProperties *vk_ext_prop,
350 const struct loader_extension_list *ext_list);
Jon Ashburn13482452015-05-12 17:26:48 -0600351
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600352void loader_add_to_ext_list(
353 struct loader_extension_list *ext_list,
354 uint32_t prop_list_count,
355 const struct loader_extension_property *props);
Jon Ashburn13482452015-05-12 17:26:48 -0600356
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600357void loader_enable_instance_layers(struct loader_instance *inst);
Jon Ashburn13482452015-05-12 17:26:48 -0600358
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600359bool loader_is_extension_scanned(const VkExtensionProperties *ext_prop);
Jon Ashburn13482452015-05-12 17:26:48 -0600360void loader_icd_scan(void);
361void layer_lib_scan(void);
362void loader_coalesce_extensions(void);
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600363
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600364struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu,
365 uint32_t *gpu_index);
Jon Ashburn13482452015-05-12 17:26:48 -0600366uint32_t loader_activate_instance_layers(struct loader_instance *inst);
Courtney Goeltzenleuchter1c512402015-06-17 20:51:59 -0600367void loader_activate_instance_layer_extensions(struct loader_instance *inst);
Chia-I Wuc1e0f962014-08-04 08:03:57 +0800368#endif /* LOADER_H */