1 /*
2  * Copyright © 2021 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #ifndef VK_INSTANCE_H
24 #define VK_INSTANCE_H
25 
26 #include "vk_dispatch_table.h"
27 #include "vk_extensions.h"
28 #include "vk_object.h"
29 
30 #include "c11/threads.h"
31 #include "util/list.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct vk_app_info {
38    /** VkApplicationInfo::pApplicationName */
39    const char*        app_name;
40 
41    /** VkApplicationInfo::applicationVersion */
42    uint32_t           app_version;
43 
44    /** VkApplicationInfo::pEngineName */
45    const char*        engine_name;
46 
47    /** VkApplicationInfo::engineVersion */
48    uint32_t           engine_version;
49 
50    /** VkApplicationInfo::apiVersion or `VK_API_VERSION_1_0`
51     *
52     * If the application does not provide a `pApplicationInfo` or the
53     * `apiVersion` field is 0, this is set to `VK_API_VERSION_1_0`.
54     */
55    uint32_t           api_version;
56 };
57 
58 /** Base struct for all `VkInstance` implementations
59  *
60  * This contains data structures necessary for detecting enabled extensions,
61  * handling entrypoint dispatch, and implementing `vkGetInstanceProcAddr()`.
62  * It also contains data copied from the `VkInstanceCreateInfo` such as the
63  * application information.
64  */
65 struct vk_instance {
66    struct vk_object_base base;
67 
68    /** Allocator used when creating this instance
69     *
70     * This is used as a fall-back for when a NULL pAllocator is passed into a
71     * device-level create function such as vkCreateImage().
72     */
73    VkAllocationCallbacks alloc;
74 
75    /** VkInstanceCreateInfo::pApplicationInfo */
76    struct vk_app_info app_info;
77 
78    /** Table of all enabled instance extensions
79     *
80     * This is generated automatically as part of `vk_instance_init()` from
81     * VkInstanceCreateInfo::ppEnabledExtensionNames.
82     */
83    struct vk_instance_extension_table enabled_extensions;
84 
85    /** Instance-level dispatch table */
86    struct vk_instance_dispatch_table dispatch_table;
87 
88    /* VK_EXT_debug_report debug callbacks */
89    struct {
90       mtx_t callbacks_mutex;
91       struct list_head callbacks;
92    } debug_report;
93 
94    /* VK_EXT_debug_utils */
95    struct {
96       /* These callbacks are only used while creating or destroying an
97        * instance
98        */
99       struct list_head instance_callbacks;
100       mtx_t callbacks_mutex;
101       /* Persistent callbacks */
102       struct list_head callbacks;
103    } debug_utils;
104 };
105 
106 VK_DEFINE_HANDLE_CASTS(vk_instance, base, VkInstance,
107                        VK_OBJECT_TYPE_INSTANCE);
108 
109 /** Initialize a vk_instance
110  *
111  * Along with initializing the data structures in `vk_instance`, this function
112  * validates the Vulkan version number provided by the client and checks that
113  * every extension specified by
114  * `VkInstanceCreateInfo::ppEnabledExtensionNames` is actually supported by
115  * the implementation and returns `VK_ERROR_EXTENSION_NOT_PRESENT` if an
116  * unsupported extension is requested.
117  *
118  * @param[out] instance             The instance to initialize
119  * @param[in]  supported_extensions Table of all instance extensions supported
120  *                                  by this instance
121  * @param[in]  dispatch_table       Instance-level dispatch table
122  * @param[in]  pCreateInfo          VkInstanceCreateInfo pointer passed to
123  *                                  `vkCreateInstance()`
124  * @param[in]  alloc                Allocation callbacks used to create this
125  *                                  instance; must not be `NULL`
126  */
127 VkResult MUST_CHECK
128 vk_instance_init(struct vk_instance *instance,
129                  const struct vk_instance_extension_table *supported_extensions,
130                  const struct vk_instance_dispatch_table *dispatch_table,
131                  const VkInstanceCreateInfo *pCreateInfo,
132                  const VkAllocationCallbacks *alloc);
133 
134 /** Tears down a vk_instance
135  *
136  * @param[out] instance             The instance to tear down
137  */
138 void
139 vk_instance_finish(struct vk_instance *instance);
140 
141 /** Implementaiton of vkEnumerateInstanceExtensionProperties() */
142 VkResult
143 vk_enumerate_instance_extension_properties(
144     const struct vk_instance_extension_table *supported_extensions,
145     uint32_t *pPropertyCount,
146     VkExtensionProperties *pProperties);
147 
148 /** Implementaiton of vkGetInstanceProcAddr() */
149 PFN_vkVoidFunction
150 vk_instance_get_proc_addr(const struct vk_instance *instance,
151                           const struct vk_instance_entrypoint_table *entrypoints,
152                           const char *name);
153 
154 /** Unchecked version of vk_instance_get_proc_addr
155  *
156  * This is identical to `vk_instance_get_proc_addr()` except that it doesn't
157  * check whether extensions are enabled before returning function pointers.
158  * This is useful in window-system code where we may use extensions without
159  * the client explicitly enabling them.
160  */
161 PFN_vkVoidFunction
162 vk_instance_get_proc_addr_unchecked(const struct vk_instance *instance,
163                                     const char *name);
164 
165 /** Implementaiton of vk_icdGetPhysicalDeviceProcAddr() */
166 PFN_vkVoidFunction
167 vk_instance_get_physical_device_proc_addr(const struct vk_instance *instance,
168                                           const char *name);
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif /* VK_INSTANCE_H */
175