blob: 4a986cd57464d2b14c3fbf3a644a23a6f9f8d7be [file] [log] [blame] [view]
Jon Ashburnc2972682016-02-08 15:42:01 -07001# Vulkan Loader Specification and Architecture Overview
2
3
4Goals of this document
5----------------------
6
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007Specify necessary functions and expected behavior of interface between the
8loader library and ICDs and layers for Windows, Linux and Android based
9systems. Also describe the application visible behaviors of the loader.
Jon Ashburnc2972682016-02-08 15:42:01 -070010
11Audience
12--------
13
14Application, Vulkan driver and Vulkan layer developers.
15
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070016Any developers interested in understanding more about loader and layer behavior
17and architecture.
Jon Ashburnc2972682016-02-08 15:42:01 -070018
19
20Loader goals
21------------
22
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070023- Support multiple ICDs (Installable Client Drivers) to co-exist on a system
24without interfering with each other.
Jon Ashburnc2972682016-02-08 15:42:01 -070025
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070026- Support optional modules (layers) that can be enabled by an application,
27developer or the system and have no impact when not enabled.
Jon Ashburnc2972682016-02-08 15:42:01 -070028
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070029- Negligible performance cost for an application calling through the loader
30to an ICD entry point.
Jon Ashburnc2972682016-02-08 15:42:01 -070031
32Architectural overview of layers and loader
33-------------------------------------------
34
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -070035Vulkan is a layered architecture. Layers can hook (intercept) Vulkan commands to
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070036achieve various functionality that a Vulkan driver (aka ICD) or loader doesn’t
37support. Functionality such as Vulkan API tracing and debugging, API usage
38validation, and other tools such as framebuffer overlays are all natural
39candidates for Vulkan layers. Layers are implemented as libraries that are
40inserted between the application and the driver.
Jon Ashburnc2972682016-02-08 15:42:01 -070041
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070042Not only is Vulkan a layered architecture but it also supports multiple GPUs
43and their drivers. Vulkan commands called by an application may wind up calling
44into a diverse set of modules: loader, layers, and ICDs. The loader is critical
45to managing the proper dispatching of Vulkan commands to the appropriate set of
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -070046layers and ICDs. The Vulkan object model allows the loader to insert layers
47into a call chain so the layers can process Vulkan commands prior to the
48ICD being called.
Jon Ashburnc2972682016-02-08 15:42:01 -070049
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070050Vulkan uses an object model to control the scope of a particular action /
51operation. The object to be acted on is always the first parameter of a Vulkan
52call and is a dispatchable object (see Vulkan specification section 2.2 Object
53Model). Under the covers, the dispatchable object handle is a pointer to a
54structure that contains a pointer to a dispatch table maintained by the loader.
55This dispatch table contains pointers to the Vulkan functions appropriate to
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -070056that object. There are two types of dispatch tables the loader maintains,
57Instance and Device. I.e. a VkInstance object’s dispatch table will point to Vulkan
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070058functions such as vkEnumeratePhysicalDevices, vkDestroyInstance,
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -070059vkCreateInstance, etc. Instance functions take a VkInstance or VkPhysicalDevice as
60their first argument.
Jon Ashburnc2972682016-02-08 15:42:01 -070061
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070062Device objects have a separate dispatch table containing the appropriate
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -070063function pointers. The device dispatch table is used for all functions that
64take a VkDevice, VkQueue or VkCommandBuffer as their first argument.
Jon Ashburnc2972682016-02-08 15:42:01 -070065
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070066These instance and device dispatch tables are constructed when the application
67calls vkCreateInstance and vkCreateDevice. At that time the application and/or
68system can specify optional layers to be included. The loader will initialize
69the specified layers to create a call chain for each Vulkan function and each
70entry of the dispatch table will point to the first element of that chain.
71Thus, the loader builds an instance call chain for each VkInstance that is
72created and a device call chain for each VkDevice that is created.
Jon Ashburnc2972682016-02-08 15:42:01 -070073
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070074For example, the diagram below represents what happens in the call chain for
75vkCreateInstance. After initializing the chain, the loader will call into the
76first layer’s vkCreateInstance which will call the next finally terminating in
77the loader again where this function calls every ICD’s vkCreateInstance and
78saves the results. This allows every enabled layer for this chain to set up
79what it needs based on the VkInstanceCreateInfo structure from the application.
Jon Ashburnc2505562016-02-15 10:19:26 -070080![Instance call chain](instance_call_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -070081
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070082This also highlights some of the complexity the loader must manage when using
83instance chains. As shown here, the loader must aggregate information from
84multiple devices when they are present. This means that the loader has to know
85about instance level extensions to aggregate them correctly.
Jon Ashburnc2972682016-02-08 15:42:01 -070086
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070087Device chains are created at vkCreateDevice and are generally simpler because
88they deal with only a single device and the ICD can always be the terminator of
89the chain. The below diagram also illustrates how layers (either device or
90instance) can skip intercepting any given Vulkan entry point.
Jon Ashburnc2505562016-02-15 10:19:26 -070091![Chain skipping layers](chain_skipping_layers.png)
Jon Ashburnc2972682016-02-08 15:42:01 -070092
93Application interface to loader
94-------------------------------
95
96In this section we’ll discuss how an application interacts with the loader.
97
98- Linking to loader library for core and WSI extension symbols.
99
100- Dynamic Vulkan command lookup & application dispatch table.
101
102- Loader library filenames for linking to different Vulkan ABI versions.
103
104- Layers
105
106- Extensions
107
108- vkGetInstanceProcAddr, vkGetDeviceProcAddr
109
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700110The loader library on Windows, Linux and Android will export all core Vulkan
111and all appropriate Window System Interface (WSI) extensions. This is done to
112make it simpler to get started with Vulkan development. When an application
113links directly to the loader library in this way, the Vulkan calls are simple
114trampoline functions that jump to the appropriate dispatch table entry for the
115object they are given.
Jon Ashburnc2972682016-02-08 15:42:01 -0700116
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700117Applications are not required to link directly to the loader library, instead
118they can use the appropriate platform specific dynamic symbol lookup on the
119loader library to initialize the application’s own dispatch table. This allows
Jeff Julianof1619872016-02-17 17:25:42 -0500120an application to fail gracefully if the loader cannot be found, and it
121provides the fastest mechanism for the application to call Vulkan functions. An
122application will only need to query (via system calls such as dlsym()) the
123address of vkGetInstanceProcAddr from the loader library. Using
124vkGetInstanceProcAddr the application can then discover the address of all
125instance and global functions and extensions, such as vkCreateInstance,
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700126vkEnumerateInstanceExtensionProperties and vkEnumerateInstanceLayerProperties
127in a platform independent way.
Jon Ashburnc2972682016-02-08 15:42:01 -0700128
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700129The Vulkan loader library will be distributed in various ways including Vulkan
130SDKs, OS package distributions and IHV driver packages. These details are
131beyond the scope of this document. However, the name and versioning of the
132Vulkan loader library is specified so an app can link to the correct Vulkan ABI
133library version. Vulkan versioning is such that ABI backwards compatibility is
Jeff Julianof1619872016-02-17 17:25:42 -0500134guaranteed for all versions with the same major number (e.g. 1.0 and 1.1). On
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700135Windows, the loader library encodes the ABI version in its name such that
136multiple ABI incompatible versions of the loader can peacefully coexist on a
Jeff Julianof1619872016-02-17 17:25:42 -0500137given system. The Vulkan loader library file name is “vulkan-<ABI
138version>.dll”. For example, for Vulkan version 1.X on Windows the library
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700139filename is vulkan-1.dll. And this library file can typically be found in the
140windows/system32 directory.
Jon Ashburnc2972682016-02-08 15:42:01 -0700141
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700142For Linux, shared libraries are versioned based on a suffix. Thus, the ABI
143number is not encoded in the base of the library filename as on Windows. On
144Linux an application wanting to link to the latest Vulkan ABI version would
145just link to the name vulkan (libvulkan.so). A specific Vulkan ABI version can
Jeff Julianof1619872016-02-17 17:25:42 -0500146also be linked to by applications (e.g. libvulkan.so.1).
Jon Ashburnc2972682016-02-08 15:42:01 -0700147
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700148Applications desiring Vulkan functionality beyond what the core API offers may
149use various layers or extensions. A layer cannot add new or modify existing
150Vulkan commands, but may offer extensions that do. A common use of layers is
151for API validation. A developer can use validation layers during application
152development, but during production the layers can be disabled by the
Jeff Julianof1619872016-02-17 17:25:42 -0500153application. Thus, eliminating the overhead of validating the application's
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600154usage of the API. Layers discovered by the loader are reported to the
155application via vkEnumerateInstanceLayerProperties.
156Layers are enabled at vkCreateInstance and are active for all Vulkan commands
157that using the given VkIstance and any of it's child objects.
158For example, the ppEnabledLayerNames array in the
159VkInstanceCreateInfo structure is used by the application to list the
160layer names to be enabled at vkCreateInstance. At vkCreateInstance and
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700161vkCreateDevice, the loader will construct call chains that include the
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600162application specified (enabled) layers. vkCreateDevice will use the layers
163specified at vkCreateInstance. vkEnumerateDeviceLayerProperties and
164device layers are deprecated. Order is important in the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700165ppEnabledLayerNames array; array element 0 is the topmost (closest to the
166application) layer inserted in the chain and the last array element is closest
167to the driver.
Jon Ashburnc2972682016-02-08 15:42:01 -0700168
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700169Developers may want to enable layers that are not enabled by the given
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600170application they are using. On Linux and Windows, the environment variable
171“VK\_INSTANCE\_LAYERS” can be used to enable
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700172additional layers which are not specified (enabled) by the application at
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600173vkCreateInstance. VK\_INSTANCE\_LAYERS is a colon
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700174(Linux)/semi-colon (Windows) separated list of layer names to enable. Order is
175relevant with the first layer in the list being the topmost layer (closest to
176the application) and the last layer in the list being the bottommost layer
177(closest to the driver).
Jon Ashburnc2972682016-02-08 15:42:01 -0700178
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700179Application specified layers and user specified layers (via environment
180variables) are aggregated and duplicates removed by the loader when enabling
181layers. Layers specified via environment variable are topmost (closest to the
182application) while layers specified by the application are bottommost.
Jon Ashburnc2972682016-02-08 15:42:01 -0700183
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700184An example of using these environment variables to activate the validation
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600185layer VK\_LAYER\_LUNARG\_parameter\_validation on Windows or Linux is as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700186
187```
Mark Lobodzinski739391a2016-03-17 15:08:18 -0600188> $ export VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_parameter_validation
Jon Ashburnc2972682016-02-08 15:42:01 -0700189
Jon Ashburnc2972682016-02-08 15:42:01 -0700190```
191
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700192Some platforms, including Linux and Windows, support layers which are enabled
193automatically by the loader rather than explicitly by the application (or via
194environment variable). Explicit layers are those layers enabled by the
195application (or environment variable) by providing the layer name. Implicit
196layers are those layers enabled by the loader automatically. Any implicit
197layers the loader discovers on the system in the appropriate location will be
198enabled (subject to environment variable overrides described later). Discovery
199of properly installed implicit and explicit layers is described later.
200Explicitly enabling a layer that is implicitly enabled has no additional
201effect: the layer will still be enabled implicitly by the loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700202
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700203Extensions are optional functionality provided by a layer, the loader or an
204ICD. Extensions can modify the behavior of the Vulkan API and need to be
205specified and registered with Khronos.
Jon Ashburnc2972682016-02-08 15:42:01 -0700206
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700207Instance extensions can be discovered via
208vkEnumerateInstanceExtensionProperties. Device extensions can be discovered via
209vkEnumerateDeviceExtensionProperties. The loader discovers and aggregates all
210extensions from layers (both explicit and implicit), ICDs and the loader before
211reporting them to the application in vkEnumerate\*ExtensionProperties. The
Jeff Julianof1619872016-02-17 17:25:42 -0500212pLayerName parameter in these functions is used to select either a single layer
213or the Vulkan platform implementation. If pLayerName is NULL, extensions from
214Vulkan implementation components (including loader, implicit layers, and ICDs)
215are enumerated. If pLayerName is equal to a discovered layer module name then
216any extensions from that layer (which may be implicit or explicit) are
217enumerated. Duplicate extensions (e.g. an implicit layer and ICD might report
Jon Ashburn859c7fb2016-03-02 17:26:31 -0700218support for the same extension) are eliminated by the loader. For duplicates, the
219ICD version is reported and the layer version is culled. Extensions must
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700220be enabled (in vkCreateInstance or vkCreateDevice) before they can be used.
Jon Ashburnc2972682016-02-08 15:42:01 -0700221
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700222Extension command entry points should be queried via vkGetInstanceProcAddr or
223vkGetDeviceProcAddr. vkGetDeviceProcAddr can only be used to query for device
224extension or core device entry points. Device entry points include any command
225that uses a VkDevice as the first parameter or a dispatchable object that is a
226child of a VkDevice (currently this includes VkQueue and VkCommandBuffer).
227vkGetInstanceProcAddr can be used to query either device or instance extension
228entry points in addition to all core entry points.
Jon Ashburnc2972682016-02-08 15:42:01 -0700229
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700230VkGetDeviceProcAddr is particularly interesting because it will provide the
231most efficient way to call into the ICD. For example, the diagram below shows
232what could happen if the application were to use vkGetDeviceProcAddr for the
233function “vkGetDeviceQueue” and “vkDestroyDevice” but not “vkAllocateMemory”.
234The resulting function pointer (fpGetDeviceQueue) would be the ICD’s entry
235point if the loader and any enabled layers do not need to see that call. Even
Jeff Julianof1619872016-02-17 17:25:42 -0500236if an enabled layer intercepts the call (e.g. vkDestroyDevice) the loader
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700237trampoline code is skipped for function pointers obtained via
238vkGetDeviceProcAddr. This also means that function pointers obtained via
239vkGetDeviceProcAddr will only work with the specific VkDevice it was created
240for, using it with another device has undefined results. For extensions,
241Get\*ProcAddr will often be the only way to access extension API features.
Jon Ashburnc2972682016-02-08 15:42:01 -0700242
Jon Ashburnc2505562016-02-15 10:19:26 -0700243![Get*ProcAddr efficiency](get_proc_addr.png)
Courtney Goeltzenleuchterab3a4662016-02-14 10:48:22 -0700244
Jon Ashburnc2972682016-02-08 15:42:01 -0700245
246Vulkan Installable Client Driver interface with the loader
247----------------------------------------------------------
248
249### ICD discovery
250
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700251Vulkan allows multiple drivers each with one or more devices (represented by a
252Vulkan VkPhysicalDevice object) to be used collectively. The loader is
253responsible for discovering available Vulkan ICDs on the system. Given a list
254of available ICDs, the loader can enumerate all the physical devices available
255for an application and return this information to the application. The process
256in which the loader discovers the available Installable Client Drivers (ICDs)
257on a system is platform dependent. Windows, Linux and Android ICD discovery
258details are listed below.
Jon Ashburnc2972682016-02-08 15:42:01 -0700259
260#### Windows
261
262##### Properly-Installed ICDs
263
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700264In order to find properly-installed ICDs, the Vulkan loader will scan the
265values in the following Windows registry key:
Jon Ashburnc2972682016-02-08 15:42:01 -0700266
267HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\Drivers
268
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700269For each value in this key which has DWORD data set to 0, the loader opens the
270JSON format text information file (a.k.a. "manifest file") specified by the
271name of the value. Each name must be a full pathname to the text manifest file.
272The Vulkan loader will open each manifest file to obtain the name or pathname
273of an ICD shared library (".dll") file. For example:
Jon Ashburnc2972682016-02-08 15:42:01 -0700274
Jon Ashburncc300a22016-02-11 14:57:30 -0700275 ```
276 {
Jon Ashburnc2972682016-02-08 15:42:01 -0700277 "file_format_version": "1.0.0",
278 "ICD": {
279 "library_path": "path to ICD library",
Tony Barbourd83f06c2016-03-08 14:50:03 -0700280 "api_version": "1.0.5"
Jon Ashburnc2972682016-02-08 15:42:01 -0700281 }
282 }
Jon Ashburncc300a22016-02-11 14:57:30 -0700283 ```
Jon Ashburnc2972682016-02-08 15:42:01 -0700284
285
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700286The "library\_path" specifies either a filename, a relative pathname, or a full
287pathname to an ICD shared library file, which the loader will attempt to load
288using LoadLibrary(). If the ICD is specified via a filename, the shared library
David Pinedo3e163ee2016-04-18 16:59:59 -0600289lives in the system's DLL search path (e.g. in the "C:\Windows\System32"
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700290folder). If the ICD is specified via a relative pathname, it is relative to the
291path of the manifest file. Relative pathnames are those that do not start with
292a drive specifier (e.g. "C:"), nor with a directory separator (i.e. the '\\'
293character), but do contain at least one directory separator.
Jon Ashburnc2972682016-02-08 15:42:01 -0700294
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700295The "file\_format\_version" specifies a major.minor.patch version number in
296case the format of the text information file changes in the future. If the same
297ICD shared library supports multiple, incompatible versions of text manifest
298file format versions, it must have multiple text info files (all of which may
299point to the same shared library).
Jon Ashburnc2972682016-02-08 15:42:01 -0700300
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700301The “api\_version” specifies the major.minor.patch version number of the Vulkan
302API that the shared library (referenced by "library\_path") was built with.
Jon Ashburnc2972682016-02-08 15:42:01 -0700303
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700304There are no rules about the name of the text information files (except the
305.json suffix).
Jon Ashburnc2972682016-02-08 15:42:01 -0700306
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700307There are no rules about the name of the ICD shared library files. For example,
308if the registry contains the following values,
Jon Ashburnc2972682016-02-08 15:42:01 -0700309
Jon Ashburncc300a22016-02-11 14:57:30 -0700310```
311[HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers\]
Jon Ashburnc2972682016-02-08 15:42:01 -0700312
David Pinedo3e163ee2016-04-18 16:59:59 -0600313"C:\vendor a\vk_vendora.json"=dword:00000000
Jon Ashburnc2972682016-02-08 15:42:01 -0700314
David Pinedo3e163ee2016-04-18 16:59:59 -0600315"C:\windows\system32\vendorb_vk.json"=dword:00000000
Jon Ashburnc2972682016-02-08 15:42:01 -0700316
David Pinedo3e163ee2016-04-18 16:59:59 -0600317"C:\windows\system32\vendorc_icd.json"=dword:00000000
Jon Ashburncc300a22016-02-11 14:57:30 -0700318```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700319then the loader will open the following text information files, with the
320specified contents:
Jon Ashburnc2972682016-02-08 15:42:01 -0700321
Jon Ashburncc300a22016-02-11 14:57:30 -0700322| Text File Name | Text File Contents |
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700323|----------------|--------------------|
David Pinedo3e163ee2016-04-18 16:59:59 -0600324|vk\_vendora.json | "ICD": { "library\_path": "C:\VENDOR A\vk_vendora.dll", "api_version": "1.0.5" } |
Tony Barbourd83f06c2016-03-08 14:50:03 -0700325| vendorb\_vk.json | "ICD": { "library\_path": "vendorb\_vk.dll", "api_version": "1.0.5" } |
326|vendorc\_icd.json | "ICD": { "library\_path": "vedorc\_icd.dll", "api_version": "1.0.5" }|
Jon Ashburnc2972682016-02-08 15:42:01 -0700327
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700328Then the loader will open the three files mentioned in the "Text File Contents"
329column, and then try to load and use the three shared libraries indicated by
330the ICD.library\_path value.
Jon Ashburnc2972682016-02-08 15:42:01 -0700331
332##### Using Pre-Production ICDs
333
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700334IHV developers (and sometimes other developers) need to use special,
335pre-production ICDs. In some cases, a pre-production ICD may be in an
336installable package. In other cases, a pre-production ICD may simply be a
337shared library in the developer's build tree. In this latter case, we want to
338allow developers to point to such an ICD without modifying the
339properly-installed ICD(s) on their system.
Jon Ashburnc2972682016-02-08 15:42:01 -0700340
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700341This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
342which will override the mechanism used for finding properly-installed ICDs. In
343other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used. The
344"VK\_ICD\_FILENAMES" environment variable is a semi-colon-separated list of ICD
345text information files (aka manifest files), containing the following:
Jon Ashburnc2972682016-02-08 15:42:01 -0700346
Jon Ashburncc300a22016-02-11 14:57:30 -0700347- A full pathname (e.g. "C:\\my\_build\\my\_icd.json")
Jon Ashburnc2972682016-02-08 15:42:01 -0700348
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700349Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
350file for a developer-built ICD. A semi-colon is only used if more than one ICD
351is listed.
Jon Ashburnc2972682016-02-08 15:42:01 -0700352
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700353For example, if a developer wants to refer to one ICD that they built, they
354could set the "VK\_ICD\_FILENAMES" environment variable to:
Jon Ashburnc2972682016-02-08 15:42:01 -0700355
Jon Ashburncc300a22016-02-11 14:57:30 -0700356C:\\my\_build\\my\_icd.json
Jon Ashburnc2972682016-02-08 15:42:01 -0700357
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700358If a developer wants to refer to two ICDs, one of which is a properly-installed
359ICD, they can use the full pathname of the text file:
Jon Ashburnc2972682016-02-08 15:42:01 -0700360
Jon Ashburncc300a22016-02-11 14:57:30 -0700361C:\\Windows\\System32\\vendorc\_icd.json;C:\\my\_build\\my\_icd.json
Jon Ashburnc2972682016-02-08 15:42:01 -0700362
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700363Notice the semi-colon between "C:\\Windows\\System32\\vendorc\_icd.json" and
364"C:\\my\_build\\my\_icd.json".
Jon Ashburnc2972682016-02-08 15:42:01 -0700365
366#### Linux
367
368##### Properly-Installed ICDs
369
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700370In order to find properly-installed ICDs, the Vulkan loader will scan the files
371in the following Linux directories:
Jon Ashburnc2972682016-02-08 15:42:01 -0700372
373/usr/share/vulkan/icd.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700374/etc/vulkan/icd.d
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700375$HOME/.local/share/vulkan/icd.d
376
377Where $HOME is the current home directory of the application's user id; this
378path will be ignored for suid programs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700379
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700380These directories will contain text information files (a.k.a. "manifest
381files"), that use a JSON format.
Jon Ashburnc2972682016-02-08 15:42:01 -0700382
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700383The Vulkan loader will open each manifest file found to obtain the name or
384pathname of an ICD shared library (".so") file. For example:
Jon Ashburnc2972682016-02-08 15:42:01 -0700385
Jon Ashburncc300a22016-02-11 14:57:30 -0700386```
387{
Jon Ashburnc2972682016-02-08 15:42:01 -0700388 "file_format_version": "1.0.0",
389 "ICD": {
390 "library_path": "path to ICD library",
Tony Barbourd83f06c2016-03-08 14:50:03 -0700391 "api_version": "1.0.5"
Jon Ashburnc2972682016-02-08 15:42:01 -0700392 }
393}
Jon Ashburncc300a22016-02-11 14:57:30 -0700394```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700395The "library\_path" specifies either a filename, a relative pathname, or a full
396pathname to an ICD shared library file. If the ICD is specified via a filename,
397the loader will attempt to open that file as a shared object using dlopen(),
398and the file must be in a directory that dlopen is configured to look in (Note:
399various distributions are configured differently). A distribution is free to
400create Vulkan-specific system directories (e.g. ".../vulkan/icd"), but is not
401required to do so. If the ICD is specified via a relative pathname, it is
402relative to the path of the info file. Relative pathnames are those that do not
403start with, but do contain at least one directory separator (i.e. the '/'
404character). For example, "lib/vendora.so" and "./vendora.so" are examples of
405relative pathnames.
Jon Ashburnc2972682016-02-08 15:42:01 -0700406
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700407The "file\_format\_version" provides a major.minor.patch version number in case
408the format of the manifest file changes in the future. If the same ICD shared
409library supports multiple, incompatible versions of manifest file format
410versions, it must have multiple manifest files (all of which may point to the
411same shared library).
Jon Ashburnc2972682016-02-08 15:42:01 -0700412
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700413The “api\_version” specifies the major.minor.patch version number of the Vulkan
414API that the shared library (referenced by "library\_path") was built with.
Jon Ashburnc2972682016-02-08 15:42:01 -0700415
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700416The "/usr/share/vulkan/icd.d" directory is for ICDs that are installed from
417Linux-distribution-provided packages. The "/etc/vulkan/icd.d" directory is for
418ICDs that are installed from non-Linux-distribution-provided packages.
Jon Ashburnc2972682016-02-08 15:42:01 -0700419
420There are no rules about the name of the text files (except the .json suffix).
421
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700422There are no rules about the name of the ICD shared library files. For example,
423if the "/usr/share/vulkan/icd.d" directory contain the following files, with
424the specified contents:
Jon Ashburnc2972682016-02-08 15:42:01 -0700425
Jon Ashburn26ed3f32016-02-14 21:54:52 -0700426| Text File Name | Text File Contents |
427|-------------------|------------------------|
Tony Barbourd83f06c2016-03-08 14:50:03 -0700428| vk\_vendora.json | "ICD": { "library\_path": "vendora.so", "api_version": "1.0.5" } |
429| vendorb\_vk.json | "ICD": { "library\_path": "vendorb\_vulkan\_icd.so", "api_version": "1.0.5" } |
430| vendorc\_icd.json | "ICD": { "library\_path": "/usr/lib/VENDORC/icd.so", "api_version": "1.0.5" } |
Jon Ashburnc2972682016-02-08 15:42:01 -0700431
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700432then the loader will open the three files mentioned in the "Text File Contents"
433column, and then try to load and use the three shared libraries indicated by
434the ICD.library\_path value.
Jon Ashburnc2972682016-02-08 15:42:01 -0700435
436##### Using Pre-Production ICDs
437
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700438IHV developers (and sometimes other developers) need to use special,
439pre-production ICDs. In some cases, a pre-production ICD may be in an
440installable package. In other cases, a pre-production ICD may simply be a
441shared library in the developer's build tree. In this latter case, we want to
442allow developers to point to such an ICD without modifying the
443properly-installed ICD(s) on their system.
Jon Ashburnc2972682016-02-08 15:42:01 -0700444
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700445This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
446which will override the mechanism used for finding properly-installed ICDs. In
447other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used.
Jon Ashburnc2972682016-02-08 15:42:01 -0700448
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700449The "VK\_ICD\_FILENAMES" environment variable is a colon-separated list of ICD
450manifest files, containing the following:
Jon Ashburnc2972682016-02-08 15:42:01 -0700451
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700452- A filename (e.g. "libvkicd.json") in the "/usr/share/vulkan/icd.d", "/etc/vulkan/icd.d" "$HOME/.local/share/vulkan/icd.d" directories
Jon Ashburnc2972682016-02-08 15:42:01 -0700453
454- A full pathname (e.g. "/my\_build/my\_icd.json")
455
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700456Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
457file for a developer-built ICD. A colon is only used if more than one ICD is
458listed.
Jon Ashburnc2972682016-02-08 15:42:01 -0700459
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700460For example, if a developer wants to refer to one ICD that they built, they
461could set the "VK\_ICD\_FILENAMES" environment variable to:
Jon Ashburnc2972682016-02-08 15:42:01 -0700462
463/my\_build/my\_icd.json
464
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700465If a developer wants to refer to two ICDs, one of which is a properly-installed
466ICD, they can use the name of the text file in the system directory:
Jon Ashburnc2972682016-02-08 15:42:01 -0700467
468vendorc\_vulkan.json:/my\_build/my\_icd.json
469
470Notice the colon between "vendorc\_vulkan.json" and "/my\_build/my\_icd.json".
471
472NOTE: this environment variable will be ignored for suid programs.
473
474#### Android
475
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700476The Android loader lives in the system library folder. The location cannot be
477changed. The loader will load the driver/ICD via hw_get_module with the ID
478of "vulkan". Due to security policies in Android none of this can be modified
479under normal use.
Jon Ashburnc2972682016-02-08 15:42:01 -0700480
481
Jon Ashburncc300a22016-02-11 14:57:30 -0700482ICD interface requirements
483----------------------------------------
Jon Ashburnc2972682016-02-08 15:42:01 -0700484
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700485Generally, for all Vulkan commands issued by an application, the loader can be
486viewed as a pass through. That is, the loader generally doesn’t modified the
Jon Ashburn54791f62016-04-22 14:40:07 -0600487commands or their parameters, but simply calls the ICDs entry point for that
488command. There are specific additional interface requirements an ICD needs to comply with that
489are over and above any requirements from the Vulkan specification including WSI extension specification.
490These addtional requirements are versioned to allow flexibility in the future.
491These interface requirements will be set forth in the following sections: 1) describing
492which "loader-ICD" interface version is available, 2) detailing the most recent interface version;
4933) the supported, older interface requirements will be described as differences
494from the most recent interface version.
Jon Ashburnc2972682016-02-08 15:42:01 -0700495
496#### Windows and Linux
497
Jon Ashburn54791f62016-04-22 14:40:07 -0600498##### Version Negotiation Between Loader and ICDs
Jon Ashburnc2972682016-02-08 15:42:01 -0700499
Jon Ashburn54791f62016-04-22 14:40:07 -0600500All ICDs (supporting interface version 2 or higher) must export the following
501function that is used for determination of the interface version that will be used.
502This entry point is not a part of the Vulkan API itself, only a private interface
503between the loader and ICDs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700504
Jon Ashburn54791f62016-04-22 14:40:07 -0600505VKAPI_ATTR VkResult VKAPI_CALL vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
Jon Ashburnc2972682016-02-08 15:42:01 -0700506
Jon Ashburn54791f62016-04-22 14:40:07 -0600507This entry point reports the "loader-ICD" interface version supported by both the loader and the ICD.
508The loader informs the ICD of it's desired interface version (typically the latest) via the
509pSupportedVersion parameter.
510This call is the first call made by the loader into the ICD (prior to any calls to
511vk\_icdGetInstanceProcAddr).
Jon Ashburnc2972682016-02-08 15:42:01 -0700512
Jon Ashburn54791f62016-04-22 14:40:07 -0600513If a loader sees that an ICD does not export this symbol it knows that it's dealing
514with a legacy ICD supporting either interface version 0 or 1.
515Similarly, if an ICD sees a call to vk\_icdGetInstanceProcAddr before a call to
516vk_icdGetLoaderICDInterfaceVersion then it knows that it's dealing with a legacy loader
517supporting version 0 or 1.
518Note if the loader calls vk\_icdGetInstanceProcAddr first it supports version 1,
519otherwise the loader only supports version 0.
Jon Ashburnc2972682016-02-08 15:42:01 -0700520
Jon Ashburn54791f62016-04-22 14:40:07 -0600521The pSupportedVersion parameter is both an input and output parameter.
522It is filled in by the loader before the call with the desired latest interface version supported by the loader.
Jeff Julianof1619872016-02-17 17:25:42 -0500523
Jon Ashburn54791f62016-04-22 14:40:07 -0600524If the ICD receiving the call no longer supports the interface version provided
525by the loader (due to deprecation) then it can report VK_ERROR_INCOMPATIBLE_DRIVER error,
526otherwise it sets the value pointed by pSupportedVersion to the latest interface
527version supported by both the ICD and the loader and returns VK_SUCCESS.
528The ICD should report VK_SUCCESS in case the loader provided interface version
529is newer than that supported by the ICD, as it's the loader's responsibility to
530determine whether it can support the older interface version supported by the ICD.
531The ICD should also report VK_SUCCESS in the case it's interface version is greater
532than the loader's, but return the loader's version. Thus, upon return of VK_SUCCESS
533the pSupportedVersion will contain the desired interface version to be used by the ICD.
Jon Ashburnc2972682016-02-08 15:42:01 -0700534
Jon Ashburn54791f62016-04-22 14:40:07 -0600535If the loader receives back an interface version from the ICD that the loader no longer
536supports (due to deprecation) or it receives a VK_ERROR_INCOMPATIBLE_DRIVER error
537instead of VK_SUCCESS then the loader will treat the ICD as incompatible
538and will not load it for use. In this case the application will not see the ICDs vkPhysicalDevice
539during enumeration.
Jon Ashburnc2972682016-02-08 15:42:01 -0700540
Jon Ashburn54791f62016-04-22 14:40:07 -0600541##### Loader Version 2 Interface Requirements
Jon Ashburnc2972682016-02-08 15:42:01 -0700542
Jon Ashburn54791f62016-04-22 14:40:07 -0600543Version 2 interface has requirements in three areas: 1) ICD Vulkan entry point discovery,
5442) KHR_surface related requirements in the WSI extensions, 3) Vulkan dispatchable object
545creation requirements.
Jon Ashburnc2972682016-02-08 15:42:01 -0700546
Jon Ashburn54791f62016-04-22 14:40:07 -0600547###### ICD Vulkan entry point discovery
548All ICDs must export the following function that is used for discovery of ICD Vulkan entry points.
549This entry point is not a part of the Vulkan API itself, only a private interface between the loader and ICDs for version 1 and higher interfaces.
Jon Ashburnc2972682016-02-08 15:42:01 -0700550
Jon Ashburn54791f62016-04-22 14:40:07 -0600551VKAPI\_ATTR PFN\_vkVoidFunction VKAPI\_CALL vk\_icdGetInstanceProcAddr(VkInstance instance, const char* pName);
552
553This function has very similar semantics to the Vulkan command vkGetInstanceProcAddr.
554vk\_icdGetInstanceProcAddr returns valid function pointers for all the global level
555and instance level Vulkan commands, and also for vkGetDeviceProcAddr.
556Global level commands are those
557which contain no dispatchable object as the first parameter, such as
558vkCreateInstance and vkEnumerateInstanceExtensionProperties. The ICD must
559support querying global level entry points by calling
560vk\_icdGetInstanceProcAddr with a NULL VkInstance parameter. Instance level
561commands are those that have either VkInstance, or VkPhysicalDevice as the
562first parameter dispatchable object. Both core entry points and any instance
563extension entry points the ICD supports should be available via
564vk\_icdGetInstanceProcAddr. Future Vulkan instance extensions may define and
565use new instance level dispatchable objects other than VkInstance and
566VkPhysicalDevice, in which case extension entry points using these newly
567defined dispatchable objects must be queryable via vk\_icdGetInstanceProcAddr.
568
569All other Vulkan entry points must either NOT be exported from the ICD
570library or else NOT use the official Vulkan function names if they are
571exported. This requirement is for ICD libraries that include other
572functionality (such as OpenGL library) and thus could be loaded by the
573application prior to when the Vulkan loader library is loaded by the
574application. In other words, the ICD library exported Vulkan symbols must not
575clash with the loader's exported Vulkan symbols.
576
577Beware of interposing by dynamic OS library loaders if the official Vulkan
578names are used. On Linux, if official names are used, the ICD library must be
579linked with -Bsymbolic.
580
581###### Handling KHR_surface objects in the WSI extensions
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700582Normally, ICDs handle object creation and destruction for various Vulkan
583objects. The WSI surface extensions for Linux and Windows
584(VK\_KHR\_win32\_surface, VK\_KHR\_xcb\_surface, VK\_KHR\_xlib\_surface,
585VK\_KHR\_mir\_surface, VK\_KHR\_wayland\_surface, and VK\_KHR\_surface) are
586handled differently. For these extensions, the VkSurfaceKHR object creation and
587destruction is handled by the loader as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700588
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07005891. Loader handles the vkCreate\*SurfaceKHR() and vkDestroySurfaceKHR()
590 functions including creating/destroying the VkSurfaceKHR object.
Jon Ashburnc2972682016-02-08 15:42:01 -0700591
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07005922. VkSurfaceKHR objects have the underlying structure (VkIcdSurface\*) as
593 defined in include/vulkan/vk\_icd.h.
Jon Ashburnc2972682016-02-08 15:42:01 -0700594
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07005953. ICDs can cast any VkSurfaceKHR object to a pointer to the appropriate
596 VkIcdSurface\* structure.
Jon Ashburnc2972682016-02-08 15:42:01 -0700597
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07005984. VkIcdSurface\* structures include VkIcdSurfaceWin32, VkIcdSurfaceXcb,
Jeff Julianof1619872016-02-17 17:25:42 -0500599 VkIcdSurfaceXlib, VkIcdSurfaceMir, and VkIcdSurfaceWayland. The first field
600 in the structure is a VkIcdSurfaceBase enumerant that indicates whether the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700601 surface object is Win32, Xcb, Xlib, Mir, or Wayland.
Jon Ashburnc2972682016-02-08 15:42:01 -0700602
Jon Ashburn54791f62016-04-22 14:40:07 -0600603###### ICD dispatchable object creation
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700604As previously covered, the loader requires dispatch tables to be accessible
605within Vulkan dispatchable objects, which include VkInstance, VkPhysicalDevice,
606VkDevice, VkQueue, and VkCommandBuffer. The specific requirements on all
607dispatchable objects created by ICDs are as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700608
Jon Ashburncc300a22016-02-11 14:57:30 -0700609- All dispatchable objects created by an ICD can be cast to void \*\*
Jon Ashburnc2972682016-02-08 15:42:01 -0700610
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700611- The loader will replace the first entry with a pointer to the dispatch table
612 which is owned by the loader. This implies three things for ICD drivers:
Jon Ashburnc2972682016-02-08 15:42:01 -0700613
Jon Ashburncc300a22016-02-11 14:57:30 -07006141. The ICD must return a pointer for the opaque dispatchable object handle.
Jon Ashburnc2972682016-02-08 15:42:01 -0700615
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07006162. This pointer points to a regular C structure with the first entry being a
617 pointer. Note: for any C\++ ICD's that implement VK objects directly as C\++
618 classes. The C\++ compiler may put a vtable at offset zero if your class is
Jeff Julianof1619872016-02-17 17:25:42 -0500619 non-POD due to the use of a virtual function. In this case use a regular C
620 structure (see below).
Jon Ashburnc2972682016-02-08 15:42:01 -0700621
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07006223. The loader checks for a magic value (ICD\_LOADER\_MAGIC) in all the created
623 dispatchable objects, as follows (see include/vulkan/vk\_icd.h):
Jon Ashburnc2972682016-02-08 15:42:01 -0700624
625```
626
Jon Ashburncc300a22016-02-11 14:57:30 -0700627#include "vk_icd.h"
Jon Ashburnc2972682016-02-08 15:42:01 -0700628
Jon Ashburncc300a22016-02-11 14:57:30 -0700629union _VK_LOADER_DATA {
630 uintptr loadermagic;
631 void *loaderData;
632} VK_LOADER_DATA;
Jon Ashburnc2972682016-02-08 15:42:01 -0700633
Jon Ashburncc300a22016-02-11 14:57:30 -0700634vkObj alloc_icd_obj()
Jon Ashburnc2972682016-02-08 15:42:01 -0700635{
Jon Ashburncc300a22016-02-11 14:57:30 -0700636 vkObj *newObj = alloc_obj();
637 ...
638 // Initialize pointer to loader's dispatch table with ICD_LOADER_MAGIC
Jon Ashburnc2972682016-02-08 15:42:01 -0700639
Jon Ashburncc300a22016-02-11 14:57:30 -0700640 set_loader_magic_value(newObj);
641 ...
642 return newObj;
Jon Ashburnc2972682016-02-08 15:42:01 -0700643}
Jon Ashburnc2972682016-02-08 15:42:01 -0700644```
645
Jon Ashburn54791f62016-04-22 14:40:07 -0600646##### Loader Version 0 and 1 Interface Differences
647
648Version 0 and 1 interfaces do not support version negotiation via vk\_icdNegotiateLoaderICDInterfaceVersion.
649ICDs can distinguish version 0 and version 1 interfaces as follows:
650if the loader calls vk\_icdGetInstanceProcAddr first it supports version 1,
651otherwise the loader only supports version 0.
652
653Version 0 interface does not support vk\_icdGetInstanceProcAddr. Version 0 interface requirements for
654obtaining ICD Vulkan entry points are as follows:
655
656- vkGetInstanceProcAddr exported in the ICD library and returns valid function
657 pointers for all the Vulkan API entry points;
658
659- vkCreateInstance exported in the ICD library;
660
661- vkEnumerateInstanceExtensionProperties exported in the ICD library;
662
663
Jon Ashburnc2972682016-02-08 15:42:01 -0700664Additional Notes:
665
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700666- The loader will filter out extensions requested in vkCreateInstance and
667vkCreateDevice before calling into the ICD; Filtering will be of extensions
Jeff Julianof1619872016-02-17 17:25:42 -0500668advertised by entities (e.g. layers) different from the ICD in question.
669- The loader will not call the ICD for vkEnumerate\*LayerProperties() as layer
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700670properties are obtained from the layer libraries and layer JSON files.
671- If an ICD library wants to implement a layer it can do so by having the
672appropriate layer JSON manifest file refer to the ICD library file.
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700673- The loader will not call the ICD for
674 vkEnumerate\*ExtensionProperties(pLayerName != NULL).
Jon Ashburn2b2f6182016-04-04 16:37:37 -0600675- ICDs creating new dispatchable objects via device extensions need to initialize
Jon Ashburn54791f62016-04-22 14:40:07 -0600676the created dispatchable object. The loader has generic trampoline code for unknown
Jon Ashburn2b2f6182016-04-04 16:37:37 -0600677device extensions. This generic trampoline code doesn't initialize the dispatch table within
Jon Ashburn54791f62016-04-22 14:40:07 -0600678the newly created object. See the section for more information on how to initialize created
679dispatchable objects for extensions non known by the loader. [layer link](#creating-new-dispatchable-objects)
Jeff Julianof1619872016-02-17 17:25:42 -0500680
Jon Ashburnc2972682016-02-08 15:42:01 -0700681#### Android
682
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700683The Android loader uses the same protocol for initializing the dispatch
684table as described above. The only difference is that the Android
685loader queries layer and extension information directly from the
686respective libraries and does not use the json manifest files used
687by the Windows and Linux loaders.
Jon Ashburnc2972682016-02-08 15:42:01 -0700688
689Vulkan layer interface with the loader
690--------------------------------------
691
692### Layer discovery
693
694#### Windows
695
696##### Properly-Installed Layers
697
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700698In order to find properly-installed layers, the Vulkan loader will use a
699similar mechanism as used for ICDs. Text information files (aka manifest
700files), that use a JSON format, are read in order to identify the names and
701attributes of layers and their extensions. The use of manifest files allows the
702loader to avoid loading any shared library files when the application does not
703query nor request any extensions. Layers and extensions have additional
704complexity, and so their manifest files contain more information than ICD info
705files. For example, a layer shared library file may contain multiple
706layers/extensions (perhaps even an ICD).
Jon Ashburnc2972682016-02-08 15:42:01 -0700707
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700708In order to find properly-installed layers, the Vulkan loader will scan the
709values in the following Windows registry keys:
Jon Ashburnc2972682016-02-08 15:42:01 -0700710
711HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers
712
713HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers
714
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700715Explicit layers are those which are enabled by an application (e.g. with the
716vkCreateInstance function), or by an environment variable (as mentioned
717previously).
Jon Ashburnc2972682016-02-08 15:42:01 -0700718
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700719Implicit layers are those which are enabled by their existence. For example,
720certain application environments (e.g. Steam or an automotive infotainment
721system) may have layers which they always want enabled for all applications
722that they start. Other implicit layers may be for all applications started on a
723given system (e.g. layers that overlay frames-per-second). Implicit layers are
724enabled automatically, whereas explicit layers must be enabled explicitly. What
725distinguishes a layer as implicit or explicit is by which registry key its
726layer information file is referenced by.
Jon Ashburnc2972682016-02-08 15:42:01 -0700727
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700728For each value in these keys which has DWORD data set to 0, the loader opens
729the JSON manifest file specified by the name of the value. Each name must be a
730full pathname to the manifest file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700731
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700732The Vulkan loader will open each info file to obtain information about the
733layer, including the name or pathname of a shared library (".dll") file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700734
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700735This manifest file is in the JSON format and contains the following
736information:
Jon Ashburnc2972682016-02-08 15:42:01 -0700737
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700738- (required) "file\_format\_version" - same as for ICDs, except that the format
739version can vary independently for ICDs and layers.
Jon Ashburnc2972682016-02-08 15:42:01 -0700740
741- (required) "name" - layer name
742
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600743- (required and deprecated) "type" - which layer chains should the layer be activated on.
744Distinct instance and device layers are deprecated; there are now just layers.
745Allowable values for type (both before and after deprecation) are "INSTANCE", "GLOBAL" and, "DEVICE."
746"DEVICE" layers are skipped over by the loader as if they were not found.
747Thus, layers must have a type of "GLOBAL" or "INSTANCE" for the loader to include the layer in it's discovery.
Jon Ashburnc2972682016-02-08 15:42:01 -0700748
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700749- (required) "library\_path" - filename / full path / relative path to the
750library file
Jon Ashburnc2972682016-02-08 15:42:01 -0700751
752- (required) "api\_version" - same as for ICDs.
753
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700754- (required) "implementation\_version" - layer version, a single number
755increasing with backward compatible changes.
Jon Ashburnc2972682016-02-08 15:42:01 -0700756
757- (required) "description" - informative description of the layer.
758
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700759- (optional) "device\_extensions" or "instance\_extensions" - array of
760extension information as follows
Jon Ashburnc2972682016-02-08 15:42:01 -0700761
Jon Ashburncc300a22016-02-11 14:57:30 -0700762 - (required) extension "name" - Vulkan registered name
Jon Ashburnc2972682016-02-08 15:42:01 -0700763
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700764 - (required) extension "spec\_version" - extension specification version, a
765single number, increasing with backward compatible changes.
Jon Ashburnc2972682016-02-08 15:42:01 -0700766
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700767 - (required for device\_extensions with entry points) extension
Jeff Julianof1619872016-02-17 17:25:42 -0500768"entrypoints" - array of device extension entry points; not used for instance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700769extensions
Jon Ashburnc2972682016-02-08 15:42:01 -0700770
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700771- (sometimes required) "functions" - mapping list of function entry points. If
772multiple layers exist within the same shared library (or if a layer is in the
773same shared library as an ICD), this must be specified to allow each layer to
Jeff Julianof1619872016-02-17 17:25:42 -0500774have its own vkGet\*ProcAddr entry points that can be found by the loader. At
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700775this time, only the following two functions are required:
Jon Ashburnc2972682016-02-08 15:42:01 -0700776
Jon Ashburncc300a22016-02-11 14:57:30 -0700777 - "vkGetInstanceProcAddr" name
Jon Ashburnc2972682016-02-08 15:42:01 -0700778
Jon Ashburncc300a22016-02-11 14:57:30 -0700779 - "vkGetDeviceProcAddr" name
Jon Ashburnc2972682016-02-08 15:42:01 -0700780
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700781- (optional for implicit layers) "enable\_environment" requirement(s) -
782environment variable and value required to enable an implicit layer. This
783environment variable (which should vary with each "version" of the layer, as in
784"ENABLE\_LAYER\_FOO\_1") must be set to the given value or else the implicit
785layer is not loaded. This is for application environments (e.g. Steam) which
786want to enable a layer(s) only for applications that they launch, and allows
787for applications run outside of an application environment to not get that
788implicit layer(s).
Jon Ashburnc2972682016-02-08 15:42:01 -0700789
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700790- (required for implicit layers) "disable\_environment" requirement(s) -
791environment variable and value required to disable an implicit layer. Note: in
792rare cases of an application not working with an implicit layer, the
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600793application can set this environment variable (before calling Vulkan commands)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700794in order to "blacklist" the layer. This environment variable (which should vary
795with each "version" of the layer, as in "DISABLE\_LAYER\_FOO\_1") must be set
796(not particularly to any value). If both the "enable\_environment" and
797"disable\_environment" variables are set, the implicit layer is disabled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700798
799For example:
800
Jon Ashburncc300a22016-02-11 14:57:30 -0700801```
Jon Ashburnc2972682016-02-08 15:42:01 -0700802{
Jon Ashburncc300a22016-02-11 14:57:30 -0700803"file_format_version" : "1.0.0",
804"layer": {
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600805 "name": "VK_LAYER_LUNARG_overlay",
806 "type": "INSTANCE",
Jon Ashburncc300a22016-02-11 14:57:30 -0700807 "library_path": "vkOverlayLayer.dll"
Tony Barbourd83f06c2016-03-08 14:50:03 -0700808 "api_version" : "1.0.5",
Jon Ashburncc300a22016-02-11 14:57:30 -0700809 "implementation_version" : "2",
810 "description" : "LunarG HUD layer",
811 "functions": {
812 "vkGetInstanceProcAddr": "OverlayLayer_GetInstanceProcAddr",
813 "vkGetDeviceProcAddr": "OverlayLayer_GetDeviceProcAddr"
814 },
Jon Ashburn26ed3f32016-02-14 21:54:52 -0700815 "instance_extensions": [
Jon Ashburncc300a22016-02-11 14:57:30 -0700816 {
817 "name": "VK_debug_report_EXT",
818 "spec_version": "1"
819 },
820 {
821 "name": "VK_VENDOR_DEBUG_X",
822 "spec_version": "3"
823 }
824 ],
Courtney Goeltzenleuchter84a75ce2016-02-15 15:07:54 -0700825 "device_extensions": [
Jon Ashburncc300a22016-02-11 14:57:30 -0700826 {
Jon Ashburn58048d02016-03-03 12:03:58 -0700827 "name": "VK_DEBUG_MARKER_EXT",
Jon Ashburncc300a22016-02-11 14:57:30 -0700828 "spec_version": "1",
829 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
830 }
831 ],
832 "disable_environment": {
833 "DISABLE_LAYER_OVERLAY_1": ""
834 }
Jon Ashburnc2972682016-02-08 15:42:01 -0700835}
Jon Ashburnc2972682016-02-08 15:42:01 -0700836}
Jon Ashburncc300a22016-02-11 14:57:30 -0700837```
Jon Ashburnc2972682016-02-08 15:42:01 -0700838
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700839The "library\_path" specifies either a filename, a relative pathname, or a full
840pathname to a layer shared library (".dll") file, which the loader will attempt
841to load using LoadLibrary(). If the layer is specified via a relative pathname,
842it is relative to the path of the info file (e.g. for cases when an application
843provides a layer that is in the same folder hierarchy as the rest of the
844application files). If the layer is specified via a filename, the shared
845library lives in the system's DLL search path (e.g. in the
846"C:\\Windows\\System32" folder).
Jon Ashburnc2972682016-02-08 15:42:01 -0700847
848There are no rules about the name of the text files (except the .json suffix).
849
850There are no rules about the name of the layer shared library files.
851
852##### Using Pre-Production Layers
853
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700854As with ICDs, developers may need to use special, pre-production layers,
855without modifying the properly-installed layers. This need is met with the use
856of the "VK\_LAYER\_PATH" environment variable, which will override the
857mechanism using for finding properly-installed layers. Because many layers may
858exist on a system, this environment variable is a semi-colon-separated list of
859folders that contain layer info files. Only the folder listed in
860"VK\_LAYER\_PATH" will be scanned for info files. Each semi-colon-separated
861entry is:
Jon Ashburnc2972682016-02-08 15:42:01 -0700862
863- The full pathname of a folder containing layer info files
864
865#### Linux
866
867##### Properly-Installed Layers
868
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700869In order to find properly-installed layers, the Vulkan loader will use a
870similar mechanism as used for ICDs. Text information files, that use a JSON
871format, are read in order to identify the names and attributes of layers and
872their extensions. The use of text info files allows the loader to avoid loading
873any shared library files when the application does not query nor request any
874extensions. Layers and extensions have additional complexity, and so their info
875files contain more information than ICD info files. For example, a layer shared
876library file may contain multiple layers/extensions (perhaps even an ICD).
Jon Ashburnc2972682016-02-08 15:42:01 -0700877
878The Vulkan loader will scan the files in the following Linux directories:
879
880/usr/share/vulkan/explicit\_layer.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700881/usr/share/vulkan/implicit\_layer.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700882/etc/vulkan/explicit\_layer.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700883/etc/vulkan/implicit\_layer.d
David Pinedo3e163ee2016-04-18 16:59:59 -0600884\$HOME/.local/share/vulkan/explicit\_layer.d
885\$HOME/.local/share/vulkan/implicit\_layer.d
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700886
887Where $HOME is the current home directory of the application's user id; this
888path will be ignored for suid programs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700889
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700890Explicit layers are those which are enabled by an application (e.g. with the
891vkCreateInstance function), or by an environment variable (as mentioned
892previously). Implicit layers are those which are enabled by their existence.
893For example, certain application environments (e.g. Steam or an automotive
894infotainment system) may have layers which they always want enabled for all
895applications that they start. Other implicit layers may be for all applications
896started on a given system (e.g. layers that overlay frames-per-second).
897Implicit layers are enabled automatically, whereas explicit layers must be
898enabled explicitly. What distinguishes a layer as implicit or explicit is by
899which directory its layer information file exists in.
Jon Ashburnc2972682016-02-08 15:42:01 -0700900
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700901The "/usr/share/vulkan/\*\_layer.d" directories are for layers that are
902installed from Linux-distribution-provided packages. The
903"/etc/vulkan/\*\_layer.d" directories are for layers that are installed from
904non-Linux-distribution-provided packages.
Jon Ashburnc2972682016-02-08 15:42:01 -0700905
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700906The information file is in the JSON format and contains the following
907information:
Jon Ashburnc2972682016-02-08 15:42:01 -0700908
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700909- (required) "file\_format\_version" – same as for ICDs, except that the format
910version can vary independently for ICDs and layers.
Jon Ashburnc2972682016-02-08 15:42:01 -0700911
912- (required) "name" - layer name
913
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600914- (required and deprecated) "type" - which layer chains should the layer be activated on.
915Distinct instance and device layers are deprecated; there are now just layers.
916Allowable values for type (both before and after deprecation) are "INSTANCE", "GLOBAL" and, "DEVICE."
917"DEVICE" layers are skipped over by the loader as if they were not found.
918Thus, layers must have a type of "GLOBAL" or "INSTANCE" for the loader to include the layer in it's discovery.
Jon Ashburnc2972682016-02-08 15:42:01 -0700919
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700920- (required) "library\_path" - filename / full path / relative path to the text
921file
Jon Ashburnc2972682016-02-08 15:42:01 -0700922
923- (required) "api\_version" – same as for ICDs.
924
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700925- (required) "implementation\_version" – layer version, a single number
926increasing with backward compatible changes.
Jon Ashburnc2972682016-02-08 15:42:01 -0700927
Jeff Julianof1619872016-02-17 17:25:42 -0500928- (required) "description" – informative description of the layer.
Jon Ashburnc2972682016-02-08 15:42:01 -0700929
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700930- (optional) "device\_extensions" or "instance\_extensions" - array of
931extension information as follows
Jon Ashburnc2972682016-02-08 15:42:01 -0700932
Jon Ashburncc300a22016-02-11 14:57:30 -0700933 - (required) extension "name" - Vulkan registered name
Jon Ashburnc2972682016-02-08 15:42:01 -0700934
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700935 - (required) extension "spec\_version" - extension specification version, a
936single number, increasing with backward compatible changes.
Jon Ashburnc2972682016-02-08 15:42:01 -0700937
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700938 - (required for device extensions with entry points) extension
Jeff Julianof1619872016-02-17 17:25:42 -0500939"entrypoints" - array of device extension entry points; not used for instance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700940extensions
Jon Ashburnc2972682016-02-08 15:42:01 -0700941
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700942- (sometimes required) "functions" - mapping list of function entry points. If
943multiple layers exist within the same shared library (or if a layer is in the
944same shared library as an ICD), this must be specified to allow each layer to
Jeff Julianof1619872016-02-17 17:25:42 -0500945have its own vkGet\*ProcAddr entry points that can be found by the loader. At
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700946this time, only the following two functions are required:
Jon Ashburncc300a22016-02-11 14:57:30 -0700947 - "vkGetInstanceProcAddr" name
948 - "vkGetDeviceProcAddr" name
Jon Ashburnc2972682016-02-08 15:42:01 -0700949
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700950- (optional for implicit layers) "enable\_environment" requirement(s) -
951environment variable and value required to enable an implicit layer. This
952environment variable (which should vary with each "version" of the layer, as in
953"ENABLE\_LAYER\_FOO\_1") must be set to the given value or else the implicit
954layer is not loaded. This is for application environments (e.g. Steam) which
955want to enable a layer(s) only for applications that they launch, and allows
956for applications run outside of an application environment to not get that
957implicit layer(s).
Jon Ashburnc2972682016-02-08 15:42:01 -0700958
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700959- (required for implicit layers) "disable\_environment" requirement(s) -
960environment variable and value required to disable an implicit layer. Note: in
961rare cases of an application not working with an implicit layer, the
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600962application can set this environment variable (before calling Vulkan commands)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700963in order to "blacklist" the layer. This environment variable (which should vary
964with each "version" of the layer, as in "DISABLE\_LAYER\_FOO\_1") must be set
965(not particularly to any value). If both the "enable\_environment" and
966"disable\_environment" variables are set, the implicit layer is disabled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700967
968For example:
Jon Ashburncc300a22016-02-11 14:57:30 -0700969```
Jon Ashburnc2972682016-02-08 15:42:01 -0700970{
Jon Ashburncc300a22016-02-11 14:57:30 -0700971"file_format_version" : "1.0.0",
Jon Ashburnc2972682016-02-08 15:42:01 -0700972"layer": {
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600973 "name": "VK_LAYER_LUNARG_overlay",
974 "type": "INSTANCE",
Jon Ashburncc300a22016-02-11 14:57:30 -0700975 "library_path": "vkOverlayLayer.dll"
Tony Barbourd83f06c2016-03-08 14:50:03 -0700976 "api_version" : "1.0.5",
Jon Ashburncc300a22016-02-11 14:57:30 -0700977 "implementation_version" : "2",
978 "description" : "LunarG HUD layer",
979 "functions": {
980 "vkGetInstanceProcAddr": "OverlayLayer_GetInstanceProcAddr",
981 "vkGetDeviceProcAddr": "OverlayLayer_GetDeviceProcAddr"
982 },
Jon Ashburn26ed3f32016-02-14 21:54:52 -0700983 "instance_extensions": [
Jon Ashburncc300a22016-02-11 14:57:30 -0700984 {
985 "name": "VK_debug_report_EXT",
986 "spec_version": "1"
987 },
988 {
989 "name": "VK_VENDOR_DEBUG_X",
990 "spec_version": "3"
991 }
992 ],
Courtney Goeltzenleuchter84a75ce2016-02-15 15:07:54 -0700993 "device_extensions": [
Jon Ashburncc300a22016-02-11 14:57:30 -0700994 {
Jon Ashburn58048d02016-03-03 12:03:58 -0700995 "name": "VK_DEBUG_MARKER_EXT",
Jon Ashburncc300a22016-02-11 14:57:30 -0700996 "spec_version": "1",
997 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
998 }
999 ],
1000 "disable_environment": {
1001 "DISABLE_LAYER_OVERLAY_1": ""
1002 }
Jon Ashburnc2972682016-02-08 15:42:01 -07001003}
Jon Ashburnc2972682016-02-08 15:42:01 -07001004}
Jon Ashburncc300a22016-02-11 14:57:30 -07001005```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001006The "library\_path" specifies either a filename, a relative pathname, or a full
1007pathname to a layer shared library (".so") file, which the loader will attempt
1008to load using dlopen(). If the layer is specified via a filename, the loader
1009will attempt to open that file as a shared object using dlopen(), and the file
1010must be in a directory that dlopen is configured to look in (Note: various
1011distributions are configured differently). A distribution is free to create
1012Vulkan-specific system directories (e.g. ".../vulkan/layers"), but is not
1013required to do so. If the layer is specified via a relative pathname, it is
1014relative to the path of the info file (e.g. for cases when an application
1015provides a layer that is in the same directory hierarchy as the rest of the
1016application files).
Jon Ashburnc2972682016-02-08 15:42:01 -07001017
1018There are no rules about the name of the text files (except the .json suffix).
1019
1020There are no rules about the name of the layer shared library files.
1021
1022##### Using Pre-Production Layers
1023
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001024As with ICDs, developers may need to use special, pre-production layers,
1025without modifying the properly-installed layers. This need is met with the use
1026of the "VK\_LAYER\_PATH" environment variable, which will override the
1027mechanism using for finding properly-installed layers. Because many layers may
1028exist on a system, this environment variable is a colon-separated list of
1029directories that contain layer info files. Only the directories listed in
1030"VK\_LAYER\_PATH" will be scanned for info files. Each colon-separated entry
1031is:
Jon Ashburnc2972682016-02-08 15:42:01 -07001032
1033- The full pathname of a directory containing layer info files
1034
1035NOTE: these environment variables will be ignored for suid programs.
1036
1037#### Android
1038
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001039The recommended way to enable layers is for applications
1040to programatically enable them. The layers are provided by the application
1041and must live in the application's library folder. The application
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001042enables the layers at vkCreateInstance as any Vulkan
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001043application would.
1044An application enabled for debug has more options. It can enumerate and enable
1045layers located in /data/local/vulkan/debug.
Jon Ashburnc2972682016-02-08 15:42:01 -07001046
Jon Ashburncc300a22016-02-11 14:57:30 -07001047Layer interface requirements
1048------------------------------------------------------
Jon Ashburnc2972682016-02-08 15:42:01 -07001049
1050#### Architectural interface overview
1051
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001052There are two key architectural features that drive the loader to layer library
1053interface: 1) separate and distinct instance and device call chains, and 2)
1054distributed dispatch. First these architectural features will be described and
1055then the detailed interface will be specified.
Jon Ashburnc2972682016-02-08 15:42:01 -07001056
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001057Call chains are the links of calls for a given Vulkan command from layer module
1058to layer module with the loader and or the ICD being the bottom most command.
1059Call chains are constructed at both the instance level and the device level by
1060the loader with cooperation from the layer libraries. Instance call chains are
1061constructed by the loader when layers are enabled at vkCreateInstance. Device
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001062call chains are constructed by the loader when layers are enabled, by the loader, at
ttyio0811cec2016-04-10 22:09:44 +08001063vkCreateDevice. A layer can intercept Vulkan instance commands, device commands
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001064or both. For a layer to intercept instance commands, it must participate in the
1065instance call chain. For a layer to intercept device commands, it must
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001066participate in the device chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001067
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001068Normally, when a layer intercepts a given Vulkan command, it will call down the
1069instance or device chain as needed. The loader and all layer libraries that
1070participate in a call chain cooperate to ensure the correct sequencing of calls
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001071from one entity to the next. This group effort for call chain sequencing is
Jeff Julianof1619872016-02-17 17:25:42 -05001072hereinafter referred to as distributed dispatch. In distributed dispatch, since
1073each layer is responsible for properly calling the next entity in the device or
1074instance chain, a dispatch mechanism is required for all Vulkan commands a
1075layer intercepts. For Vulkan commands that are not intercepted by a layer, or
1076if the layer chooses to terminate a given Vulkan command by not calling down
1077the chain, then no dispatch mechanism is needed for that particular Vulkan
1078command. Only for those Vulkan commands, which may be a subset of all Vulkan
1079commands, that a layer intercepts is a dispatching mechanism by the layer
1080needed. The loader is responsible for dispatching all core and instance
1081extension Vulkan commands to the first entity in the chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001082
Jeff Julianof1619872016-02-17 17:25:42 -05001083Instance level Vulkan commands are those that have the dispatchable objects
1084VkInstance, or VkPhysicalDevice as the first parameter and also includes
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001085vkCreateInstance.
Jeff Julianof1619872016-02-17 17:25:42 -05001086
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001087Device level Vulkan commands are those that use VkDevice, VkQueue or
1088VkCommandBuffer as the first parameter and also include vkCreateDevice. Future
1089extensions may introduce new instance or device level dispatchable objects, so
1090the above lists may be extended in the future.
Jon Ashburnc2972682016-02-08 15:42:01 -07001091
Chia-I Wucb24fec2016-04-20 06:23:24 +08001092#### Layer Library Interface
Jeff Julianof1619872016-02-17 17:25:42 -05001093
Chia-I Wucb24fec2016-04-20 06:23:24 +08001094A layer library is a container of layers. This section defines an extensible
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001095manifest file interface or programming interface to discover layers contained in layer libraries.
1096The extensible programming interface is used on Android only. For Windows and Linux,
1097the layer manifest JSON files are used.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001098
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001099It also specifies the minimal conventions
1100and rules a layer must follow. Other sections might have other guidelines that layers should follow.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001101
1102##### Layer Conventions and Rules
1103
1104A layer, when inserted into an otherwise compliant Vulkan implementation, must
1105still result in a compliant Vulkan implementation[\*]. It must additionally
1106follow some conventions and rules.
1107
1108A layer is always chained with other layers. It must not make invalid calls
1109to or rely on undefined behaviors of its lower layers. When it changes the
1110behavior of a command, it must make sure its upper layers do not make invalid
1111calls to or rely on undefined behaviors of its lower layers because of the
1112changed behavior. For example, when a layer intercepts an object creation
1113command to wrap the objects created by its lower layers, it must make sure its
1114lower layers never see the wrapping objects, directly from itself or
1115indirectly from its upper layers.
1116
Chia-I Wub5e850e2016-05-06 08:41:52 +08001117When a layer requires host memory, it may ignore the provided allocators. It
1118should use memory allocators if the layer is intended to run in a production
1119environment, such as an implicit layer that is always enabled. That will
1120allow applications to include the layer's memory usage.
1121
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001122`vkEnumerateInstanceLayerProperties` must enumerate and only enumerate the
1123layer itself.
1124
1125`vkEnumerateInstanceExtensionProperties` must handle the case where
1126`pLayerName` is itself. It must return `VK_ERROR_LAYER_NOT_PRESENT`
1127otherwise, including when `pLayerName` is `NULL`.
1128
1129`vkEnumerateDeviceLayerProperties` is deprecated and may be omitted. The
1130behavior is undefined.
1131
Chia-I Wuadac8342016-04-22 08:12:19 +08001132`vkEnumerateDeviceExtensionProperties` must handle the case where `pLayerName`
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001133is itself. In other cases, it should normally chain to other layers.
1134
1135`vkCreateInstance` must not generate an error for unrecognized layer names and
1136extension names. It may assume the layer names and extension names have been
1137validated.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001138
1139`vkGetInstanceProcAddr` can intercept a command by returning a function
1140pointer different from what would be returned through chaining.
1141
1142`vkGetDeviceProcAddr` can intercept a command by returning a function pointer
1143different from what would be returned through chaining.
1144
Chia-I Wucb24fec2016-04-20 06:23:24 +08001145[\*]: The intention is for layers to have a well-defined baseline behavior.
1146Some of the conventions or rules, for example, may be considered abuses of the
1147specification.
1148
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001149###### Layer Library Interface Version 0 (Android)
Chia-I Wucb24fec2016-04-20 06:23:24 +08001150
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001151A layer library supporting interface version 0 must define and export these
1152introspection functions, unrelated to any Vulkan command despite the names,
1153signatures, and other similarities:
Chia-I Wucb24fec2016-04-20 06:23:24 +08001154
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001155 - `vkEnumerateInstanceLayerProperties` enumerates all layers in a layer
1156 library. This function never fails.
1157
1158 When a layer library contains only one layer, this function may be an alias
1159 to the layer's `vkEnumerateInstanceLayerProperties`.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001160
1161 - `vkEnumerateInstanceExtensionProperties` enumerates instance extensions of
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001162 layers in a layer library. `pLayerName` is always a valid layer name.
1163 This function never fails.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001164
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001165 When a layer library contains only one layer, this function may be an alias
1166 to the layer's `vkEnumerateInstanceExtensionProperties`.
1167
1168 - `vkEnumerateDeviceLayerProperties` enumerates a subset (can be full,
1169 proper, or empty subset) of layers in a layer library. `physicalDevice` is
1170 always `VK_NULL_HANDLE`. This function never fails.
1171
1172 If a layer is not enumerated by this function, it will not participate in
1173 device command interception.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001174
1175 - `vkEnumerateDeviceExtensionProperties` enumerates device extensions of
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001176 layers in a layer library. `physicalDevice` is always `VK_NULL_HANDLE`.
1177 `pLayerName` is always a valid layer name. This function never fails.
1178
1179The introspection functions are not used by the desktop loader.
1180
1181It must also define and export these functions:
Chia-I Wucb24fec2016-04-20 06:23:24 +08001182
1183 - `<layerName>GetInstanceProcAddr` behaves as if `<layerName>`'s
1184 `vkGetInstanceProcAddr` is called, except
1185
1186 - when `pName` is `vkEnumerateInstanceLayerProperties`,
1187 `vkEnumerateInstanceExtensionProperties`, or
1188 `vkEnumerateDeviceLayerProperties` (but _not_
1189 `vkEnumerateDeviceExtensionProperties`), it returns a function pointer to
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001190 the corresponding introspection function defined by this interface.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001191 - when `pName` is `vkGetInstanceProcAddr`, it returns a function pointer
1192 to itself.
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001193
1194 For compatibility with older layer libraries,
1195
Chia-I Wucb24fec2016-04-20 06:23:24 +08001196 - when `pName` is `vkCreateDevice`, it ignores `instance`.
1197 - when `pName` is a device command defined by Vulkan 1.0 or
1198 `VK_KHR_swapchain` (but _not_ other device commands), it may chain to
1199 other layers without intercepting. A loader should avoid querying such
1200 device commands.
1201
1202 When a layer library contains only one layer, this function may
1203 alternatively be named `vkGetInstanceProcAddr`.
1204
1205 - `<layerName>GetDeviceProcAddr` behaves as if `<layerName>`'s
1206 `vkGetDeviceProcAddr` is called.
1207
1208 When a layer library contains only one layer, this function may
1209 alternatively be named `vkGetDeviceProcAddr`.
1210
1211All contained layers must support [`vk_layer.h`][]. They do not need to
1212implement commands that are not queryable. They are recommended not to export
1213any command.
1214
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001215###### Layer Library Interface Version 0 (Windows and Linux)
1216On Windows and Linux (desktop), the loader uses manifest files to discover
1217layer libraries and layers. The desktop loader doesn't directly query the
1218layer library except during chaining. On Android, the loader queries the layer libraries directly as outlined above.
1219
1220The layer libraries and the manifest files must be kept in sync.
1221
1222The following table associates the desktop JSON nodes with the Android layer library queries. It also indicates requirements.
1223
1224| Property | JSON node | Android library query | Notes |
1225|----------|-----------|-----------------------|-------|
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001226| layers in library | layer | vkEnumerateInstanceLayerProperties | one node required for each layer in the library |
1227|layer name | name | vkEnumerateInstanceLayerProperties | one node is required |
1228| layer type | type | vkEnumerateInstanceLayerProperties | one node is required (deprecated) |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001229| library location | library_path | N/A | one node is required |
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001230| vulkan spec version | api_version | vkEnumerateInstanceLayerProperties | one node is required |
1231| layer implementation version | api_version | vkEnumerateInstanceLayerProperties | one node is required |
1232| layer description | description | vkEnumerateInstanceLayerProperties | one node is required |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001233| chaining functions | functions | vkGet*ProcAddr | see Note 1 |
1234| instance extensions | instance_extensions | vkEnumerateInstanceExtensionProperties | see Note 2 |
1235| device extensions | device_extensions | vkEnumerateDeviceExtensionProperties | see Note 3 |
1236
1237Note 1: The "functions" node is required if the layer is using alternative
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001238names for vkGetInstanceProcAddr or vkGetDeviceProcAddr. vkGetInstanceProcAddr and vkGetDeviceProcAddr
1239are required for all layers. See further requirements below.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001240
1241Note 2: One "instance_extensions" node with an array of 1 or more elements
1242required if any instance
1243extensions are supported by a layer, otherwise the node is optional. Each
1244element of the array must have the nodes "name" and "spec_version" which
1245correspond to VkExtensionProperties "extensionName" and "specVersion"
1246respectively.
1247
1248Note 3: One "device_extensions" node with an array of 1 or more elements
1249required if any device
1250extensions are supported by a layer, otherwise the node is optional. Each
1251element of the array must have the nodes "name" and "spec_version" which
1252correspond to VkExtensionProperties "extensionName" and "specVersion"
1253respectively. Additionally, each element of the array of device extensions
1254must have the node "entrypoints" if the device extension adds Vulkan API commands,
1255otherwise this node is not required.
1256The "entrypoint" node is an array of the names of all entrypoints added by the
1257supported extension.
1258
1259The manifest file nodes "file_format_version", "disable_environment", and
1260"enable_environment" have no corresponding equivalent in the Vulkan API nor
1261in the Android layer library interface.
1262
1263"file_format_version" is used to indicate the valid JSON syntax of the file.
1264As nodes are added or deleted which would change the parsing of this file,
1265the file_format_version should change. This version
1266is NOT the same as the interface version. The interface version is a superset
1267of the "file_format_version" and includes the semantics of the nodes in the JSON file. For interface version 0 the file format version must be "1.0.0"
1268
1269"disable_environment" (required) and "enable_evironment" (optional) are for implicit layers as previously described.
1270
1271vkGetInstanceProcAddr requirements:
1272-Irregardless of the name, this function must be implemented and exported in the library for all layers.
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001273-This function must return the local entry points for all instance level Vulkan commands it intercepts.
1274At a minimum, this includes vkGetInstanceProcAddr and vkCreateInstance.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001275Optionally, this function may return intercepted device level
1276Vulkan commands.
1277-Vulkan commands that a layer doesn't intercept must be passed to the next
1278entity in the chain. That is, the next layer/ICD's GetInstanceProcAddr must be called.
1279-Currently this function must be able to handle a VkInstance parameter equal
1280to NULL for instance level commands it intercepts including vkCreateDevice.
1281
1282VkGetDeviceProcAddr requirements:
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001283-Irregardless of the name, this function must be implemented and exported in the library for all layers.
1284-This function must return the local entry points for all device level Vulkan
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001285commands it intercepts. At a minimum, this includes vkGetDeviceProcAddr and vkCreateDevice.
1286-Vulkan commands that a layer doesn't intercept must be passed to the next
1287entity in the chain. That is, the next layer/ICD's GetDeviceProcAddr must be called.
1288
1289There are no requirements on the names of the intercepting functions a layer
1290implements except those listed above for vkGetInstanceProcAddr and
1291vkGetDeviceProcAddr. Layers do not need to implement commands that are not going to be intercepted.
1292
1293All layers within a library must support [`vk_layer.h`][].
Chia-I Wucb24fec2016-04-20 06:23:24 +08001294[`vk_layer.h`]: ../include/vulkan/vk_layer.h
1295
Jon Ashburnc2972682016-02-08 15:42:01 -07001296#### Layer intercept requirements
Jeff Julianof1619872016-02-17 17:25:42 -05001297
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001298- Layers intercept a Vulkan command by defining a C/C++ function with signature
1299identical to the Vulkan API for that command.
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001300- A layer must intercept at least vkGetInstanceProcAddr and
1301vkCreateInstance. Additionally, a layer would also intercept vkGetDeviceProcAddr and vkCreateDevice to participate in the device chain.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001302- Other than the two vkGet*ProcAddr, all other functions intercepted by a layer
1303need NOT be exported by the layer.
1304- For any Vulkan command a layer intercepts which has a non-void return value,
1305an appropriate value must be returned by the layer intercept function.
1306- The layer intercept function must call down the chain to the corresponding
1307Vulkan command in the next entity. Undefined results will occur if a layer
1308doesn't propagate calls down the chain. The two exceptions to this requirement
1309are vkGetInstanceProcAddr and vkGetDeviceProcAddr which only call down the
1310chain for Vulkan commands that they do not intercept.
1311- Layer intercept functions may insert extra calls to Vulkan commands in
1312addition to the intercept. For example, a layer intercepting vkQueueSubmit may
1313want to add a call to vkQueueWaitIdle after calling down the chain for
1314vkQueueSubmit. Any additional calls inserted by a layer must be on the same
1315chain. They should call down the chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001316
1317#### Distributed dispatching requirements
Jeff Julianof1619872016-02-17 17:25:42 -05001318
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001319- For each entry point a layer intercepts, it must keep track of the entry
1320point residing in the next entity in the chain it will call down into. In other
1321words, the layer must have a list of pointers to functions of the appropriate
1322type to call into the next entity. This can be implemented in various ways but
1323for clarity will be referred to as a dispatch table.
1324- A layer can use the VkLayerDispatchTable structure as a device dispatch table
1325(see include/vulkan/vk_layer.h).
1326- A layer can use the VkLayerInstanceDispatchTable structure as a instance
1327dispatch table (see include/vulkan/vk_layer.h).
1328- Layers vkGetInstanceProcAddr function uses the next entity's
Jeff Julianof1619872016-02-17 17:25:42 -05001329vkGetInstanceProcAddr to call down the chain for unknown (i.e. non-intercepted)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001330functions.
1331- Layers vkGetDeviceProcAddr function uses the next entity's
Jeff Julianof1619872016-02-17 17:25:42 -05001332vkGetDeviceProcAddr to call down the chain for unknown (i.e. non-intercepted)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001333functions.
Jon Ashburnc2972682016-02-08 15:42:01 -07001334
1335#### Layer dispatch initialization
Jeff Julianof1619872016-02-17 17:25:42 -05001336
1337- A layer initializes its instance dispatch table within its vkCreateInstance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001338function.
Jeff Julianof1619872016-02-17 17:25:42 -05001339- A layer initializes its device dispatch table within its vkCreateDevice
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001340function.
1341- The loader passes a linked list of initialization structures to layers via
1342the "pNext" field in the VkInstanceCreateInfo and VkDeviceCreateInfo structures
1343for vkCreateInstance and VkCreateDevice respectively.
1344- The head node in this linked list is of type VkLayerInstanceCreateInfo for
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001345instance and VkLayerDeviceCreateInfo for device. See file
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001346include/vulkan/vk_layer.h for details.
1347- A VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO is used by the loader for the
1348"sType" field in VkLayerInstanceCreateInfo.
1349- A VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO is used by the loader for the
1350"sType" field in VkLayerDeviceCreateInfo.
1351- The "function" field indicates how the union field "u" should be interpreted
1352within VkLayer*CreateInfo. The loader will set the "function" field to
1353VK_LAYER_LINK_INFO. This indicates "u" field should be VkLayerInstanceLink or
1354VkLayerDeviceLink.
Jon Ashburnc2972682016-02-08 15:42:01 -07001355- The VkLayerInstanceLink and VkLayerDeviceLink structures are the list nodes.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001356- The VkLayerInstanceLink contains the next entity's vkGetInstanceProcAddr used
1357by a layer.
1358- The VkLayerDeviceLink contains the next entity's vkGetInstanceProcAddr and
1359vkGetDeviceProcAddr used by a layer.
1360- Given the above structures set up by the loader, layer must initialize their
1361dispatch table as follows:
1362 - Find the VkLayerInstanceCreateInfo/VkLayerDeviceCreateInfo structure in
1363the VkInstanceCreateInfo/VkDeviceCreateInfo structure.
Jon Ashburncc300a22016-02-11 14:57:30 -07001364 - Get the next entity's vkGet*ProcAddr from the "pLayerInfo" field.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001365 - For CreateInstance get the next entity's vkCreateInstance by calling the
1366"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001367 pfnNextGetInstanceProcAddr(NULL, "vkCreateInstance").
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001368 - For CreateDevice get the next entity's vkCreateDevice by calling the
1369"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001370 pfnNextGetInstanceProcAddr(NULL, "vkCreateDevice").
Jon Ashburncc300a22016-02-11 14:57:30 -07001371 - Advanced the linked list to the next node: pLayerInfo = pLayerInfo->pNext.
1372 - Call down the chain either CreateDevice or CreateInstance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001373 - Initialize your layer dispatch table by calling the next entity's
1374Get*ProcAddr function once for each Vulkan command needed in your dispatch
1375table
Jon Ashburncc300a22016-02-11 14:57:30 -07001376
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001377#### Example code for CreateInstance
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001378
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001379```cpp
1380VkResult vkCreateInstance(
1381 const VkInstanceCreateInfo *pCreateInfo,
1382 const VkAllocationCallbacks *pAllocator,
1383 VkInstance *pInstance)
1384{
1385 VkLayerInstanceCreateInfo *chain_info =
1386 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1387
1388 assert(chain_info->u.pLayerInfo);
1389 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1390 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1391 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001392 (PFN_vkCreateInstance)fpGetInstanceProcAddr(*pInstance, "vkCreateInstance");
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001393 if (fpCreateInstance == NULL) {
1394 return VK_ERROR_INITIALIZATION_FAILED;
1395 }
1396
1397 // Advance the link info for the next element of the chain
1398 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1399
1400 // Continue call down the chain
1401 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
1402 if (result != VK_SUCCESS)
1403 return result;
1404
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001405 // Init layer's dispatch table using GetInstanceProcAddr of
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001406 // next layer in the chain.
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001407 instance_dispatch_table = new VkLayerInstanceDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001408 layer_init_instance_dispatch_table(
1409 *pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
1410
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001411 // Other layer initialization
1412 ...
1413
1414 return VK_SUCCESS;
1415}
1416```
1417
1418#### Example code for CreateDevice
1419
1420```cpp
1421VkResult
1422vkCreateDevice(
1423 VkPhysicalDevice gpu,
1424 const VkDeviceCreateInfo *pCreateInfo,
1425 const VkAllocationCallbacks *pAllocator,
1426 VkDevice *pDevice)
1427{
1428 VkLayerDeviceCreateInfo *chain_info =
1429 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1430
1431 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1432 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1433 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr =
1434 chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1435 PFN_vkCreateDevice fpCreateDevice =
1436 (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
1437 if (fpCreateDevice == NULL) {
1438 return VK_ERROR_INITIALIZATION_FAILED;
1439 }
1440
1441 // Advance the link info for the next element on the chain
1442 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1443
1444 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
1445 if (result != VK_SUCCESS) {
1446 return result;
1447 }
1448
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001449 // initialize layer's dispatch table
1450 device_dispatch_table = new VkLayerDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001451 layer_init_device_dispatch_table(
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001452 *pDevice, device_dispatch_table, fpGetDeviceProcAddr);
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001453
1454 // Other layer initialization
1455 ...
1456
1457 return VK_SUCCESS;
1458}
1459```
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001460
Jon Ashburncc300a22016-02-11 14:57:30 -07001461#### Special Considerations
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001462##### Associating private data with Vulkan objects within a layer
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001463A layer may want to associate it's own private data with one or more Vulkan
1464objects.
1465Two common methods to do this are hash maps and object wrapping. The loader
1466supports layers wrapping any Vulkan object including dispatchable objects.
1467Layers which wrap objects should ensure they always unwrap objects before
1468passing them down the chain. This implies the layer must intercept every Vulkan
1469command which uses the object in question. Layers above the object wrapping
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001470layer will see the wrapped object. Layers which wrap dispatchable objects must
1471ensure that the first field in the wrapping structure is a pointer to a dispatch table
1472as defined in vk_layer.h. Specifically, an instance wrapped dispatchable object
1473could be as follows:
1474```
1475struct my_wrapped_instance_obj_ {
1476 VkLayerInstanceDispatchTable *disp;
1477 // whatever data layer wants to add to this object
1478};
1479```
1480A device wrapped dispatchable object could be as follows:
1481```
1482struct my_wrapped_instance_obj_ {
1483 VkLayerDispatchTable *disp;
1484 // whatever data layer wants to add to this object
1485};
1486```
Jeff Julianof1619872016-02-17 17:25:42 -05001487
1488Alternatively, a layer may want to use a hash map to associate data with a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001489given object. The key to the map could be the object. Alternatively, for
1490dispatchable objects at a given level (eg device or instance) the layer may
1491want data associated with the VkDevice or VkInstance objects. Since
Jeff Julianof1619872016-02-17 17:25:42 -05001492there are multiple dispatchable objects for a given VkInstance or VkDevice, the
1493VkDevice or VkInstance object is not a great map key. Instead the layer should
1494use the dispatch table pointer within the VkDevice or VkInstance since that
1495will be unique for a given VkInstance or VkDevice.
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001496
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001497##### Creating new dispatchable objects
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001498Layers which create dispatchable objects take special care. Remember that loader
1499trampoline code normally fills in the dispatch table pointer in the newly
1500created object. Thus, the layer must fill in the dispatch table pointer if the
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001501loader trampoline will not do so. Common cases where a layer (or ICD) may create a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001502dispatchable object without loader trampoline code is as follows:
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001503- object wrapping layers that wrap dispatchable objects
1504- layers which add extensions that create dispatchable objects
1505- layers which insert extra Vulkan commands in the stream of commands they
1506intercept from the application
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001507- ICDs which add extensions that create dispatchable objects
1508
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001509The Windows/Linux loader provides a callback that can be used for initializing
1510a dispatchable object. The callback is passed as an extension structure via the
1511pNext field in VkInstanceCreateInfo and VkDeviceCreateInfo. The callback prototype
1512is defined as follows for instance and device callbacks respectively (see vk_layer.h):
1513```
1514VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceLoaderData(VkInstance instance, void *object);
1515VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceLoaderData)(VkDevice device, void *object);
1516```
1517To obtain these callbacks the layer must search through the list of structures
1518pointed to by the "pNext" field in the VkInstanceCreateInfo and VkDeviceCreateInfo parameters to find any callback structures inserted by the loader. The salient details are as follows:
1519- For CreateInstance the callback structure pointed to by "pNext" is VkLayerInstanceCreateInfo as defined in vk_layer.h.
1520- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO within VkInstanceCreateInfo parameter indicates a loader structure.
1521- Within VkLayerInstanceCreateInfo, the "function" field indicates how the union field "u" should be interpreted.
1522- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will contain the callback in "pfnSetInstanceLoaderData".
1523- For CreateDevice the callback structure pointed to by "pNext" is VkLayerDeviceCreateInfo as defined in include/vulkan/vk_layer.h.
1524- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO within VkDeviceCreateInfo parameter indicates a loader structure.
1525- Within VkLayerDeviceCreateInfo, the "function" field indicates how the union field "u" should be interpreted.
1526- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will contain the callback in "pfnSetDeviceLoaderData".
1527
1528Alternatively, if an older loader is being used that doesn't provide these callbacks, the layer may manually initialize the newly created dispatchable object.
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001529To fill in the dispatch table pointer in newly created dispatchable object,
1530the layer should copy the dispatch pointer, which is always the first entry in the structure, from an existing parent object of the same level (instance versus
1531device). For example, if there is a newly created VkCommandBuffer object, then the dispatch pointer from the VkDevice object, which is the parent of the VkCommandBuffer object, should be copied into the newly created object.
Jon Ashburnc2972682016-02-08 15:42:01 -07001532