blob: 22d5520f78678cb1950c5ca7442497ec95a2e68f [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
Mark Young6d026a72016-06-01 17:49:30 -0600158####Layer Usage
159
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
Jon Ashburnc2972682016-02-08 15:42:01 -0700209```
210
Mark Young6d026a72016-06-01 17:49:30 -0600211#### Implicit vs Explicit Layers
212
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700213Some platforms, including Linux and Windows, support layers which are enabled
214automatically by the loader rather than explicitly by the application (or via
215environment variable). Explicit layers are those layers enabled by the
216application (or environment variable) by providing the layer name. Implicit
217layers are those layers enabled by the loader automatically. Any implicit
218layers the loader discovers on the system in the appropriate location will be
219enabled (subject to environment variable overrides described later). Discovery
220of properly installed implicit and explicit layers is described later.
221Explicitly enabling a layer that is implicitly enabled has no additional
222effect: the layer will still be enabled implicitly by the loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700223
Mark Young02ee5382016-07-22 08:51:05 -0600224Implicit layers have an additional requirement over explicit layers in that they
225require being able to be disabled by an environmental variable. This is due
226to the fact that they are not visible to the application and could cause issues.
227A good principle to keep in mind would be to define both an enable and disable
228environment variable so the users can deterministicly enable the functionality.
229On Desktop platforms (Windows and Linux), these enable/disable settings are
230defined in the layer's JSON file.
231
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700232Extensions are optional functionality provided by a layer, the loader or an
233ICD. Extensions can modify the behavior of the Vulkan API and need to be
234specified and registered with Khronos.
Jon Ashburnc2972682016-02-08 15:42:01 -0700235
Mark Young6d026a72016-06-01 17:49:30 -0600236#### Instance/Device Extensions
237
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700238Instance extensions can be discovered via
239vkEnumerateInstanceExtensionProperties. Device extensions can be discovered via
240vkEnumerateDeviceExtensionProperties. The loader discovers and aggregates all
241extensions from layers (both explicit and implicit), ICDs and the loader before
242reporting them to the application in vkEnumerate\*ExtensionProperties. The
Jeff Julianof1619872016-02-17 17:25:42 -0500243pLayerName parameter in these functions is used to select either a single layer
244or the Vulkan platform implementation. If pLayerName is NULL, extensions from
245Vulkan implementation components (including loader, implicit layers, and ICDs)
246are enumerated. If pLayerName is equal to a discovered layer module name then
247any extensions from that layer (which may be implicit or explicit) are
248enumerated. Duplicate extensions (e.g. an implicit layer and ICD might report
Jon Ashburn859c7fb2016-03-02 17:26:31 -0700249support for the same extension) are eliminated by the loader. For duplicates, the
250ICD version is reported and the layer version is culled. Extensions must
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700251be enabled (in vkCreateInstance or vkCreateDevice) before they can be used.
Jon Ashburnc2972682016-02-08 15:42:01 -0700252
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700253Extension command entry points should be queried via vkGetInstanceProcAddr or
254vkGetDeviceProcAddr. vkGetDeviceProcAddr can only be used to query for device
255extension or core device entry points. Device entry points include any command
256that uses a VkDevice as the first parameter or a dispatchable object that is a
257child of a VkDevice (currently this includes VkQueue and VkCommandBuffer).
258vkGetInstanceProcAddr can be used to query either device or instance extension
259entry points in addition to all core entry points.
Jon Ashburnc2972682016-02-08 15:42:01 -0700260
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700261VkGetDeviceProcAddr is particularly interesting because it will provide the
262most efficient way to call into the ICD. For example, the diagram below shows
263what could happen if the application were to use vkGetDeviceProcAddr for the
Mark Young6d026a72016-06-01 17:49:30 -0600264function "vkGetDeviceQueue" and "vkDestroyDevice" but not "vkAllocateMemory".
265The resulting function pointer (fpGetDeviceQueue) would be the ICD's entry
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700266point if the loader and any enabled layers do not need to see that call. Even
Jeff Julianof1619872016-02-17 17:25:42 -0500267if an enabled layer intercepts the call (e.g. vkDestroyDevice) the loader
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700268trampoline code is skipped for function pointers obtained via
269vkGetDeviceProcAddr. This also means that function pointers obtained via
270vkGetDeviceProcAddr will only work with the specific VkDevice it was created
271for, using it with another device has undefined results. For extensions,
272Get\*ProcAddr will often be the only way to access extension API features.
Jon Ashburnc2972682016-02-08 15:42:01 -0700273
Jon Ashburnc2505562016-02-15 10:19:26 -0700274![Get*ProcAddr efficiency](get_proc_addr.png)
Courtney Goeltzenleuchterab3a4662016-02-14 10:48:22 -0700275
Mark Young78f88c82016-07-19 11:49:45 -0600276##### WSI Extensions
277
278Khronos approved WSI extensions are available and provide Windows System Integration
279support for various execution environments. It is important to understand that some WSI
280extensions are valid for all targets, but others are particular to a given execution
281environment (and loader). This desktop loader (currently targeting Windows and Linux)
282only enables those WSI extensions that are appropriate to the current environment.
283For the most part, the selection is done in the loader using compile-time preprocessor
284flags. All versions of the desktop loader currently expose at least the following WSI
285extension support:
286- VK_KHR_surface
287- VK_KHR_swapchain
288- VK_KHR_display
289
290In addition, each of the following OS targets for the loader support target-specific extensions:
291- **Windows** : VK_KHR_win32_surface
292- **Linux (default)** : VK_KHR_xcb_surface and VK_KHR_xlib_surface
293- **Linux (Wayland build)** : VK_KHR_wayland_surface
294- **Linux (Mir build)** : VK_KHR_mir_surface
295
296**NOTE:** Wayland and Mir targets are not fully supported at this time and should be considered
297alpha quality.
298
299It is important to understand that while the loader may support the various entry-points
300for these extensions, there is a hand-shake required to actually use them:
301* At least one physical device must support the extension(s)
302* The application must select such a physical device
303* 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).
304* The instance and/or logical device creation must succeed.
305
306Only then can you expect to properly use a WSI extension in your Vulkan program.
307
308##### New Extensions
309
310With the ability to expand Vulkan so easily, extensions will be created that the loader knows
311nothing about. If the extension is a device extension, the loader will pass the unknown
312entry-point down the device call chain ending with the appropriate ICD entry-points.
313However, if the extension is an instance extension, the loader will fail to load it.
314
315*But why doesn't the loader support unknown instance extensions?*
316<br/>
317Let's look again at the Instance call chain:
318![Instance call chain](instance_call_chain.png)
319
320Notice that for a normal instance function call, the loader has to handle passing along the
321function call to the available ICDs. If the loader has no idea of the parameters or return
322value of the instance call, it can't properly pass information along to the ICDs.
323There may be ways to do this, which will be explored in the future. However, for now, this
324loader does not support any unknown instance extensions.
325
326Because the device call-chain does not pass through the loader terminator, this is not
327a problem for device extensions. Instead, device extensions terminate directly in the
328ICD they are associated with.
329
330*Is this a big problem?*
331<br/>
332No! Most extension functionality only affects a device and not an instance or a physical
333device. Thus, the overwhelming majority of extensions will be device extensions rather than
334instance extensions.
335
Mark Youngcb6e6702016-07-20 11:38:53 -0600336
337## Vulkan Installable Client Driver interface with the loader ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700338
339### ICD discovery
340
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700341Vulkan allows multiple drivers each with one or more devices (represented by a
342Vulkan VkPhysicalDevice object) to be used collectively. The loader is
343responsible for discovering available Vulkan ICDs on the system. Given a list
344of available ICDs, the loader can enumerate all the physical devices available
345for an application and return this information to the application. The process
346in which the loader discovers the available Installable Client Drivers (ICDs)
347on a system is platform dependent. Windows, Linux and Android ICD discovery
348details are listed below.
Jon Ashburnc2972682016-02-08 15:42:01 -0700349
350#### Windows
351
352##### Properly-Installed ICDs
353
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700354In order to find properly-installed ICDs, the Vulkan loader will scan the
355values in the following Windows registry key:
Jon Ashburnc2972682016-02-08 15:42:01 -0700356
357HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\Drivers
358
Mark Young02ee5382016-07-22 08:51:05 -0600359On 64-bit Windows, when a 32-bit application is triggered, the loader
360will scan for 32-bit drivers in a separate area of the registry:
361
362HKEY\_LOCAL\_MACHINE\\SOFTWARE\\WOW6432Node\\Khronos\\Vulkan\\Drivers
363
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700364For each value in this key which has DWORD data set to 0, the loader opens the
365JSON format text information file (a.k.a. "manifest file") specified by the
366name of the value. Each name must be a full pathname to the text manifest file.
367The Vulkan loader will open each manifest file to obtain the name or pathname
368of an ICD shared library (".dll") file. For example:
Jon Ashburnc2972682016-02-08 15:42:01 -0700369
Jon Ashburncc300a22016-02-11 14:57:30 -0700370 ```
371 {
Jon Ashburnc2972682016-02-08 15:42:01 -0700372 "file_format_version": "1.0.0",
373 "ICD": {
374 "library_path": "path to ICD library",
Tony Barbourd83f06c2016-03-08 14:50:03 -0700375 "api_version": "1.0.5"
Jon Ashburnc2972682016-02-08 15:42:01 -0700376 }
377 }
Jon Ashburncc300a22016-02-11 14:57:30 -0700378 ```
Jon Ashburnc2972682016-02-08 15:42:01 -0700379
380
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700381The "library\_path" specifies either a filename, a relative pathname, or a full
382pathname to an ICD shared library file, which the loader will attempt to load
383using LoadLibrary(). If the ICD is specified via a filename, the shared library
David Pinedo3e163ee2016-04-18 16:59:59 -0600384lives in the system's DLL search path (e.g. in the "C:\Windows\System32"
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700385folder). If the ICD is specified via a relative pathname, it is relative to the
386path of the manifest file. Relative pathnames are those that do not start with
387a drive specifier (e.g. "C:"), nor with a directory separator (i.e. the '\\'
388character), but do contain at least one directory separator.
Jon Ashburnc2972682016-02-08 15:42:01 -0700389
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700390The "file\_format\_version" specifies a major.minor.patch version number in
391case the format of the text information file changes in the future. If the same
392ICD shared library supports multiple, incompatible versions of text manifest
Mark Young02ee5382016-07-22 08:51:05 -0600393file format versions, it must have separate JSON files for each (all of which may
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700394point to the same shared library).
Jon Ashburnc2972682016-02-08 15:42:01 -0700395
Mark Young6d026a72016-06-01 17:49:30 -0600396The "api\_version" specifies the major.minor.patch version number of the Vulkan
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700397API that the shared library (referenced by "library\_path") was built with.
Jon Ashburnc2972682016-02-08 15:42:01 -0700398
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700399There are no rules about the name of the text information files (except the
400.json suffix).
Jon Ashburnc2972682016-02-08 15:42:01 -0700401
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700402There are no rules about the name of the ICD shared library files. For example,
403if the registry contains the following values,
Jon Ashburnc2972682016-02-08 15:42:01 -0700404
Jon Ashburncc300a22016-02-11 14:57:30 -0700405```
406[HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers\]
Jon Ashburnc2972682016-02-08 15:42:01 -0700407
David Pinedo3e163ee2016-04-18 16:59:59 -0600408"C:\vendor a\vk_vendora.json"=dword:00000000
Jon Ashburnc2972682016-02-08 15:42:01 -0700409
David Pinedo3e163ee2016-04-18 16:59:59 -0600410"C:\windows\system32\vendorb_vk.json"=dword:00000000
Jon Ashburnc2972682016-02-08 15:42:01 -0700411
David Pinedo3e163ee2016-04-18 16:59:59 -0600412"C:\windows\system32\vendorc_icd.json"=dword:00000000
Jon Ashburncc300a22016-02-11 14:57:30 -0700413```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700414then the loader will open the following text information files, with the
415specified contents:
Jon Ashburnc2972682016-02-08 15:42:01 -0700416
Jon Ashburncc300a22016-02-11 14:57:30 -0700417| Text File Name | Text File Contents |
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700418|----------------|--------------------|
David Pinedo3e163ee2016-04-18 16:59:59 -0600419|vk\_vendora.json | "ICD": { "library\_path": "C:\VENDOR A\vk_vendora.dll", "api_version": "1.0.5" } |
Tony Barbourd83f06c2016-03-08 14:50:03 -0700420| vendorb\_vk.json | "ICD": { "library\_path": "vendorb\_vk.dll", "api_version": "1.0.5" } |
421|vendorc\_icd.json | "ICD": { "library\_path": "vedorc\_icd.dll", "api_version": "1.0.5" }|
Jon Ashburnc2972682016-02-08 15:42:01 -0700422
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700423Then the loader will open the three files mentioned in the "Text File Contents"
424column, and then try to load and use the three shared libraries indicated by
425the ICD.library\_path value.
Jon Ashburnc2972682016-02-08 15:42:01 -0700426
427##### Using Pre-Production ICDs
428
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700429IHV developers (and sometimes other developers) need to use special,
430pre-production ICDs. In some cases, a pre-production ICD may be in an
431installable package. In other cases, a pre-production ICD may simply be a
432shared library in the developer's build tree. In this latter case, we want to
433allow developers to point to such an ICD without modifying the
434properly-installed ICD(s) on their system.
Jon Ashburnc2972682016-02-08 15:42:01 -0700435
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700436This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
437which will override the mechanism used for finding properly-installed ICDs. In
438other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used. The
439"VK\_ICD\_FILENAMES" environment variable is a semi-colon-separated list of ICD
440text information files (aka manifest files), containing the following:
Jon Ashburnc2972682016-02-08 15:42:01 -0700441
Jon Ashburncc300a22016-02-11 14:57:30 -0700442- A full pathname (e.g. "C:\\my\_build\\my\_icd.json")
Jon Ashburnc2972682016-02-08 15:42:01 -0700443
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700444Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
445file for a developer-built ICD. A semi-colon is only used if more than one ICD
446is listed.
Jon Ashburnc2972682016-02-08 15:42:01 -0700447
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700448For example, if a developer wants to refer to one ICD that they built, they
449could set the "VK\_ICD\_FILENAMES" environment variable to:
Jon Ashburnc2972682016-02-08 15:42:01 -0700450
Jon Ashburncc300a22016-02-11 14:57:30 -0700451C:\\my\_build\\my\_icd.json
Jon Ashburnc2972682016-02-08 15:42:01 -0700452
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700453If a developer wants to refer to two ICDs, one of which is a properly-installed
454ICD, they can use the full pathname of the text file:
Jon Ashburnc2972682016-02-08 15:42:01 -0700455
Jon Ashburncc300a22016-02-11 14:57:30 -0700456C:\\Windows\\System32\\vendorc\_icd.json;C:\\my\_build\\my\_icd.json
Jon Ashburnc2972682016-02-08 15:42:01 -0700457
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700458Notice the semi-colon between "C:\\Windows\\System32\\vendorc\_icd.json" and
459"C:\\my\_build\\my\_icd.json".
Jon Ashburnc2972682016-02-08 15:42:01 -0700460
461#### Linux
462
463##### Properly-Installed ICDs
464
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700465In order to find properly-installed ICDs, the Vulkan loader will scan the files
466in the following Linux directories:
Jon Ashburnc2972682016-02-08 15:42:01 -0700467
468/usr/share/vulkan/icd.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700469/etc/vulkan/icd.d
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700470$HOME/.local/share/vulkan/icd.d
471
472Where $HOME is the current home directory of the application's user id; this
473path will be ignored for suid programs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700474
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700475These directories will contain text information files (a.k.a. "manifest
476files"), that use a JSON format.
Jon Ashburnc2972682016-02-08 15:42:01 -0700477
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700478The Vulkan loader will open each manifest file found to obtain the name or
479pathname of an ICD shared library (".so") file. For example:
Jon Ashburnc2972682016-02-08 15:42:01 -0700480
Jon Ashburncc300a22016-02-11 14:57:30 -0700481```
482{
Jon Ashburnc2972682016-02-08 15:42:01 -0700483 "file_format_version": "1.0.0",
484 "ICD": {
485 "library_path": "path to ICD library",
Tony Barbourd83f06c2016-03-08 14:50:03 -0700486 "api_version": "1.0.5"
Jon Ashburnc2972682016-02-08 15:42:01 -0700487 }
488}
Jon Ashburncc300a22016-02-11 14:57:30 -0700489```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700490The "library\_path" specifies either a filename, a relative pathname, or a full
491pathname to an ICD shared library file. If the ICD is specified via a filename,
492the loader will attempt to open that file as a shared object using dlopen(),
493and the file must be in a directory that dlopen is configured to look in (Note:
494various distributions are configured differently). A distribution is free to
495create Vulkan-specific system directories (e.g. ".../vulkan/icd"), but is not
496required to do so. If the ICD is specified via a relative pathname, it is
497relative to the path of the info file. Relative pathnames are those that do not
498start with, but do contain at least one directory separator (i.e. the '/'
499character). For example, "lib/vendora.so" and "./vendora.so" are examples of
500relative pathnames.
Jon Ashburnc2972682016-02-08 15:42:01 -0700501
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700502The "file\_format\_version" provides a major.minor.patch version number in case
503the format of the manifest file changes in the future. If the same ICD shared
504library supports multiple, incompatible versions of manifest file format
505versions, it must have multiple manifest files (all of which may point to the
506same shared library).
Jon Ashburnc2972682016-02-08 15:42:01 -0700507
Mark Young6d026a72016-06-01 17:49:30 -0600508The "api\_version" specifies the major.minor.patch version number of the Vulkan
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700509API that the shared library (referenced by "library\_path") was built with.
Jon Ashburnc2972682016-02-08 15:42:01 -0700510
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700511The "/usr/share/vulkan/icd.d" directory is for ICDs that are installed from
512Linux-distribution-provided packages. The "/etc/vulkan/icd.d" directory is for
513ICDs that are installed from non-Linux-distribution-provided packages.
Jon Ashburnc2972682016-02-08 15:42:01 -0700514
515There are no rules about the name of the text files (except the .json suffix).
516
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700517There are no rules about the name of the ICD shared library files. For example,
518if the "/usr/share/vulkan/icd.d" directory contain the following files, with
519the specified contents:
Jon Ashburnc2972682016-02-08 15:42:01 -0700520
Jon Ashburn26ed3f32016-02-14 21:54:52 -0700521| Text File Name | Text File Contents |
522|-------------------|------------------------|
Tony Barbourd83f06c2016-03-08 14:50:03 -0700523| vk\_vendora.json | "ICD": { "library\_path": "vendora.so", "api_version": "1.0.5" } |
524| vendorb\_vk.json | "ICD": { "library\_path": "vendorb\_vulkan\_icd.so", "api_version": "1.0.5" } |
525| vendorc\_icd.json | "ICD": { "library\_path": "/usr/lib/VENDORC/icd.so", "api_version": "1.0.5" } |
Jon Ashburnc2972682016-02-08 15:42:01 -0700526
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700527then the loader will open the three files mentioned in the "Text File Contents"
528column, and then try to load and use the three shared libraries indicated by
529the ICD.library\_path value.
Jon Ashburnc2972682016-02-08 15:42:01 -0700530
531##### Using Pre-Production ICDs
532
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700533IHV developers (and sometimes other developers) need to use special,
534pre-production ICDs. In some cases, a pre-production ICD may be in an
535installable package. In other cases, a pre-production ICD may simply be a
536shared library in the developer's build tree. In this latter case, we want to
537allow developers to point to such an ICD without modifying the
538properly-installed ICD(s) on their system.
Jon Ashburnc2972682016-02-08 15:42:01 -0700539
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700540This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
541which will override the mechanism used for finding properly-installed ICDs. In
542other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used.
Jon Ashburnc2972682016-02-08 15:42:01 -0700543
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700544The "VK\_ICD\_FILENAMES" environment variable is a colon-separated list of ICD
545manifest files, containing the following:
Jon Ashburnc2972682016-02-08 15:42:01 -0700546
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700547- A filename (e.g. "libvkicd.json") in the "/usr/share/vulkan/icd.d", "/etc/vulkan/icd.d" "$HOME/.local/share/vulkan/icd.d" directories
Jon Ashburnc2972682016-02-08 15:42:01 -0700548
549- A full pathname (e.g. "/my\_build/my\_icd.json")
550
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700551Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
552file for a developer-built ICD. A colon is only used if more than one ICD is
553listed.
Jon Ashburnc2972682016-02-08 15:42:01 -0700554
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700555For example, if a developer wants to refer to one ICD that they built, they
556could set the "VK\_ICD\_FILENAMES" environment variable to:
Jon Ashburnc2972682016-02-08 15:42:01 -0700557
558/my\_build/my\_icd.json
559
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700560If a developer wants to refer to two ICDs, one of which is a properly-installed
561ICD, they can use the name of the text file in the system directory:
Jon Ashburnc2972682016-02-08 15:42:01 -0700562
563vendorc\_vulkan.json:/my\_build/my\_icd.json
564
565Notice the colon between "vendorc\_vulkan.json" and "/my\_build/my\_icd.json".
566
567NOTE: this environment variable will be ignored for suid programs.
568
569#### Android
570
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700571The Android loader lives in the system library folder. The location cannot be
572changed. The loader will load the driver/ICD via hw_get_module with the ID
573of "vulkan". Due to security policies in Android none of this can be modified
574under normal use.
Jon Ashburnc2972682016-02-08 15:42:01 -0700575
Mark Youngcb6e6702016-07-20 11:38:53 -0600576<br/>
Jon Ashburnc2972682016-02-08 15:42:01 -0700577
Mark Youngcb6e6702016-07-20 11:38:53 -0600578## ICD interface requirements ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700579
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700580Generally, for all Vulkan commands issued by an application, the loader can be
Mark Young6d026a72016-06-01 17:49:30 -0600581viewed as a pass through. That is, the loader generally doesn't modify the
Jon Ashburn54791f62016-04-22 14:40:07 -0600582commands or their parameters, but simply calls the ICDs entry point for that
583command. There are specific additional interface requirements an ICD needs to comply with that
584are over and above any requirements from the Vulkan specification including WSI extension specification.
585These addtional requirements are versioned to allow flexibility in the future.
586These interface requirements will be set forth in the following sections: 1) describing
587which "loader-ICD" interface version is available, 2) detailing the most recent interface version;
5883) the supported, older interface requirements will be described as differences
589from the most recent interface version.
Jon Ashburnc2972682016-02-08 15:42:01 -0700590
591#### Windows and Linux
592
Jon Ashburn54791f62016-04-22 14:40:07 -0600593##### Version Negotiation Between Loader and ICDs
Jon Ashburnc2972682016-02-08 15:42:01 -0700594
Jon Ashburn54791f62016-04-22 14:40:07 -0600595All ICDs (supporting interface version 2 or higher) must export the following
596function that is used for determination of the interface version that will be used.
597This entry point is not a part of the Vulkan API itself, only a private interface
598between the loader and ICDs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700599
Jon Ashburn54791f62016-04-22 14:40:07 -0600600VKAPI_ATTR VkResult VKAPI_CALL vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
Jon Ashburnc2972682016-02-08 15:42:01 -0700601
Jon Ashburn54791f62016-04-22 14:40:07 -0600602This entry point reports the "loader-ICD" interface version supported by both the loader and the ICD.
603The loader informs the ICD of it's desired interface version (typically the latest) via the
604pSupportedVersion parameter.
605This call is the first call made by the loader into the ICD (prior to any calls to
606vk\_icdGetInstanceProcAddr).
Jon Ashburnc2972682016-02-08 15:42:01 -0700607
Jon Ashburn54791f62016-04-22 14:40:07 -0600608If a loader sees that an ICD does not export this symbol it knows that it's dealing
609with a legacy ICD supporting either interface version 0 or 1.
610Similarly, if an ICD sees a call to vk\_icdGetInstanceProcAddr before a call to
611vk_icdGetLoaderICDInterfaceVersion then it knows that it's dealing with a legacy loader
612supporting version 0 or 1.
Mark Young02ee5382016-07-22 08:51:05 -0600613**Note** if the loader calls vk\_icdGetInstanceProcAddr first it supports at least version 1,
Jon Ashburn54791f62016-04-22 14:40:07 -0600614otherwise the loader only supports version 0.
Jon Ashburnc2972682016-02-08 15:42:01 -0700615
Jon Ashburn54791f62016-04-22 14:40:07 -0600616The pSupportedVersion parameter is both an input and output parameter.
617It 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 -0500618
Jon Ashburn54791f62016-04-22 14:40:07 -0600619If the ICD receiving the call no longer supports the interface version provided
620by the loader (due to deprecation) then it can report VK_ERROR_INCOMPATIBLE_DRIVER error,
621otherwise it sets the value pointed by pSupportedVersion to the latest interface
622version supported by both the ICD and the loader and returns VK_SUCCESS.
623The ICD should report VK_SUCCESS in case the loader provided interface version
624is newer than that supported by the ICD, as it's the loader's responsibility to
625determine whether it can support the older interface version supported by the ICD.
626The ICD should also report VK_SUCCESS in the case it's interface version is greater
627than the loader's, but return the loader's version. Thus, upon return of VK_SUCCESS
628the pSupportedVersion will contain the desired interface version to be used by the ICD.
Jon Ashburnc2972682016-02-08 15:42:01 -0700629
Jon Ashburn54791f62016-04-22 14:40:07 -0600630If the loader receives back an interface version from the ICD that the loader no longer
631supports (due to deprecation) or it receives a VK_ERROR_INCOMPATIBLE_DRIVER error
632instead of VK_SUCCESS then the loader will treat the ICD as incompatible
633and will not load it for use. In this case the application will not see the ICDs vkPhysicalDevice
634during enumeration.
Jon Ashburnc2972682016-02-08 15:42:01 -0700635
Jon Ashburn54791f62016-04-22 14:40:07 -0600636##### Loader Version 2 Interface Requirements
Jon Ashburnc2972682016-02-08 15:42:01 -0700637
Jon Ashburn54791f62016-04-22 14:40:07 -0600638Version 2 interface has requirements in three areas: 1) ICD Vulkan entry point discovery,
6392) KHR_surface related requirements in the WSI extensions, 3) Vulkan dispatchable object
640creation requirements.
Jon Ashburnc2972682016-02-08 15:42:01 -0700641
Jon Ashburn54791f62016-04-22 14:40:07 -0600642###### ICD Vulkan entry point discovery
643All ICDs must export the following function that is used for discovery of ICD Vulkan entry points.
644This 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 -0700645
Jon Ashburn54791f62016-04-22 14:40:07 -0600646VKAPI\_ATTR PFN\_vkVoidFunction VKAPI\_CALL vk\_icdGetInstanceProcAddr(VkInstance instance, const char* pName);
647
648This function has very similar semantics to the Vulkan command vkGetInstanceProcAddr.
649vk\_icdGetInstanceProcAddr returns valid function pointers for all the global level
650and instance level Vulkan commands, and also for vkGetDeviceProcAddr.
651Global level commands are those
652which contain no dispatchable object as the first parameter, such as
653vkCreateInstance and vkEnumerateInstanceExtensionProperties. The ICD must
654support querying global level entry points by calling
655vk\_icdGetInstanceProcAddr with a NULL VkInstance parameter. Instance level
656commands are those that have either VkInstance, or VkPhysicalDevice as the
657first parameter dispatchable object. Both core entry points and any instance
658extension entry points the ICD supports should be available via
659vk\_icdGetInstanceProcAddr. Future Vulkan instance extensions may define and
660use new instance level dispatchable objects other than VkInstance and
661VkPhysicalDevice, in which case extension entry points using these newly
662defined dispatchable objects must be queryable via vk\_icdGetInstanceProcAddr.
663
664All other Vulkan entry points must either NOT be exported from the ICD
665library or else NOT use the official Vulkan function names if they are
666exported. This requirement is for ICD libraries that include other
667functionality (such as OpenGL library) and thus could be loaded by the
668application prior to when the Vulkan loader library is loaded by the
669application. In other words, the ICD library exported Vulkan symbols must not
670clash with the loader's exported Vulkan symbols.
671
672Beware of interposing by dynamic OS library loaders if the official Vulkan
673names are used. On Linux, if official names are used, the ICD library must be
674linked with -Bsymbolic.
675
676###### Handling KHR_surface objects in the WSI extensions
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700677Normally, ICDs handle object creation and destruction for various Vulkan
678objects. The WSI surface extensions for Linux and Windows
679(VK\_KHR\_win32\_surface, VK\_KHR\_xcb\_surface, VK\_KHR\_xlib\_surface,
680VK\_KHR\_mir\_surface, VK\_KHR\_wayland\_surface, and VK\_KHR\_surface) are
681handled differently. For these extensions, the VkSurfaceKHR object creation and
682destruction is handled by the loader as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700683
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07006841. Loader handles the vkCreate\*SurfaceKHR() and vkDestroySurfaceKHR()
685 functions including creating/destroying the VkSurfaceKHR object.
Jon Ashburnc2972682016-02-08 15:42:01 -0700686
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07006872. VkSurfaceKHR objects have the underlying structure (VkIcdSurface\*) as
688 defined in include/vulkan/vk\_icd.h.
Jon Ashburnc2972682016-02-08 15:42:01 -0700689
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07006903. ICDs can cast any VkSurfaceKHR object to a pointer to the appropriate
691 VkIcdSurface\* structure.
Jon Ashburnc2972682016-02-08 15:42:01 -0700692
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07006934. VkIcdSurface\* structures include VkIcdSurfaceWin32, VkIcdSurfaceXcb,
Jeff Julianof1619872016-02-17 17:25:42 -0500694 VkIcdSurfaceXlib, VkIcdSurfaceMir, and VkIcdSurfaceWayland. The first field
695 in the structure is a VkIcdSurfaceBase enumerant that indicates whether the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700696 surface object is Win32, Xcb, Xlib, Mir, or Wayland.
Jon Ashburnc2972682016-02-08 15:42:01 -0700697
Jon Ashburn54791f62016-04-22 14:40:07 -0600698###### ICD dispatchable object creation
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700699As previously covered, the loader requires dispatch tables to be accessible
700within Vulkan dispatchable objects, which include VkInstance, VkPhysicalDevice,
701VkDevice, VkQueue, and VkCommandBuffer. The specific requirements on all
702dispatchable objects created by ICDs are as follows:
Jon Ashburnc2972682016-02-08 15:42:01 -0700703
Jon Ashburncc300a22016-02-11 14:57:30 -0700704- All dispatchable objects created by an ICD can be cast to void \*\*
Jon Ashburnc2972682016-02-08 15:42:01 -0700705
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700706- The loader will replace the first entry with a pointer to the dispatch table
707 which is owned by the loader. This implies three things for ICD drivers:
Jon Ashburnc2972682016-02-08 15:42:01 -0700708
Jon Ashburncc300a22016-02-11 14:57:30 -07007091. The ICD must return a pointer for the opaque dispatchable object handle.
Jon Ashburnc2972682016-02-08 15:42:01 -0700710
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007112. This pointer points to a regular C structure with the first entry being a
712 pointer. Note: for any C\++ ICD's that implement VK objects directly as C\++
713 classes. The C\++ compiler may put a vtable at offset zero if your class is
Jeff Julianof1619872016-02-17 17:25:42 -0500714 non-POD due to the use of a virtual function. In this case use a regular C
715 structure (see below).
Jon Ashburnc2972682016-02-08 15:42:01 -0700716
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07007173. The loader checks for a magic value (ICD\_LOADER\_MAGIC) in all the created
718 dispatchable objects, as follows (see include/vulkan/vk\_icd.h):
Jon Ashburnc2972682016-02-08 15:42:01 -0700719
720```
721
Jon Ashburncc300a22016-02-11 14:57:30 -0700722#include "vk_icd.h"
Jon Ashburnc2972682016-02-08 15:42:01 -0700723
Jon Ashburncc300a22016-02-11 14:57:30 -0700724union _VK_LOADER_DATA {
725 uintptr loadermagic;
726 void *loaderData;
727} VK_LOADER_DATA;
Jon Ashburnc2972682016-02-08 15:42:01 -0700728
Jon Ashburncc300a22016-02-11 14:57:30 -0700729vkObj alloc_icd_obj()
Jon Ashburnc2972682016-02-08 15:42:01 -0700730{
Jon Ashburncc300a22016-02-11 14:57:30 -0700731 vkObj *newObj = alloc_obj();
732 ...
733 // Initialize pointer to loader's dispatch table with ICD_LOADER_MAGIC
Jon Ashburnc2972682016-02-08 15:42:01 -0700734
Jon Ashburncc300a22016-02-11 14:57:30 -0700735 set_loader_magic_value(newObj);
736 ...
737 return newObj;
Jon Ashburnc2972682016-02-08 15:42:01 -0700738}
Jon Ashburnc2972682016-02-08 15:42:01 -0700739```
740
Jon Ashburn54791f62016-04-22 14:40:07 -0600741##### Loader Version 0 and 1 Interface Differences
742
743Version 0 and 1 interfaces do not support version negotiation via vk\_icdNegotiateLoaderICDInterfaceVersion.
744ICDs can distinguish version 0 and version 1 interfaces as follows:
745if the loader calls vk\_icdGetInstanceProcAddr first it supports version 1,
746otherwise the loader only supports version 0.
747
748Version 0 interface does not support vk\_icdGetInstanceProcAddr. Version 0 interface requirements for
749obtaining ICD Vulkan entry points are as follows:
750
751- vkGetInstanceProcAddr exported in the ICD library and returns valid function
752 pointers for all the Vulkan API entry points;
753
754- vkCreateInstance exported in the ICD library;
755
756- vkEnumerateInstanceExtensionProperties exported in the ICD library;
757
758
Jon Ashburnc2972682016-02-08 15:42:01 -0700759Additional Notes:
760
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700761- The loader will filter out extensions requested in vkCreateInstance and
762vkCreateDevice before calling into the ICD; Filtering will be of extensions
Jeff Julianof1619872016-02-17 17:25:42 -0500763advertised by entities (e.g. layers) different from the ICD in question.
764- The loader will not call the ICD for vkEnumerate\*LayerProperties() as layer
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700765properties are obtained from the layer libraries and layer JSON files.
766- If an ICD library wants to implement a layer it can do so by having the
767appropriate layer JSON manifest file refer to the ICD library file.
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700768- The loader will not call the ICD for
769 vkEnumerate\*ExtensionProperties(pLayerName != NULL).
Jon Ashburn2b2f6182016-04-04 16:37:37 -0600770- ICDs creating new dispatchable objects via device extensions need to initialize
Jon Ashburn54791f62016-04-22 14:40:07 -0600771the created dispatchable object. The loader has generic trampoline code for unknown
Jon Ashburn2b2f6182016-04-04 16:37:37 -0600772device extensions. This generic trampoline code doesn't initialize the dispatch table within
Jon Ashburn54791f62016-04-22 14:40:07 -0600773the newly created object. See the section for more information on how to initialize created
774dispatchable objects for extensions non known by the loader. [layer link](#creating-new-dispatchable-objects)
Jeff Julianof1619872016-02-17 17:25:42 -0500775
Jon Ashburnc2972682016-02-08 15:42:01 -0700776#### Android
777
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700778The Android loader uses the same protocol for initializing the dispatch
779table as described above. The only difference is that the Android
780loader queries layer and extension information directly from the
781respective libraries and does not use the json manifest files used
782by the Windows and Linux loaders.
Jon Ashburnc2972682016-02-08 15:42:01 -0700783
Mark Youngcb6e6702016-07-20 11:38:53 -0600784<br/>
785
786## Vulkan layer interface with the loader ##
Jon Ashburnc2972682016-02-08 15:42:01 -0700787
788### Layer discovery
789
790#### Windows
791
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -0600792<a name="ManifestFileExample"></a>
Jon Ashburnc2972682016-02-08 15:42:01 -0700793##### Properly-Installed Layers
794
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700795In order to find properly-installed layers, the Vulkan loader will use a
796similar mechanism as used for ICDs. Text information files (aka manifest
797files), that use a JSON format, are read in order to identify the names and
798attributes of layers and their extensions. The use of manifest files allows the
799loader to avoid loading any shared library files when the application does not
800query nor request any extensions. Layers and extensions have additional
801complexity, and so their manifest files contain more information than ICD info
802files. For example, a layer shared library file may contain multiple
803layers/extensions (perhaps even an ICD).
Jon Ashburnc2972682016-02-08 15:42:01 -0700804
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700805In order to find properly-installed layers, the Vulkan loader will scan the
806values in the following Windows registry keys:
Jon Ashburnc2972682016-02-08 15:42:01 -0700807
808HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers
809
810HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers
811
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700812Explicit layers are those which are enabled by an application (e.g. with the
813vkCreateInstance function), or by an environment variable (as mentioned
814previously).
Jon Ashburnc2972682016-02-08 15:42:01 -0700815
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700816Implicit layers are those which are enabled by their existence. For example,
817certain application environments (e.g. Steam or an automotive infotainment
818system) may have layers which they always want enabled for all applications
819that they start. Other implicit layers may be for all applications started on a
820given system (e.g. layers that overlay frames-per-second). Implicit layers are
821enabled automatically, whereas explicit layers must be enabled explicitly. What
822distinguishes a layer as implicit or explicit is by which registry key its
823layer information file is referenced by.
Jon Ashburnc2972682016-02-08 15:42:01 -0700824
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700825For each value in these keys which has DWORD data set to 0, the loader opens
826the JSON manifest file specified by the name of the value. Each name must be a
827full pathname to the manifest file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700828
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700829The Vulkan loader will open each info file to obtain information about the
830layer, including the name or pathname of a shared library (".dll") file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700831
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -0600832This manifest file is in the JSON format as shown in the following example.
833See 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 -0700834
Jon Ashburncc300a22016-02-11 14:57:30 -0700835```
Jon Ashburnc2972682016-02-08 15:42:01 -0700836{
Mark Youngc3a6d2e2016-06-13 14:49:53 -0600837 "file_format_version" : "1.0.0",
838 "layer": {
839 "name": "VK_LAYER_LUNARG_overlay",
840 "type": "INSTANCE",
841 "library_path": "vkOverlayLayer.dll"
842 "api_version" : "1.0.5",
843 "implementation_version" : "2",
844 "description" : "LunarG HUD layer",
845 "functions": {
846 "vkGetInstanceProcAddr": "OverlayLayer_GetInstanceProcAddr",
847 "vkGetDeviceProcAddr": "OverlayLayer_GetDeviceProcAddr"
848 },
849 "instance_extensions": [
850 {
851 "name": "VK_EXT_debug_report",
852 "spec_version": "1"
853 },
854 {
855 "name": "VK_VENDOR_ext_x",
856 "spec_version": "3"
857 }
858 ],
859 "device_extensions": [
860 {
861 "name": "VK_EXT_debug_marker",
862 "spec_version": "1",
863 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
864 }
865 ],
866 "enable_environment": {
867 "ENABLE_LAYER_OVERLAY_1": "1"
868 }
869 "disable_environment": {
870 "DISABLE_LAYER_OVERLAY_1": ""
871 }
872 }
Jon Ashburnc2972682016-02-08 15:42:01 -0700873}
Jon Ashburncc300a22016-02-11 14:57:30 -0700874```
Jon Ashburnc2972682016-02-08 15:42:01 -0700875
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700876The "library\_path" specifies either a filename, a relative pathname, or a full
877pathname to a layer shared library (".dll") file, which the loader will attempt
878to load using LoadLibrary(). If the layer is specified via a relative pathname,
879it is relative to the path of the info file (e.g. for cases when an application
880provides a layer that is in the same folder hierarchy as the rest of the
881application files). If the layer is specified via a filename, the shared
882library lives in the system's DLL search path (e.g. in the
883"C:\\Windows\\System32" folder).
Jon Ashburnc2972682016-02-08 15:42:01 -0700884
Mark Youngc3a6d2e2016-06-13 14:49:53 -0600885If defining multiple layers in a single JSON file prior to "file\_format\_version"
8861.0.1, you would simply define multiple "layer" objects. However, this is not
887valid JSON syntax. Instead, you should now define "file\_format\_version"
8881.0.1 (or newer) and use the new "layers" array object as seen in the
889following example:
890
891```
892{
893 "file_format_version" : "1.0.1",
894 "layers": [
895 {
896 "name": "VK_LAYER_layer_name1",
897 "type": "INSTANCE",
898 ...
899 },
900 {
901 "name": "VK_LAYER_layer_name2",
902 "type": "INSTANCE",
903 ...
904 }
905 ]
906}
907```
908
909You could use the "layers" array object to define a single layer, as long as
910your "file\_format\_version" is defined to at least 1.0.1. It is functionally the
911same as using a single "layer" object.
912
Jon Ashburnc2972682016-02-08 15:42:01 -0700913There are no rules about the name of the text files (except the .json suffix).
914
915There are no rules about the name of the layer shared library files.
916
917##### Using Pre-Production Layers
918
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700919As with ICDs, developers may need to use special, pre-production layers,
920without modifying the properly-installed layers. This need is met with the use
921of the "VK\_LAYER\_PATH" environment variable, which will override the
922mechanism using for finding properly-installed layers. Because many layers may
923exist on a system, this environment variable is a semi-colon-separated list of
924folders that contain layer info files. Only the folder listed in
925"VK\_LAYER\_PATH" will be scanned for info files. Each semi-colon-separated
926entry is:
Jon Ashburnc2972682016-02-08 15:42:01 -0700927
928- The full pathname of a folder containing layer info files
929
930#### Linux
931
932##### Properly-Installed Layers
933
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700934In order to find properly-installed layers, the Vulkan loader will use a
935similar mechanism as used for ICDs. Text information files, that use a JSON
936format, are read in order to identify the names and attributes of layers and
937their extensions. The use of text info files allows the loader to avoid loading
938any shared library files when the application does not query nor request any
939extensions. Layers and extensions have additional complexity, and so their info
940files contain more information than ICD info files. For example, a layer shared
941library file may contain multiple layers/extensions (perhaps even an ICD).
Jon Ashburnc2972682016-02-08 15:42:01 -0700942
943The Vulkan loader will scan the files in the following Linux directories:
944
945/usr/share/vulkan/explicit\_layer.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700946/usr/share/vulkan/implicit\_layer.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700947/etc/vulkan/explicit\_layer.d
Jon Ashburnc2972682016-02-08 15:42:01 -0700948/etc/vulkan/implicit\_layer.d
David Pinedo3e163ee2016-04-18 16:59:59 -0600949\$HOME/.local/share/vulkan/explicit\_layer.d
950\$HOME/.local/share/vulkan/implicit\_layer.d
Jon Ashburn7f00ca82016-02-24 12:00:55 -0700951
952Where $HOME is the current home directory of the application's user id; this
953path will be ignored for suid programs.
Jon Ashburnc2972682016-02-08 15:42:01 -0700954
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700955Explicit layers are those which are enabled by an application (e.g. with the
956vkCreateInstance function), or by an environment variable (as mentioned
957previously). Implicit layers are those which are enabled by their existence.
958For example, certain application environments (e.g. Steam or an automotive
959infotainment system) may have layers which they always want enabled for all
960applications that they start. Other implicit layers may be for all applications
961started on a given system (e.g. layers that overlay frames-per-second).
962Implicit layers are enabled automatically, whereas explicit layers must be
963enabled explicitly. What distinguishes a layer as implicit or explicit is by
964which directory its layer information file exists in.
Jon Ashburnc2972682016-02-08 15:42:01 -0700965
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700966The "/usr/share/vulkan/\*\_layer.d" directories are for layers that are
967installed from Linux-distribution-provided packages. The
968"/etc/vulkan/\*\_layer.d" directories are for layers that are installed from
969non-Linux-distribution-provided packages.
Jon Ashburnc2972682016-02-08 15:42:01 -0700970
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -0600971This manifest file is in the JSON format as shown in the following example.
972See 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 -0700973
Jon Ashburncc300a22016-02-11 14:57:30 -0700974```
Jon Ashburnc2972682016-02-08 15:42:01 -0700975{
Mark Youngc3a6d2e2016-06-13 14:49:53 -0600976 "file_format_version" : "1.0.0",
977 "layer": {
978 "name": "VK_LAYER_LUNARG_overlay",
979 "type": "INSTANCE",
980 "library_path": "libvkOverlayLayer.so"
981 "api_version" : "1.0.5",
982 "implementation_version" : "2",
983 "description" : "LunarG HUD layer",
984 "functions": {
985 "vkGetInstanceProcAddr": "OverlayLayer_GetInstanceProcAddr",
986 "vkGetDeviceProcAddr": "OverlayLayer_GetDeviceProcAddr"
987 },
988 "instance_extensions": [
989 {
990 "name": "VK_EXT_debug_report",
991 "spec_version": "1"
992 },
993 {
994 "name": "VK_VENDOR_ext_x",
995 "spec_version": "3"
996 }
997 ],
998 "device_extensions": [
999 {
1000 "name": "VK_EXT_debug_marker",
1001 "spec_version": "1",
1002 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1003 }
1004 ],
1005 "enable_environment": {
1006 "ENABLE_LAYER_OVERLAY_1": "1"
1007 },
1008 "disable_environment": {
1009 "DISABLE_LAYER_OVERLAY_1": ""
1010 }
1011 }
Jon Ashburnc2972682016-02-08 15:42:01 -07001012}
Jon Ashburncc300a22016-02-11 14:57:30 -07001013```
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001014The "library\_path" specifies either a filename, a relative pathname, or a full
1015pathname to a layer shared library (".so") file, which the loader will attempt
1016to load using dlopen(). If the layer is specified via a filename, the loader
1017will attempt to open that file as a shared object using dlopen(), and the file
1018must be in a directory that dlopen is configured to look in (Note: various
1019distributions are configured differently). A distribution is free to create
1020Vulkan-specific system directories (e.g. ".../vulkan/layers"), but is not
1021required to do so. If the layer is specified via a relative pathname, it is
1022relative to the path of the info file (e.g. for cases when an application
1023provides a layer that is in the same directory hierarchy as the rest of the
1024application files).
Jon Ashburnc2972682016-02-08 15:42:01 -07001025
1026There are no rules about the name of the text files (except the .json suffix).
1027
1028There are no rules about the name of the layer shared library files.
1029
1030##### Using Pre-Production Layers
1031
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001032As with ICDs, developers may need to use special, pre-production layers,
1033without modifying the properly-installed layers. This need is met with the use
1034of the "VK\_LAYER\_PATH" environment variable, which will override the
1035mechanism using for finding properly-installed layers. Because many layers may
1036exist on a system, this environment variable is a colon-separated list of
1037directories that contain layer info files. Only the directories listed in
1038"VK\_LAYER\_PATH" will be scanned for info files. Each colon-separated entry
1039is:
Jon Ashburnc2972682016-02-08 15:42:01 -07001040
1041- The full pathname of a directory containing layer info files
1042
1043NOTE: these environment variables will be ignored for suid programs.
1044
1045#### Android
1046
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001047The recommended way to enable layers is for applications
1048to programatically enable them. The layers are provided by the application
1049and must live in the application's library folder. The application
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001050enables the layers at vkCreateInstance as any Vulkan
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001051application would.
1052An application enabled for debug has more options. It can enumerate and enable
1053layers located in /data/local/vulkan/debug.
Jon Ashburnc2972682016-02-08 15:42:01 -07001054
Mark Youngcb6e6702016-07-20 11:38:53 -06001055<br/>
1056
1057## Layer interface requirements ##
Jon Ashburnc2972682016-02-08 15:42:01 -07001058
1059#### Architectural interface overview
1060
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001061There are two key architectural features that drive the loader to layer library
1062interface: 1) separate and distinct instance and device call chains, and 2)
1063distributed dispatch. First these architectural features will be described and
1064then the detailed interface will be specified.
Jon Ashburnc2972682016-02-08 15:42:01 -07001065
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001066Call chains are the links of calls for a given Vulkan command from layer module
1067to layer module with the loader and or the ICD being the bottom most command.
1068Call chains are constructed at both the instance level and the device level by
1069the loader with cooperation from the layer libraries. Instance call chains are
1070constructed by the loader when layers are enabled at vkCreateInstance. Device
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001071call chains are constructed by the loader when layers are enabled, by the loader, at
ttyio0811cec2016-04-10 22:09:44 +08001072vkCreateDevice. A layer can intercept Vulkan instance commands, device commands
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001073or both. For a layer to intercept instance commands, it must participate in the
1074instance call chain. For a layer to intercept device commands, it must
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001075participate in the device chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001076
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001077Normally, when a layer intercepts a given Vulkan command, it will call down the
1078instance or device chain as needed. The loader and all layer libraries that
1079participate in a call chain cooperate to ensure the correct sequencing of calls
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001080from one entity to the next. This group effort for call chain sequencing is
Jeff Julianof1619872016-02-17 17:25:42 -05001081hereinafter referred to as distributed dispatch. In distributed dispatch, since
1082each layer is responsible for properly calling the next entity in the device or
1083instance chain, a dispatch mechanism is required for all Vulkan commands a
1084layer intercepts. For Vulkan commands that are not intercepted by a layer, or
1085if the layer chooses to terminate a given Vulkan command by not calling down
1086the chain, then no dispatch mechanism is needed for that particular Vulkan
1087command. Only for those Vulkan commands, which may be a subset of all Vulkan
1088commands, that a layer intercepts is a dispatching mechanism by the layer
1089needed. The loader is responsible for dispatching all core and instance
1090extension Vulkan commands to the first entity in the chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001091
Jeff Julianof1619872016-02-17 17:25:42 -05001092Instance level Vulkan commands are those that have the dispatchable objects
1093VkInstance, or VkPhysicalDevice as the first parameter and also includes
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001094vkCreateInstance.
Jeff Julianof1619872016-02-17 17:25:42 -05001095
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001096Device level Vulkan commands are those that use VkDevice, VkQueue or
1097VkCommandBuffer as the first parameter and also include vkCreateDevice. Future
1098extensions may introduce new instance or device level dispatchable objects, so
1099the above lists may be extended in the future.
Jon Ashburnc2972682016-02-08 15:42:01 -07001100
Chia-I Wucb24fec2016-04-20 06:23:24 +08001101#### Layer Library Interface
Jeff Julianof1619872016-02-17 17:25:42 -05001102
Chia-I Wucb24fec2016-04-20 06:23:24 +08001103A layer library is a container of layers. This section defines an extensible
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001104interface to discover layers contained in layer libraries.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001105The extensible programming interface is used on Android only. For Windows and Linux,
1106the layer manifest JSON files are used.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001107
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001108It also specifies the minimal conventions
1109and rules a layer must follow. Other sections might have other guidelines that layers should follow.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001110
1111##### Layer Conventions and Rules
1112
1113A layer, when inserted into an otherwise compliant Vulkan implementation, must
1114still result in a compliant Vulkan implementation[\*]. It must additionally
1115follow some conventions and rules.
1116
1117A layer is always chained with other layers. It must not make invalid calls
1118to or rely on undefined behaviors of its lower layers. When it changes the
1119behavior of a command, it must make sure its upper layers do not make invalid
1120calls to or rely on undefined behaviors of its lower layers because of the
1121changed behavior. For example, when a layer intercepts an object creation
1122command to wrap the objects created by its lower layers, it must make sure its
1123lower layers never see the wrapping objects, directly from itself or
1124indirectly from its upper layers.
1125
Chia-I Wub5e850e2016-05-06 08:41:52 +08001126When a layer requires host memory, it may ignore the provided allocators. It
1127should use memory allocators if the layer is intended to run in a production
1128environment, such as an implicit layer that is always enabled. That will
1129allow applications to include the layer's memory usage.
1130
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001131`vkEnumerateInstanceLayerProperties` must enumerate and only enumerate the
1132layer itself.
1133
1134`vkEnumerateInstanceExtensionProperties` must handle the case where
1135`pLayerName` is itself. It must return `VK_ERROR_LAYER_NOT_PRESENT`
1136otherwise, including when `pLayerName` is `NULL`.
1137
1138`vkEnumerateDeviceLayerProperties` is deprecated and may be omitted. The
1139behavior is undefined.
1140
Chia-I Wuadac8342016-04-22 08:12:19 +08001141`vkEnumerateDeviceExtensionProperties` must handle the case where `pLayerName`
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001142is itself. In other cases, it should normally chain to other layers.
1143
1144`vkCreateInstance` must not generate an error for unrecognized layer names and
1145extension names. It may assume the layer names and extension names have been
1146validated.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001147
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001148`vkGetInstanceProcAddr` intercepts a Vulkan command by returning a local entry point,
1149otherwise it returns the value obtained by calling down the instance chain.
1150 These commands must be intercepted
1151 - vkGetInstanceProcAddr
1152 - vkCreateInstance
1153 - vkCreateDevice (only required for any device-level chaining)
Chia-I Wucb24fec2016-04-20 06:23:24 +08001154
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001155 For compatibility with older layer libraries,
1156 - when `pName` is `vkCreateDevice`, it ignores `instance`.
1157
1158`vkGetDeviceProcAddr` intercepts a Vulkan command by returning a local entry point,
1159otherwise it returns the value obtained by calling down the device chain.
1160
1161The specification requires `NULL` to be returned from `vkGetInstanceProcAddr` and
1162`vkGetDeviceProcAddr` for disabled commands. A layer may return `NULL` itself or
1163rely on the following layers to do so.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001164
Chia-I Wucb24fec2016-04-20 06:23:24 +08001165[\*]: The intention is for layers to have a well-defined baseline behavior.
1166Some of the conventions or rules, for example, may be considered abuses of the
1167specification.
1168
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001169##### Layer Library API Version 0
Chia-I Wucb24fec2016-04-20 06:23:24 +08001170
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001171A layer library supporting interface version 0 must define and export these
1172introspection functions, unrelated to any Vulkan command despite the names,
1173signatures, and other similarities:
Chia-I Wucb24fec2016-04-20 06:23:24 +08001174
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001175 - `vkEnumerateInstanceLayerProperties` enumerates all layers in a layer
1176 library. This function never fails.
1177
1178 When a layer library contains only one layer, this function may be an alias
1179 to the layer's `vkEnumerateInstanceLayerProperties`.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001180
1181 - `vkEnumerateInstanceExtensionProperties` enumerates instance extensions of
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001182 layers in a layer library. `pLayerName` is always a valid layer name.
1183 This function never fails.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001184
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001185 When a layer library contains only one layer, this function may be an alias
1186 to the layer's `vkEnumerateInstanceExtensionProperties`.
1187
1188 - `vkEnumerateDeviceLayerProperties` enumerates a subset (can be full,
1189 proper, or empty subset) of layers in a layer library. `physicalDevice` is
1190 always `VK_NULL_HANDLE`. This function never fails.
1191
1192 If a layer is not enumerated by this function, it will not participate in
1193 device command interception.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001194
1195 - `vkEnumerateDeviceExtensionProperties` enumerates device extensions of
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001196 layers in a layer library. `physicalDevice` is always `VK_NULL_HANDLE`.
1197 `pLayerName` is always a valid layer name. This function never fails.
1198
1199The introspection functions are not used by the desktop loader.
1200
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001201It must also define and export these functions one for each layer in the library:
Chia-I Wucb24fec2016-04-20 06:23:24 +08001202
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001203 - `<layerName>GetInstanceProcAddr(instance, pName)` behaves identically to a layer's vkGetInstanceProcAddr except it is exported.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001204
1205 When a layer library contains only one layer, this function may
1206 alternatively be named `vkGetInstanceProcAddr`.
1207
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001208 - `<layerName>GetDeviceProcAddr` behaves identically to a layer's vkGetDeviceProcAddr except it is exported.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001209
1210 When a layer library contains only one layer, this function may
1211 alternatively be named `vkGetDeviceProcAddr`.
1212
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001213All layers contained within a library must support [`vk_layer.h`][]. They do not need to
1214implement commands that they do not intercept. They are recommended not to export
1215any commands.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001216
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001217<a name="LayerLibraryManifestFile"></a>
1218##### Layer Library Manifest File Version 0
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001219On Windows and Linux (desktop), the loader uses manifest files to discover
1220layer libraries and layers. The desktop loader doesn't directly query the
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001221layer library except during chaining.
1222On Android, the loader queries the layer libraries via the introspection functions as outlined above.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001223
1224The layer libraries and the manifest files must be kept in sync.
1225
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001226The following table associates the desktop JSON nodes with the layer library introspection queries. It also indicates requirements.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001227
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001228| Property | JSON node | Introspection query | Notes |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001229|----------|-----------|-----------------------|-------|
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001230| file version | file_format_version | N/A | one node required per JSON file |
1231| layers in library | layer | vkEnumerateInstanceLayerProperties | one node required per layer |
1232| layer name | name | vkEnumerateInstanceLayerProperties | one node is required |
1233| layer type | type | vkEnumerate*LayerProperties | see Note 1 |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001234| library location | library_path | N/A | one node is required |
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001235| vulkan spec version | api_version | vkEnumerateInstanceLayerProperties | one node is required |
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001236| layer implementation version | api_version | vkEnumerateInstanceLayerProperties | see Note 2 |
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001237| layer description | description | vkEnumerateInstanceLayerProperties | one node is required |
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001238| chaining functions | functions | vkGet*ProcAddr | see Note 3 |
1239| instance extensions | instance_extensions | vkEnumerateInstanceExtensionProperties | see Note 4 |
1240| device extensions | device_extensions | vkEnumerateDeviceExtensionProperties | see Note 5 |
1241| enable implicit | enable_environment | N/A | See Note 6 |
1242| disable implicit | enable_environment | N/A | See Note 7 |
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001243
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001244"file\_format\_version" is used to indicate the valid JSON syntax of the file.
1245As nodes are added or deleted which would change the parsing of this file,
1246the file_format_version should change. This version
1247is NOT the same as the layer library interface version. The interface version is a superset
1248of the "file_format_version" and includes the semantics of the nodes in the JSON file.
1249For interface version 0 the file format version must be "1.0.0"
1250
1251Note 1: Prior to deprecation, the "type" node was used to indicate which layer chain(s)
1252to activate the layer upon: instance, device, or both.
1253Distinct instance and device layers are deprecated; there are now just layers.
1254Allowable values for type (both before and after deprecation) are "INSTANCE", "GLOBAL" and, "DEVICE."
1255"DEVICE" layers are skipped over by the loader as if they were not found.
1256Thus, layers must have a type of "GLOBAL" or "INSTANCE" for the loader to include the layer in the enumerated instance layer list.
1257
1258"library\_path" is the filename, full path, or relative path to the library file.
Mark Young57551512016-06-23 11:25:03 -06001259See [Manifest File Example](#ManifestFileExample) section for more details.
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001260
1261Note 2: One "implementation\_version" node is required per layer. This node gives the layer version, a single number
1262increasing with backward uncompatible changes.
1263
1264Note 3: The "functions" node is required if the layer is using alternative
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001265names for vkGetInstanceProcAddr or vkGetDeviceProcAddr. vkGetInstanceProcAddr and vkGetDeviceProcAddr
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001266are required for all layers. See further requirements in the Layer Library API section above.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001267
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001268Note 4: One "instance_extensions" node with an array of one or more elements
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001269required if any instance
1270extensions are supported by a layer, otherwise the node is optional. Each
1271element of the array must have the nodes "name" and "spec_version" which
1272correspond to VkExtensionProperties "extensionName" and "specVersion"
1273respectively.
1274
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001275Note 5: One "device_extensions" node with an array of one or more elements
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001276required if any device
1277extensions are supported by a layer, otherwise the node is optional. Each
1278element of the array must have the nodes "name" and "spec_version" which
1279correspond to VkExtensionProperties "extensionName" and "specVersion"
1280respectively. Additionally, each element of the array of device extensions
1281must have the node "entrypoints" if the device extension adds Vulkan API commands,
1282otherwise this node is not required.
1283The "entrypoint" node is an array of the names of all entrypoints added by the
1284supported extension.
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001285```
1286 "device_extensions": [
1287 {
1288 "name": "VK_EXT_debug_marker",
1289 "spec_version": "1",
1290 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1291 }
1292 ```
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001293
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001294Note 6: The "enable\_environment" node is only for implicit layers only. It is optional for implicit layers.
1295This node gives an environment variable and value required to enable an implicit layer. This
1296environment variable (which should vary with each "version" of the layer) must be set to the
1297given value or else the implicit layer is not loaded. This is for application environments (e.g. Steam) which
1298want to enable a layer(s) only for applications that they launch, and allows
1299for applications run outside of an application environment to not get that
1300implicit layer(s).
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001301
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001302Note 7: The "disable\_environment" node is only for implicit layers only. It is required for implicit layers.
1303This node gives an environment variable and value required to disable an implicit layer. In
1304rare cases of an application not working with an implicit layer, the
1305application can set this environment variable (before calling Vulkan commands)
1306in order to "blacklist" the layer. This environment variable (which should vary
1307with each "version" of the layer) must be set (not particularly to any value).
1308If both the "enable\_environment" and
1309"disable\_environment" variables are set, the implicit layer is disabled.
Jon Ashburn6bda65b2016-05-10 09:24:52 -06001310
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001311#### Layer Dispatch Interface Version 0
1312##### Layer intercept requirements
Jeff Julianof1619872016-02-17 17:25:42 -05001313
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001314- Layers intercept a Vulkan command by defining a C/C++ function with signature
1315identical to the Vulkan API for that command.
Jon Ashburnc9d7fc92016-05-18 14:07:47 -06001316- A layer must intercept at least vkGetInstanceProcAddr and
1317vkCreateInstance. Additionally, a layer would also intercept vkGetDeviceProcAddr and vkCreateDevice to participate in the device chain.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001318- For any Vulkan command a layer intercepts which has a non-void return value,
1319an appropriate value must be returned by the layer intercept function.
1320- The layer intercept function must call down the chain to the corresponding
1321Vulkan command in the next entity. Undefined results will occur if a layer
1322doesn't propagate calls down the chain. The two exceptions to this requirement
1323are vkGetInstanceProcAddr and vkGetDeviceProcAddr which only call down the
1324chain for Vulkan commands that they do not intercept.
1325- Layer intercept functions may insert extra calls to Vulkan commands in
1326addition to the intercept. For example, a layer intercepting vkQueueSubmit may
1327want to add a call to vkQueueWaitIdle after calling down the chain for
1328vkQueueSubmit. Any additional calls inserted by a layer must be on the same
1329chain. They should call down the chain.
Jon Ashburnc2972682016-02-08 15:42:01 -07001330
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001331##### Distributed dispatching requirements
Jeff Julianof1619872016-02-17 17:25:42 -05001332
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001333- For each entry point a layer intercepts, it must keep track of the entry
1334point residing in the next entity in the chain it will call down into. In other
1335words, the layer must have a list of pointers to functions of the appropriate
1336type to call into the next entity. This can be implemented in various ways but
1337for clarity will be referred to as a dispatch table.
1338- A layer can use the VkLayerDispatchTable structure as a device dispatch table
1339(see include/vulkan/vk_layer.h).
1340- A layer can use the VkLayerInstanceDispatchTable structure as a instance
1341dispatch table (see include/vulkan/vk_layer.h).
1342- Layers vkGetInstanceProcAddr function uses the next entity's
Jeff Julianof1619872016-02-17 17:25:42 -05001343vkGetInstanceProcAddr to call down the chain for unknown (i.e. non-intercepted)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001344functions.
1345- Layers vkGetDeviceProcAddr function uses the next entity's
Jeff Julianof1619872016-02-17 17:25:42 -05001346vkGetDeviceProcAddr to call down the chain for unknown (i.e. non-intercepted)
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001347functions.
Jon Ashburnc2972682016-02-08 15:42:01 -07001348
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001349##### Layer dispatch initialization
Jeff Julianof1619872016-02-17 17:25:42 -05001350
1351- A layer initializes its instance dispatch table within its vkCreateInstance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001352function.
Jeff Julianof1619872016-02-17 17:25:42 -05001353- A layer initializes its device dispatch table within its vkCreateDevice
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001354function.
1355- The loader passes a linked list of initialization structures to layers via
1356the "pNext" field in the VkInstanceCreateInfo and VkDeviceCreateInfo structures
1357for vkCreateInstance and VkCreateDevice respectively.
1358- The head node in this linked list is of type VkLayerInstanceCreateInfo for
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001359instance and VkLayerDeviceCreateInfo for device. See file
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001360include/vulkan/vk_layer.h for details.
1361- A VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO is used by the loader for the
1362"sType" field in VkLayerInstanceCreateInfo.
1363- A VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO is used by the loader for the
1364"sType" field in VkLayerDeviceCreateInfo.
1365- The "function" field indicates how the union field "u" should be interpreted
1366within VkLayer*CreateInfo. The loader will set the "function" field to
1367VK_LAYER_LINK_INFO. This indicates "u" field should be VkLayerInstanceLink or
1368VkLayerDeviceLink.
Jon Ashburnc2972682016-02-08 15:42:01 -07001369- The VkLayerInstanceLink and VkLayerDeviceLink structures are the list nodes.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001370- The VkLayerInstanceLink contains the next entity's vkGetInstanceProcAddr used
1371by a layer.
1372- The VkLayerDeviceLink contains the next entity's vkGetInstanceProcAddr and
1373vkGetDeviceProcAddr used by a layer.
1374- Given the above structures set up by the loader, layer must initialize their
1375dispatch table as follows:
1376 - Find the VkLayerInstanceCreateInfo/VkLayerDeviceCreateInfo structure in
1377the VkInstanceCreateInfo/VkDeviceCreateInfo structure.
Jon Ashburncc300a22016-02-11 14:57:30 -07001378 - Get the next entity's vkGet*ProcAddr from the "pLayerInfo" field.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001379 - For CreateInstance get the next entity's vkCreateInstance by calling the
1380"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001381 pfnNextGetInstanceProcAddr(NULL, "vkCreateInstance").
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001382 - For CreateDevice get the next entity's vkCreateDevice by calling the
1383"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001384 pfnNextGetInstanceProcAddr(NULL, "vkCreateDevice").
Jon Ashburncc300a22016-02-11 14:57:30 -07001385 - Advanced the linked list to the next node: pLayerInfo = pLayerInfo->pNext.
1386 - Call down the chain either CreateDevice or CreateInstance
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001387 - Initialize your layer dispatch table by calling the next entity's
1388Get*ProcAddr function once for each Vulkan command needed in your dispatch
1389table
Jon Ashburncc300a22016-02-11 14:57:30 -07001390
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001391##### Example code for CreateInstance
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001392
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001393```cpp
1394VkResult vkCreateInstance(
1395 const VkInstanceCreateInfo *pCreateInfo,
1396 const VkAllocationCallbacks *pAllocator,
1397 VkInstance *pInstance)
1398{
1399 VkLayerInstanceCreateInfo *chain_info =
1400 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1401
1402 assert(chain_info->u.pLayerInfo);
1403 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1404 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1405 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001406 (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001407 if (fpCreateInstance == NULL) {
1408 return VK_ERROR_INITIALIZATION_FAILED;
1409 }
1410
1411 // Advance the link info for the next element of the chain
1412 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1413
1414 // Continue call down the chain
1415 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
1416 if (result != VK_SUCCESS)
1417 return result;
1418
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001419 // Init layer's dispatch table using GetInstanceProcAddr of
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001420 // next layer in the chain.
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001421 instance_dispatch_table = new VkLayerInstanceDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001422 layer_init_instance_dispatch_table(
1423 *pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
1424
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001425 // Other layer initialization
1426 ...
1427
1428 return VK_SUCCESS;
1429}
1430```
1431
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001432##### Example code for CreateDevice
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001433
1434```cpp
1435VkResult
1436vkCreateDevice(
1437 VkPhysicalDevice gpu,
1438 const VkDeviceCreateInfo *pCreateInfo,
1439 const VkAllocationCallbacks *pAllocator,
1440 VkDevice *pDevice)
1441{
1442 VkLayerDeviceCreateInfo *chain_info =
1443 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1444
1445 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1446 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1447 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr =
1448 chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1449 PFN_vkCreateDevice fpCreateDevice =
1450 (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
1451 if (fpCreateDevice == NULL) {
1452 return VK_ERROR_INITIALIZATION_FAILED;
1453 }
1454
1455 // Advance the link info for the next element on the chain
1456 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1457
1458 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
1459 if (result != VK_SUCCESS) {
1460 return result;
1461 }
1462
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001463 // initialize layer's dispatch table
1464 device_dispatch_table = new VkLayerDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001465 layer_init_device_dispatch_table(
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001466 *pDevice, device_dispatch_table, fpGetDeviceProcAddr);
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001467
1468 // Other layer initialization
1469 ...
1470
1471 return VK_SUCCESS;
1472}
1473```
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001474
Jon Ashburncc300a22016-02-11 14:57:30 -07001475#### Special Considerations
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001476##### Associating private data with Vulkan objects within a layer
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001477A layer may want to associate it's own private data with one or more Vulkan
Mark Youngf7914cf2016-08-31 11:53:26 -06001478objects. Two common methods to do this are hash maps and object wrapping.
1479
1480###### Wrapping:
1481
Ian Elliott0b082e42016-08-31 14:08:44 -06001482The loader supports layers wrapping any Vulkan object including dispatchable
1483objects.
1484For commands that return object handles, the layer saves the handle that is
1485returned from a lower-level layer (possibly the ICD), and returns its own
1486handle to the layer above it (possibly the application). For commands that are
1487given previously-returned handles, the layer unwraps the handle; that is it
1488looks up the saved handle and gives that to the layer below it.
1489
1490Layers which wrap objects must ensure they always unwrap objects before passing
1491them down the chain. This means that the layer must intercept every Vulkan
1492command which uses the object in question, and wrap or unwrap the object, as
1493appropriate. This includes adding support for all extensions with commands
1494using any object the layer wraps.
Mark Youngf7914cf2016-08-31 11:53:26 -06001495
1496Layers above the object wrapping layer will see the wrapped object. Layers
1497which wrap dispatchable objects must ensure that the first field in the wrapping
1498structure is a pointer to a dispatch table as defined in vk_layer.h. Specifically, an
1499instance wrapped dispatchable object could be as follows:
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001500```
1501struct my_wrapped_instance_obj_ {
1502 VkLayerInstanceDispatchTable *disp;
1503 // whatever data layer wants to add to this object
1504};
1505```
1506A device wrapped dispatchable object could be as follows:
1507```
1508struct my_wrapped_instance_obj_ {
1509 VkLayerDispatchTable *disp;
1510 // whatever data layer wants to add to this object
1511};
1512```
Jeff Julianof1619872016-02-17 17:25:42 -05001513
Ian Elliott0b082e42016-08-31 14:08:44 -06001514Layers that wrap dispatchable objects must follow the guidelines for creating
1515new dispatchable objects (below).
1516
Mark Young67929282016-09-01 13:43:04 -06001517<u><b>Cautions</b></u>
Ian Elliott0b082e42016-08-31 14:08:44 -06001518
1519Layers are generally discouraged from wrapping objects, because of the
1520potential for incompatibilities with new extensions. For example, let's say
1521that a layer wraps VkImage objects, and properly wraps and unwraps VkImage
1522object handles for all core commands. If a new extension is created which has
1523commands that take VkImage objects as parameters, and if the layer does not
1524support those new commands, an application that uses both the layer and the new
1525extension will have undefined behavior when those new commands are called (e.g.
1526the application may crash). This is becaues the lower-level layers and ICD
1527won't receive the handle that they generated. Instead, they will receive a
1528handle that is only known by the layer that is wrapping the object.
1529
1530Because of the potential for incompatibilities with unsupported extensions,
1531layers that wrap objects must check which extensions are being used by the
1532application, and take appropriate action if the layer is used with unsupported
1533extensions (e.g. disable layer functionality, stop wrapping objects, issue a
1534message to the user).
1535
1536The reason that the validation layers wrap objects, is to track the proper use
1537and destruction of each object. They issue a validation error if used with
1538unsupported extensions, alerting the user to the potential for undefined
1539behavior.
1540
Mark Youngf7914cf2016-08-31 11:53:26 -06001541###### Hash Maps:
Jeff Julianof1619872016-02-17 17:25:42 -05001542Alternatively, a layer may want to use a hash map to associate data with a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001543given object. The key to the map could be the object. Alternatively, for
1544dispatchable objects at a given level (eg device or instance) the layer may
1545want data associated with the VkDevice or VkInstance objects. Since
Jeff Julianof1619872016-02-17 17:25:42 -05001546there are multiple dispatchable objects for a given VkInstance or VkDevice, the
1547VkDevice or VkInstance object is not a great map key. Instead the layer should
1548use the dispatch table pointer within the VkDevice or VkInstance since that
1549will be unique for a given VkInstance or VkDevice.
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001550
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001551##### Creating new dispatchable objects
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001552Layers which create dispatchable objects take special care. Remember that loader
1553trampoline code normally fills in the dispatch table pointer in the newly
1554created object. Thus, the layer must fill in the dispatch table pointer if the
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001555loader trampoline will not do so. Common cases where a layer (or ICD) may create a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001556dispatchable object without loader trampoline code is as follows:
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001557- object wrapping layers that wrap dispatchable objects
1558- layers which add extensions that create dispatchable objects
1559- layers which insert extra Vulkan commands in the stream of commands they
1560intercept from the application
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001561- ICDs which add extensions that create dispatchable objects
1562
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001563The Windows/Linux loader provides a callback that can be used for initializing
1564a dispatchable object. The callback is passed as an extension structure via the
1565pNext field in VkInstanceCreateInfo and VkDeviceCreateInfo. The callback prototype
1566is defined as follows for instance and device callbacks respectively (see vk_layer.h):
1567```
1568VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceLoaderData(VkInstance instance, void *object);
1569VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceLoaderData)(VkDevice device, void *object);
1570```
1571To obtain these callbacks the layer must search through the list of structures
1572pointed 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:
1573- For CreateInstance the callback structure pointed to by "pNext" is VkLayerInstanceCreateInfo as defined in vk_layer.h.
1574- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO within VkInstanceCreateInfo parameter indicates a loader structure.
1575- Within VkLayerInstanceCreateInfo, the "function" field indicates how the union field "u" should be interpreted.
1576- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will contain the callback in "pfnSetInstanceLoaderData".
1577- For CreateDevice the callback structure pointed to by "pNext" is VkLayerDeviceCreateInfo as defined in include/vulkan/vk_layer.h.
1578- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO within VkDeviceCreateInfo parameter indicates a loader structure.
1579- Within VkLayerDeviceCreateInfo, the "function" field indicates how the union field "u" should be interpreted.
1580- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will contain the callback in "pfnSetDeviceLoaderData".
1581
1582Alternatively, 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 -07001583To fill in the dispatch table pointer in newly created dispatchable object,
1584the 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
1585device). 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 -07001586