xref: /aosp_15_r20/external/mesa3d/docs/vulkan/base-objs.rst (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard WorkerBase object structs
2*61046927SAndroid Build Coastguard Worker===================
3*61046927SAndroid Build Coastguard Worker
4*61046927SAndroid Build Coastguard WorkerThe Vulkan runtime code provides a set of base object structs which must be
5*61046927SAndroid Build Coastguard Workerused if you want your driver to take advantage of any of the runtime code.
6*61046927SAndroid Build Coastguard WorkerThere are other base structs for various things which are not covered here
7*61046927SAndroid Build Coastguard Workerbut those are optional.  The ones covered here are the bare minimum set
8*61046927SAndroid Build Coastguard Workerwhich form the core of the Vulkan runtime code:
9*61046927SAndroid Build Coastguard Worker
10*61046927SAndroid Build Coastguard Worker.. contents::
11*61046927SAndroid Build Coastguard Worker   :local:
12*61046927SAndroid Build Coastguard Worker
13*61046927SAndroid Build Coastguard WorkerAs one might expect, :c:struct:`vk_instance` is the required base struct
14*61046927SAndroid Build Coastguard Workerfor implementing ``VkInstance``, :c:struct:`vk_physical_device` is
15*61046927SAndroid Build Coastguard Workerrequired for ``VkPhysicalDevice``, and :c:struct:`vk_device` for
16*61046927SAndroid Build Coastguard Worker``VkDevice``.  Everything else must derive from
17*61046927SAndroid Build Coastguard Worker:c:struct:`vk_object_base` or from some struct that derives from
18*61046927SAndroid Build Coastguard Worker:c:struct:`vk_object_base`.
19*61046927SAndroid Build Coastguard Worker
20*61046927SAndroid Build Coastguard Worker
21*61046927SAndroid Build Coastguard Workervk_object_base
22*61046927SAndroid Build Coastguard Worker--------------
23*61046927SAndroid Build Coastguard Worker
24*61046927SAndroid Build Coastguard WorkerThe root base struct for all Vulkan objects is
25*61046927SAndroid Build Coastguard Worker:c:struct:`vk_object_base`.  Every object exposed to the client through
26*61046927SAndroid Build Coastguard Workerthe Vulkan API *must* inherit from :c:struct:`vk_object_base` by having a
27*61046927SAndroid Build Coastguard Worker:c:struct:`vk_object_base` or some struct that inherits from
28*61046927SAndroid Build Coastguard Worker:c:struct:`vk_object_base` as the driver struct's first member.  Even
29*61046927SAndroid Build Coastguard Workerthough we have ``container_of()`` and use it liberally, the
30*61046927SAndroid Build Coastguard Worker:c:struct:`vk_object_base` should be the first member as there are a few
31*61046927SAndroid Build Coastguard Workerplaces, particularly in the logging framework, where we use void pointers
32*61046927SAndroid Build Coastguard Workerto avoid casting and this only works if the address of the driver struct is
33*61046927SAndroid Build Coastguard Workerthe same as the address of the :c:struct:`vk_object_base`.
34*61046927SAndroid Build Coastguard Worker
35*61046927SAndroid Build Coastguard WorkerThe standard pattern for defining a Vulkan object inside a driver looks
36*61046927SAndroid Build Coastguard Workersomething like this:
37*61046927SAndroid Build Coastguard Worker
38*61046927SAndroid Build Coastguard Worker.. code-block:: c
39*61046927SAndroid Build Coastguard Worker
40*61046927SAndroid Build Coastguard Worker   struct drv_sampler {
41*61046927SAndroid Build Coastguard Worker      struct vk_object_base base;
42*61046927SAndroid Build Coastguard Worker
43*61046927SAndroid Build Coastguard Worker      /* Driver fields */
44*61046927SAndroid Build Coastguard Worker   };
45*61046927SAndroid Build Coastguard Worker
46*61046927SAndroid Build Coastguard Worker   VK_DEFINE_NONDISP_HANDLE_CASTS(drv_sampler, base, VkSampler,
47*61046927SAndroid Build Coastguard Worker                                  VK_OBJECT_TYPE_SAMPLER);
48*61046927SAndroid Build Coastguard Worker
49*61046927SAndroid Build Coastguard WorkerThen, to the object in a Vulkan entrypoint,
50*61046927SAndroid Build Coastguard Worker
51*61046927SAndroid Build Coastguard Worker.. code-block:: c
52*61046927SAndroid Build Coastguard Worker
53*61046927SAndroid Build Coastguard Worker   VKAPI_ATTR void VKAPI_CALL drv_DestroySampler(
54*61046927SAndroid Build Coastguard Worker       VkDevice                                    _device,
55*61046927SAndroid Build Coastguard Worker       VkSampler                                   _sampler,
56*61046927SAndroid Build Coastguard Worker       const VkAllocationCallbacks*                pAllocator)
57*61046927SAndroid Build Coastguard Worker   {
58*61046927SAndroid Build Coastguard Worker      VK_FROM_HANDLE(drv_device, device, _device);
59*61046927SAndroid Build Coastguard Worker      VK_FROM_HANDLE(drv_sampler, sampler, _sampler);
60*61046927SAndroid Build Coastguard Worker
61*61046927SAndroid Build Coastguard Worker      if (!sampler)
62*61046927SAndroid Build Coastguard Worker         return;
63*61046927SAndroid Build Coastguard Worker
64*61046927SAndroid Build Coastguard Worker      /* Tear down the sampler */
65*61046927SAndroid Build Coastguard Worker
66*61046927SAndroid Build Coastguard Worker      vk_object_free(&device->vk, pAllocator, sampler);
67*61046927SAndroid Build Coastguard Worker   }
68*61046927SAndroid Build Coastguard Worker
69*61046927SAndroid Build Coastguard WorkerThe :c:macro:`VK_DEFINE_NONDISP_HANDLE_CASTS()` macro defines a set of
70*61046927SAndroid Build Coastguard Workertype-safe cast functions called ``drv_sampler_from_handle()`` and
71*61046927SAndroid Build Coastguard Worker``drv_sampler_to_handle()`` which cast a :c:type:`VkSampler` to and from a
72*61046927SAndroid Build Coastguard Worker``struct drv_sampler *``.  Because compile-time type checking with Vulkan
73*61046927SAndroid Build Coastguard Workerhandle types doesn't always work in C, the ``_from_handle()`` helper uses the
74*61046927SAndroid Build Coastguard Workerprovided :c:type:`VkObjectType` to assert at runtime that the provided
75*61046927SAndroid Build Coastguard Workerhandle is the correct type of object.  Both cast helpers properly handle
76*61046927SAndroid Build Coastguard Worker``NULL`` and ``VK_NULL_HANDLE`` as inputs.  The :c:macro:`VK_FROM_HANDLE()`
77*61046927SAndroid Build Coastguard Workermacro provides a convenient way to declare a ``drv_foo`` pointer and
78*61046927SAndroid Build Coastguard Workerinitialize it from a ``VkFoo`` handle in one smooth motion.
79*61046927SAndroid Build Coastguard Worker
80*61046927SAndroid Build Coastguard Worker.. c:autostruct:: vk_object_base
81*61046927SAndroid Build Coastguard Worker   :file: src/vulkan/runtime/vk_object.h
82*61046927SAndroid Build Coastguard Worker   :members:
83*61046927SAndroid Build Coastguard Worker
84*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_object_base_init
85*61046927SAndroid Build Coastguard Worker
86*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_object_base_finish
87*61046927SAndroid Build Coastguard Worker
88*61046927SAndroid Build Coastguard Worker.. c:automacro:: VK_DEFINE_HANDLE_CASTS
89*61046927SAndroid Build Coastguard Worker
90*61046927SAndroid Build Coastguard Worker.. c:automacro:: VK_DEFINE_NONDISP_HANDLE_CASTS
91*61046927SAndroid Build Coastguard Worker
92*61046927SAndroid Build Coastguard Worker.. c:automacro:: VK_FROM_HANDLE
93*61046927SAndroid Build Coastguard Worker
94*61046927SAndroid Build Coastguard Worker
95*61046927SAndroid Build Coastguard Workervk_instance
96*61046927SAndroid Build Coastguard Worker-----------
97*61046927SAndroid Build Coastguard Worker
98*61046927SAndroid Build Coastguard Worker.. c:autostruct:: vk_instance
99*61046927SAndroid Build Coastguard Worker   :file: src/vulkan/runtime/vk_instance.h
100*61046927SAndroid Build Coastguard Worker   :members:
101*61046927SAndroid Build Coastguard Worker
102*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_instance_init
103*61046927SAndroid Build Coastguard Worker
104*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_instance_finish
105*61046927SAndroid Build Coastguard Worker
106*61046927SAndroid Build Coastguard WorkerOnce a driver has a :c:struct:`vk_instance`, implementing all the various
107*61046927SAndroid Build Coastguard Workerinstance-level ``vkGet*ProcAddr()`` entrypoints is trivial:
108*61046927SAndroid Build Coastguard Worker
109*61046927SAndroid Build Coastguard Worker.. code-block:: c
110*61046927SAndroid Build Coastguard Worker
111*61046927SAndroid Build Coastguard Worker   VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
112*61046927SAndroid Build Coastguard Worker   drv_GetInstanceProcAddr(VkInstance _instance,
113*61046927SAndroid Build Coastguard Worker                           const char *pName)
114*61046927SAndroid Build Coastguard Worker   {
115*61046927SAndroid Build Coastguard Worker      VK_FROM_HANDLE(vk_instance, instance, _instance);
116*61046927SAndroid Build Coastguard Worker      return vk_instance_get_proc_addr(instance,
117*61046927SAndroid Build Coastguard Worker                                       &drv_instance_entrypoints,
118*61046927SAndroid Build Coastguard Worker                                       pName);
119*61046927SAndroid Build Coastguard Worker   }
120*61046927SAndroid Build Coastguard Worker
121*61046927SAndroid Build Coastguard Worker   PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
122*61046927SAndroid Build Coastguard Worker   vk_icdGetInstanceProcAddr(VkInstance instance,
123*61046927SAndroid Build Coastguard Worker                             const char *pName)
124*61046927SAndroid Build Coastguard Worker   {
125*61046927SAndroid Build Coastguard Worker      return drv_GetInstanceProcAddr(instance, pName);
126*61046927SAndroid Build Coastguard Worker   }
127*61046927SAndroid Build Coastguard Worker
128*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_instance_get_proc_addr
129*61046927SAndroid Build Coastguard Worker
130*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_instance_get_proc_addr_unchecked
131*61046927SAndroid Build Coastguard Worker
132*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_instance_get_physical_device_proc_addr
133*61046927SAndroid Build Coastguard Worker
134*61046927SAndroid Build Coastguard WorkerWe also provide an implementation of
135*61046927SAndroid Build Coastguard Worker``vkEnumerateInstanceExtensionProperties()`` which can be used similarly:
136*61046927SAndroid Build Coastguard Worker
137*61046927SAndroid Build Coastguard Worker.. code-block:: c
138*61046927SAndroid Build Coastguard Worker
139*61046927SAndroid Build Coastguard Worker   VKAPI_ATTR VkResult VKAPI_CALL
140*61046927SAndroid Build Coastguard Worker   drv_EnumerateInstanceExtensionProperties(const char *pLayerName,
141*61046927SAndroid Build Coastguard Worker                                            uint32_t *pPropertyCount,
142*61046927SAndroid Build Coastguard Worker                                            VkExtensionProperties *pProperties)
143*61046927SAndroid Build Coastguard Worker   {
144*61046927SAndroid Build Coastguard Worker      if (pLayerName)
145*61046927SAndroid Build Coastguard Worker         return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
146*61046927SAndroid Build Coastguard Worker
147*61046927SAndroid Build Coastguard Worker      return vk_enumerate_instance_extension_properties(
148*61046927SAndroid Build Coastguard Worker         &instance_extensions, pPropertyCount, pProperties);
149*61046927SAndroid Build Coastguard Worker   }
150*61046927SAndroid Build Coastguard Worker
151*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_enumerate_instance_extension_properties
152*61046927SAndroid Build Coastguard Worker
153*61046927SAndroid Build Coastguard Workervk_physical_device
154*61046927SAndroid Build Coastguard Worker------------------
155*61046927SAndroid Build Coastguard Worker
156*61046927SAndroid Build Coastguard Worker.. c:autostruct:: vk_physical_device
157*61046927SAndroid Build Coastguard Worker   :file: src/vulkan/runtime/vk_physical_device.h
158*61046927SAndroid Build Coastguard Worker   :members:
159*61046927SAndroid Build Coastguard Worker
160*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_physical_device_init
161*61046927SAndroid Build Coastguard Worker
162*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_physical_device_finish
163*61046927SAndroid Build Coastguard Worker
164*61046927SAndroid Build Coastguard Workervk_device
165*61046927SAndroid Build Coastguard Worker------------------
166*61046927SAndroid Build Coastguard Worker
167*61046927SAndroid Build Coastguard Worker.. c:autostruct:: vk_device
168*61046927SAndroid Build Coastguard Worker   :file: src/vulkan/runtime/vk_device.h
169*61046927SAndroid Build Coastguard Worker   :members:
170*61046927SAndroid Build Coastguard Worker
171*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_device_init
172*61046927SAndroid Build Coastguard Worker
173*61046927SAndroid Build Coastguard Worker.. c:autofunction:: vk_device_finish
174