blob: a1bbbfe7361202454fa81c9d94041e12e5169bd9 [file] [log] [blame] [view]
Mark Young39389872017-01-19 21:10:49 -07001# Architecture of the Vulkan Loader Interfaces
Jon Ashburnc2972682016-02-08 15:42:01 -07002
Mark Young39389872017-01-19 21:10:49 -07003## Table of Contents
Lenny Komowde3924a2017-05-04 14:50:01 -06004 * [Overview](#overview)
5 * [Who Should Read This Document](#who-should-read-this-document)
6 * [The Loader](#the-loader)
7 * [Layers](#layers)
8 * [Installable Client Drivers](#installable-client-drivers)
9 * [Instance Versus Device](#instance-versus-device)
10 * [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains)
Jon Ashburnc2972682016-02-08 15:42:01 -070011
Lenny Komowde3924a2017-05-04 14:50:01 -060012 * [Application Interface to the Loader](#application-interface-to-the-loader)
13 * [Interfacing with Vulkan Functions](#interfacing-with-vulkan-functions)
14 * [Application Layer Usage](#application-layer-usage)
15 * [Application Usage of Extensions](#application-usage-of-extensions)
Jon Ashburnc2972682016-02-08 15:42:01 -070016
Lenny Komowde3924a2017-05-04 14:50:01 -060017 * [Loader and Layer Interface](#loader-and-layer-interface)
18 * [Layer Discovery](#layer-discovery)
19 * [Layer Version Negotiation](#layer-version-negotiation)
20 * [Layer Call Chains and Distributed Dispatch](#layer-call-chains-and-distributed-dispatch)
21 * [Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-extensions)
22 * [Layer Intercept Requirements](#layer-intercept-requirements)
23 * [Distributed Dispatching Requirements](#distributed-dispatching-requirements)
24 * [Layer Conventions and Rules](#layer-conventions-and-rules)
25 * [Layer Dispatch Initialization](#layer-dispatch-initialization)
26 * [Example Code for CreateInstance](#example-code-for-createinstance)
27 * [Example Code for CreateDevice](#example-code-for-createdevice)
Mark Youngc82a0622017-05-05 11:17:17 -060028 * [Meta-layers](#meta-layers)
Lenny Komowde3924a2017-05-04 14:50:01 -060029 * [Special Considerations](#special-considerations)
30 * [Layer Manifest File Format](#layer-manifest-file-format)
31 * [Layer Library Versions](#layer-library-versions)
Jon Ashburnc2972682016-02-08 15:42:01 -070032
Lenny Komowde3924a2017-05-04 14:50:01 -060033 * [Vulkan Installable Client Driver interface with the loader](#vulkan-installable-client-driver-interface-with-the-loader)
34 * [ICD Discovery](#icd-discovery)
35 * [ICD Manifest File Format](#icd-manifest-file-format)
36 * [ICD Vulkan Entry-Point Discovery](#icd-vulkan-entry-point-discovery)
37 * [ICD Unknown Physical Device Extensions](#icd-unknown-physical-device-extensions)
38 * [ICD Dispatchable Object Creation](#icd-dispatchable-object-creation)
39 * [Handling KHR Surface Objects in WSI Extensions](#handling-khr-surface-objects-in-wsi-extensions)
40 * [Loader and ICD Interface Negotiation](#loader-and-icd-interface-negotiation)
Mark Young540d71c2017-05-18 14:52:14 -060041
42 * [Table of Debug Environment Variables](#table-of-debug-environment-variables)
Lenny Komowde3924a2017-05-04 14:50:01 -060043 * [Glossary of Terms](#glossary-of-terms)
Mark Young39389872017-01-19 21:10:49 -070044
45## Overview
Mark Youngcb6e6702016-07-20 11:38:53 -060046
Mark Young39389872017-01-19 21:10:49 -070047Vulkan is a layered architecture, made up of the following elements:
Lenny Komowde3924a2017-05-04 14:50:01 -060048 * The Vulkan Application
49 * [The Vulkan Loader](#the-loader)
50 * [Vulkan Layers](#layers)
51 * [Installable Client Drivers (ICDs)](#installable-client-drivers)
Jon Ashburnc2972682016-02-08 15:42:01 -070052
Mark Young39389872017-01-19 21:10:49 -070053![High Level View of Loader](./images/high_level_loader.png)
Jon Ashburnc2972682016-02-08 15:42:01 -070054
Mark Young39389872017-01-19 21:10:49 -070055The general concepts in this document are applicable to the loaders available
56for Windows, Linux and Android based systems.
Jon Ashburnc2972682016-02-08 15:42:01 -070057
58
Mark Young39389872017-01-19 21:10:49 -070059#### Who Should Read This Document
Jon Ashburnc2972682016-02-08 15:42:01 -070060
Mark Young39389872017-01-19 21:10:49 -070061While this document is primarily targeted at developers of Vulkan applications,
62drivers and layers, the information contained in it could be useful to anyone
63wanting a better understanding of the Vulkan runtime.
Jon Ashburnc2972682016-02-08 15:42:01 -070064
Jon Ashburnc2972682016-02-08 15:42:01 -070065
Mark Young39389872017-01-19 21:10:49 -070066#### The Loader
Jon Ashburnc2972682016-02-08 15:42:01 -070067
Jeff Juliano18e50202017-01-31 16:36:18 -050068The application sits on one end of, and interfaces directly with, the
69loader. On the other end of the loader from the application are the ICDs, which
Mark Young39389872017-01-19 21:10:49 -070070control the Vulkan-capable hardware. An important point to remember is that
Jeff Juliano18e50202017-01-31 16:36:18 -050071Vulkan-capable hardware can be graphics-based, compute-based, or both. Between
72the application and the ICDs the loader can inject a number of optional
73[layers](#layers) that provide special functionality.
Mark Youngcb6e6702016-07-20 11:38:53 -060074
75The loader is responsible for working with the various layers as well as
Mark Young39389872017-01-19 21:10:49 -070076supporting multiple GPUs and their drivers. Any Vulkan function may
Mark Youngcb6e6702016-07-20 11:38:53 -060077wind up calling into a diverse set of modules: loader, layers, and ICDs.
78The loader is critical to managing the proper dispatching of Vulkan
Mark Young39389872017-01-19 21:10:49 -070079functions to the appropriate set of layers and ICDs. The Vulkan object
Mark Youngcb6e6702016-07-20 11:38:53 -060080model allows the loader to insert layers into a call chain so that the layers
Mark Young39389872017-01-19 21:10:49 -070081can process Vulkan functions prior to the ICD being called.
82
83This document is intended to provide an overview of the necessary interfaces
84between each of these.
85
86
87##### Goals of the Loader
88
89The loader was designed with the following goals in mind.
90 1. Support one or more Vulkan-capable ICD on a user's computer system without
91them interfering with one another.
92 2. Support Vulkan Layers which are optional modules that can be enabled by an
93application, developer, or standard system settings.
94 3. Impact the overall performance of a Vulkan application in the lowest
95possible fashion.
96
97
98#### Layers
99
100Layers are optional components that augment the Vulkan system. They can
101intercept, evaluate, and modify existing Vulkan functions on their way from the
102application down to the hardware. Layers are implemented as libraries that can
103be enabled in different ways (including by application request) and are loaded
104during CreateInstance. Each layer can choose to hook (intercept) any Vulkan
105functions which in turn can be ignored or augmented. A layer does not need to
106intercept all Vulkan functions. It may choose to intercept all known functions,
107or, it may choose to intercept only one function.
108
109Some examples of features that layers may expose include:
110 * Validating API usage
111 * Adding the ability to perform Vulkan API tracing and debugging
112 * Overlay additional content on the applications surfaces
113
114Because layers are optionally, you may choose to enable layers for debugging
115your application, but then disable any layer usage when you release your
116product.
117
118
119#### Installable Client Drivers
120
121Vulkan allows multiple Installable Client Drivers (ICDs) each supporting one
122or more devices (represented by a Vulkan `VkPhysicalDevice` object) to be used
123collectively. The loader is responsible for discovering available Vulkan ICDs on
124the system. Given a list of available ICDs, the loader can enumerate all the
125physical devices available for an application and return this information to
126the application.
127
128
129#### Instance Versus Device
130
131There is an important concept which you will see brought up repeatedly
132throughout this document. Many functions, extensions, and other things in
133Vulkan are separated into two main groups:
134 * Instance-related Objects
135 * Device-related Objects
136
137
138##### Instance-related Objects
139
140A Vulkan Instance is a high-level construct used to provide Vulkan system-level
141information, or functionality. Vulkan objects associated directly with an
142instance are:
143 * `VkInstance`
144 * `VkPhysicalDevice`
145
146An Instance function is any Vulkan function which takes as its first parameter
147either an object from the Instance list, or nothing at all. Some Vulkan
148Instance functions are:
149 * `vkEnumerateInstanceExtensionProperties`
150 * `vkEnumeratePhysicalDevices`
151 * `vkCreateInstance`
152 * `vkDestroyInstance`
153
154You query Vulkan Instance functions using `vkGetInstanceProcAddr`.
155`vkGetInstanceProcAddr` can be used to query either device or instance entry-
156points in addition to all core entry-points. The returned function pointer is
157valid for this Instance and any object created under this Instance (including
158all `VkDevice` objects).
159
160Similarly, an Instance extension is a set of Vulkan Instance functions extending
161the Vulkan language. These will be discussed in more detail later.
162
163
164##### Device-related Objects
165
166A Vulkan Device, on the other-hand, is a logical identifier used to associate
167functions with a particular physical device on a user's system. Vulkan
168constructs associated directly with a device include:
169 * `VkDevice`
170 * `VkQueue`
171 * `VkCommandBuffer`
172 * Any dispatchable object that is a child of a one of the above.
173
174A Device function is any Vulkan function which takes any Device Object as its
175first parameter. Some Vulkan Device functions are:
176 * `vkQueueSubmit`
177 * `vkBeginCommandBuffer`
178 * `vkCreateEvent`
179
180You can query Vulkan Device functions using either `vkGetInstanceProcAddr` or
181`vkGetDeviceProcAddr`. If you choose to use `vkGetInstanceProcAddr`, it will
182have an additional level built into the call chain, which will reduce
183performance slightly. However, the function pointer returned can be used for
184any device created later, as long as it is associated with the same Vulkan
185Instance. If, instead you use `vkGetDeviceProcAddr`, the call chain will be more
186optimized to the specific device, but it will **only** work for the device used
187to query the function function pointer. Also, unlike `vkGetInstanceProcAddr`,
188`vkGetDeviceProcAddr` can only be used on core Vulkan Device functions, or
189Device extension functions.
190
191The best solution is to query Instance extension functions using
192`vkGetInstanceProcAddr`, and to query Device extension functions using
193`vkGetDeviceProcAddr`. See
194[Best Application Performance Setup](#best-application-performance-setup) for
195more information on this.
196
197As with Instance extensions, a Device extension is a set of Vulkan Device
198functions extending the Vulkan language. You can read more about these later in
199the document.
200
201
202#### Dispatch Tables and Call Chains
Jon Ashburnc2972682016-02-08 15:42:01 -0700203
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700204Vulkan uses an object model to control the scope of a particular action /
205operation. The object to be acted on is always the first parameter of a Vulkan
Mark Young6d026a72016-06-01 17:49:30 -0600206call and is a dispatchable object (see Vulkan specification section 2.3 Object
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700207Model). Under the covers, the dispatchable object handle is a pointer to a
Mark Youngcb6e6702016-07-20 11:38:53 -0600208structure, which in turn, contains a pointer to a dispatch table maintained by
Mark Young39389872017-01-19 21:10:49 -0700209the loader. This dispatch table contains pointers to the Vulkan functions
210appropriate to that object.
Jon Ashburnc2972682016-02-08 15:42:01 -0700211
Mark Youngcb6e6702016-07-20 11:38:53 -0600212There are two types of dispatch tables the loader maintains:
Mark Young39389872017-01-19 21:10:49 -0700213 - Instance Dispatch Table
214 - Created in the loader during the call to `vkCreateInstance`
215 - Device Dispatch Table
216 - Created in the loader during the call to `vkCreateDevice`
Jon Ashburnc2972682016-02-08 15:42:01 -0700217
Mark Young39389872017-01-19 21:10:49 -0700218At that time the application and/or system can specify optional layers to be
219included. The loader will initialize the specified layers to create a call
220chain for each Vulkan function and each entry of the dispatch table will point
221to the first element of that chain. Thus, the loader builds an instance call
222chain for each `VkInstance` that is created and a device call chain for each
223`VkDevice` that is created.
224
225When an application calls a Vulkan function, this typically will first hit a
226*trampoline* function in the loader. These *trampoline* functions are small,
227simple functions that jump to the appropriate dispatch table entry for the
228object they are given. Additionally, for functions in the instance call chain,
229the loader has an additional function, called a *terminator*, which is called
230after all enabled layers to marshall the appropriate information to all
231available ICDs.
232
233
234##### Instance Call Chain Example
Jon Ashburnc2972682016-02-08 15:42:01 -0700235
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700236For example, the diagram below represents what happens in the call chain for
Mark Young39389872017-01-19 21:10:49 -0700237`vkCreateInstance`. After initializing the chain, the loader will call into the
238first layer's `vkCreateInstance` which will call the next finally terminating in
239the loader again where this function calls every ICD's `vkCreateInstance` and
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700240saves the results. This allows every enabled layer for this chain to set up
Mark Young39389872017-01-19 21:10:49 -0700241what it needs based on the `VkInstanceCreateInfo` structure from the
242application.
243
244![Instance Call Chain](./images/loader_instance_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -0700245
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700246This also highlights some of the complexity the loader must manage when using
Mark Young39389872017-01-19 21:10:49 -0700247instance call chains. As shown here, the loader's *terminator* must aggregate
248information to and from multiple ICDs when they are present. This implies that
249the loader has to be aware of any instance-level extensions which work on a
250`VkInstance` to aggregate them correctly.
Jon Ashburnc2972682016-02-08 15:42:01 -0700251
Mark Young39389872017-01-19 21:10:49 -0700252
253##### Device Call Chain Example
254
255Device call chains are created at `vkCreateDevice` and are generally simpler
256because they deal with only a single device and the ICD can always be the
257*terminator* of the chain.
258
259![Loader Device Call Chain](./images/loader_device_chain_loader.png)
260
Jon Ashburnc2972682016-02-08 15:42:01 -0700261
Mark Youngcb6e6702016-07-20 11:38:53 -0600262<br/>
Mark Young39389872017-01-19 21:10:49 -0700263<br/>
Mark Youngcb6e6702016-07-20 11:38:53 -0600264
Mark Young39389872017-01-19 21:10:49 -0700265## Application Interface to the Loader
Jon Ashburnc2972682016-02-08 15:42:01 -0700266
Mark Young39389872017-01-19 21:10:49 -0700267In this section we'll discuss how an application interacts with the loader,
268including:
Lenny Komowde3924a2017-05-04 14:50:01 -0600269 * [Interfacing with Vulkan Functions](#interfacing-with-vulkan-functions)
270 * [Vulkan Direct Exports](#vulkan-direct-exports)
271 * [Directly Linking to the Loader](#directly-linking-to-the-loader)
272 * [Dynamic Linking](#dynamic-linking)
273 * [Static Linking](#static-linking)
274 * [Indirectly Linking to the Loader](#indirectly-linking-to-the-loader)
275 * [Best Application Performance Setup](#best-application-performance-setup)
276 * [ABI Versioning](#abi-versioning)
277 * [Application Layer Usage](#application-layer-usage)
278 * [Implicit vs Explicit Layers](#implicit-vs-explicit-layers)
279 * [Forcing Layer Source Folders](#forcing-layer-source-folders)
280 * [Forcing Layers to be Enabled](#forcing-layers-to-be-enabled)
281 * [Overall Layer Ordering](#overall-layer-ordering)
282 * [Application Usage of Extensions](#application-usage-of-extensions)
283 * [Instance and Device Extensions](#instance-and-device-extensions)
284 * [WSI Extensions](#wsi-extensions)
285 * [Unknown Extensions](#unknown-extensions)
Jon Ashburnc2972682016-02-08 15:42:01 -0700286
Mark Young39389872017-01-19 21:10:49 -0700287
288#### Interfacing with Vulkan Functions
289There are several ways you can interface with Vulkan functions through the
290loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700291
Jon Ashburnc2972682016-02-08 15:42:01 -0700292
Mark Young39389872017-01-19 21:10:49 -0700293##### Vulkan Direct Exports
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700294The loader library on Windows, Linux and Android will export all core Vulkan
295and all appropriate Window System Interface (WSI) extensions. This is done to
296make it simpler to get started with Vulkan development. When an application
297links directly to the loader library in this way, the Vulkan calls are simple
Mark Young39389872017-01-19 21:10:49 -0700298*trampoline* functions that jump to the appropriate dispatch table entry for the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700299object they are given.
Jon Ashburnc2972682016-02-08 15:42:01 -0700300
Mark Young39389872017-01-19 21:10:49 -0700301
Lenny Komowde3924a2017-05-04 14:50:01 -0600302##### Directly Linking to the Loader
303
304###### Dynamic Linking
305The loader is ordinarily distributed as a dynamic library (.dll on Windows or
306.so on Linux) which gets installed to the system path for dynamic libraries.
307Linking to the dynamic library is generally the preferred method of linking to
308the loader, as doing so allows the loader to be updated for bug fixes and
309improvements. Furthermore, the dynamic library is generally installed to Windows
310systems as part of driver installation and is generally provided on Linux
311through the system package manager. This means that applications can usually
312expect a copy of the loader to be present on a system. If applications want to
313be completely sure that a loader is present, they can include a loader or
314runtime installer with their application.
315
316###### Static Linking
317The loader can also be used as a static library (this is shipped in the
318Windows SDK as `VKstatic.1.lib`). Linking to the static loader means that the
319user does not need to have a Vulkan runtime installed, and it also guarantees
320that your application will use a specific version of the loader. However, there
321are several downsides to this approach:
322
323 - The static library can never be updated without re-linking the application
324 - This opens up the possibility that two included libraries could contain
325 different versions of the loader
326 - This could potentially cause conflicts between the different loader versions
327
328As a result, it is recommended that users prefer linking to the .dll and
329.so versions of the loader.
330
331
Mark Young39389872017-01-19 21:10:49 -0700332##### Indirectly Linking to the Loader
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700333Applications are not required to link directly to the loader library, instead
334they can use the appropriate platform specific dynamic symbol lookup on the
Mark Young6d026a72016-06-01 17:49:30 -0600335loader library to initialize the application's own dispatch table. This allows
Mark Young39389872017-01-19 21:10:49 -0700336an application to fail gracefully if the loader cannot be found. It also
Jeff Julianof1619872016-02-17 17:25:42 -0500337provides the fastest mechanism for the application to call Vulkan functions. An
338application will only need to query (via system calls such as dlsym()) the
Mark Young39389872017-01-19 21:10:49 -0700339address of `vkGetInstanceProcAddr` from the loader library. Using
340`vkGetInstanceProcAddr` the application can then discover the address of all
341functions and extensions available, such as `vkCreateInstance`,
342`vkEnumerateInstanceExtensionProperties` and
343`vkEnumerateInstanceLayerProperties` in a platform-independent way.
Jon Ashburnc2972682016-02-08 15:42:01 -0700344
Mark Young39389872017-01-19 21:10:49 -0700345
346##### Best Application Performance Setup
347
348If you desire the best performance possible, you should setup your own
349dispatch table so that all your Instance functions are queried using
350`vkGetInstanceProcAddr` and all your Device functions are queried using
351`vkGetDeviceProcAddr`.
352
353*Why should you do this?*
354
355The answer comes in how the call chain of Instance functions are implemented
356versus the call chain of a Device functions. Remember, a [Vulkan Instance is a
357high-level construct used to provide Vulkan system-level information](#instance-
358related-objects). Because of this, Instance functions need to be broadcasted to
359every available ICD on the system. The following diagram shows an approximate
360view of an Instance call chain with 3 enabled layers:
361
362![Instance Call Chain](./images/loader_instance_chain.png)
363
364This is also how a Vulkan Device function call chain looks if you query it
365using `vkGetInstanceProcAddr`. On the otherhand, a Device
366function doesn't need to worry about the broadcast becuase it knows specifically
367which associated ICD and which associated Physical Device the call should
368terminate at. Because of this, the loader doesn't need to get involved between
369any enabled layers and the ICD. Thus, if you used a loader-exported Vulkan
370Device function, the call chain in the same scenario as above would look like:
371
372![Loader Device Call Chain](./images/loader_device_chain_loader.png)
373
374An even better solution would be for an application to perform a
375`vkGetDeviceProcAddr` call on all Device functions. This further optimizes the
376call chain by removing the loader all-together under most scenarios:
377
378![Application Device Call Chain](./images/loader_device_chain_app.png)
379
380Also, notice if no layers are enabled, your application function pointer would
381point **directly to the ICD**. If called enough, those fewer calls can add up
382to performance savings.
383
384**NOTE:** There are some Device functions which still require the loader to
385intercept them with a *trampoline* and *terminator*. There are very few of
386these, but they are typically functions which the loader wraps with its own
387data. In those cases, even the Device call chain will continue to look like the
388Instance call chain. One example of a Device function requiring a *terminator*
389is `vkCreateSwapchainKHR`. For that function, the loader needs to potentially
390convert the KHR_surface object into an ICD-specific KHR_surface object prior to
391passing down the rest of the function's information to the ICD.
392
393Remember:
394 * `vkGetInstanceProcAddr` can be used to query
395either device or instance entry-points in addition to all core entry-points.
396 * `vkGetDeviceProcAddr` can only be used to query for device
397extension or core device entry-points.
398
399
400##### ABI Versioning
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700401The Vulkan loader library will be distributed in various ways including Vulkan
Mark Young39389872017-01-19 21:10:49 -0700402SDKs, OS package distributions and Independent Hardware Vendor (IHV) driver
403packages. These details are beyond the scope of this document. However, the name
404and versioning of the Vulkan loader library is specified so an app can link to
405the correct Vulkan ABI library version. Vulkan versioning is such that ABI
406backwards compatibility is guaranteed for all versions with the same major
407number (e.g. 1.0 and 1.1). On Windows, the loader library encodes the ABI
408version in its name such that multiple ABI incompatible versions of the loader
409can peacefully coexist on a given system. The Vulkan loader library file name is
Jeff Juliano18e50202017-01-31 16:36:18 -0500410`vulkan-<ABI version>.dll`. For example, for Vulkan version 1.X on Windows the
Mark Young39389872017-01-19 21:10:49 -0700411library filename is vulkan-1.dll. And this library file can typically be found
412in the windows/system32 directory (on 64-bit Windows installs, the 32-bit
413version of the loader with the same name can be found in the windows/sysWOW64
414directory).
Jon Ashburnc2972682016-02-08 15:42:01 -0700415
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700416For Linux, shared libraries are versioned based on a suffix. Thus, the ABI
417number is not encoded in the base of the library filename as on Windows. On
418Linux an application wanting to link to the latest Vulkan ABI version would
419just link to the name vulkan (libvulkan.so). A specific Vulkan ABI version can
Jeff Julianof1619872016-02-17 17:25:42 -0500420also be linked to by applications (e.g. libvulkan.so.1).
Jon Ashburnc2972682016-02-08 15:42:01 -0700421
Mark Young39389872017-01-19 21:10:49 -0700422
423#### Application Layer Usage
Mark Young6d026a72016-06-01 17:49:30 -0600424
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700425Applications desiring Vulkan functionality beyond what the core API offers may
Mark Young39389872017-01-19 21:10:49 -0700426use various layers or extensions. A layer cannot introduce new Vulkan core API
427entry-points to an application that are not exposed in Vulkan.h. However,
428layers may offer extensions that introduce new Vulkan commands that can be
429queried through the extension interface.
Mark Young02ee5382016-07-22 08:51:05 -0600430
Mark Young39389872017-01-19 21:10:49 -0700431A common use of layers is for API validation which can be enabled by
432loading the layer during application development, but not loading the layer
433for application release. This eliminates the overhead of validating the
434application's usage of the API, something that wasn't available on some previous
435graphics APIs.
436
437To find out what layers are available to your application, use
438`vkEnumerateInstanceLayerProperties`. This will report all layers
439that have been discovered by the loader. The loader looks in various locations
440to find layers on the system. For more information see the
441[Layer discovery](#layer-discovery) section below.
442
443To enable a layer, or layers, simply pass the name of the layers you wish to
444enable in the `ppEnabledLayerNames` field of the `VkInstanceCreateInfo` during
445a call to `vkCreateInstance`. Once done, the layers you have enabled will be
446active for all Vulkan functions using the created `VkInstance`, and any of
447its child objects.
448
449**NOTE:** Layer ordering is important in several cases since some layers
450interact with each other. Be careful when enabling layers as this may be
451the case. See the [Overall Layer Ordering](#overall-layer-ordering) section
452for more information.
453
454The following code section shows how you would go about enabling the
455VK_LAYER_LUNARG_standard_validation layer.
456
457```
458 char *instance_validation_layers[] = {
459 "VK_LAYER_LUNARG_standard_validation"
460 };
461 const VkApplicationInfo app = {
462 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
463 .pNext = NULL,
464 .pApplicationName = "TEST_APP",
465 .applicationVersion = 0,
466 .pEngineName = "TEST_ENGINE",
467 .engineVersion = 0,
468 .apiVersion = VK_API_VERSION_1_0,
469 };
470 VkInstanceCreateInfo inst_info = {
471 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
472 .pNext = NULL,
473 .pApplicationInfo = &app,
474 .enabledLayerCount = 1,
475 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
476 .enabledExtensionCount = 0,
477 .ppEnabledExtensionNames = NULL,
478 };
479 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
480```
481
482At `vkCreateInstance` and `vkCreateDevice`, the loader constructs call chains
483that include the application specified (enabled) layers. Order is important in
484the `ppEnabledLayerNames` array; array element 0 is the topmost (closest to the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700485application) layer inserted in the chain and the last array element is closest
Mark Young39389872017-01-19 21:10:49 -0700486to the driver. See the [Overall Layer Ordering](#overall-layer-ordering)
487section for more information on layer ordering.
Jon Ashburnc2972682016-02-08 15:42:01 -0700488
Mark Young39389872017-01-19 21:10:49 -0700489**NOTE:** *Device Layers Are Now Deprecated*
490> `vkCreateDevice` originally was able to select layers in a similar manner to
491`vkCreateInstance`. This lead to the concept of "instance
492> layers" and "device layers". It was decided by Khronos to deprecate the
493> "device layer" functionality and only consider "instance layers".
494> Therefore, `vkCreateDevice` will use the layers specified at
495`vkCreateInstance`.
496> Because of this, the following items have been deprecated:
497> * `VkDeviceCreateInfo` fields:
498> * `ppEnabledLayerNames`
499> * `enabledLayerCount`
500> * The `vkEnumerateDeviceLayerProperties` function
Mark Young02ee5382016-07-22 08:51:05 -0600501
Jon Ashburnc2972682016-02-08 15:42:01 -0700502
Mark Young39389872017-01-19 21:10:49 -0700503##### Implicit vs Explicit Layers
Jon Ashburnc2972682016-02-08 15:42:01 -0700504
Mark Young39389872017-01-19 21:10:49 -0700505Explicit layers are layers which are enabled by an application (e.g. with the
506vkCreateInstance function), or by an environment variable (as mentioned
507previously).
Jon Ashburnc2972682016-02-08 15:42:01 -0700508
Mark Young39389872017-01-19 21:10:49 -0700509Implicit layers are those which are enabled by their existence. For example,
510certain application environments (e.g. Steam or an automotive infotainment
511system) may have layers which they always want enabled for all applications
512that they start. Other implicit layers may be for all applications started on a
513given system (e.g. layers that overlay frames-per-second). Implicit layers are
514enabled automatically, whereas explicit layers must be enabled explicitly.
Jon Ashburnc2972682016-02-08 15:42:01 -0700515
Mark Young02ee5382016-07-22 08:51:05 -0600516Implicit layers have an additional requirement over explicit layers in that they
517require being able to be disabled by an environmental variable. This is due
518to the fact that they are not visible to the application and could cause issues.
519A good principle to keep in mind would be to define both an enable and disable
520environment variable so the users can deterministicly enable the functionality.
521On Desktop platforms (Windows and Linux), these enable/disable settings are
522defined in the layer's JSON file.
523
Mark Young39389872017-01-19 21:10:49 -0700524Discovery of system-installed implicit and explicit layers is described later in
525the [Layer Discovery Section](#layer-discovery). For now, simply know that what
526distinguishes a layer as implicit or explicit is dependent on the Operating
527system, as shown in the table below.
528
529| Operating System | Implicit Layer Identification |
530|----------------|--------------------|
531| Windows | Implicit Layers are located in a different Windows registry location than Explicit Layers. |
532| Linux | Implicit Layers are located in a different directory location than Explicit Layers. |
533| Android | There is **No Support For Implicit Layers** on Android. |
534
535
536##### Forcing Layer Source Folders
537
538Developers may need to use special, pre-production layers, without modifying the
539system-installed layers. You can direct the loader to look for layers in a
540specific folder by defining the "VK\_LAYER\_PATH" environment variable. This
541will override the mechanism used for finding system-installed layers. Because
Mark Young18bbaab2017-03-20 08:27:14 -0600542layers of interest may exist in several disinct folders on a system, this
Mark Young39389872017-01-19 21:10:49 -0700543environment variable can containis several paths seperated by the operating
544specific path separator. On Windows, each separate folder should be separated
545in the list using a semi-colon. On Linux, each folder name should be separated
546using a colon.
547
548If "VK\_LAYER\_PATH" exists, **only** the folders listed in it will be scanned
549for layers. Each directory listed should be the full pathname of a folder
550containing layer manifest files.
551
552
553##### Forcing Layers to be Enabled on Windows and Linux
554
555Developers may want to enable layers that are not enabled by the given
556application they are using. On Linux and Windows, the environment variable
557"VK\_INSTANCE\_LAYERS" can be used to enable additional layers which are
558not specified (enabled) by the application at `vkCreateInstance`.
559"VK\_INSTANCE\_LAYERS" is a colon (Linux)/semi-colon (Windows) separated
560list of layer names to enable. Order is relevant with the first layer in the
561list being the top-most layer (closest to the application) and the last
562layer in the list being the bottom-most layer (closest to the driver).
563See the [Overall Layer Ordering](#overall-layer-ordering) section
564for more information.
565
566Application specified layers and user specified layers (via environment
567variables) are aggregated and duplicates removed by the loader when enabling
568layers. Layers specified via environment variable are top-most (closest to the
569application) while layers specified by the application are bottommost.
570
571An example of using these environment variables to activate the validation
572layer `VK_LAYER_LUNARG_parameter_validation` on Windows or Linux is as follows:
573
574```
575> $ export VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_parameter_validation
576```
577
578
579##### Overall Layer Ordering
580
581The overall ordering of all layers by the loader based on the above looks
582as follows:
583
584![Loader Layer Ordering](./images/loader_layer_order.png)
585
586Ordering may also be important internal to the list of Explicit Layers.
587Some layers may be dependent on other behavior being implemented before
588or after the loader calls it. For example: the VK_LAYER_LUNARG_core_validation
589layer expects the VK_LAYER_LUNARG_parameter_validation to be called first.
590This is because the VK_LAYER_LUNARG_parameter_validation will filter out any
591invalid `NULL` pointer calls prior to the rest of the validation checking
592done by VK_LAYER_LUNARG_core_validation. If not done properly, you may see
593crashes in the VK_LAYER_LUNARG_core_validation layer that would otherwise be
594avoided.
595
596
597#### Application Usage of Extensions
598
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700599Extensions are optional functionality provided by a layer, the loader or an
600ICD. Extensions can modify the behavior of the Vulkan API and need to be
Mark Young39389872017-01-19 21:10:49 -0700601specified and registered with Khronos. These extensions can be created
602by an Independent Hardware Vendor (IHV) to expose new hardware functionality,
603or by a layer writer to expose some internal feature, or by the loader to
604improve functional behavior. Information about various extensions can be
605found in the Vulkan Spec, and vulkan.h header file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700606
Mark Young6d026a72016-06-01 17:49:30 -0600607
Mark Young39389872017-01-19 21:10:49 -0700608##### Instance and Device Extensions
609
610As hinted at in the [Instance Versus Device](#instance-versus-device) section,
611there are really two types of extensions:
612 * Instance Extensions
613 * Device Extensions
614
615An Instance extension is an extension which modifies existing behavior or
616implements new behavior on instance-level objects, like a `VkInstance` or
617a `VkPhysicalDevice`. A Device extension is an extension which does the same,
618but for any `VkDevice` object, or any dispatchable object that is a child of a
619`VkDevice` (`VkQueue` and `VkCommandBuffer` are examples of these).
620
621It is **very** important to know what type of extension you are desiring to
622enable as you will enable Instance extensions during `vkCreateInstance` and
623Device extensions during `vkCreateDevice`.
624
625The loader discovers and aggregates all
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700626extensions from layers (both explicit and implicit), ICDs and the loader before
Mark Young39389872017-01-19 21:10:49 -0700627reporting them to the application in `vkEnumerateXXXExtensionProperties`
628(where XXX is either "Instance" or "Device").
629 - Instance extensions are discovered via
630`vkEnumerateInstanceExtensionProperties`.
631 - Device extensions are be discovered via
632`vkEnumerateDeviceExtensionProperties`.
633
634Looking at `vulkan.h`, you'll notice that they are both similar. For example,
635`vkEnumerateInstanceExtensionProperties` prototype looks as follows:
636
637```
638 VkResult
639 vkEnumerateInstanceExtensionProperties(const char *pLayerName,
640 uint32_t *pPropertyCount,
641 VkExtensionProperties *pProperties);
642```
643
644The "pLayerName" parameter in these functions is used to select either a single
645layer or the Vulkan platform implementation. If "pLayerName" is NULL, extensions
646from Vulkan implementation components (including loader, implicit layers, and
647ICDs) are enumerated. If "pLayerName" is equal to a discovered layer module name
648then only extensions from that layer (which may be implicit or explicit) are
Jeff Julianof1619872016-02-17 17:25:42 -0500649enumerated. Duplicate extensions (e.g. an implicit layer and ICD might report
Mark Young39389872017-01-19 21:10:49 -0700650support for the same extension) are eliminated by the loader. For duplicates,
651the ICD version is reported and the layer version is culled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700652
Mark Young39389872017-01-19 21:10:49 -0700653Also, Extensions *must be enabled* (in `vkCreateInstance` or `vkCreateDevice`)
654before the functions associated with the extensions can be used. If you get an
655Extension function using either `vkGetInstanceProcAddr` or
656`vkGetDeviceProcAddr`, but fail to enable it, you could experience undefined
657behavior. This should actually be flagged if you run with Validation layers
658enabled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700659
Courtney Goeltzenleuchterab3a4662016-02-14 10:48:22 -0700660
Mark Young78f88c82016-07-19 11:49:45 -0600661##### WSI Extensions
662
Mark Young39389872017-01-19 21:10:49 -0700663Khronos approved WSI extensions are available and provide Windows System
664Integration support for various execution environments. It is important to
665understand that some WSI extensions are valid for all targets, but others are
666particular to a given execution environment (and loader). This desktop loader
667(currently targeting Windows and Linux) only enables and directly exports those
668WSI extensions that are appropriate to the current environment. For the most
669part, the selection is done in the loader using compile-time preprocessor flags.
670All versions of the desktop loader currently expose at least the following WSI
Mark Young78f88c82016-07-19 11:49:45 -0600671extension support:
672- VK_KHR_surface
673- VK_KHR_swapchain
674- VK_KHR_display
675
Mark Young39389872017-01-19 21:10:49 -0700676In addition, each of the following OS targets for the loader support target-
677specific extensions:
Mark Young78f88c82016-07-19 11:49:45 -0600678
Mark Young39389872017-01-19 21:10:49 -0700679| Windowing System | Extensions available |
680|----------------|--------------------|
681| Windows | VK_KHR_win32_surface |
682| Linux (Default) | VK_KHR_xcb_surface and VK_KHR_xlib_surface |
683| Linux (Wayland) | VK_KHR_wayland_surface |
684| Linux (Mir) | VK_KHR_mir_surface |
Mark Young78f88c82016-07-19 11:49:45 -0600685
Mark Young39389872017-01-19 21:10:49 -0700686**NOTE:** Wayland and Mir targets are not fully supported at this time. Wayland
687support is present, but should be considered Beta quality. Mir support is not
688completely implemented at this time.
689
690It is important to understand that while the loader may support the various
691entry-points for these extensions, there is a hand-shake required to actually
692use them:
Mark Young78f88c82016-07-19 11:49:45 -0600693* At least one physical device must support the extension(s)
694* The application must select such a physical device
Mark Young39389872017-01-19 21:10:49 -0700695* The application must request the extension(s) be enabled while creating the
696instance or logical device (This depends on whether or not the given extension
697works with an instance or a device).
Mark Young78f88c82016-07-19 11:49:45 -0600698* The instance and/or logical device creation must succeed.
699
700Only then can you expect to properly use a WSI extension in your Vulkan program.
701
Mark Young78f88c82016-07-19 11:49:45 -0600702
Mark Young39389872017-01-19 21:10:49 -0700703##### Unknown Extensions
704
705With the ability to expand Vulkan so easily, extensions will be created that the
706loader knows nothing about. If the extension is a device extension, the loader
707will pass the unknown entry-point down the device call chain ending with the
708appropriate ICD entry-points. The same thing will happen, if the extension is
709an instance extension which takes a physical device paramater as it's first
710component. However, for all other instance extensions the loader will fail to
711load it.
Mark Young78f88c82016-07-19 11:49:45 -0600712
713*But why doesn't the loader support unknown instance extensions?*
714<br/>
715Let's look again at the Instance call chain:
Mark Young78f88c82016-07-19 11:49:45 -0600716
Mark Young39389872017-01-19 21:10:49 -0700717![Instance call chain](./images/loader_instance_chain.png)
Mark Young78f88c82016-07-19 11:49:45 -0600718
Mark Young39389872017-01-19 21:10:49 -0700719Notice that for a normal instance function call, the loader has to handle
720passing along the function call to the available ICDs. If the loader has no
721idea of the parameters or return value of the instance call, it can't properly
722pass information along to the ICDs. There may be ways to do this, which will be
723explored in the future. However, for now, this loader does not support
724instance extensions which don't take a physical device as their first parameter.
725
726Because the device call-chain does not normally pass through the loader
727*terminator*, this is not a problem for device extensions. Additionally,
728since a physical device is associated with one ICD, we can use a generic
729*terminator* pointing to one ICD. This is because both of these extensions
730terminate directly in the ICD they are associated with.
Mark Young78f88c82016-07-19 11:49:45 -0600731
732*Is this a big problem?*
733<br/>
Mark Young39389872017-01-19 21:10:49 -0700734No! Most extension functionality only affects either a physical or logical
735device and not an instance. Thus, the overwhelming majority of extensions
736should be supported with direct loader support.
Jon Ashburnc2972682016-02-08 15:42:01 -0700737
Mark Young6340bb82017-02-13 15:39:22 -0700738##### Filtering Out Unknown Instance Extension Names
739In some cases, an ICD may support instance extensions that the loader does not.
740For the above reasons, the loader will filter out the names of these unknown instance
741extensions when an application calls `vkEnumerateInstanceExtensionProperties`.
742Additionally, this behavior will cause the loader to throw an error during
743`vkCreateInstance` if you still attempt to use one of these extensions. The intent is
744to protect applications so that they don't inadvertantly use functionality
745which could lead to a crash.
746
747On the other-hand, if you know you can safely use the extension, you may disable
748the filtering by defining the environment variable `VK_LOADER_DISABLE_INST_EXT_FILTER`
749and setting the value to a non-zero number. This will effectively disable the
750loader's filtering out of instance extension names.
Jon Ashburnc2972682016-02-08 15:42:01 -0700751
Mark Youngcb6e6702016-07-20 11:38:53 -0600752<br/>
Mark Youngcb6e6702016-07-20 11:38:53 -0600753<br/>
754
Mark Young39389872017-01-19 21:10:49 -0700755## Loader and Layer Interface
Jon Ashburnc2972682016-02-08 15:42:01 -0700756
Mark Young39389872017-01-19 21:10:49 -0700757In this section we'll discuss how the loader interacts with layers, including:
Lenny Komowde3924a2017-05-04 14:50:01 -0600758 * [Layer Discovery](#layer-discovery)
759 * [Layer Manifest File Usage](#layer-manifest-file-usage)
760 * [Android Layer Discovery](#android-layer-discovery)
761 * [Windows Layer Discovery](#windows-layer-discovery)
762 * [Linux Layer Discovery](#linux-layer-discovery)
763 * [Layer Version Negotiation](#layer-version-negotiation)
764 * [Layer Call Chains and Distributed Dispatch](#layer-call-chains-and-distributed-dispatch)
765 * [Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-extensions)
766 * [Layer Intercept Requirements](#layer-intercept-requirements)
767 * [Distributed Dispatching Requirements](#distributed-dispatching-requirements)
768 * [Layer Conventions and Rules](#layer-conventions-and-rules)
769 * [Layer Dispatch Initialization](#layer-dispatch-initialization)
770 * [Example Code for CreateInstance](#example-code-for-createinstance)
771 * [Example Code for CreateDevice](#example-code-for-createdevice)
Mark Youngc82a0622017-05-05 11:17:17 -0600772 * [Meta-layers](#meta-layers)
Lenny Komowde3924a2017-05-04 14:50:01 -0600773 * [Special Considerations](#special-considerations)
774 * [Associating Private Data with Vulkan Objects Within a Layer](#associating-private-data-with-vulkan-objects-within-a-layer)
775 * [Wrapping](#wrapping)
776 * [Hash Maps](#hash-maps)
777 * [Creating New Dispatchable Objects](#creating-new-dispatchable-objects)
778 * [Layer Manifest File Format](#layer-manifest-file-format)
779 * [Layer Manifest File Version History](#layer-manifest-file-version-history)
780 * [Layer Library Versions](#layer-library-versions)
781 * [Layer Library API Version 2](#layer-library-api-version-2)
782 * [Layer Library API Version 1](#layer-library-api-version-1)
783 * [Layer Library API Version 0](#layer-library-api-version-0)
Mark Young39389872017-01-19 21:10:49 -0700784
Jon Ashburnc2972682016-02-08 15:42:01 -0700785
Mark Young39389872017-01-19 21:10:49 -0700786
787#### Layer Discovery
Jon Ashburnc2972682016-02-08 15:42:01 -0700788
Mark Young39389872017-01-19 21:10:49 -0700789As mentioned in the
790[Application Interface section](#implicit-vs-explicit-layers),
791layers can be categorized into two categories:
792 * Implicit Layers
793 * Explicit Layers
Jon Ashburnc2972682016-02-08 15:42:01 -0700794
Mark Young39389872017-01-19 21:10:49 -0700795The main difference between the two is that Implicit Layers are automatically
796enabled, unless overriden, and Explicit Layers must be enabled. Remember,
797Implicit Layers are not present on all Operating Systems (like Android).
Jon Ashburnc2972682016-02-08 15:42:01 -0700798
Mark Young39389872017-01-19 21:10:49 -0700799On any system, the loader looks in specific areas for information on the
800layers that it can load at a user's request. The process of finding the
801available layers on a system is known as Layer Discovery. During discovery,
802the loader determines what layers are available, the layer name, the layer
803version, and any extensions supported by the layer. This information is
804provided back to an application through `vkEnumerateInstanceLayerProperties`.
805
806The group of layers available to the loader is known as a layer library. This
807section defines an extensible interface to discover what layers are contained in
808the layer library.
809
810This section also specifies the minimal conventions and rules a layer must
811follow, especially with regards to interacting with the loader and other layers.
812
813##### Layer Manifest File Usage
814
Mark Young18bbaab2017-03-20 08:27:14 -0600815On Windows and Linux systems, JSON formatted manifest files are used to store
Mark Young39389872017-01-19 21:10:49 -0700816layer information. In order to find system-installed layers, the Vulkan loader
817will read the JSON files to identify the names and attributes of layers and
818their extensions. The use of manifest files allows the loader to avoid loading
819any shared library files when the application does not query nor request any
820extensions. The format of [Layer Manifest File](#layer-manifest-file-format)
821is detailed below.
822
823The Android loader does not use manifest files. Instead, the loader queries the
824layer properties using special functions known as "introspection" functions.
825The intent of these functions is to determine the same required information
826gathered from reading the manifest files. These introspection functions are
827not used by the desktop loader but should be present in layers to maintain
828consistency. The specific "introspection" functions are called out in
829the [Layer Manifest File Format](#layer-manifest-file-format) table.
830
831
832##### Android Layer Discovery
833
834On Android, the loader looks for layers to enumerate in the
835/data/local/debug/vulkan folder. An application enabled for debug has the
836ability to enumerate and enable any layers in that location.
837
838
839##### Windows Layer Discovery
840
841In order to find system-installed layers, the Vulkan loader will scan the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700842values in the following Windows registry keys:
Jon Ashburnc2972682016-02-08 15:42:01 -0700843
Mark Young39389872017-01-19 21:10:49 -0700844```
845 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\ExplicitLayers
Lenny Komowdfa37352017-03-02 11:29:03 -0700846 HKEY_CURRENT_USER\SOFTWARE\Khronos\Vulkan\ExplicitLayers
Mark Young39389872017-01-19 21:10:49 -0700847 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\ImplicitLayers
Lenny Komowdfa37352017-03-02 11:29:03 -0700848 HKEY_CURRENT_USER\SOFTWARE\Khronos\Vulkan\ImplicitLayers
Mark Young39389872017-01-19 21:10:49 -0700849```
Jon Ashburnc2972682016-02-08 15:42:01 -0700850
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700851For each value in these keys which has DWORD data set to 0, the loader opens
852the JSON manifest file specified by the name of the value. Each name must be a
Mark Young39389872017-01-19 21:10:49 -0700853full pathname to the manifest file. The Vulkan loader will open each info file
854to obtain information about the layer, including the name or pathname of a
855shared library (".dll") file. However, if VK\_LAYER\_PATH is defined, then the
856loader will instead look at the paths defined by that variable instead of using
857the information provided by these registry keys. See
858[Forcing Layer Source Folders](#forcing-layer-source-folders) for more
859information on this.
Jon Ashburnc2972682016-02-08 15:42:01 -0700860
Jon Ashburnc2972682016-02-08 15:42:01 -0700861
Mark Young39389872017-01-19 21:10:49 -0700862##### Linux Layer Discovery
Jon Ashburnc2972682016-02-08 15:42:01 -0700863
Mark Young39389872017-01-19 21:10:49 -0700864On Linux, the Vulkan loader will scan the files in the following Linux
865directories:
Jon Ashburnc2972682016-02-08 15:42:01 -0700866
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600867 /usr/local/etc/vulkan/explicit_layer.d
868 /usr/local/etc/vulkan/implicit_layer.d
869 /usr/local/share/vulkan/explicit_layer.d
870 /usr/local/share/vulkan/implicit_layer.d
871 /etc/vulkan/explicit_layer.d
872 /etc/vulkan/implicit_layer.d
873 /usr/share/vulkan/explicit_layer.d
874 /usr/share/vulkan/implicit_layer.d
875 $HOME/.local/share/vulkan/explicit_layer.d
876 $HOME/.local/share/vulkan/implicit_layer.d
877
Mark Young39389872017-01-19 21:10:49 -0700878Of course, ther are some things you have to know about the above folders:
879 1. The "/usr/local/*" directories can be configured to be other directories at
880build time.
881 2. $HOME is the current home directory of the application's user id; this path
882will be ignored for suid programs.
883 3. The "/usr/local/etc/vulkan/\*\_layer.d" and
884"/usr/local/share/vulkan/\*\_layer.d" directories are for layers that are
885installed from locally-built sources.
886 4. The "/usr/share/vulkan/\*\_layer.d" directories are for layers that are
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600887installed from Linux-distribution-provided packages.
888
Mark Young39389872017-01-19 21:10:49 -0700889As on Windows, if VK\_LAYER\_PATH is defined, then the
890loader will instead look at the paths defined by that variable instead of using
891the information provided by these default paths. However, these
892environment variables are only used for non-suid programs. See
893[Forcing Layer Source Folders](#forcing-layer-source-folders) for more
894information on this.
Jon Ashburnc2972682016-02-08 15:42:01 -0700895
Mark Young39389872017-01-19 21:10:49 -0700896
897#### Layer Version Negotiation
898
899Now that a layer has been discovered, an application can choose to load it (or
900it is loaded by default if it is an Implicit layer). When the loader attempts
901to load the layer, the first thing it does is attempt to negotiate the version
902of the loader to layer interface. In order to negotiate the loader/layer
903interface version, the layer must implement the
904`vkNegotiateLoaderLayerInterfaceVersion` function. The following information is
905provided for this interface in include/vulkan/vk_layer.h:
906
907```cpp
908 typedef enum VkNegotiateLayerStructType {
909 LAYER_NEGOTIATE_INTERFACE_STRUCT = 1,
910 } VkNegotiateLayerStructType;
911
912 typedef struct VkNegotiateLayerInterface {
913 VkNegotiateLayerStructType sType;
914 void *pNext;
915 uint32_t loaderLayerInterfaceVersion;
916 PFN_vkGetInstanceProcAddr pfnGetInstanceProcAddr;
917 PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr;
918 PFN_GetPhysicalDeviceProcAddr pfnGetPhysicalDeviceProcAddr;
919 } VkNegotiateLayerInterface;
920
921 VkResult vkNegotiateLoaderLayerInterfaceVersion(
922 VkNegotiateLayerInterface *pVersionStruct);
Jon Ashburncc300a22016-02-11 14:57:30 -0700923```
Jon Ashburnc2972682016-02-08 15:42:01 -0700924
Mark Young39389872017-01-19 21:10:49 -0700925You'll notice the `VkNegotiateLayerInterface` structure is similar to other
926Vulkan structures. The "sType" field, in this case takes a new enum defined
927just for internal loader/layer interfacing use. The valid values for "sType"
928could grow in the future, but right only havs the one value
929"LAYER_NEGOTIATE_INTERFACE_STRUCT".
Jon Ashburnc2972682016-02-08 15:42:01 -0700930
Mark Young39389872017-01-19 21:10:49 -0700931This function (`vkNegotiateLoaderLayerInterfaceVersion`) should be exported by
932the layer so that using "GetProcAddress" on Windows or "dlsym" on Linux, should
933return a valid function pointer to it. Once the loader has grabbed a valid
934address to the layers function, the loader will create a variable of type
935`VkNegotiateLayerInterface` and initialize it in the following ways:
936 1. Set the structure "sType" to "LAYER_NEGOTIATE_INTERFACE_STRUCT"
937 2. Set pNext to NULL.
938 - This is for future growth
939 3. Set "loaderLayerInterfaceVersion" to the current version the loader desires
940to set the interface to.
941 - The minimum value sent by the loader will be 2 since it is the first
942version supporting this function.
Jon Ashburnc2972682016-02-08 15:42:01 -0700943
Mark Young39389872017-01-19 21:10:49 -0700944The loader will then individually call each layer’s
945`vkNegotiateLoaderLayerInterfaceVersion` function with the filled out
946“VkNegotiateLayerInterface”. The layer will either accept the loader's version
947set in "loaderLayerInterfaceVersion", or modify it to the closest value version
948of the interface that the layer can support. The value should not be higher
949than the version requested by the loader. If the layer can't support at a
950minimum the version requested, then the layer should return an error like
951"VK_ERROR_INITIALIZATION_FAILED". If a layer can support some version, then
952the layer should do the following:
953 1. Adjust the version to the layer's desired version.
954 2. The layer should fill in the function pointer values to its internal
955functions:
956 - "pfnGetInstanceProcAddr" should be set to the layer’s internal
957`GetInstanceProcAddr` function.
958 - "pfnGetDeviceProcAddr" should be set to the layer’s internal
959`GetDeviceProcAddr` function.
960 - "pfnGetPhysicalDeviceProcAddr" should be set to the layer’s internal
961`GetPhysicalDeviceProcAddr` function.
962 - If the layer supports no physical device extensions, it may set the
963value to NULL.
964 - More on this function later
965 3. The layer should return "VK_SUCCESS"
Jon Ashburnc2972682016-02-08 15:42:01 -0700966
Mark Young39389872017-01-19 21:10:49 -0700967This function **SHOULD NOT CALL DOWN** the layer chain to the next layer.
968The loader will work with each layer individually.
Jon Ashburnc2972682016-02-08 15:42:01 -0700969
Mark Young39389872017-01-19 21:10:49 -0700970If the layer supports the new interface and reports version 2 or greater, then
971the loader will use the “fpGetInstanceProcAddr” and “fpGetDeviceProcAddr”
972functions from the “VkNegotiateLayerInterface” structure. Prior to these
973changes, the loader would query each of those functions using "GetProcAddress"
974on Windows or "dlsym" on Linux.
Jon Ashburnc2972682016-02-08 15:42:01 -0700975
Jon Ashburnc2972682016-02-08 15:42:01 -0700976
Mark Young39389872017-01-19 21:10:49 -0700977#### Layer Call Chains and Distributed Dispatch
Jon Ashburnc2972682016-02-08 15:42:01 -0700978
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700979There are two key architectural features that drive the loader to layer library
Mark Young39389872017-01-19 21:10:49 -0700980interface:
981 1. Separate and distinct instance and device call chains
982 2. Distributed dispatch.
Jon Ashburnc2972682016-02-08 15:42:01 -0700983
Mark Young39389872017-01-19 21:10:49 -0700984You can read an overview of dispatch tables and call chains above in the
985[Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) section.
Jon Ashburnc2972682016-02-08 15:42:01 -0700986
Mark Young39389872017-01-19 21:10:49 -0700987What's important to note here is that a layer can intercept Vulkan
988instance functions, device functions or both. For a layer to intercept instance
989functions, it must participate in the instance call chain. For a layer to
990intercept device functions, it must participate in the device call chain.
991
992Remember, a layer does not need to intercept all instance or device functions,
993instead, it can choose to intercept only a subset of those functions.
994
995Normally, when a layer intercepts a given Vulkan function, it will call down the
996instance or device call chain as needed. The loader and all layer libraries that
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700997participate in a call chain cooperate to ensure the correct sequencing of calls
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700998from one entity to the next. This group effort for call chain sequencing is
Mark Young39389872017-01-19 21:10:49 -0700999hereinafter referred to as **distributed dispatch**.
Jon Ashburnc2972682016-02-08 15:42:01 -07001000
Mark Young39389872017-01-19 21:10:49 -07001001In distributed dispatch each layer is responsible for properly calling the next
1002entity in the call chain. This means that a dispatch mechanism is required for
1003all Vulkan functions that a layer intercepts. If a Vulkan function is not
1004intercepted by a layer, or if a layer chooses to terminate the function by not
1005calling down the chain, then no dispatch is needed for that particular function.
Jeff Julianof1619872016-02-17 17:25:42 -05001006
Mark Young39389872017-01-19 21:10:49 -07001007For example, if the enabled layers intercepted only certain instance functions,
1008the call chain would look as follows:
1009![Instance Function Chain](./images/function_instance_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -07001010
Mark Young39389872017-01-19 21:10:49 -07001011Likewise, if the enabled layers intercepted only a few of the device functions,
1012the call chain could look this way:
1013![Device Function Chain](./images/function_device_chain.png)
Jeff Julianof1619872016-02-17 17:25:42 -05001014
Mark Young39389872017-01-19 21:10:49 -07001015The loader is responsible for dispatching all core and instance extension Vulkan
1016functions to the first entity in the call chain.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001017
Chia-I Wucb24fec2016-04-20 06:23:24 +08001018
Mark Young39389872017-01-19 21:10:49 -07001019#### Layer Unknown Physical Device Extensions
1020
1021Originally, if the loader was called with `vkGetInstanceProcAddr`, it would
1022result in the following behavior:
1023 1. The loader would check if core function:
1024 - If it was, it would return the function pointer
1025 2. The loader would check if known extension function:
1026 - If it was, it would return the function pointer
1027 3. If the loader knew nothing about it, it would call down using
1028`GetInstanceProcAddr`
1029 - If it returned non-NULL, treat it as an unknown logical device command.
1030 - This meant setting up a generic trampoline function that takes in a
1031VkDevice as the first parameter and adjusting the dispatch table to call the
1032ICD/Layers function after getting the dispatch table from the VkDevice.
1033 4. If all the above failed, the loader would return NULL to the application.
1034
1035This caused problems when a layer attempted to expose new physical device
1036extensions the loader knew nothing about, but an application did. Because the
1037loader knew nothing about it, the loader would get to step 3 in the above
1038process and would treat the function as an unknown logical device command. The
1039problem is, this would create a generic VkDevice trampoline function which, on
1040the first call, would attempt to dereference the VkPhysicalDevice as a VkDevice.
1041This would lead to a crash or corruption.
1042
1043In order to identify the extension entry-points specific to physical device
1044extensions, the following function can be added to a layer:
1045
1046```cpp
1047PFN_vkVoidFunction vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1048 const char* pName);
1049```
1050
1051This function behaves similar to `vkGetInstanceProcAddr` and
1052`vkGetDeviceProcAddr` except it should only return values for physical device
1053extension entry-points. In this way, it compares "pName" to every physical
1054device function supported in the layer.
1055
1056The following rules apply:
Lenny Komowde3924a2017-05-04 14:50:01 -06001057 * If it is the name of a physical device function supported by the layer, the
Mark Young39389872017-01-19 21:10:49 -07001058pointer to the layer's corresponding function should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06001059 * If it is the name of a valid function which is **not** a physical device
Mark Young39389872017-01-19 21:10:49 -07001060function (i.e. an Instance, Device, or other function implemented by the layer),
1061then the value of NULL should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06001062 * We don’t call down since we know the command is not a physical device
Mark Young39389872017-01-19 21:10:49 -07001063extension).
Lenny Komowde3924a2017-05-04 14:50:01 -06001064 * If the layer has no idea what this function is, it should call down the layer
Mark Young39389872017-01-19 21:10:49 -07001065chain to the next `vk_layerGetPhysicalDeviceProcAddr` call.
Lenny Komowde3924a2017-05-04 14:50:01 -06001066 * This can be retrieved in one of two ways:
1067 * During `vkCreateInstance`, it is passed to a layer in the
Mark Young39389872017-01-19 21:10:49 -07001068chain information passed to a layer in the `VkLayerInstanceCreateInfo`
1069structure.
1070 * Use `get_chain_info()` to get the pointer to the
1071`VkLayerInstanceCreateInfo` structure. Let's call it chain_info.
1072 * The address is then under
1073chain_info->u.pLayerInfo->pfnNextGetPhysicalDeviceProcAddr
1074 * See
1075[Example Code for CreateInstance](#example-code-for-createinstance)
Lenny Komowde3924a2017-05-04 14:50:01 -06001076 * Using the next layer’s `GetInstanceProcAddr` function to query for
Mark Young39389872017-01-19 21:10:49 -07001077`vk_layerGetPhysicalDeviceProcAddr`.
1078
1079This support is optional and should not be considered a requirement. This is
1080only required if a layer intends to support some functionality not directly
Mark Young18bbaab2017-03-20 08:27:14 -06001081supported by loaders released in the public. If a layer does implement this
Mark Young39389872017-01-19 21:10:49 -07001082support, it should return the address of its `vk_layerGetPhysicalDeviceProcAddr`
1083function in the "pfnGetPhysicalDeviceProcAddr" member of the
1084`VkNegotiateLayerInterface` structure during
1085[Layer Version Negotiation](#layer-version-negotiation). Additionally, the
1086layer should also make sure `vkGetInstanceProcAddr` returns a valid function
1087pointer to a query of `vk_layerGetPhysicalDeviceProcAddr`.
1088
1089The new behavior of the loader's `vkGetInstanceProcAddr` with support for the
1090`vk_layerGetPhysicalDeviceProcAddr` function is as follows:
1091 1. Check if core function:
1092 - If it is, return the function pointer
1093 2. Check if known instance or device extension function:
1094 - If it is, return the function pointer
1095 3. Call the layer/ICD `GetPhysicalDeviceProcAddr`
1096 - If it returns non-NULL, return a trampoline to a generic physical device
1097function, and setup a generic terminator which will pass it to the proper ICD.
1098 4. Call down using `GetInstanceProcAddr`
1099 - If it returns non-NULL, treat it as an unknown logical device command.
1100This means setting up a generic trampoline function that takes in a VkDevice as
1101the first parameter and adjusting the dispatch table to call the ICD/Layers
1102function after getting the dispatch table from the VkDevice. Then, return the
1103pointer to corresponding trampoline function.
1104 5. Return NULL
1105
1106You can see now, that, if the command gets promoted to core later, it will no
1107longer be setup using `vk_layerGetPhysicalDeviceProcAddr`. Additionally, if the
1108loader adds direct support for the extension, it will no longer get to step 3,
1109because step 2 will return a valid function pointer. However, the layer should
1110continue to support the command query via `vk_layerGetPhysicalDeviceProcAddr`,
1111until at least a Vulkan version bump, because an older loader may still be
1112attempting to use the commands.
1113
1114
1115#### Layer Intercept Requirements
1116
Lenny Komowde3924a2017-05-04 14:50:01 -06001117 * Layers intercept a Vulkan function by defining a C/C++ function with
Mark Young39389872017-01-19 21:10:49 -07001118signature **identical** to the Vulkan API for that function.
Lenny Komowde3924a2017-05-04 14:50:01 -06001119 * A layer **must intercept at least** `vkGetInstanceProcAddr` and
Mark Young39389872017-01-19 21:10:49 -07001120`vkCreateInstance` to participate in the instance call chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001121 * A layer **may also intercept** `vkGetDeviceProcAddr` and `vkCreateDevice`
Mark Young39389872017-01-19 21:10:49 -07001122to participate in the device call chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001123 * For any Vulkan function a layer intercepts which has a non-void return value,
Mark Young39389872017-01-19 21:10:49 -07001124**an appropriate value must be returned** by the layer intercept function.
Lenny Komowde3924a2017-05-04 14:50:01 -06001125 * Most functions a layer intercepts **should call down the chain** to the
Mark Young39389872017-01-19 21:10:49 -07001126corresponding Vulkan function in the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001127 * The common behavior for a layer is to intercept a call, perform some
Mark Young39389872017-01-19 21:10:49 -07001128behavior, then pass it down to the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001129 * If you don't pass the information down, undefined behavior may occur.
1130 * This is because the function will not be received by layers further down
Mark Young39389872017-01-19 21:10:49 -07001131the chain, or any ICDs.
Lenny Komowde3924a2017-05-04 14:50:01 -06001132 * One function that **must never call down the chain** is:
1133 * `vkNegotiateLoaderLayerInterfaceVersion`
1134 * Three common functions that **may not call down the chain** are:
1135 * `vkGetInstanceProcAddr`
1136 * `vkGetDeviceProcAddr`
1137 * `vk_layerGetPhysicalDeviceProcAddr`
1138 * These functions only call down the chain for Vulkan functions that they
Mark Young39389872017-01-19 21:10:49 -07001139do not intercept.
Lenny Komowde3924a2017-05-04 14:50:01 -06001140 * Layer intercept functions **may insert extra calls** to Vulkan functions in
Mark Young39389872017-01-19 21:10:49 -07001141addition to the intercept.
Lenny Komowde3924a2017-05-04 14:50:01 -06001142 * For example, a layer intercepting `vkQueueSubmit` may want to add a call to
Mark Young39389872017-01-19 21:10:49 -07001143`vkQueueWaitIdle` after calling down the chain for `vkQueueSubmit`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001144 * This would result in two calls down the chain: First a call down the
Mark Young39389872017-01-19 21:10:49 -07001145`vkQueueSubmit` chain, followed by a call down the `vkQueueWaitIdle` chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001146 * Any additional calls inserted by a layer must be on the same chain
1147 * If the function is a device function, only other device functions should
Mark Young39389872017-01-19 21:10:49 -07001148be added.
Lenny Komowde3924a2017-05-04 14:50:01 -06001149 * Likewise, if the function is an instance function, only other instance
Mark Young39389872017-01-19 21:10:49 -07001150functions should be added.
1151
1152
1153#### Distributed Dispatching Requirements
1154
1155- For each entry-point a layer intercepts, it must keep track of the entry
1156point residing in the next entity in the chain it will call down into.
Lenny Komowde3924a2017-05-04 14:50:01 -06001157 * In other words, the layer must have a list of pointers to functions of the
Mark Young39389872017-01-19 21:10:49 -07001158appropriate type to call into the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001159 * This can be implemented in various ways but
Mark Young39389872017-01-19 21:10:49 -07001160for clarity, will be referred to as a dispatch table.
1161- A layer can use the `VkLayerDispatchTable` structure as a device dispatch
1162table (see include/vulkan/vk_layer.h).
1163- A layer can use the `VkLayerInstanceDispatchTable` structure as a instance
1164dispatch table (see include/vulkan/vk_layer.h).
1165- A Layer's `vkGetInstanceProcAddr` function uses the next entity's
1166`vkGetInstanceProcAddr` to call down the chain for unknown (i.e.
1167non-intercepted) functions.
1168- A Layer's `vkGetDeviceProcAddr` function uses the next entity's
1169`vkGetDeviceProcAddr` to call down the chain for unknown (i.e. non-intercepted)
1170functions.
1171- A Layer's `vk_layerGetPhysicalDeviceProcAddr` function uses the next entity's
1172`vk_layerGetPhysicalDeviceProcAddr` to call down the chain for unknown (i.e.
1173non-intercepted) functions.
1174
1175
1176#### Layer Conventions and Rules
Chia-I Wucb24fec2016-04-20 06:23:24 +08001177
1178A layer, when inserted into an otherwise compliant Vulkan implementation, must
Mark Young39389872017-01-19 21:10:49 -07001179still result in a compliant Vulkan implementation. The intention is for layers
1180to have a well-defined baseline behavior. Therefore, it must follow some
1181conventions and rules defined below.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001182
1183A layer is always chained with other layers. It must not make invalid calls
Mark Young39389872017-01-19 21:10:49 -07001184to, or rely on undefined behaviors of, its lower layers. When it changes the
1185behavior of a function, it must make sure its upper layers do not make invalid
Chia-I Wucb24fec2016-04-20 06:23:24 +08001186calls to or rely on undefined behaviors of its lower layers because of the
1187changed behavior. For example, when a layer intercepts an object creation
Mark Young39389872017-01-19 21:10:49 -07001188function to wrap the objects created by its lower layers, it must make sure its
Chia-I Wucb24fec2016-04-20 06:23:24 +08001189lower layers never see the wrapping objects, directly from itself or
1190indirectly from its upper layers.
1191
Chia-I Wub5e850e2016-05-06 08:41:52 +08001192When a layer requires host memory, it may ignore the provided allocators. It
1193should use memory allocators if the layer is intended to run in a production
Mark Young39389872017-01-19 21:10:49 -07001194environment. For example, this usually applies to implicit layers that are
1195always enabled. That will allow applications to include the layer's memory
1196usage.
Chia-I Wub5e850e2016-05-06 08:41:52 +08001197
Mark Young39389872017-01-19 21:10:49 -07001198Additional rules include:
Lenny Komowde3924a2017-05-04 14:50:01 -06001199 - `vkEnumerateInstanceLayerProperties` **must** enumerate and **only**
Mark Young39389872017-01-19 21:10:49 -07001200enumerate the layer itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001201 - `vkEnumerateInstanceExtensionProperties` **must** handle the case where
Mark Young39389872017-01-19 21:10:49 -07001202`pLayerName` is itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001203 - It **must** return `VK_ERROR_LAYER_NOT_PRESENT` otherwise, including when
Mark Young39389872017-01-19 21:10:49 -07001204`pLayerName` is `NULL`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001205 - `vkEnumerateDeviceLayerProperties` **is deprecated and may be omitted**.
1206 - Using this will result in undefined behavior.
1207 - `vkEnumerateDeviceExtensionProperties` **must** handle the case where
Mark Young39389872017-01-19 21:10:49 -07001208`pLayerName` is itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001209 - In other cases, it should normally chain to other layers.
1210 - `vkCreateInstance` **must not** generate an error for unrecognized layer
Mark Young39389872017-01-19 21:10:49 -07001211names and extension names.
Lenny Komowde3924a2017-05-04 14:50:01 -06001212 - It may assume the layer names and extension names have been validated.
1213 - `vkGetInstanceProcAddr` intercepts a Vulkan function by returning a local
Mark Young39389872017-01-19 21:10:49 -07001214entry-point
Lenny Komowde3924a2017-05-04 14:50:01 -06001215 - Otherwise it returns the value obtained by calling down the instance call
Mark Young39389872017-01-19 21:10:49 -07001216chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001217 - `vkGetDeviceProcAddr` intercepts a Vulkan function by returning a local
Mark Young39389872017-01-19 21:10:49 -07001218entry-point
Lenny Komowde3924a2017-05-04 14:50:01 -06001219 - Otherwise it returns the value obtained by calling down the device call
Mark Young39389872017-01-19 21:10:49 -07001220chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001221 - These additional functions must be intercepted if the layer implements
Mark Young39389872017-01-19 21:10:49 -07001222device-level call chaining:
Lenny Komowde3924a2017-05-04 14:50:01 -06001223 - `vkGetDeviceProcAddr`
1224 - `vkCreateDevice`(only required for any device-level chaining)
1225 - **NOTE:** older layer libraries may expect that `vkGetInstanceProcAddr`
Mark Young39389872017-01-19 21:10:49 -07001226ignore `instance` when `pName` is `vkCreateDevice`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001227 - The specification **requires** `NULL` to be returned from
Mark Young39389872017-01-19 21:10:49 -07001228`vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` for disabled functions.
Lenny Komowde3924a2017-05-04 14:50:01 -06001229 - A layer may return `NULL` itself or rely on the following layers to do so.
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001230
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001231
Mark Young39389872017-01-19 21:10:49 -07001232#### Layer Dispatch Initialization
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001233
Mark Young39389872017-01-19 21:10:49 -07001234- A layer initializes its instance dispatch table within its `vkCreateInstance`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001235function.
Mark Young39389872017-01-19 21:10:49 -07001236- A layer initializes its device dispatch table within its `vkCreateDevice`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001237function.
1238- The loader passes a linked list of initialization structures to layers via
Mark Young39389872017-01-19 21:10:49 -07001239the "pNext" field in the VkInstanceCreateInfo and `VkDeviceCreateInfo`
1240structures for `vkCreateInstance` and `VkCreateDevice` respectively.
1241- The head node in this linked list is of type `VkLayerInstanceCreateInfo` for
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001242instance and VkLayerDeviceCreateInfo for device. See file
Mark Young39389872017-01-19 21:10:49 -07001243`include/vulkan/vk_layer.h` for details.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001244- A VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO is used by the loader for the
1245"sType" field in VkLayerInstanceCreateInfo.
1246- A VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO is used by the loader for the
1247"sType" field in VkLayerDeviceCreateInfo.
1248- The "function" field indicates how the union field "u" should be interpreted
Mark Young39389872017-01-19 21:10:49 -07001249within `VkLayer*CreateInfo`. The loader will set the "function" field to
1250VK_LAYER_LINK_INFO. This indicates "u" field should be `VkLayerInstanceLink` or
1251`VkLayerDeviceLink`.
1252- The `VkLayerInstanceLink` and `VkLayerDeviceLink` structures are the list
1253nodes.
1254- The `VkLayerInstanceLink` contains the next entity's `vkGetInstanceProcAddr`
1255used by a layer.
1256- The `VkLayerDeviceLink` contains the next entity's `vkGetInstanceProcAddr` and
1257`vkGetDeviceProcAddr` used by a layer.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001258- Given the above structures set up by the loader, layer must initialize their
1259dispatch table as follows:
Mark Young39389872017-01-19 21:10:49 -07001260 - Find the `VkLayerInstanceCreateInfo`/`VkLayerDeviceCreateInfo` structure in
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001261the VkInstanceCreateInfo/VkDeviceCreateInfo structure.
Jon Ashburncc300a22016-02-11 14:57:30 -07001262 - Get the next entity's vkGet*ProcAddr from the "pLayerInfo" field.
Mark Young39389872017-01-19 21:10:49 -07001263 - For CreateInstance get the next entity's `vkCreateInstance` by calling the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001264"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001265 pfnNextGetInstanceProcAddr(NULL, "vkCreateInstance").
Mark Young39389872017-01-19 21:10:49 -07001266 - For CreateDevice get the next entity's `vkCreateDevice` by calling the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001267"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001268 pfnNextGetInstanceProcAddr(NULL, "vkCreateDevice").
Jon Ashburncc300a22016-02-11 14:57:30 -07001269 - Advanced the linked list to the next node: pLayerInfo = pLayerInfo->pNext.
Mark Young39389872017-01-19 21:10:49 -07001270 - Call down the chain either `vkCreateDevice` or `vkCreateInstance`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001271 - Initialize your layer dispatch table by calling the next entity's
Mark Young39389872017-01-19 21:10:49 -07001272Get*ProcAddr function once for each Vulkan function needed in your dispatch
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001273table
Jon Ashburncc300a22016-02-11 14:57:30 -07001274
Mark Young39389872017-01-19 21:10:49 -07001275#### Example Code for CreateInstance
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001276
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001277```cpp
1278VkResult vkCreateInstance(
1279 const VkInstanceCreateInfo *pCreateInfo,
1280 const VkAllocationCallbacks *pAllocator,
1281 VkInstance *pInstance)
1282{
1283 VkLayerInstanceCreateInfo *chain_info =
1284 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1285
1286 assert(chain_info->u.pLayerInfo);
1287 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1288 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1289 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001290 (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001291 if (fpCreateInstance == NULL) {
1292 return VK_ERROR_INITIALIZATION_FAILED;
1293 }
1294
1295 // Advance the link info for the next element of the chain
1296 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1297
1298 // Continue call down the chain
1299 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
1300 if (result != VK_SUCCESS)
1301 return result;
1302
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001303 // Init layer's dispatch table using GetInstanceProcAddr of
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001304 // next layer in the chain.
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001305 instance_dispatch_table = new VkLayerInstanceDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001306 layer_init_instance_dispatch_table(
1307 *pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
1308
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001309 // Other layer initialization
1310 ...
1311
1312 return VK_SUCCESS;
1313}
1314```
1315
Mark Young39389872017-01-19 21:10:49 -07001316#### Example Code for CreateDevice
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001317
1318```cpp
1319VkResult
1320vkCreateDevice(
1321 VkPhysicalDevice gpu,
1322 const VkDeviceCreateInfo *pCreateInfo,
1323 const VkAllocationCallbacks *pAllocator,
1324 VkDevice *pDevice)
1325{
1326 VkLayerDeviceCreateInfo *chain_info =
1327 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1328
1329 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1330 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1331 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr =
1332 chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1333 PFN_vkCreateDevice fpCreateDevice =
1334 (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
1335 if (fpCreateDevice == NULL) {
1336 return VK_ERROR_INITIALIZATION_FAILED;
1337 }
1338
1339 // Advance the link info for the next element on the chain
1340 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1341
1342 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
1343 if (result != VK_SUCCESS) {
1344 return result;
1345 }
1346
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001347 // initialize layer's dispatch table
1348 device_dispatch_table = new VkLayerDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001349 layer_init_device_dispatch_table(
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001350 *pDevice, device_dispatch_table, fpGetDeviceProcAddr);
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001351
1352 // Other layer initialization
1353 ...
1354
1355 return VK_SUCCESS;
1356}
1357```
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001358
Mark Young39389872017-01-19 21:10:49 -07001359
Mark Youngc82a0622017-05-05 11:17:17 -06001360#### Meta-layers
1361
1362Meta-layers are a special kind of layer which is only available through the
1363desktop loader. While normal layers are associated with one particular library,
1364a meta-layer is actually a collection layer which contains an ordered list of
1365other layers (called component layers).
1366
1367The most common example of a meta-layer is the
1368`VK_LAYER_LUNARG_standard_validation` layer which groups all the most common
1369individual validation layers into a single layer for ease-of-use.
1370
1371The benefits of a meta-layer are:
1372 1. You can activate more than one layer using a single layer name by simply
1373grouping multiple layers in a meta-layer.
1374 2. You can define the order the loader will activate individual layers within
1375the meta-layer.
1376 3. You can easily share your special layer configuration with others.
1377 4. The loader will automatically collate all instance and device extensions in
1378a meta-layer's component layers, and report them as the meta-layer's properties
1379to the application when queried.
1380
1381Restrictions to defining and using a meta-layer are:
1382 1. A Meta-layer Manifest file **must** be a properly formated that contains one
1383or more component layers.
1384 3. All component layers **must be** present on a system for the meta-layer to
1385be used.
1386 4. All component layers **must be** at the same Vulkan API major and minor
1387version for the meta-layer to be used.
1388
1389The ordering of a meta-layer's component layers in the instance or device
1390call-chain is simple:
1391 * The first layer listed will be the layer closest to the application.
1392 * The last layer listed will be the layer closest to the drivers.
1393
1394Inside the meta-layer Manifest file, each component layer is listed by its
1395layer name. This is the "name" tag's value associated with each component layer's
1396Manifest file under the "layer" or "layers" tag. This is also the name that
1397would normally be used when activating a layer during `vkCreateInstance`.
1398
1399Any duplicate layer names in either the component layer list, or globally among
1400all enabled layers, will simply be ignored. Only the first instance of any
1401layer name will be used.
1402
1403For example, if you have a layer enabled using the environment variable
1404`VK_INSTANCE_LAYERS` and have that same layer listed in a meta-layer, then the
1405environment variable enabled layer will be used and the component layer will
1406be dropped. Likewise, if a person were to enable a meta-layer and then
1407separately enable one of the component layers afterwards, the second
1408instantiation of the layer name would be ignored.
1409
1410The
1411Manifest file formatting necessary to define a meta-layer can be found in the
1412[Layer Manifest File Format](#layer-manifest-file-format) section.
1413
Jon Ashburncc300a22016-02-11 14:57:30 -07001414#### Special Considerations
Mark Young39389872017-01-19 21:10:49 -07001415
1416
1417##### Associating Private Data with Vulkan Objects Within a Layer
1418
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001419A layer may want to associate it's own private data with one or more Vulkan
Mark Youngf7914cf2016-08-31 11:53:26 -06001420objects. Two common methods to do this are hash maps and object wrapping.
1421
Mark Youngf7914cf2016-08-31 11:53:26 -06001422
Mark Young39389872017-01-19 21:10:49 -07001423###### Wrapping
Ian Elliott0b082e42016-08-31 14:08:44 -06001424
Mark Young39389872017-01-19 21:10:49 -07001425The loader supports layers wrapping any Vulkan object, including dispatchable
1426objects. For functions that return object handles, each layer does not touch
1427the value passed down the call chain. This is because lower items may need to
1428use the original value. However, when the value is returned from a
1429lower-level layer (possibly the ICD), the layer saves the handle and returns
1430its own handle to the layer above it (possibly the application). When a layer
1431receives a Vulkan function using something that it previously returned a handle
1432for, the layer is required to unwrap the handle and pass along the saved handle
1433to the layer below it. This means that the layer **must intercept every Vulkan
1434function which uses the object in question**, and wrap or unwrap the object, as
1435appropriate. This includes adding support for all extensions with functions
Ian Elliott0b082e42016-08-31 14:08:44 -06001436using any object the layer wraps.
Mark Youngf7914cf2016-08-31 11:53:26 -06001437
1438Layers above the object wrapping layer will see the wrapped object. Layers
1439which wrap dispatchable objects must ensure that the first field in the wrapping
Mark Young39389872017-01-19 21:10:49 -07001440structure is a pointer to a dispatch table as defined in `vk_layer.h`.
1441Specifically, an instance wrapped dispatchable object could be as follows:
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001442```
1443struct my_wrapped_instance_obj_ {
1444 VkLayerInstanceDispatchTable *disp;
1445 // whatever data layer wants to add to this object
1446};
1447```
1448A device wrapped dispatchable object could be as follows:
1449```
1450struct my_wrapped_instance_obj_ {
1451 VkLayerDispatchTable *disp;
1452 // whatever data layer wants to add to this object
1453};
1454```
Jeff Julianof1619872016-02-17 17:25:42 -05001455
Ian Elliott0b082e42016-08-31 14:08:44 -06001456Layers that wrap dispatchable objects must follow the guidelines for creating
1457new dispatchable objects (below).
1458
Mark Young39389872017-01-19 21:10:49 -07001459<u>Cautions About Wrapping</u>
Ian Elliott0b082e42016-08-31 14:08:44 -06001460
1461Layers are generally discouraged from wrapping objects, because of the
1462potential for incompatibilities with new extensions. For example, let's say
Mark Young39389872017-01-19 21:10:49 -07001463that a layer wraps `VkImage` objects, and properly wraps and unwraps `VkImage`
1464object handles for all core functions. If a new extension is created which has
1465functions that take `VkImage` objects as parameters, and if the layer does not
1466support those new functions, an application that uses both the layer and the new
1467extension will have undefined behavior when those new functions are called (e.g.
Mark Young18bbaab2017-03-20 08:27:14 -06001468the application may crash). This is because the lower-level layers and ICD
Ian Elliott0b082e42016-08-31 14:08:44 -06001469won't receive the handle that they generated. Instead, they will receive a
1470handle that is only known by the layer that is wrapping the object.
1471
1472Because of the potential for incompatibilities with unsupported extensions,
1473layers that wrap objects must check which extensions are being used by the
1474application, and take appropriate action if the layer is used with unsupported
1475extensions (e.g. disable layer functionality, stop wrapping objects, issue a
1476message to the user).
1477
1478The reason that the validation layers wrap objects, is to track the proper use
1479and destruction of each object. They issue a validation error if used with
1480unsupported extensions, alerting the user to the potential for undefined
1481behavior.
1482
Mark Young39389872017-01-19 21:10:49 -07001483
1484###### Hash Maps
1485
Jeff Julianof1619872016-02-17 17:25:42 -05001486Alternatively, a layer may want to use a hash map to associate data with a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001487given object. The key to the map could be the object. Alternatively, for
1488dispatchable objects at a given level (eg device or instance) the layer may
Mark Young39389872017-01-19 21:10:49 -07001489want data associated with the `VkDevice` or `VkInstance` objects. Since
1490there are multiple dispatchable objects for a given `VkInstance` or `VkDevice`,
1491the `VkDevice` or `VkInstance` object is not a great map key. Instead the layer
1492should use the dispatch table pointer within the `VkDevice` or `VkInstance`
1493since that will be unique for a given `VkInstance` or `VkDevice`.
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001494
Mark Young39389872017-01-19 21:10:49 -07001495
1496##### Creating New Dispatchable Objects
1497
1498Layers which create dispatchable objects must take special care. Remember that
1499loader *trampoline* code normally fills in the dispatch table pointer in the
1500newly created object. Thus, the layer must fill in the dispatch table pointer if
1501the loader *trampoline* will not do so. Common cases where a layer (or ICD) may
1502create a dispatchable object without loader *trampoline* code is as follows:
1503- layers that wrap dispatchable objects
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001504- layers which add extensions that create dispatchable objects
Mark Young18bbaab2017-03-20 08:27:14 -06001505- layers which insert extra Vulkan functions in the stream of functions they
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001506intercept from the application
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001507- ICDs which add extensions that create dispatchable objects
1508
Mark Young39389872017-01-19 21:10:49 -07001509The desktop loader provides a callback that can be used for initializing
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001510a dispatchable object. The callback is passed as an extension structure via the
Mark Young39389872017-01-19 21:10:49 -07001511pNext field in the create info structure when creating an instance
1512(`VkInstanceCreateInfo`) or device (`VkDeviceCreateInfo`). The callback
1513prototype is defined as follows for instance and device callbacks respectively
1514(see `vk_layer.h`):
1515
1516```cpp
1517VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceLoaderData(VkInstance instance,
1518 void *object);
1519VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceLoaderData(VkDevice device,
1520 void *object);
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001521```
Mark Young39389872017-01-19 21:10:49 -07001522
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001523To obtain these callbacks the layer must search through the list of structures
Mark Young39389872017-01-19 21:10:49 -07001524pointed to by the "pNext" field in the `VkInstanceCreateInfo` and
1525`VkDeviceCreateInfo` parameters to find any callback structures inserted by the
1526loader. The salient details are as follows:
1527- For `VkInstanceCreateInfo` the callback structure pointed to by "pNext" is
1528`VkLayerInstanceCreateInfo` as defined in `include/vulkan/vk_layer.h`.
1529- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO within
1530`VkInstanceCreateInfo` parameter indicates a loader structure.
1531- Within `VkLayerInstanceCreateInfo`, the "function" field indicates how the
1532union field "u" should be interpreted.
1533- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will
1534contain the callback in "pfnSetInstanceLoaderData".
1535- For `VkDeviceCreateInfo` the callback structure pointed to by "pNext" is
1536`VkLayerDeviceCreateInfo` as defined in `include/vulkan/vk_layer.h`.
1537- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO within
1538`VkDeviceCreateInfo` parameter indicates a loader structure.
1539- Within `VkLayerDeviceCreateInfo`, the "function" field indicates how the union
1540field "u" should be interpreted.
1541- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will
1542contain the callback in "pfnSetDeviceLoaderData".
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001543
Mark Young39389872017-01-19 21:10:49 -07001544Alternatively, if an older loader is being used that doesn't provide these
1545callbacks, the layer may manually initialize the newly created dispatchable
1546object. To fill in the dispatch table pointer in newly created dispatchable
1547object, the layer should copy the dispatch pointer, which is always the first
1548entry in the structure, from an existing parent object of the same level
1549(instance versus device).
Jon Ashburnc2972682016-02-08 15:42:01 -07001550
Mark Young39389872017-01-19 21:10:49 -07001551For example, if there is a newly created `VkCommandBuffer` object, then the
1552dispatch pointer from the `VkDevice` object, which is the parent of the
1553`VkCommandBuffer` object, should be copied into the newly created object.
1554
1555
1556#### Layer Manifest File Format
1557
1558On Windows and Linux (desktop), the loader uses manifest files to discover
1559layer libraries and layers. The desktop loader doesn't directly query the
1560layer library except during chaining. This is to reduce the likelihood of
1561loading a malicious layer into memory. Instead, details are read from the
1562Manifest file, which are then provided for applications to determine what
1563layers should actually be loaded.
1564
1565The following section discusses the details of the Layer Manifest JSON file
1566format. The JSON file itself does not have any requirements for naming. The
1567only requirement is that the extension suffix of the file ends with ".json".
1568
1569Here is an example layer JSON Manifest file with a single layer:
1570
1571```
1572{
1573 "file_format_version" : "1.0.0",
1574 "layer": {
1575 "name": "VK_LAYER_LUNARG_overlay",
1576 "type": "INSTANCE",
1577 "library_path": "vkOverlayLayer.dll"
1578 "api_version" : "1.0.5",
1579 "implementation_version" : "2",
1580 "description" : "LunarG HUD layer",
1581 "functions": {
1582 "vkNegotiateLoaderLayerInterfaceVersion":
1583 "OverlayLayer_NegotiateLoaderLayerInterfaceVersion"
1584 },
1585 "instance_extensions": [
1586 {
1587 "name": "VK_EXT_debug_report",
1588 "spec_version": "1"
1589 },
1590 {
1591 "name": "VK_VENDOR_ext_x",
1592 "spec_version": "3"
1593 }
1594 ],
1595 "device_extensions": [
1596 {
1597 "name": "VK_EXT_debug_marker",
1598 "spec_version": "1",
1599 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1600 }
1601 ],
1602 "enable_environment": {
1603 "ENABLE_LAYER_OVERLAY_1": "1"
1604 }
1605 "disable_environment": {
1606 "DISABLE_LAYER_OVERLAY_1": ""
1607 }
1608 }
1609}
1610```
1611
1612Here's a snippet with the changes required to support multiple layers per
1613manifest file:
1614```
1615{
1616 "file_format_version" : "1.0.1",
1617 "layers": [
1618 {
1619 "name": "VK_LAYER_layer_name1",
1620 "type": "INSTANCE",
1621 ...
1622 },
1623 {
1624 "name": "VK_LAYER_layer_name2",
1625 "type": "INSTANCE",
1626 ...
1627 }
1628 ]
1629}
1630```
1631
Mark Youngc82a0622017-05-05 11:17:17 -06001632Here's an example of a meta-layer manifest file:
1633```
1634{
1635 "file_format_version" : "1.1.1",
1636 "layer": {
1637 "name": "VK_LAYER_LUNARG_standard_validation",
1638 "type": "GLOBAL",
1639 "api_version" : "1.0.40",
1640 "implementation_version" : "1",
1641 "description" : "LunarG Standard Validation Meta-layer",
1642 "component_layers": [
1643 "VK_LAYER_GOOGLE_threading",
1644 "VK_LAYER_LUNARG_parameter_validation",
1645 "VK_LAYER_LUNARG_object_tracker",
1646 "VK_LAYER_LUNARG_core_validation",
1647 "VK_LAYER_LUNARG_swapchain",
1648 "VK_LAYER_GOOGLE_unique_objects"
1649 ]
1650 }
1651}
1652```
Mark Young39389872017-01-19 21:10:49 -07001653| JSON Node | Description and Notes | Introspection Query |
1654|:----------------:|--------------------|:----------------:
1655| "file\_format\_version" | Manifest format major.minor.patch version number. | N/A |
1656| | Supported versions are: 1.0.0, 1.0.1, and 1.1.0. | |
1657| "layer" | The identifier used to group a single layer's information together. | vkEnumerateInstanceLayerProperties |
1658| "layers" | The identifier used to group multiple layers' information together. This requires a minimum Manifest file format version of 1.0.1.| vkEnumerateInstanceLayerProperties |
1659| "name" | The string used to uniquely identify this layer to applications. | vkEnumerateInstanceLayerProperties |
1660| "type" | This field indicates the type of layer. The values can be: GLOBAL, or INSTANCE | vkEnumerate*LayerProperties |
1661| | **NOTES:** Prior to deprecation, the "type" node was used to indicate which layer chain(s) to activate the layer upon: instance, device, or both. Distinct instance and device layers are deprecated; there are now just layers. Allowable values for type (both before and after deprecation) are "INSTANCE", "GLOBAL" and, "DEVICE." "DEVICE" layers are skipped over by the loader as if they were not found. | |
Mark Youngc82a0622017-05-05 11:17:17 -06001662| "library\_path" | The "library\_path" specifies either a filename, a relative pathname, or a full pathname to a layer shared library file. If "library\_path" specifies a relative pathname, it is relative to the path of the JSON manifest file (e.g. for cases when an application provides a layer that is in the same folder hierarchy as the rest of the application files). If "library\_path" specifies a filename, the library must live in the system's shared object search path. There are no rules about the name of the layer shared library files other than it should end with the appropriate suffix (".DLL" on Windows, and ".so" on Linux). **This field must not be present if "component_layers" is defined** | N/A |
Mark Young39389872017-01-19 21:10:49 -07001663| "api\_version" | The major.minor.patch version number of the Vulkan API that the shared library file for the library was built against. For example: 1.0.33. | vkEnumerateInstanceLayerProperties |
1664| "implementation_version" | The version of the layer implemented. If the layer itself has any major changes, this number should change so the loader and/or application can identify it properly. | vkEnumerateInstanceLayerProperties |
1665| "description" | A high-level description of the layer and it's intended use. | vkEnumerateInstanceLayerProperties |
1666| "functions" | **OPTIONAL:** This section can be used to identify a different function name for the loader to use in place of standard layer interface functions. The "functions" node is required if the layer is using an alternative name for `vkNegotiateLoaderLayerInterfaceVersion`. | vkGet*ProcAddr |
1667| "instance\_extensions" | **OPTIONAL:** Contains the list of instance extension names supported by this layer. One "instance\_extensions" node with an array of one or more elements is required if any instance extensions are supported by a layer, otherwise the node is optional. Each element of the array must have the nodes "name" and "spec_version" which correspond to `VkExtensionProperties` "extensionName" and "specVersion" respectively. | vkEnumerateInstanceExtensionProperties |
1668| "device\_extensions" | **OPTIONAL:** Contains the list of device extension names supported by this layer. One "device_\extensions" node with an array of one or more elements is required if any device extensions are supported by a layer, otherwise the node is optional. Each element of the array must have the nodes "name" and "spec_version" which correspond to `VkExtensionProperties` "extensionName" and "specVersion" respectively. Additionally, each element of the array of device extensions must have the node "entrypoints" if the device extension adds Vulkan API functions, otherwise this node is not required. The "entrypoint" node is an array of the names of all entrypoints added by the supported extension. | vkEnumerateDeviceExtensionProperties |
1669| "enable\_environment" | **Implicit Layers Only** - **OPTIONAL:** Indicates an environment variable used to enable the Implicit Layer (w/ value of 1). This environment variable (which should vary with each "version" of the layer) must be set to the given value or else the implicit layer is not loaded. This is for application environments (e.g. Steam) which want to enable a layer(s) only for applications that they launch, and allows for applications run outside of an application environment to not get that implicit layer(s).| N/A |
1670| "disable\_environment" | **Implicit Layers Only** - **REQUIRED:**Indicates an environment variable used to disable the Implicit Layer (w/ value of 1). In rare cases of an application not working with an implicit layer, the application can set this environment variable (before calling Vulkan functions) in order to "blacklist" the layer. This environment variable (which should vary with each "version" of the layer) must be set (not particularly to any value). If both the "enable_environment" and "disable_environment" variables are set, the implicit layer is disabled. | N/A |
Mark Youngc82a0622017-05-05 11:17:17 -06001671| "component_layers" | **Meta-layers Only** - Indicates the component layer names that are part of a meta-layer. The names listed must be the "name" identified in each of the component layer's Mainfest file "name" tag (this is the same as the name of the layer that is passed to the `vkCreateInstance` command). All component layers must be present on the system and found by the loader in order for this meta-layer to be available and activated. **This field must not be present if "library\_path" is defined** | N/A |
Mark Young39389872017-01-19 21:10:49 -07001672
1673##### Layer Manifest File Version History
1674
1675The current highest supported Layer Manifest file format supported is 1.1.0.
1676Information about each version is detailed in the following sub-sections:
1677
1678###### Layer Manifest File Version 1.1.0
1679
1680Layer Manifest File Version 1.1.0 is tied to changes exposed by the Loader/Layer
1681interface version 2.
1682 1. Renaming "vkGetInstanceProcAddr" in the "functions" section is
1683 deprecated since the loader no longer needs to query the layer about
1684 "vkGetInstanceProcAddr" directly. It is now returned during the layer
1685 negotiation, so this field will be ignored.
1686 2. Renaming "vkGetDeviceProcAddr" in the "functions" section is
1687 deprecated since the loader no longer needs to query the layer about
1688 "vkGetDeviceProcAddr" directly. It too is now returned during the layer
1689 negotiation, so this field will be ignored.
1690 3. Renaming the "vkNegotiateLoaderLayerInterfaceVersion" function is
1691 being added to the "functions" section, since this is now the only
1692 function the loader needs to query using OS-specific calls.
1693 - NOTE: This is an optional field and, as the two previous fields, only
1694needed if the layer requires changing the name of the function for some reason.
1695
1696You do not need to update your layer manifest file if you don't change the
1697names of any of the listed functions.
1698
1699###### Layer Manifest File Version 1.0.1
1700
1701The ability to define multiple layers using the "layers" array was added. This
1702JSON array field can be used when defining a single layer or multiple layers.
1703The "layer" field is still present and valid for a single layer definition.
1704
1705###### Layer Manifest File Version 1.0.0
1706
1707The initial version of the layer manifest file specified the basic format and
1708fields of a layer JSON file. The fields of the 1.0.0 file format include:
1709 * "file\_format\_version"
1710 * "layer"
1711 * "name"
1712 * "type"
1713 * "library\_path"
1714 * "api\_version"
1715 * "implementation\_version"
1716 * "description"
1717 * "functions"
1718 * "instance\_extensions"
1719 * "device\_extensions"
1720 * "enable\_environment"
1721 * "disable\_environment"
1722
1723It was also during this time that the value of "DEVICE" was deprecated from
1724the "type" field.
1725
1726
1727#### Layer Library Versions
1728
1729The current Layer Library interface is at version 2. The following sections
1730detail the differences between the various versions.
1731
1732##### Layer Library API Version 2
1733
1734Introduced the concept of
1735[loader and layer interface](#layer-version-negotiation) using the new
1736`vkNegotiateLoaderLayerInterfaceVersion` function. Additionally, it introduced
1737the concept of
1738[Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-
1739extensions)
1740and the associated `vk_layerGetPhysicalDeviceProcAddr` function. Finally, it
1741changed the manifest file defition to 1.1.0.
1742
1743##### Layer Library API Version 1
1744
1745A layer library supporting interface version 1 had the following behavior:
1746 1. `GetInstanceProcAddr` and `GetDeviceProcAddr` were directly exported
1747 2. The layer manifest file was able to override the names of the
1748`GetInstanceProcAddr` and `GetDeviceProcAddr`functions.
1749
1750##### Layer Library API Version 0
1751
1752A layer library supporting interface version 0 must define and export these
1753introspection functions, unrelated to any Vulkan function despite the names,
1754signatures, and other similarities:
1755
Lenny Komowde3924a2017-05-04 14:50:01 -06001756- `vkEnumerateInstanceLayerProperties` enumerates all layers in a layer
Mark Young39389872017-01-19 21:10:49 -07001757library.
1758 - This function never fails.
1759 - When a layer library contains only one layer, this function may be an alias
1760 to the layer's `vkEnumerateInstanceLayerProperties`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001761- `vkEnumerateInstanceExtensionProperties` enumerates instance extensions of
Mark Young39389872017-01-19 21:10:49 -07001762 layers in a layer library.
1763 - "pLayerName" is always a valid layer name.
1764 - This function never fails.
1765 - When a layer library contains only one layer, this function may be an alias
1766 to the layer's `vkEnumerateInstanceExtensionProperties`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001767- `vkEnumerateDeviceLayerProperties` enumerates a subset (can be full,
Mark Young39389872017-01-19 21:10:49 -07001768 proper, or empty subset) of layers in a layer library.
1769 - "physicalDevice" is always `VK_NULL_HANDLE`.
1770 - This function never fails.
1771 - If a layer is not enumerated by this function, it will not participate in
1772 device function interception.
Lenny Komowde3924a2017-05-04 14:50:01 -06001773- `vkEnumerateDeviceExtensionProperties` enumerates device extensions of
Mark Young39389872017-01-19 21:10:49 -07001774 layers in a layer library.
1775 - "physicalDevice" is always `VK_NULL_HANDLE`.
1776 - "pLayerName" is always a valid layer name.
1777 - This function never fails.
1778
1779It must also define and export these functions once for each layer in the
1780library:
1781
Lenny Komowde3924a2017-05-04 14:50:01 -06001782- `<layerName>GetInstanceProcAddr(instance, pName)` behaves identically to a
Mark Young39389872017-01-19 21:10:49 -07001783layer's vkGetInstanceProcAddr except it is exported.
1784
1785 When a layer library contains only one layer, this function may
1786 alternatively be named `vkGetInstanceProcAddr`.
1787
Lenny Komowde3924a2017-05-04 14:50:01 -06001788- `<layerName>GetDeviceProcAddr` behaves identically to a layer's
Mark Young39389872017-01-19 21:10:49 -07001789vkGetDeviceProcAddr except it is exported.
1790
1791 When a layer library contains only one layer, this function may
1792 alternatively be named `vkGetDeviceProcAddr`.
1793
1794All layers contained within a library must support `vk_layer.h`. They do not
1795need to implement functions that they do not intercept. They are recommended
1796not to export any functions.
1797
1798
1799<br/>
1800<br/>
1801
1802## Vulkan Installable Client Driver Interface With the Loader
1803
1804This section discusses the various requirements for the loader and a Vulkan
1805ICD to properly hand-shake.
1806
Lenny Komowde3924a2017-05-04 14:50:01 -06001807 * [ICD Discovery](#icd-discovery)
Mark Young540d71c2017-05-18 14:52:14 -06001808 * [Overriding the Default ICD Usage](#overriding-the-default-icd-usage)
Lenny Komowde3924a2017-05-04 14:50:01 -06001809 * [ICD Manifest File Usage](#icd-manifest-file-usage)
1810 * [ICD Discovery on Windows](#icd-discovery-on-windows)
1811 * [ICD Discovery on Linux](#icd-discovery-on-linux)
1812 * [Using Pre-Production ICDs on Windows and Linux](#using-pre-production-icds-on-windows-and-linux)
1813 * [ICD Discovery on Android](#icd-discovery-on-android)
1814 * [ICD Manifest File Format](#icd-manifest-file-format)
1815 * [ICD Manifest File Versions](#icd-manifest-file-versions)
1816 * [ICD Manifest File Version 1.0.0](#icd-manifest-file-version-1.0.0)
1817 * [ICD Vulkan Entry-Point Discovery](#icd-vulkan-entry-point-discovery)
1818 * [ICD Unknown Physical Device Extensions](#icd-unknown-physical-device-extensions)
1819 * [ICD Dispatchable Object Creation](#icd-dispatchable-object-creation)
1820 * [Handling KHR Surface Objects in WSI Extensions](#handling-khr-surface-objects-in-wsi-extensions)
1821 * [Loader and ICD Interface Negotiation](#loader-and-icd-interface-negotiation)
1822 * [Windows and Linux ICD Negotiation](#windows-and-linux-icd-negotiation)
1823 * [Version Negotiation Between Loader and ICDs](#version-negotiation-between-loader-and-icds)
1824 * [Interfacing With Legacy ICDs or Loader](#interfacing-with-legacy-icds-or-loader)
1825 * [Loader Version 4 Interface Requirements](#loader-version-4-interface-requirements)
1826 * [Loader Version 3 Interface Requirements](#loader-version-3-interface-requirements)
1827 * [Loader Version 2 Interface Requirements](#loader-version-2-interface-requirements)
1828 * [Loader Versions 0 and 1 Interface Requirements](#loader-versions-0-and-1-interface-requirements)
1829 * [Android ICD Negotiation](#android-icd-negotiation)
Mark Young39389872017-01-19 21:10:49 -07001830
1831
1832### ICD Discovery
1833
1834Vulkan allows multiple drivers each with one or more devices (represented by a
1835Vulkan `VkPhysicalDevice` object) to be used collectively. The loader is
1836responsible for discovering available Vulkan ICDs on the system. Given a list
1837of available ICDs, the loader can enumerate all the physical devices available
1838for an application and return this information to the application. The process
1839in which the loader discovers the available Installable Client Drivers (ICDs)
1840on a system is platform dependent. Windows, Linux and Android ICD discovery
1841details are listed below.
1842
Mark Young540d71c2017-05-18 14:52:14 -06001843#### Overriding the Default ICD Usage
1844
1845There may be times that a developer wishes to force the loader to use a specific ICD.
1846This could be for many reasons including : using a beta driver, or forcing the loader
1847to skip a problematic ICD. In order to support this, the loader can be forced to
1848look at specific ICDs with the `VK_ICD_FILENAMES` environment variable. In order
1849to use the setting, simply set it to a properly delimited list of ICD Manifest
1850files that you wish to use. In this case, please provide the global path to these
1851files to reduce issues.
1852
1853For example:
1854
1855##### On Windows
1856
1857```
1858set VK_ICD_FILENAMES=/windows/system32/nv-vk64.json
1859```
1860
1861This is an example which is using the `VK_ICD_FILENAMES` override on Windows to point
1862to the Nvidia Vulkan driver's ICD Manifest file.
1863
1864##### On Linux
1865
1866```
1867export VK_ICD_FILENAMES=/home/user/dev/mesa/share/vulkan/icd.d/intel_icd.x86_64.json
1868```
1869
1870This is an example which is using the `VK_ICD_FILENAMES` override on Linux to point
1871to the Intel Mesa driver's ICD Manifest file.
1872
1873
Mark Young39389872017-01-19 21:10:49 -07001874#### ICD Manifest File Usage
1875
Mark Young18bbaab2017-03-20 08:27:14 -06001876As with layers, on Windows and Linux systems, JSON formatted manifest files are
Mark Young39389872017-01-19 21:10:49 -07001877used to store ICD information. In order to find system-installed drivers, the
1878Vulkan loader will read the JSON files to identify the names and attributes of
1879each driver. One thing you will notice is that ICD Manifest files are much
1880simpler than the corresponding layer Manifest files.
1881
1882See the [Current ICD Manifest File Format](#icd-manifest-file-format) section
1883for more details.
1884
1885
1886#### ICD Discovery on Windows
1887
1888In order to find installed ICDs, the Vulkan loader will scan the
1889values in the following Windows registry key:
1890
1891```
1892 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers
1893```
1894
1895For 32-bit applications on 64-bit Windows, the loader scan's the 32-bit
1896registry location:
1897
1898```
1899 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Khronos\Vulkan\Drivers
1900```
1901
1902The loader will look at the appropriate registry location and check each value
1903listed. If the key is of type DWORD, and it has a value of 0, the loader will
1904open the JSON manifest file specified by the name. Each name must be a full
1905pathname to a text manifest file. The Vulkan loader will attempt to open each
1906manifest file to obtain the information about an ICD's shared library (".dll")
1907file.
1908
1909For example, let us assume the registry contains the following data:
1910
1911```
1912[HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers\]
1913
1914"C:\vendor a\vk_vendora.json"=dword:00000000
1915"C:\windows\system32\vendorb_vk.json"=dword:00000001
1916"C:\windows\system32\vendorc_icd.json"=dword:00000000
1917```
1918
1919In this case, the loader will step through each entry, and check the value. If
1920the value is 0, then the loader will attempt to load the file. In this case,
Mark Young18bbaab2017-03-20 08:27:14 -06001921the loader will open the first and last listings, but not the middle. This
Mark Young39389872017-01-19 21:10:49 -07001922is because the value of 1 for vendorb_vk.json disables the driver.
1923
1924The Vulkan loader will open each enabled manifest file found to obtain the name
1925or pathname of an ICD shared library (".DLL") file.
1926
1927See the [ICD Manifest File Format](#icd-manifest-file-format) section for more
1928details.
1929
1930
1931#### ICD Discovery on Linux
1932
1933In order to find installed ICDs, the Vulkan loader will scan the files
1934in the following Linux directories:
1935
1936```
1937 /usr/local/etc/vulkan/icd.d
1938 /usr/local/share/vulkan/icd.d
1939 /etc/vulkan/icd.d
1940 /usr/share/vulkan/icd.d
1941 $HOME/.local/share/vulkan/icd.d
1942```
1943
1944The "/usr/local/*" directories can be configured to be other directories at
1945build time.
1946
1947The typical usage of the directories is indicated in the table below.
1948
1949| Location | Details |
1950|-------------------|------------------------|
1951| $HOME/.local/share/vulkan/icd.d | $HOME is the current home directory of the application's user id; this path will be ignored for suid programs |
1952| "/usr/local/etc/vulkan/icd.d" | Directory for locally built ICDs |
1953| "/usr/local/share/vulkan/icd.d" | Directory for locally built ICDs |
1954| "/etc/vulkan/icd.d" | Location of ICDs installed from non-Linux-distribution-provided packages |
1955| "/usr/share/vulkan/icd.d" | Location of ICDs installed from Linux-distribution-provided packages |
1956
1957The Vulkan loader will open each manifest file found to obtain the name or
1958pathname of an ICD shared library (".so") file.
1959
1960See the [ICD Manifest File Format](#icd-manifest-file-format) section for more
1961details.
1962
Mark Young540d71c2017-05-18 14:52:14 -06001963##### Additional Settings For ICD Debugging
1964
1965If you are seeing issues which may be related to the ICD. A possible option to debug is to enable the
1966`LD_BIND_NOW` environment variable. This forces every dynamic library's symbols to be fully resolved on load. If
1967there is a problem with an ICD missing symbols on your system, this will expose it and cause the Vulkan loader
1968to fail on loading the ICD. It is recommended that you enable `LD_BIND_NOW` along with `VK_LOADER_DEBUG=warn`
1969to expose any issues.
1970
Mark Young39389872017-01-19 21:10:49 -07001971#### Using Pre-Production ICDs on Windows and Linux
1972
1973Independent Hardware Vendor (IHV) pre-production ICDs. In some cases, a
1974pre-production ICD may be in an installable package. In other cases, a
1975pre-production ICD may simply be a shared library in the developer's build tree.
1976In this latter case, we want to allow developers to point to such an ICD without
1977modifying the system-installed ICD(s) on their system.
1978
1979This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
1980which will override the mechanism used for finding system-installed ICDs. In
1981other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used.
1982
1983The "VK\_ICD\_FILENAMES" environment variable is a list of ICD
1984manifest files, containing the full path to the ICD JSON Manifest file. This
1985list is colon-separated on Linux, and semi-colon separated on Windows.
1986
1987Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
1988file for a developer-built ICD. A separator (colon or semi-colon) is only used
1989if more than one ICD is listed.
1990
1991**NOTE:** On Linux, this environment variable will be ignored for suid programs.
1992
1993
1994#### ICD Discovery on Android
1995
1996The Android loader lives in the system library folder. The location cannot be
1997changed. The loader will load the driver/ICD via hw\_get\_module with the ID
1998of "vulkan". **Due to security policies in Android, none of this can be modified
1999under normal use.**
2000
2001
2002### ICD Manifest File Format
2003
2004The following section discusses the details of the ICD Manifest JSON file
2005format. The JSON file itself does not have any requirements for naming. The
2006only requirement is that the extension suffix of the file ends with ".json".
2007
2008Here is an example layer JSON Manifest file:
2009
2010```
2011{
2012 "file_format_version": "1.0.0",
2013 "ICD": {
2014 "library_path": "path to ICD library",
2015 "api_version": "1.0.5"
2016 }
2017}
2018```
2019
2020| Field Name | Field Value |
2021|----------------|--------------------|
2022| "file\_format\_version" | The JSON format major.minor.patch version number of this file. Currently supported version is 1.0.0. |
2023| "ICD" | The identifier used to group all ICD information together. |
2024| "library_path" | The "library\_path" specifies either a filename, a relative pathname, or a full pathname to a layer shared library file. If "library\_path" specifies a relative pathname, it is relative to the path of the JSON manifest file. If "library\_path" specifies a filename, the library must live in the system's shared object search path. There are no rules about the name of the ICD shared library files other than it should end with the appropriate suffix (".DLL" on Windows, and ".so" on Linux). | N/A |
2025| "api_version" | The major.minor.patch version number of the Vulkan API that the shared library files for the ICD was built against. For example: 1.0.33. |
2026
2027**NOTE:** If the same ICD shared library supports multiple, incompatible
2028versions of text manifest file format versions, it must have separate
2029JSON files for each (all of which may point to the same shared library).
2030
2031##### ICD Manifest File Versions
2032
2033There has only been one version of the ICD manifest files supported. This is
2034version 1.0.0.
2035
2036###### ICD Manifest File Version 1.0.0
2037
2038The initial version of the ICD Manifest file specified the basic format and
2039fields of a layer JSON file. The fields of the 1.0.0 file format include:
2040 * "file\_format\_version"
2041 * "ICD"
2042 * "library\_path"
2043 * "api\_version"
2044
2045
2046### ICD Vulkan Entry-Point Discovery
2047
2048The Vulkan symbols exported by an ICD must not clash with the loader's exported
2049Vulkan symbols. This could be for several reasons. Because of this, all ICDs
2050must export the following function that is used for discovery of ICD Vulkan
2051entry-points. This entry-point is not a part of the Vulkan API itself, only a
2052private interface between the loader and ICDs for version 1 and higher
2053interfaces.
2054
2055```cpp
2056VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
2057 VkInstance instance,
2058 const char* pName);
2059```
2060
2061This function has very similar semantics to `vkGetInstanceProcAddr`.
2062`vk_icdGetInstanceProcAddr` returns valid function pointers for all the global-
2063level and instance-level Vulkan functions, and also for `vkGetDeviceProcAddr`.
2064Global-level functions are those which contain no dispatchable object as the
2065first parameter, such as `vkCreateInstance` and
2066`vkEnumerateInstanceExtensionProperties`. The ICD must support querying global-
2067level entry-points by calling `vk_icdGetInstanceProcAddr` with a NULL
2068`VkInstance` parameter. Instance-level functions are those that have either
2069`VkInstance`, or `VkPhysicalDevice` as the first parameter dispatchable object.
2070Both core entry-points and any instance extension entry-points the ICD supports
2071should be available via `vk_icdGetInstanceProcAddr`. Future Vulkan instance
2072extensions may define and use new instance-level dispatchable objects other
2073than `VkInstance` and `VkPhysicalDevice`, in which case extension entry-points
2074using these newly defined dispatchable objects must be queryable via
2075`vk_icdGetInstanceProcAddr`.
2076
2077All other Vulkan entry-points must either:
2078 * NOT be exported directly from the ICD library
2079 * or NOT use the official Vulkan function names if they are exported
2080
2081This requirement is for ICD libraries that include other
2082functionality (such as OpenGL) and thus could be loaded by the
2083application prior to when the Vulkan loader library is loaded by the
2084application.
2085
2086Beware of interposing by dynamic OS library loaders if the official Vulkan
2087names are used. On Linux, if official names are used, the ICD library must be
2088linked with -Bsymbolic.
2089
2090
2091### ICD Unknown Physical Device Extensions
2092
2093Originally, if the loader was called with `vkGetInstanceProcAddr`, it would
2094result in the following behavior:
2095 1. The loader would check if core function:
2096 - If it was, it would return the function pointer
2097 2. The loader would check if known extension function:
2098 - If it was, it would return the function pointer
2099 3. If the loader knew nothing about it, it would call down using
2100`GetInstanceProcAddr`
2101 - If it returned non-NULL, treat it as an unknown logical device command.
2102 - This meant setting up a generic trampoline function that takes in a
2103VkDevice as the first parameter and adjusting the dispatch table to call the
2104ICD/Layers function after getting the dispatch table from the VkDevice.
2105 4. If all the above failed, the loader would return NULL to the application.
2106
2107This caused problems when an ICD attempted to expose new physical device
2108extensions the loader knew nothing about, but an application did. Because the
2109loader knew nothing about it, the loader would get to step 3 in the above
2110process and would treat the function as an unknown logical device command. The
2111problem is, this would create a generic VkDevice trampoline function which, on
2112the first call, would attempt to dereference the VkPhysicalDevice as a VkDevice.
2113This would lead to a crash or corruption.
2114
2115In order to identify the extension entry-points specific to physical device
2116extensions, the following function can be added to an ICD:
2117
2118```cpp
2119PFN_vkVoidFunction vk_icdGetPhysicalDeviceProcAddr(VkInstance instance,
2120 const char* pName);
2121```
2122
2123This function behaves similar to `vkGetInstanceProcAddr` and
2124`vkGetDeviceProcAddr` except it should only return values for physical device
2125extension entry-points. In this way, it compares "pName" to every physical
2126device function supported in the ICD.
2127
2128The following rules apply:
Lenny Komowde3924a2017-05-04 14:50:01 -06002129* If it is the name of a physical device function supported by the ICD, the
Mark Young39389872017-01-19 21:10:49 -07002130pointer to the ICD's corresponding function should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06002131* If it is the name of a valid function which is **not** a physical device
Mark Young39389872017-01-19 21:10:49 -07002132function (i.e. an Instance, Device, or other function implemented by the ICD),
2133then the value of NULL should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06002134* If the ICD has no idea what this function is, it should return NULL.
Mark Young39389872017-01-19 21:10:49 -07002135
2136This support is optional and should not be considered a requirement. This is
2137only required if an ICD intends to support some functionality not directly
2138supported by a significant population of loaders in the public. If an ICD
Mark Young18bbaab2017-03-20 08:27:14 -06002139does implement this support, it should return the address of its
Mark Young39389872017-01-19 21:10:49 -07002140`vk_icdGetPhysicalDeviceProcAddr` function through the `vkGetInstanceProcAddr`
2141function.
2142
2143The new behavior of the loader's vkGetInstanceProcAddr with support for the
2144`vk_icdGetPhysicalDeviceProcAddr` function is as follows:
2145 1. Check if core function:
2146 - If it is, return the function pointer
2147 2. Check if known instance or device extension function:
2148 - If it is, return the function pointer
2149 3. Call the layer/ICD `GetPhysicalDeviceProcAddr`
2150 - If it returns non-NULL, return a trampoline to a generic physical device
2151function, and setup a generic terminator which will pass it to the proper ICD.
2152 4. Call down using `GetInstanceProcAddr`
2153 - If it returns non-NULL, treat it as an unknown logical device command.
2154This means setting up a generic trampoline function that takes in a VkDevice as
2155the first parameter and adjusting the dispatch table to call the ICD/Layers
2156function after getting the dispatch table from the VkDevice. Then, return the
2157pointer to corresponding trampoline function.
2158 5. Return NULL
2159
2160You can see now, that, if the command gets promoted to core later, it will no
2161longer be setup using `vk_icdGetPhysicalDeviceProcAddr`. Additionally, if the
2162loader adds direct support for the extension, it will no longer get to step 3,
2163because step 2 will return a valid function pointer. However, the ICD should
2164continue to support the command query via `vk_icdGetPhysicalDeviceProcAddr`,
2165until at least a Vulkan version bump, because an older loader may still be
2166attempting to use the commands.
2167
2168
2169### ICD Dispatchable Object Creation
2170
2171As previously covered, the loader requires dispatch tables to be accessible
2172within Vulkan dispatchable objects, such as: `VkInstance`, `VkPhysicalDevice`,
2173`VkDevice`, `VkQueue`, and `VkCommandBuffer`. The specific requirements on all
2174dispatchable objects created by ICDs are as follows:
2175
2176- All dispatchable objects created by an ICD can be cast to void \*\*
2177- The loader will replace the first entry with a pointer to the dispatch table
2178 which is owned by the loader. This implies three things for ICD drivers
2179 1. The ICD must return a pointer for the opaque dispatchable object handle
2180 2. This pointer points to a regular C structure with the first entry being a
2181 pointer.
2182 * **NOTE:** For any C\++ ICD's that implement VK objects directly as C\++
2183classes.
2184 * The C\++ compiler may put a vtable at offset zero if your class is non-
2185POD due to the use of a virtual function.
2186 * In this case use a regular C structure (see below).
2187 3. The loader checks for a magic value (ICD\_LOADER\_MAGIC) in all the created
2188 dispatchable objects, as follows (see `include/vulkan/vk_icd.h`):
2189
2190```cpp
2191#include "vk_icd.h"
2192
2193union _VK_LOADER_DATA {
2194 uintptr loadermagic;
2195 void *loaderData;
2196} VK_LOADER_DATA;
2197
2198vkObj alloc_icd_obj()
2199{
2200 vkObj *newObj = alloc_obj();
2201 ...
2202 // Initialize pointer to loader's dispatch table with ICD_LOADER_MAGIC
2203
2204 set_loader_magic_value(newObj);
2205 ...
2206 return newObj;
2207}
2208```
2209
2210
2211### Handling KHR Surface Objects in WSI Extensions
2212
2213Normally, ICDs handle object creation and destruction for various Vulkan
2214objects. The WSI surface extensions for Linux and Windows
2215("VK\_KHR\_win32\_surface", "VK\_KHR\_xcb\_surface", "VK\_KHR\_xlib\_surface",
2216"VK\_KHR\_mir\_surface", "VK\_KHR\_wayland\_surface", and "VK\_KHR\_surface")
2217are handled differently. For these extensions, the `VkSurfaceKHR` object
2218creation and destruction may be handled by either the loader, or an ICD.
2219
2220If the loader handles the management of the `VkSurfaceKHR` objects:
2221 1. The loader will handle the calls to `vkCreateXXXSurfaceKHR` and
2222`vkDestroySurfaceKHR`
2223 functions without involving the ICDs.
2224 * Where XXX stands for the Windowing System name:
2225 * Mir
2226 * Wayland
2227 * Xcb
2228 * Xlib
2229 * Windows
2230 * Android
2231 2. The loader creates a `VkIcdSurfaceXXX` object for the corresponding
2232`vkCreateXXXSurfaceKHR` call.
2233 * The `VkIcdSurfaceXXX` structures are defined in `include/vulkan/vk_icd.h`.
2234 3. ICDs can cast any `VkSurfaceKHR` object to a pointer to the appropriate
2235 `VkIcdSurfaceXXX` structure.
2236 4. The first field of all the `VkIcdSurfaceXXX` structures is a
2237`VkIcdSurfaceBase` enumerant that indicates whether the
2238 surface object is Win32, Xcb, Xlib, Mir, or Wayland.
2239
2240The ICD may choose to handle `VkSurfaceKHR` object creation instead. If an ICD
2241desires to handle creating and destroying it must do the following:
2242 1. Support version 3 or newer of the loader/ICD interface.
2243 2. Export and handle all functions that take in a `VkSurfaceKHR` object,
2244including:
2245 * `vkCreateXXXSurfaceKHR`
2246 * `vkGetPhysicalDeviceSurfaceSupportKHR`
2247 * `vkGetPhysicalDeviceSurfaceCapabilitiesKHR`
2248 * `vkGetPhysicalDeviceSurfaceFormatsKHR`
2249 * `vkGetPhysicalDeviceSurfacePresentModesKHR`
2250 * `vkCreateSwapchainKHR`
2251 * `vkDestroySurfaceKHR`
2252
2253Because the `VkSurfaceKHR` object is an instance-level object, one object can be
2254associated with multiple ICDs. Therefore, when the loader receives the
2255`vkCreateXXXSurfaceKHR` call, it still creates an internal `VkSurfaceIcdXXX`
2256object. This object acts as a container for each ICD's version of the
2257`VkSurfaceKHR` object. If an ICD does not support the creation of its own
2258`VkSurfaceKHR` object, the loader's container stores a NULL for that ICD. On
2259the otherhand, if the ICD does support `VkSurfaceKHR` creation, the loader will
2260make the appropriate `vkCreateXXXSurfaceKHR` call to the ICD, and store the
2261returned pointer in it's container object. The loader then returns the
2262`VkSurfaceIcdXXX` as a `VkSurfaceKHR` object back up the call chain. Finally,
2263when the loader receives the `vkDestroySurfaceKHR` call, it subsequently calls
2264`vkDestroySurfaceKHR` for each ICD who's internal `VkSurfaceKHR` object is not
2265NULL. Then the loader destroys the container object before returning.
2266
2267
2268### Loader and ICD Interface Negotiation
2269
2270Generally, for functions issued by an application, the loader can be
2271viewed as a pass through. That is, the loader generally doesn't modify the
2272functions or their parameters, but simply calls the ICDs entry-point for that
2273function. There are specific additional interface requirements an ICD needs to
2274comply with that are not part of any requirements from the Vulkan specification.
2275These addtional requirements are versioned to allow flexibility in the future.
2276
2277
2278#### Windows and Linux ICD Negotiation
2279
2280
2281##### Version Negotiation Between Loader and ICDs
2282
2283All ICDs (supporting interface version 2 or higher) must export the following
2284function that is used for determination of the interface version that will be
2285used. This entry-point is not a part of the Vulkan API itself, only a private
2286interface between the loader and ICDs.
2287
2288```cpp
2289 VKAPI_ATTR VkResult VKAPI_CALL
2290 vk_icdNegotiateLoaderICDInterfaceVersion(
2291 uint32_t* pSupportedVersion);
2292```
2293
2294This function allows the loader and ICD to agree on an interface version to use.
2295The "pSupportedVersion" parameter is both an input and output parameter.
2296"pSupportedVersion" is filled in by the loader with the desired latest interface
2297version supported by the loader (typically the latest). The ICD receives this
2298and returns back the version it desires in the same field. Because it is
2299setting up the interface version between the loader and ICD, this should be
2300the first call made by a loader to the ICD (even prior to any calls to
2301`vk_icdGetInstanceProcAddr`).
2302
2303If the ICD receiving the call no longer supports the interface version provided
2304by the loader (due to deprecation), then it should report
2305VK_ERROR_INCOMPATIBLE_DRIVER error. Otherwise it sets the value pointed by
2306"pSupportedVersion" to the latest interface version supported by both the ICD
2307and the loader and returns VK_SUCCESS.
2308
2309The ICD should report VK_SUCCESS in case the loader provided interface version
2310is newer than that supported by the ICD, as it's the loader's responsibility to
2311determine whether it can support the older interface version supported by the
2312ICD. The ICD should also report VK_SUCCESS in the case its interface version
2313is greater than the loader's, but return the loader's version. Thus, upon
2314return of VK_SUCCESS the "pSupportedVersion" will contain the desired interface
2315version to be used by the ICD.
2316
2317If the loader receives an interface version from the ICD that the loader no
2318longer supports (due to deprecation), or it receives a
2319VK_ERROR_INCOMPATIBLE_DRIVER error instead of VK_SUCCESS, then the loader will
2320treat the ICD as incompatible and will not load it for use. In this case, the
2321application will not see the ICDs `vkPhysicalDevice` during enumeration.
2322
2323###### Interfacing With Legacy ICDs or Loader
2324
2325If a loader sees that an ICD does not export the
2326`vk_icdNegotiateLoaderICDInterfaceVersion` function, then the loader assumes the
2327corresponding ICD only supports either interface version 0 or 1.
2328
2329From the other side of the interface, if an ICD sees a call to
2330`vk_icdGetInstanceProcAddr` before a call to
2331`vk_icdGetLoaderICDInterfaceVersion`, then it knows that loader making the calls
2332is a legacy loader supporting version 0 or 1. If the loader calls
2333`vk_icdGetInstanceProcAddr` first, it supports at least version 1. Otherwise,
2334the loader only supports version 0.
2335
2336
2337##### Loader Version 4 Interface Requirements
2338
2339The major change to version 4 of the loader/ICD interface is the support of
2340[Unknown Physical Device Extensions](#icd-unknown-physical-device-
2341extensions] using the `vk_icdGetPhysicalDeviceProcAddr` function. This
2342function is purely optional. However, if an ICD supports a Physical Device
2343extension, it must provide a `vk_icdGetPhysicalDeviceProcAddr` function.
2344Otherwise, the loader will continue to treat any unknown functions as VkDevice
2345functions and cause invalid behavior.
2346
2347
2348##### Loader Version 3 Interface Requirements
2349
2350The primary change that occurred in version 3 of the loader/ICD interface was to
2351allow an ICD to handle creation/destruction of their own KHR_surfaces. Up until
2352this point, the loader created a surface object that was used by all ICDs.
2353However, some ICDs may want to provide their own surface handles. If an ICD
2354chooses to enable this support, it must export support for version 3 of the
2355loader/ICD interface, as well as any Vulkan function that uses a KHR_surface
2356handle, such as:
2357- `vkCreateXXXSurfaceKHR` (where XXX is the platform specific identifier [i.e.
2358`vkCreateWin32SurfaceKHR` for Windows])
2359- `vkDestroySurfaceKHR`
2360- `vkCreateSwapchainKHR`
2361- `vkGetPhysicalDeviceSurfaceSupportKHR`
2362- `vkGetPhysicalDeviceSurfaceCapabilitiesKHR`
2363- `vkGetPhysicalDeviceSurfaceFormatsKHR`
2364- `vkGetPhysicalDeviceSurfacePresentModesKHR`
2365
2366An ICD can still choose to not take advantage of this functionality by simply
2367not exposing the above the `vkCreateXXXSurfaceKHR` and `vkDestroySurfaceKHR`
2368functions.
2369
2370
2371##### Loader Version 2 Interface Requirements
2372
2373Version 2 interface has requirements in three areas:
2374 1. ICD Vulkan entry-point discovery,
2375 2. `KHR_surface` related requirements in the WSI extensions,
2376 3. Vulkan dispatchable object creation requirements.
2377
2378##### Loader Versions 0 and 1 Interface Requirements
2379
2380Version 0 and 1 interfaces do not support version negotiation via
2381`vk_icdNegotiateLoaderICDInterfaceVersion`. ICDs can distinguish version 0 and
2382version 1 interfaces as follows: if the loader calls `vk_icdGetInstanceProcAddr`
2383first it supports version 1; otherwise the loader only supports version 0.
2384
2385Version 0 interface does not support `vk_icdGetInstanceProcAddr`. Version 0
2386interface requirements for obtaining ICD Vulkan entry-points are as follows:
2387
2388- The function `vkGetInstanceProcAddr` **must be exported** in the ICD library
2389and returns valid function pointers for all the Vulkan API entry-points.
2390- `vkCreateInstance` **must be exported** by the ICD library.
2391- `vkEnumerateInstanceExtensionProperties` **must be exported** by the ICD
2392library.
2393
2394Additional Notes:
2395
2396- The loader will filter out extensions requested in `vkCreateInstance` and
2397`vkCreateDevice` before calling into the ICD; Filtering will be of extensions
2398advertised by entities (e.g. layers) different from the ICD in question.
2399- The loader will not call the ICD for `vkEnumerate\*LayerProperties`() as layer
2400properties are obtained from the layer libraries and layer JSON files.
2401- If an ICD library author wants to implement a layer, it can do so by having
2402the appropriate layer JSON manifest file refer to the ICD library file.
2403- The loader will not call the ICD for
2404 `vkEnumerate\*ExtensionProperties` if "pLayerName" is not equal to `NULL`.
2405- ICDs creating new dispatchable objects via device extensions need to
2406initialize the created dispatchable object. The loader has generic *trampoline*
2407code for unknown device extensions. This generic *trampoline* code doesn't
2408initialize the dispatch table within the newly created object. See the
2409[Creating New Dispatchable Objects](#creating-new-dispatchable-objects) section
2410for more information on how to initialize created dispatchable objects for
2411extensions non known by the loader.
2412
2413
2414#### Android ICD Negotiation
2415
2416The Android loader uses the same protocol for initializing the dispatch
2417table as described above. The only difference is that the Android
2418loader queries layer and extension information directly from the
2419respective libraries and does not use the json manifest files used
2420by the Windows and Linux loaders.
2421
Mark Young540d71c2017-05-18 14:52:14 -06002422## Table of Debug Environment Variables
2423
2424The following are all the Debug Environment Variables available for use with the
2425Loader. These are referenced throughout the text, but collected here for ease
2426of discovery.
2427
2428| Environment Variable | Behavior | Example Format |
2429|----------------|---------------------|----------------------|
2430| VK_ICD_FILENAMES | Force the loader to use the specific ICD JSON files. The value should contain a list of delimited full path listings to ICD JSON Manifest files. **NOTE:** If you fail to use the global path to a JSON file, you may encounter issues. | `export VK_ICD_FILENAMES=<folder_a>\intel.json:<folder_b>\amd.json`<br/><br/>`set VK_ICD_FILENAMES=<folder_a>\nvidia.json;<folder_b>\mesa.json` |
2431| VK_INSTANCE_LAYERS | Force the loader to add the given layers to the list of Enabled layers normally passed into `vkCreateInstance`. These layers are added first, and the loader will remove any duplicate layers that appear in both this list as well as that passed into `ppEnabledLayerNames`. | `export VK_INSTANCE_LAYERS=<layer_a>:<layer_b>`<br/><br/>`set VK_INSTANCE_LAYERS=<layer_a>;<layer_b>` |
2432| VK_LAYER_PATH | Override the loader's standard Layer library search folders and use the provided delimited folders to search for layer Manifest files. | `export VK_LAYER_PATH=<path_a>:<path_b>`<br/><br/>`set VK_LAYER_PATH=<path_a>;<pathb>` |
2433| VK_LOADER_DISABLE_INST_EXT_FILTER | Disable the filtering out of instance extensions that the loader doesn't know about. This will allow applications to enable instance extensions exposed by ICDs but that the loader has no support for. **NOTE:** This may cause the loader or applciation to crash. | `export VK_LOADER_DISABLE_INST_EXT_FILTER=1`<br/><br/>`set VK_LOADER_DISABLE_INST_EXT_FILTER=1` |
2434| VK_LOADER_DEBUG | Enable loader debug messages. Options are:<br/>- error (only errors)<br/>- warn (warnings and errors)<br/>- info (info, warning, and errors)<br/> - debug (debug + all before) <br/> -all (report out all messages) | `export VK_LOADER_DEBUG=all`<br/><br/>`set VK_LOADER_DEBUG=warn` |
Mark Young39389872017-01-19 21:10:49 -07002435
2436## Glossary of Terms
2437
2438| Field Name | Field Value |
2439|----------------|--------------------|
2440| Android Loader | The loader designed to work primarily for the Android OS. This is generated from a different code-base than the desktop loader. But, in all important aspects, should be functionally equivalent. |
2441| Desktop Loader | The loader designed to work on both Windows and Linux. This is generated from a different [code-base](#https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers) than the Android loader. But in all important aspects, should be functionally equivalent. |
2442| Core Function | A function that is already part of the Vulkan core specification and not an extension. For example, vkCreateDevice(). |
2443| Device Call Chain | The call chain of functions followed for device functions. This call chain for a device function is usually as follows: first the application calls into a loader trampoline, then the loader trampoline calls enabled layers, the final layer calls into the ICD specific to the device. See the [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) section for more information |
2444| Device Function | A Device function is any Vulkan function which takes a `VkDevice`, `VkQueue`, `VkCommandBuffer`, or any child of these, as its first parameter. Some Vulkan Device functions are: `vkQueueSubmit`, `vkBeginCommandBuffer`, `vkCreateEvent`. See the [Instance Versus Device](#instance-versus-device) section for more information. |
2445| Discovery | The process of the loader searching for ICD and Layer files to setup the internal list of Vulkan objects available. On Windows/Linux, the discovery process typically focuses on searching for Manifest files. While on Android, the process focuses on searching for library files. |
2446| Dispatch Table | An array of function pointers (including core and possibly extension functions) used to step to the next entity in a call chain. The entity could be the loader, a layer or an ICD. See [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) for more information. |
2447| Extension | A concept of Vulkan used to expand the core Vulkan functionality. Extensions may be IHV-specific, platform-specific, or more broadly available. You should always query if an extension exists, and enable it during `vkCreateInstance` (if it is an instance extension) or during `vkCreateDevice` (if it is a device extension). |
2448| ICD | Acronym for Installable Client Driver. These are drivers that are provided by IHVs to interact with the hardware they provide. See [Installable Client Drivers](#installable-client-drivers) section for more information.
2449| IHV | Acronym for an Independent Hardware Vendor. Typically the company that built the underlying hardware technology you are trying to use. A typical examples for a Graphics IHV are: AMD, ARM, Imagination, Intel, Nvidia, Qualcomm, etc. |
2450| Instance Call Chain | The call chain of functions followed for instance functions. This call chain for an instance function is usually as follows: first the application calls into a loader trampoline, then the loader trampoline calls enabled layers, the final layer calls a loader terminator, and the loader terminator calls all available ICDs. See the [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) section for more information |
2451| Instance Function | An Instance function is any Vulkan function which takes as its first parameter either a `VkInstance` or a `VkPhysicalDevice` or nothing at all. Some Vulkan Instance functions are: `vkEnumerateInstanceExtensionProperties`, `vkEnumeratePhysicalDevices`, `vkCreateInstance`, `vkDestroyInstance`. See the [Instance Versus Device](#instance-versus-device) section for more information. |
2452| Layer | Layers are optional components that augment the Vulkan system. They can intercept, evaluate, and modify existing Vulkan functions on their way from the application down to the hardware. See the [Layers](#layers) section for more information. |
2453| Loader | The middle-ware program which acts as the mediator between Vulkan applications, Vulkan layers and Vulkan drivers. See [The Loader](#the loader) section for more information. |
2454| Manifest Files | Data files in JSON format used by the desktop loader. These files contain specific information for either a [Layer](#layer-manifest-file-format) or an [ICD](#icd-manifest-file-format).
2455| Terminator Function | The last function in the instance call chain above the ICDs and owned by the loader. This function is required in the instance call chain because all instance functionality must be communicated to all ICDs capable of receiving the call. See [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) for more information. |
2456| Trampoline Function | The first function in an instance or device call chain owned by the loader which handles the setup and proper call chain walk using the appropriate dispatch table. On device functions (in the device call chain) this function can actually be skipped. See [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) for more information. |
2457| WSI Extension | Acronym for Windowing System Integration. A Vulkan extension targeting a particular Windowing and designed to interface between the Windowing system and Vulkan. See [WSI Extensions](#wsi-extensions) for more information. |