blob: 73b780fd302a827c62133227a861aba825c7f7da [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
Jon Ashburn13482452015-05-12 17:26:48 -060046#define MAX_LAYER_LIBRARIES 64
47#define MAX_GPUS_FOR_LAYER 16
48
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060049enum extension_origin {
50 VK_EXTENSION_ORIGIN_ICD,
51 VK_EXTENSION_ORIGIN_LAYER,
52 VK_EXTENSION_ORIGIN_LOADER
Jon Ashburn13482452015-05-12 17:26:48 -060053};
54
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -060055struct loader_extension_property {
56 VkExtensionProperties info;
57 const char *lib_name;
58 enum extension_origin origin;
59 bool hosted; // does the extension reside in one driver/layer
60};
61
62struct loader_extension_list {
63 size_t capacity;
64 uint32_t count;
65 struct loader_extension_property *list;
66};
67
68struct loader_scanned_layers {
69 char *lib_name;
70
71 /* cache of global extensions for a specific layer */
72// uint32_t global_extension_count;
73// struct loader_extension_property *global_extensions;
74 struct loader_extension_list global_extension_list;
75
76 /*
77 * cache of device extensions for a specific layer,
78 * filled in at CreateInstance time
79 */
80 uint32_t device_ext_count;
81 struct loader_extension_property *device_ext_list;
Jon Ashburn13482452015-05-12 17:26:48 -060082};
83
Jon Ashburnebfa25e2015-05-15 15:09:35 -060084struct loader_icd {
85 const struct loader_scanned_icds *scanned_icds;
86
87 VkLayerDispatchTable *loader_dispatch;
88 uint32_t layer_count[MAX_GPUS_FOR_LAYER];
Jon Ashburnebfa25e2015-05-15 15:09:35 -060089 VkBaseLayerObject *wrappedGpus[MAX_GPUS_FOR_LAYER];
90 uint32_t gpu_count;
91 VkBaseLayerObject *gpus;
92 VkInstance instance; // instance object from the icd
Jon Ashburnc89dd4a2015-05-18 13:20:15 -060093 PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
Jon Ashburnebfa25e2015-05-15 15:09:35 -060094 PFN_vkDestroyInstance DestroyInstance;
95 PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
96 PFN_vkGetPhysicalDeviceInfo GetPhysicalDeviceInfo;
97 PFN_vkCreateDevice CreateDevice;
98 PFN_vkGetPhysicalDeviceExtensionInfo GetPhysicalDeviceExtensionInfo;
Jon Ashburnebfa25e2015-05-15 15:09:35 -060099 PFN_vkGetMultiDeviceCompatibility GetMultiDeviceCompatibility;
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600100 PFN_vkGetDisplayInfoWSI GetDisplayInfoWSI;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600101 PFN_vkDbgCreateMsgCallback DbgCreateMsgCallback;
102 PFN_vkDbgDestroyMsgCallback DbgDestroyMsgCallback;
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600103 struct loader_icd *next;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600104
105 uint32_t app_extension_count[MAX_GPUS_FOR_LAYER];
106 VkExtensionProperties *app_extension_props[MAX_GPUS_FOR_LAYER];
107
108 struct loader_extension_list enabled_device_extensions[MAX_GPUS_FOR_LAYER];
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600109};
110
Jon Ashburn13482452015-05-12 17:26:48 -0600111struct loader_instance {
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600112 VkLayerInstanceDispatchTable *disp; // must be first entry in structure
113
Jon Ashburn13482452015-05-12 17:26:48 -0600114 uint32_t layer_count;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600115// struct loader_layers layer_libs[MAX_LAYER_LIBRARIES];
Jon Ashburn13482452015-05-12 17:26:48 -0600116 VkBaseLayerObject *wrappedInstance;
117 uint32_t total_gpu_count;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600118 uint32_t total_icd_count;
Jon Ashburn13482452015-05-12 17:26:48 -0600119 struct loader_icd *icds;
120 struct loader_instance *next;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600121
122 /* TODO: Should keep track of application provided allocation functions */
123
124 /*
125 * CreateMsgCallback is global and needs to be
126 * applied to all layers and ICDs.
127 * What happens if a layer is enabled on both the instance chain
128 * as well as the device chain and a call to CreateMsgCallback is made?
129 * Do we need to make sure that each layer / driver only gets called once?
130 * Should a layer implementing support for CreateMsgCallback only be allowed (?)
131 * to live on one chain? Or maybe make it the application's responsibility.
132 * If the app enables DRAW_STATE on at both CreateInstance time and CreateDevice
133 * time, CreateMsgCallback will call the DRAW_STATE layer twice. Once via
134 * the instance chain and once via the device chain.
135 * The loader should only return the DEBUG_REPORT extension as supported
136 * for the GetGlobalExtensionSupport call. That should help eliminate one
137 * duplication.
138 * Since the instance chain requires us iterating over the available ICDs
139 * and each ICD will have it's own unique MsgCallback object we need to
140 * track those objects to give back the right one.
141 * This also implies that the loader has to intercept vkDestroyObject and
142 * if the extension is enabled and the object type is a MsgCallback then
143 * we must translate the object into the proper ICD specific ones.
144 * DestroyObject works on a device chain. Should not be what's destroying
145 * the MsgCallback object. That needs to be an instance thing. So, since
146 * we used an instance to create it, we need a custom Destroy that also
147 * takes an instance. That way we can iterate over the ICDs properly.
148 * Example use:
149 * CreateInstance: DEBUG_REPORT
150 * Loader will create instance chain with enabled extensions.
151 * TODO: Should validation layers be enabled here? If not, they will not be in the instance chain.
152 * fn = GetProcAddr(INSTANCE, "vkCreateMsgCallback") -> point to loader's vkCreateMsgCallback
153 * App creates a callback object: fn(..., &MsgCallbackObject1)
154 * Have only established the instance chain so far. Loader will call the instance chain.
155 * Each layer in the instance chain will call down to the next layer, terminating with
156 * the CreateMsgCallback loader terminator function that creates the actual MsgCallbackObject1 object.
157 * The loader CreateMsgCallback terminator will iterate over the ICDs.
158 * Calling each ICD that supports vkCreateMsgCallback and collect answers in icd_msg_callback_map here.
159 * As result is sent back up the chain each layer has opportunity to record the callback operation and
160 * appropriate MsgCallback object.
161 * ...
162 * Any reports matching the flags set in MsgCallbackObject1 will generate the defined callback behavior
163 * in the layer / ICD that initiated that report.
164 * ...
165 * CreateDevice: MemTracker:...
166 * App does not include DEBUG_REPORT as that is a global extension.
167 * TODO: GetExtensionSupport must not report DEBUG_REPORT when using instance.
168 * App MUST include any desired validation layers or they will not participate in the device call chain.
169 * App creates a callback object: fn(..., &MsgCallbackObject2)
170 * Loader's vkCreateMsgCallback is called.
171 * Loader sends call down instance chain - this is a global extension - any validation layer that was
172 * enabled at CreateInstance will be able to register the callback. Loader will iterate over the ICDs and
173 * will record the ICD's version of the MsgCallback2 object here.
174 * ...
175 * Any report will go to the layer's report function and it will check the flags for MsgCallbackObject1
176 * and MsgCallbackObject2 and take the appropriate action as indicated by the app.
177 * ...
178 * App calls vkDestroyMsgCallback( MsgCallbackObject1 )
179 * Loader's DestroyMsgCallback is where call starts. DestroyMsgCallback will be sent down instance chain
180 * ending in the loader's DestroyMsgCallback terminator which will iterate over the ICD's destroying each
181 * ICD version of that MsgCallback object and then destroy the loader's version of the object.
182 * Any reports generated after this will only have MsgCallbackObject2 available.
183 */
184 struct loader_msg_callback_map_entry *icd_msg_callback_map;
185
186 struct loader_extension_list enabled_instance_extensions;
187
188 uint32_t app_extension_count;
189 VkExtensionProperties *app_extension_props;
190
191 bool debug_report_enabled;
192 bool wsi_lunarg_enabled;
193 VkLayerDbgFunctionNode *DbgFunctionHead;
194};
195
196struct loader_lib_info {
197 const char *lib_name;
198 uint32_t ref_count;
199 loader_platform_dl_handle lib_handle;
Jon Ashburn13482452015-05-12 17:26:48 -0600200};
201
202struct loader_struct {
203 struct loader_instance *instances;
204 bool icds_scanned;
205 struct loader_scanned_icds *scanned_icd_list;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600206 bool layers_scanned;
207
208 unsigned int loaded_layer_lib_count;
209 struct loader_lib_info *loaded_layer_lib_list;
210
Jon Ashburn13482452015-05-12 17:26:48 -0600211 char *layer_dirs;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600212
213 /* TODO: eliminate fixed limit */
214 unsigned int scanned_layer_count; // indicate number of scanned layers
Jon Ashburn13482452015-05-12 17:26:48 -0600215 size_t scanned_ext_list_capacity;
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600216 struct loader_scanned_layers scanned_layers[MAX_LAYER_LIBRARIES];
217
218 /* Keep track of all the extensions available via GetGlobalExtensionInfo */
219 struct loader_extension_list global_extensions;
220};
221
222struct loader_scanned_icds {
223 char *lib_name;
224 loader_platform_dl_handle handle;
225
226 PFN_vkCreateInstance CreateInstance;
227 PFN_vkDestroyInstance DestroyInstance;
228 PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
229 PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
230 PFN_vkGetPhysicalDeviceExtensionInfo GetPhysicalDeviceExtensionInfo;
231 VkInstance instance;
232 struct loader_scanned_icds *next;
233
234 /* cache of global extensions for specific ICD */
235 struct loader_extension_list global_extension_list;
236
237 /*
238 * cache of device extensions for specific ICD,
239 * filled in at CreateInstance time
240 */
241 uint32_t device_extension_count;
242 struct loader_extension_property *device_extensions;
Jon Ashburn13482452015-05-12 17:26:48 -0600243};
244
Jon Ashburn62a54d12015-05-01 18:00:33 -0600245static inline void loader_set_dispatch(VkObject obj, const void *data)
Chia-I Wufe333ed2015-04-11 15:34:07 +0800246{
247 *((const void **) obj) = data;
248}
249
Jon Ashburn62a54d12015-05-01 18:00:33 -0600250static inline VkLayerDispatchTable *loader_get_dispatch(const VkObject obj)
Chia-I Wufe333ed2015-04-11 15:34:07 +0800251{
Jon Ashburn62a54d12015-05-01 18:00:33 -0600252 return *((VkLayerDispatchTable **) obj);
Chia-I Wufe333ed2015-04-11 15:34:07 +0800253}
254
Jon Ashburn13482452015-05-12 17:26:48 -0600255static inline VkLayerInstanceDispatchTable *loader_get_instance_dispatch(const VkObject obj)
256{
257 return *((VkLayerInstanceDispatchTable **) obj);
258}
259
Jon Ashburn62a54d12015-05-01 18:00:33 -0600260static inline void loader_init_dispatch(VkObject obj, const void *data)
Chia-I Wufe333ed2015-04-11 15:34:07 +0800261{
Jon Ashburn167b2b12015-04-15 13:34:33 -0600262#ifdef DEBUG
Chia-I Wufe333ed2015-04-11 15:34:07 +0800263 assert(valid_loader_magic_value(obj) &&
264 "Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.");
Jon Ashburn167b2b12015-04-15 13:34:33 -0600265#endif
Chia-I Wufe333ed2015-04-11 15:34:07 +0800266
Jon Ashburn62a54d12015-05-01 18:00:33 -0600267 loader_set_dispatch(obj, data);
Chia-I Wufe333ed2015-04-11 15:34:07 +0800268}
269
Jon Ashburn13482452015-05-12 17:26:48 -0600270/* global variables used across files */
271extern struct loader_struct loader;
272extern LOADER_PLATFORM_THREAD_ONCE_DEFINITION(once_icd);
273extern LOADER_PLATFORM_THREAD_ONCE_DEFINITION(once_layer);
274extern LOADER_PLATFORM_THREAD_ONCE_DEFINITION(once_exts);
275extern VkLayerInstanceDispatchTable instance_disp;
Jon Ashburn37e7f972015-04-06 10:58:22 -0600276
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600277struct loader_msg_callback_map_entry {
278 VkDbgMsgCallback icd_obj;
279 VkDbgMsgCallback loader_obj;
280};
281
282bool compare_vk_extension_properties(
283 const VkExtensionProperties* op1,
284 const VkExtensionProperties* op2);
285
Jon Ashburn13482452015-05-12 17:26:48 -0600286/* instance layer chain termination entrypoint definitions */
287VkResult loader_CreateInstance(
288 const VkInstanceCreateInfo* pCreateInfo,
289 VkInstance* pInstance);
Chia-I Wufe333ed2015-04-11 15:34:07 +0800290
Jon Ashburn13482452015-05-12 17:26:48 -0600291VkResult loader_DestroyInstance(
292 VkInstance instance);
293
294VkResult loader_EnumeratePhysicalDevices(
295 VkInstance instance,
296 uint32_t* pPhysicalDeviceCount,
297 VkPhysicalDevice* pPhysicalDevices);
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600298VkResult loader_GetPhysicalDeviceInfo(
299 VkPhysicalDevice gpu,
300 VkPhysicalDeviceInfoType infoType,
301 size_t* pDataSize,
302 void* pData);
303
304VkResult loader_CreateDevice(
305 VkPhysicalDevice gpu,
306 const VkDeviceCreateInfo* pCreateInfo,
307 VkDevice* pDevice);
Jon Ashburn13482452015-05-12 17:26:48 -0600308
Jon Ashburne8c90eb2015-05-18 15:28:32 -0600309#if 0
Jon Ashburn13482452015-05-12 17:26:48 -0600310VkResult VKAPI loader_GetGlobalExtensionInfo(
311 VkExtensionInfoType infoType,
312 uint32_t extensionIndex,
313 size_t* pDataSize,
314 void* pData);
Jon Ashburne8c90eb2015-05-18 15:28:32 -0600315#endif
Jon Ashburn13482452015-05-12 17:26:48 -0600316
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600317VkResult loader_GetPhysicalDeviceExtensionInfo(
318 VkPhysicalDevice gpu,
319 VkExtensionInfoType infoType,
320 uint32_t extensionIndex,
321 size_t* pDataSize,
322 void* pData);
323
324VkResult loader_EnumerateLayers(
325 VkPhysicalDevice gpu,
326 size_t maxStringSize,
327 size_t* pLayerCount,
328 char* const* pOutLayers,
329 void* pReserved);
330
331VkResult loader_GetMultiDeviceCompatibility(
332 VkPhysicalDevice gpu0,
333 VkPhysicalDevice gpu1,
334 VkPhysicalDeviceCompatibilityInfo* pInfo);
335
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600336/* helper function definitions */
337bool has_vk_extension_property(
338 const VkExtensionProperties *vk_ext_prop,
339 const struct loader_extension_list *ext_list);
Jon Ashburn13482452015-05-12 17:26:48 -0600340
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600341void loader_add_to_ext_list(
342 struct loader_extension_list *ext_list,
343 uint32_t prop_list_count,
344 const struct loader_extension_property *props);
Jon Ashburn13482452015-05-12 17:26:48 -0600345
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600346void loader_enable_instance_layers(struct loader_instance *inst);
Jon Ashburn13482452015-05-12 17:26:48 -0600347
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600348bool loader_is_extension_scanned(const VkExtensionProperties *ext_prop);
Jon Ashburn13482452015-05-12 17:26:48 -0600349void loader_icd_scan(void);
350void layer_lib_scan(void);
351void loader_coalesce_extensions(void);
Courtney Goeltzenleuchter67146d02015-06-10 17:39:03 -0600352
Jon Ashburnebfa25e2015-05-15 15:09:35 -0600353struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu,
354 uint32_t *gpu_index);
Jon Ashburn13482452015-05-12 17:26:48 -0600355uint32_t loader_activate_instance_layers(struct loader_instance *inst);
Chia-I Wuc1e0f962014-08-04 08:03:57 +0800356#endif /* LOADER_H */