blob: cf11c78fb5bc9e5ca9fe02fb762436759d1c4cfe [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)
28 * [Special Considerations](#special-considerations)
29 * [Layer Manifest File Format](#layer-manifest-file-format)
30 * [Layer Library Versions](#layer-library-versions)
Jon Ashburnc2972682016-02-08 15:42:01 -070031
Lenny Komowde3924a2017-05-04 14:50:01 -060032 * [Vulkan Installable Client Driver interface with the loader](#vulkan-installable-client-driver-interface-with-the-loader)
33 * [ICD Discovery](#icd-discovery)
34 * [ICD Manifest File Format](#icd-manifest-file-format)
35 * [ICD Vulkan Entry-Point Discovery](#icd-vulkan-entry-point-discovery)
36 * [ICD Unknown Physical Device Extensions](#icd-unknown-physical-device-extensions)
37 * [ICD Dispatchable Object Creation](#icd-dispatchable-object-creation)
38 * [Handling KHR Surface Objects in WSI Extensions](#handling-khr-surface-objects-in-wsi-extensions)
39 * [Loader and ICD Interface Negotiation](#loader-and-icd-interface-negotiation)
40 * [Glossary of Terms](#glossary-of-terms)
Mark Young39389872017-01-19 21:10:49 -070041
42## Overview
Mark Youngcb6e6702016-07-20 11:38:53 -060043
Mark Young39389872017-01-19 21:10:49 -070044Vulkan is a layered architecture, made up of the following elements:
Lenny Komowde3924a2017-05-04 14:50:01 -060045 * The Vulkan Application
46 * [The Vulkan Loader](#the-loader)
47 * [Vulkan Layers](#layers)
48 * [Installable Client Drivers (ICDs)](#installable-client-drivers)
Jon Ashburnc2972682016-02-08 15:42:01 -070049
Mark Young39389872017-01-19 21:10:49 -070050![High Level View of Loader](./images/high_level_loader.png)
Jon Ashburnc2972682016-02-08 15:42:01 -070051
Mark Young39389872017-01-19 21:10:49 -070052The general concepts in this document are applicable to the loaders available
53for Windows, Linux and Android based systems.
Jon Ashburnc2972682016-02-08 15:42:01 -070054
55
Mark Young39389872017-01-19 21:10:49 -070056#### Who Should Read This Document
Jon Ashburnc2972682016-02-08 15:42:01 -070057
Mark Young39389872017-01-19 21:10:49 -070058While this document is primarily targeted at developers of Vulkan applications,
59drivers and layers, the information contained in it could be useful to anyone
60wanting a better understanding of the Vulkan runtime.
Jon Ashburnc2972682016-02-08 15:42:01 -070061
Jon Ashburnc2972682016-02-08 15:42:01 -070062
Mark Young39389872017-01-19 21:10:49 -070063#### The Loader
Jon Ashburnc2972682016-02-08 15:42:01 -070064
Jeff Juliano18e50202017-01-31 16:36:18 -050065The application sits on one end of, and interfaces directly with, the
66loader. On the other end of the loader from the application are the ICDs, which
Mark Young39389872017-01-19 21:10:49 -070067control the Vulkan-capable hardware. An important point to remember is that
Jeff Juliano18e50202017-01-31 16:36:18 -050068Vulkan-capable hardware can be graphics-based, compute-based, or both. Between
69the application and the ICDs the loader can inject a number of optional
70[layers](#layers) that provide special functionality.
Mark Youngcb6e6702016-07-20 11:38:53 -060071
72The loader is responsible for working with the various layers as well as
Mark Young39389872017-01-19 21:10:49 -070073supporting multiple GPUs and their drivers. Any Vulkan function may
Mark Youngcb6e6702016-07-20 11:38:53 -060074wind up calling into a diverse set of modules: loader, layers, and ICDs.
75The loader is critical to managing the proper dispatching of Vulkan
Mark Young39389872017-01-19 21:10:49 -070076functions to the appropriate set of layers and ICDs. The Vulkan object
Mark Youngcb6e6702016-07-20 11:38:53 -060077model allows the loader to insert layers into a call chain so that the layers
Mark Young39389872017-01-19 21:10:49 -070078can process Vulkan functions prior to the ICD being called.
79
80This document is intended to provide an overview of the necessary interfaces
81between each of these.
82
83
84##### Goals of the Loader
85
86The loader was designed with the following goals in mind.
87 1. Support one or more Vulkan-capable ICD on a user's computer system without
88them interfering with one another.
89 2. Support Vulkan Layers which are optional modules that can be enabled by an
90application, developer, or standard system settings.
91 3. Impact the overall performance of a Vulkan application in the lowest
92possible fashion.
93
94
95#### Layers
96
97Layers are optional components that augment the Vulkan system. They can
98intercept, evaluate, and modify existing Vulkan functions on their way from the
99application down to the hardware. Layers are implemented as libraries that can
100be enabled in different ways (including by application request) and are loaded
101during CreateInstance. Each layer can choose to hook (intercept) any Vulkan
102functions which in turn can be ignored or augmented. A layer does not need to
103intercept all Vulkan functions. It may choose to intercept all known functions,
104or, it may choose to intercept only one function.
105
106Some examples of features that layers may expose include:
107 * Validating API usage
108 * Adding the ability to perform Vulkan API tracing and debugging
109 * Overlay additional content on the applications surfaces
110
111Because layers are optionally, you may choose to enable layers for debugging
112your application, but then disable any layer usage when you release your
113product.
114
115
116#### Installable Client Drivers
117
118Vulkan allows multiple Installable Client Drivers (ICDs) each supporting one
119or more devices (represented by a Vulkan `VkPhysicalDevice` object) to be used
120collectively. The loader is responsible for discovering available Vulkan ICDs on
121the system. Given a list of available ICDs, the loader can enumerate all the
122physical devices available for an application and return this information to
123the application.
124
125
126#### Instance Versus Device
127
128There is an important concept which you will see brought up repeatedly
129throughout this document. Many functions, extensions, and other things in
130Vulkan are separated into two main groups:
131 * Instance-related Objects
132 * Device-related Objects
133
134
135##### Instance-related Objects
136
137A Vulkan Instance is a high-level construct used to provide Vulkan system-level
138information, or functionality. Vulkan objects associated directly with an
139instance are:
140 * `VkInstance`
141 * `VkPhysicalDevice`
142
143An Instance function is any Vulkan function which takes as its first parameter
144either an object from the Instance list, or nothing at all. Some Vulkan
145Instance functions are:
146 * `vkEnumerateInstanceExtensionProperties`
147 * `vkEnumeratePhysicalDevices`
148 * `vkCreateInstance`
149 * `vkDestroyInstance`
150
151You query Vulkan Instance functions using `vkGetInstanceProcAddr`.
152`vkGetInstanceProcAddr` can be used to query either device or instance entry-
153points in addition to all core entry-points. The returned function pointer is
154valid for this Instance and any object created under this Instance (including
155all `VkDevice` objects).
156
157Similarly, an Instance extension is a set of Vulkan Instance functions extending
158the Vulkan language. These will be discussed in more detail later.
159
160
161##### Device-related Objects
162
163A Vulkan Device, on the other-hand, is a logical identifier used to associate
164functions with a particular physical device on a user's system. Vulkan
165constructs associated directly with a device include:
166 * `VkDevice`
167 * `VkQueue`
168 * `VkCommandBuffer`
169 * Any dispatchable object that is a child of a one of the above.
170
171A Device function is any Vulkan function which takes any Device Object as its
172first parameter. Some Vulkan Device functions are:
173 * `vkQueueSubmit`
174 * `vkBeginCommandBuffer`
175 * `vkCreateEvent`
176
177You can query Vulkan Device functions using either `vkGetInstanceProcAddr` or
178`vkGetDeviceProcAddr`. If you choose to use `vkGetInstanceProcAddr`, it will
179have an additional level built into the call chain, which will reduce
180performance slightly. However, the function pointer returned can be used for
181any device created later, as long as it is associated with the same Vulkan
182Instance. If, instead you use `vkGetDeviceProcAddr`, the call chain will be more
183optimized to the specific device, but it will **only** work for the device used
184to query the function function pointer. Also, unlike `vkGetInstanceProcAddr`,
185`vkGetDeviceProcAddr` can only be used on core Vulkan Device functions, or
186Device extension functions.
187
188The best solution is to query Instance extension functions using
189`vkGetInstanceProcAddr`, and to query Device extension functions using
190`vkGetDeviceProcAddr`. See
191[Best Application Performance Setup](#best-application-performance-setup) for
192more information on this.
193
194As with Instance extensions, a Device extension is a set of Vulkan Device
195functions extending the Vulkan language. You can read more about these later in
196the document.
197
198
199#### Dispatch Tables and Call Chains
Jon Ashburnc2972682016-02-08 15:42:01 -0700200
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700201Vulkan uses an object model to control the scope of a particular action /
202operation. The object to be acted on is always the first parameter of a Vulkan
Mark Young6d026a72016-06-01 17:49:30 -0600203call and is a dispatchable object (see Vulkan specification section 2.3 Object
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700204Model). Under the covers, the dispatchable object handle is a pointer to a
Mark Youngcb6e6702016-07-20 11:38:53 -0600205structure, which in turn, contains a pointer to a dispatch table maintained by
Mark Young39389872017-01-19 21:10:49 -0700206the loader. This dispatch table contains pointers to the Vulkan functions
207appropriate to that object.
Jon Ashburnc2972682016-02-08 15:42:01 -0700208
Mark Youngcb6e6702016-07-20 11:38:53 -0600209There are two types of dispatch tables the loader maintains:
Mark Young39389872017-01-19 21:10:49 -0700210 - Instance Dispatch Table
211 - Created in the loader during the call to `vkCreateInstance`
212 - Device Dispatch Table
213 - Created in the loader during the call to `vkCreateDevice`
Jon Ashburnc2972682016-02-08 15:42:01 -0700214
Mark Young39389872017-01-19 21:10:49 -0700215At that time the application and/or system can specify optional layers to be
216included. The loader will initialize the specified layers to create a call
217chain for each Vulkan function and each entry of the dispatch table will point
218to the first element of that chain. Thus, the loader builds an instance call
219chain for each `VkInstance` that is created and a device call chain for each
220`VkDevice` that is created.
221
222When an application calls a Vulkan function, this typically will first hit a
223*trampoline* function in the loader. These *trampoline* functions are small,
224simple functions that jump to the appropriate dispatch table entry for the
225object they are given. Additionally, for functions in the instance call chain,
226the loader has an additional function, called a *terminator*, which is called
227after all enabled layers to marshall the appropriate information to all
228available ICDs.
229
230
231##### Instance Call Chain Example
Jon Ashburnc2972682016-02-08 15:42:01 -0700232
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700233For example, the diagram below represents what happens in the call chain for
Mark Young39389872017-01-19 21:10:49 -0700234`vkCreateInstance`. After initializing the chain, the loader will call into the
235first layer's `vkCreateInstance` which will call the next finally terminating in
236the loader again where this function calls every ICD's `vkCreateInstance` and
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700237saves the results. This allows every enabled layer for this chain to set up
Mark Young39389872017-01-19 21:10:49 -0700238what it needs based on the `VkInstanceCreateInfo` structure from the
239application.
240
241![Instance Call Chain](./images/loader_instance_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -0700242
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700243This also highlights some of the complexity the loader must manage when using
Mark Young39389872017-01-19 21:10:49 -0700244instance call chains. As shown here, the loader's *terminator* must aggregate
245information to and from multiple ICDs when they are present. This implies that
246the loader has to be aware of any instance-level extensions which work on a
247`VkInstance` to aggregate them correctly.
Jon Ashburnc2972682016-02-08 15:42:01 -0700248
Mark Young39389872017-01-19 21:10:49 -0700249
250##### Device Call Chain Example
251
252Device call chains are created at `vkCreateDevice` and are generally simpler
253because they deal with only a single device and the ICD can always be the
254*terminator* of the chain.
255
256![Loader Device Call Chain](./images/loader_device_chain_loader.png)
257
Jon Ashburnc2972682016-02-08 15:42:01 -0700258
Mark Youngcb6e6702016-07-20 11:38:53 -0600259<br/>
Mark Young39389872017-01-19 21:10:49 -0700260<br/>
Mark Youngcb6e6702016-07-20 11:38:53 -0600261
Mark Young39389872017-01-19 21:10:49 -0700262## Application Interface to the Loader
Jon Ashburnc2972682016-02-08 15:42:01 -0700263
Mark Young39389872017-01-19 21:10:49 -0700264In this section we'll discuss how an application interacts with the loader,
265including:
Lenny Komowde3924a2017-05-04 14:50:01 -0600266 * [Interfacing with Vulkan Functions](#interfacing-with-vulkan-functions)
267 * [Vulkan Direct Exports](#vulkan-direct-exports)
268 * [Directly Linking to the Loader](#directly-linking-to-the-loader)
269 * [Dynamic Linking](#dynamic-linking)
270 * [Static Linking](#static-linking)
271 * [Indirectly Linking to the Loader](#indirectly-linking-to-the-loader)
272 * [Best Application Performance Setup](#best-application-performance-setup)
273 * [ABI Versioning](#abi-versioning)
274 * [Application Layer Usage](#application-layer-usage)
275 * [Implicit vs Explicit Layers](#implicit-vs-explicit-layers)
276 * [Forcing Layer Source Folders](#forcing-layer-source-folders)
277 * [Forcing Layers to be Enabled](#forcing-layers-to-be-enabled)
278 * [Overall Layer Ordering](#overall-layer-ordering)
279 * [Application Usage of Extensions](#application-usage-of-extensions)
280 * [Instance and Device Extensions](#instance-and-device-extensions)
281 * [WSI Extensions](#wsi-extensions)
282 * [Unknown Extensions](#unknown-extensions)
Jon Ashburnc2972682016-02-08 15:42:01 -0700283
Mark Young39389872017-01-19 21:10:49 -0700284
285#### Interfacing with Vulkan Functions
286There are several ways you can interface with Vulkan functions through the
287loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700288
Jon Ashburnc2972682016-02-08 15:42:01 -0700289
Mark Young39389872017-01-19 21:10:49 -0700290##### Vulkan Direct Exports
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700291The loader library on Windows, Linux and Android will export all core Vulkan
292and all appropriate Window System Interface (WSI) extensions. This is done to
293make it simpler to get started with Vulkan development. When an application
294links directly to the loader library in this way, the Vulkan calls are simple
Mark Young39389872017-01-19 21:10:49 -0700295*trampoline* functions that jump to the appropriate dispatch table entry for the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700296object they are given.
Jon Ashburnc2972682016-02-08 15:42:01 -0700297
Mark Young39389872017-01-19 21:10:49 -0700298
Lenny Komowde3924a2017-05-04 14:50:01 -0600299##### Directly Linking to the Loader
300
301###### Dynamic Linking
302The loader is ordinarily distributed as a dynamic library (.dll on Windows or
303.so on Linux) which gets installed to the system path for dynamic libraries.
304Linking to the dynamic library is generally the preferred method of linking to
305the loader, as doing so allows the loader to be updated for bug fixes and
306improvements. Furthermore, the dynamic library is generally installed to Windows
307systems as part of driver installation and is generally provided on Linux
308through the system package manager. This means that applications can usually
309expect a copy of the loader to be present on a system. If applications want to
310be completely sure that a loader is present, they can include a loader or
311runtime installer with their application.
312
313###### Static Linking
314The loader can also be used as a static library (this is shipped in the
315Windows SDK as `VKstatic.1.lib`). Linking to the static loader means that the
316user does not need to have a Vulkan runtime installed, and it also guarantees
317that your application will use a specific version of the loader. However, there
318are several downsides to this approach:
319
320 - The static library can never be updated without re-linking the application
321 - This opens up the possibility that two included libraries could contain
322 different versions of the loader
323 - This could potentially cause conflicts between the different loader versions
324
325As a result, it is recommended that users prefer linking to the .dll and
326.so versions of the loader.
327
328
Mark Young39389872017-01-19 21:10:49 -0700329##### Indirectly Linking to the Loader
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700330Applications are not required to link directly to the loader library, instead
331they can use the appropriate platform specific dynamic symbol lookup on the
Mark Young6d026a72016-06-01 17:49:30 -0600332loader library to initialize the application's own dispatch table. This allows
Mark Young39389872017-01-19 21:10:49 -0700333an application to fail gracefully if the loader cannot be found. It also
Jeff Julianof1619872016-02-17 17:25:42 -0500334provides the fastest mechanism for the application to call Vulkan functions. An
335application will only need to query (via system calls such as dlsym()) the
Mark Young39389872017-01-19 21:10:49 -0700336address of `vkGetInstanceProcAddr` from the loader library. Using
337`vkGetInstanceProcAddr` the application can then discover the address of all
338functions and extensions available, such as `vkCreateInstance`,
339`vkEnumerateInstanceExtensionProperties` and
340`vkEnumerateInstanceLayerProperties` in a platform-independent way.
Jon Ashburnc2972682016-02-08 15:42:01 -0700341
Mark Young39389872017-01-19 21:10:49 -0700342
343##### Best Application Performance Setup
344
345If you desire the best performance possible, you should setup your own
346dispatch table so that all your Instance functions are queried using
347`vkGetInstanceProcAddr` and all your Device functions are queried using
348`vkGetDeviceProcAddr`.
349
350*Why should you do this?*
351
352The answer comes in how the call chain of Instance functions are implemented
353versus the call chain of a Device functions. Remember, a [Vulkan Instance is a
354high-level construct used to provide Vulkan system-level information](#instance-
355related-objects). Because of this, Instance functions need to be broadcasted to
356every available ICD on the system. The following diagram shows an approximate
357view of an Instance call chain with 3 enabled layers:
358
359![Instance Call Chain](./images/loader_instance_chain.png)
360
361This is also how a Vulkan Device function call chain looks if you query it
362using `vkGetInstanceProcAddr`. On the otherhand, a Device
363function doesn't need to worry about the broadcast becuase it knows specifically
364which associated ICD and which associated Physical Device the call should
365terminate at. Because of this, the loader doesn't need to get involved between
366any enabled layers and the ICD. Thus, if you used a loader-exported Vulkan
367Device function, the call chain in the same scenario as above would look like:
368
369![Loader Device Call Chain](./images/loader_device_chain_loader.png)
370
371An even better solution would be for an application to perform a
372`vkGetDeviceProcAddr` call on all Device functions. This further optimizes the
373call chain by removing the loader all-together under most scenarios:
374
375![Application Device Call Chain](./images/loader_device_chain_app.png)
376
377Also, notice if no layers are enabled, your application function pointer would
378point **directly to the ICD**. If called enough, those fewer calls can add up
379to performance savings.
380
381**NOTE:** There are some Device functions which still require the loader to
382intercept them with a *trampoline* and *terminator*. There are very few of
383these, but they are typically functions which the loader wraps with its own
384data. In those cases, even the Device call chain will continue to look like the
385Instance call chain. One example of a Device function requiring a *terminator*
386is `vkCreateSwapchainKHR`. For that function, the loader needs to potentially
387convert the KHR_surface object into an ICD-specific KHR_surface object prior to
388passing down the rest of the function's information to the ICD.
389
390Remember:
391 * `vkGetInstanceProcAddr` can be used to query
392either device or instance entry-points in addition to all core entry-points.
393 * `vkGetDeviceProcAddr` can only be used to query for device
394extension or core device entry-points.
395
396
397##### ABI Versioning
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700398The Vulkan loader library will be distributed in various ways including Vulkan
Mark Young39389872017-01-19 21:10:49 -0700399SDKs, OS package distributions and Independent Hardware Vendor (IHV) driver
400packages. These details are beyond the scope of this document. However, the name
401and versioning of the Vulkan loader library is specified so an app can link to
402the correct Vulkan ABI library version. Vulkan versioning is such that ABI
403backwards compatibility is guaranteed for all versions with the same major
404number (e.g. 1.0 and 1.1). On Windows, the loader library encodes the ABI
405version in its name such that multiple ABI incompatible versions of the loader
406can peacefully coexist on a given system. The Vulkan loader library file name is
Jeff Juliano18e50202017-01-31 16:36:18 -0500407`vulkan-<ABI version>.dll`. For example, for Vulkan version 1.X on Windows the
Mark Young39389872017-01-19 21:10:49 -0700408library filename is vulkan-1.dll. And this library file can typically be found
409in the windows/system32 directory (on 64-bit Windows installs, the 32-bit
410version of the loader with the same name can be found in the windows/sysWOW64
411directory).
Jon Ashburnc2972682016-02-08 15:42:01 -0700412
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700413For Linux, shared libraries are versioned based on a suffix. Thus, the ABI
414number is not encoded in the base of the library filename as on Windows. On
415Linux an application wanting to link to the latest Vulkan ABI version would
416just link to the name vulkan (libvulkan.so). A specific Vulkan ABI version can
Jeff Julianof1619872016-02-17 17:25:42 -0500417also be linked to by applications (e.g. libvulkan.so.1).
Jon Ashburnc2972682016-02-08 15:42:01 -0700418
Mark Young39389872017-01-19 21:10:49 -0700419
420#### Application Layer Usage
Mark Young6d026a72016-06-01 17:49:30 -0600421
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700422Applications desiring Vulkan functionality beyond what the core API offers may
Mark Young39389872017-01-19 21:10:49 -0700423use various layers or extensions. A layer cannot introduce new Vulkan core API
424entry-points to an application that are not exposed in Vulkan.h. However,
425layers may offer extensions that introduce new Vulkan commands that can be
426queried through the extension interface.
Mark Young02ee5382016-07-22 08:51:05 -0600427
Mark Young39389872017-01-19 21:10:49 -0700428A common use of layers is for API validation which can be enabled by
429loading the layer during application development, but not loading the layer
430for application release. This eliminates the overhead of validating the
431application's usage of the API, something that wasn't available on some previous
432graphics APIs.
433
434To find out what layers are available to your application, use
435`vkEnumerateInstanceLayerProperties`. This will report all layers
436that have been discovered by the loader. The loader looks in various locations
437to find layers on the system. For more information see the
438[Layer discovery](#layer-discovery) section below.
439
440To enable a layer, or layers, simply pass the name of the layers you wish to
441enable in the `ppEnabledLayerNames` field of the `VkInstanceCreateInfo` during
442a call to `vkCreateInstance`. Once done, the layers you have enabled will be
443active for all Vulkan functions using the created `VkInstance`, and any of
444its child objects.
445
446**NOTE:** Layer ordering is important in several cases since some layers
447interact with each other. Be careful when enabling layers as this may be
448the case. See the [Overall Layer Ordering](#overall-layer-ordering) section
449for more information.
450
451The following code section shows how you would go about enabling the
452VK_LAYER_LUNARG_standard_validation layer.
453
454```
455 char *instance_validation_layers[] = {
456 "VK_LAYER_LUNARG_standard_validation"
457 };
458 const VkApplicationInfo app = {
459 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
460 .pNext = NULL,
461 .pApplicationName = "TEST_APP",
462 .applicationVersion = 0,
463 .pEngineName = "TEST_ENGINE",
464 .engineVersion = 0,
465 .apiVersion = VK_API_VERSION_1_0,
466 };
467 VkInstanceCreateInfo inst_info = {
468 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
469 .pNext = NULL,
470 .pApplicationInfo = &app,
471 .enabledLayerCount = 1,
472 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
473 .enabledExtensionCount = 0,
474 .ppEnabledExtensionNames = NULL,
475 };
476 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
477```
478
479At `vkCreateInstance` and `vkCreateDevice`, the loader constructs call chains
480that include the application specified (enabled) layers. Order is important in
481the `ppEnabledLayerNames` array; array element 0 is the topmost (closest to the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700482application) layer inserted in the chain and the last array element is closest
Mark Young39389872017-01-19 21:10:49 -0700483to the driver. See the [Overall Layer Ordering](#overall-layer-ordering)
484section for more information on layer ordering.
Jon Ashburnc2972682016-02-08 15:42:01 -0700485
Mark Young39389872017-01-19 21:10:49 -0700486**NOTE:** *Device Layers Are Now Deprecated*
487> `vkCreateDevice` originally was able to select layers in a similar manner to
488`vkCreateInstance`. This lead to the concept of "instance
489> layers" and "device layers". It was decided by Khronos to deprecate the
490> "device layer" functionality and only consider "instance layers".
491> Therefore, `vkCreateDevice` will use the layers specified at
492`vkCreateInstance`.
493> Because of this, the following items have been deprecated:
494> * `VkDeviceCreateInfo` fields:
495> * `ppEnabledLayerNames`
496> * `enabledLayerCount`
497> * The `vkEnumerateDeviceLayerProperties` function
Mark Young02ee5382016-07-22 08:51:05 -0600498
Jon Ashburnc2972682016-02-08 15:42:01 -0700499
Mark Young39389872017-01-19 21:10:49 -0700500##### Implicit vs Explicit Layers
Jon Ashburnc2972682016-02-08 15:42:01 -0700501
Mark Young39389872017-01-19 21:10:49 -0700502Explicit layers are layers which are enabled by an application (e.g. with the
503vkCreateInstance function), or by an environment variable (as mentioned
504previously).
Jon Ashburnc2972682016-02-08 15:42:01 -0700505
Mark Young39389872017-01-19 21:10:49 -0700506Implicit layers are those which are enabled by their existence. For example,
507certain application environments (e.g. Steam or an automotive infotainment
508system) may have layers which they always want enabled for all applications
509that they start. Other implicit layers may be for all applications started on a
510given system (e.g. layers that overlay frames-per-second). Implicit layers are
511enabled automatically, whereas explicit layers must be enabled explicitly.
Jon Ashburnc2972682016-02-08 15:42:01 -0700512
Mark Young02ee5382016-07-22 08:51:05 -0600513Implicit layers have an additional requirement over explicit layers in that they
514require being able to be disabled by an environmental variable. This is due
515to the fact that they are not visible to the application and could cause issues.
516A good principle to keep in mind would be to define both an enable and disable
517environment variable so the users can deterministicly enable the functionality.
518On Desktop platforms (Windows and Linux), these enable/disable settings are
519defined in the layer's JSON file.
520
Mark Young39389872017-01-19 21:10:49 -0700521Discovery of system-installed implicit and explicit layers is described later in
522the [Layer Discovery Section](#layer-discovery). For now, simply know that what
523distinguishes a layer as implicit or explicit is dependent on the Operating
524system, as shown in the table below.
525
526| Operating System | Implicit Layer Identification |
527|----------------|--------------------|
528| Windows | Implicit Layers are located in a different Windows registry location than Explicit Layers. |
529| Linux | Implicit Layers are located in a different directory location than Explicit Layers. |
530| Android | There is **No Support For Implicit Layers** on Android. |
531
532
533##### Forcing Layer Source Folders
534
535Developers may need to use special, pre-production layers, without modifying the
536system-installed layers. You can direct the loader to look for layers in a
537specific folder by defining the "VK\_LAYER\_PATH" environment variable. This
538will override the mechanism used for finding system-installed layers. Because
Mark Young18bbaab2017-03-20 08:27:14 -0600539layers of interest may exist in several disinct folders on a system, this
Mark Young39389872017-01-19 21:10:49 -0700540environment variable can containis several paths seperated by the operating
541specific path separator. On Windows, each separate folder should be separated
542in the list using a semi-colon. On Linux, each folder name should be separated
543using a colon.
544
545If "VK\_LAYER\_PATH" exists, **only** the folders listed in it will be scanned
546for layers. Each directory listed should be the full pathname of a folder
547containing layer manifest files.
548
549
550##### Forcing Layers to be Enabled on Windows and Linux
551
552Developers may want to enable layers that are not enabled by the given
553application they are using. On Linux and Windows, the environment variable
554"VK\_INSTANCE\_LAYERS" can be used to enable additional layers which are
555not specified (enabled) by the application at `vkCreateInstance`.
556"VK\_INSTANCE\_LAYERS" is a colon (Linux)/semi-colon (Windows) separated
557list of layer names to enable. Order is relevant with the first layer in the
558list being the top-most layer (closest to the application) and the last
559layer in the list being the bottom-most layer (closest to the driver).
560See the [Overall Layer Ordering](#overall-layer-ordering) section
561for more information.
562
563Application specified layers and user specified layers (via environment
564variables) are aggregated and duplicates removed by the loader when enabling
565layers. Layers specified via environment variable are top-most (closest to the
566application) while layers specified by the application are bottommost.
567
568An example of using these environment variables to activate the validation
569layer `VK_LAYER_LUNARG_parameter_validation` on Windows or Linux is as follows:
570
571```
572> $ export VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_parameter_validation
573```
574
575
576##### Overall Layer Ordering
577
578The overall ordering of all layers by the loader based on the above looks
579as follows:
580
581![Loader Layer Ordering](./images/loader_layer_order.png)
582
583Ordering may also be important internal to the list of Explicit Layers.
584Some layers may be dependent on other behavior being implemented before
585or after the loader calls it. For example: the VK_LAYER_LUNARG_core_validation
586layer expects the VK_LAYER_LUNARG_parameter_validation to be called first.
587This is because the VK_LAYER_LUNARG_parameter_validation will filter out any
588invalid `NULL` pointer calls prior to the rest of the validation checking
589done by VK_LAYER_LUNARG_core_validation. If not done properly, you may see
590crashes in the VK_LAYER_LUNARG_core_validation layer that would otherwise be
591avoided.
592
593
594#### Application Usage of Extensions
595
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700596Extensions are optional functionality provided by a layer, the loader or an
597ICD. Extensions can modify the behavior of the Vulkan API and need to be
Mark Young39389872017-01-19 21:10:49 -0700598specified and registered with Khronos. These extensions can be created
599by an Independent Hardware Vendor (IHV) to expose new hardware functionality,
600or by a layer writer to expose some internal feature, or by the loader to
601improve functional behavior. Information about various extensions can be
602found in the Vulkan Spec, and vulkan.h header file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700603
Mark Young6d026a72016-06-01 17:49:30 -0600604
Mark Young39389872017-01-19 21:10:49 -0700605##### Instance and Device Extensions
606
607As hinted at in the [Instance Versus Device](#instance-versus-device) section,
608there are really two types of extensions:
609 * Instance Extensions
610 * Device Extensions
611
612An Instance extension is an extension which modifies existing behavior or
613implements new behavior on instance-level objects, like a `VkInstance` or
614a `VkPhysicalDevice`. A Device extension is an extension which does the same,
615but for any `VkDevice` object, or any dispatchable object that is a child of a
616`VkDevice` (`VkQueue` and `VkCommandBuffer` are examples of these).
617
618It is **very** important to know what type of extension you are desiring to
619enable as you will enable Instance extensions during `vkCreateInstance` and
620Device extensions during `vkCreateDevice`.
621
622The loader discovers and aggregates all
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700623extensions from layers (both explicit and implicit), ICDs and the loader before
Mark Young39389872017-01-19 21:10:49 -0700624reporting them to the application in `vkEnumerateXXXExtensionProperties`
625(where XXX is either "Instance" or "Device").
626 - Instance extensions are discovered via
627`vkEnumerateInstanceExtensionProperties`.
628 - Device extensions are be discovered via
629`vkEnumerateDeviceExtensionProperties`.
630
631Looking at `vulkan.h`, you'll notice that they are both similar. For example,
632`vkEnumerateInstanceExtensionProperties` prototype looks as follows:
633
634```
635 VkResult
636 vkEnumerateInstanceExtensionProperties(const char *pLayerName,
637 uint32_t *pPropertyCount,
638 VkExtensionProperties *pProperties);
639```
640
641The "pLayerName" parameter in these functions is used to select either a single
642layer or the Vulkan platform implementation. If "pLayerName" is NULL, extensions
643from Vulkan implementation components (including loader, implicit layers, and
644ICDs) are enumerated. If "pLayerName" is equal to a discovered layer module name
645then only extensions from that layer (which may be implicit or explicit) are
Jeff Julianof1619872016-02-17 17:25:42 -0500646enumerated. Duplicate extensions (e.g. an implicit layer and ICD might report
Mark Young39389872017-01-19 21:10:49 -0700647support for the same extension) are eliminated by the loader. For duplicates,
648the ICD version is reported and the layer version is culled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700649
Mark Young39389872017-01-19 21:10:49 -0700650Also, Extensions *must be enabled* (in `vkCreateInstance` or `vkCreateDevice`)
651before the functions associated with the extensions can be used. If you get an
652Extension function using either `vkGetInstanceProcAddr` or
653`vkGetDeviceProcAddr`, but fail to enable it, you could experience undefined
654behavior. This should actually be flagged if you run with Validation layers
655enabled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700656
Courtney Goeltzenleuchterab3a4662016-02-14 10:48:22 -0700657
Mark Young78f88c82016-07-19 11:49:45 -0600658##### WSI Extensions
659
Mark Young39389872017-01-19 21:10:49 -0700660Khronos approved WSI extensions are available and provide Windows System
661Integration support for various execution environments. It is important to
662understand that some WSI extensions are valid for all targets, but others are
663particular to a given execution environment (and loader). This desktop loader
664(currently targeting Windows and Linux) only enables and directly exports those
665WSI extensions that are appropriate to the current environment. For the most
666part, the selection is done in the loader using compile-time preprocessor flags.
667All versions of the desktop loader currently expose at least the following WSI
Mark Young78f88c82016-07-19 11:49:45 -0600668extension support:
669- VK_KHR_surface
670- VK_KHR_swapchain
671- VK_KHR_display
672
Mark Young39389872017-01-19 21:10:49 -0700673In addition, each of the following OS targets for the loader support target-
674specific extensions:
Mark Young78f88c82016-07-19 11:49:45 -0600675
Mark Young39389872017-01-19 21:10:49 -0700676| Windowing System | Extensions available |
677|----------------|--------------------|
678| Windows | VK_KHR_win32_surface |
679| Linux (Default) | VK_KHR_xcb_surface and VK_KHR_xlib_surface |
680| Linux (Wayland) | VK_KHR_wayland_surface |
681| Linux (Mir) | VK_KHR_mir_surface |
Mark Young78f88c82016-07-19 11:49:45 -0600682
Mark Young39389872017-01-19 21:10:49 -0700683**NOTE:** Wayland and Mir targets are not fully supported at this time. Wayland
684support is present, but should be considered Beta quality. Mir support is not
685completely implemented at this time.
686
687It is important to understand that while the loader may support the various
688entry-points for these extensions, there is a hand-shake required to actually
689use them:
Mark Young78f88c82016-07-19 11:49:45 -0600690* At least one physical device must support the extension(s)
691* The application must select such a physical device
Mark Young39389872017-01-19 21:10:49 -0700692* The application must request the extension(s) be enabled while creating the
693instance or logical device (This depends on whether or not the given extension
694works with an instance or a device).
Mark Young78f88c82016-07-19 11:49:45 -0600695* The instance and/or logical device creation must succeed.
696
697Only then can you expect to properly use a WSI extension in your Vulkan program.
698
Mark Young78f88c82016-07-19 11:49:45 -0600699
Mark Young39389872017-01-19 21:10:49 -0700700##### Unknown Extensions
701
702With the ability to expand Vulkan so easily, extensions will be created that the
703loader knows nothing about. If the extension is a device extension, the loader
704will pass the unknown entry-point down the device call chain ending with the
705appropriate ICD entry-points. The same thing will happen, if the extension is
706an instance extension which takes a physical device paramater as it's first
707component. However, for all other instance extensions the loader will fail to
708load it.
Mark Young78f88c82016-07-19 11:49:45 -0600709
710*But why doesn't the loader support unknown instance extensions?*
711<br/>
712Let's look again at the Instance call chain:
Mark Young78f88c82016-07-19 11:49:45 -0600713
Mark Young39389872017-01-19 21:10:49 -0700714![Instance call chain](./images/loader_instance_chain.png)
Mark Young78f88c82016-07-19 11:49:45 -0600715
Mark Young39389872017-01-19 21:10:49 -0700716Notice that for a normal instance function call, the loader has to handle
717passing along the function call to the available ICDs. If the loader has no
718idea of the parameters or return value of the instance call, it can't properly
719pass information along to the ICDs. There may be ways to do this, which will be
720explored in the future. However, for now, this loader does not support
721instance extensions which don't take a physical device as their first parameter.
722
723Because the device call-chain does not normally pass through the loader
724*terminator*, this is not a problem for device extensions. Additionally,
725since a physical device is associated with one ICD, we can use a generic
726*terminator* pointing to one ICD. This is because both of these extensions
727terminate directly in the ICD they are associated with.
Mark Young78f88c82016-07-19 11:49:45 -0600728
729*Is this a big problem?*
730<br/>
Mark Young39389872017-01-19 21:10:49 -0700731No! Most extension functionality only affects either a physical or logical
732device and not an instance. Thus, the overwhelming majority of extensions
733should be supported with direct loader support.
Jon Ashburnc2972682016-02-08 15:42:01 -0700734
Mark Young6340bb82017-02-13 15:39:22 -0700735##### Filtering Out Unknown Instance Extension Names
736In some cases, an ICD may support instance extensions that the loader does not.
737For the above reasons, the loader will filter out the names of these unknown instance
738extensions when an application calls `vkEnumerateInstanceExtensionProperties`.
739Additionally, this behavior will cause the loader to throw an error during
740`vkCreateInstance` if you still attempt to use one of these extensions. The intent is
741to protect applications so that they don't inadvertantly use functionality
742which could lead to a crash.
743
744On the other-hand, if you know you can safely use the extension, you may disable
745the filtering by defining the environment variable `VK_LOADER_DISABLE_INST_EXT_FILTER`
746and setting the value to a non-zero number. This will effectively disable the
747loader's filtering out of instance extension names.
Jon Ashburnc2972682016-02-08 15:42:01 -0700748
Mark Youngcb6e6702016-07-20 11:38:53 -0600749<br/>
Mark Youngcb6e6702016-07-20 11:38:53 -0600750<br/>
751
Mark Young39389872017-01-19 21:10:49 -0700752## Loader and Layer Interface
Jon Ashburnc2972682016-02-08 15:42:01 -0700753
Mark Young39389872017-01-19 21:10:49 -0700754In this section we'll discuss how the loader interacts with layers, including:
Lenny Komowde3924a2017-05-04 14:50:01 -0600755 * [Layer Discovery](#layer-discovery)
756 * [Layer Manifest File Usage](#layer-manifest-file-usage)
757 * [Android Layer Discovery](#android-layer-discovery)
758 * [Windows Layer Discovery](#windows-layer-discovery)
759 * [Linux Layer Discovery](#linux-layer-discovery)
760 * [Layer Version Negotiation](#layer-version-negotiation)
761 * [Layer Call Chains and Distributed Dispatch](#layer-call-chains-and-distributed-dispatch)
762 * [Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-extensions)
763 * [Layer Intercept Requirements](#layer-intercept-requirements)
764 * [Distributed Dispatching Requirements](#distributed-dispatching-requirements)
765 * [Layer Conventions and Rules](#layer-conventions-and-rules)
766 * [Layer Dispatch Initialization](#layer-dispatch-initialization)
767 * [Example Code for CreateInstance](#example-code-for-createinstance)
768 * [Example Code for CreateDevice](#example-code-for-createdevice)
769 * [Special Considerations](#special-considerations)
770 * [Associating Private Data with Vulkan Objects Within a Layer](#associating-private-data-with-vulkan-objects-within-a-layer)
771 * [Wrapping](#wrapping)
772 * [Hash Maps](#hash-maps)
773 * [Creating New Dispatchable Objects](#creating-new-dispatchable-objects)
774 * [Layer Manifest File Format](#layer-manifest-file-format)
775 * [Layer Manifest File Version History](#layer-manifest-file-version-history)
776 * [Layer Library Versions](#layer-library-versions)
777 * [Layer Library API Version 2](#layer-library-api-version-2)
778 * [Layer Library API Version 1](#layer-library-api-version-1)
779 * [Layer Library API Version 0](#layer-library-api-version-0)
Mark Young39389872017-01-19 21:10:49 -0700780
Jon Ashburnc2972682016-02-08 15:42:01 -0700781
Mark Young39389872017-01-19 21:10:49 -0700782
783#### Layer Discovery
Jon Ashburnc2972682016-02-08 15:42:01 -0700784
Mark Young39389872017-01-19 21:10:49 -0700785As mentioned in the
786[Application Interface section](#implicit-vs-explicit-layers),
787layers can be categorized into two categories:
788 * Implicit Layers
789 * Explicit Layers
Jon Ashburnc2972682016-02-08 15:42:01 -0700790
Mark Young39389872017-01-19 21:10:49 -0700791The main difference between the two is that Implicit Layers are automatically
792enabled, unless overriden, and Explicit Layers must be enabled. Remember,
793Implicit Layers are not present on all Operating Systems (like Android).
Jon Ashburnc2972682016-02-08 15:42:01 -0700794
Mark Young39389872017-01-19 21:10:49 -0700795On any system, the loader looks in specific areas for information on the
796layers that it can load at a user's request. The process of finding the
797available layers on a system is known as Layer Discovery. During discovery,
798the loader determines what layers are available, the layer name, the layer
799version, and any extensions supported by the layer. This information is
800provided back to an application through `vkEnumerateInstanceLayerProperties`.
801
802The group of layers available to the loader is known as a layer library. This
803section defines an extensible interface to discover what layers are contained in
804the layer library.
805
806This section also specifies the minimal conventions and rules a layer must
807follow, especially with regards to interacting with the loader and other layers.
808
809##### Layer Manifest File Usage
810
Mark Young18bbaab2017-03-20 08:27:14 -0600811On Windows and Linux systems, JSON formatted manifest files are used to store
Mark Young39389872017-01-19 21:10:49 -0700812layer information. In order to find system-installed layers, the Vulkan loader
813will read the JSON files to identify the names and attributes of layers and
814their extensions. The use of manifest files allows the loader to avoid loading
815any shared library files when the application does not query nor request any
816extensions. The format of [Layer Manifest File](#layer-manifest-file-format)
817is detailed below.
818
819The Android loader does not use manifest files. Instead, the loader queries the
820layer properties using special functions known as "introspection" functions.
821The intent of these functions is to determine the same required information
822gathered from reading the manifest files. These introspection functions are
823not used by the desktop loader but should be present in layers to maintain
824consistency. The specific "introspection" functions are called out in
825the [Layer Manifest File Format](#layer-manifest-file-format) table.
826
827
828##### Android Layer Discovery
829
830On Android, the loader looks for layers to enumerate in the
831/data/local/debug/vulkan folder. An application enabled for debug has the
832ability to enumerate and enable any layers in that location.
833
834
835##### Windows Layer Discovery
836
837In order to find system-installed layers, the Vulkan loader will scan the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700838values in the following Windows registry keys:
Jon Ashburnc2972682016-02-08 15:42:01 -0700839
Mark Young39389872017-01-19 21:10:49 -0700840```
841 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\ExplicitLayers
Lenny Komowdfa37352017-03-02 11:29:03 -0700842 HKEY_CURRENT_USER\SOFTWARE\Khronos\Vulkan\ExplicitLayers
Mark Young39389872017-01-19 21:10:49 -0700843 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\ImplicitLayers
Lenny Komowdfa37352017-03-02 11:29:03 -0700844 HKEY_CURRENT_USER\SOFTWARE\Khronos\Vulkan\ImplicitLayers
Mark Young39389872017-01-19 21:10:49 -0700845```
Jon Ashburnc2972682016-02-08 15:42:01 -0700846
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700847For each value in these keys which has DWORD data set to 0, the loader opens
848the JSON manifest file specified by the name of the value. Each name must be a
Mark Young39389872017-01-19 21:10:49 -0700849full pathname to the manifest file. The Vulkan loader will open each info file
850to obtain information about the layer, including the name or pathname of a
851shared library (".dll") file. However, if VK\_LAYER\_PATH is defined, then the
852loader will instead look at the paths defined by that variable instead of using
853the information provided by these registry keys. See
854[Forcing Layer Source Folders](#forcing-layer-source-folders) for more
855information on this.
Jon Ashburnc2972682016-02-08 15:42:01 -0700856
Jon Ashburnc2972682016-02-08 15:42:01 -0700857
Mark Young39389872017-01-19 21:10:49 -0700858##### Linux Layer Discovery
Jon Ashburnc2972682016-02-08 15:42:01 -0700859
Mark Young39389872017-01-19 21:10:49 -0700860On Linux, the Vulkan loader will scan the files in the following Linux
861directories:
Jon Ashburnc2972682016-02-08 15:42:01 -0700862
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600863 /usr/local/etc/vulkan/explicit_layer.d
864 /usr/local/etc/vulkan/implicit_layer.d
865 /usr/local/share/vulkan/explicit_layer.d
866 /usr/local/share/vulkan/implicit_layer.d
867 /etc/vulkan/explicit_layer.d
868 /etc/vulkan/implicit_layer.d
869 /usr/share/vulkan/explicit_layer.d
870 /usr/share/vulkan/implicit_layer.d
871 $HOME/.local/share/vulkan/explicit_layer.d
872 $HOME/.local/share/vulkan/implicit_layer.d
873
Mark Young39389872017-01-19 21:10:49 -0700874Of course, ther are some things you have to know about the above folders:
875 1. The "/usr/local/*" directories can be configured to be other directories at
876build time.
877 2. $HOME is the current home directory of the application's user id; this path
878will be ignored for suid programs.
879 3. The "/usr/local/etc/vulkan/\*\_layer.d" and
880"/usr/local/share/vulkan/\*\_layer.d" directories are for layers that are
881installed from locally-built sources.
882 4. The "/usr/share/vulkan/\*\_layer.d" directories are for layers that are
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600883installed from Linux-distribution-provided packages.
884
Mark Young39389872017-01-19 21:10:49 -0700885As on Windows, if VK\_LAYER\_PATH is defined, then the
886loader will instead look at the paths defined by that variable instead of using
887the information provided by these default paths. However, these
888environment variables are only used for non-suid programs. See
889[Forcing Layer Source Folders](#forcing-layer-source-folders) for more
890information on this.
Jon Ashburnc2972682016-02-08 15:42:01 -0700891
Mark Young39389872017-01-19 21:10:49 -0700892
893#### Layer Version Negotiation
894
895Now that a layer has been discovered, an application can choose to load it (or
896it is loaded by default if it is an Implicit layer). When the loader attempts
897to load the layer, the first thing it does is attempt to negotiate the version
898of the loader to layer interface. In order to negotiate the loader/layer
899interface version, the layer must implement the
900`vkNegotiateLoaderLayerInterfaceVersion` function. The following information is
901provided for this interface in include/vulkan/vk_layer.h:
902
903```cpp
904 typedef enum VkNegotiateLayerStructType {
905 LAYER_NEGOTIATE_INTERFACE_STRUCT = 1,
906 } VkNegotiateLayerStructType;
907
908 typedef struct VkNegotiateLayerInterface {
909 VkNegotiateLayerStructType sType;
910 void *pNext;
911 uint32_t loaderLayerInterfaceVersion;
912 PFN_vkGetInstanceProcAddr pfnGetInstanceProcAddr;
913 PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr;
914 PFN_GetPhysicalDeviceProcAddr pfnGetPhysicalDeviceProcAddr;
915 } VkNegotiateLayerInterface;
916
917 VkResult vkNegotiateLoaderLayerInterfaceVersion(
918 VkNegotiateLayerInterface *pVersionStruct);
Jon Ashburncc300a22016-02-11 14:57:30 -0700919```
Jon Ashburnc2972682016-02-08 15:42:01 -0700920
Mark Young39389872017-01-19 21:10:49 -0700921You'll notice the `VkNegotiateLayerInterface` structure is similar to other
922Vulkan structures. The "sType" field, in this case takes a new enum defined
923just for internal loader/layer interfacing use. The valid values for "sType"
924could grow in the future, but right only havs the one value
925"LAYER_NEGOTIATE_INTERFACE_STRUCT".
Jon Ashburnc2972682016-02-08 15:42:01 -0700926
Mark Young39389872017-01-19 21:10:49 -0700927This function (`vkNegotiateLoaderLayerInterfaceVersion`) should be exported by
928the layer so that using "GetProcAddress" on Windows or "dlsym" on Linux, should
929return a valid function pointer to it. Once the loader has grabbed a valid
930address to the layers function, the loader will create a variable of type
931`VkNegotiateLayerInterface` and initialize it in the following ways:
932 1. Set the structure "sType" to "LAYER_NEGOTIATE_INTERFACE_STRUCT"
933 2. Set pNext to NULL.
934 - This is for future growth
935 3. Set "loaderLayerInterfaceVersion" to the current version the loader desires
936to set the interface to.
937 - The minimum value sent by the loader will be 2 since it is the first
938version supporting this function.
Jon Ashburnc2972682016-02-08 15:42:01 -0700939
Mark Young39389872017-01-19 21:10:49 -0700940The loader will then individually call each layer’s
941`vkNegotiateLoaderLayerInterfaceVersion` function with the filled out
942“VkNegotiateLayerInterface”. The layer will either accept the loader's version
943set in "loaderLayerInterfaceVersion", or modify it to the closest value version
944of the interface that the layer can support. The value should not be higher
945than the version requested by the loader. If the layer can't support at a
946minimum the version requested, then the layer should return an error like
947"VK_ERROR_INITIALIZATION_FAILED". If a layer can support some version, then
948the layer should do the following:
949 1. Adjust the version to the layer's desired version.
950 2. The layer should fill in the function pointer values to its internal
951functions:
952 - "pfnGetInstanceProcAddr" should be set to the layer’s internal
953`GetInstanceProcAddr` function.
954 - "pfnGetDeviceProcAddr" should be set to the layer’s internal
955`GetDeviceProcAddr` function.
956 - "pfnGetPhysicalDeviceProcAddr" should be set to the layer’s internal
957`GetPhysicalDeviceProcAddr` function.
958 - If the layer supports no physical device extensions, it may set the
959value to NULL.
960 - More on this function later
961 3. The layer should return "VK_SUCCESS"
Jon Ashburnc2972682016-02-08 15:42:01 -0700962
Mark Young39389872017-01-19 21:10:49 -0700963This function **SHOULD NOT CALL DOWN** the layer chain to the next layer.
964The loader will work with each layer individually.
Jon Ashburnc2972682016-02-08 15:42:01 -0700965
Mark Young39389872017-01-19 21:10:49 -0700966If the layer supports the new interface and reports version 2 or greater, then
967the loader will use the “fpGetInstanceProcAddr” and “fpGetDeviceProcAddr”
968functions from the “VkNegotiateLayerInterface” structure. Prior to these
969changes, the loader would query each of those functions using "GetProcAddress"
970on Windows or "dlsym" on Linux.
Jon Ashburnc2972682016-02-08 15:42:01 -0700971
Jon Ashburnc2972682016-02-08 15:42:01 -0700972
Mark Young39389872017-01-19 21:10:49 -0700973#### Layer Call Chains and Distributed Dispatch
Jon Ashburnc2972682016-02-08 15:42:01 -0700974
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700975There are two key architectural features that drive the loader to layer library
Mark Young39389872017-01-19 21:10:49 -0700976interface:
977 1. Separate and distinct instance and device call chains
978 2. Distributed dispatch.
Jon Ashburnc2972682016-02-08 15:42:01 -0700979
Mark Young39389872017-01-19 21:10:49 -0700980You can read an overview of dispatch tables and call chains above in the
981[Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) section.
Jon Ashburnc2972682016-02-08 15:42:01 -0700982
Mark Young39389872017-01-19 21:10:49 -0700983What's important to note here is that a layer can intercept Vulkan
984instance functions, device functions or both. For a layer to intercept instance
985functions, it must participate in the instance call chain. For a layer to
986intercept device functions, it must participate in the device call chain.
987
988Remember, a layer does not need to intercept all instance or device functions,
989instead, it can choose to intercept only a subset of those functions.
990
991Normally, when a layer intercepts a given Vulkan function, it will call down the
992instance or device call chain as needed. The loader and all layer libraries that
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700993participate in a call chain cooperate to ensure the correct sequencing of calls
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700994from one entity to the next. This group effort for call chain sequencing is
Mark Young39389872017-01-19 21:10:49 -0700995hereinafter referred to as **distributed dispatch**.
Jon Ashburnc2972682016-02-08 15:42:01 -0700996
Mark Young39389872017-01-19 21:10:49 -0700997In distributed dispatch each layer is responsible for properly calling the next
998entity in the call chain. This means that a dispatch mechanism is required for
999all Vulkan functions that a layer intercepts. If a Vulkan function is not
1000intercepted by a layer, or if a layer chooses to terminate the function by not
1001calling down the chain, then no dispatch is needed for that particular function.
Jeff Julianof1619872016-02-17 17:25:42 -05001002
Mark Young39389872017-01-19 21:10:49 -07001003For example, if the enabled layers intercepted only certain instance functions,
1004the call chain would look as follows:
1005![Instance Function Chain](./images/function_instance_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -07001006
Mark Young39389872017-01-19 21:10:49 -07001007Likewise, if the enabled layers intercepted only a few of the device functions,
1008the call chain could look this way:
1009![Device Function Chain](./images/function_device_chain.png)
Jeff Julianof1619872016-02-17 17:25:42 -05001010
Mark Young39389872017-01-19 21:10:49 -07001011The loader is responsible for dispatching all core and instance extension Vulkan
1012functions to the first entity in the call chain.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001013
Chia-I Wucb24fec2016-04-20 06:23:24 +08001014
Mark Young39389872017-01-19 21:10:49 -07001015#### Layer Unknown Physical Device Extensions
1016
1017Originally, if the loader was called with `vkGetInstanceProcAddr`, it would
1018result in the following behavior:
1019 1. The loader would check if core function:
1020 - If it was, it would return the function pointer
1021 2. The loader would check if known extension function:
1022 - If it was, it would return the function pointer
1023 3. If the loader knew nothing about it, it would call down using
1024`GetInstanceProcAddr`
1025 - If it returned non-NULL, treat it as an unknown logical device command.
1026 - This meant setting up a generic trampoline function that takes in a
1027VkDevice as the first parameter and adjusting the dispatch table to call the
1028ICD/Layers function after getting the dispatch table from the VkDevice.
1029 4. If all the above failed, the loader would return NULL to the application.
1030
1031This caused problems when a layer attempted to expose new physical device
1032extensions the loader knew nothing about, but an application did. Because the
1033loader knew nothing about it, the loader would get to step 3 in the above
1034process and would treat the function as an unknown logical device command. The
1035problem is, this would create a generic VkDevice trampoline function which, on
1036the first call, would attempt to dereference the VkPhysicalDevice as a VkDevice.
1037This would lead to a crash or corruption.
1038
1039In order to identify the extension entry-points specific to physical device
1040extensions, the following function can be added to a layer:
1041
1042```cpp
1043PFN_vkVoidFunction vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1044 const char* pName);
1045```
1046
1047This function behaves similar to `vkGetInstanceProcAddr` and
1048`vkGetDeviceProcAddr` except it should only return values for physical device
1049extension entry-points. In this way, it compares "pName" to every physical
1050device function supported in the layer.
1051
1052The following rules apply:
Lenny Komowde3924a2017-05-04 14:50:01 -06001053 * If it is the name of a physical device function supported by the layer, the
Mark Young39389872017-01-19 21:10:49 -07001054pointer to the layer's corresponding function should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06001055 * If it is the name of a valid function which is **not** a physical device
Mark Young39389872017-01-19 21:10:49 -07001056function (i.e. an Instance, Device, or other function implemented by the layer),
1057then the value of NULL should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06001058 * We don’t call down since we know the command is not a physical device
Mark Young39389872017-01-19 21:10:49 -07001059extension).
Lenny Komowde3924a2017-05-04 14:50:01 -06001060 * If the layer has no idea what this function is, it should call down the layer
Mark Young39389872017-01-19 21:10:49 -07001061chain to the next `vk_layerGetPhysicalDeviceProcAddr` call.
Lenny Komowde3924a2017-05-04 14:50:01 -06001062 * This can be retrieved in one of two ways:
1063 * During `vkCreateInstance`, it is passed to a layer in the
Mark Young39389872017-01-19 21:10:49 -07001064chain information passed to a layer in the `VkLayerInstanceCreateInfo`
1065structure.
1066 * Use `get_chain_info()` to get the pointer to the
1067`VkLayerInstanceCreateInfo` structure. Let's call it chain_info.
1068 * The address is then under
1069chain_info->u.pLayerInfo->pfnNextGetPhysicalDeviceProcAddr
1070 * See
1071[Example Code for CreateInstance](#example-code-for-createinstance)
Lenny Komowde3924a2017-05-04 14:50:01 -06001072 * Using the next layer’s `GetInstanceProcAddr` function to query for
Mark Young39389872017-01-19 21:10:49 -07001073`vk_layerGetPhysicalDeviceProcAddr`.
1074
1075This support is optional and should not be considered a requirement. This is
1076only required if a layer intends to support some functionality not directly
Mark Young18bbaab2017-03-20 08:27:14 -06001077supported by loaders released in the public. If a layer does implement this
Mark Young39389872017-01-19 21:10:49 -07001078support, it should return the address of its `vk_layerGetPhysicalDeviceProcAddr`
1079function in the "pfnGetPhysicalDeviceProcAddr" member of the
1080`VkNegotiateLayerInterface` structure during
1081[Layer Version Negotiation](#layer-version-negotiation). Additionally, the
1082layer should also make sure `vkGetInstanceProcAddr` returns a valid function
1083pointer to a query of `vk_layerGetPhysicalDeviceProcAddr`.
1084
1085The new behavior of the loader's `vkGetInstanceProcAddr` with support for the
1086`vk_layerGetPhysicalDeviceProcAddr` function is as follows:
1087 1. Check if core function:
1088 - If it is, return the function pointer
1089 2. Check if known instance or device extension function:
1090 - If it is, return the function pointer
1091 3. Call the layer/ICD `GetPhysicalDeviceProcAddr`
1092 - If it returns non-NULL, return a trampoline to a generic physical device
1093function, and setup a generic terminator which will pass it to the proper ICD.
1094 4. Call down using `GetInstanceProcAddr`
1095 - If it returns non-NULL, treat it as an unknown logical device command.
1096This means setting up a generic trampoline function that takes in a VkDevice as
1097the first parameter and adjusting the dispatch table to call the ICD/Layers
1098function after getting the dispatch table from the VkDevice. Then, return the
1099pointer to corresponding trampoline function.
1100 5. Return NULL
1101
1102You can see now, that, if the command gets promoted to core later, it will no
1103longer be setup using `vk_layerGetPhysicalDeviceProcAddr`. Additionally, if the
1104loader adds direct support for the extension, it will no longer get to step 3,
1105because step 2 will return a valid function pointer. However, the layer should
1106continue to support the command query via `vk_layerGetPhysicalDeviceProcAddr`,
1107until at least a Vulkan version bump, because an older loader may still be
1108attempting to use the commands.
1109
1110
1111#### Layer Intercept Requirements
1112
Lenny Komowde3924a2017-05-04 14:50:01 -06001113 * Layers intercept a Vulkan function by defining a C/C++ function with
Mark Young39389872017-01-19 21:10:49 -07001114signature **identical** to the Vulkan API for that function.
Lenny Komowde3924a2017-05-04 14:50:01 -06001115 * A layer **must intercept at least** `vkGetInstanceProcAddr` and
Mark Young39389872017-01-19 21:10:49 -07001116`vkCreateInstance` to participate in the instance call chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001117 * A layer **may also intercept** `vkGetDeviceProcAddr` and `vkCreateDevice`
Mark Young39389872017-01-19 21:10:49 -07001118to participate in the device call chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001119 * For any Vulkan function a layer intercepts which has a non-void return value,
Mark Young39389872017-01-19 21:10:49 -07001120**an appropriate value must be returned** by the layer intercept function.
Lenny Komowde3924a2017-05-04 14:50:01 -06001121 * Most functions a layer intercepts **should call down the chain** to the
Mark Young39389872017-01-19 21:10:49 -07001122corresponding Vulkan function in the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001123 * The common behavior for a layer is to intercept a call, perform some
Mark Young39389872017-01-19 21:10:49 -07001124behavior, then pass it down to the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001125 * If you don't pass the information down, undefined behavior may occur.
1126 * This is because the function will not be received by layers further down
Mark Young39389872017-01-19 21:10:49 -07001127the chain, or any ICDs.
Lenny Komowde3924a2017-05-04 14:50:01 -06001128 * One function that **must never call down the chain** is:
1129 * `vkNegotiateLoaderLayerInterfaceVersion`
1130 * Three common functions that **may not call down the chain** are:
1131 * `vkGetInstanceProcAddr`
1132 * `vkGetDeviceProcAddr`
1133 * `vk_layerGetPhysicalDeviceProcAddr`
1134 * These functions only call down the chain for Vulkan functions that they
Mark Young39389872017-01-19 21:10:49 -07001135do not intercept.
Lenny Komowde3924a2017-05-04 14:50:01 -06001136 * Layer intercept functions **may insert extra calls** to Vulkan functions in
Mark Young39389872017-01-19 21:10:49 -07001137addition to the intercept.
Lenny Komowde3924a2017-05-04 14:50:01 -06001138 * For example, a layer intercepting `vkQueueSubmit` may want to add a call to
Mark Young39389872017-01-19 21:10:49 -07001139`vkQueueWaitIdle` after calling down the chain for `vkQueueSubmit`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001140 * This would result in two calls down the chain: First a call down the
Mark Young39389872017-01-19 21:10:49 -07001141`vkQueueSubmit` chain, followed by a call down the `vkQueueWaitIdle` chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001142 * Any additional calls inserted by a layer must be on the same chain
1143 * If the function is a device function, only other device functions should
Mark Young39389872017-01-19 21:10:49 -07001144be added.
Lenny Komowde3924a2017-05-04 14:50:01 -06001145 * Likewise, if the function is an instance function, only other instance
Mark Young39389872017-01-19 21:10:49 -07001146functions should be added.
1147
1148
1149#### Distributed Dispatching Requirements
1150
1151- For each entry-point a layer intercepts, it must keep track of the entry
1152point residing in the next entity in the chain it will call down into.
Lenny Komowde3924a2017-05-04 14:50:01 -06001153 * In other words, the layer must have a list of pointers to functions of the
Mark Young39389872017-01-19 21:10:49 -07001154appropriate type to call into the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001155 * This can be implemented in various ways but
Mark Young39389872017-01-19 21:10:49 -07001156for clarity, will be referred to as a dispatch table.
1157- A layer can use the `VkLayerDispatchTable` structure as a device dispatch
1158table (see include/vulkan/vk_layer.h).
1159- A layer can use the `VkLayerInstanceDispatchTable` structure as a instance
1160dispatch table (see include/vulkan/vk_layer.h).
1161- A Layer's `vkGetInstanceProcAddr` function uses the next entity's
1162`vkGetInstanceProcAddr` to call down the chain for unknown (i.e.
1163non-intercepted) functions.
1164- A Layer's `vkGetDeviceProcAddr` function uses the next entity's
1165`vkGetDeviceProcAddr` to call down the chain for unknown (i.e. non-intercepted)
1166functions.
1167- A Layer's `vk_layerGetPhysicalDeviceProcAddr` function uses the next entity's
1168`vk_layerGetPhysicalDeviceProcAddr` to call down the chain for unknown (i.e.
1169non-intercepted) functions.
1170
1171
1172#### Layer Conventions and Rules
Chia-I Wucb24fec2016-04-20 06:23:24 +08001173
1174A layer, when inserted into an otherwise compliant Vulkan implementation, must
Mark Young39389872017-01-19 21:10:49 -07001175still result in a compliant Vulkan implementation. The intention is for layers
1176to have a well-defined baseline behavior. Therefore, it must follow some
1177conventions and rules defined below.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001178
1179A layer is always chained with other layers. It must not make invalid calls
Mark Young39389872017-01-19 21:10:49 -07001180to, or rely on undefined behaviors of, its lower layers. When it changes the
1181behavior of a function, it must make sure its upper layers do not make invalid
Chia-I Wucb24fec2016-04-20 06:23:24 +08001182calls to or rely on undefined behaviors of its lower layers because of the
1183changed behavior. For example, when a layer intercepts an object creation
Mark Young39389872017-01-19 21:10:49 -07001184function to wrap the objects created by its lower layers, it must make sure its
Chia-I Wucb24fec2016-04-20 06:23:24 +08001185lower layers never see the wrapping objects, directly from itself or
1186indirectly from its upper layers.
1187
Chia-I Wub5e850e2016-05-06 08:41:52 +08001188When a layer requires host memory, it may ignore the provided allocators. It
1189should use memory allocators if the layer is intended to run in a production
Mark Young39389872017-01-19 21:10:49 -07001190environment. For example, this usually applies to implicit layers that are
1191always enabled. That will allow applications to include the layer's memory
1192usage.
Chia-I Wub5e850e2016-05-06 08:41:52 +08001193
Mark Young39389872017-01-19 21:10:49 -07001194Additional rules include:
Lenny Komowde3924a2017-05-04 14:50:01 -06001195 - `vkEnumerateInstanceLayerProperties` **must** enumerate and **only**
Mark Young39389872017-01-19 21:10:49 -07001196enumerate the layer itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001197 - `vkEnumerateInstanceExtensionProperties` **must** handle the case where
Mark Young39389872017-01-19 21:10:49 -07001198`pLayerName` is itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001199 - It **must** return `VK_ERROR_LAYER_NOT_PRESENT` otherwise, including when
Mark Young39389872017-01-19 21:10:49 -07001200`pLayerName` is `NULL`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001201 - `vkEnumerateDeviceLayerProperties` **is deprecated and may be omitted**.
1202 - Using this will result in undefined behavior.
1203 - `vkEnumerateDeviceExtensionProperties` **must** handle the case where
Mark Young39389872017-01-19 21:10:49 -07001204`pLayerName` is itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001205 - In other cases, it should normally chain to other layers.
1206 - `vkCreateInstance` **must not** generate an error for unrecognized layer
Mark Young39389872017-01-19 21:10:49 -07001207names and extension names.
Lenny Komowde3924a2017-05-04 14:50:01 -06001208 - It may assume the layer names and extension names have been validated.
1209 - `vkGetInstanceProcAddr` intercepts a Vulkan function by returning a local
Mark Young39389872017-01-19 21:10:49 -07001210entry-point
Lenny Komowde3924a2017-05-04 14:50:01 -06001211 - Otherwise it returns the value obtained by calling down the instance call
Mark Young39389872017-01-19 21:10:49 -07001212chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001213 - `vkGetDeviceProcAddr` 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 device call
Mark Young39389872017-01-19 21:10:49 -07001216chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001217 - These additional functions must be intercepted if the layer implements
Mark Young39389872017-01-19 21:10:49 -07001218device-level call chaining:
Lenny Komowde3924a2017-05-04 14:50:01 -06001219 - `vkGetDeviceProcAddr`
1220 - `vkCreateDevice`(only required for any device-level chaining)
1221 - **NOTE:** older layer libraries may expect that `vkGetInstanceProcAddr`
Mark Young39389872017-01-19 21:10:49 -07001222ignore `instance` when `pName` is `vkCreateDevice`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001223 - The specification **requires** `NULL` to be returned from
Mark Young39389872017-01-19 21:10:49 -07001224`vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` for disabled functions.
Lenny Komowde3924a2017-05-04 14:50:01 -06001225 - A layer may return `NULL` itself or rely on the following layers to do so.
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001226
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001227
Mark Young39389872017-01-19 21:10:49 -07001228#### Layer Dispatch Initialization
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001229
Mark Young39389872017-01-19 21:10:49 -07001230- A layer initializes its instance dispatch table within its `vkCreateInstance`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001231function.
Mark Young39389872017-01-19 21:10:49 -07001232- A layer initializes its device dispatch table within its `vkCreateDevice`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001233function.
1234- The loader passes a linked list of initialization structures to layers via
Mark Young39389872017-01-19 21:10:49 -07001235the "pNext" field in the VkInstanceCreateInfo and `VkDeviceCreateInfo`
1236structures for `vkCreateInstance` and `VkCreateDevice` respectively.
1237- The head node in this linked list is of type `VkLayerInstanceCreateInfo` for
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001238instance and VkLayerDeviceCreateInfo for device. See file
Mark Young39389872017-01-19 21:10:49 -07001239`include/vulkan/vk_layer.h` for details.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001240- A VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO is used by the loader for the
1241"sType" field in VkLayerInstanceCreateInfo.
1242- A VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO is used by the loader for the
1243"sType" field in VkLayerDeviceCreateInfo.
1244- The "function" field indicates how the union field "u" should be interpreted
Mark Young39389872017-01-19 21:10:49 -07001245within `VkLayer*CreateInfo`. The loader will set the "function" field to
1246VK_LAYER_LINK_INFO. This indicates "u" field should be `VkLayerInstanceLink` or
1247`VkLayerDeviceLink`.
1248- The `VkLayerInstanceLink` and `VkLayerDeviceLink` structures are the list
1249nodes.
1250- The `VkLayerInstanceLink` contains the next entity's `vkGetInstanceProcAddr`
1251used by a layer.
1252- The `VkLayerDeviceLink` contains the next entity's `vkGetInstanceProcAddr` and
1253`vkGetDeviceProcAddr` used by a layer.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001254- Given the above structures set up by the loader, layer must initialize their
1255dispatch table as follows:
Mark Young39389872017-01-19 21:10:49 -07001256 - Find the `VkLayerInstanceCreateInfo`/`VkLayerDeviceCreateInfo` structure in
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001257the VkInstanceCreateInfo/VkDeviceCreateInfo structure.
Jon Ashburncc300a22016-02-11 14:57:30 -07001258 - Get the next entity's vkGet*ProcAddr from the "pLayerInfo" field.
Mark Young39389872017-01-19 21:10:49 -07001259 - For CreateInstance get the next entity's `vkCreateInstance` by calling the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001260"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001261 pfnNextGetInstanceProcAddr(NULL, "vkCreateInstance").
Mark Young39389872017-01-19 21:10:49 -07001262 - For CreateDevice get the next entity's `vkCreateDevice` by calling the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001263"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001264 pfnNextGetInstanceProcAddr(NULL, "vkCreateDevice").
Jon Ashburncc300a22016-02-11 14:57:30 -07001265 - Advanced the linked list to the next node: pLayerInfo = pLayerInfo->pNext.
Mark Young39389872017-01-19 21:10:49 -07001266 - Call down the chain either `vkCreateDevice` or `vkCreateInstance`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001267 - Initialize your layer dispatch table by calling the next entity's
Mark Young39389872017-01-19 21:10:49 -07001268Get*ProcAddr function once for each Vulkan function needed in your dispatch
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001269table
Jon Ashburncc300a22016-02-11 14:57:30 -07001270
Mark Young39389872017-01-19 21:10:49 -07001271#### Example Code for CreateInstance
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001272
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001273```cpp
1274VkResult vkCreateInstance(
1275 const VkInstanceCreateInfo *pCreateInfo,
1276 const VkAllocationCallbacks *pAllocator,
1277 VkInstance *pInstance)
1278{
1279 VkLayerInstanceCreateInfo *chain_info =
1280 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1281
1282 assert(chain_info->u.pLayerInfo);
1283 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1284 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1285 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001286 (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001287 if (fpCreateInstance == NULL) {
1288 return VK_ERROR_INITIALIZATION_FAILED;
1289 }
1290
1291 // Advance the link info for the next element of the chain
1292 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1293
1294 // Continue call down the chain
1295 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
1296 if (result != VK_SUCCESS)
1297 return result;
1298
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001299 // Init layer's dispatch table using GetInstanceProcAddr of
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001300 // next layer in the chain.
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001301 instance_dispatch_table = new VkLayerInstanceDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001302 layer_init_instance_dispatch_table(
1303 *pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
1304
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001305 // Other layer initialization
1306 ...
1307
1308 return VK_SUCCESS;
1309}
1310```
1311
Mark Young39389872017-01-19 21:10:49 -07001312#### Example Code for CreateDevice
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001313
1314```cpp
1315VkResult
1316vkCreateDevice(
1317 VkPhysicalDevice gpu,
1318 const VkDeviceCreateInfo *pCreateInfo,
1319 const VkAllocationCallbacks *pAllocator,
1320 VkDevice *pDevice)
1321{
1322 VkLayerDeviceCreateInfo *chain_info =
1323 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1324
1325 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1326 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1327 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr =
1328 chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1329 PFN_vkCreateDevice fpCreateDevice =
1330 (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
1331 if (fpCreateDevice == NULL) {
1332 return VK_ERROR_INITIALIZATION_FAILED;
1333 }
1334
1335 // Advance the link info for the next element on the chain
1336 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1337
1338 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
1339 if (result != VK_SUCCESS) {
1340 return result;
1341 }
1342
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001343 // initialize layer's dispatch table
1344 device_dispatch_table = new VkLayerDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001345 layer_init_device_dispatch_table(
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001346 *pDevice, device_dispatch_table, fpGetDeviceProcAddr);
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001347
1348 // Other layer initialization
1349 ...
1350
1351 return VK_SUCCESS;
1352}
1353```
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001354
Mark Young39389872017-01-19 21:10:49 -07001355
Jon Ashburncc300a22016-02-11 14:57:30 -07001356#### Special Considerations
Mark Young39389872017-01-19 21:10:49 -07001357
1358
1359##### Associating Private Data with Vulkan Objects Within a Layer
1360
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001361A layer may want to associate it's own private data with one or more Vulkan
Mark Youngf7914cf2016-08-31 11:53:26 -06001362objects. Two common methods to do this are hash maps and object wrapping.
1363
Mark Youngf7914cf2016-08-31 11:53:26 -06001364
Mark Young39389872017-01-19 21:10:49 -07001365###### Wrapping
Ian Elliott0b082e42016-08-31 14:08:44 -06001366
Mark Young39389872017-01-19 21:10:49 -07001367The loader supports layers wrapping any Vulkan object, including dispatchable
1368objects. For functions that return object handles, each layer does not touch
1369the value passed down the call chain. This is because lower items may need to
1370use the original value. However, when the value is returned from a
1371lower-level layer (possibly the ICD), the layer saves the handle and returns
1372its own handle to the layer above it (possibly the application). When a layer
1373receives a Vulkan function using something that it previously returned a handle
1374for, the layer is required to unwrap the handle and pass along the saved handle
1375to the layer below it. This means that the layer **must intercept every Vulkan
1376function which uses the object in question**, and wrap or unwrap the object, as
1377appropriate. This includes adding support for all extensions with functions
Ian Elliott0b082e42016-08-31 14:08:44 -06001378using any object the layer wraps.
Mark Youngf7914cf2016-08-31 11:53:26 -06001379
1380Layers above the object wrapping layer will see the wrapped object. Layers
1381which wrap dispatchable objects must ensure that the first field in the wrapping
Mark Young39389872017-01-19 21:10:49 -07001382structure is a pointer to a dispatch table as defined in `vk_layer.h`.
1383Specifically, an instance wrapped dispatchable object could be as follows:
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001384```
1385struct my_wrapped_instance_obj_ {
1386 VkLayerInstanceDispatchTable *disp;
1387 // whatever data layer wants to add to this object
1388};
1389```
1390A device wrapped dispatchable object could be as follows:
1391```
1392struct my_wrapped_instance_obj_ {
1393 VkLayerDispatchTable *disp;
1394 // whatever data layer wants to add to this object
1395};
1396```
Jeff Julianof1619872016-02-17 17:25:42 -05001397
Ian Elliott0b082e42016-08-31 14:08:44 -06001398Layers that wrap dispatchable objects must follow the guidelines for creating
1399new dispatchable objects (below).
1400
Mark Young39389872017-01-19 21:10:49 -07001401<u>Cautions About Wrapping</u>
Ian Elliott0b082e42016-08-31 14:08:44 -06001402
1403Layers are generally discouraged from wrapping objects, because of the
1404potential for incompatibilities with new extensions. For example, let's say
Mark Young39389872017-01-19 21:10:49 -07001405that a layer wraps `VkImage` objects, and properly wraps and unwraps `VkImage`
1406object handles for all core functions. If a new extension is created which has
1407functions that take `VkImage` objects as parameters, and if the layer does not
1408support those new functions, an application that uses both the layer and the new
1409extension will have undefined behavior when those new functions are called (e.g.
Mark Young18bbaab2017-03-20 08:27:14 -06001410the application may crash). This is because the lower-level layers and ICD
Ian Elliott0b082e42016-08-31 14:08:44 -06001411won't receive the handle that they generated. Instead, they will receive a
1412handle that is only known by the layer that is wrapping the object.
1413
1414Because of the potential for incompatibilities with unsupported extensions,
1415layers that wrap objects must check which extensions are being used by the
1416application, and take appropriate action if the layer is used with unsupported
1417extensions (e.g. disable layer functionality, stop wrapping objects, issue a
1418message to the user).
1419
1420The reason that the validation layers wrap objects, is to track the proper use
1421and destruction of each object. They issue a validation error if used with
1422unsupported extensions, alerting the user to the potential for undefined
1423behavior.
1424
Mark Young39389872017-01-19 21:10:49 -07001425
1426###### Hash Maps
1427
Jeff Julianof1619872016-02-17 17:25:42 -05001428Alternatively, a layer may want to use a hash map to associate data with a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001429given object. The key to the map could be the object. Alternatively, for
1430dispatchable objects at a given level (eg device or instance) the layer may
Mark Young39389872017-01-19 21:10:49 -07001431want data associated with the `VkDevice` or `VkInstance` objects. Since
1432there are multiple dispatchable objects for a given `VkInstance` or `VkDevice`,
1433the `VkDevice` or `VkInstance` object is not a great map key. Instead the layer
1434should use the dispatch table pointer within the `VkDevice` or `VkInstance`
1435since that will be unique for a given `VkInstance` or `VkDevice`.
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001436
Mark Young39389872017-01-19 21:10:49 -07001437
1438##### Creating New Dispatchable Objects
1439
1440Layers which create dispatchable objects must take special care. Remember that
1441loader *trampoline* code normally fills in the dispatch table pointer in the
1442newly created object. Thus, the layer must fill in the dispatch table pointer if
1443the loader *trampoline* will not do so. Common cases where a layer (or ICD) may
1444create a dispatchable object without loader *trampoline* code is as follows:
1445- layers that wrap dispatchable objects
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001446- layers which add extensions that create dispatchable objects
Mark Young18bbaab2017-03-20 08:27:14 -06001447- layers which insert extra Vulkan functions in the stream of functions they
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001448intercept from the application
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001449- ICDs which add extensions that create dispatchable objects
1450
Mark Young39389872017-01-19 21:10:49 -07001451The desktop loader provides a callback that can be used for initializing
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001452a dispatchable object. The callback is passed as an extension structure via the
Mark Young39389872017-01-19 21:10:49 -07001453pNext field in the create info structure when creating an instance
1454(`VkInstanceCreateInfo`) or device (`VkDeviceCreateInfo`). The callback
1455prototype is defined as follows for instance and device callbacks respectively
1456(see `vk_layer.h`):
1457
1458```cpp
1459VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceLoaderData(VkInstance instance,
1460 void *object);
1461VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceLoaderData(VkDevice device,
1462 void *object);
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001463```
Mark Young39389872017-01-19 21:10:49 -07001464
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001465To obtain these callbacks the layer must search through the list of structures
Mark Young39389872017-01-19 21:10:49 -07001466pointed to by the "pNext" field in the `VkInstanceCreateInfo` and
1467`VkDeviceCreateInfo` parameters to find any callback structures inserted by the
1468loader. The salient details are as follows:
1469- For `VkInstanceCreateInfo` the callback structure pointed to by "pNext" is
1470`VkLayerInstanceCreateInfo` as defined in `include/vulkan/vk_layer.h`.
1471- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO within
1472`VkInstanceCreateInfo` parameter indicates a loader structure.
1473- Within `VkLayerInstanceCreateInfo`, the "function" field indicates how the
1474union field "u" should be interpreted.
1475- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will
1476contain the callback in "pfnSetInstanceLoaderData".
1477- For `VkDeviceCreateInfo` the callback structure pointed to by "pNext" is
1478`VkLayerDeviceCreateInfo` as defined in `include/vulkan/vk_layer.h`.
1479- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO within
1480`VkDeviceCreateInfo` parameter indicates a loader structure.
1481- Within `VkLayerDeviceCreateInfo`, the "function" field indicates how the union
1482field "u" should be interpreted.
1483- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will
1484contain the callback in "pfnSetDeviceLoaderData".
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001485
Mark Young39389872017-01-19 21:10:49 -07001486Alternatively, if an older loader is being used that doesn't provide these
1487callbacks, the layer may manually initialize the newly created dispatchable
1488object. To fill in the dispatch table pointer in newly created dispatchable
1489object, the layer should copy the dispatch pointer, which is always the first
1490entry in the structure, from an existing parent object of the same level
1491(instance versus device).
Jon Ashburnc2972682016-02-08 15:42:01 -07001492
Mark Young39389872017-01-19 21:10:49 -07001493For example, if there is a newly created `VkCommandBuffer` object, then the
1494dispatch pointer from the `VkDevice` object, which is the parent of the
1495`VkCommandBuffer` object, should be copied into the newly created object.
1496
1497
1498#### Layer Manifest File Format
1499
1500On Windows and Linux (desktop), the loader uses manifest files to discover
1501layer libraries and layers. The desktop loader doesn't directly query the
1502layer library except during chaining. This is to reduce the likelihood of
1503loading a malicious layer into memory. Instead, details are read from the
1504Manifest file, which are then provided for applications to determine what
1505layers should actually be loaded.
1506
1507The following section discusses the details of the Layer Manifest JSON file
1508format. The JSON file itself does not have any requirements for naming. The
1509only requirement is that the extension suffix of the file ends with ".json".
1510
1511Here is an example layer JSON Manifest file with a single layer:
1512
1513```
1514{
1515 "file_format_version" : "1.0.0",
1516 "layer": {
1517 "name": "VK_LAYER_LUNARG_overlay",
1518 "type": "INSTANCE",
1519 "library_path": "vkOverlayLayer.dll"
1520 "api_version" : "1.0.5",
1521 "implementation_version" : "2",
1522 "description" : "LunarG HUD layer",
1523 "functions": {
1524 "vkNegotiateLoaderLayerInterfaceVersion":
1525 "OverlayLayer_NegotiateLoaderLayerInterfaceVersion"
1526 },
1527 "instance_extensions": [
1528 {
1529 "name": "VK_EXT_debug_report",
1530 "spec_version": "1"
1531 },
1532 {
1533 "name": "VK_VENDOR_ext_x",
1534 "spec_version": "3"
1535 }
1536 ],
1537 "device_extensions": [
1538 {
1539 "name": "VK_EXT_debug_marker",
1540 "spec_version": "1",
1541 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1542 }
1543 ],
1544 "enable_environment": {
1545 "ENABLE_LAYER_OVERLAY_1": "1"
1546 }
1547 "disable_environment": {
1548 "DISABLE_LAYER_OVERLAY_1": ""
1549 }
1550 }
1551}
1552```
1553
1554Here's a snippet with the changes required to support multiple layers per
1555manifest file:
1556```
1557{
1558 "file_format_version" : "1.0.1",
1559 "layers": [
1560 {
1561 "name": "VK_LAYER_layer_name1",
1562 "type": "INSTANCE",
1563 ...
1564 },
1565 {
1566 "name": "VK_LAYER_layer_name2",
1567 "type": "INSTANCE",
1568 ...
1569 }
1570 ]
1571}
1572```
1573
1574| JSON Node | Description and Notes | Introspection Query |
1575|:----------------:|--------------------|:----------------:
1576| "file\_format\_version" | Manifest format major.minor.patch version number. | N/A |
1577| | Supported versions are: 1.0.0, 1.0.1, and 1.1.0. | |
1578| "layer" | The identifier used to group a single layer's information together. | vkEnumerateInstanceLayerProperties |
1579| "layers" | The identifier used to group multiple layers' information together. This requires a minimum Manifest file format version of 1.0.1.| vkEnumerateInstanceLayerProperties |
1580| "name" | The string used to uniquely identify this layer to applications. | vkEnumerateInstanceLayerProperties |
1581| "type" | This field indicates the type of layer. The values can be: GLOBAL, or INSTANCE | vkEnumerate*LayerProperties |
1582| | **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. | |
1583| "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). | N/A |
1584| "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 |
1585| "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 |
1586| "description" | A high-level description of the layer and it's intended use. | vkEnumerateInstanceLayerProperties |
1587| "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 |
1588| "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 |
1589| "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 |
1590| "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 |
1591| "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 |
1592
1593
1594##### Layer Manifest File Version History
1595
1596The current highest supported Layer Manifest file format supported is 1.1.0.
1597Information about each version is detailed in the following sub-sections:
1598
1599###### Layer Manifest File Version 1.1.0
1600
1601Layer Manifest File Version 1.1.0 is tied to changes exposed by the Loader/Layer
1602interface version 2.
1603 1. Renaming "vkGetInstanceProcAddr" in the "functions" section is
1604 deprecated since the loader no longer needs to query the layer about
1605 "vkGetInstanceProcAddr" directly. It is now returned during the layer
1606 negotiation, so this field will be ignored.
1607 2. Renaming "vkGetDeviceProcAddr" in the "functions" section is
1608 deprecated since the loader no longer needs to query the layer about
1609 "vkGetDeviceProcAddr" directly. It too is now returned during the layer
1610 negotiation, so this field will be ignored.
1611 3. Renaming the "vkNegotiateLoaderLayerInterfaceVersion" function is
1612 being added to the "functions" section, since this is now the only
1613 function the loader needs to query using OS-specific calls.
1614 - NOTE: This is an optional field and, as the two previous fields, only
1615needed if the layer requires changing the name of the function for some reason.
1616
1617You do not need to update your layer manifest file if you don't change the
1618names of any of the listed functions.
1619
1620###### Layer Manifest File Version 1.0.1
1621
1622The ability to define multiple layers using the "layers" array was added. This
1623JSON array field can be used when defining a single layer or multiple layers.
1624The "layer" field is still present and valid for a single layer definition.
1625
1626###### Layer Manifest File Version 1.0.0
1627
1628The initial version of the layer manifest file specified the basic format and
1629fields of a layer JSON file. The fields of the 1.0.0 file format include:
1630 * "file\_format\_version"
1631 * "layer"
1632 * "name"
1633 * "type"
1634 * "library\_path"
1635 * "api\_version"
1636 * "implementation\_version"
1637 * "description"
1638 * "functions"
1639 * "instance\_extensions"
1640 * "device\_extensions"
1641 * "enable\_environment"
1642 * "disable\_environment"
1643
1644It was also during this time that the value of "DEVICE" was deprecated from
1645the "type" field.
1646
1647
1648#### Layer Library Versions
1649
1650The current Layer Library interface is at version 2. The following sections
1651detail the differences between the various versions.
1652
1653##### Layer Library API Version 2
1654
1655Introduced the concept of
1656[loader and layer interface](#layer-version-negotiation) using the new
1657`vkNegotiateLoaderLayerInterfaceVersion` function. Additionally, it introduced
1658the concept of
1659[Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-
1660extensions)
1661and the associated `vk_layerGetPhysicalDeviceProcAddr` function. Finally, it
1662changed the manifest file defition to 1.1.0.
1663
1664##### Layer Library API Version 1
1665
1666A layer library supporting interface version 1 had the following behavior:
1667 1. `GetInstanceProcAddr` and `GetDeviceProcAddr` were directly exported
1668 2. The layer manifest file was able to override the names of the
1669`GetInstanceProcAddr` and `GetDeviceProcAddr`functions.
1670
1671##### Layer Library API Version 0
1672
1673A layer library supporting interface version 0 must define and export these
1674introspection functions, unrelated to any Vulkan function despite the names,
1675signatures, and other similarities:
1676
Lenny Komowde3924a2017-05-04 14:50:01 -06001677- `vkEnumerateInstanceLayerProperties` enumerates all layers in a layer
Mark Young39389872017-01-19 21:10:49 -07001678library.
1679 - This function never fails.
1680 - When a layer library contains only one layer, this function may be an alias
1681 to the layer's `vkEnumerateInstanceLayerProperties`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001682- `vkEnumerateInstanceExtensionProperties` enumerates instance extensions of
Mark Young39389872017-01-19 21:10:49 -07001683 layers in a layer library.
1684 - "pLayerName" is always a valid layer name.
1685 - This function never fails.
1686 - When a layer library contains only one layer, this function may be an alias
1687 to the layer's `vkEnumerateInstanceExtensionProperties`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001688- `vkEnumerateDeviceLayerProperties` enumerates a subset (can be full,
Mark Young39389872017-01-19 21:10:49 -07001689 proper, or empty subset) of layers in a layer library.
1690 - "physicalDevice" is always `VK_NULL_HANDLE`.
1691 - This function never fails.
1692 - If a layer is not enumerated by this function, it will not participate in
1693 device function interception.
Lenny Komowde3924a2017-05-04 14:50:01 -06001694- `vkEnumerateDeviceExtensionProperties` enumerates device extensions of
Mark Young39389872017-01-19 21:10:49 -07001695 layers in a layer library.
1696 - "physicalDevice" is always `VK_NULL_HANDLE`.
1697 - "pLayerName" is always a valid layer name.
1698 - This function never fails.
1699
1700It must also define and export these functions once for each layer in the
1701library:
1702
Lenny Komowde3924a2017-05-04 14:50:01 -06001703- `<layerName>GetInstanceProcAddr(instance, pName)` behaves identically to a
Mark Young39389872017-01-19 21:10:49 -07001704layer's vkGetInstanceProcAddr except it is exported.
1705
1706 When a layer library contains only one layer, this function may
1707 alternatively be named `vkGetInstanceProcAddr`.
1708
Lenny Komowde3924a2017-05-04 14:50:01 -06001709- `<layerName>GetDeviceProcAddr` behaves identically to a layer's
Mark Young39389872017-01-19 21:10:49 -07001710vkGetDeviceProcAddr except it is exported.
1711
1712 When a layer library contains only one layer, this function may
1713 alternatively be named `vkGetDeviceProcAddr`.
1714
1715All layers contained within a library must support `vk_layer.h`. They do not
1716need to implement functions that they do not intercept. They are recommended
1717not to export any functions.
1718
1719
1720<br/>
1721<br/>
1722
1723## Vulkan Installable Client Driver Interface With the Loader
1724
1725This section discusses the various requirements for the loader and a Vulkan
1726ICD to properly hand-shake.
1727
Lenny Komowde3924a2017-05-04 14:50:01 -06001728 * [ICD Discovery](#icd-discovery)
1729 * [ICD Manifest File Usage](#icd-manifest-file-usage)
1730 * [ICD Discovery on Windows](#icd-discovery-on-windows)
1731 * [ICD Discovery on Linux](#icd-discovery-on-linux)
1732 * [Using Pre-Production ICDs on Windows and Linux](#using-pre-production-icds-on-windows-and-linux)
1733 * [ICD Discovery on Android](#icd-discovery-on-android)
1734 * [ICD Manifest File Format](#icd-manifest-file-format)
1735 * [ICD Manifest File Versions](#icd-manifest-file-versions)
1736 * [ICD Manifest File Version 1.0.0](#icd-manifest-file-version-1.0.0)
1737 * [ICD Vulkan Entry-Point Discovery](#icd-vulkan-entry-point-discovery)
1738 * [ICD Unknown Physical Device Extensions](#icd-unknown-physical-device-extensions)
1739 * [ICD Dispatchable Object Creation](#icd-dispatchable-object-creation)
1740 * [Handling KHR Surface Objects in WSI Extensions](#handling-khr-surface-objects-in-wsi-extensions)
1741 * [Loader and ICD Interface Negotiation](#loader-and-icd-interface-negotiation)
1742 * [Windows and Linux ICD Negotiation](#windows-and-linux-icd-negotiation)
1743 * [Version Negotiation Between Loader and ICDs](#version-negotiation-between-loader-and-icds)
1744 * [Interfacing With Legacy ICDs or Loader](#interfacing-with-legacy-icds-or-loader)
1745 * [Loader Version 4 Interface Requirements](#loader-version-4-interface-requirements)
1746 * [Loader Version 3 Interface Requirements](#loader-version-3-interface-requirements)
1747 * [Loader Version 2 Interface Requirements](#loader-version-2-interface-requirements)
1748 * [Loader Versions 0 and 1 Interface Requirements](#loader-versions-0-and-1-interface-requirements)
1749 * [Android ICD Negotiation](#android-icd-negotiation)
Mark Young39389872017-01-19 21:10:49 -07001750
1751
1752### ICD Discovery
1753
1754Vulkan allows multiple drivers each with one or more devices (represented by a
1755Vulkan `VkPhysicalDevice` object) to be used collectively. The loader is
1756responsible for discovering available Vulkan ICDs on the system. Given a list
1757of available ICDs, the loader can enumerate all the physical devices available
1758for an application and return this information to the application. The process
1759in which the loader discovers the available Installable Client Drivers (ICDs)
1760on a system is platform dependent. Windows, Linux and Android ICD discovery
1761details are listed below.
1762
1763#### ICD Manifest File Usage
1764
Mark Young18bbaab2017-03-20 08:27:14 -06001765As with layers, on Windows and Linux systems, JSON formatted manifest files are
Mark Young39389872017-01-19 21:10:49 -07001766used to store ICD information. In order to find system-installed drivers, the
1767Vulkan loader will read the JSON files to identify the names and attributes of
1768each driver. One thing you will notice is that ICD Manifest files are much
1769simpler than the corresponding layer Manifest files.
1770
1771See the [Current ICD Manifest File Format](#icd-manifest-file-format) section
1772for more details.
1773
1774
1775#### ICD Discovery on Windows
1776
1777In order to find installed ICDs, the Vulkan loader will scan the
1778values in the following Windows registry key:
1779
1780```
1781 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers
1782```
1783
1784For 32-bit applications on 64-bit Windows, the loader scan's the 32-bit
1785registry location:
1786
1787```
1788 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Khronos\Vulkan\Drivers
1789```
1790
1791The loader will look at the appropriate registry location and check each value
1792listed. If the key is of type DWORD, and it has a value of 0, the loader will
1793open the JSON manifest file specified by the name. Each name must be a full
1794pathname to a text manifest file. The Vulkan loader will attempt to open each
1795manifest file to obtain the information about an ICD's shared library (".dll")
1796file.
1797
1798For example, let us assume the registry contains the following data:
1799
1800```
1801[HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers\]
1802
1803"C:\vendor a\vk_vendora.json"=dword:00000000
1804"C:\windows\system32\vendorb_vk.json"=dword:00000001
1805"C:\windows\system32\vendorc_icd.json"=dword:00000000
1806```
1807
1808In this case, the loader will step through each entry, and check the value. If
1809the value is 0, then the loader will attempt to load the file. In this case,
Mark Young18bbaab2017-03-20 08:27:14 -06001810the loader will open the first and last listings, but not the middle. This
Mark Young39389872017-01-19 21:10:49 -07001811is because the value of 1 for vendorb_vk.json disables the driver.
1812
1813The Vulkan loader will open each enabled manifest file found to obtain the name
1814or pathname of an ICD shared library (".DLL") file.
1815
1816See the [ICD Manifest File Format](#icd-manifest-file-format) section for more
1817details.
1818
1819
1820#### ICD Discovery on Linux
1821
1822In order to find installed ICDs, the Vulkan loader will scan the files
1823in the following Linux directories:
1824
1825```
1826 /usr/local/etc/vulkan/icd.d
1827 /usr/local/share/vulkan/icd.d
1828 /etc/vulkan/icd.d
1829 /usr/share/vulkan/icd.d
1830 $HOME/.local/share/vulkan/icd.d
1831```
1832
1833The "/usr/local/*" directories can be configured to be other directories at
1834build time.
1835
1836The typical usage of the directories is indicated in the table below.
1837
1838| Location | Details |
1839|-------------------|------------------------|
1840| $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 |
1841| "/usr/local/etc/vulkan/icd.d" | Directory for locally built ICDs |
1842| "/usr/local/share/vulkan/icd.d" | Directory for locally built ICDs |
1843| "/etc/vulkan/icd.d" | Location of ICDs installed from non-Linux-distribution-provided packages |
1844| "/usr/share/vulkan/icd.d" | Location of ICDs installed from Linux-distribution-provided packages |
1845
1846The Vulkan loader will open each manifest file found to obtain the name or
1847pathname of an ICD shared library (".so") file.
1848
1849See the [ICD Manifest File Format](#icd-manifest-file-format) section for more
1850details.
1851
1852#### Using Pre-Production ICDs on Windows and Linux
1853
1854Independent Hardware Vendor (IHV) pre-production ICDs. In some cases, a
1855pre-production ICD may be in an installable package. In other cases, a
1856pre-production ICD may simply be a shared library in the developer's build tree.
1857In this latter case, we want to allow developers to point to such an ICD without
1858modifying the system-installed ICD(s) on their system.
1859
1860This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
1861which will override the mechanism used for finding system-installed ICDs. In
1862other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used.
1863
1864The "VK\_ICD\_FILENAMES" environment variable is a list of ICD
1865manifest files, containing the full path to the ICD JSON Manifest file. This
1866list is colon-separated on Linux, and semi-colon separated on Windows.
1867
1868Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
1869file for a developer-built ICD. A separator (colon or semi-colon) is only used
1870if more than one ICD is listed.
1871
1872**NOTE:** On Linux, this environment variable will be ignored for suid programs.
1873
1874
1875#### ICD Discovery on Android
1876
1877The Android loader lives in the system library folder. The location cannot be
1878changed. The loader will load the driver/ICD via hw\_get\_module with the ID
1879of "vulkan". **Due to security policies in Android, none of this can be modified
1880under normal use.**
1881
1882
1883### ICD Manifest File Format
1884
1885The following section discusses the details of the ICD Manifest JSON file
1886format. The JSON file itself does not have any requirements for naming. The
1887only requirement is that the extension suffix of the file ends with ".json".
1888
1889Here is an example layer JSON Manifest file:
1890
1891```
1892{
1893 "file_format_version": "1.0.0",
1894 "ICD": {
1895 "library_path": "path to ICD library",
1896 "api_version": "1.0.5"
1897 }
1898}
1899```
1900
1901| Field Name | Field Value |
1902|----------------|--------------------|
1903| "file\_format\_version" | The JSON format major.minor.patch version number of this file. Currently supported version is 1.0.0. |
1904| "ICD" | The identifier used to group all ICD information together. |
1905| "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 |
1906| "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. |
1907
1908**NOTE:** If the same ICD shared library supports multiple, incompatible
1909versions of text manifest file format versions, it must have separate
1910JSON files for each (all of which may point to the same shared library).
1911
1912##### ICD Manifest File Versions
1913
1914There has only been one version of the ICD manifest files supported. This is
1915version 1.0.0.
1916
1917###### ICD Manifest File Version 1.0.0
1918
1919The initial version of the ICD Manifest file specified the basic format and
1920fields of a layer JSON file. The fields of the 1.0.0 file format include:
1921 * "file\_format\_version"
1922 * "ICD"
1923 * "library\_path"
1924 * "api\_version"
1925
1926
1927### ICD Vulkan Entry-Point Discovery
1928
1929The Vulkan symbols exported by an ICD must not clash with the loader's exported
1930Vulkan symbols. This could be for several reasons. Because of this, all ICDs
1931must export the following function that is used for discovery of ICD Vulkan
1932entry-points. This entry-point is not a part of the Vulkan API itself, only a
1933private interface between the loader and ICDs for version 1 and higher
1934interfaces.
1935
1936```cpp
1937VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1938 VkInstance instance,
1939 const char* pName);
1940```
1941
1942This function has very similar semantics to `vkGetInstanceProcAddr`.
1943`vk_icdGetInstanceProcAddr` returns valid function pointers for all the global-
1944level and instance-level Vulkan functions, and also for `vkGetDeviceProcAddr`.
1945Global-level functions are those which contain no dispatchable object as the
1946first parameter, such as `vkCreateInstance` and
1947`vkEnumerateInstanceExtensionProperties`. The ICD must support querying global-
1948level entry-points by calling `vk_icdGetInstanceProcAddr` with a NULL
1949`VkInstance` parameter. Instance-level functions are those that have either
1950`VkInstance`, or `VkPhysicalDevice` as the first parameter dispatchable object.
1951Both core entry-points and any instance extension entry-points the ICD supports
1952should be available via `vk_icdGetInstanceProcAddr`. Future Vulkan instance
1953extensions may define and use new instance-level dispatchable objects other
1954than `VkInstance` and `VkPhysicalDevice`, in which case extension entry-points
1955using these newly defined dispatchable objects must be queryable via
1956`vk_icdGetInstanceProcAddr`.
1957
1958All other Vulkan entry-points must either:
1959 * NOT be exported directly from the ICD library
1960 * or NOT use the official Vulkan function names if they are exported
1961
1962This requirement is for ICD libraries that include other
1963functionality (such as OpenGL) and thus could be loaded by the
1964application prior to when the Vulkan loader library is loaded by the
1965application.
1966
1967Beware of interposing by dynamic OS library loaders if the official Vulkan
1968names are used. On Linux, if official names are used, the ICD library must be
1969linked with -Bsymbolic.
1970
1971
1972### ICD Unknown Physical Device Extensions
1973
1974Originally, if the loader was called with `vkGetInstanceProcAddr`, it would
1975result in the following behavior:
1976 1. The loader would check if core function:
1977 - If it was, it would return the function pointer
1978 2. The loader would check if known extension function:
1979 - If it was, it would return the function pointer
1980 3. If the loader knew nothing about it, it would call down using
1981`GetInstanceProcAddr`
1982 - If it returned non-NULL, treat it as an unknown logical device command.
1983 - This meant setting up a generic trampoline function that takes in a
1984VkDevice as the first parameter and adjusting the dispatch table to call the
1985ICD/Layers function after getting the dispatch table from the VkDevice.
1986 4. If all the above failed, the loader would return NULL to the application.
1987
1988This caused problems when an ICD attempted to expose new physical device
1989extensions the loader knew nothing about, but an application did. Because the
1990loader knew nothing about it, the loader would get to step 3 in the above
1991process and would treat the function as an unknown logical device command. The
1992problem is, this would create a generic VkDevice trampoline function which, on
1993the first call, would attempt to dereference the VkPhysicalDevice as a VkDevice.
1994This would lead to a crash or corruption.
1995
1996In order to identify the extension entry-points specific to physical device
1997extensions, the following function can be added to an ICD:
1998
1999```cpp
2000PFN_vkVoidFunction vk_icdGetPhysicalDeviceProcAddr(VkInstance instance,
2001 const char* pName);
2002```
2003
2004This function behaves similar to `vkGetInstanceProcAddr` and
2005`vkGetDeviceProcAddr` except it should only return values for physical device
2006extension entry-points. In this way, it compares "pName" to every physical
2007device function supported in the ICD.
2008
2009The following rules apply:
Lenny Komowde3924a2017-05-04 14:50:01 -06002010* If it is the name of a physical device function supported by the ICD, the
Mark Young39389872017-01-19 21:10:49 -07002011pointer to the ICD's corresponding function should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06002012* If it is the name of a valid function which is **not** a physical device
Mark Young39389872017-01-19 21:10:49 -07002013function (i.e. an Instance, Device, or other function implemented by the ICD),
2014then the value of NULL should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06002015* If the ICD has no idea what this function is, it should return NULL.
Mark Young39389872017-01-19 21:10:49 -07002016
2017This support is optional and should not be considered a requirement. This is
2018only required if an ICD intends to support some functionality not directly
2019supported by a significant population of loaders in the public. If an ICD
Mark Young18bbaab2017-03-20 08:27:14 -06002020does implement this support, it should return the address of its
Mark Young39389872017-01-19 21:10:49 -07002021`vk_icdGetPhysicalDeviceProcAddr` function through the `vkGetInstanceProcAddr`
2022function.
2023
2024The new behavior of the loader's vkGetInstanceProcAddr with support for the
2025`vk_icdGetPhysicalDeviceProcAddr` function is as follows:
2026 1. Check if core function:
2027 - If it is, return the function pointer
2028 2. Check if known instance or device extension function:
2029 - If it is, return the function pointer
2030 3. Call the layer/ICD `GetPhysicalDeviceProcAddr`
2031 - If it returns non-NULL, return a trampoline to a generic physical device
2032function, and setup a generic terminator which will pass it to the proper ICD.
2033 4. Call down using `GetInstanceProcAddr`
2034 - If it returns non-NULL, treat it as an unknown logical device command.
2035This means setting up a generic trampoline function that takes in a VkDevice as
2036the first parameter and adjusting the dispatch table to call the ICD/Layers
2037function after getting the dispatch table from the VkDevice. Then, return the
2038pointer to corresponding trampoline function.
2039 5. Return NULL
2040
2041You can see now, that, if the command gets promoted to core later, it will no
2042longer be setup using `vk_icdGetPhysicalDeviceProcAddr`. Additionally, if the
2043loader adds direct support for the extension, it will no longer get to step 3,
2044because step 2 will return a valid function pointer. However, the ICD should
2045continue to support the command query via `vk_icdGetPhysicalDeviceProcAddr`,
2046until at least a Vulkan version bump, because an older loader may still be
2047attempting to use the commands.
2048
2049
2050### ICD Dispatchable Object Creation
2051
2052As previously covered, the loader requires dispatch tables to be accessible
2053within Vulkan dispatchable objects, such as: `VkInstance`, `VkPhysicalDevice`,
2054`VkDevice`, `VkQueue`, and `VkCommandBuffer`. The specific requirements on all
2055dispatchable objects created by ICDs are as follows:
2056
2057- All dispatchable objects created by an ICD can be cast to void \*\*
2058- The loader will replace the first entry with a pointer to the dispatch table
2059 which is owned by the loader. This implies three things for ICD drivers
2060 1. The ICD must return a pointer for the opaque dispatchable object handle
2061 2. This pointer points to a regular C structure with the first entry being a
2062 pointer.
2063 * **NOTE:** For any C\++ ICD's that implement VK objects directly as C\++
2064classes.
2065 * The C\++ compiler may put a vtable at offset zero if your class is non-
2066POD due to the use of a virtual function.
2067 * In this case use a regular C structure (see below).
2068 3. The loader checks for a magic value (ICD\_LOADER\_MAGIC) in all the created
2069 dispatchable objects, as follows (see `include/vulkan/vk_icd.h`):
2070
2071```cpp
2072#include "vk_icd.h"
2073
2074union _VK_LOADER_DATA {
2075 uintptr loadermagic;
2076 void *loaderData;
2077} VK_LOADER_DATA;
2078
2079vkObj alloc_icd_obj()
2080{
2081 vkObj *newObj = alloc_obj();
2082 ...
2083 // Initialize pointer to loader's dispatch table with ICD_LOADER_MAGIC
2084
2085 set_loader_magic_value(newObj);
2086 ...
2087 return newObj;
2088}
2089```
2090
2091
2092### Handling KHR Surface Objects in WSI Extensions
2093
2094Normally, ICDs handle object creation and destruction for various Vulkan
2095objects. The WSI surface extensions for Linux and Windows
2096("VK\_KHR\_win32\_surface", "VK\_KHR\_xcb\_surface", "VK\_KHR\_xlib\_surface",
2097"VK\_KHR\_mir\_surface", "VK\_KHR\_wayland\_surface", and "VK\_KHR\_surface")
2098are handled differently. For these extensions, the `VkSurfaceKHR` object
2099creation and destruction may be handled by either the loader, or an ICD.
2100
2101If the loader handles the management of the `VkSurfaceKHR` objects:
2102 1. The loader will handle the calls to `vkCreateXXXSurfaceKHR` and
2103`vkDestroySurfaceKHR`
2104 functions without involving the ICDs.
2105 * Where XXX stands for the Windowing System name:
2106 * Mir
2107 * Wayland
2108 * Xcb
2109 * Xlib
2110 * Windows
2111 * Android
2112 2. The loader creates a `VkIcdSurfaceXXX` object for the corresponding
2113`vkCreateXXXSurfaceKHR` call.
2114 * The `VkIcdSurfaceXXX` structures are defined in `include/vulkan/vk_icd.h`.
2115 3. ICDs can cast any `VkSurfaceKHR` object to a pointer to the appropriate
2116 `VkIcdSurfaceXXX` structure.
2117 4. The first field of all the `VkIcdSurfaceXXX` structures is a
2118`VkIcdSurfaceBase` enumerant that indicates whether the
2119 surface object is Win32, Xcb, Xlib, Mir, or Wayland.
2120
2121The ICD may choose to handle `VkSurfaceKHR` object creation instead. If an ICD
2122desires to handle creating and destroying it must do the following:
2123 1. Support version 3 or newer of the loader/ICD interface.
2124 2. Export and handle all functions that take in a `VkSurfaceKHR` object,
2125including:
2126 * `vkCreateXXXSurfaceKHR`
2127 * `vkGetPhysicalDeviceSurfaceSupportKHR`
2128 * `vkGetPhysicalDeviceSurfaceCapabilitiesKHR`
2129 * `vkGetPhysicalDeviceSurfaceFormatsKHR`
2130 * `vkGetPhysicalDeviceSurfacePresentModesKHR`
2131 * `vkCreateSwapchainKHR`
2132 * `vkDestroySurfaceKHR`
2133
2134Because the `VkSurfaceKHR` object is an instance-level object, one object can be
2135associated with multiple ICDs. Therefore, when the loader receives the
2136`vkCreateXXXSurfaceKHR` call, it still creates an internal `VkSurfaceIcdXXX`
2137object. This object acts as a container for each ICD's version of the
2138`VkSurfaceKHR` object. If an ICD does not support the creation of its own
2139`VkSurfaceKHR` object, the loader's container stores a NULL for that ICD. On
2140the otherhand, if the ICD does support `VkSurfaceKHR` creation, the loader will
2141make the appropriate `vkCreateXXXSurfaceKHR` call to the ICD, and store the
2142returned pointer in it's container object. The loader then returns the
2143`VkSurfaceIcdXXX` as a `VkSurfaceKHR` object back up the call chain. Finally,
2144when the loader receives the `vkDestroySurfaceKHR` call, it subsequently calls
2145`vkDestroySurfaceKHR` for each ICD who's internal `VkSurfaceKHR` object is not
2146NULL. Then the loader destroys the container object before returning.
2147
2148
2149### Loader and ICD Interface Negotiation
2150
2151Generally, for functions issued by an application, the loader can be
2152viewed as a pass through. That is, the loader generally doesn't modify the
2153functions or their parameters, but simply calls the ICDs entry-point for that
2154function. There are specific additional interface requirements an ICD needs to
2155comply with that are not part of any requirements from the Vulkan specification.
2156These addtional requirements are versioned to allow flexibility in the future.
2157
2158
2159#### Windows and Linux ICD Negotiation
2160
2161
2162##### Version Negotiation Between Loader and ICDs
2163
2164All ICDs (supporting interface version 2 or higher) must export the following
2165function that is used for determination of the interface version that will be
2166used. This entry-point is not a part of the Vulkan API itself, only a private
2167interface between the loader and ICDs.
2168
2169```cpp
2170 VKAPI_ATTR VkResult VKAPI_CALL
2171 vk_icdNegotiateLoaderICDInterfaceVersion(
2172 uint32_t* pSupportedVersion);
2173```
2174
2175This function allows the loader and ICD to agree on an interface version to use.
2176The "pSupportedVersion" parameter is both an input and output parameter.
2177"pSupportedVersion" is filled in by the loader with the desired latest interface
2178version supported by the loader (typically the latest). The ICD receives this
2179and returns back the version it desires in the same field. Because it is
2180setting up the interface version between the loader and ICD, this should be
2181the first call made by a loader to the ICD (even prior to any calls to
2182`vk_icdGetInstanceProcAddr`).
2183
2184If the ICD receiving the call no longer supports the interface version provided
2185by the loader (due to deprecation), then it should report
2186VK_ERROR_INCOMPATIBLE_DRIVER error. Otherwise it sets the value pointed by
2187"pSupportedVersion" to the latest interface version supported by both the ICD
2188and the loader and returns VK_SUCCESS.
2189
2190The ICD should report VK_SUCCESS in case the loader provided interface version
2191is newer than that supported by the ICD, as it's the loader's responsibility to
2192determine whether it can support the older interface version supported by the
2193ICD. The ICD should also report VK_SUCCESS in the case its interface version
2194is greater than the loader's, but return the loader's version. Thus, upon
2195return of VK_SUCCESS the "pSupportedVersion" will contain the desired interface
2196version to be used by the ICD.
2197
2198If the loader receives an interface version from the ICD that the loader no
2199longer supports (due to deprecation), or it receives a
2200VK_ERROR_INCOMPATIBLE_DRIVER error instead of VK_SUCCESS, then the loader will
2201treat the ICD as incompatible and will not load it for use. In this case, the
2202application will not see the ICDs `vkPhysicalDevice` during enumeration.
2203
2204###### Interfacing With Legacy ICDs or Loader
2205
2206If a loader sees that an ICD does not export the
2207`vk_icdNegotiateLoaderICDInterfaceVersion` function, then the loader assumes the
2208corresponding ICD only supports either interface version 0 or 1.
2209
2210From the other side of the interface, if an ICD sees a call to
2211`vk_icdGetInstanceProcAddr` before a call to
2212`vk_icdGetLoaderICDInterfaceVersion`, then it knows that loader making the calls
2213is a legacy loader supporting version 0 or 1. If the loader calls
2214`vk_icdGetInstanceProcAddr` first, it supports at least version 1. Otherwise,
2215the loader only supports version 0.
2216
2217
2218##### Loader Version 4 Interface Requirements
2219
2220The major change to version 4 of the loader/ICD interface is the support of
2221[Unknown Physical Device Extensions](#icd-unknown-physical-device-
2222extensions] using the `vk_icdGetPhysicalDeviceProcAddr` function. This
2223function is purely optional. However, if an ICD supports a Physical Device
2224extension, it must provide a `vk_icdGetPhysicalDeviceProcAddr` function.
2225Otherwise, the loader will continue to treat any unknown functions as VkDevice
2226functions and cause invalid behavior.
2227
2228
2229##### Loader Version 3 Interface Requirements
2230
2231The primary change that occurred in version 3 of the loader/ICD interface was to
2232allow an ICD to handle creation/destruction of their own KHR_surfaces. Up until
2233this point, the loader created a surface object that was used by all ICDs.
2234However, some ICDs may want to provide their own surface handles. If an ICD
2235chooses to enable this support, it must export support for version 3 of the
2236loader/ICD interface, as well as any Vulkan function that uses a KHR_surface
2237handle, such as:
2238- `vkCreateXXXSurfaceKHR` (where XXX is the platform specific identifier [i.e.
2239`vkCreateWin32SurfaceKHR` for Windows])
2240- `vkDestroySurfaceKHR`
2241- `vkCreateSwapchainKHR`
2242- `vkGetPhysicalDeviceSurfaceSupportKHR`
2243- `vkGetPhysicalDeviceSurfaceCapabilitiesKHR`
2244- `vkGetPhysicalDeviceSurfaceFormatsKHR`
2245- `vkGetPhysicalDeviceSurfacePresentModesKHR`
2246
2247An ICD can still choose to not take advantage of this functionality by simply
2248not exposing the above the `vkCreateXXXSurfaceKHR` and `vkDestroySurfaceKHR`
2249functions.
2250
2251
2252##### Loader Version 2 Interface Requirements
2253
2254Version 2 interface has requirements in three areas:
2255 1. ICD Vulkan entry-point discovery,
2256 2. `KHR_surface` related requirements in the WSI extensions,
2257 3. Vulkan dispatchable object creation requirements.
2258
2259##### Loader Versions 0 and 1 Interface Requirements
2260
2261Version 0 and 1 interfaces do not support version negotiation via
2262`vk_icdNegotiateLoaderICDInterfaceVersion`. ICDs can distinguish version 0 and
2263version 1 interfaces as follows: if the loader calls `vk_icdGetInstanceProcAddr`
2264first it supports version 1; otherwise the loader only supports version 0.
2265
2266Version 0 interface does not support `vk_icdGetInstanceProcAddr`. Version 0
2267interface requirements for obtaining ICD Vulkan entry-points are as follows:
2268
2269- The function `vkGetInstanceProcAddr` **must be exported** in the ICD library
2270and returns valid function pointers for all the Vulkan API entry-points.
2271- `vkCreateInstance` **must be exported** by the ICD library.
2272- `vkEnumerateInstanceExtensionProperties` **must be exported** by the ICD
2273library.
2274
2275Additional Notes:
2276
2277- The loader will filter out extensions requested in `vkCreateInstance` and
2278`vkCreateDevice` before calling into the ICD; Filtering will be of extensions
2279advertised by entities (e.g. layers) different from the ICD in question.
2280- The loader will not call the ICD for `vkEnumerate\*LayerProperties`() as layer
2281properties are obtained from the layer libraries and layer JSON files.
2282- If an ICD library author wants to implement a layer, it can do so by having
2283the appropriate layer JSON manifest file refer to the ICD library file.
2284- The loader will not call the ICD for
2285 `vkEnumerate\*ExtensionProperties` if "pLayerName" is not equal to `NULL`.
2286- ICDs creating new dispatchable objects via device extensions need to
2287initialize the created dispatchable object. The loader has generic *trampoline*
2288code for unknown device extensions. This generic *trampoline* code doesn't
2289initialize the dispatch table within the newly created object. See the
2290[Creating New Dispatchable Objects](#creating-new-dispatchable-objects) section
2291for more information on how to initialize created dispatchable objects for
2292extensions non known by the loader.
2293
2294
2295#### Android ICD Negotiation
2296
2297The Android loader uses the same protocol for initializing the dispatch
2298table as described above. The only difference is that the Android
2299loader queries layer and extension information directly from the
2300respective libraries and does not use the json manifest files used
2301by the Windows and Linux loaders.
2302
2303
2304## Glossary of Terms
2305
2306| Field Name | Field Value |
2307|----------------|--------------------|
2308| 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. |
2309| 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. |
2310| Core Function | A function that is already part of the Vulkan core specification and not an extension. For example, vkCreateDevice(). |
2311| 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 |
2312| 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. |
2313| 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. |
2314| 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. |
2315| 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). |
2316| 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.
2317| 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. |
2318| 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 |
2319| 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. |
2320| 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. |
2321| 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. |
2322| 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).
2323| 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. |
2324| 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. |
2325| 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. |