blob: 104eccc21556aabe3d4b25d4803297437c7f1f8b [file] [log] [blame] [view]
Jon Ashburnc2972682016-02-08 15:42:01 -07001# Vulkan Loader Specification and Architecture Overview
2
Mark Youngcb6e6702016-07-20 11:38:53 -06003<br/>
Jon Ashburnc2972682016-02-08 15:42:01 -07004
Mark Youngcb6e6702016-07-20 11:38:53 -06005## Goals of this document ##
Jon Ashburnc2972682016-02-08 15:42:01 -07006
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
Mark Youngcb6e6702016-07-20 11:38:53 -060011<br/>
12
13## Audience ##
Jon Ashburnc2972682016-02-08 15:42:01 -070014
Mark Youngcb6e6702016-07-20 11:38:53 -060015This document is primarily targeted at Vulkan application, driver and layer developers.
16However, it can also be used by any developer interested in understanding more about
17how the Vulkan loader and layers interact.
Jon Ashburnc2972682016-02-08 15:42:01 -070018
Mark Youngcb6e6702016-07-20 11:38:53 -060019<br/>
Jon Ashburnc2972682016-02-08 15:42:01 -070020
21
Mark Youngcb6e6702016-07-20 11:38:53 -060022## Loader goals ##
Jon Ashburnc2972682016-02-08 15:42:01 -070023
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070024- Support multiple ICDs (Installable Client Drivers) to co-exist on a system
25without interfering with each other.
Jon Ashburnc2972682016-02-08 15:42:01 -070026
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070027- Support optional modules (layers) that can be enabled by an application,
28developer or the system and have no impact when not enabled.
Jon Ashburnc2972682016-02-08 15:42:01 -070029
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070030- Negligible performance cost for an application calling through the loader
31to an ICD entry point.
Jon Ashburnc2972682016-02-08 15:42:01 -070032
Mark Youngcb6e6702016-07-20 11:38:53 -060033<br/>
34
35## Architectural overview of layers and loader ##
Jon Ashburnc2972682016-02-08 15:42:01 -070036
Mark Youngcb6e6702016-07-20 11:38:53 -060037Vulkan is a layered architecture placing the Application on one end, the
38ICDs on the other, and the loader and some number of layers in between.
Jon Ashburnc2972682016-02-08 15:42:01 -070039
Mark Youngcb6e6702016-07-20 11:38:53 -060040Layers are implemented as libraries that can be enabled in different ways
41(including by application request) and loaded during CreateInstance. Each
42layer can chooses to hook (intercept) any Vulkan commands which in turn
43can be ignored, augmented, or simply passed along. A layer may also
44expose functionality not available in the loader or any ICD. Some examples
45of this include: the ability to perform Vulkan API tracing and debugging,
46validate API usage, or overlay additional content on the applications surfaces.
47
48The loader is responsible for working with the various layers as well as
49supporting multiple GPUs and their drivers. Any Vulkan command may
50wind up calling into a diverse set of modules: loader, layers, and ICDs.
51The loader is critical to managing the proper dispatching of Vulkan
52commands to the appropriate set of layers and ICDs. The Vulkan object
53model allows the loader to insert layers into a call chain so that the layers
54can process Vulkan commands prior to the ICD being called.
Jon Ashburnc2972682016-02-08 15:42:01 -070055
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070056Vulkan uses an object model to control the scope of a particular action /
57operation. The object to be acted on is always the first parameter of a Vulkan
Mark Young6d026a72016-06-01 17:49:30 -060058call and is a dispatchable object (see Vulkan specification section 2.3 Object
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070059Model). Under the covers, the dispatchable object handle is a pointer to a
Mark Youngcb6e6702016-07-20 11:38:53 -060060structure, which in turn, contains a pointer to a dispatch table maintained by
61the loader. This dispatch table contains pointers to the Vulkan functions appropriate to
62that object.
Jon Ashburnc2972682016-02-08 15:42:01 -070063
Mark Youngcb6e6702016-07-20 11:38:53 -060064There are two types of dispatch tables the loader maintains:
65- **Instance Dispatch Table**
66 - Contains any function that takes a VkInstance or VkPhysicalDevice as their first parameter
67 - vkEnumeratePhysicalDevices
68 - vkDestroyInstance
69 - vkCreateInstance
70 - ...
71- **Device Dispatch Table**
72 - Contains any function that takes a VkDevice, VkQueue or VkCommandBuffer as their first parameter
Jon Ashburnc2972682016-02-08 15:42:01 -070073
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070074These instance and device dispatch tables are constructed when the application
75calls vkCreateInstance and vkCreateDevice. At that time the application and/or
76system can specify optional layers to be included. The loader will initialize
77the specified layers to create a call chain for each Vulkan function and each
78entry of the dispatch table will point to the first element of that chain.
79Thus, the loader builds an instance call chain for each VkInstance that is
80created and a device call chain for each VkDevice that is created.
Jon Ashburnc2972682016-02-08 15:42:01 -070081
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070082For example, the diagram below represents what happens in the call chain for
83vkCreateInstance. After initializing the chain, the loader will call into the
Mark Young6d026a72016-06-01 17:49:30 -060084first layer's vkCreateInstance which will call the next finally terminating in
85the loader again where this function calls every ICD's vkCreateInstance and
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070086saves the results. This allows every enabled layer for this chain to set up
87what it needs based on the VkInstanceCreateInfo structure from the application.
Jon Ashburnc2505562016-02-15 10:19:26 -070088![Instance call chain](instance_call_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -070089
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070090This also highlights some of the complexity the loader must manage when using
91instance chains. As shown here, the loader must aggregate information from
92multiple devices when they are present. This means that the loader has to know
93about instance level extensions to aggregate them correctly.
Jon Ashburnc2972682016-02-08 15:42:01 -070094
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -070095Device chains are created at vkCreateDevice and are generally simpler because
96they deal with only a single device and the ICD can always be the terminator of
97the chain. The below diagram also illustrates how layers (either device or
98instance) can skip intercepting any given Vulkan entry point.
Jon Ashburnc2505562016-02-15 10:19:26 -070099![Chain skipping layers](chain_skipping_layers.png)
Jon Ashburnc2972682016-02-08 15:42:01 -0700100
Mark Youngcb6e6702016-07-20 11:38:53 -0600101<br/>
102
103## Application interface to loader ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700104
Mark Young6d026a72016-06-01 17:49:30 -0600105In this section we'll discuss how an application interacts with the loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700106
107- Linking to loader library for core and WSI extension symbols.
108
109- Dynamic Vulkan command lookup & application dispatch table.
110
111- Loader library filenames for linking to different Vulkan ABI versions.
112
113- Layers
114
115- Extensions
116
117- vkGetInstanceProcAddr, vkGetDeviceProcAddr
118
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700119The loader library on Windows, Linux and Android will export all core Vulkan
120and all appropriate Window System Interface (WSI) extensions. This is done to
121make it simpler to get started with Vulkan development. When an application
122links directly to the loader library in this way, the Vulkan calls are simple
123trampoline functions that jump to the appropriate dispatch table entry for the
124object they are given.
Jon Ashburnc2972682016-02-08 15:42:01 -0700125
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700126Applications are not required to link directly to the loader library, instead
127they can use the appropriate platform specific dynamic symbol lookup on the
Mark Young6d026a72016-06-01 17:49:30 -0600128loader library to initialize the application's own dispatch table. This allows
Jeff Julianof1619872016-02-17 17:25:42 -0500129an application to fail gracefully if the loader cannot be found, and it
130provides the fastest mechanism for the application to call Vulkan functions. An
131application will only need to query (via system calls such as dlsym()) the
132address of vkGetInstanceProcAddr from the loader library. Using
133vkGetInstanceProcAddr the application can then discover the address of all
134instance and global functions and extensions, such as vkCreateInstance,
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700135vkEnumerateInstanceExtensionProperties and vkEnumerateInstanceLayerProperties
136in a platform independent way.
Jon Ashburnc2972682016-02-08 15:42:01 -0700137
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700138The Vulkan loader library will be distributed in various ways including Vulkan
139SDKs, OS package distributions and IHV driver packages. These details are
140beyond the scope of this document. However, the name and versioning of the
141Vulkan loader library is specified so an app can link to the correct Vulkan ABI
142library version. Vulkan versioning is such that ABI backwards compatibility is
Jeff Julianof1619872016-02-17 17:25:42 -0500143guaranteed for all versions with the same major number (e.g. 1.0 and 1.1). On
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700144Windows, the loader library encodes the ABI version in its name such that
145multiple ABI incompatible versions of the loader can peacefully coexist on a
Mark Young6d026a72016-06-01 17:49:30 -0600146given system. The Vulkan loader library file name is "vulkan-<ABI
147version>.dll". For example, for Vulkan version 1.X on Windows the library
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700148filename is vulkan-1.dll. And this library file can typically be found in the
Mark Young6d026a72016-06-01 17:49:30 -0600149windows/system32 directory (on 64-bit Windows installs, the 32-bit version of
150the loader with the same name can be found in the windows/sysWOW64 directory).
Jon Ashburnc2972682016-02-08 15:42:01 -0700151
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700152For Linux, shared libraries are versioned based on a suffix. Thus, the ABI
153number is not encoded in the base of the library filename as on Windows. On
154Linux an application wanting to link to the latest Vulkan ABI version would
155just link to the name vulkan (libvulkan.so). A specific Vulkan ABI version can
Jeff Julianof1619872016-02-17 17:25:42 -0500156also be linked to by applications (e.g. libvulkan.so.1).
Jon Ashburnc2972682016-02-08 15:42:01 -0700157
Rene Lindsaya0508df2016-10-25 11:05:09 -0600158#### Layer Usage
Mark Young6d026a72016-06-01 17:49:30 -0600159
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700160Applications desiring Vulkan functionality beyond what the core API offers may
Mark Young02ee5382016-07-22 08:51:05 -0600161use various layers or extensions. A layer cannot introduce new Vulkan API
162entry-points not exposed in Vulkan.h, but may offer extensions that do. A
163common use of layers is for API validation which can be enabled by loading the
164layer during application development, but not loading the layer for application
165release. This eliminates the overhead of validating the application's
166usage of the API, something that wasn't available on some previous graphics
167APIs.
168
169Layers discovered by the loader are reported to the application via
170vkEnumerateInstanceLayerProperties. Layers are enabled at vkCreateInstance
171and are active for all Vulkan commands using the given VkInstance and any
172of it's child objects. For example, the ppEnabledLayerNames array in the
173VkInstanceCreateInfo structure is used by the application to list the layer
174names to be enabled at vkCreateInstance. At vkCreateInstance and
175vkCreateDevice, the loader will construct call chains that include the application
176specified (enabled) layers. Order is important in the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700177ppEnabledLayerNames array; array element 0 is the topmost (closest to the
178application) layer inserted in the chain and the last array element is closest
179to the driver.
Jon Ashburnc2972682016-02-08 15:42:01 -0700180
Mark Young02ee5382016-07-22 08:51:05 -0600181**NOTE**: vkCreateDevice originally was able to select layers in a
182similar manner to vkCreateInstance. This lead to the concept of "instance
183layers" and "device layers". It was decided by Khronos to deprecate the
184"device layer" functionality and only consider "instance layers".
185Therefore, vkCreateDevice will use the layers specified at vkCreateInstance.
186Additionally, vkEnumerateDeviceLayerProperties has been deprecated.
187
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700188Developers may want to enable layers that are not enabled by the given
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600189application they are using. On Linux and Windows, the environment variable
Mark Young6d026a72016-06-01 17:49:30 -0600190"VK\_INSTANCE\_LAYERS" can be used to enable
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700191additional layers which are not specified (enabled) by the application at
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600192vkCreateInstance. VK\_INSTANCE\_LAYERS is a colon
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700193(Linux)/semi-colon (Windows) separated list of layer names to enable. Order is
194relevant with the first layer in the list being the topmost layer (closest to
195the application) and the last layer in the list being the bottommost layer
196(closest to the driver).
Jon Ashburnc2972682016-02-08 15:42:01 -0700197
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700198Application specified layers and user specified layers (via environment
199variables) are aggregated and duplicates removed by the loader when enabling
200layers. Layers specified via environment variable are topmost (closest to the
201application) while layers specified by the application are bottommost.
Jon Ashburnc2972682016-02-08 15:42:01 -0700202
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700203An example of using these environment variables to activate the validation
Jon Ashburnc9d7fc92016-05-18 14:07:47 -0600204layer VK\_LAYER\_LUNARG\_parameter\_validation on Windows or Linux is as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700205
206```
Mark Lobodzinski739391a2016-03-17 15:08:18 -0600207> $ export VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_parameter_validation
Jon Ashburnc2972682016-02-08 15:42:01 -0700208```
209
Mark Young6d026a72016-06-01 17:49:30 -0600210#### Implicit vs Explicit Layers
211
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700212Some platforms, including Linux and Windows, support layers which are enabled
213automatically by the loader rather than explicitly by the application (or via
214environment variable). Explicit layers are those layers enabled by the
215application (or environment variable) by providing the layer name. Implicit
216layers are those layers enabled by the loader automatically. Any implicit
217layers the loader discovers on the system in the appropriate location will be
218enabled (subject to environment variable overrides described later). Discovery
219of properly installed implicit and explicit layers is described later.
220Explicitly enabling a layer that is implicitly enabled has no additional
221effect: the layer will still be enabled implicitly by the loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700222
Mark Young02ee5382016-07-22 08:51:05 -0600223Implicit layers have an additional requirement over explicit layers in that they
224require being able to be disabled by an environmental variable. This is due
225to the fact that they are not visible to the application and could cause issues.
226A good principle to keep in mind would be to define both an enable and disable
227environment variable so the users can deterministicly enable the functionality.
228On Desktop platforms (Windows and Linux), these enable/disable settings are
229defined in the layer's JSON file.
230
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700231Extensions are optional functionality provided by a layer, the loader or an
232ICD. Extensions can modify the behavior of the Vulkan API and need to be
233specified and registered with Khronos.
Jon Ashburnc2972682016-02-08 15:42:01 -0700234
Mark Young6d026a72016-06-01 17:49:30 -0600235#### Instance/Device Extensions
236
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700237Instance extensions can be discovered via
238vkEnumerateInstanceExtensionProperties. Device extensions can be discovered via
239vkEnumerateDeviceExtensionProperties. The loader discovers and aggregates all
240extensions from layers (both explicit and implicit), ICDs and the loader before
241reporting them to the application in vkEnumerate\*ExtensionProperties. The
Jeff Julianof1619872016-02-17 17:25:42 -0500242pLayerName parameter in these functions is used to select either a single layer
243or the Vulkan platform implementation. If pLayerName is NULL, extensions from
244Vulkan implementation components (including loader, implicit layers, and ICDs)
245are enumerated. If pLayerName is equal to a discovered layer module name then
246any extensions from that layer (which may be implicit or explicit) are
247enumerated. Duplicate extensions (e.g. an implicit layer and ICD might report
Jon Ashburn859c7fb2016-03-02 17:26:31 -0700248support for the same extension) are eliminated by the loader. For duplicates, the
249ICD version is reported and the layer version is culled. Extensions must
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700250be enabled (in vkCreateInstance or vkCreateDevice) before they can be used.
Jon Ashburnc2972682016-02-08 15:42:01 -0700251
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700252Extension command entry points should be queried via vkGetInstanceProcAddr or
253vkGetDeviceProcAddr. vkGetDeviceProcAddr can only be used to query for device
254extension or core device entry points. Device entry points include any command
255that uses a VkDevice as the first parameter or a dispatchable object that is a
256child of a VkDevice (currently this includes VkQueue and VkCommandBuffer).
257vkGetInstanceProcAddr can be used to query either device or instance extension
258entry points in addition to all core entry points.
Jon Ashburnc2972682016-02-08 15:42:01 -0700259
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700260VkGetDeviceProcAddr is particularly interesting because it will provide the
261most efficient way to call into the ICD. For example, the diagram below shows
262what could happen if the application were to use vkGetDeviceProcAddr for the
Mark Young6d026a72016-06-01 17:49:30 -0600263function "vkGetDeviceQueue" and "vkDestroyDevice" but not "vkAllocateMemory".
264The resulting function pointer (fpGetDeviceQueue) would be the ICD's entry
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700265point if the loader and any enabled layers do not need to see that call. Even
Jeff Julianof1619872016-02-17 17:25:42 -0500266if an enabled layer intercepts the call (e.g. vkDestroyDevice) the loader
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700267trampoline code is skipped for function pointers obtained via
268vkGetDeviceProcAddr. This also means that function pointers obtained via
269vkGetDeviceProcAddr will only work with the specific VkDevice it was created
270for, using it with another device has undefined results. For extensions,
271Get\*ProcAddr will often be the only way to access extension API features.
Jon Ashburnc2972682016-02-08 15:42:01 -0700272
Jon Ashburnc2505562016-02-15 10:19:26 -0700273![Get*ProcAddr efficiency](get_proc_addr.png)
Courtney Goeltzenleuchterab3a4662016-02-14 10:48:22 -0700274
Mark Young78f88c82016-07-19 11:49:45 -0600275##### WSI Extensions
276
277Khronos approved WSI extensions are available and provide Windows System Integration
278support for various execution environments. It is important to understand that some WSI
279extensions are valid for all targets, but others are particular to a given execution
280environment (and loader). This desktop loader (currently targeting Windows and Linux)
281only enables those WSI extensions that are appropriate to the current environment.
282For the most part, the selection is done in the loader using compile-time preprocessor
283flags. All versions of the desktop loader currently expose at least the following WSI
284extension support:
285- VK_KHR_surface
286- VK_KHR_swapchain
287- VK_KHR_display
288
289In addition, each of the following OS targets for the loader support target-specific extensions:
290- **Windows** : VK_KHR_win32_surface
291- **Linux (default)** : VK_KHR_xcb_surface and VK_KHR_xlib_surface
292- **Linux (Wayland build)** : VK_KHR_wayland_surface
293- **Linux (Mir build)** : VK_KHR_mir_surface
294
295**NOTE:** Wayland and Mir targets are not fully supported at this time and should be considered
296alpha quality.
297
298It is important to understand that while the loader may support the various entry-points
299for these extensions, there is a hand-shake required to actually use them:
300* At least one physical device must support the extension(s)
301* The application must select such a physical device
302* The application must request the extension(s) be enabled while creating the instance or logical device (This depends on whether or not the given extension works with an instance or a device).
303* The instance and/or logical device creation must succeed.
304
305Only then can you expect to properly use a WSI extension in your Vulkan program.
306
307##### New Extensions
308
309With the ability to expand Vulkan so easily, extensions will be created that the loader knows
310nothing about. If the extension is a device extension, the loader will pass the unknown
311entry-point down the device call chain ending with the appropriate ICD entry-points.
312However, if the extension is an instance extension, the loader will fail to load it.
313
314*But why doesn't the loader support unknown instance extensions?*
315<br/>
316Let's look again at the Instance call chain:
317![Instance call chain](instance_call_chain.png)
318
319Notice that for a normal instance function call, the loader has to handle passing along the
320function call to the available ICDs. If the loader has no idea of the parameters or return
321value of the instance call, it can't properly pass information along to the ICDs.
322There may be ways to do this, which will be explored in the future. However, for now, this
323loader does not support any unknown instance extensions.
324
325Because the device call-chain does not pass through the loader terminator, this is not
326a problem for device extensions. Instead, device extensions terminate directly in the
327ICD they are associated with.
328
329*Is this a big problem?*
330<br/>
331No! Most extension functionality only affects a device and not an instance or a physical
332device. Thus, the overwhelming majority of extensions will be device extensions rather than
333instance extensions.
334
Mark Youngcb6e6702016-07-20 11:38:53 -0600335
336## Vulkan Installable Client Driver interface with the loader ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700337
338### ICD discovery
339
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700340Vulkan allows multiple drivers each with one or more devices (represented by a
341Vulkan VkPhysicalDevice object) to be used collectively. The loader is
342responsible for discovering available Vulkan ICDs on the system. Given a list
343of available ICDs, the loader can enumerate all the physical devices available
344for an application and return this information to the application. The process
345in which the loader discovers the available Installable Client Drivers (ICDs)
346on a system is platform dependent. Windows, Linux and Android ICD discovery
347details are listed below.
Jon Ashburnc2972682016-02-08 15:42:01 -0700348
349#### Windows
350
351##### Properly-Installed ICDs
352
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700353In order to find properly-installed ICDs, the Vulkan loader will scan the
354values in the following Windows registry key:
Jon Ashburnc2972682016-02-08 15:42:01 -0700355
356HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\Drivers
357
Mark Young02ee5382016-07-22 08:51:05 -0600358On 64-bit Windows, when a 32-bit application is triggered, the loader
359will scan for 32-bit drivers in a separate area of the registry:
360
361HKEY\_LOCAL\_MACHINE\\SOFTWARE\\WOW6432Node\\Khronos\\Vulkan\\Drivers
362
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700363For each value in this key which has DWORD data set to 0, the loader opens the
364JSON format text information file (a.k.a. "manifest file") specified by the
365name of the value. Each name must be a full pathname to the text manifest file.
366The Vulkan loader will open each manifest file to obtain the name or pathname
367of an ICD shared library (".dll") file. For example:
Jon Ashburnc2972682016-02-08 15:42:01 -0700368
Jon Ashburncc300a22016-02-11 14:57:30 -0700369 ```
370 {
Jon Ashburnc2972682016-02-08 15:42:01 -0700371 "file_format_version": "1.0.0",
372 "ICD": {
373 "library_path": "path to ICD library",
Tony Barbourd83f06c2016-03-08 14:50:03 -0700374 "api_version": "1.0.5"
Jon Ashburnc2972682016-02-08 15:42:01 -0700375 }
376 }
Jon Ashburncc300a22016-02-11 14:57:30 -0700377 ```
Jon Ashburnc2972682016-02-08 15:42:01 -0700378
379
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700380The "library\_path" specifies either a filename, a relative pathname, or a full
381pathname to an ICD shared library file, which the loader will attempt to load
382using LoadLibrary(). If the ICD is specified via a filename, the shared library
David Pinedo3e163ee2016-04-18 16:59:59 -0600383lives in the system's DLL search path (e.g. in the "C:\Windows\System32"
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700384folder). If the ICD is specified via a relative pathname, it is relative to the
385path of the manifest file. Relative pathnames are those that do not start with
386a drive specifier (e.g. "C:"), nor with a directory separator (i.e. the '\\'
387character), but do contain at least one directory separator.
Jon Ashburnc2972682016-02-08 15:42:01 -0700388
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700389The "file\_format\_version" specifies a major.minor.patch version number in
390case the format of the text information file changes in the future. If the same
391ICD shared library supports multiple, incompatible versions of text manifest
Mark Young02ee5382016-07-22 08:51:05 -0600392file format versions, it must have separate JSON files for each (all of which may
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700393point to the same shared library).
Jon Ashburnc2972682016-02-08 15:42:01 -0700394
Mark Young6d026a72016-06-01 17:49:30 -0600395The "api\_version" specifies the major.minor.patch version number of the Vulkan
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700396API that the shared library (referenced by "library\_path") was built with.
Jon Ashburnc2972682016-02-08 15:42:01 -0700397
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700398There are no rules about the name of the text information files (except the
399.json suffix).
Jon Ashburnc2972682016-02-08 15:42:01 -0700400
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700401There are no rules about the name of the ICD shared library files. For example,
402if the registry contains the following values,
Jon Ashburnc2972682016-02-08 15:42:01 -0700403
Jon Ashburncc300a22016-02-11 14:57:30 -0700404```
405[HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers\]
Jon Ashburnc2972682016-02-08 15:42:01 -0700406
David Pinedo3e163ee2016-04-18 16:59:59 -0600407"C:\vendor a\vk_vendora.json"=dword:00000000
Jon Ashburnc2972682016-02-08 15:42:01 -0700408
David Pinedo3e163ee2016-04-18 16:59:59 -0600409"C:\windows\system32\vendorb_vk.json"=dword:00000000
Jon Ashburnc2972682016-02-08 15:42:01 -0700410
David Pinedo3e163ee2016-04-18 16:59:59 -0600411"C:\windows\system32\vendorc_icd.json"=dword:00000000
Jon Ashburncc300a22016-02-11 14:57:30 -0700412```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700413then the loader will open the following text information files, with the
414specified contents:
Jon Ashburnc2972682016-02-08 15:42:01 -0700415
Jon Ashburncc300a22016-02-11 14:57:30 -0700416| Text File Name | Text File Contents |
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700417|----------------|--------------------|
David Pinedo3e163ee2016-04-18 16:59:59 -0600418|vk\_vendora.json | "ICD": { "library\_path": "C:\VENDOR A\vk_vendora.dll", "api_version": "1.0.5" } |
Tony Barbourd83f06c2016-03-08 14:50:03 -0700419| vendorb\_vk.json | "ICD": { "library\_path": "vendorb\_vk.dll", "api_version": "1.0.5" } |
420|vendorc\_icd.json | "ICD": { "library\_path": "vedorc\_icd.dll", "api_version": "1.0.5" }|
Jon Ashburnc2972682016-02-08 15:42:01 -0700421
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700422Then the loader will open the three files mentioned in the "Text File Contents"
423column, and then try to load and use the three shared libraries indicated by
424the ICD.library\_path value.
Jon Ashburnc2972682016-02-08 15:42:01 -0700425
426##### Using Pre-Production ICDs
427
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700428IHV developers (and sometimes other developers) need to use special,
429pre-production ICDs. In some cases, a pre-production ICD may be in an
430installable package. In other cases, a pre-production ICD may simply be a
431shared library in the developer's build tree. In this latter case, we want to
432allow developers to point to such an ICD without modifying the
433properly-installed ICD(s) on their system.
Jon Ashburnc2972682016-02-08 15:42:01 -0700434
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700435This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
436which will override the mechanism used for finding properly-installed ICDs. In
437other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used. The
438"VK\_ICD\_FILENAMES" environment variable is a semi-colon-separated list of ICD
439text information files (aka manifest files), containing the following:
Jon Ashburnc2972682016-02-08 15:42:01 -0700440
Jon Ashburncc300a22016-02-11 14:57:30 -0700441- A full pathname (e.g. "C:\\my\_build\\my\_icd.json")
Jon Ashburnc2972682016-02-08 15:42:01 -0700442
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700443Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
444file for a developer-built ICD. A semi-colon is only used if more than one ICD
445is listed.
Jon Ashburnc2972682016-02-08 15:42:01 -0700446
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700447For example, if a developer wants to refer to one ICD that they built, they
448could set the "VK\_ICD\_FILENAMES" environment variable to:
Jon Ashburnc2972682016-02-08 15:42:01 -0700449
Jon Ashburncc300a22016-02-11 14:57:30 -0700450C:\\my\_build\\my\_icd.json
Jon Ashburnc2972682016-02-08 15:42:01 -0700451
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700452If a developer wants to refer to two ICDs, one of which is a properly-installed
453ICD, they can use the full pathname of the text file:
Jon Ashburnc2972682016-02-08 15:42:01 -0700454
Jon Ashburncc300a22016-02-11 14:57:30 -0700455C:\\Windows\\System32\\vendorc\_icd.json;C:\\my\_build\\my\_icd.json
Jon Ashburnc2972682016-02-08 15:42:01 -0700456
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700457Notice the semi-colon between "C:\\Windows\\System32\\vendorc\_icd.json" and
458"C:\\my\_build\\my\_icd.json".
Jon Ashburnc2972682016-02-08 15:42:01 -0700459
460#### Linux
461
462##### Properly-Installed ICDs
463
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700464In order to find properly-installed ICDs, the Vulkan loader will scan the files
465in the following Linux directories:
Jon Ashburnc2972682016-02-08 15:42:01 -0700466
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600467 /usr/local/etc/vulkan/icd.d
468 /usr/local/share/vulkan/icd.d
469 /etc/vulkan/icd.d
470 /usr/share/vulkan/icd.d
471 $HOME/.local/share/vulkan/icd.d
472
473The "/usr/local/*" directories can be configured to be other directories at build time.
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700474
475Where $HOME is the current home directory of the application's user id; this
476path will be ignored for suid programs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700477
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700478These directories will contain text information files (a.k.a. "manifest
479files"), that use a JSON format.
Jon Ashburnc2972682016-02-08 15:42:01 -0700480
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700481The Vulkan loader will open each manifest file found to obtain the name or
482pathname of an ICD shared library (".so") file. For example:
Jon Ashburnc2972682016-02-08 15:42:01 -0700483
Jon Ashburncc300a22016-02-11 14:57:30 -0700484```
485{
Jon Ashburnc2972682016-02-08 15:42:01 -0700486 "file_format_version": "1.0.0",
487 "ICD": {
488 "library_path": "path to ICD library",
Tony Barbourd83f06c2016-03-08 14:50:03 -0700489 "api_version": "1.0.5"
Jon Ashburnc2972682016-02-08 15:42:01 -0700490 }
491}
Jon Ashburncc300a22016-02-11 14:57:30 -0700492```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700493The "library\_path" specifies either a filename, a relative pathname, or a full
494pathname to an ICD shared library file. If the ICD is specified via a filename,
495the loader will attempt to open that file as a shared object using dlopen(),
496and the file must be in a directory that dlopen is configured to look in (Note:
497various distributions are configured differently). A distribution is free to
498create Vulkan-specific system directories (e.g. ".../vulkan/icd"), but is not
499required to do so. If the ICD is specified via a relative pathname, it is
500relative to the path of the info file. Relative pathnames are those that do not
501start with, but do contain at least one directory separator (i.e. the '/'
502character). For example, "lib/vendora.so" and "./vendora.so" are examples of
503relative pathnames.
Jon Ashburnc2972682016-02-08 15:42:01 -0700504
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700505The "file\_format\_version" provides a major.minor.patch version number in case
506the format of the manifest file changes in the future. If the same ICD shared
507library supports multiple, incompatible versions of manifest file format
508versions, it must have multiple manifest files (all of which may point to the
509same shared library).
Jon Ashburnc2972682016-02-08 15:42:01 -0700510
Mark Young6d026a72016-06-01 17:49:30 -0600511The "api\_version" specifies the major.minor.patch version number of the Vulkan
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700512API that the shared library (referenced by "library\_path") was built with.
Jon Ashburnc2972682016-02-08 15:42:01 -0700513
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600514The "/usr/local/etc/vulkan/icd.d" and "/usr/local/share/vulkan/icd.d"
515directories are for locally-built ICDs.
516
517The "/etc/vulkan/icd.d" directory is for
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700518ICDs that are installed from non-Linux-distribution-provided packages.
Jon Ashburnc2972682016-02-08 15:42:01 -0700519
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600520The "/usr/share/vulkan/icd.d" directory is for ICDs that are installed from
521Linux-distribution-provided packages.
522
Jon Ashburnc2972682016-02-08 15:42:01 -0700523There are no rules about the name of the text files (except the .json suffix).
524
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700525There are no rules about the name of the ICD shared library files. For example,
526if the "/usr/share/vulkan/icd.d" directory contain the following files, with
527the specified contents:
Jon Ashburnc2972682016-02-08 15:42:01 -0700528
Jon Ashburn26ed3f32016-02-14 21:54:52 -0700529| Text File Name | Text File Contents |
530|-------------------|------------------------|
Tony Barbourd83f06c2016-03-08 14:50:03 -0700531| vk\_vendora.json | "ICD": { "library\_path": "vendora.so", "api_version": "1.0.5" } |
532| vendorb\_vk.json | "ICD": { "library\_path": "vendorb\_vulkan\_icd.so", "api_version": "1.0.5" } |
533| vendorc\_icd.json | "ICD": { "library\_path": "/usr/lib/VENDORC/icd.so", "api_version": "1.0.5" } |
Jon Ashburnc2972682016-02-08 15:42:01 -0700534
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700535then the loader will open the three files mentioned in the "Text File Contents"
536column, and then try to load and use the three shared libraries indicated by
537the ICD.library\_path value.
Jon Ashburnc2972682016-02-08 15:42:01 -0700538
539##### Using Pre-Production ICDs
540
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700541IHV developers (and sometimes other developers) need to use special,
542pre-production ICDs. In some cases, a pre-production ICD may be in an
543installable package. In other cases, a pre-production ICD may simply be a
544shared library in the developer's build tree. In this latter case, we want to
545allow developers to point to such an ICD without modifying the
546properly-installed ICD(s) on their system.
Jon Ashburnc2972682016-02-08 15:42:01 -0700547
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700548This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
549which will override the mechanism used for finding properly-installed ICDs. In
550other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used.
Jon Ashburnc2972682016-02-08 15:42:01 -0700551
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700552The "VK\_ICD\_FILENAMES" environment variable is a colon-separated list of ICD
553manifest files, containing the following:
Jon Ashburnc2972682016-02-08 15:42:01 -0700554
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600555- A filename (e.g. "libvkicd.json") in the
556"/usr/local/etc/vulkan/icd.d",
557"/usr/local/share/vulkan/icd.d",
558"/etc/vulkan/icd.d",
559"/usr/share/vulkan/icd.d",
560"$HOME/.local/share/vulkan/icd.d"
561directories
Jon Ashburnc2972682016-02-08 15:42:01 -0700562
563- A full pathname (e.g. "/my\_build/my\_icd.json")
564
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700565Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
566file for a developer-built ICD. A colon is only used if more than one ICD is
567listed.
Jon Ashburnc2972682016-02-08 15:42:01 -0700568
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700569For example, if a developer wants to refer to one ICD that they built, they
570could set the "VK\_ICD\_FILENAMES" environment variable to:
Jon Ashburnc2972682016-02-08 15:42:01 -0700571
572/my\_build/my\_icd.json
573
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700574If a developer wants to refer to two ICDs, one of which is a properly-installed
575ICD, they can use the name of the text file in the system directory:
Jon Ashburnc2972682016-02-08 15:42:01 -0700576
577vendorc\_vulkan.json:/my\_build/my\_icd.json
578
579Notice the colon between "vendorc\_vulkan.json" and "/my\_build/my\_icd.json".
580
581NOTE: this environment variable will be ignored for suid programs.
582
583#### Android
584
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700585The Android loader lives in the system library folder. The location cannot be
586changed. The loader will load the driver/ICD via hw_get_module with the ID
587of "vulkan". Due to security policies in Android none of this can be modified
588under normal use.
Jon Ashburnc2972682016-02-08 15:42:01 -0700589
Mark Youngcb6e6702016-07-20 11:38:53 -0600590<br/>
Jon Ashburnc2972682016-02-08 15:42:01 -0700591
Mark Youngcb6e6702016-07-20 11:38:53 -0600592## ICD interface requirements ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700593
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700594Generally, for all Vulkan commands issued by an application, the loader can be
Mark Young6d026a72016-06-01 17:49:30 -0600595viewed as a pass through. That is, the loader generally doesn't modify the
Jon Ashburn54791f62016-04-22 14:40:07 -0600596commands or their parameters, but simply calls the ICDs entry point for that
597command. There are specific additional interface requirements an ICD needs to comply with that
598are over and above any requirements from the Vulkan specification including WSI extension specification.
599These addtional requirements are versioned to allow flexibility in the future.
600These interface requirements will be set forth in the following sections: 1) describing
601which "loader-ICD" interface version is available, 2) detailing the most recent interface version;
6023) the supported, older interface requirements will be described as differences
603from the most recent interface version.
Jon Ashburnc2972682016-02-08 15:42:01 -0700604
605#### Windows and Linux
606
Jon Ashburn54791f62016-04-22 14:40:07 -0600607##### Version Negotiation Between Loader and ICDs
Jon Ashburnc2972682016-02-08 15:42:01 -0700608
Mark Younga7c51fd2016-09-16 10:18:42 -0600609All ICDs (supporting interface version 2 or higher) must export the following
Jon Ashburn54791f62016-04-22 14:40:07 -0600610function that is used for determination of the interface version that will be used.
611This entry point is not a part of the Vulkan API itself, only a private interface
612between the loader and ICDs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700613
Jon Ashburn54791f62016-04-22 14:40:07 -0600614VKAPI_ATTR VkResult VKAPI_CALL vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
Jon Ashburnc2972682016-02-08 15:42:01 -0700615
Jon Ashburn54791f62016-04-22 14:40:07 -0600616This entry point reports the "loader-ICD" interface version supported by both the loader and the ICD.
617The loader informs the ICD of it's desired interface version (typically the latest) via the
618pSupportedVersion parameter.
619This call is the first call made by the loader into the ICD (prior to any calls to
620vk\_icdGetInstanceProcAddr).
Jon Ashburnc2972682016-02-08 15:42:01 -0700621
Jon Ashburn54791f62016-04-22 14:40:07 -0600622If a loader sees that an ICD does not export this symbol it knows that it's dealing
623with a legacy ICD supporting either interface version 0 or 1.
624Similarly, if an ICD sees a call to vk\_icdGetInstanceProcAddr before a call to
625vk_icdGetLoaderICDInterfaceVersion then it knows that it's dealing with a legacy loader
626supporting version 0 or 1.
Mark Young02ee5382016-07-22 08:51:05 -0600627**Note** if the loader calls vk\_icdGetInstanceProcAddr first it supports at least version 1,
Jon Ashburn54791f62016-04-22 14:40:07 -0600628otherwise the loader only supports version 0.
Jon Ashburnc2972682016-02-08 15:42:01 -0700629
Jon Ashburn54791f62016-04-22 14:40:07 -0600630The pSupportedVersion parameter is both an input and output parameter.
631It 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 -0500632
Jon Ashburn54791f62016-04-22 14:40:07 -0600633If the ICD receiving the call no longer supports the interface version provided
634by the loader (due to deprecation) then it can report VK_ERROR_INCOMPATIBLE_DRIVER error,
635otherwise it sets the value pointed by pSupportedVersion to the latest interface
636version supported by both the ICD and the loader and returns VK_SUCCESS.
637The ICD should report VK_SUCCESS in case the loader provided interface version
638is newer than that supported by the ICD, as it's the loader's responsibility to
639determine whether it can support the older interface version supported by the ICD.
640The ICD should also report VK_SUCCESS in the case it's interface version is greater
641than the loader's, but return the loader's version. Thus, upon return of VK_SUCCESS
642the pSupportedVersion will contain the desired interface version to be used by the ICD.
Jon Ashburnc2972682016-02-08 15:42:01 -0700643
Jon Ashburn54791f62016-04-22 14:40:07 -0600644If the loader receives back an interface version from the ICD that the loader no longer
645supports (due to deprecation) or it receives a VK_ERROR_INCOMPATIBLE_DRIVER error
646instead of VK_SUCCESS then the loader will treat the ICD as incompatible
647and will not load it for use. In this case the application will not see the ICDs vkPhysicalDevice
648during enumeration.
Jon Ashburnc2972682016-02-08 15:42:01 -0700649
Mark Younga7c51fd2016-09-16 10:18:42 -0600650##### Loader Version 3 Interface Changes
651
652The primary change occuring in version 3 of the loader/ICD interface is to allow an ICD to
653handle Creation/Destruction of their own KHR_surfaces. Up until this point, the loader created
654a surface object that was used by all ICDs. However, some ICDs may want to provide their
655own surface handles. If an ICD chooses to enable this support, they must export support for
656version 3 of the Loader/ICD interface as well as any Vulkan command that uses a KHR_surface handle,
657such as:
658- vkCreateXXXSurfaceKHR (where XXX is the platform specific identifier [i.e.CreateWin32SurfaceKHR for Windows])
659- vkDestroySurfaceKHR
660- vkCreateSwapchainKHR
661- vkGetPhysicalDeviceSurfaceSupportKHR
662- vkGetPhysicalDeviceSurfaceCapabilitiesKHR
663- vkGetPhysicalDeviceSurfaceFormatsKHR
664- vkGetPhysicalDeviceSurfacePresentModesKHR
665
666An ICD can still choose to not take advantage of this functionality by simply not exposing the
667above the vkCreateXXXSurfaceKHR and vkDestroySurfaceKHR commands.
668
Jon Ashburn54791f62016-04-22 14:40:07 -0600669##### Loader Version 2 Interface Requirements
Jon Ashburnc2972682016-02-08 15:42:01 -0700670
Mark Younga7c51fd2016-09-16 10:18:42 -0600671Version 2 interface has requirements in three areas:
672 1. ICD Vulkan entry point discovery,
673 2. KHR_surface related requirements in the WSI extensions,
674 3. Vulkan dispatchable object creation requirements.
Jon Ashburnc2972682016-02-08 15:42:01 -0700675
Jon Ashburn54791f62016-04-22 14:40:07 -0600676###### ICD Vulkan entry point discovery
677All ICDs must export the following function that is used for discovery of ICD Vulkan entry points.
678This 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 -0700679
Jon Ashburn54791f62016-04-22 14:40:07 -0600680VKAPI\_ATTR PFN\_vkVoidFunction VKAPI\_CALL vk\_icdGetInstanceProcAddr(VkInstance instance, const char* pName);
681
682This function has very similar semantics to the Vulkan command vkGetInstanceProcAddr.
683vk\_icdGetInstanceProcAddr returns valid function pointers for all the global level
684and instance level Vulkan commands, and also for vkGetDeviceProcAddr.
685Global level commands are those
686which contain no dispatchable object as the first parameter, such as
687vkCreateInstance and vkEnumerateInstanceExtensionProperties. The ICD must
688support querying global level entry points by calling
689vk\_icdGetInstanceProcAddr with a NULL VkInstance parameter. Instance level
690commands are those that have either VkInstance, or VkPhysicalDevice as the
691first parameter dispatchable object. Both core entry points and any instance
692extension entry points the ICD supports should be available via
693vk\_icdGetInstanceProcAddr. Future Vulkan instance extensions may define and
694use new instance level dispatchable objects other than VkInstance and
695VkPhysicalDevice, in which case extension entry points using these newly
696defined dispatchable objects must be queryable via vk\_icdGetInstanceProcAddr.
697
698All other Vulkan entry points must either NOT be exported from the ICD
699library or else NOT use the official Vulkan function names if they are
700exported. This requirement is for ICD libraries that include other
701functionality (such as OpenGL library) and thus could be loaded by the
702application prior to when the Vulkan loader library is loaded by the
703application. In other words, the ICD library exported Vulkan symbols must not
704clash with the loader's exported Vulkan symbols.
705
706Beware of interposing by dynamic OS library loaders if the official Vulkan
707names are used. On Linux, if official names are used, the ICD library must be
708linked with -Bsymbolic.
709
710###### Handling KHR_surface objects in the WSI extensions
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700711Normally, ICDs handle object creation and destruction for various Vulkan
712objects. The WSI surface extensions for Linux and Windows
713(VK\_KHR\_win32\_surface, VK\_KHR\_xcb\_surface, VK\_KHR\_xlib\_surface,
714VK\_KHR\_mir\_surface, VK\_KHR\_wayland\_surface, and VK\_KHR\_surface) are
715handled differently. For these extensions, the VkSurfaceKHR object creation and
716destruction is handled by the loader as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700717
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007181. Loader handles the vkCreate\*SurfaceKHR() and vkDestroySurfaceKHR()
719 functions including creating/destroying the VkSurfaceKHR object.
Jon Ashburnc2972682016-02-08 15:42:01 -0700720
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007212. VkSurfaceKHR objects have the underlying structure (VkIcdSurface\*) as
722 defined in include/vulkan/vk\_icd.h.
Jon Ashburnc2972682016-02-08 15:42:01 -0700723
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007243. ICDs can cast any VkSurfaceKHR object to a pointer to the appropriate
725 VkIcdSurface\* structure.
Jon Ashburnc2972682016-02-08 15:42:01 -0700726
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007274. VkIcdSurface\* structures include VkIcdSurfaceWin32, VkIcdSurfaceXcb,
Jeff Julianof1619872016-02-17 17:25:42 -0500728 VkIcdSurfaceXlib, VkIcdSurfaceMir, and VkIcdSurfaceWayland. The first field
729 in the structure is a VkIcdSurfaceBase enumerant that indicates whether the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700730 surface object is Win32, Xcb, Xlib, Mir, or Wayland.
Jon Ashburnc2972682016-02-08 15:42:01 -0700731
Jon Ashburn54791f62016-04-22 14:40:07 -0600732###### ICD dispatchable object creation
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700733As previously covered, the loader requires dispatch tables to be accessible
734within Vulkan dispatchable objects, which include VkInstance, VkPhysicalDevice,
735VkDevice, VkQueue, and VkCommandBuffer. The specific requirements on all
736dispatchable objects created by ICDs are as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700737
Jon Ashburncc300a22016-02-11 14:57:30 -0700738- All dispatchable objects created by an ICD can be cast to void \*\*
Jon Ashburnc2972682016-02-08 15:42:01 -0700739
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700740- The loader will replace the first entry with a pointer to the dispatch table
741 which is owned by the loader. This implies three things for ICD drivers:
Jon Ashburnc2972682016-02-08 15:42:01 -0700742
Jon Ashburncc300a22016-02-11 14:57:30 -07007431. The ICD must return a pointer for the opaque dispatchable object handle.
Jon Ashburnc2972682016-02-08 15:42:01 -0700744
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007452. This pointer points to a regular C structure with the first entry being a
746 pointer. Note: for any C\++ ICD's that implement VK objects directly as C\++
747 classes. The C\++ compiler may put a vtable at offset zero if your class is
Jeff Julianof1619872016-02-17 17:25:42 -0500748 non-POD due to the use of a virtual function. In this case use a regular C
749 structure (see below).
Jon Ashburnc2972682016-02-08 15:42:01 -0700750
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007513. The loader checks for a magic value (ICD\_LOADER\_MAGIC) in all the created
752 dispatchable objects, as follows (see include/vulkan/vk\_icd.h):
Jon Ashburnc2972682016-02-08 15:42:01 -0700753
754```
755
Jon Ashburncc300a22016-02-11 14:57:30 -0700756#include "vk_icd.h"
Jon Ashburnc2972682016-02-08 15:42:01 -0700757
Jon Ashburncc300a22016-02-11 14:57:30 -0700758union _VK_LOADER_DATA {
759 uintptr loadermagic;
760 void *loaderData;
761} VK_LOADER_DATA;
Jon Ashburnc2972682016-02-08 15:42:01 -0700762
Jon Ashburncc300a22016-02-11 14:57:30 -0700763vkObj alloc_icd_obj()
Jon Ashburnc2972682016-02-08 15:42:01 -0700764{
Jon Ashburncc300a22016-02-11 14:57:30 -0700765 vkObj *newObj = alloc_obj();
766 ...
767 // Initialize pointer to loader's dispatch table with ICD_LOADER_MAGIC
Jon Ashburnc2972682016-02-08 15:42:01 -0700768
Jon Ashburncc300a22016-02-11 14:57:30 -0700769 set_loader_magic_value(newObj);
770 ...
771 return newObj;
Jon Ashburnc2972682016-02-08 15:42:01 -0700772}
Jon Ashburnc2972682016-02-08 15:42:01 -0700773```
774
Jon Ashburn54791f62016-04-22 14:40:07 -0600775##### Loader Version 0 and 1 Interface Differences
776
777Version 0 and 1 interfaces do not support version negotiation via vk\_icdNegotiateLoaderICDInterfaceVersion.
778ICDs can distinguish version 0 and version 1 interfaces as follows:
779if the loader calls vk\_icdGetInstanceProcAddr first it supports version 1,
780otherwise the loader only supports version 0.
781
782Version 0 interface does not support vk\_icdGetInstanceProcAddr. Version 0 interface requirements for
783obtaining ICD Vulkan entry points are as follows:
784
785- vkGetInstanceProcAddr exported in the ICD library and returns valid function
786 pointers for all the Vulkan API entry points;
787
788- vkCreateInstance exported in the ICD library;
789
790- vkEnumerateInstanceExtensionProperties exported in the ICD library;
791
792
Jon Ashburnc2972682016-02-08 15:42:01 -0700793Additional Notes:
794
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700795- The loader will filter out extensions requested in vkCreateInstance and
796vkCreateDevice before calling into the ICD; Filtering will be of extensions
Jeff Julianof1619872016-02-17 17:25:42 -0500797advertised by entities (e.g. layers) different from the ICD in question.
798- The loader will not call the ICD for vkEnumerate\*LayerProperties() as layer
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700799properties are obtained from the layer libraries and layer JSON files.
800- If an ICD library wants to implement a layer it can do so by having the
801appropriate layer JSON manifest file refer to the ICD library file.
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700802- The loader will not call the ICD for
803 vkEnumerate\*ExtensionProperties(pLayerName != NULL).
Jon Ashburn2b2f6182016-04-04 16:37:37 -0600804- ICDs creating new dispatchable objects via device extensions need to initialize
Jon Ashburn54791f62016-04-22 14:40:07 -0600805the created dispatchable object. The loader has generic trampoline code for unknown
Jon Ashburn2b2f6182016-04-04 16:37:37 -0600806device extensions. This generic trampoline code doesn't initialize the dispatch table within
Jon Ashburn54791f62016-04-22 14:40:07 -0600807the newly created object. See the section for more information on how to initialize created
808dispatchable objects for extensions non known by the loader. [layer link](#creating-new-dispatchable-objects)
Jeff Julianof1619872016-02-17 17:25:42 -0500809
Jon Ashburnc2972682016-02-08 15:42:01 -0700810#### Android
811
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700812The Android loader uses the same protocol for initializing the dispatch
813table as described above. The only difference is that the Android
814loader queries layer and extension information directly from the
815respective libraries and does not use the json manifest files used
816by the Windows and Linux loaders.
Jon Ashburnc2972682016-02-08 15:42:01 -0700817
Mark Youngcb6e6702016-07-20 11:38:53 -0600818<br/>
819
820## Vulkan layer interface with the loader ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700821
822### Layer discovery
823
824#### Windows
825
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -0600826<a name="ManifestFileExample"></a>
Jon Ashburnc2972682016-02-08 15:42:01 -0700827##### Properly-Installed Layers
828
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700829In order to find properly-installed layers, the Vulkan loader will use a
830similar mechanism as used for ICDs. Text information files (aka manifest
831files), that use a JSON format, are read in order to identify the names and
832attributes of layers and their extensions. The use of manifest files allows the
833loader to avoid loading any shared library files when the application does not
834query nor request any extensions. Layers and extensions have additional
835complexity, and so their manifest files contain more information than ICD info
836files. For example, a layer shared library file may contain multiple
837layers/extensions (perhaps even an ICD).
Jon Ashburnc2972682016-02-08 15:42:01 -0700838
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700839In order to find properly-installed layers, the Vulkan loader will scan the
840values in the following Windows registry keys:
Jon Ashburnc2972682016-02-08 15:42:01 -0700841
842HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers
843
844HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers
845
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700846Explicit layers are those which are enabled by an application (e.g. with the
847vkCreateInstance function), or by an environment variable (as mentioned
848previously).
Jon Ashburnc2972682016-02-08 15:42:01 -0700849
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700850Implicit layers are those which are enabled by their existence. For example,
851certain application environments (e.g. Steam or an automotive infotainment
852system) may have layers which they always want enabled for all applications
853that they start. Other implicit layers may be for all applications started on a
854given system (e.g. layers that overlay frames-per-second). Implicit layers are
855enabled automatically, whereas explicit layers must be enabled explicitly. What
856distinguishes a layer as implicit or explicit is by which registry key its
857layer information file is referenced by.
Jon Ashburnc2972682016-02-08 15:42:01 -0700858
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700859For each value in these keys which has DWORD data set to 0, the loader opens
860the JSON manifest file specified by the name of the value. Each name must be a
861full pathname to the manifest file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700862
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700863The Vulkan loader will open each info file to obtain information about the
864layer, including the name or pathname of a shared library (".dll") file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700865
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -0600866This manifest file is in the JSON format as shown in the following example.
867See the section [Layer Library Manifest File](#LayerLibraryManifestFile) for more information about each of the nodes in the JSON file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700868
Jon Ashburncc300a22016-02-11 14:57:30 -0700869```
Jon Ashburnc2972682016-02-08 15:42:01 -0700870{
Mark Youngc3a6d2e2016-06-13 14:49:53 -0600871 "file_format_version" : "1.0.0",
872 "layer": {
873 "name": "VK_LAYER_LUNARG_overlay",
874 "type": "INSTANCE",
875 "library_path": "vkOverlayLayer.dll"
876 "api_version" : "1.0.5",
877 "implementation_version" : "2",
878 "description" : "LunarG HUD layer",
879 "functions": {
880 "vkGetInstanceProcAddr": "OverlayLayer_GetInstanceProcAddr",
881 "vkGetDeviceProcAddr": "OverlayLayer_GetDeviceProcAddr"
882 },
883 "instance_extensions": [
884 {
885 "name": "VK_EXT_debug_report",
886 "spec_version": "1"
887 },
888 {
889 "name": "VK_VENDOR_ext_x",
890 "spec_version": "3"
891 }
892 ],
893 "device_extensions": [
894 {
895 "name": "VK_EXT_debug_marker",
896 "spec_version": "1",
897 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
898 }
899 ],
900 "enable_environment": {
901 "ENABLE_LAYER_OVERLAY_1": "1"
902 }
903 "disable_environment": {
904 "DISABLE_LAYER_OVERLAY_1": ""
905 }
906 }
Jon Ashburnc2972682016-02-08 15:42:01 -0700907}
Jon Ashburncc300a22016-02-11 14:57:30 -0700908```
Jon Ashburnc2972682016-02-08 15:42:01 -0700909
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700910The "library\_path" specifies either a filename, a relative pathname, or a full
911pathname to a layer shared library (".dll") file, which the loader will attempt
912to load using LoadLibrary(). If the layer is specified via a relative pathname,
913it is relative to the path of the info file (e.g. for cases when an application
914provides a layer that is in the same folder hierarchy as the rest of the
915application files). If the layer is specified via a filename, the shared
916library lives in the system's DLL search path (e.g. in the
917"C:\\Windows\\System32" folder).
Jon Ashburnc2972682016-02-08 15:42:01 -0700918
Mark Youngc3a6d2e2016-06-13 14:49:53 -0600919If defining multiple layers in a single JSON file prior to "file\_format\_version"
9201.0.1, you would simply define multiple "layer" objects. However, this is not
921valid JSON syntax. Instead, you should now define "file\_format\_version"
9221.0.1 (or newer) and use the new "layers" array object as seen in the
923following example:
924
925```
926{
927 "file_format_version" : "1.0.1",
928 "layers": [
929 {
930 "name": "VK_LAYER_layer_name1",
931 "type": "INSTANCE",
932 ...
933 },
934 {
935 "name": "VK_LAYER_layer_name2",
936 "type": "INSTANCE",
937 ...
938 }
939 ]
940}
941```
942
943You could use the "layers" array object to define a single layer, as long as
944your "file\_format\_version" is defined to at least 1.0.1. It is functionally the
945same as using a single "layer" object.
946
Jon Ashburnc2972682016-02-08 15:42:01 -0700947There are no rules about the name of the text files (except the .json suffix).
948
949There are no rules about the name of the layer shared library files.
950
951##### Using Pre-Production Layers
952
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700953As with ICDs, developers may need to use special, pre-production layers,
954without modifying the properly-installed layers. This need is met with the use
955of the "VK\_LAYER\_PATH" environment variable, which will override the
956mechanism using for finding properly-installed layers. Because many layers may
957exist on a system, this environment variable is a semi-colon-separated list of
958folders that contain layer info files. Only the folder listed in
959"VK\_LAYER\_PATH" will be scanned for info files. Each semi-colon-separated
960entry is:
Jon Ashburnc2972682016-02-08 15:42:01 -0700961
962- The full pathname of a folder containing layer info files
963
964#### Linux
965
966##### Properly-Installed Layers
967
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700968In order to find properly-installed layers, the Vulkan loader will use a
969similar mechanism as used for ICDs. Text information files, that use a JSON
970format, are read in order to identify the names and attributes of layers and
971their extensions. The use of text info files allows the loader to avoid loading
972any shared library files when the application does not query nor request any
973extensions. Layers and extensions have additional complexity, and so their info
974files contain more information than ICD info files. For example, a layer shared
975library file may contain multiple layers/extensions (perhaps even an ICD).
Jon Ashburnc2972682016-02-08 15:42:01 -0700976
977The Vulkan loader will scan the files in the following Linux directories:
978
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600979 /usr/local/etc/vulkan/explicit_layer.d
980 /usr/local/etc/vulkan/implicit_layer.d
981 /usr/local/share/vulkan/explicit_layer.d
982 /usr/local/share/vulkan/implicit_layer.d
983 /etc/vulkan/explicit_layer.d
984 /etc/vulkan/implicit_layer.d
985 /usr/share/vulkan/explicit_layer.d
986 /usr/share/vulkan/implicit_layer.d
987 $HOME/.local/share/vulkan/explicit_layer.d
988 $HOME/.local/share/vulkan/implicit_layer.d
989
990The "/usr/local/*" directories can be configured to be other directories at build time.
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700991
992Where $HOME is the current home directory of the application's user id; this
993path will be ignored for suid programs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700994
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700995Explicit layers are those which are enabled by an application (e.g. with the
996vkCreateInstance function), or by an environment variable (as mentioned
997previously). Implicit layers are those which are enabled by their existence.
998For example, certain application environments (e.g. Steam or an automotive
999infotainment system) may have layers which they always want enabled for all
1000applications that they start. Other implicit layers may be for all applications
1001started on a given system (e.g. layers that overlay frames-per-second).
1002Implicit layers are enabled automatically, whereas explicit layers must be
1003enabled explicitly. What distinguishes a layer as implicit or explicit is by
1004which directory its layer information file exists in.
Jon Ashburnc2972682016-02-08 15:42:01 -07001005
Karl Schultz1f58d7e2016-10-31 15:58:21 -06001006The "/usr/local/etc/vulkan/\*\_layer.d" and "/usr/local/share/vulkan/\*\_layer.d"
1007directories are for layers that are installed from locally-built sources.
1008
1009The "/etc/vulkan/\*\_layer.d" directories are for layers that are installed from
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001010non-Linux-distribution-provided packages.
Jon Ashburnc2972682016-02-08 15:42:01 -07001011
Karl Schultz1f58d7e2016-10-31 15:58:21 -06001012The "/usr/share/vulkan/\*\_layer.d" directories are for layers that are
1013installed from Linux-distribution-provided packages.
1014
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001015This manifest file is in the JSON format as shown in the following example.
1016See the section [Layer Library Manifest File](#LayerLibraryManifestFile) for more information about each of the nodes in the JSON file.
Jon Ashburnc2972682016-02-08 15:42:01 -07001017
Jon Ashburncc300a22016-02-11 14:57:30 -07001018```
Jon Ashburnc2972682016-02-08 15:42:01 -07001019{
Mark Youngc3a6d2e2016-06-13 14:49:53 -06001020 "file_format_version" : "1.0.0",
1021 "layer": {
1022 "name": "VK_LAYER_LUNARG_overlay",
1023 "type": "INSTANCE",
1024 "library_path": "libvkOverlayLayer.so"
1025 "api_version" : "1.0.5",
1026 "implementation_version" : "2",
1027 "description" : "LunarG HUD layer",
1028 "functions": {
1029 "vkGetInstanceProcAddr": "OverlayLayer_GetInstanceProcAddr",
1030 "vkGetDeviceProcAddr": "OverlayLayer_GetDeviceProcAddr"
1031 },
1032 "instance_extensions": [
1033 {
1034 "name": "VK_EXT_debug_report",
1035 "spec_version": "1"
1036 },
1037 {
1038 "name": "VK_VENDOR_ext_x",
1039 "spec_version": "3"
1040 }
1041 ],
1042 "device_extensions": [
1043 {
1044 "name": "VK_EXT_debug_marker",
1045 "spec_version": "1",
1046 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1047 }
1048 ],
1049 "enable_environment": {
1050 "ENABLE_LAYER_OVERLAY_1": "1"
1051 },
1052 "disable_environment": {
1053 "DISABLE_LAYER_OVERLAY_1": ""
1054 }
1055 }
Jon Ashburnc2972682016-02-08 15:42:01 -07001056}
Jon Ashburncc300a22016-02-11 14:57:30 -07001057```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001058The "library\_path" specifies either a filename, a relative pathname, or a full
1059pathname to a layer shared library (".so") file, which the loader will attempt
1060to load using dlopen(). If the layer is specified via a filename, the loader
1061will attempt to open that file as a shared object using dlopen(), and the file
1062must be in a directory that dlopen is configured to look in (Note: various
1063distributions are configured differently). A distribution is free to create
1064Vulkan-specific system directories (e.g. ".../vulkan/layers"), but is not
1065required to do so. If the layer is specified via a relative pathname, it is
1066relative to the path of the info file (e.g. for cases when an application
1067provides a layer that is in the same directory hierarchy as the rest of the
1068application files).
Jon Ashburnc2972682016-02-08 15:42:01 -07001069
1070There are no rules about the name of the text files (except the .json suffix).
1071
1072There are no rules about the name of the layer shared library files.
1073
1074##### Using Pre-Production Layers
1075
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001076As with ICDs, developers may need to use special, pre-production layers,
1077without modifying the properly-installed layers. This need is met with the use
1078of the "VK\_LAYER\_PATH" environment variable, which will override the
1079mechanism using for finding properly-installed layers. Because many layers may
1080exist on a system, this environment variable is a colon-separated list of
1081directories that contain layer info files. Only the directories listed in
1082"VK\_LAYER\_PATH" will be scanned for info files. Each colon-separated entry
1083is:
Jon Ashburnc2972682016-02-08 15:42:01 -07001084
1085- The full pathname of a directory containing layer info files
1086
1087NOTE: these environment variables will be ignored for suid programs.
1088
1089#### Android
1090
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001091The recommended way to enable layers is for applications
1092to programatically enable them. The layers are provided by the application
1093and must live in the application's library folder. The application
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001094enables the layers at vkCreateInstance as any Vulkan
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001095application would.
1096An application enabled for debug has more options. It can enumerate and enable
Karl Schultzfb5e4bc2016-10-31 08:52:38 -06001097layers located in /data/local/debug/vulkan.
Jon Ashburnc2972682016-02-08 15:42:01 -07001098
Mark Youngcb6e6702016-07-20 11:38:53 -06001099<br/>
1100
1101## Layer interface requirements ##
Jon Ashburnc2972682016-02-08 15:42:01 -07001102
1103#### Architectural interface overview
1104
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001105There are two key architectural features that drive the loader to layer library
1106interface: 1) separate and distinct instance and device call chains, and 2)
1107distributed dispatch. First these architectural features will be described and
1108then the detailed interface will be specified.
Jon Ashburnc2972682016-02-08 15:42:01 -07001109
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001110Call chains are the links of calls for a given Vulkan command from layer module
1111to layer module with the loader and or the ICD being the bottom most command.
1112Call chains are constructed at both the instance level and the device level by
1113the loader with cooperation from the layer libraries. Instance call chains are
1114constructed by the loader when layers are enabled at vkCreateInstance. Device
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001115call chains are constructed by the loader when layers are enabled, by the loader, at
ttyio0811cec2016-04-10 22:09:44 +08001116vkCreateDevice. A layer can intercept Vulkan instance commands, device commands
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001117or both. For a layer to intercept instance commands, it must participate in the
1118instance call chain. For a layer to intercept device commands, it must
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001119participate in the device chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001120
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001121Normally, when a layer intercepts a given Vulkan command, it will call down the
1122instance or device chain as needed. The loader and all layer libraries that
1123participate in a call chain cooperate to ensure the correct sequencing of calls
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001124from one entity to the next. This group effort for call chain sequencing is
Jeff Julianof1619872016-02-17 17:25:42 -05001125hereinafter referred to as distributed dispatch. In distributed dispatch, since
1126each layer is responsible for properly calling the next entity in the device or
1127instance chain, a dispatch mechanism is required for all Vulkan commands a
1128layer intercepts. For Vulkan commands that are not intercepted by a layer, or
1129if the layer chooses to terminate a given Vulkan command by not calling down
1130the chain, then no dispatch mechanism is needed for that particular Vulkan
1131command. Only for those Vulkan commands, which may be a subset of all Vulkan
1132commands, that a layer intercepts is a dispatching mechanism by the layer
1133needed. The loader is responsible for dispatching all core and instance
1134extension Vulkan commands to the first entity in the chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001135
Jeff Julianof1619872016-02-17 17:25:42 -05001136Instance level Vulkan commands are those that have the dispatchable objects
1137VkInstance, or VkPhysicalDevice as the first parameter and also includes
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001138vkCreateInstance.
Jeff Julianof1619872016-02-17 17:25:42 -05001139
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001140Device level Vulkan commands are those that use VkDevice, VkQueue or
1141VkCommandBuffer as the first parameter and also include vkCreateDevice. Future
1142extensions may introduce new instance or device level dispatchable objects, so
1143the above lists may be extended in the future.
Jon Ashburnc2972682016-02-08 15:42:01 -07001144
Chia-I Wucb24fec2016-04-20 06:23:24 +08001145#### Layer Library Interface
Jeff Julianof1619872016-02-17 17:25:42 -05001146
Chia-I Wucb24fec2016-04-20 06:23:24 +08001147A layer library is a container of layers. This section defines an extensible
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001148interface to discover layers contained in layer libraries.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001149The extensible programming interface is used on Android only. For Windows and Linux,
1150the layer manifest JSON files are used.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001151
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001152It also specifies the minimal conventions
1153and rules a layer must follow. Other sections might have other guidelines that layers should follow.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001154
1155##### Layer Conventions and Rules
1156
1157A layer, when inserted into an otherwise compliant Vulkan implementation, must
1158still result in a compliant Vulkan implementation[\*]. It must additionally
1159follow some conventions and rules.
1160
1161A layer is always chained with other layers. It must not make invalid calls
1162to or rely on undefined behaviors of its lower layers. When it changes the
1163behavior of a command, it must make sure its upper layers do not make invalid
1164calls to or rely on undefined behaviors of its lower layers because of the
1165changed behavior. For example, when a layer intercepts an object creation
1166command to wrap the objects created by its lower layers, it must make sure its
1167lower layers never see the wrapping objects, directly from itself or
1168indirectly from its upper layers.
1169
Chia-I Wub5e850e2016-05-06 08:41:52 +08001170When a layer requires host memory, it may ignore the provided allocators. It
1171should use memory allocators if the layer is intended to run in a production
1172environment, such as an implicit layer that is always enabled. That will
1173allow applications to include the layer's memory usage.
1174
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001175`vkEnumerateInstanceLayerProperties` must enumerate and only enumerate the
1176layer itself.
1177
1178`vkEnumerateInstanceExtensionProperties` must handle the case where
1179`pLayerName` is itself. It must return `VK_ERROR_LAYER_NOT_PRESENT`
1180otherwise, including when `pLayerName` is `NULL`.
1181
1182`vkEnumerateDeviceLayerProperties` is deprecated and may be omitted. The
1183behavior is undefined.
1184
Chia-I Wuadac8342016-04-22 08:12:19 +08001185`vkEnumerateDeviceExtensionProperties` must handle the case where `pLayerName`
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001186is itself. In other cases, it should normally chain to other layers.
1187
1188`vkCreateInstance` must not generate an error for unrecognized layer names and
1189extension names. It may assume the layer names and extension names have been
1190validated.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001191
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001192`vkGetInstanceProcAddr` intercepts a Vulkan command by returning a local entry point,
1193otherwise it returns the value obtained by calling down the instance chain.
1194 These commands must be intercepted
1195 - vkGetInstanceProcAddr
1196 - vkCreateInstance
1197 - vkCreateDevice (only required for any device-level chaining)
Chia-I Wucb24fec2016-04-20 06:23:24 +08001198
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001199 For compatibility with older layer libraries,
1200 - when `pName` is `vkCreateDevice`, it ignores `instance`.
1201
1202`vkGetDeviceProcAddr` intercepts a Vulkan command by returning a local entry point,
1203otherwise it returns the value obtained by calling down the device chain.
1204
1205The specification requires `NULL` to be returned from `vkGetInstanceProcAddr` and
1206`vkGetDeviceProcAddr` for disabled commands. A layer may return `NULL` itself or
1207rely on the following layers to do so.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001208
Chia-I Wucb24fec2016-04-20 06:23:24 +08001209[\*]: The intention is for layers to have a well-defined baseline behavior.
1210Some of the conventions or rules, for example, may be considered abuses of the
1211specification.
1212
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001213##### Layer Library API Version 0
Chia-I Wucb24fec2016-04-20 06:23:24 +08001214
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001215A layer library supporting interface version 0 must define and export these
1216introspection functions, unrelated to any Vulkan command despite the names,
1217signatures, and other similarities:
Chia-I Wucb24fec2016-04-20 06:23:24 +08001218
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001219 - `vkEnumerateInstanceLayerProperties` enumerates all layers in a layer
1220 library. This function never fails.
1221
1222 When a layer library contains only one layer, this function may be an alias
1223 to the layer's `vkEnumerateInstanceLayerProperties`.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001224
1225 - `vkEnumerateInstanceExtensionProperties` enumerates instance extensions of
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001226 layers in a layer library. `pLayerName` is always a valid layer name.
1227 This function never fails.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001228
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001229 When a layer library contains only one layer, this function may be an alias
1230 to the layer's `vkEnumerateInstanceExtensionProperties`.
1231
1232 - `vkEnumerateDeviceLayerProperties` enumerates a subset (can be full,
1233 proper, or empty subset) of layers in a layer library. `physicalDevice` is
1234 always `VK_NULL_HANDLE`. This function never fails.
1235
1236 If a layer is not enumerated by this function, it will not participate in
1237 device command interception.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001238
1239 - `vkEnumerateDeviceExtensionProperties` enumerates device extensions of
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001240 layers in a layer library. `physicalDevice` is always `VK_NULL_HANDLE`.
1241 `pLayerName` is always a valid layer name. This function never fails.
1242
1243The introspection functions are not used by the desktop loader.
1244
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001245It must also define and export these functions one for each layer in the library:
Chia-I Wucb24fec2016-04-20 06:23:24 +08001246
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001247 - `<layerName>GetInstanceProcAddr(instance, pName)` behaves identically to a layer's vkGetInstanceProcAddr except it is exported.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001248
1249 When a layer library contains only one layer, this function may
1250 alternatively be named `vkGetInstanceProcAddr`.
1251
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001252 - `<layerName>GetDeviceProcAddr` behaves identically to a layer's vkGetDeviceProcAddr except it is exported.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001253
1254 When a layer library contains only one layer, this function may
1255 alternatively be named `vkGetDeviceProcAddr`.
1256
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001257All layers contained within a library must support [`vk_layer.h`][]. They do not need to
1258implement commands that they do not intercept. They are recommended not to export
1259any commands.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001260
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001261<a name="LayerLibraryManifestFile"></a>
1262##### Layer Library Manifest File Version 0
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001263On Windows and Linux (desktop), the loader uses manifest files to discover
1264layer libraries and layers. The desktop loader doesn't directly query the
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001265layer library except during chaining.
1266On Android, the loader queries the layer libraries via the introspection functions as outlined above.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001267
1268The layer libraries and the manifest files must be kept in sync.
1269
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001270The following table associates the desktop JSON nodes with the layer library introspection queries. It also indicates requirements.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001271
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001272| Property | JSON node | Introspection query | Notes |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001273|----------|-----------|-----------------------|-------|
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001274| file version | file_format_version | N/A | one node required per JSON file |
1275| layers in library | layer | vkEnumerateInstanceLayerProperties | one node required per layer |
1276| layer name | name | vkEnumerateInstanceLayerProperties | one node is required |
1277| layer type | type | vkEnumerate*LayerProperties | see Note 1 |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001278| library location | library_path | N/A | one node is required |
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001279| vulkan spec version | api_version | vkEnumerateInstanceLayerProperties | one node is required |
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001280| layer implementation version | api_version | vkEnumerateInstanceLayerProperties | see Note 2 |
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001281| layer description | description | vkEnumerateInstanceLayerProperties | one node is required |
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001282| chaining functions | functions | vkGet*ProcAddr | see Note 3 |
1283| instance extensions | instance_extensions | vkEnumerateInstanceExtensionProperties | see Note 4 |
1284| device extensions | device_extensions | vkEnumerateDeviceExtensionProperties | see Note 5 |
1285| enable implicit | enable_environment | N/A | See Note 6 |
1286| disable implicit | enable_environment | N/A | See Note 7 |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001287
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001288"file\_format\_version" is used to indicate the valid JSON syntax of the file.
1289As nodes are added or deleted which would change the parsing of this file,
1290the file_format_version should change. This version
1291is NOT the same as the layer library interface version. The interface version is a superset
1292of the "file_format_version" and includes the semantics of the nodes in the JSON file.
1293For interface version 0 the file format version must be "1.0.0"
1294
1295Note 1: Prior to deprecation, the "type" node was used to indicate which layer chain(s)
1296to activate the layer upon: instance, device, or both.
1297Distinct instance and device layers are deprecated; there are now just layers.
1298Allowable values for type (both before and after deprecation) are "INSTANCE", "GLOBAL" and, "DEVICE."
1299"DEVICE" layers are skipped over by the loader as if they were not found.
1300Thus, layers must have a type of "GLOBAL" or "INSTANCE" for the loader to include the layer in the enumerated instance layer list.
1301
1302"library\_path" is the filename, full path, or relative path to the library file.
Mark Young57551512016-06-23 11:25:03 -06001303See [Manifest File Example](#ManifestFileExample) section for more details.
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001304
1305Note 2: One "implementation\_version" node is required per layer. This node gives the layer version, a single number
1306increasing with backward uncompatible changes.
1307
1308Note 3: The "functions" node is required if the layer is using alternative
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001309names for vkGetInstanceProcAddr or vkGetDeviceProcAddr. vkGetInstanceProcAddr and vkGetDeviceProcAddr
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001310are required for all layers. See further requirements in the Layer Library API section above.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001311
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001312Note 4: One "instance_extensions" node with an array of one or more elements
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001313required if any instance
1314extensions are supported by a layer, otherwise the node is optional. Each
1315element of the array must have the nodes "name" and "spec_version" which
1316correspond to VkExtensionProperties "extensionName" and "specVersion"
1317respectively.
1318
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001319Note 5: One "device_extensions" node with an array of one or more elements
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001320required if any device
1321extensions are supported by a layer, otherwise the node is optional. Each
1322element of the array must have the nodes "name" and "spec_version" which
1323correspond to VkExtensionProperties "extensionName" and "specVersion"
1324respectively. Additionally, each element of the array of device extensions
1325must have the node "entrypoints" if the device extension adds Vulkan API commands,
1326otherwise this node is not required.
1327The "entrypoint" node is an array of the names of all entrypoints added by the
1328supported extension.
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001329```
1330 "device_extensions": [
1331 {
1332 "name": "VK_EXT_debug_marker",
1333 "spec_version": "1",
1334 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1335 }
1336 ```
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001337
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001338Note 6: The "enable\_environment" node is only for implicit layers only. It is optional for implicit layers.
1339This node gives an environment variable and value required to enable an implicit layer. This
1340environment variable (which should vary with each "version" of the layer) must be set to the
1341given value or else the implicit layer is not loaded. This is for application environments (e.g. Steam) which
1342want to enable a layer(s) only for applications that they launch, and allows
1343for applications run outside of an application environment to not get that
1344implicit layer(s).
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001345
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001346Note 7: The "disable\_environment" node is only for implicit layers only. It is required for implicit layers.
1347This node gives an environment variable and value required to disable an implicit layer. In
1348rare cases of an application not working with an implicit layer, the
1349application can set this environment variable (before calling Vulkan commands)
1350in order to "blacklist" the layer. This environment variable (which should vary
1351with each "version" of the layer) must be set (not particularly to any value).
1352If both the "enable\_environment" and
1353"disable\_environment" variables are set, the implicit layer is disabled.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001354
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001355#### Layer Dispatch Interface Version 0
1356##### Layer intercept requirements
Jeff Julianof1619872016-02-17 17:25:42 -05001357
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001358- Layers intercept a Vulkan command by defining a C/C++ function with signature
1359identical to the Vulkan API for that command.
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001360- A layer must intercept at least vkGetInstanceProcAddr and
1361vkCreateInstance. Additionally, a layer would also intercept vkGetDeviceProcAddr and vkCreateDevice to participate in the device chain.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001362- For any Vulkan command a layer intercepts which has a non-void return value,
1363an appropriate value must be returned by the layer intercept function.
1364- The layer intercept function must call down the chain to the corresponding
1365Vulkan command in the next entity. Undefined results will occur if a layer
1366doesn't propagate calls down the chain. The two exceptions to this requirement
1367are vkGetInstanceProcAddr and vkGetDeviceProcAddr which only call down the
1368chain for Vulkan commands that they do not intercept.
1369- Layer intercept functions may insert extra calls to Vulkan commands in
1370addition to the intercept. For example, a layer intercepting vkQueueSubmit may
1371want to add a call to vkQueueWaitIdle after calling down the chain for
1372vkQueueSubmit. Any additional calls inserted by a layer must be on the same
1373chain. They should call down the chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001374
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001375##### Distributed dispatching requirements
Jeff Julianof1619872016-02-17 17:25:42 -05001376
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001377- For each entry point a layer intercepts, it must keep track of the entry
1378point residing in the next entity in the chain it will call down into. In other
1379words, the layer must have a list of pointers to functions of the appropriate
1380type to call into the next entity. This can be implemented in various ways but
1381for clarity will be referred to as a dispatch table.
1382- A layer can use the VkLayerDispatchTable structure as a device dispatch table
1383(see include/vulkan/vk_layer.h).
1384- A layer can use the VkLayerInstanceDispatchTable structure as a instance
1385dispatch table (see include/vulkan/vk_layer.h).
1386- Layers vkGetInstanceProcAddr function uses the next entity's
Jeff Julianof1619872016-02-17 17:25:42 -05001387vkGetInstanceProcAddr to call down the chain for unknown (i.e. non-intercepted)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001388functions.
1389- Layers vkGetDeviceProcAddr function uses the next entity's
Jeff Julianof1619872016-02-17 17:25:42 -05001390vkGetDeviceProcAddr to call down the chain for unknown (i.e. non-intercepted)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001391functions.
Jon Ashburnc2972682016-02-08 15:42:01 -07001392
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001393##### Layer dispatch initialization
Jeff Julianof1619872016-02-17 17:25:42 -05001394
1395- A layer initializes its instance dispatch table within its vkCreateInstance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001396function.
Jeff Julianof1619872016-02-17 17:25:42 -05001397- A layer initializes its device dispatch table within its vkCreateDevice
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001398function.
1399- The loader passes a linked list of initialization structures to layers via
1400the "pNext" field in the VkInstanceCreateInfo and VkDeviceCreateInfo structures
1401for vkCreateInstance and VkCreateDevice respectively.
1402- The head node in this linked list is of type VkLayerInstanceCreateInfo for
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001403instance and VkLayerDeviceCreateInfo for device. See file
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001404include/vulkan/vk_layer.h for details.
1405- A VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO is used by the loader for the
1406"sType" field in VkLayerInstanceCreateInfo.
1407- A VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO is used by the loader for the
1408"sType" field in VkLayerDeviceCreateInfo.
1409- The "function" field indicates how the union field "u" should be interpreted
1410within VkLayer*CreateInfo. The loader will set the "function" field to
1411VK_LAYER_LINK_INFO. This indicates "u" field should be VkLayerInstanceLink or
1412VkLayerDeviceLink.
Jon Ashburnc2972682016-02-08 15:42:01 -07001413- The VkLayerInstanceLink and VkLayerDeviceLink structures are the list nodes.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001414- The VkLayerInstanceLink contains the next entity's vkGetInstanceProcAddr used
1415by a layer.
1416- The VkLayerDeviceLink contains the next entity's vkGetInstanceProcAddr and
1417vkGetDeviceProcAddr used by a layer.
1418- Given the above structures set up by the loader, layer must initialize their
1419dispatch table as follows:
1420 - Find the VkLayerInstanceCreateInfo/VkLayerDeviceCreateInfo structure in
1421the VkInstanceCreateInfo/VkDeviceCreateInfo structure.
Jon Ashburncc300a22016-02-11 14:57:30 -07001422 - Get the next entity's vkGet*ProcAddr from the "pLayerInfo" field.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001423 - For CreateInstance get the next entity's vkCreateInstance by calling the
1424"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001425 pfnNextGetInstanceProcAddr(NULL, "vkCreateInstance").
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001426 - For CreateDevice get the next entity's vkCreateDevice by calling the
1427"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001428 pfnNextGetInstanceProcAddr(NULL, "vkCreateDevice").
Jon Ashburncc300a22016-02-11 14:57:30 -07001429 - Advanced the linked list to the next node: pLayerInfo = pLayerInfo->pNext.
1430 - Call down the chain either CreateDevice or CreateInstance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001431 - Initialize your layer dispatch table by calling the next entity's
1432Get*ProcAddr function once for each Vulkan command needed in your dispatch
1433table
Jon Ashburncc300a22016-02-11 14:57:30 -07001434
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001435##### Example code for CreateInstance
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001436
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001437```cpp
1438VkResult vkCreateInstance(
1439 const VkInstanceCreateInfo *pCreateInfo,
1440 const VkAllocationCallbacks *pAllocator,
1441 VkInstance *pInstance)
1442{
1443 VkLayerInstanceCreateInfo *chain_info =
1444 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1445
1446 assert(chain_info->u.pLayerInfo);
1447 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1448 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1449 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001450 (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001451 if (fpCreateInstance == NULL) {
1452 return VK_ERROR_INITIALIZATION_FAILED;
1453 }
1454
1455 // Advance the link info for the next element of the chain
1456 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1457
1458 // Continue call down the chain
1459 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
1460 if (result != VK_SUCCESS)
1461 return result;
1462
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001463 // Init layer's dispatch table using GetInstanceProcAddr of
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001464 // next layer in the chain.
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001465 instance_dispatch_table = new VkLayerInstanceDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001466 layer_init_instance_dispatch_table(
1467 *pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
1468
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001469 // Other layer initialization
1470 ...
1471
1472 return VK_SUCCESS;
1473}
1474```
1475
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001476##### Example code for CreateDevice
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001477
1478```cpp
1479VkResult
1480vkCreateDevice(
1481 VkPhysicalDevice gpu,
1482 const VkDeviceCreateInfo *pCreateInfo,
1483 const VkAllocationCallbacks *pAllocator,
1484 VkDevice *pDevice)
1485{
1486 VkLayerDeviceCreateInfo *chain_info =
1487 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1488
1489 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1490 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1491 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr =
1492 chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1493 PFN_vkCreateDevice fpCreateDevice =
1494 (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
1495 if (fpCreateDevice == NULL) {
1496 return VK_ERROR_INITIALIZATION_FAILED;
1497 }
1498
1499 // Advance the link info for the next element on the chain
1500 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1501
1502 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
1503 if (result != VK_SUCCESS) {
1504 return result;
1505 }
1506
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001507 // initialize layer's dispatch table
1508 device_dispatch_table = new VkLayerDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001509 layer_init_device_dispatch_table(
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001510 *pDevice, device_dispatch_table, fpGetDeviceProcAddr);
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001511
1512 // Other layer initialization
1513 ...
1514
1515 return VK_SUCCESS;
1516}
1517```
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001518
Jon Ashburncc300a22016-02-11 14:57:30 -07001519#### Special Considerations
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001520##### Associating private data with Vulkan objects within a layer
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001521A layer may want to associate it's own private data with one or more Vulkan
Mark Youngf7914cf2016-08-31 11:53:26 -06001522objects. Two common methods to do this are hash maps and object wrapping.
1523
1524###### Wrapping:
1525
Ian Elliott0b082e42016-08-31 14:08:44 -06001526The loader supports layers wrapping any Vulkan object including dispatchable
1527objects.
1528For commands that return object handles, the layer saves the handle that is
1529returned from a lower-level layer (possibly the ICD), and returns its own
1530handle to the layer above it (possibly the application). For commands that are
1531given previously-returned handles, the layer unwraps the handle; that is it
1532looks up the saved handle and gives that to the layer below it.
1533
1534Layers which wrap objects must ensure they always unwrap objects before passing
1535them down the chain. This means that the layer must intercept every Vulkan
1536command which uses the object in question, and wrap or unwrap the object, as
1537appropriate. This includes adding support for all extensions with commands
1538using any object the layer wraps.
Mark Youngf7914cf2016-08-31 11:53:26 -06001539
1540Layers above the object wrapping layer will see the wrapped object. Layers
1541which wrap dispatchable objects must ensure that the first field in the wrapping
1542structure is a pointer to a dispatch table as defined in vk_layer.h. Specifically, an
1543instance wrapped dispatchable object could be as follows:
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001544```
1545struct my_wrapped_instance_obj_ {
1546 VkLayerInstanceDispatchTable *disp;
1547 // whatever data layer wants to add to this object
1548};
1549```
1550A device wrapped dispatchable object could be as follows:
1551```
1552struct my_wrapped_instance_obj_ {
1553 VkLayerDispatchTable *disp;
1554 // whatever data layer wants to add to this object
1555};
1556```
Jeff Julianof1619872016-02-17 17:25:42 -05001557
Ian Elliott0b082e42016-08-31 14:08:44 -06001558Layers that wrap dispatchable objects must follow the guidelines for creating
1559new dispatchable objects (below).
1560
Mark Young67929282016-09-01 13:43:04 -06001561<u><b>Cautions</b></u>
Ian Elliott0b082e42016-08-31 14:08:44 -06001562
1563Layers are generally discouraged from wrapping objects, because of the
1564potential for incompatibilities with new extensions. For example, let's say
1565that a layer wraps VkImage objects, and properly wraps and unwraps VkImage
1566object handles for all core commands. If a new extension is created which has
1567commands that take VkImage objects as parameters, and if the layer does not
1568support those new commands, an application that uses both the layer and the new
1569extension will have undefined behavior when those new commands are called (e.g.
1570the application may crash). This is becaues the lower-level layers and ICD
1571won't receive the handle that they generated. Instead, they will receive a
1572handle that is only known by the layer that is wrapping the object.
1573
1574Because of the potential for incompatibilities with unsupported extensions,
1575layers that wrap objects must check which extensions are being used by the
1576application, and take appropriate action if the layer is used with unsupported
1577extensions (e.g. disable layer functionality, stop wrapping objects, issue a
1578message to the user).
1579
1580The reason that the validation layers wrap objects, is to track the proper use
1581and destruction of each object. They issue a validation error if used with
1582unsupported extensions, alerting the user to the potential for undefined
1583behavior.
1584
Mark Youngf7914cf2016-08-31 11:53:26 -06001585###### Hash Maps:
Jeff Julianof1619872016-02-17 17:25:42 -05001586Alternatively, a layer may want to use a hash map to associate data with a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001587given object. The key to the map could be the object. Alternatively, for
1588dispatchable objects at a given level (eg device or instance) the layer may
1589want data associated with the VkDevice or VkInstance objects. Since
Jeff Julianof1619872016-02-17 17:25:42 -05001590there are multiple dispatchable objects for a given VkInstance or VkDevice, the
1591VkDevice or VkInstance object is not a great map key. Instead the layer should
1592use the dispatch table pointer within the VkDevice or VkInstance since that
1593will be unique for a given VkInstance or VkDevice.
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001594
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001595##### Creating new dispatchable objects
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001596Layers which create dispatchable objects take special care. Remember that loader
1597trampoline code normally fills in the dispatch table pointer in the newly
1598created object. Thus, the layer must fill in the dispatch table pointer if the
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001599loader trampoline will not do so. Common cases where a layer (or ICD) may create a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001600dispatchable object without loader trampoline code is as follows:
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001601- object wrapping layers that wrap dispatchable objects
1602- layers which add extensions that create dispatchable objects
1603- layers which insert extra Vulkan commands in the stream of commands they
1604intercept from the application
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001605- ICDs which add extensions that create dispatchable objects
1606
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001607The Windows/Linux loader provides a callback that can be used for initializing
1608a dispatchable object. The callback is passed as an extension structure via the
1609pNext field in VkInstanceCreateInfo and VkDeviceCreateInfo. The callback prototype
1610is defined as follows for instance and device callbacks respectively (see vk_layer.h):
1611```
1612VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceLoaderData(VkInstance instance, void *object);
1613VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceLoaderData)(VkDevice device, void *object);
1614```
1615To obtain these callbacks the layer must search through the list of structures
1616pointed 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:
1617- For CreateInstance the callback structure pointed to by "pNext" is VkLayerInstanceCreateInfo as defined in vk_layer.h.
1618- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO within VkInstanceCreateInfo parameter indicates a loader structure.
1619- Within VkLayerInstanceCreateInfo, the "function" field indicates how the union field "u" should be interpreted.
1620- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will contain the callback in "pfnSetInstanceLoaderData".
1621- For CreateDevice the callback structure pointed to by "pNext" is VkLayerDeviceCreateInfo as defined in include/vulkan/vk_layer.h.
1622- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO within VkDeviceCreateInfo parameter indicates a loader structure.
1623- Within VkLayerDeviceCreateInfo, the "function" field indicates how the union field "u" should be interpreted.
1624- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will contain the callback in "pfnSetDeviceLoaderData".
1625
1626Alternatively, 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 -07001627To fill in the dispatch table pointer in newly created dispatchable object,
1628the 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
1629device). 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 -07001630