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