blob: 144dff9caaa0ec7a6834d6ceb180ffec20932df3 [file] [log] [blame] [view]
Mark Young39389872017-01-19 21:10:49 -07001# Architecture of the Vulkan Loader Interfaces
Jon Ashburnc2972682016-02-08 15:42:01 -07002
Mark Young39389872017-01-19 21:10:49 -07003## Table of Contents
Lenny Komowde3924a2017-05-04 14:50:01 -06004 * [Overview](#overview)
5 * [Who Should Read This Document](#who-should-read-this-document)
6 * [The Loader](#the-loader)
7 * [Layers](#layers)
8 * [Installable Client Drivers](#installable-client-drivers)
9 * [Instance Versus Device](#instance-versus-device)
10 * [Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains)
Jon Ashburnc2972682016-02-08 15:42:01 -070011
Lenny Komowde3924a2017-05-04 14:50:01 -060012 * [Application Interface to the Loader](#application-interface-to-the-loader)
13 * [Interfacing with Vulkan Functions](#interfacing-with-vulkan-functions)
14 * [Application Layer Usage](#application-layer-usage)
15 * [Application Usage of Extensions](#application-usage-of-extensions)
Jon Ashburnc2972682016-02-08 15:42:01 -070016
Lenny Komowde3924a2017-05-04 14:50:01 -060017 * [Loader and Layer Interface](#loader-and-layer-interface)
18 * [Layer Discovery](#layer-discovery)
19 * [Layer Version Negotiation](#layer-version-negotiation)
20 * [Layer Call Chains and Distributed Dispatch](#layer-call-chains-and-distributed-dispatch)
21 * [Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-extensions)
22 * [Layer Intercept Requirements](#layer-intercept-requirements)
23 * [Distributed Dispatching Requirements](#distributed-dispatching-requirements)
24 * [Layer Conventions and Rules](#layer-conventions-and-rules)
25 * [Layer Dispatch Initialization](#layer-dispatch-initialization)
26 * [Example Code for CreateInstance](#example-code-for-createinstance)
27 * [Example Code for CreateDevice](#example-code-for-createdevice)
Mark Youngc82a0622017-05-05 11:17:17 -060028 * [Meta-layers](#meta-layers)
Lenny Komowde3924a2017-05-04 14:50:01 -060029 * [Special Considerations](#special-considerations)
30 * [Layer Manifest File Format](#layer-manifest-file-format)
31 * [Layer Library Versions](#layer-library-versions)
Jon Ashburnc2972682016-02-08 15:42:01 -070032
Lenny Komowde3924a2017-05-04 14:50:01 -060033 * [Vulkan Installable Client Driver interface with the loader](#vulkan-installable-client-driver-interface-with-the-loader)
34 * [ICD Discovery](#icd-discovery)
35 * [ICD Manifest File Format](#icd-manifest-file-format)
36 * [ICD Vulkan Entry-Point Discovery](#icd-vulkan-entry-point-discovery)
37 * [ICD Unknown Physical Device Extensions](#icd-unknown-physical-device-extensions)
38 * [ICD Dispatchable Object Creation](#icd-dispatchable-object-creation)
39 * [Handling KHR Surface Objects in WSI Extensions](#handling-khr-surface-objects-in-wsi-extensions)
40 * [Loader and ICD Interface Negotiation](#loader-and-icd-interface-negotiation)
41 * [Glossary of Terms](#glossary-of-terms)
Mark Young39389872017-01-19 21:10:49 -070042
43## Overview
Mark Youngcb6e6702016-07-20 11:38:53 -060044
Mark Young39389872017-01-19 21:10:49 -070045Vulkan is a layered architecture, made up of the following elements:
Lenny Komowde3924a2017-05-04 14:50:01 -060046 * The Vulkan Application
47 * [The Vulkan Loader](#the-loader)
48 * [Vulkan Layers](#layers)
49 * [Installable Client Drivers (ICDs)](#installable-client-drivers)
Jon Ashburnc2972682016-02-08 15:42:01 -070050
Mark Young39389872017-01-19 21:10:49 -070051![High Level View of Loader](./images/high_level_loader.png)
Jon Ashburnc2972682016-02-08 15:42:01 -070052
Mark Young39389872017-01-19 21:10:49 -070053The general concepts in this document are applicable to the loaders available
54for Windows, Linux and Android based systems.
Jon Ashburnc2972682016-02-08 15:42:01 -070055
56
Mark Young39389872017-01-19 21:10:49 -070057#### Who Should Read This Document
Jon Ashburnc2972682016-02-08 15:42:01 -070058
Mark Young39389872017-01-19 21:10:49 -070059While this document is primarily targeted at developers of Vulkan applications,
60drivers and layers, the information contained in it could be useful to anyone
61wanting a better understanding of the Vulkan runtime.
Jon Ashburnc2972682016-02-08 15:42:01 -070062
Jon Ashburnc2972682016-02-08 15:42:01 -070063
Mark Young39389872017-01-19 21:10:49 -070064#### The Loader
Jon Ashburnc2972682016-02-08 15:42:01 -070065
Jeff Juliano18e50202017-01-31 16:36:18 -050066The application sits on one end of, and interfaces directly with, the
67loader. On the other end of the loader from the application are the ICDs, which
Mark Young39389872017-01-19 21:10:49 -070068control the Vulkan-capable hardware. An important point to remember is that
Jeff Juliano18e50202017-01-31 16:36:18 -050069Vulkan-capable hardware can be graphics-based, compute-based, or both. Between
70the application and the ICDs the loader can inject a number of optional
71[layers](#layers) that provide special functionality.
Mark Youngcb6e6702016-07-20 11:38:53 -060072
73The loader is responsible for working with the various layers as well as
Mark Young39389872017-01-19 21:10:49 -070074supporting multiple GPUs and their drivers. Any Vulkan function may
Mark Youngcb6e6702016-07-20 11:38:53 -060075wind up calling into a diverse set of modules: loader, layers, and ICDs.
76The loader is critical to managing the proper dispatching of Vulkan
Mark Young39389872017-01-19 21:10:49 -070077functions to the appropriate set of layers and ICDs. The Vulkan object
Mark Youngcb6e6702016-07-20 11:38:53 -060078model allows the loader to insert layers into a call chain so that the layers
Mark Young39389872017-01-19 21:10:49 -070079can process Vulkan functions prior to the ICD being called.
80
81This document is intended to provide an overview of the necessary interfaces
82between each of these.
83
84
85##### Goals of the Loader
86
87The loader was designed with the following goals in mind.
88 1. Support one or more Vulkan-capable ICD on a user's computer system without
89them interfering with one another.
90 2. Support Vulkan Layers which are optional modules that can be enabled by an
91application, developer, or standard system settings.
92 3. Impact the overall performance of a Vulkan application in the lowest
93possible fashion.
94
95
96#### Layers
97
98Layers are optional components that augment the Vulkan system. They can
99intercept, evaluate, and modify existing Vulkan functions on their way from the
100application down to the hardware. Layers are implemented as libraries that can
101be enabled in different ways (including by application request) and are loaded
102during CreateInstance. Each layer can choose to hook (intercept) any Vulkan
103functions which in turn can be ignored or augmented. A layer does not need to
104intercept all Vulkan functions. It may choose to intercept all known functions,
105or, it may choose to intercept only one function.
106
107Some examples of features that layers may expose include:
108 * Validating API usage
109 * Adding the ability to perform Vulkan API tracing and debugging
110 * Overlay additional content on the applications surfaces
111
112Because layers are optionally, you may choose to enable layers for debugging
113your application, but then disable any layer usage when you release your
114product.
115
116
117#### Installable Client Drivers
118
119Vulkan allows multiple Installable Client Drivers (ICDs) each supporting one
120or more devices (represented by a Vulkan `VkPhysicalDevice` object) to be used
121collectively. The loader is responsible for discovering available Vulkan ICDs on
122the system. Given a list of available ICDs, the loader can enumerate all the
123physical devices available for an application and return this information to
124the application.
125
126
127#### Instance Versus Device
128
129There is an important concept which you will see brought up repeatedly
130throughout this document. Many functions, extensions, and other things in
131Vulkan are separated into two main groups:
132 * Instance-related Objects
133 * Device-related Objects
134
135
136##### Instance-related Objects
137
138A Vulkan Instance is a high-level construct used to provide Vulkan system-level
139information, or functionality. Vulkan objects associated directly with an
140instance are:
141 * `VkInstance`
142 * `VkPhysicalDevice`
143
144An Instance function is any Vulkan function which takes as its first parameter
145either an object from the Instance list, or nothing at all. Some Vulkan
146Instance functions are:
147 * `vkEnumerateInstanceExtensionProperties`
148 * `vkEnumeratePhysicalDevices`
149 * `vkCreateInstance`
150 * `vkDestroyInstance`
151
152You query Vulkan Instance functions using `vkGetInstanceProcAddr`.
153`vkGetInstanceProcAddr` can be used to query either device or instance entry-
154points in addition to all core entry-points. The returned function pointer is
155valid for this Instance and any object created under this Instance (including
156all `VkDevice` objects).
157
158Similarly, an Instance extension is a set of Vulkan Instance functions extending
159the Vulkan language. These will be discussed in more detail later.
160
161
162##### Device-related Objects
163
164A Vulkan Device, on the other-hand, is a logical identifier used to associate
165functions with a particular physical device on a user's system. Vulkan
166constructs associated directly with a device include:
167 * `VkDevice`
168 * `VkQueue`
169 * `VkCommandBuffer`
170 * Any dispatchable object that is a child of a one of the above.
171
172A Device function is any Vulkan function which takes any Device Object as its
173first parameter. Some Vulkan Device functions are:
174 * `vkQueueSubmit`
175 * `vkBeginCommandBuffer`
176 * `vkCreateEvent`
177
178You can query Vulkan Device functions using either `vkGetInstanceProcAddr` or
179`vkGetDeviceProcAddr`. If you choose to use `vkGetInstanceProcAddr`, it will
180have an additional level built into the call chain, which will reduce
181performance slightly. However, the function pointer returned can be used for
182any device created later, as long as it is associated with the same Vulkan
183Instance. If, instead you use `vkGetDeviceProcAddr`, the call chain will be more
184optimized to the specific device, but it will **only** work for the device used
185to query the function function pointer. Also, unlike `vkGetInstanceProcAddr`,
186`vkGetDeviceProcAddr` can only be used on core Vulkan Device functions, or
187Device extension functions.
188
189The best solution is to query Instance extension functions using
190`vkGetInstanceProcAddr`, and to query Device extension functions using
191`vkGetDeviceProcAddr`. See
192[Best Application Performance Setup](#best-application-performance-setup) for
193more information on this.
194
195As with Instance extensions, a Device extension is a set of Vulkan Device
196functions extending the Vulkan language. You can read more about these later in
197the document.
198
199
200#### Dispatch Tables and Call Chains
Jon Ashburnc2972682016-02-08 15:42:01 -0700201
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700202Vulkan uses an object model to control the scope of a particular action /
203operation. The object to be acted on is always the first parameter of a Vulkan
Mark Young6d026a72016-06-01 17:49:30 -0600204call and is a dispatchable object (see Vulkan specification section 2.3 Object
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700205Model). Under the covers, the dispatchable object handle is a pointer to a
Mark Youngcb6e6702016-07-20 11:38:53 -0600206structure, which in turn, contains a pointer to a dispatch table maintained by
Mark Young39389872017-01-19 21:10:49 -0700207the loader. This dispatch table contains pointers to the Vulkan functions
208appropriate to that object.
Jon Ashburnc2972682016-02-08 15:42:01 -0700209
Mark Youngcb6e6702016-07-20 11:38:53 -0600210There are two types of dispatch tables the loader maintains:
Mark Young39389872017-01-19 21:10:49 -0700211 - Instance Dispatch Table
212 - Created in the loader during the call to `vkCreateInstance`
213 - Device Dispatch Table
214 - Created in the loader during the call to `vkCreateDevice`
Jon Ashburnc2972682016-02-08 15:42:01 -0700215
Mark Young39389872017-01-19 21:10:49 -0700216At that time the application and/or system can specify optional layers to be
217included. The loader will initialize the specified layers to create a call
218chain for each Vulkan function and each entry of the dispatch table will point
219to the first element of that chain. Thus, the loader builds an instance call
220chain for each `VkInstance` that is created and a device call chain for each
221`VkDevice` that is created.
222
223When an application calls a Vulkan function, this typically will first hit a
224*trampoline* function in the loader. These *trampoline* functions are small,
225simple functions that jump to the appropriate dispatch table entry for the
226object they are given. Additionally, for functions in the instance call chain,
227the loader has an additional function, called a *terminator*, which is called
228after all enabled layers to marshall the appropriate information to all
229available ICDs.
230
231
232##### Instance Call Chain Example
Jon Ashburnc2972682016-02-08 15:42:01 -0700233
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700234For example, the diagram below represents what happens in the call chain for
Mark Young39389872017-01-19 21:10:49 -0700235`vkCreateInstance`. After initializing the chain, the loader will call into the
236first layer's `vkCreateInstance` which will call the next finally terminating in
237the loader again where this function calls every ICD's `vkCreateInstance` and
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700238saves the results. This allows every enabled layer for this chain to set up
Mark Young39389872017-01-19 21:10:49 -0700239what it needs based on the `VkInstanceCreateInfo` structure from the
240application.
241
242![Instance Call Chain](./images/loader_instance_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -0700243
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700244This also highlights some of the complexity the loader must manage when using
Mark Young39389872017-01-19 21:10:49 -0700245instance call chains. As shown here, the loader's *terminator* must aggregate
246information to and from multiple ICDs when they are present. This implies that
247the loader has to be aware of any instance-level extensions which work on a
248`VkInstance` to aggregate them correctly.
Jon Ashburnc2972682016-02-08 15:42:01 -0700249
Mark Young39389872017-01-19 21:10:49 -0700250
251##### Device Call Chain Example
252
253Device call chains are created at `vkCreateDevice` and are generally simpler
254because they deal with only a single device and the ICD can always be the
255*terminator* of the chain.
256
257![Loader Device Call Chain](./images/loader_device_chain_loader.png)
258
Jon Ashburnc2972682016-02-08 15:42:01 -0700259
Mark Youngcb6e6702016-07-20 11:38:53 -0600260<br/>
Mark Young39389872017-01-19 21:10:49 -0700261<br/>
Mark Youngcb6e6702016-07-20 11:38:53 -0600262
Mark Young39389872017-01-19 21:10:49 -0700263## Application Interface to the Loader
Jon Ashburnc2972682016-02-08 15:42:01 -0700264
Mark Young39389872017-01-19 21:10:49 -0700265In this section we'll discuss how an application interacts with the loader,
266including:
Lenny Komowde3924a2017-05-04 14:50:01 -0600267 * [Interfacing with Vulkan Functions](#interfacing-with-vulkan-functions)
268 * [Vulkan Direct Exports](#vulkan-direct-exports)
269 * [Directly Linking to the Loader](#directly-linking-to-the-loader)
270 * [Dynamic Linking](#dynamic-linking)
271 * [Static Linking](#static-linking)
272 * [Indirectly Linking to the Loader](#indirectly-linking-to-the-loader)
273 * [Best Application Performance Setup](#best-application-performance-setup)
274 * [ABI Versioning](#abi-versioning)
275 * [Application Layer Usage](#application-layer-usage)
276 * [Implicit vs Explicit Layers](#implicit-vs-explicit-layers)
277 * [Forcing Layer Source Folders](#forcing-layer-source-folders)
278 * [Forcing Layers to be Enabled](#forcing-layers-to-be-enabled)
279 * [Overall Layer Ordering](#overall-layer-ordering)
280 * [Application Usage of Extensions](#application-usage-of-extensions)
281 * [Instance and Device Extensions](#instance-and-device-extensions)
282 * [WSI Extensions](#wsi-extensions)
283 * [Unknown Extensions](#unknown-extensions)
Jon Ashburnc2972682016-02-08 15:42:01 -0700284
Mark Young39389872017-01-19 21:10:49 -0700285
286#### Interfacing with Vulkan Functions
287There are several ways you can interface with Vulkan functions through the
288loader.
Jon Ashburnc2972682016-02-08 15:42:01 -0700289
Jon Ashburnc2972682016-02-08 15:42:01 -0700290
Mark Young39389872017-01-19 21:10:49 -0700291##### Vulkan Direct Exports
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700292The loader library on Windows, Linux and Android will export all core Vulkan
293and all appropriate Window System Interface (WSI) extensions. This is done to
294make it simpler to get started with Vulkan development. When an application
295links directly to the loader library in this way, the Vulkan calls are simple
Mark Young39389872017-01-19 21:10:49 -0700296*trampoline* functions that jump to the appropriate dispatch table entry for the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700297object they are given.
Jon Ashburnc2972682016-02-08 15:42:01 -0700298
Mark Young39389872017-01-19 21:10:49 -0700299
Lenny Komowde3924a2017-05-04 14:50:01 -0600300##### Directly Linking to the Loader
301
302###### Dynamic Linking
303The loader is ordinarily distributed as a dynamic library (.dll on Windows or
304.so on Linux) which gets installed to the system path for dynamic libraries.
305Linking to the dynamic library is generally the preferred method of linking to
306the loader, as doing so allows the loader to be updated for bug fixes and
307improvements. Furthermore, the dynamic library is generally installed to Windows
308systems as part of driver installation and is generally provided on Linux
309through the system package manager. This means that applications can usually
310expect a copy of the loader to be present on a system. If applications want to
311be completely sure that a loader is present, they can include a loader or
312runtime installer with their application.
313
314###### Static Linking
315The loader can also be used as a static library (this is shipped in the
316Windows SDK as `VKstatic.1.lib`). Linking to the static loader means that the
317user does not need to have a Vulkan runtime installed, and it also guarantees
318that your application will use a specific version of the loader. However, there
319are several downsides to this approach:
320
321 - The static library can never be updated without re-linking the application
322 - This opens up the possibility that two included libraries could contain
323 different versions of the loader
324 - This could potentially cause conflicts between the different loader versions
325
326As a result, it is recommended that users prefer linking to the .dll and
327.so versions of the loader.
328
329
Mark Young39389872017-01-19 21:10:49 -0700330##### Indirectly Linking to the Loader
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700331Applications are not required to link directly to the loader library, instead
332they can use the appropriate platform specific dynamic symbol lookup on the
Mark Young6d026a72016-06-01 17:49:30 -0600333loader library to initialize the application's own dispatch table. This allows
Mark Young39389872017-01-19 21:10:49 -0700334an application to fail gracefully if the loader cannot be found. It also
Jeff Julianof1619872016-02-17 17:25:42 -0500335provides the fastest mechanism for the application to call Vulkan functions. An
336application will only need to query (via system calls such as dlsym()) the
Mark Young39389872017-01-19 21:10:49 -0700337address of `vkGetInstanceProcAddr` from the loader library. Using
338`vkGetInstanceProcAddr` the application can then discover the address of all
339functions and extensions available, such as `vkCreateInstance`,
340`vkEnumerateInstanceExtensionProperties` and
341`vkEnumerateInstanceLayerProperties` in a platform-independent way.
Jon Ashburnc2972682016-02-08 15:42:01 -0700342
Mark Young39389872017-01-19 21:10:49 -0700343
344##### Best Application Performance Setup
345
346If you desire the best performance possible, you should setup your own
347dispatch table so that all your Instance functions are queried using
348`vkGetInstanceProcAddr` and all your Device functions are queried using
349`vkGetDeviceProcAddr`.
350
351*Why should you do this?*
352
353The answer comes in how the call chain of Instance functions are implemented
354versus the call chain of a Device functions. Remember, a [Vulkan Instance is a
355high-level construct used to provide Vulkan system-level information](#instance-
356related-objects). Because of this, Instance functions need to be broadcasted to
357every available ICD on the system. The following diagram shows an approximate
358view of an Instance call chain with 3 enabled layers:
359
360![Instance Call Chain](./images/loader_instance_chain.png)
361
362This is also how a Vulkan Device function call chain looks if you query it
363using `vkGetInstanceProcAddr`. On the otherhand, a Device
364function doesn't need to worry about the broadcast becuase it knows specifically
365which associated ICD and which associated Physical Device the call should
366terminate at. Because of this, the loader doesn't need to get involved between
367any enabled layers and the ICD. Thus, if you used a loader-exported Vulkan
368Device function, the call chain in the same scenario as above would look like:
369
370![Loader Device Call Chain](./images/loader_device_chain_loader.png)
371
372An even better solution would be for an application to perform a
373`vkGetDeviceProcAddr` call on all Device functions. This further optimizes the
374call chain by removing the loader all-together under most scenarios:
375
376![Application Device Call Chain](./images/loader_device_chain_app.png)
377
378Also, notice if no layers are enabled, your application function pointer would
379point **directly to the ICD**. If called enough, those fewer calls can add up
380to performance savings.
381
382**NOTE:** There are some Device functions which still require the loader to
383intercept them with a *trampoline* and *terminator*. There are very few of
384these, but they are typically functions which the loader wraps with its own
385data. In those cases, even the Device call chain will continue to look like the
386Instance call chain. One example of a Device function requiring a *terminator*
387is `vkCreateSwapchainKHR`. For that function, the loader needs to potentially
388convert the KHR_surface object into an ICD-specific KHR_surface object prior to
389passing down the rest of the function's information to the ICD.
390
391Remember:
392 * `vkGetInstanceProcAddr` can be used to query
393either device or instance entry-points in addition to all core entry-points.
394 * `vkGetDeviceProcAddr` can only be used to query for device
395extension or core device entry-points.
396
397
398##### ABI Versioning
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700399The Vulkan loader library will be distributed in various ways including Vulkan
Mark Young39389872017-01-19 21:10:49 -0700400SDKs, OS package distributions and Independent Hardware Vendor (IHV) driver
401packages. These details are beyond the scope of this document. However, the name
402and versioning of the Vulkan loader library is specified so an app can link to
403the correct Vulkan ABI library version. Vulkan versioning is such that ABI
404backwards compatibility is guaranteed for all versions with the same major
405number (e.g. 1.0 and 1.1). On Windows, the loader library encodes the ABI
406version in its name such that multiple ABI incompatible versions of the loader
407can peacefully coexist on a given system. The Vulkan loader library file name is
Jeff Juliano18e50202017-01-31 16:36:18 -0500408`vulkan-<ABI version>.dll`. For example, for Vulkan version 1.X on Windows the
Mark Young39389872017-01-19 21:10:49 -0700409library filename is vulkan-1.dll. And this library file can typically be found
410in the windows/system32 directory (on 64-bit Windows installs, the 32-bit
411version of the loader with the same name can be found in the windows/sysWOW64
412directory).
Jon Ashburnc2972682016-02-08 15:42:01 -0700413
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700414For Linux, shared libraries are versioned based on a suffix. Thus, the ABI
415number is not encoded in the base of the library filename as on Windows. On
416Linux an application wanting to link to the latest Vulkan ABI version would
417just link to the name vulkan (libvulkan.so). A specific Vulkan ABI version can
Jeff Julianof1619872016-02-17 17:25:42 -0500418also be linked to by applications (e.g. libvulkan.so.1).
Jon Ashburnc2972682016-02-08 15:42:01 -0700419
Mark Young39389872017-01-19 21:10:49 -0700420
421#### Application Layer Usage
Mark Young6d026a72016-06-01 17:49:30 -0600422
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700423Applications desiring Vulkan functionality beyond what the core API offers may
Mark Young39389872017-01-19 21:10:49 -0700424use various layers or extensions. A layer cannot introduce new Vulkan core API
425entry-points to an application that are not exposed in Vulkan.h. However,
426layers may offer extensions that introduce new Vulkan commands that can be
427queried through the extension interface.
Mark Young02ee5382016-07-22 08:51:05 -0600428
Mark Young39389872017-01-19 21:10:49 -0700429A common use of layers is for API validation which can be enabled by
430loading the layer during application development, but not loading the layer
431for application release. This eliminates the overhead of validating the
432application's usage of the API, something that wasn't available on some previous
433graphics APIs.
434
435To find out what layers are available to your application, use
436`vkEnumerateInstanceLayerProperties`. This will report all layers
437that have been discovered by the loader. The loader looks in various locations
438to find layers on the system. For more information see the
439[Layer discovery](#layer-discovery) section below.
440
441To enable a layer, or layers, simply pass the name of the layers you wish to
442enable in the `ppEnabledLayerNames` field of the `VkInstanceCreateInfo` during
443a call to `vkCreateInstance`. Once done, the layers you have enabled will be
444active for all Vulkan functions using the created `VkInstance`, and any of
445its child objects.
446
447**NOTE:** Layer ordering is important in several cases since some layers
448interact with each other. Be careful when enabling layers as this may be
449the case. See the [Overall Layer Ordering](#overall-layer-ordering) section
450for more information.
451
452The following code section shows how you would go about enabling the
453VK_LAYER_LUNARG_standard_validation layer.
454
455```
456 char *instance_validation_layers[] = {
457 "VK_LAYER_LUNARG_standard_validation"
458 };
459 const VkApplicationInfo app = {
460 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
461 .pNext = NULL,
462 .pApplicationName = "TEST_APP",
463 .applicationVersion = 0,
464 .pEngineName = "TEST_ENGINE",
465 .engineVersion = 0,
466 .apiVersion = VK_API_VERSION_1_0,
467 };
468 VkInstanceCreateInfo inst_info = {
469 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
470 .pNext = NULL,
471 .pApplicationInfo = &app,
472 .enabledLayerCount = 1,
473 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
474 .enabledExtensionCount = 0,
475 .ppEnabledExtensionNames = NULL,
476 };
477 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
478```
479
480At `vkCreateInstance` and `vkCreateDevice`, the loader constructs call chains
481that include the application specified (enabled) layers. Order is important in
482the `ppEnabledLayerNames` array; array element 0 is the topmost (closest to the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700483application) layer inserted in the chain and the last array element is closest
Mark Young39389872017-01-19 21:10:49 -0700484to the driver. See the [Overall Layer Ordering](#overall-layer-ordering)
485section for more information on layer ordering.
Jon Ashburnc2972682016-02-08 15:42:01 -0700486
Mark Young39389872017-01-19 21:10:49 -0700487**NOTE:** *Device Layers Are Now Deprecated*
488> `vkCreateDevice` originally was able to select layers in a similar manner to
489`vkCreateInstance`. This lead to the concept of "instance
490> layers" and "device layers". It was decided by Khronos to deprecate the
491> "device layer" functionality and only consider "instance layers".
492> Therefore, `vkCreateDevice` will use the layers specified at
493`vkCreateInstance`.
494> Because of this, the following items have been deprecated:
495> * `VkDeviceCreateInfo` fields:
496> * `ppEnabledLayerNames`
497> * `enabledLayerCount`
498> * The `vkEnumerateDeviceLayerProperties` function
Mark Young02ee5382016-07-22 08:51:05 -0600499
Jon Ashburnc2972682016-02-08 15:42:01 -0700500
Mark Young39389872017-01-19 21:10:49 -0700501##### Implicit vs Explicit Layers
Jon Ashburnc2972682016-02-08 15:42:01 -0700502
Mark Young39389872017-01-19 21:10:49 -0700503Explicit layers are layers which are enabled by an application (e.g. with the
504vkCreateInstance function), or by an environment variable (as mentioned
505previously).
Jon Ashburnc2972682016-02-08 15:42:01 -0700506
Mark Young39389872017-01-19 21:10:49 -0700507Implicit layers are those which are enabled by their existence. For example,
508certain application environments (e.g. Steam or an automotive infotainment
509system) may have layers which they always want enabled for all applications
510that they start. Other implicit layers may be for all applications started on a
511given system (e.g. layers that overlay frames-per-second). Implicit layers are
512enabled automatically, whereas explicit layers must be enabled explicitly.
Jon Ashburnc2972682016-02-08 15:42:01 -0700513
Mark Young02ee5382016-07-22 08:51:05 -0600514Implicit layers have an additional requirement over explicit layers in that they
515require being able to be disabled by an environmental variable. This is due
516to the fact that they are not visible to the application and could cause issues.
517A good principle to keep in mind would be to define both an enable and disable
518environment variable so the users can deterministicly enable the functionality.
519On Desktop platforms (Windows and Linux), these enable/disable settings are
520defined in the layer's JSON file.
521
Mark Young39389872017-01-19 21:10:49 -0700522Discovery of system-installed implicit and explicit layers is described later in
523the [Layer Discovery Section](#layer-discovery). For now, simply know that what
524distinguishes a layer as implicit or explicit is dependent on the Operating
525system, as shown in the table below.
526
527| Operating System | Implicit Layer Identification |
528|----------------|--------------------|
529| Windows | Implicit Layers are located in a different Windows registry location than Explicit Layers. |
530| Linux | Implicit Layers are located in a different directory location than Explicit Layers. |
531| Android | There is **No Support For Implicit Layers** on Android. |
532
533
534##### Forcing Layer Source Folders
535
536Developers may need to use special, pre-production layers, without modifying the
537system-installed layers. You can direct the loader to look for layers in a
538specific folder by defining the "VK\_LAYER\_PATH" environment variable. This
539will override the mechanism used for finding system-installed layers. Because
Mark Young18bbaab2017-03-20 08:27:14 -0600540layers of interest may exist in several disinct folders on a system, this
Mark Young39389872017-01-19 21:10:49 -0700541environment variable can containis several paths seperated by the operating
542specific path separator. On Windows, each separate folder should be separated
543in the list using a semi-colon. On Linux, each folder name should be separated
544using a colon.
545
546If "VK\_LAYER\_PATH" exists, **only** the folders listed in it will be scanned
547for layers. Each directory listed should be the full pathname of a folder
548containing layer manifest files.
549
550
551##### Forcing Layers to be Enabled on Windows and Linux
552
553Developers may want to enable layers that are not enabled by the given
554application they are using. On Linux and Windows, the environment variable
555"VK\_INSTANCE\_LAYERS" can be used to enable additional layers which are
556not specified (enabled) by the application at `vkCreateInstance`.
557"VK\_INSTANCE\_LAYERS" is a colon (Linux)/semi-colon (Windows) separated
558list of layer names to enable. Order is relevant with the first layer in the
559list being the top-most layer (closest to the application) and the last
560layer in the list being the bottom-most layer (closest to the driver).
561See the [Overall Layer Ordering](#overall-layer-ordering) section
562for more information.
563
564Application specified layers and user specified layers (via environment
565variables) are aggregated and duplicates removed by the loader when enabling
566layers. Layers specified via environment variable are top-most (closest to the
567application) while layers specified by the application are bottommost.
568
569An example of using these environment variables to activate the validation
570layer `VK_LAYER_LUNARG_parameter_validation` on Windows or Linux is as follows:
571
572```
573> $ export VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_parameter_validation
574```
575
576
577##### Overall Layer Ordering
578
579The overall ordering of all layers by the loader based on the above looks
580as follows:
581
582![Loader Layer Ordering](./images/loader_layer_order.png)
583
584Ordering may also be important internal to the list of Explicit Layers.
585Some layers may be dependent on other behavior being implemented before
586or after the loader calls it. For example: the VK_LAYER_LUNARG_core_validation
587layer expects the VK_LAYER_LUNARG_parameter_validation to be called first.
588This is because the VK_LAYER_LUNARG_parameter_validation will filter out any
589invalid `NULL` pointer calls prior to the rest of the validation checking
590done by VK_LAYER_LUNARG_core_validation. If not done properly, you may see
591crashes in the VK_LAYER_LUNARG_core_validation layer that would otherwise be
592avoided.
593
594
595#### Application Usage of Extensions
596
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700597Extensions are optional functionality provided by a layer, the loader or an
598ICD. Extensions can modify the behavior of the Vulkan API and need to be
Mark Young39389872017-01-19 21:10:49 -0700599specified and registered with Khronos. These extensions can be created
600by an Independent Hardware Vendor (IHV) to expose new hardware functionality,
601or by a layer writer to expose some internal feature, or by the loader to
602improve functional behavior. Information about various extensions can be
603found in the Vulkan Spec, and vulkan.h header file.
Jon Ashburnc2972682016-02-08 15:42:01 -0700604
Mark Young6d026a72016-06-01 17:49:30 -0600605
Mark Young39389872017-01-19 21:10:49 -0700606##### Instance and Device Extensions
607
608As hinted at in the [Instance Versus Device](#instance-versus-device) section,
609there are really two types of extensions:
610 * Instance Extensions
611 * Device Extensions
612
613An Instance extension is an extension which modifies existing behavior or
614implements new behavior on instance-level objects, like a `VkInstance` or
615a `VkPhysicalDevice`. A Device extension is an extension which does the same,
616but for any `VkDevice` object, or any dispatchable object that is a child of a
617`VkDevice` (`VkQueue` and `VkCommandBuffer` are examples of these).
618
619It is **very** important to know what type of extension you are desiring to
620enable as you will enable Instance extensions during `vkCreateInstance` and
621Device extensions during `vkCreateDevice`.
622
623The loader discovers and aggregates all
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700624extensions from layers (both explicit and implicit), ICDs and the loader before
Mark Young39389872017-01-19 21:10:49 -0700625reporting them to the application in `vkEnumerateXXXExtensionProperties`
626(where XXX is either "Instance" or "Device").
627 - Instance extensions are discovered via
628`vkEnumerateInstanceExtensionProperties`.
629 - Device extensions are be discovered via
630`vkEnumerateDeviceExtensionProperties`.
631
632Looking at `vulkan.h`, you'll notice that they are both similar. For example,
633`vkEnumerateInstanceExtensionProperties` prototype looks as follows:
634
635```
636 VkResult
637 vkEnumerateInstanceExtensionProperties(const char *pLayerName,
638 uint32_t *pPropertyCount,
639 VkExtensionProperties *pProperties);
640```
641
642The "pLayerName" parameter in these functions is used to select either a single
643layer or the Vulkan platform implementation. If "pLayerName" is NULL, extensions
644from Vulkan implementation components (including loader, implicit layers, and
645ICDs) are enumerated. If "pLayerName" is equal to a discovered layer module name
646then only extensions from that layer (which may be implicit or explicit) are
Jeff Julianof1619872016-02-17 17:25:42 -0500647enumerated. Duplicate extensions (e.g. an implicit layer and ICD might report
Mark Young39389872017-01-19 21:10:49 -0700648support for the same extension) are eliminated by the loader. For duplicates,
649the ICD version is reported and the layer version is culled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700650
Mark Young39389872017-01-19 21:10:49 -0700651Also, Extensions *must be enabled* (in `vkCreateInstance` or `vkCreateDevice`)
652before the functions associated with the extensions can be used. If you get an
653Extension function using either `vkGetInstanceProcAddr` or
654`vkGetDeviceProcAddr`, but fail to enable it, you could experience undefined
655behavior. This should actually be flagged if you run with Validation layers
656enabled.
Jon Ashburnc2972682016-02-08 15:42:01 -0700657
Courtney Goeltzenleuchterab3a4662016-02-14 10:48:22 -0700658
Mark Young78f88c82016-07-19 11:49:45 -0600659##### WSI Extensions
660
Mark Young39389872017-01-19 21:10:49 -0700661Khronos approved WSI extensions are available and provide Windows System
662Integration support for various execution environments. It is important to
663understand that some WSI extensions are valid for all targets, but others are
664particular to a given execution environment (and loader). This desktop loader
665(currently targeting Windows and Linux) only enables and directly exports those
666WSI extensions that are appropriate to the current environment. For the most
667part, the selection is done in the loader using compile-time preprocessor flags.
668All versions of the desktop loader currently expose at least the following WSI
Mark Young78f88c82016-07-19 11:49:45 -0600669extension support:
670- VK_KHR_surface
671- VK_KHR_swapchain
672- VK_KHR_display
673
Mark Young39389872017-01-19 21:10:49 -0700674In addition, each of the following OS targets for the loader support target-
675specific extensions:
Mark Young78f88c82016-07-19 11:49:45 -0600676
Mark Young39389872017-01-19 21:10:49 -0700677| Windowing System | Extensions available |
678|----------------|--------------------|
679| Windows | VK_KHR_win32_surface |
680| Linux (Default) | VK_KHR_xcb_surface and VK_KHR_xlib_surface |
681| Linux (Wayland) | VK_KHR_wayland_surface |
682| Linux (Mir) | VK_KHR_mir_surface |
Mark Young78f88c82016-07-19 11:49:45 -0600683
Mark Young39389872017-01-19 21:10:49 -0700684**NOTE:** Wayland and Mir targets are not fully supported at this time. Wayland
685support is present, but should be considered Beta quality. Mir support is not
686completely implemented at this time.
687
688It is important to understand that while the loader may support the various
689entry-points for these extensions, there is a hand-shake required to actually
690use them:
Mark Young78f88c82016-07-19 11:49:45 -0600691* At least one physical device must support the extension(s)
692* The application must select such a physical device
Mark Young39389872017-01-19 21:10:49 -0700693* The application must request the extension(s) be enabled while creating the
694instance or logical device (This depends on whether or not the given extension
695works with an instance or a device).
Mark Young78f88c82016-07-19 11:49:45 -0600696* The instance and/or logical device creation must succeed.
697
698Only then can you expect to properly use a WSI extension in your Vulkan program.
699
Mark Young78f88c82016-07-19 11:49:45 -0600700
Mark Young39389872017-01-19 21:10:49 -0700701##### Unknown Extensions
702
703With the ability to expand Vulkan so easily, extensions will be created that the
704loader knows nothing about. If the extension is a device extension, the loader
705will pass the unknown entry-point down the device call chain ending with the
706appropriate ICD entry-points. The same thing will happen, if the extension is
707an instance extension which takes a physical device paramater as it's first
708component. However, for all other instance extensions the loader will fail to
709load it.
Mark Young78f88c82016-07-19 11:49:45 -0600710
711*But why doesn't the loader support unknown instance extensions?*
712<br/>
713Let's look again at the Instance call chain:
Mark Young78f88c82016-07-19 11:49:45 -0600714
Mark Young39389872017-01-19 21:10:49 -0700715![Instance call chain](./images/loader_instance_chain.png)
Mark Young78f88c82016-07-19 11:49:45 -0600716
Mark Young39389872017-01-19 21:10:49 -0700717Notice that for a normal instance function call, the loader has to handle
718passing along the function call to the available ICDs. If the loader has no
719idea of the parameters or return value of the instance call, it can't properly
720pass information along to the ICDs. There may be ways to do this, which will be
721explored in the future. However, for now, this loader does not support
722instance extensions which don't take a physical device as their first parameter.
723
724Because the device call-chain does not normally pass through the loader
725*terminator*, this is not a problem for device extensions. Additionally,
726since a physical device is associated with one ICD, we can use a generic
727*terminator* pointing to one ICD. This is because both of these extensions
728terminate directly in the ICD they are associated with.
Mark Young78f88c82016-07-19 11:49:45 -0600729
730*Is this a big problem?*
731<br/>
Mark Young39389872017-01-19 21:10:49 -0700732No! Most extension functionality only affects either a physical or logical
733device and not an instance. Thus, the overwhelming majority of extensions
734should be supported with direct loader support.
Jon Ashburnc2972682016-02-08 15:42:01 -0700735
Mark Young6340bb82017-02-13 15:39:22 -0700736##### Filtering Out Unknown Instance Extension Names
737In some cases, an ICD may support instance extensions that the loader does not.
738For the above reasons, the loader will filter out the names of these unknown instance
739extensions when an application calls `vkEnumerateInstanceExtensionProperties`.
740Additionally, this behavior will cause the loader to throw an error during
741`vkCreateInstance` if you still attempt to use one of these extensions. The intent is
742to protect applications so that they don't inadvertantly use functionality
743which could lead to a crash.
744
745On the other-hand, if you know you can safely use the extension, you may disable
746the filtering by defining the environment variable `VK_LOADER_DISABLE_INST_EXT_FILTER`
747and setting the value to a non-zero number. This will effectively disable the
748loader's filtering out of instance extension names.
Jon Ashburnc2972682016-02-08 15:42:01 -0700749
Mark Youngcb6e6702016-07-20 11:38:53 -0600750<br/>
Mark Youngcb6e6702016-07-20 11:38:53 -0600751<br/>
752
Mark Young39389872017-01-19 21:10:49 -0700753## Loader and Layer Interface
Jon Ashburnc2972682016-02-08 15:42:01 -0700754
Mark Young39389872017-01-19 21:10:49 -0700755In this section we'll discuss how the loader interacts with layers, including:
Lenny Komowde3924a2017-05-04 14:50:01 -0600756 * [Layer Discovery](#layer-discovery)
757 * [Layer Manifest File Usage](#layer-manifest-file-usage)
758 * [Android Layer Discovery](#android-layer-discovery)
759 * [Windows Layer Discovery](#windows-layer-discovery)
760 * [Linux Layer Discovery](#linux-layer-discovery)
761 * [Layer Version Negotiation](#layer-version-negotiation)
762 * [Layer Call Chains and Distributed Dispatch](#layer-call-chains-and-distributed-dispatch)
763 * [Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-extensions)
764 * [Layer Intercept Requirements](#layer-intercept-requirements)
765 * [Distributed Dispatching Requirements](#distributed-dispatching-requirements)
766 * [Layer Conventions and Rules](#layer-conventions-and-rules)
767 * [Layer Dispatch Initialization](#layer-dispatch-initialization)
768 * [Example Code for CreateInstance](#example-code-for-createinstance)
769 * [Example Code for CreateDevice](#example-code-for-createdevice)
Mark Youngc82a0622017-05-05 11:17:17 -0600770 * [Meta-layers](#meta-layers)
Lenny Komowde3924a2017-05-04 14:50:01 -0600771 * [Special Considerations](#special-considerations)
772 * [Associating Private Data with Vulkan Objects Within a Layer](#associating-private-data-with-vulkan-objects-within-a-layer)
773 * [Wrapping](#wrapping)
774 * [Hash Maps](#hash-maps)
775 * [Creating New Dispatchable Objects](#creating-new-dispatchable-objects)
776 * [Layer Manifest File Format](#layer-manifest-file-format)
777 * [Layer Manifest File Version History](#layer-manifest-file-version-history)
778 * [Layer Library Versions](#layer-library-versions)
779 * [Layer Library API Version 2](#layer-library-api-version-2)
780 * [Layer Library API Version 1](#layer-library-api-version-1)
781 * [Layer Library API Version 0](#layer-library-api-version-0)
Mark Young39389872017-01-19 21:10:49 -0700782
Jon Ashburnc2972682016-02-08 15:42:01 -0700783
Mark Young39389872017-01-19 21:10:49 -0700784
785#### Layer Discovery
Jon Ashburnc2972682016-02-08 15:42:01 -0700786
Mark Young39389872017-01-19 21:10:49 -0700787As mentioned in the
788[Application Interface section](#implicit-vs-explicit-layers),
789layers can be categorized into two categories:
790 * Implicit Layers
791 * Explicit Layers
Jon Ashburnc2972682016-02-08 15:42:01 -0700792
Mark Young39389872017-01-19 21:10:49 -0700793The main difference between the two is that Implicit Layers are automatically
794enabled, unless overriden, and Explicit Layers must be enabled. Remember,
795Implicit Layers are not present on all Operating Systems (like Android).
Jon Ashburnc2972682016-02-08 15:42:01 -0700796
Mark Young39389872017-01-19 21:10:49 -0700797On any system, the loader looks in specific areas for information on the
798layers that it can load at a user's request. The process of finding the
799available layers on a system is known as Layer Discovery. During discovery,
800the loader determines what layers are available, the layer name, the layer
801version, and any extensions supported by the layer. This information is
802provided back to an application through `vkEnumerateInstanceLayerProperties`.
803
804The group of layers available to the loader is known as a layer library. This
805section defines an extensible interface to discover what layers are contained in
806the layer library.
807
808This section also specifies the minimal conventions and rules a layer must
809follow, especially with regards to interacting with the loader and other layers.
810
811##### Layer Manifest File Usage
812
Mark Young18bbaab2017-03-20 08:27:14 -0600813On Windows and Linux systems, JSON formatted manifest files are used to store
Mark Young39389872017-01-19 21:10:49 -0700814layer information. In order to find system-installed layers, the Vulkan loader
815will read the JSON files to identify the names and attributes of layers and
816their extensions. The use of manifest files allows the loader to avoid loading
817any shared library files when the application does not query nor request any
818extensions. The format of [Layer Manifest File](#layer-manifest-file-format)
819is detailed below.
820
821The Android loader does not use manifest files. Instead, the loader queries the
822layer properties using special functions known as "introspection" functions.
823The intent of these functions is to determine the same required information
824gathered from reading the manifest files. These introspection functions are
825not used by the desktop loader but should be present in layers to maintain
826consistency. The specific "introspection" functions are called out in
827the [Layer Manifest File Format](#layer-manifest-file-format) table.
828
829
830##### Android Layer Discovery
831
832On Android, the loader looks for layers to enumerate in the
833/data/local/debug/vulkan folder. An application enabled for debug has the
834ability to enumerate and enable any layers in that location.
835
836
837##### Windows Layer Discovery
838
839In order to find system-installed layers, the Vulkan loader will scan the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700840values in the following Windows registry keys:
Jon Ashburnc2972682016-02-08 15:42:01 -0700841
Mark Young39389872017-01-19 21:10:49 -0700842```
843 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\ExplicitLayers
Lenny Komowdfa37352017-03-02 11:29:03 -0700844 HKEY_CURRENT_USER\SOFTWARE\Khronos\Vulkan\ExplicitLayers
Mark Young39389872017-01-19 21:10:49 -0700845 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\ImplicitLayers
Lenny Komowdfa37352017-03-02 11:29:03 -0700846 HKEY_CURRENT_USER\SOFTWARE\Khronos\Vulkan\ImplicitLayers
Mark Young39389872017-01-19 21:10:49 -0700847```
Jon Ashburnc2972682016-02-08 15:42:01 -0700848
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700849For each value in these keys which has DWORD data set to 0, the loader opens
850the JSON manifest file specified by the name of the value. Each name must be a
Mark Young39389872017-01-19 21:10:49 -0700851full pathname to the manifest file. The Vulkan loader will open each info file
852to obtain information about the layer, including the name or pathname of a
853shared library (".dll") file. However, if VK\_LAYER\_PATH is defined, then the
854loader will instead look at the paths defined by that variable instead of using
855the information provided by these registry keys. See
856[Forcing Layer Source Folders](#forcing-layer-source-folders) for more
857information on this.
Jon Ashburnc2972682016-02-08 15:42:01 -0700858
Jon Ashburnc2972682016-02-08 15:42:01 -0700859
Mark Young39389872017-01-19 21:10:49 -0700860##### Linux Layer Discovery
Jon Ashburnc2972682016-02-08 15:42:01 -0700861
Mark Young39389872017-01-19 21:10:49 -0700862On Linux, the Vulkan loader will scan the files in the following Linux
863directories:
Jon Ashburnc2972682016-02-08 15:42:01 -0700864
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600865 /usr/local/etc/vulkan/explicit_layer.d
866 /usr/local/etc/vulkan/implicit_layer.d
867 /usr/local/share/vulkan/explicit_layer.d
868 /usr/local/share/vulkan/implicit_layer.d
869 /etc/vulkan/explicit_layer.d
870 /etc/vulkan/implicit_layer.d
871 /usr/share/vulkan/explicit_layer.d
872 /usr/share/vulkan/implicit_layer.d
873 $HOME/.local/share/vulkan/explicit_layer.d
874 $HOME/.local/share/vulkan/implicit_layer.d
875
Mark Young39389872017-01-19 21:10:49 -0700876Of course, ther are some things you have to know about the above folders:
877 1. The "/usr/local/*" directories can be configured to be other directories at
878build time.
879 2. $HOME is the current home directory of the application's user id; this path
880will be ignored for suid programs.
881 3. The "/usr/local/etc/vulkan/\*\_layer.d" and
882"/usr/local/share/vulkan/\*\_layer.d" directories are for layers that are
883installed from locally-built sources.
884 4. The "/usr/share/vulkan/\*\_layer.d" directories are for layers that are
Karl Schultz1f58d7e2016-10-31 15:58:21 -0600885installed from Linux-distribution-provided packages.
886
Mark Young39389872017-01-19 21:10:49 -0700887As on Windows, if VK\_LAYER\_PATH is defined, then the
888loader will instead look at the paths defined by that variable instead of using
889the information provided by these default paths. However, these
890environment variables are only used for non-suid programs. See
891[Forcing Layer Source Folders](#forcing-layer-source-folders) for more
892information on this.
Jon Ashburnc2972682016-02-08 15:42:01 -0700893
Mark Young39389872017-01-19 21:10:49 -0700894
895#### Layer Version Negotiation
896
897Now that a layer has been discovered, an application can choose to load it (or
898it is loaded by default if it is an Implicit layer). When the loader attempts
899to load the layer, the first thing it does is attempt to negotiate the version
900of the loader to layer interface. In order to negotiate the loader/layer
901interface version, the layer must implement the
902`vkNegotiateLoaderLayerInterfaceVersion` function. The following information is
903provided for this interface in include/vulkan/vk_layer.h:
904
905```cpp
906 typedef enum VkNegotiateLayerStructType {
907 LAYER_NEGOTIATE_INTERFACE_STRUCT = 1,
908 } VkNegotiateLayerStructType;
909
910 typedef struct VkNegotiateLayerInterface {
911 VkNegotiateLayerStructType sType;
912 void *pNext;
913 uint32_t loaderLayerInterfaceVersion;
914 PFN_vkGetInstanceProcAddr pfnGetInstanceProcAddr;
915 PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr;
916 PFN_GetPhysicalDeviceProcAddr pfnGetPhysicalDeviceProcAddr;
917 } VkNegotiateLayerInterface;
918
919 VkResult vkNegotiateLoaderLayerInterfaceVersion(
920 VkNegotiateLayerInterface *pVersionStruct);
Jon Ashburncc300a22016-02-11 14:57:30 -0700921```
Jon Ashburnc2972682016-02-08 15:42:01 -0700922
Mark Young39389872017-01-19 21:10:49 -0700923You'll notice the `VkNegotiateLayerInterface` structure is similar to other
924Vulkan structures. The "sType" field, in this case takes a new enum defined
925just for internal loader/layer interfacing use. The valid values for "sType"
926could grow in the future, but right only havs the one value
927"LAYER_NEGOTIATE_INTERFACE_STRUCT".
Jon Ashburnc2972682016-02-08 15:42:01 -0700928
Mark Young39389872017-01-19 21:10:49 -0700929This function (`vkNegotiateLoaderLayerInterfaceVersion`) should be exported by
930the layer so that using "GetProcAddress" on Windows or "dlsym" on Linux, should
931return a valid function pointer to it. Once the loader has grabbed a valid
932address to the layers function, the loader will create a variable of type
933`VkNegotiateLayerInterface` and initialize it in the following ways:
934 1. Set the structure "sType" to "LAYER_NEGOTIATE_INTERFACE_STRUCT"
935 2. Set pNext to NULL.
936 - This is for future growth
937 3. Set "loaderLayerInterfaceVersion" to the current version the loader desires
938to set the interface to.
939 - The minimum value sent by the loader will be 2 since it is the first
940version supporting this function.
Jon Ashburnc2972682016-02-08 15:42:01 -0700941
Mark Young39389872017-01-19 21:10:49 -0700942The loader will then individually call each layer’s
943`vkNegotiateLoaderLayerInterfaceVersion` function with the filled out
944“VkNegotiateLayerInterface”. The layer will either accept the loader's version
945set in "loaderLayerInterfaceVersion", or modify it to the closest value version
946of the interface that the layer can support. The value should not be higher
947than the version requested by the loader. If the layer can't support at a
948minimum the version requested, then the layer should return an error like
949"VK_ERROR_INITIALIZATION_FAILED". If a layer can support some version, then
950the layer should do the following:
951 1. Adjust the version to the layer's desired version.
952 2. The layer should fill in the function pointer values to its internal
953functions:
954 - "pfnGetInstanceProcAddr" should be set to the layer’s internal
955`GetInstanceProcAddr` function.
956 - "pfnGetDeviceProcAddr" should be set to the layer’s internal
957`GetDeviceProcAddr` function.
958 - "pfnGetPhysicalDeviceProcAddr" should be set to the layer’s internal
959`GetPhysicalDeviceProcAddr` function.
960 - If the layer supports no physical device extensions, it may set the
961value to NULL.
962 - More on this function later
963 3. The layer should return "VK_SUCCESS"
Jon Ashburnc2972682016-02-08 15:42:01 -0700964
Mark Young39389872017-01-19 21:10:49 -0700965This function **SHOULD NOT CALL DOWN** the layer chain to the next layer.
966The loader will work with each layer individually.
Jon Ashburnc2972682016-02-08 15:42:01 -0700967
Mark Young39389872017-01-19 21:10:49 -0700968If the layer supports the new interface and reports version 2 or greater, then
969the loader will use the “fpGetInstanceProcAddr” and “fpGetDeviceProcAddr”
970functions from the “VkNegotiateLayerInterface” structure. Prior to these
971changes, the loader would query each of those functions using "GetProcAddress"
972on Windows or "dlsym" on Linux.
Jon Ashburnc2972682016-02-08 15:42:01 -0700973
Jon Ashburnc2972682016-02-08 15:42:01 -0700974
Mark Young39389872017-01-19 21:10:49 -0700975#### Layer Call Chains and Distributed Dispatch
Jon Ashburnc2972682016-02-08 15:42:01 -0700976
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700977There are two key architectural features that drive the loader to layer library
Mark Young39389872017-01-19 21:10:49 -0700978interface:
979 1. Separate and distinct instance and device call chains
980 2. Distributed dispatch.
Jon Ashburnc2972682016-02-08 15:42:01 -0700981
Mark Young39389872017-01-19 21:10:49 -0700982You can read an overview of dispatch tables and call chains above in the
983[Dispatch Tables and Call Chains](#dispatch-tables-and-call-chains) section.
Jon Ashburnc2972682016-02-08 15:42:01 -0700984
Mark Young39389872017-01-19 21:10:49 -0700985What's important to note here is that a layer can intercept Vulkan
986instance functions, device functions or both. For a layer to intercept instance
987functions, it must participate in the instance call chain. For a layer to
988intercept device functions, it must participate in the device call chain.
989
990Remember, a layer does not need to intercept all instance or device functions,
991instead, it can choose to intercept only a subset of those functions.
992
993Normally, when a layer intercepts a given Vulkan function, it will call down the
994instance or device call chain as needed. The loader and all layer libraries that
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -0700995participate in a call chain cooperate to ensure the correct sequencing of calls
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -0700996from one entity to the next. This group effort for call chain sequencing is
Mark Young39389872017-01-19 21:10:49 -0700997hereinafter referred to as **distributed dispatch**.
Jon Ashburnc2972682016-02-08 15:42:01 -0700998
Mark Young39389872017-01-19 21:10:49 -0700999In distributed dispatch each layer is responsible for properly calling the next
1000entity in the call chain. This means that a dispatch mechanism is required for
1001all Vulkan functions that a layer intercepts. If a Vulkan function is not
1002intercepted by a layer, or if a layer chooses to terminate the function by not
1003calling down the chain, then no dispatch is needed for that particular function.
Jeff Julianof1619872016-02-17 17:25:42 -05001004
Mark Young39389872017-01-19 21:10:49 -07001005For example, if the enabled layers intercepted only certain instance functions,
1006the call chain would look as follows:
1007![Instance Function Chain](./images/function_instance_chain.png)
Jon Ashburnc2972682016-02-08 15:42:01 -07001008
Mark Young39389872017-01-19 21:10:49 -07001009Likewise, if the enabled layers intercepted only a few of the device functions,
1010the call chain could look this way:
1011![Device Function Chain](./images/function_device_chain.png)
Jeff Julianof1619872016-02-17 17:25:42 -05001012
Mark Young39389872017-01-19 21:10:49 -07001013The loader is responsible for dispatching all core and instance extension Vulkan
1014functions to the first entity in the call chain.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001015
Chia-I Wucb24fec2016-04-20 06:23:24 +08001016
Mark Young39389872017-01-19 21:10:49 -07001017#### Layer Unknown Physical Device Extensions
1018
1019Originally, if the loader was called with `vkGetInstanceProcAddr`, it would
1020result in the following behavior:
1021 1. The loader would check if core function:
1022 - If it was, it would return the function pointer
1023 2. The loader would check if known extension function:
1024 - If it was, it would return the function pointer
1025 3. If the loader knew nothing about it, it would call down using
1026`GetInstanceProcAddr`
1027 - If it returned non-NULL, treat it as an unknown logical device command.
1028 - This meant setting up a generic trampoline function that takes in a
1029VkDevice as the first parameter and adjusting the dispatch table to call the
1030ICD/Layers function after getting the dispatch table from the VkDevice.
1031 4. If all the above failed, the loader would return NULL to the application.
1032
1033This caused problems when a layer attempted to expose new physical device
1034extensions the loader knew nothing about, but an application did. Because the
1035loader knew nothing about it, the loader would get to step 3 in the above
1036process and would treat the function as an unknown logical device command. The
1037problem is, this would create a generic VkDevice trampoline function which, on
1038the first call, would attempt to dereference the VkPhysicalDevice as a VkDevice.
1039This would lead to a crash or corruption.
1040
1041In order to identify the extension entry-points specific to physical device
1042extensions, the following function can be added to a layer:
1043
1044```cpp
1045PFN_vkVoidFunction vk_layerGetPhysicalDeviceProcAddr(VkInstance instance,
1046 const char* pName);
1047```
1048
1049This function behaves similar to `vkGetInstanceProcAddr` and
1050`vkGetDeviceProcAddr` except it should only return values for physical device
1051extension entry-points. In this way, it compares "pName" to every physical
1052device function supported in the layer.
1053
1054The following rules apply:
Lenny Komowde3924a2017-05-04 14:50:01 -06001055 * If it is the name of a physical device function supported by the layer, the
Mark Young39389872017-01-19 21:10:49 -07001056pointer to the layer's corresponding function should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06001057 * If it is the name of a valid function which is **not** a physical device
Mark Young39389872017-01-19 21:10:49 -07001058function (i.e. an Instance, Device, or other function implemented by the layer),
1059then the value of NULL should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06001060 * We don’t call down since we know the command is not a physical device
Mark Young39389872017-01-19 21:10:49 -07001061extension).
Lenny Komowde3924a2017-05-04 14:50:01 -06001062 * If the layer has no idea what this function is, it should call down the layer
Mark Young39389872017-01-19 21:10:49 -07001063chain to the next `vk_layerGetPhysicalDeviceProcAddr` call.
Lenny Komowde3924a2017-05-04 14:50:01 -06001064 * This can be retrieved in one of two ways:
1065 * During `vkCreateInstance`, it is passed to a layer in the
Mark Young39389872017-01-19 21:10:49 -07001066chain information passed to a layer in the `VkLayerInstanceCreateInfo`
1067structure.
1068 * Use `get_chain_info()` to get the pointer to the
1069`VkLayerInstanceCreateInfo` structure. Let's call it chain_info.
1070 * The address is then under
1071chain_info->u.pLayerInfo->pfnNextGetPhysicalDeviceProcAddr
1072 * See
1073[Example Code for CreateInstance](#example-code-for-createinstance)
Lenny Komowde3924a2017-05-04 14:50:01 -06001074 * Using the next layer’s `GetInstanceProcAddr` function to query for
Mark Young39389872017-01-19 21:10:49 -07001075`vk_layerGetPhysicalDeviceProcAddr`.
1076
1077This support is optional and should not be considered a requirement. This is
1078only required if a layer intends to support some functionality not directly
Mark Young18bbaab2017-03-20 08:27:14 -06001079supported by loaders released in the public. If a layer does implement this
Mark Young39389872017-01-19 21:10:49 -07001080support, it should return the address of its `vk_layerGetPhysicalDeviceProcAddr`
1081function in the "pfnGetPhysicalDeviceProcAddr" member of the
1082`VkNegotiateLayerInterface` structure during
1083[Layer Version Negotiation](#layer-version-negotiation). Additionally, the
1084layer should also make sure `vkGetInstanceProcAddr` returns a valid function
1085pointer to a query of `vk_layerGetPhysicalDeviceProcAddr`.
1086
1087The new behavior of the loader's `vkGetInstanceProcAddr` with support for the
1088`vk_layerGetPhysicalDeviceProcAddr` function is as follows:
1089 1. Check if core function:
1090 - If it is, return the function pointer
1091 2. Check if known instance or device extension function:
1092 - If it is, return the function pointer
1093 3. Call the layer/ICD `GetPhysicalDeviceProcAddr`
1094 - If it returns non-NULL, return a trampoline to a generic physical device
1095function, and setup a generic terminator which will pass it to the proper ICD.
1096 4. Call down using `GetInstanceProcAddr`
1097 - If it returns non-NULL, treat it as an unknown logical device command.
1098This means setting up a generic trampoline function that takes in a VkDevice as
1099the first parameter and adjusting the dispatch table to call the ICD/Layers
1100function after getting the dispatch table from the VkDevice. Then, return the
1101pointer to corresponding trampoline function.
1102 5. Return NULL
1103
1104You can see now, that, if the command gets promoted to core later, it will no
1105longer be setup using `vk_layerGetPhysicalDeviceProcAddr`. Additionally, if the
1106loader adds direct support for the extension, it will no longer get to step 3,
1107because step 2 will return a valid function pointer. However, the layer should
1108continue to support the command query via `vk_layerGetPhysicalDeviceProcAddr`,
1109until at least a Vulkan version bump, because an older loader may still be
1110attempting to use the commands.
1111
1112
1113#### Layer Intercept Requirements
1114
Lenny Komowde3924a2017-05-04 14:50:01 -06001115 * Layers intercept a Vulkan function by defining a C/C++ function with
Mark Young39389872017-01-19 21:10:49 -07001116signature **identical** to the Vulkan API for that function.
Lenny Komowde3924a2017-05-04 14:50:01 -06001117 * A layer **must intercept at least** `vkGetInstanceProcAddr` and
Mark Young39389872017-01-19 21:10:49 -07001118`vkCreateInstance` to participate in the instance call chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001119 * A layer **may also intercept** `vkGetDeviceProcAddr` and `vkCreateDevice`
Mark Young39389872017-01-19 21:10:49 -07001120to participate in the device call chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001121 * For any Vulkan function a layer intercepts which has a non-void return value,
Mark Young39389872017-01-19 21:10:49 -07001122**an appropriate value must be returned** by the layer intercept function.
Lenny Komowde3924a2017-05-04 14:50:01 -06001123 * Most functions a layer intercepts **should call down the chain** to the
Mark Young39389872017-01-19 21:10:49 -07001124corresponding Vulkan function in the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001125 * The common behavior for a layer is to intercept a call, perform some
Mark Young39389872017-01-19 21:10:49 -07001126behavior, then pass it down to the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001127 * If you don't pass the information down, undefined behavior may occur.
1128 * This is because the function will not be received by layers further down
Mark Young39389872017-01-19 21:10:49 -07001129the chain, or any ICDs.
Lenny Komowde3924a2017-05-04 14:50:01 -06001130 * One function that **must never call down the chain** is:
1131 * `vkNegotiateLoaderLayerInterfaceVersion`
1132 * Three common functions that **may not call down the chain** are:
1133 * `vkGetInstanceProcAddr`
1134 * `vkGetDeviceProcAddr`
1135 * `vk_layerGetPhysicalDeviceProcAddr`
1136 * These functions only call down the chain for Vulkan functions that they
Mark Young39389872017-01-19 21:10:49 -07001137do not intercept.
Lenny Komowde3924a2017-05-04 14:50:01 -06001138 * Layer intercept functions **may insert extra calls** to Vulkan functions in
Mark Young39389872017-01-19 21:10:49 -07001139addition to the intercept.
Lenny Komowde3924a2017-05-04 14:50:01 -06001140 * For example, a layer intercepting `vkQueueSubmit` may want to add a call to
Mark Young39389872017-01-19 21:10:49 -07001141`vkQueueWaitIdle` after calling down the chain for `vkQueueSubmit`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001142 * This would result in two calls down the chain: First a call down the
Mark Young39389872017-01-19 21:10:49 -07001143`vkQueueSubmit` chain, followed by a call down the `vkQueueWaitIdle` chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001144 * Any additional calls inserted by a layer must be on the same chain
1145 * If the function is a device function, only other device functions should
Mark Young39389872017-01-19 21:10:49 -07001146be added.
Lenny Komowde3924a2017-05-04 14:50:01 -06001147 * Likewise, if the function is an instance function, only other instance
Mark Young39389872017-01-19 21:10:49 -07001148functions should be added.
1149
1150
1151#### Distributed Dispatching Requirements
1152
1153- For each entry-point a layer intercepts, it must keep track of the entry
1154point residing in the next entity in the chain it will call down into.
Lenny Komowde3924a2017-05-04 14:50:01 -06001155 * In other words, the layer must have a list of pointers to functions of the
Mark Young39389872017-01-19 21:10:49 -07001156appropriate type to call into the next entity.
Lenny Komowde3924a2017-05-04 14:50:01 -06001157 * This can be implemented in various ways but
Mark Young39389872017-01-19 21:10:49 -07001158for clarity, will be referred to as a dispatch table.
1159- A layer can use the `VkLayerDispatchTable` structure as a device dispatch
1160table (see include/vulkan/vk_layer.h).
1161- A layer can use the `VkLayerInstanceDispatchTable` structure as a instance
1162dispatch table (see include/vulkan/vk_layer.h).
1163- A Layer's `vkGetInstanceProcAddr` function uses the next entity's
1164`vkGetInstanceProcAddr` to call down the chain for unknown (i.e.
1165non-intercepted) functions.
1166- A Layer's `vkGetDeviceProcAddr` function uses the next entity's
1167`vkGetDeviceProcAddr` to call down the chain for unknown (i.e. non-intercepted)
1168functions.
1169- A Layer's `vk_layerGetPhysicalDeviceProcAddr` function uses the next entity's
1170`vk_layerGetPhysicalDeviceProcAddr` to call down the chain for unknown (i.e.
1171non-intercepted) functions.
1172
1173
1174#### Layer Conventions and Rules
Chia-I Wucb24fec2016-04-20 06:23:24 +08001175
1176A layer, when inserted into an otherwise compliant Vulkan implementation, must
Mark Young39389872017-01-19 21:10:49 -07001177still result in a compliant Vulkan implementation. The intention is for layers
1178to have a well-defined baseline behavior. Therefore, it must follow some
1179conventions and rules defined below.
Chia-I Wucb24fec2016-04-20 06:23:24 +08001180
1181A layer is always chained with other layers. It must not make invalid calls
Mark Young39389872017-01-19 21:10:49 -07001182to, or rely on undefined behaviors of, its lower layers. When it changes the
1183behavior of a function, it must make sure its upper layers do not make invalid
Chia-I Wucb24fec2016-04-20 06:23:24 +08001184calls to or rely on undefined behaviors of its lower layers because of the
1185changed behavior. For example, when a layer intercepts an object creation
Mark Young39389872017-01-19 21:10:49 -07001186function to wrap the objects created by its lower layers, it must make sure its
Chia-I Wucb24fec2016-04-20 06:23:24 +08001187lower layers never see the wrapping objects, directly from itself or
1188indirectly from its upper layers.
1189
Chia-I Wub5e850e2016-05-06 08:41:52 +08001190When a layer requires host memory, it may ignore the provided allocators. It
1191should use memory allocators if the layer is intended to run in a production
Mark Young39389872017-01-19 21:10:49 -07001192environment. For example, this usually applies to implicit layers that are
1193always enabled. That will allow applications to include the layer's memory
1194usage.
Chia-I Wub5e850e2016-05-06 08:41:52 +08001195
Mark Young39389872017-01-19 21:10:49 -07001196Additional rules include:
Lenny Komowde3924a2017-05-04 14:50:01 -06001197 - `vkEnumerateInstanceLayerProperties` **must** enumerate and **only**
Mark Young39389872017-01-19 21:10:49 -07001198enumerate the layer itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001199 - `vkEnumerateInstanceExtensionProperties` **must** handle the case where
Mark Young39389872017-01-19 21:10:49 -07001200`pLayerName` is itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001201 - It **must** return `VK_ERROR_LAYER_NOT_PRESENT` otherwise, including when
Mark Young39389872017-01-19 21:10:49 -07001202`pLayerName` is `NULL`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001203 - `vkEnumerateDeviceLayerProperties` **is deprecated and may be omitted**.
1204 - Using this will result in undefined behavior.
1205 - `vkEnumerateDeviceExtensionProperties` **must** handle the case where
Mark Young39389872017-01-19 21:10:49 -07001206`pLayerName` is itself.
Lenny Komowde3924a2017-05-04 14:50:01 -06001207 - In other cases, it should normally chain to other layers.
1208 - `vkCreateInstance` **must not** generate an error for unrecognized layer
Mark Young39389872017-01-19 21:10:49 -07001209names and extension names.
Lenny Komowde3924a2017-05-04 14:50:01 -06001210 - It may assume the layer names and extension names have been validated.
1211 - `vkGetInstanceProcAddr` intercepts a Vulkan function by returning a local
Mark Young39389872017-01-19 21:10:49 -07001212entry-point
Lenny Komowde3924a2017-05-04 14:50:01 -06001213 - Otherwise it returns the value obtained by calling down the instance call
Mark Young39389872017-01-19 21:10:49 -07001214chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001215 - `vkGetDeviceProcAddr` intercepts a Vulkan function by returning a local
Mark Young39389872017-01-19 21:10:49 -07001216entry-point
Lenny Komowde3924a2017-05-04 14:50:01 -06001217 - Otherwise it returns the value obtained by calling down the device call
Mark Young39389872017-01-19 21:10:49 -07001218chain.
Lenny Komowde3924a2017-05-04 14:50:01 -06001219 - These additional functions must be intercepted if the layer implements
Mark Young39389872017-01-19 21:10:49 -07001220device-level call chaining:
Lenny Komowde3924a2017-05-04 14:50:01 -06001221 - `vkGetDeviceProcAddr`
1222 - `vkCreateDevice`(only required for any device-level chaining)
1223 - **NOTE:** older layer libraries may expect that `vkGetInstanceProcAddr`
Mark Young39389872017-01-19 21:10:49 -07001224ignore `instance` when `pName` is `vkCreateDevice`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001225 - The specification **requires** `NULL` to be returned from
Mark Young39389872017-01-19 21:10:49 -07001226`vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` for disabled functions.
Lenny Komowde3924a2017-05-04 14:50:01 -06001227 - A layer may return `NULL` itself or rely on the following layers to do so.
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001228
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001229
Mark Young39389872017-01-19 21:10:49 -07001230#### Layer Dispatch Initialization
Chia-I Wu0e9aae72016-05-19 10:45:02 +08001231
Mark Young39389872017-01-19 21:10:49 -07001232- A layer initializes its instance dispatch table within its `vkCreateInstance`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001233function.
Mark Young39389872017-01-19 21:10:49 -07001234- A layer initializes its device dispatch table within its `vkCreateDevice`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001235function.
1236- The loader passes a linked list of initialization structures to layers via
Mark Young39389872017-01-19 21:10:49 -07001237the "pNext" field in the VkInstanceCreateInfo and `VkDeviceCreateInfo`
1238structures for `vkCreateInstance` and `VkCreateDevice` respectively.
1239- The head node in this linked list is of type `VkLayerInstanceCreateInfo` for
Courtney Goeltzenleuchter42c4cdb2016-02-14 11:42:24 -07001240instance and VkLayerDeviceCreateInfo for device. See file
Mark Young39389872017-01-19 21:10:49 -07001241`include/vulkan/vk_layer.h` for details.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001242- A VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO is used by the loader for the
1243"sType" field in VkLayerInstanceCreateInfo.
1244- A VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO is used by the loader for the
1245"sType" field in VkLayerDeviceCreateInfo.
1246- The "function" field indicates how the union field "u" should be interpreted
Mark Young39389872017-01-19 21:10:49 -07001247within `VkLayer*CreateInfo`. The loader will set the "function" field to
1248VK_LAYER_LINK_INFO. This indicates "u" field should be `VkLayerInstanceLink` or
1249`VkLayerDeviceLink`.
1250- The `VkLayerInstanceLink` and `VkLayerDeviceLink` structures are the list
1251nodes.
1252- The `VkLayerInstanceLink` contains the next entity's `vkGetInstanceProcAddr`
1253used by a layer.
1254- The `VkLayerDeviceLink` contains the next entity's `vkGetInstanceProcAddr` and
1255`vkGetDeviceProcAddr` used by a layer.
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001256- Given the above structures set up by the loader, layer must initialize their
1257dispatch table as follows:
Mark Young39389872017-01-19 21:10:49 -07001258 - Find the `VkLayerInstanceCreateInfo`/`VkLayerDeviceCreateInfo` structure in
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001259the VkInstanceCreateInfo/VkDeviceCreateInfo structure.
Jon Ashburncc300a22016-02-11 14:57:30 -07001260 - Get the next entity's vkGet*ProcAddr from the "pLayerInfo" field.
Mark Young39389872017-01-19 21:10:49 -07001261 - For CreateInstance get the next entity's `vkCreateInstance` by calling the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001262"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001263 pfnNextGetInstanceProcAddr(NULL, "vkCreateInstance").
Mark Young39389872017-01-19 21:10:49 -07001264 - For CreateDevice get the next entity's `vkCreateDevice` by calling the
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001265"pfnNextGetInstanceProcAddr":
Jon Ashburnc2972682016-02-08 15:42:01 -07001266 pfnNextGetInstanceProcAddr(NULL, "vkCreateDevice").
Jon Ashburncc300a22016-02-11 14:57:30 -07001267 - Advanced the linked list to the next node: pLayerInfo = pLayerInfo->pNext.
Mark Young39389872017-01-19 21:10:49 -07001268 - Call down the chain either `vkCreateDevice` or `vkCreateInstance`
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001269 - Initialize your layer dispatch table by calling the next entity's
Mark Young39389872017-01-19 21:10:49 -07001270Get*ProcAddr function once for each Vulkan function needed in your dispatch
Courtney Goeltzenleuchtera1473762016-02-14 09:31:24 -07001271table
Jon Ashburncc300a22016-02-11 14:57:30 -07001272
Mark Young39389872017-01-19 21:10:49 -07001273#### Example Code for CreateInstance
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001274
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001275```cpp
1276VkResult vkCreateInstance(
1277 const VkInstanceCreateInfo *pCreateInfo,
1278 const VkAllocationCallbacks *pAllocator,
1279 VkInstance *pInstance)
1280{
1281 VkLayerInstanceCreateInfo *chain_info =
1282 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1283
1284 assert(chain_info->u.pLayerInfo);
1285 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1286 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1287 PFN_vkCreateInstance fpCreateInstance =
Jon Ashburn2b4d7bb2016-05-23 13:05:21 -06001288 (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001289 if (fpCreateInstance == NULL) {
1290 return VK_ERROR_INITIALIZATION_FAILED;
1291 }
1292
1293 // Advance the link info for the next element of the chain
1294 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1295
1296 // Continue call down the chain
1297 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
1298 if (result != VK_SUCCESS)
1299 return result;
1300
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001301 // Init layer's dispatch table using GetInstanceProcAddr of
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001302 // next layer in the chain.
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001303 instance_dispatch_table = new VkLayerInstanceDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001304 layer_init_instance_dispatch_table(
1305 *pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
1306
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001307 // Other layer initialization
1308 ...
1309
1310 return VK_SUCCESS;
1311}
1312```
1313
Mark Young39389872017-01-19 21:10:49 -07001314#### Example Code for CreateDevice
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001315
1316```cpp
1317VkResult
1318vkCreateDevice(
1319 VkPhysicalDevice gpu,
1320 const VkDeviceCreateInfo *pCreateInfo,
1321 const VkAllocationCallbacks *pAllocator,
1322 VkDevice *pDevice)
1323{
1324 VkLayerDeviceCreateInfo *chain_info =
1325 get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1326
1327 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr =
1328 chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1329 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr =
1330 chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1331 PFN_vkCreateDevice fpCreateDevice =
1332 (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
1333 if (fpCreateDevice == NULL) {
1334 return VK_ERROR_INITIALIZATION_FAILED;
1335 }
1336
1337 // Advance the link info for the next element on the chain
1338 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
1339
1340 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
1341 if (result != VK_SUCCESS) {
1342 return result;
1343 }
1344
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001345 // initialize layer's dispatch table
1346 device_dispatch_table = new VkLayerDispatchTable;
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001347 layer_init_device_dispatch_table(
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001348 *pDevice, device_dispatch_table, fpGetDeviceProcAddr);
Courtney Goeltzenleuchterf6abc202016-02-15 15:05:16 -07001349
1350 // Other layer initialization
1351 ...
1352
1353 return VK_SUCCESS;
1354}
1355```
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001356
Mark Young39389872017-01-19 21:10:49 -07001357
Mark Youngc82a0622017-05-05 11:17:17 -06001358#### Meta-layers
1359
1360Meta-layers are a special kind of layer which is only available through the
1361desktop loader. While normal layers are associated with one particular library,
1362a meta-layer is actually a collection layer which contains an ordered list of
1363other layers (called component layers).
1364
1365The most common example of a meta-layer is the
1366`VK_LAYER_LUNARG_standard_validation` layer which groups all the most common
1367individual validation layers into a single layer for ease-of-use.
1368
1369The benefits of a meta-layer are:
1370 1. You can activate more than one layer using a single layer name by simply
1371grouping multiple layers in a meta-layer.
1372 2. You can define the order the loader will activate individual layers within
1373the meta-layer.
1374 3. You can easily share your special layer configuration with others.
1375 4. The loader will automatically collate all instance and device extensions in
1376a meta-layer's component layers, and report them as the meta-layer's properties
1377to the application when queried.
1378
1379Restrictions to defining and using a meta-layer are:
1380 1. A Meta-layer Manifest file **must** be a properly formated that contains one
1381or more component layers.
1382 3. All component layers **must be** present on a system for the meta-layer to
1383be used.
1384 4. All component layers **must be** at the same Vulkan API major and minor
1385version for the meta-layer to be used.
1386
1387The ordering of a meta-layer's component layers in the instance or device
1388call-chain is simple:
1389 * The first layer listed will be the layer closest to the application.
1390 * The last layer listed will be the layer closest to the drivers.
1391
1392Inside the meta-layer Manifest file, each component layer is listed by its
1393layer name. This is the "name" tag's value associated with each component layer's
1394Manifest file under the "layer" or "layers" tag. This is also the name that
1395would normally be used when activating a layer during `vkCreateInstance`.
1396
1397Any duplicate layer names in either the component layer list, or globally among
1398all enabled layers, will simply be ignored. Only the first instance of any
1399layer name will be used.
1400
1401For example, if you have a layer enabled using the environment variable
1402`VK_INSTANCE_LAYERS` and have that same layer listed in a meta-layer, then the
1403environment variable enabled layer will be used and the component layer will
1404be dropped. Likewise, if a person were to enable a meta-layer and then
1405separately enable one of the component layers afterwards, the second
1406instantiation of the layer name would be ignored.
1407
1408The
1409Manifest file formatting necessary to define a meta-layer can be found in the
1410[Layer Manifest File Format](#layer-manifest-file-format) section.
1411
Jon Ashburncc300a22016-02-11 14:57:30 -07001412#### Special Considerations
Mark Young39389872017-01-19 21:10:49 -07001413
1414
1415##### Associating Private Data with Vulkan Objects Within a Layer
1416
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001417A layer may want to associate it's own private data with one or more Vulkan
Mark Youngf7914cf2016-08-31 11:53:26 -06001418objects. Two common methods to do this are hash maps and object wrapping.
1419
Mark Youngf7914cf2016-08-31 11:53:26 -06001420
Mark Young39389872017-01-19 21:10:49 -07001421###### Wrapping
Ian Elliott0b082e42016-08-31 14:08:44 -06001422
Mark Young39389872017-01-19 21:10:49 -07001423The loader supports layers wrapping any Vulkan object, including dispatchable
1424objects. For functions that return object handles, each layer does not touch
1425the value passed down the call chain. This is because lower items may need to
1426use the original value. However, when the value is returned from a
1427lower-level layer (possibly the ICD), the layer saves the handle and returns
1428its own handle to the layer above it (possibly the application). When a layer
1429receives a Vulkan function using something that it previously returned a handle
1430for, the layer is required to unwrap the handle and pass along the saved handle
1431to the layer below it. This means that the layer **must intercept every Vulkan
1432function which uses the object in question**, and wrap or unwrap the object, as
1433appropriate. This includes adding support for all extensions with functions
Ian Elliott0b082e42016-08-31 14:08:44 -06001434using any object the layer wraps.
Mark Youngf7914cf2016-08-31 11:53:26 -06001435
1436Layers above the object wrapping layer will see the wrapped object. Layers
1437which wrap dispatchable objects must ensure that the first field in the wrapping
Mark Young39389872017-01-19 21:10:49 -07001438structure is a pointer to a dispatch table as defined in `vk_layer.h`.
1439Specifically, an instance wrapped dispatchable object could be as follows:
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001440```
1441struct my_wrapped_instance_obj_ {
1442 VkLayerInstanceDispatchTable *disp;
1443 // whatever data layer wants to add to this object
1444};
1445```
1446A device wrapped dispatchable object could be as follows:
1447```
1448struct my_wrapped_instance_obj_ {
1449 VkLayerDispatchTable *disp;
1450 // whatever data layer wants to add to this object
1451};
1452```
Jeff Julianof1619872016-02-17 17:25:42 -05001453
Ian Elliott0b082e42016-08-31 14:08:44 -06001454Layers that wrap dispatchable objects must follow the guidelines for creating
1455new dispatchable objects (below).
1456
Mark Young39389872017-01-19 21:10:49 -07001457<u>Cautions About Wrapping</u>
Ian Elliott0b082e42016-08-31 14:08:44 -06001458
1459Layers are generally discouraged from wrapping objects, because of the
1460potential for incompatibilities with new extensions. For example, let's say
Mark Young39389872017-01-19 21:10:49 -07001461that a layer wraps `VkImage` objects, and properly wraps and unwraps `VkImage`
1462object handles for all core functions. If a new extension is created which has
1463functions that take `VkImage` objects as parameters, and if the layer does not
1464support those new functions, an application that uses both the layer and the new
1465extension will have undefined behavior when those new functions are called (e.g.
Mark Young18bbaab2017-03-20 08:27:14 -06001466the application may crash). This is because the lower-level layers and ICD
Ian Elliott0b082e42016-08-31 14:08:44 -06001467won't receive the handle that they generated. Instead, they will receive a
1468handle that is only known by the layer that is wrapping the object.
1469
1470Because of the potential for incompatibilities with unsupported extensions,
1471layers that wrap objects must check which extensions are being used by the
1472application, and take appropriate action if the layer is used with unsupported
1473extensions (e.g. disable layer functionality, stop wrapping objects, issue a
1474message to the user).
1475
1476The reason that the validation layers wrap objects, is to track the proper use
1477and destruction of each object. They issue a validation error if used with
1478unsupported extensions, alerting the user to the potential for undefined
1479behavior.
1480
Mark Young39389872017-01-19 21:10:49 -07001481
1482###### Hash Maps
1483
Jeff Julianof1619872016-02-17 17:25:42 -05001484Alternatively, a layer may want to use a hash map to associate data with a
Courtney Goeltzenleuchter7221a5a2016-02-15 14:59:37 -07001485given object. The key to the map could be the object. Alternatively, for
1486dispatchable objects at a given level (eg device or instance) the layer may
Mark Young39389872017-01-19 21:10:49 -07001487want data associated with the `VkDevice` or `VkInstance` objects. Since
1488there are multiple dispatchable objects for a given `VkInstance` or `VkDevice`,
1489the `VkDevice` or `VkInstance` object is not a great map key. Instead the layer
1490should use the dispatch table pointer within the `VkDevice` or `VkInstance`
1491since that will be unique for a given `VkInstance` or `VkDevice`.
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001492
Mark Young39389872017-01-19 21:10:49 -07001493
1494##### Creating New Dispatchable Objects
1495
1496Layers which create dispatchable objects must take special care. Remember that
1497loader *trampoline* code normally fills in the dispatch table pointer in the
1498newly created object. Thus, the layer must fill in the dispatch table pointer if
1499the loader *trampoline* will not do so. Common cases where a layer (or ICD) may
1500create a dispatchable object without loader *trampoline* code is as follows:
1501- layers that wrap dispatchable objects
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001502- layers which add extensions that create dispatchable objects
Mark Young18bbaab2017-03-20 08:27:14 -06001503- layers which insert extra Vulkan functions in the stream of functions they
Jon Ashburnfe630fb2016-02-14 21:40:34 -07001504intercept from the application
Jon Ashburn859c7fb2016-03-02 17:26:31 -07001505- ICDs which add extensions that create dispatchable objects
1506
Mark Young39389872017-01-19 21:10:49 -07001507The desktop loader provides a callback that can be used for initializing
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001508a dispatchable object. The callback is passed as an extension structure via the
Mark Young39389872017-01-19 21:10:49 -07001509pNext field in the create info structure when creating an instance
1510(`VkInstanceCreateInfo`) or device (`VkDeviceCreateInfo`). The callback
1511prototype is defined as follows for instance and device callbacks respectively
1512(see `vk_layer.h`):
1513
1514```cpp
1515VKAPI_ATTR VkResult VKAPI_CALL vkSetInstanceLoaderData(VkInstance instance,
1516 void *object);
1517VKAPI_ATTR VkResult VKAPI_CALL vkSetDeviceLoaderData(VkDevice device,
1518 void *object);
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001519```
Mark Young39389872017-01-19 21:10:49 -07001520
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001521To obtain these callbacks the layer must search through the list of structures
Mark Young39389872017-01-19 21:10:49 -07001522pointed to by the "pNext" field in the `VkInstanceCreateInfo` and
1523`VkDeviceCreateInfo` parameters to find any callback structures inserted by the
1524loader. The salient details are as follows:
1525- For `VkInstanceCreateInfo` the callback structure pointed to by "pNext" is
1526`VkLayerInstanceCreateInfo` as defined in `include/vulkan/vk_layer.h`.
1527- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO within
1528`VkInstanceCreateInfo` parameter indicates a loader structure.
1529- Within `VkLayerInstanceCreateInfo`, the "function" field indicates how the
1530union field "u" should be interpreted.
1531- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will
1532contain the callback in "pfnSetInstanceLoaderData".
1533- For `VkDeviceCreateInfo` the callback structure pointed to by "pNext" is
1534`VkLayerDeviceCreateInfo` as defined in `include/vulkan/vk_layer.h`.
1535- A "sType" field in of VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO within
1536`VkDeviceCreateInfo` parameter indicates a loader structure.
1537- Within `VkLayerDeviceCreateInfo`, the "function" field indicates how the union
1538field "u" should be interpreted.
1539- A "function" equal to VK_LOADER_DATA_CALLBACK indicates the "u" field will
1540contain the callback in "pfnSetDeviceLoaderData".
Jon Ashburn2b2f6182016-04-04 16:37:37 -06001541
Mark Young39389872017-01-19 21:10:49 -07001542Alternatively, if an older loader is being used that doesn't provide these
1543callbacks, the layer may manually initialize the newly created dispatchable
1544object. To fill in the dispatch table pointer in newly created dispatchable
1545object, the layer should copy the dispatch pointer, which is always the first
1546entry in the structure, from an existing parent object of the same level
1547(instance versus device).
Jon Ashburnc2972682016-02-08 15:42:01 -07001548
Mark Young39389872017-01-19 21:10:49 -07001549For example, if there is a newly created `VkCommandBuffer` object, then the
1550dispatch pointer from the `VkDevice` object, which is the parent of the
1551`VkCommandBuffer` object, should be copied into the newly created object.
1552
1553
1554#### Layer Manifest File Format
1555
1556On Windows and Linux (desktop), the loader uses manifest files to discover
1557layer libraries and layers. The desktop loader doesn't directly query the
1558layer library except during chaining. This is to reduce the likelihood of
1559loading a malicious layer into memory. Instead, details are read from the
1560Manifest file, which are then provided for applications to determine what
1561layers should actually be loaded.
1562
1563The following section discusses the details of the Layer Manifest JSON file
1564format. The JSON file itself does not have any requirements for naming. The
1565only requirement is that the extension suffix of the file ends with ".json".
1566
1567Here is an example layer JSON Manifest file with a single layer:
1568
1569```
1570{
1571 "file_format_version" : "1.0.0",
1572 "layer": {
1573 "name": "VK_LAYER_LUNARG_overlay",
1574 "type": "INSTANCE",
1575 "library_path": "vkOverlayLayer.dll"
1576 "api_version" : "1.0.5",
1577 "implementation_version" : "2",
1578 "description" : "LunarG HUD layer",
1579 "functions": {
1580 "vkNegotiateLoaderLayerInterfaceVersion":
1581 "OverlayLayer_NegotiateLoaderLayerInterfaceVersion"
1582 },
1583 "instance_extensions": [
1584 {
1585 "name": "VK_EXT_debug_report",
1586 "spec_version": "1"
1587 },
1588 {
1589 "name": "VK_VENDOR_ext_x",
1590 "spec_version": "3"
1591 }
1592 ],
1593 "device_extensions": [
1594 {
1595 "name": "VK_EXT_debug_marker",
1596 "spec_version": "1",
1597 "entrypoints": ["vkCmdDbgMarkerBegin", "vkCmdDbgMarkerEnd"]
1598 }
1599 ],
1600 "enable_environment": {
1601 "ENABLE_LAYER_OVERLAY_1": "1"
1602 }
1603 "disable_environment": {
1604 "DISABLE_LAYER_OVERLAY_1": ""
1605 }
1606 }
1607}
1608```
1609
1610Here's a snippet with the changes required to support multiple layers per
1611manifest file:
1612```
1613{
1614 "file_format_version" : "1.0.1",
1615 "layers": [
1616 {
1617 "name": "VK_LAYER_layer_name1",
1618 "type": "INSTANCE",
1619 ...
1620 },
1621 {
1622 "name": "VK_LAYER_layer_name2",
1623 "type": "INSTANCE",
1624 ...
1625 }
1626 ]
1627}
1628```
1629
Mark Youngc82a0622017-05-05 11:17:17 -06001630Here's an example of a meta-layer manifest file:
1631```
1632{
1633 "file_format_version" : "1.1.1",
1634 "layer": {
1635 "name": "VK_LAYER_LUNARG_standard_validation",
1636 "type": "GLOBAL",
1637 "api_version" : "1.0.40",
1638 "implementation_version" : "1",
1639 "description" : "LunarG Standard Validation Meta-layer",
1640 "component_layers": [
1641 "VK_LAYER_GOOGLE_threading",
1642 "VK_LAYER_LUNARG_parameter_validation",
1643 "VK_LAYER_LUNARG_object_tracker",
1644 "VK_LAYER_LUNARG_core_validation",
1645 "VK_LAYER_LUNARG_swapchain",
1646 "VK_LAYER_GOOGLE_unique_objects"
1647 ]
1648 }
1649}
1650```
Mark Young39389872017-01-19 21:10:49 -07001651| JSON Node | Description and Notes | Introspection Query |
1652|:----------------:|--------------------|:----------------:
1653| "file\_format\_version" | Manifest format major.minor.patch version number. | N/A |
1654| | Supported versions are: 1.0.0, 1.0.1, and 1.1.0. | |
1655| "layer" | The identifier used to group a single layer's information together. | vkEnumerateInstanceLayerProperties |
1656| "layers" | The identifier used to group multiple layers' information together. This requires a minimum Manifest file format version of 1.0.1.| vkEnumerateInstanceLayerProperties |
1657| "name" | The string used to uniquely identify this layer to applications. | vkEnumerateInstanceLayerProperties |
1658| "type" | This field indicates the type of layer. The values can be: GLOBAL, or INSTANCE | vkEnumerate*LayerProperties |
1659| | **NOTES:** Prior to deprecation, the "type" node was used to indicate which layer chain(s) to activate the layer upon: instance, device, or both. Distinct instance and device layers are deprecated; there are now just layers. Allowable values for type (both before and after deprecation) are "INSTANCE", "GLOBAL" and, "DEVICE." "DEVICE" layers are skipped over by the loader as if they were not found. | |
Mark Youngc82a0622017-05-05 11:17:17 -06001660| "library\_path" | The "library\_path" specifies either a filename, a relative pathname, or a full pathname to a layer shared library file. If "library\_path" specifies a relative pathname, it is relative to the path of the JSON manifest file (e.g. for cases when an application provides a layer that is in the same folder hierarchy as the rest of the application files). If "library\_path" specifies a filename, the library must live in the system's shared object search path. There are no rules about the name of the layer shared library files other than it should end with the appropriate suffix (".DLL" on Windows, and ".so" on Linux). **This field must not be present if "component_layers" is defined** | N/A |
Mark Young39389872017-01-19 21:10:49 -07001661| "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 |
1662| "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 |
1663| "description" | A high-level description of the layer and it's intended use. | vkEnumerateInstanceLayerProperties |
1664| "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 |
1665| "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 |
1666| "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 |
1667| "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 |
1668| "disable\_environment" | **Implicit Layers Only** - **REQUIRED:**Indicates an environment variable used to disable the Implicit Layer (w/ value of 1). In rare cases of an application not working with an implicit layer, the application can set this environment variable (before calling Vulkan functions) in order to "blacklist" the layer. This environment variable (which should vary with each "version" of the layer) must be set (not particularly to any value). If both the "enable_environment" and "disable_environment" variables are set, the implicit layer is disabled. | N/A |
Mark Youngc82a0622017-05-05 11:17:17 -06001669| "component_layers" | **Meta-layers Only** - Indicates the component layer names that are part of a meta-layer. The names listed must be the "name" identified in each of the component layer's Mainfest file "name" tag (this is the same as the name of the layer that is passed to the `vkCreateInstance` command). All component layers must be present on the system and found by the loader in order for this meta-layer to be available and activated. **This field must not be present if "library\_path" is defined** | N/A |
Mark Young39389872017-01-19 21:10:49 -07001670
1671##### Layer Manifest File Version History
1672
1673The current highest supported Layer Manifest file format supported is 1.1.0.
1674Information about each version is detailed in the following sub-sections:
1675
1676###### Layer Manifest File Version 1.1.0
1677
1678Layer Manifest File Version 1.1.0 is tied to changes exposed by the Loader/Layer
1679interface version 2.
1680 1. Renaming "vkGetInstanceProcAddr" in the "functions" section is
1681 deprecated since the loader no longer needs to query the layer about
1682 "vkGetInstanceProcAddr" directly. It is now returned during the layer
1683 negotiation, so this field will be ignored.
1684 2. Renaming "vkGetDeviceProcAddr" in the "functions" section is
1685 deprecated since the loader no longer needs to query the layer about
1686 "vkGetDeviceProcAddr" directly. It too is now returned during the layer
1687 negotiation, so this field will be ignored.
1688 3. Renaming the "vkNegotiateLoaderLayerInterfaceVersion" function is
1689 being added to the "functions" section, since this is now the only
1690 function the loader needs to query using OS-specific calls.
1691 - NOTE: This is an optional field and, as the two previous fields, only
1692needed if the layer requires changing the name of the function for some reason.
1693
1694You do not need to update your layer manifest file if you don't change the
1695names of any of the listed functions.
1696
1697###### Layer Manifest File Version 1.0.1
1698
1699The ability to define multiple layers using the "layers" array was added. This
1700JSON array field can be used when defining a single layer or multiple layers.
1701The "layer" field is still present and valid for a single layer definition.
1702
1703###### Layer Manifest File Version 1.0.0
1704
1705The initial version of the layer manifest file specified the basic format and
1706fields of a layer JSON file. The fields of the 1.0.0 file format include:
1707 * "file\_format\_version"
1708 * "layer"
1709 * "name"
1710 * "type"
1711 * "library\_path"
1712 * "api\_version"
1713 * "implementation\_version"
1714 * "description"
1715 * "functions"
1716 * "instance\_extensions"
1717 * "device\_extensions"
1718 * "enable\_environment"
1719 * "disable\_environment"
1720
1721It was also during this time that the value of "DEVICE" was deprecated from
1722the "type" field.
1723
1724
1725#### Layer Library Versions
1726
1727The current Layer Library interface is at version 2. The following sections
1728detail the differences between the various versions.
1729
1730##### Layer Library API Version 2
1731
1732Introduced the concept of
1733[loader and layer interface](#layer-version-negotiation) using the new
1734`vkNegotiateLoaderLayerInterfaceVersion` function. Additionally, it introduced
1735the concept of
1736[Layer Unknown Physical Device Extensions](#layer-unknown-physical-device-
1737extensions)
1738and the associated `vk_layerGetPhysicalDeviceProcAddr` function. Finally, it
1739changed the manifest file defition to 1.1.0.
1740
1741##### Layer Library API Version 1
1742
1743A layer library supporting interface version 1 had the following behavior:
1744 1. `GetInstanceProcAddr` and `GetDeviceProcAddr` were directly exported
1745 2. The layer manifest file was able to override the names of the
1746`GetInstanceProcAddr` and `GetDeviceProcAddr`functions.
1747
1748##### Layer Library API Version 0
1749
1750A layer library supporting interface version 0 must define and export these
1751introspection functions, unrelated to any Vulkan function despite the names,
1752signatures, and other similarities:
1753
Lenny Komowde3924a2017-05-04 14:50:01 -06001754- `vkEnumerateInstanceLayerProperties` enumerates all layers in a layer
Mark Young39389872017-01-19 21:10:49 -07001755library.
1756 - This function never fails.
1757 - When a layer library contains only one layer, this function may be an alias
1758 to the layer's `vkEnumerateInstanceLayerProperties`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001759- `vkEnumerateInstanceExtensionProperties` enumerates instance extensions of
Mark Young39389872017-01-19 21:10:49 -07001760 layers in a layer library.
1761 - "pLayerName" is always a valid layer name.
1762 - This function never fails.
1763 - When a layer library contains only one layer, this function may be an alias
1764 to the layer's `vkEnumerateInstanceExtensionProperties`.
Lenny Komowde3924a2017-05-04 14:50:01 -06001765- `vkEnumerateDeviceLayerProperties` enumerates a subset (can be full,
Mark Young39389872017-01-19 21:10:49 -07001766 proper, or empty subset) of layers in a layer library.
1767 - "physicalDevice" is always `VK_NULL_HANDLE`.
1768 - This function never fails.
1769 - If a layer is not enumerated by this function, it will not participate in
1770 device function interception.
Lenny Komowde3924a2017-05-04 14:50:01 -06001771- `vkEnumerateDeviceExtensionProperties` enumerates device extensions of
Mark Young39389872017-01-19 21:10:49 -07001772 layers in a layer library.
1773 - "physicalDevice" is always `VK_NULL_HANDLE`.
1774 - "pLayerName" is always a valid layer name.
1775 - This function never fails.
1776
1777It must also define and export these functions once for each layer in the
1778library:
1779
Lenny Komowde3924a2017-05-04 14:50:01 -06001780- `<layerName>GetInstanceProcAddr(instance, pName)` behaves identically to a
Mark Young39389872017-01-19 21:10:49 -07001781layer's vkGetInstanceProcAddr except it is exported.
1782
1783 When a layer library contains only one layer, this function may
1784 alternatively be named `vkGetInstanceProcAddr`.
1785
Lenny Komowde3924a2017-05-04 14:50:01 -06001786- `<layerName>GetDeviceProcAddr` behaves identically to a layer's
Mark Young39389872017-01-19 21:10:49 -07001787vkGetDeviceProcAddr except it is exported.
1788
1789 When a layer library contains only one layer, this function may
1790 alternatively be named `vkGetDeviceProcAddr`.
1791
1792All layers contained within a library must support `vk_layer.h`. They do not
1793need to implement functions that they do not intercept. They are recommended
1794not to export any functions.
1795
1796
1797<br/>
1798<br/>
1799
1800## Vulkan Installable Client Driver Interface With the Loader
1801
1802This section discusses the various requirements for the loader and a Vulkan
1803ICD to properly hand-shake.
1804
Lenny Komowde3924a2017-05-04 14:50:01 -06001805 * [ICD Discovery](#icd-discovery)
1806 * [ICD Manifest File Usage](#icd-manifest-file-usage)
1807 * [ICD Discovery on Windows](#icd-discovery-on-windows)
1808 * [ICD Discovery on Linux](#icd-discovery-on-linux)
1809 * [Using Pre-Production ICDs on Windows and Linux](#using-pre-production-icds-on-windows-and-linux)
1810 * [ICD Discovery on Android](#icd-discovery-on-android)
1811 * [ICD Manifest File Format](#icd-manifest-file-format)
1812 * [ICD Manifest File Versions](#icd-manifest-file-versions)
1813 * [ICD Manifest File Version 1.0.0](#icd-manifest-file-version-1.0.0)
1814 * [ICD Vulkan Entry-Point Discovery](#icd-vulkan-entry-point-discovery)
1815 * [ICD Unknown Physical Device Extensions](#icd-unknown-physical-device-extensions)
1816 * [ICD Dispatchable Object Creation](#icd-dispatchable-object-creation)
1817 * [Handling KHR Surface Objects in WSI Extensions](#handling-khr-surface-objects-in-wsi-extensions)
1818 * [Loader and ICD Interface Negotiation](#loader-and-icd-interface-negotiation)
1819 * [Windows and Linux ICD Negotiation](#windows-and-linux-icd-negotiation)
1820 * [Version Negotiation Between Loader and ICDs](#version-negotiation-between-loader-and-icds)
1821 * [Interfacing With Legacy ICDs or Loader](#interfacing-with-legacy-icds-or-loader)
1822 * [Loader Version 4 Interface Requirements](#loader-version-4-interface-requirements)
1823 * [Loader Version 3 Interface Requirements](#loader-version-3-interface-requirements)
1824 * [Loader Version 2 Interface Requirements](#loader-version-2-interface-requirements)
1825 * [Loader Versions 0 and 1 Interface Requirements](#loader-versions-0-and-1-interface-requirements)
1826 * [Android ICD Negotiation](#android-icd-negotiation)
Mark Young39389872017-01-19 21:10:49 -07001827
1828
1829### ICD Discovery
1830
1831Vulkan allows multiple drivers each with one or more devices (represented by a
1832Vulkan `VkPhysicalDevice` object) to be used collectively. The loader is
1833responsible for discovering available Vulkan ICDs on the system. Given a list
1834of available ICDs, the loader can enumerate all the physical devices available
1835for an application and return this information to the application. The process
1836in which the loader discovers the available Installable Client Drivers (ICDs)
1837on a system is platform dependent. Windows, Linux and Android ICD discovery
1838details are listed below.
1839
1840#### ICD Manifest File Usage
1841
Mark Young18bbaab2017-03-20 08:27:14 -06001842As with layers, on Windows and Linux systems, JSON formatted manifest files are
Mark Young39389872017-01-19 21:10:49 -07001843used to store ICD information. In order to find system-installed drivers, the
1844Vulkan loader will read the JSON files to identify the names and attributes of
1845each driver. One thing you will notice is that ICD Manifest files are much
1846simpler than the corresponding layer Manifest files.
1847
1848See the [Current ICD Manifest File Format](#icd-manifest-file-format) section
1849for more details.
1850
1851
1852#### ICD Discovery on Windows
1853
1854In order to find installed ICDs, the Vulkan loader will scan the
1855values in the following Windows registry key:
1856
1857```
1858 HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers
1859```
1860
1861For 32-bit applications on 64-bit Windows, the loader scan's the 32-bit
1862registry location:
1863
1864```
1865 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Khronos\Vulkan\Drivers
1866```
1867
1868The loader will look at the appropriate registry location and check each value
1869listed. If the key is of type DWORD, and it has a value of 0, the loader will
1870open the JSON manifest file specified by the name. Each name must be a full
1871pathname to a text manifest file. The Vulkan loader will attempt to open each
1872manifest file to obtain the information about an ICD's shared library (".dll")
1873file.
1874
1875For example, let us assume the registry contains the following data:
1876
1877```
1878[HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\Vulkan\Drivers\]
1879
1880"C:\vendor a\vk_vendora.json"=dword:00000000
1881"C:\windows\system32\vendorb_vk.json"=dword:00000001
1882"C:\windows\system32\vendorc_icd.json"=dword:00000000
1883```
1884
1885In this case, the loader will step through each entry, and check the value. If
1886the value is 0, then the loader will attempt to load the file. In this case,
Mark Young18bbaab2017-03-20 08:27:14 -06001887the loader will open the first and last listings, but not the middle. This
Mark Young39389872017-01-19 21:10:49 -07001888is because the value of 1 for vendorb_vk.json disables the driver.
1889
1890The Vulkan loader will open each enabled manifest file found to obtain the name
1891or pathname of an ICD shared library (".DLL") file.
1892
1893See the [ICD Manifest File Format](#icd-manifest-file-format) section for more
1894details.
1895
1896
1897#### ICD Discovery on Linux
1898
1899In order to find installed ICDs, the Vulkan loader will scan the files
1900in the following Linux directories:
1901
1902```
1903 /usr/local/etc/vulkan/icd.d
1904 /usr/local/share/vulkan/icd.d
1905 /etc/vulkan/icd.d
1906 /usr/share/vulkan/icd.d
1907 $HOME/.local/share/vulkan/icd.d
1908```
1909
1910The "/usr/local/*" directories can be configured to be other directories at
1911build time.
1912
1913The typical usage of the directories is indicated in the table below.
1914
1915| Location | Details |
1916|-------------------|------------------------|
1917| $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 |
1918| "/usr/local/etc/vulkan/icd.d" | Directory for locally built ICDs |
1919| "/usr/local/share/vulkan/icd.d" | Directory for locally built ICDs |
1920| "/etc/vulkan/icd.d" | Location of ICDs installed from non-Linux-distribution-provided packages |
1921| "/usr/share/vulkan/icd.d" | Location of ICDs installed from Linux-distribution-provided packages |
1922
1923The Vulkan loader will open each manifest file found to obtain the name or
1924pathname of an ICD shared library (".so") file.
1925
1926See the [ICD Manifest File Format](#icd-manifest-file-format) section for more
1927details.
1928
1929#### Using Pre-Production ICDs on Windows and Linux
1930
1931Independent Hardware Vendor (IHV) pre-production ICDs. In some cases, a
1932pre-production ICD may be in an installable package. In other cases, a
1933pre-production ICD may simply be a shared library in the developer's build tree.
1934In this latter case, we want to allow developers to point to such an ICD without
1935modifying the system-installed ICD(s) on their system.
1936
1937This need is met with the use of the "VK\_ICD\_FILENAMES" environment variable,
1938which will override the mechanism used for finding system-installed ICDs. In
1939other words, only the ICDs listed in "VK\_ICD\_FILENAMES" will be used.
1940
1941The "VK\_ICD\_FILENAMES" environment variable is a list of ICD
1942manifest files, containing the full path to the ICD JSON Manifest file. This
1943list is colon-separated on Linux, and semi-colon separated on Windows.
1944
1945Typically, "VK\_ICD\_FILENAMES" will only contain a full pathname to one info
1946file for a developer-built ICD. A separator (colon or semi-colon) is only used
1947if more than one ICD is listed.
1948
1949**NOTE:** On Linux, this environment variable will be ignored for suid programs.
1950
1951
1952#### ICD Discovery on Android
1953
1954The Android loader lives in the system library folder. The location cannot be
1955changed. The loader will load the driver/ICD via hw\_get\_module with the ID
1956of "vulkan". **Due to security policies in Android, none of this can be modified
1957under normal use.**
1958
1959
1960### ICD Manifest File Format
1961
1962The following section discusses the details of the ICD Manifest JSON file
1963format. The JSON file itself does not have any requirements for naming. The
1964only requirement is that the extension suffix of the file ends with ".json".
1965
1966Here is an example layer JSON Manifest file:
1967
1968```
1969{
1970 "file_format_version": "1.0.0",
1971 "ICD": {
1972 "library_path": "path to ICD library",
1973 "api_version": "1.0.5"
1974 }
1975}
1976```
1977
1978| Field Name | Field Value |
1979|----------------|--------------------|
1980| "file\_format\_version" | The JSON format major.minor.patch version number of this file. Currently supported version is 1.0.0. |
1981| "ICD" | The identifier used to group all ICD information together. |
1982| "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 |
1983| "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. |
1984
1985**NOTE:** If the same ICD shared library supports multiple, incompatible
1986versions of text manifest file format versions, it must have separate
1987JSON files for each (all of which may point to the same shared library).
1988
1989##### ICD Manifest File Versions
1990
1991There has only been one version of the ICD manifest files supported. This is
1992version 1.0.0.
1993
1994###### ICD Manifest File Version 1.0.0
1995
1996The initial version of the ICD Manifest file specified the basic format and
1997fields of a layer JSON file. The fields of the 1.0.0 file format include:
1998 * "file\_format\_version"
1999 * "ICD"
2000 * "library\_path"
2001 * "api\_version"
2002
2003
2004### ICD Vulkan Entry-Point Discovery
2005
2006The Vulkan symbols exported by an ICD must not clash with the loader's exported
2007Vulkan symbols. This could be for several reasons. Because of this, all ICDs
2008must export the following function that is used for discovery of ICD Vulkan
2009entry-points. This entry-point is not a part of the Vulkan API itself, only a
2010private interface between the loader and ICDs for version 1 and higher
2011interfaces.
2012
2013```cpp
2014VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
2015 VkInstance instance,
2016 const char* pName);
2017```
2018
2019This function has very similar semantics to `vkGetInstanceProcAddr`.
2020`vk_icdGetInstanceProcAddr` returns valid function pointers for all the global-
2021level and instance-level Vulkan functions, and also for `vkGetDeviceProcAddr`.
2022Global-level functions are those which contain no dispatchable object as the
2023first parameter, such as `vkCreateInstance` and
2024`vkEnumerateInstanceExtensionProperties`. The ICD must support querying global-
2025level entry-points by calling `vk_icdGetInstanceProcAddr` with a NULL
2026`VkInstance` parameter. Instance-level functions are those that have either
2027`VkInstance`, or `VkPhysicalDevice` as the first parameter dispatchable object.
2028Both core entry-points and any instance extension entry-points the ICD supports
2029should be available via `vk_icdGetInstanceProcAddr`. Future Vulkan instance
2030extensions may define and use new instance-level dispatchable objects other
2031than `VkInstance` and `VkPhysicalDevice`, in which case extension entry-points
2032using these newly defined dispatchable objects must be queryable via
2033`vk_icdGetInstanceProcAddr`.
2034
2035All other Vulkan entry-points must either:
2036 * NOT be exported directly from the ICD library
2037 * or NOT use the official Vulkan function names if they are exported
2038
2039This requirement is for ICD libraries that include other
2040functionality (such as OpenGL) and thus could be loaded by the
2041application prior to when the Vulkan loader library is loaded by the
2042application.
2043
2044Beware of interposing by dynamic OS library loaders if the official Vulkan
2045names are used. On Linux, if official names are used, the ICD library must be
2046linked with -Bsymbolic.
2047
2048
2049### ICD Unknown Physical Device Extensions
2050
2051Originally, if the loader was called with `vkGetInstanceProcAddr`, it would
2052result in the following behavior:
2053 1. The loader would check if core function:
2054 - If it was, it would return the function pointer
2055 2. The loader would check if known extension function:
2056 - If it was, it would return the function pointer
2057 3. If the loader knew nothing about it, it would call down using
2058`GetInstanceProcAddr`
2059 - If it returned non-NULL, treat it as an unknown logical device command.
2060 - This meant setting up a generic trampoline function that takes in a
2061VkDevice as the first parameter and adjusting the dispatch table to call the
2062ICD/Layers function after getting the dispatch table from the VkDevice.
2063 4. If all the above failed, the loader would return NULL to the application.
2064
2065This caused problems when an ICD attempted to expose new physical device
2066extensions the loader knew nothing about, but an application did. Because the
2067loader knew nothing about it, the loader would get to step 3 in the above
2068process and would treat the function as an unknown logical device command. The
2069problem is, this would create a generic VkDevice trampoline function which, on
2070the first call, would attempt to dereference the VkPhysicalDevice as a VkDevice.
2071This would lead to a crash or corruption.
2072
2073In order to identify the extension entry-points specific to physical device
2074extensions, the following function can be added to an ICD:
2075
2076```cpp
2077PFN_vkVoidFunction vk_icdGetPhysicalDeviceProcAddr(VkInstance instance,
2078 const char* pName);
2079```
2080
2081This function behaves similar to `vkGetInstanceProcAddr` and
2082`vkGetDeviceProcAddr` except it should only return values for physical device
2083extension entry-points. In this way, it compares "pName" to every physical
2084device function supported in the ICD.
2085
2086The following rules apply:
Lenny Komowde3924a2017-05-04 14:50:01 -06002087* If it is the name of a physical device function supported by the ICD, the
Mark Young39389872017-01-19 21:10:49 -07002088pointer to the ICD's corresponding function should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06002089* If it is the name of a valid function which is **not** a physical device
Mark Young39389872017-01-19 21:10:49 -07002090function (i.e. an Instance, Device, or other function implemented by the ICD),
2091then the value of NULL should be returned.
Lenny Komowde3924a2017-05-04 14:50:01 -06002092* If the ICD has no idea what this function is, it should return NULL.
Mark Young39389872017-01-19 21:10:49 -07002093
2094This support is optional and should not be considered a requirement. This is
2095only required if an ICD intends to support some functionality not directly
2096supported by a significant population of loaders in the public. If an ICD
Mark Young18bbaab2017-03-20 08:27:14 -06002097does implement this support, it should return the address of its
Mark Young39389872017-01-19 21:10:49 -07002098`vk_icdGetPhysicalDeviceProcAddr` function through the `vkGetInstanceProcAddr`
2099function.
2100
2101The new behavior of the loader's vkGetInstanceProcAddr with support for the
2102`vk_icdGetPhysicalDeviceProcAddr` function is as follows:
2103 1. Check if core function:
2104 - If it is, return the function pointer
2105 2. Check if known instance or device extension function:
2106 - If it is, return the function pointer
2107 3. Call the layer/ICD `GetPhysicalDeviceProcAddr`
2108 - If it returns non-NULL, return a trampoline to a generic physical device
2109function, and setup a generic terminator which will pass it to the proper ICD.
2110 4. Call down using `GetInstanceProcAddr`
2111 - If it returns non-NULL, treat it as an unknown logical device command.
2112This means setting up a generic trampoline function that takes in a VkDevice as
2113the first parameter and adjusting the dispatch table to call the ICD/Layers
2114function after getting the dispatch table from the VkDevice. Then, return the
2115pointer to corresponding trampoline function.
2116 5. Return NULL
2117
2118You can see now, that, if the command gets promoted to core later, it will no
2119longer be setup using `vk_icdGetPhysicalDeviceProcAddr`. Additionally, if the
2120loader adds direct support for the extension, it will no longer get to step 3,
2121because step 2 will return a valid function pointer. However, the ICD should
2122continue to support the command query via `vk_icdGetPhysicalDeviceProcAddr`,
2123until at least a Vulkan version bump, because an older loader may still be
2124attempting to use the commands.
2125
2126
2127### ICD Dispatchable Object Creation
2128
2129As previously covered, the loader requires dispatch tables to be accessible
2130within Vulkan dispatchable objects, such as: `VkInstance`, `VkPhysicalDevice`,
2131`VkDevice`, `VkQueue`, and `VkCommandBuffer`. The specific requirements on all
2132dispatchable objects created by ICDs are as follows:
2133
2134- All dispatchable objects created by an ICD can be cast to void \*\*
2135- The loader will replace the first entry with a pointer to the dispatch table
2136 which is owned by the loader. This implies three things for ICD drivers
2137 1. The ICD must return a pointer for the opaque dispatchable object handle
2138 2. This pointer points to a regular C structure with the first entry being a
2139 pointer.
2140 * **NOTE:** For any C\++ ICD's that implement VK objects directly as C\++
2141classes.
2142 * The C\++ compiler may put a vtable at offset zero if your class is non-
2143POD due to the use of a virtual function.
2144 * In this case use a regular C structure (see below).
2145 3. The loader checks for a magic value (ICD\_LOADER\_MAGIC) in all the created
2146 dispatchable objects, as follows (see `include/vulkan/vk_icd.h`):
2147
2148```cpp
2149#include "vk_icd.h"
2150
2151union _VK_LOADER_DATA {
2152 uintptr loadermagic;
2153 void *loaderData;
2154} VK_LOADER_DATA;
2155
2156vkObj alloc_icd_obj()
2157{
2158 vkObj *newObj = alloc_obj();
2159 ...
2160 // Initialize pointer to loader's dispatch table with ICD_LOADER_MAGIC
2161
2162 set_loader_magic_value(newObj);
2163 ...
2164 return newObj;
2165}
2166```
2167
2168
2169### Handling KHR Surface Objects in WSI Extensions
2170
2171Normally, ICDs handle object creation and destruction for various Vulkan
2172objects. The WSI surface extensions for Linux and Windows
2173("VK\_KHR\_win32\_surface", "VK\_KHR\_xcb\_surface", "VK\_KHR\_xlib\_surface",
2174"VK\_KHR\_mir\_surface", "VK\_KHR\_wayland\_surface", and "VK\_KHR\_surface")
2175are handled differently. For these extensions, the `VkSurfaceKHR` object
2176creation and destruction may be handled by either the loader, or an ICD.
2177
2178If the loader handles the management of the `VkSurfaceKHR` objects:
2179 1. The loader will handle the calls to `vkCreateXXXSurfaceKHR` and
2180`vkDestroySurfaceKHR`
2181 functions without involving the ICDs.
2182 * Where XXX stands for the Windowing System name:
2183 * Mir
2184 * Wayland
2185 * Xcb
2186 * Xlib
2187 * Windows
2188 * Android
2189 2. The loader creates a `VkIcdSurfaceXXX` object for the corresponding
2190`vkCreateXXXSurfaceKHR` call.
2191 * The `VkIcdSurfaceXXX` structures are defined in `include/vulkan/vk_icd.h`.
2192 3. ICDs can cast any `VkSurfaceKHR` object to a pointer to the appropriate
2193 `VkIcdSurfaceXXX` structure.
2194 4. The first field of all the `VkIcdSurfaceXXX` structures is a
2195`VkIcdSurfaceBase` enumerant that indicates whether the
2196 surface object is Win32, Xcb, Xlib, Mir, or Wayland.
2197
2198The ICD may choose to handle `VkSurfaceKHR` object creation instead. If an ICD
2199desires to handle creating and destroying it must do the following:
2200 1. Support version 3 or newer of the loader/ICD interface.
2201 2. Export and handle all functions that take in a `VkSurfaceKHR` object,
2202including:
2203 * `vkCreateXXXSurfaceKHR`
2204 * `vkGetPhysicalDeviceSurfaceSupportKHR`
2205 * `vkGetPhysicalDeviceSurfaceCapabilitiesKHR`
2206 * `vkGetPhysicalDeviceSurfaceFormatsKHR`
2207 * `vkGetPhysicalDeviceSurfacePresentModesKHR`
2208 * `vkCreateSwapchainKHR`
2209 * `vkDestroySurfaceKHR`
2210
2211Because the `VkSurfaceKHR` object is an instance-level object, one object can be
2212associated with multiple ICDs. Therefore, when the loader receives the
2213`vkCreateXXXSurfaceKHR` call, it still creates an internal `VkSurfaceIcdXXX`
2214object. This object acts as a container for each ICD's version of the
2215`VkSurfaceKHR` object. If an ICD does not support the creation of its own
2216`VkSurfaceKHR` object, the loader's container stores a NULL for that ICD. On
2217the otherhand, if the ICD does support `VkSurfaceKHR` creation, the loader will
2218make the appropriate `vkCreateXXXSurfaceKHR` call to the ICD, and store the
2219returned pointer in it's container object. The loader then returns the
2220`VkSurfaceIcdXXX` as a `VkSurfaceKHR` object back up the call chain. Finally,
2221when the loader receives the `vkDestroySurfaceKHR` call, it subsequently calls
2222`vkDestroySurfaceKHR` for each ICD who's internal `VkSurfaceKHR` object is not
2223NULL. Then the loader destroys the container object before returning.
2224
2225
2226### Loader and ICD Interface Negotiation
2227
2228Generally, for functions issued by an application, the loader can be
2229viewed as a pass through. That is, the loader generally doesn't modify the
2230functions or their parameters, but simply calls the ICDs entry-point for that
2231function. There are specific additional interface requirements an ICD needs to
2232comply with that are not part of any requirements from the Vulkan specification.
2233These addtional requirements are versioned to allow flexibility in the future.
2234
2235
2236#### Windows and Linux ICD Negotiation
2237
2238
2239##### Version Negotiation Between Loader and ICDs
2240
2241All ICDs (supporting interface version 2 or higher) must export the following
2242function that is used for determination of the interface version that will be
2243used. This entry-point is not a part of the Vulkan API itself, only a private
2244interface between the loader and ICDs.
2245
2246```cpp
2247 VKAPI_ATTR VkResult VKAPI_CALL
2248 vk_icdNegotiateLoaderICDInterfaceVersion(
2249 uint32_t* pSupportedVersion);
2250```
2251
2252This function allows the loader and ICD to agree on an interface version to use.
2253The "pSupportedVersion" parameter is both an input and output parameter.
2254"pSupportedVersion" is filled in by the loader with the desired latest interface
2255version supported by the loader (typically the latest). The ICD receives this
2256and returns back the version it desires in the same field. Because it is
2257setting up the interface version between the loader and ICD, this should be
2258the first call made by a loader to the ICD (even prior to any calls to
2259`vk_icdGetInstanceProcAddr`).
2260
2261If the ICD receiving the call no longer supports the interface version provided
2262by the loader (due to deprecation), then it should report
2263VK_ERROR_INCOMPATIBLE_DRIVER error. Otherwise it sets the value pointed by
2264"pSupportedVersion" to the latest interface version supported by both the ICD
2265and the loader and returns VK_SUCCESS.
2266
2267The ICD should report VK_SUCCESS in case the loader provided interface version
2268is newer than that supported by the ICD, as it's the loader's responsibility to
2269determine whether it can support the older interface version supported by the
2270ICD. The ICD should also report VK_SUCCESS in the case its interface version
2271is greater than the loader's, but return the loader's version. Thus, upon
2272return of VK_SUCCESS the "pSupportedVersion" will contain the desired interface
2273version to be used by the ICD.
2274
2275If the loader receives an interface version from the ICD that the loader no
2276longer supports (due to deprecation), or it receives a
2277VK_ERROR_INCOMPATIBLE_DRIVER error instead of VK_SUCCESS, then the loader will
2278treat the ICD as incompatible and will not load it for use. In this case, the
2279application will not see the ICDs `vkPhysicalDevice` during enumeration.
2280
2281###### Interfacing With Legacy ICDs or Loader
2282
2283If a loader sees that an ICD does not export the
2284`vk_icdNegotiateLoaderICDInterfaceVersion` function, then the loader assumes the
2285corresponding ICD only supports either interface version 0 or 1.
2286
2287From the other side of the interface, if an ICD sees a call to
2288`vk_icdGetInstanceProcAddr` before a call to
2289`vk_icdGetLoaderICDInterfaceVersion`, then it knows that loader making the calls
2290is a legacy loader supporting version 0 or 1. If the loader calls
2291`vk_icdGetInstanceProcAddr` first, it supports at least version 1. Otherwise,
2292the loader only supports version 0.
2293
2294
2295##### Loader Version 4 Interface Requirements
2296
2297The major change to version 4 of the loader/ICD interface is the support of
2298[Unknown Physical Device Extensions](#icd-unknown-physical-device-
2299extensions] using the `vk_icdGetPhysicalDeviceProcAddr` function. This
2300function is purely optional. However, if an ICD supports a Physical Device
2301extension, it must provide a `vk_icdGetPhysicalDeviceProcAddr` function.
2302Otherwise, the loader will continue to treat any unknown functions as VkDevice
2303functions and cause invalid behavior.
2304
2305
2306##### Loader Version 3 Interface Requirements
2307
2308The primary change that occurred in version 3 of the loader/ICD interface was to
2309allow an ICD to handle creation/destruction of their own KHR_surfaces. Up until
2310this point, the loader created a surface object that was used by all ICDs.
2311However, some ICDs may want to provide their own surface handles. If an ICD
2312chooses to enable this support, it must export support for version 3 of the
2313loader/ICD interface, as well as any Vulkan function that uses a KHR_surface
2314handle, such as:
2315- `vkCreateXXXSurfaceKHR` (where XXX is the platform specific identifier [i.e.
2316`vkCreateWin32SurfaceKHR` for Windows])
2317- `vkDestroySurfaceKHR`
2318- `vkCreateSwapchainKHR`
2319- `vkGetPhysicalDeviceSurfaceSupportKHR`
2320- `vkGetPhysicalDeviceSurfaceCapabilitiesKHR`
2321- `vkGetPhysicalDeviceSurfaceFormatsKHR`
2322- `vkGetPhysicalDeviceSurfacePresentModesKHR`
2323
2324An ICD can still choose to not take advantage of this functionality by simply
2325not exposing the above the `vkCreateXXXSurfaceKHR` and `vkDestroySurfaceKHR`
2326functions.
2327
2328
2329##### Loader Version 2 Interface Requirements
2330
2331Version 2 interface has requirements in three areas:
2332 1. ICD Vulkan entry-point discovery,
2333 2. `KHR_surface` related requirements in the WSI extensions,
2334 3. Vulkan dispatchable object creation requirements.
2335
2336##### Loader Versions 0 and 1 Interface Requirements
2337
2338Version 0 and 1 interfaces do not support version negotiation via
2339`vk_icdNegotiateLoaderICDInterfaceVersion`. ICDs can distinguish version 0 and
2340version 1 interfaces as follows: if the loader calls `vk_icdGetInstanceProcAddr`
2341first it supports version 1; otherwise the loader only supports version 0.
2342
2343Version 0 interface does not support `vk_icdGetInstanceProcAddr`. Version 0
2344interface requirements for obtaining ICD Vulkan entry-points are as follows:
2345
2346- The function `vkGetInstanceProcAddr` **must be exported** in the ICD library
2347and returns valid function pointers for all the Vulkan API entry-points.
2348- `vkCreateInstance` **must be exported** by the ICD library.
2349- `vkEnumerateInstanceExtensionProperties` **must be exported** by the ICD
2350library.
2351
2352Additional Notes:
2353
2354- The loader will filter out extensions requested in `vkCreateInstance` and
2355`vkCreateDevice` before calling into the ICD; Filtering will be of extensions
2356advertised by entities (e.g. layers) different from the ICD in question.
2357- The loader will not call the ICD for `vkEnumerate\*LayerProperties`() as layer
2358properties are obtained from the layer libraries and layer JSON files.
2359- If an ICD library author wants to implement a layer, it can do so by having
2360the appropriate layer JSON manifest file refer to the ICD library file.
2361- The loader will not call the ICD for
2362 `vkEnumerate\*ExtensionProperties` if "pLayerName" is not equal to `NULL`.
2363- ICDs creating new dispatchable objects via device extensions need to
2364initialize the created dispatchable object. The loader has generic *trampoline*
2365code for unknown device extensions. This generic *trampoline* code doesn't
2366initialize the dispatch table within the newly created object. See the
2367[Creating New Dispatchable Objects](#creating-new-dispatchable-objects) section
2368for more information on how to initialize created dispatchable objects for
2369extensions non known by the loader.
2370
2371
2372#### Android ICD Negotiation
2373
2374The Android loader uses the same protocol for initializing the dispatch
2375table as described above. The only difference is that the Android
2376loader queries layer and extension information directly from the
2377respective libraries and does not use the json manifest files used
2378by the Windows and Linux loaders.
2379
2380
2381## Glossary of Terms
2382
2383| Field Name | Field Value |
2384|----------------|--------------------|
2385| 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. |
2386| 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. |
2387| Core Function | A function that is already part of the Vulkan core specification and not an extension. For example, vkCreateDevice(). |
2388| 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 |
2389| 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. |
2390| 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. |
2391| 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. |
2392| 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). |
2393| 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.
2394| 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. |
2395| 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 |
2396| 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. |
2397| 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. |
2398| 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. |
2399| 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).
2400| 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. |
2401| 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. |
2402| 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. |