在 Windows Subsystem for Linux (WSL2) 的 Ubuntu 系统上配置 Vulkan 开发环境
在 Windows Subsystem for Linux (WSL2) 的 Ubuntu 系统上配置 Vulkan 开发环境
在 Windows Subsystem for Linux (WSL2) 的 Ubuntu 系统上配置 Vulkan 开发环境
Vulkan Tutorial
https://vulkan-tutorial.com/
Development environment - Linux
https://vulkan-tutorial.com/Development_environment
1 Vulkan - Cross platform 3D Graphics
Vulkan is a new generation graphics and compute API that provides high-efficiency, cross-platform access to modern graphics processing units (GPUs), which are used in a wide variety of devices from PCs and consoles to mobile phones and embedded platforms. The Vulkan API was created by the Khronos Group, a consortium of technology hardware and software companies.
Vulkan is a low-overhead, cross-platform API, open standard for 3D graphics and computing. Vulkan targets high-performance real-time 3D graphics applications, such as video games and interactive media. Vulkan is intended to offer higher performance and more efficient CPU and GPU usage compared to older OpenGL and Direct3D 11 APIs. It provides a considerably lower-level API for the application than the older APIs, making Vulkan comparable to Apple’s Metal API and Microsoft’s Direct3D 12 and harder to use than the higher-level OpenGL and Direct3D 11 APIs. In addition to its lower CPU usage, Vulkan is designed to allow developers to better distribute work among multiple CPU cores.
Vulkan 是一个低开销、跨平台的 3D graphics and computing 的应用程序接口 (API)。Vulkan 针对全平台实时 3D 图形程序而设计,并提供高性能与更均衡的 CPU 与 GPU 利用率。Vulkan 是一个底层 API,而且能执行并行任务,Vulkan 还能更好地分配多个 CPU 核心的使用。
Vulkan was first announced by the non-profit Khronos Group at GDC 2015. The Vulkan API was initially referred to as the “next generation OpenGL initiative
”, or “OpenGL next
” by Khronos, but use of those names was discontinued when “Vulkan” was announced.
Vulkan 由 Khronos Group 在 2015 年游戏开发者大会 (GDC) 上发表。正式宣布 Vulkan 之后这些名字就没有再使用了。
Vulkan is derived from and built upon components of AMD’s Mantle API, which was donated by AMD to Khronos with the intent of giving Khronos a foundation on which to begin developing a low-level API that they could standardize across the industry.
Vulkan 基于 AMD’s Mantle API 构建,AMD 将其捐赠给 Khronos Group,给予该组织开发底层 API 的基础,使其像 OpenGL 一样成为行业标准。
Vulkan offers lower overhead, more direct control over the GPU, and lower CPU usage.
Vulkan 旨在提供更低的 CPU 开销与更直接的 GPU 控制。
console [kənˈsəʊl]:vt. 安慰,抚慰,慰藉 n. (机器、电子设备等的) 控制台,操纵台,仪表板
consortium [kənˈsɔː(r)tiəm]:n. 联盟,(合作进行某项工程的) 财团,银团,联营企业
All of the tools we’ll use, with the exception of the compiler, are compatible with Windows, Linux and MacOS.
1.1 Vulkan Packages
Essentials tools, documentation and libraries for every Vulkan developer.
The most important components you’ll need for developing Vulkan applications on Linux are the Vulkan loader, validation layers, and a couple of command-line utilities to test whether your machine is Vulkan-capable.
在 Linux 上开发 Vulkan 应用程序所需的最重要组件是 Vulkan 加载器、校验层和命令行程序,用于测试你的机器是否支持 Vulkan。
sudo apt install vulkan-tools
: Command-line utilities, most importantlyvulkaninfo
andvkcube
. Run these to confirm your machine supports Vulkan.sudo apt install libvulkan-dev
: Installs Vulkan loader. The loader looks up the functions in the driver at runtime, similarly to GLEW for OpenGL.sudo apt install vulkan-validationlayers-dev spirv-tools
: Installs the standard validation layers and required SPIR-V tools. These are crucial when debugging Vulkan applications, and we’ll discuss them in the upcoming chapter.
Remember to run vkcube
and ensure you see the following pop up in a window:
You don’t have to create an account, but it will give you access to some additional documentation that may be useful to you.
Vulkan SDK 可以从 LunarG 的网站上免费下载。
- Vulkan SDK for Windows, Linux, and macOS
https://vulkan.lunarg.com/
LunarG has developed the quintessential developer SDK including build tools, documentation, libraries and more.
Google gives you everything you need to incorporate Vulkan into your Android games and other apps where graphics performance is key.
Visit the Android developer website to download the API, samples, and documentation: resources to help you hit the ground running.
- Arm Mali SDK
https://github.com/ARM-software/vulkan-sdk
ARM’s Vulkan Software Development Kit is a collection of resources to help you build Vulkan applications for a platform with a Mali GPU and an ARM processor. You can use it for creating new applications, training, and exploration of implementation possibilities.
- PowerVR SDK
https://github.com/powervr-graphics/Native_SDK
The PowerVR SDK is an open source codebase to help with the development of graphics applications for PowerVR and other platforms. It consists of two main parts: the Framework and a set of examples.
quintessential [.kwɪntɪ'senʃ(ə)l]:adj. 精髓的,最完美的
essential [ɪ'senʃ(ə)l]:n. 要点,要素,实质,必需品 adj. 完全必要的,必不可少的,极其重要的,本质的
1.2 下载 Vulkan SDK (Linux)
https://vulkan.lunarg.com/
https://vulkan.lunarg.com/sdk/home
Download the latest Vulkan SDK from vulkan.LunarG.com.
You should have a compiler that supports C++17 (GCC 7+ or Clang 5+). You’ll also need make
.
The Khronos Vulkan API is an explicit, low-overhead, cross-platform graphics and compute API. Vulkan provides applications with control over the system execution and the system memory to maximize application efficiency on a wide variety of devices from PCs and consoles to mobile phones and embedded platforms.
The Vulkan SDK enables Vulkan developers to develop Vulkan applications. The Vulkan SDK includes:
- Vulkan API usage validation thanks to the Khronos Validation layer.
- Vulkan Layers configuration thanks to Vulkan Configurator.
- SPIR-V Shader compilation, optimization and validation thanks to DXC, SPIR-V Tools and SPIR-V Cross.
- Vulkan API capture and replay for Vulkan debugging thanks to GFXReconstruct.
- Vulkan system report thanks to Vulkan Installation Analyzer (VIA), Vulkan Hardware Capability Viewer, and Vulkan Info.
- Third-party libraries and tools such as Volk, and VMA.
Note: The Vulkan Installation Analyzer may be deprecated at some point in the future. The Vulkan loader debugging output has been expanded to replace this tool.
All documentation for the SDK can be found and downloaded online at vulkan.lunarg.com.
Important: This SDK does not install Vulkan drivers!! It only provides tools and libraries for application development and debugging. If you are looking for drivers, please refer to your system GPU vendor’s website.
With each SDK release, a release of the Khronos Validation Layer binaries for Android are released on the Validation layer Repository. The Android binaries are not included in this SDK.
Version 1.3.239.0:
-
SDK - SDK Installer
vulkansdk-linux-x86_64-1.3.239.0.tar.gz (330MB) -
SDK Config - Config.json
config.json (0MB)
复制链接地址:
yongqiang@yongqiang:~$ mkdir vulkan_sdk
yongqiang@yongqiang:~$ cd vulkan_sdk/
yongqiang@yongqiang:~/vulkan_sdk$ wget https://sdk.lunarg.com/sdk/download/1.3.239.0/linux/config.json
--2023-02-23 23:54:28-- https://sdk.lunarg.com/sdk/download/1.3.239.0/linux/config.json
Resolving sdk.lunarg.com (sdk.lunarg.com)... 104.26.0.199, 172.67.73.90, 104.26.1.199, ...
Connecting to sdk.lunarg.com (sdk.lunarg.com)|104.26.0.199|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8921 (8.7K) [application/json]
Saving to: ‘config.json’
config.json 100%[=================================================>] 8.71K --.-KB/s in 0.006s
2023-02-23 23:54:30 (1.39 MB/s) - ‘config.json’ saved [8921/8921]
yongqiang@yongqiang:~/vulkan_sdk$ wget https://sdk.lunarg.com/sdk/download/1.3.239.0/linux/vulkansdk-linux-x86_64-1.3.239.0.tar.gz
--2023-02-23 23:54:48-- https://sdk.lunarg.com/sdk/download/1.3.239.0/linux/vulkansdk-linux-x86_64-1.3.239.0.tar.gz
Resolving sdk.lunarg.com (sdk.lunarg.com)... 104.26.0.199, 172.67.73.90, 104.26.1.199, ...
Connecting to sdk.lunarg.com (sdk.lunarg.com)|104.26.0.199|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 346518877 (330M) [application/octet-stream]
Saving to: ‘vulkansdk-linux-x86_64-1.3.239.0.tar.gz’
vulkansdk-linux-x86_64-1.3.239.0 100%[========================================================>] 330.47M 11.3MB/s in 37s
2023-02-23 23:55:26 (8.91 MB/s) - ‘vulkansdk-linux-x86_64-1.3.239.0.tar.gz’ saved [346518877/346518877]
yongqiang@yongqiang:~/vulkan_sdk$
yongqiang@yongqiang:~/vulkan_sdk$ ls -l
total 338412
-rw-r--r-- 1 yongqiang yongqiang 8921 Jan 28 02:44 config.json
-rw-r--r-- 1 yongqiang yongqiang 346518877 Jan 28 02:45 vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan_sdk$
yongqiang@yongqiang:~/vulkan_sdk$ chmod a+x ./vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan_sdk$
yongqiang@yongqiang:~/vulkan_sdk$ ls -l
total 338412
-rw-r--r-- 1 yongqiang yongqiang 8921 Jan 28 02:44 config.json
-rwxr-xr-x 1 yongqiang yongqiang 346518877 Jan 28 02:45 vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan_sdk$
1.2.1 Vulkan Docs
https://vulkan.lunarg.com/doc/sdk/latest/linux/getting_started.html
Getting Started with the Linux Tarball Vulkan SDK
1.2.2 SDK Versioning
The components in this SDK are built with a specific version of Khronos Vulkan API header, whose version is reflected in the SDK’s version number. For example, SDK version v.w.xx.0
indicates the SDK uses the Vulkan header revision where:
v
is the Vulkan major versionw
is the Vulkan minor versionxx
is the Vulkan patch version (e.g., 24)
The last number in the SDK version indicates the revision of an SDK for the given Vulkan header revision. It is used in case it is necessary to release multiple SDKs for the same version of Vulkan.
For example, SDK version 1.1.70.0
indicates the SDK uses Vulkan header revision 1.1.70
.
Note: A more recent SDK version number does not indicate an application cannot be developed for earlier Vulkan versions. As well, an older SDK version can not be used to develop applications for a newer version of Vulkan. For example, the 1.1.130.0 SDK can be used to develop Vulkan 1.0 applications but cannot be used to develop applications for Vulkan 1.2. Furthermore, the presence of a 1.2.xx SDK does not necessarily indicate a system can actually run Vulkan 1.2. A Vulkan 1.2 driver is required for applications to use most Vulkan 1.2 functionality.
presence [ˈprez(ə)ns]:n. 存在,出现,在场,出席
Once you have installed a Vulkan driver on your system, there is a version for your Vulkan Loader, and a version for each physical device.
- Vulkan Instance Version. This is the version of your Vulkan Loader. If you run
vulkaninfo
, the Vulkan Instance Version is the first item reported. - For each physical device, there is an
apiVersion
. TheapiVersion
is the version of your Vulkan driver. You can find theapiVersion
for each physical device from yourvulkaninfo
output under the section labeled “Device Properties and Extensions”.
1.3 System requirements
Your Linux development and host systems must meet specific hardware and software requirements to install the Vulkan SDK and create Vulkan applications. You will need a Vulkan ICD to execute Vulkan applications.
需要一个 Vulkan ICD 来执行 Vulkan 应用程序。
The Vulkan SDK is supported on 64-bit Linux systems. This guide provides instructions for the following Linux distributions:
- Ubuntu 20.04 and 22.04
- Fedora CoreOS (36 and 37)
- Arch Linux (latest)
LunarG does extensive testing on the Ubuntu distributions and touch testing on the other Linux distributions. Additional Linux distributions may work but have not been verified. If the pre-built binaries don’t run on your system we provide the source and a helper script to build these components. Also, you can search your distribution’s package manager for Vulkan development libraries.
LunarG 在 Ubuntu 发行版上进行了大量测试,并在其他 Linux 发行版上进行了 touch testing。其他 Linux 发行版可能有效,但尚未经过验证。如果预构建的二进制文件不能在您的系统上运行,我们会提供源代码和帮助脚本来构建这些组件。此外,您还可以在您的发行版包管理器中搜索 Vulkan 开发库。
Otherwise you may download and build from publicly available repositories. Everything provided in the SDK is open source.
SDK 中提供的所有内容都是开源的。
The following list are the core repositories that the SDK is composed of:
- Khronos Vulkan Headers: https://github.com/KhronosGroup/Vulkan-Headers
- Khronos Vulkan Loader source: https://github.com/KhronosGroup/Vulkan-Loader
- Khronos Vulkan Validation Layers source: https://github.com/KhronosGroup/Vulkan-ValidationLayers
- Khronos Vulkan Tools source: https://github.com/KhronosGroup/Vulkan-Tools
- LunarG Vulkan Tools source: https://github.com/LunarG/VulkanTools
- Khronos glslang source: https://github.com/KhronosGroup/glslang
- Google Shaderc source: https://github.com/google/shaderc
- Khronos SPIRV-Headers source: https://github.com/KhronosGroup/SPIRV-Headers
- Khronos SPIRV-Tools source: https://github.com/KhronosGroup/SPIRV-Tools
- Khronos SPIRV-Cross source: https://github.com/KhronosGroup/SPIRV-Cross
- LunarG GFXReconstruct source: https://github.com/LunarG/gfxreconstruct
- Khronos SPIRV-Reflect source: https://github.com/KhronosGroup/SPIRV-Reflect
- Khronos Vulkan Extension Layer source: https://github.com/KhronosGroup/Vulkan-ExtensionLayer
- Khronos Vulkan Profiles: https://github.com/KhronosGroup/Vulkan-Profiles
- Microsoft DirectX Shader Compiler: https://github.com/microsoft/DirectXShaderCompiler
- Volk Vulkan Meta Loader: https://github.com/zeux/volk
- Vulkan Memory Allocator Library: https://gpuopen.com/vulkan-memory-allocator/
- Vulkan Caps Viewer: https://github.com/SaschaWillems/VulkanCapsViewer
You will find instructions to build all of the above in their respective repositories.
1.3.1 Ubuntu distributions (20.04 and 22.04)
Upgrade your system to be current on package updates:
sudo apt update && sudo apt upgrade -y
If you want to clone and build any of the repositories using the vulkansdk
script, install the following prerequisite packages:
sudo apt install libglm-dev cmake libxcb-dri3-0 libxcb-present0 libpciaccess0 \
libpng-dev libxcb-keysyms1-dev libxcb-dri3-dev libx11-dev g++ gcc g++-multilib \
libmirclient-dev libwayland-dev libxrandr-dev libxcb-randr0-dev libxcb-ewmh-dev \
git python python3 bison libx11-xcb-dev liblz4-dev libzstd-dev python3-distutils \
ocaml-core ninja-build pkg-config libxml2-dev wayland-protocols python3-jsonschema
For ubuntu 20.04, to install qt5 run:
sudo apt install qt5-default
For Ubuntu 22.04, to install qt5 run:
sudo apt install qtbase5-dev
Minimum CMake 3.17.2 version is required.
1.4 安装 Vulkan SDK (Linux)
The Vulkan SDK installation process consists of extracting the tar
file into a desired directory. The extracted file is a folder in the form 1.x.yy.z
and can be placed anywhere on your system.
Vulkan SDK 安装过程包括将 tar
文件提取到所需目录中。提取的文件是一个文件夹,格式为 1.x.yy.z
,可以放在系统的任何位置。
- Create a work directory.
These instructions assume you want to install the SDK under a folder called vulkan
, which can be placed anywhere on your system. In this case, it is your HOME directory.
cd ~
mkdir vulkan
cd vulkan
- Generate the sha256sum and ensure it matches with the one listed on vulkan.lunarg.com located next to the SDK download link.
Assuming you had downloaded the package to /home/yongqiang/vulkan
:
sha256sum ./vulkansdk-linux-x86_64-1.x.yy.z.tar.gz
yongqiang@yongqiang:~/vulkan$ pwd
/home/yongqiang/vulkan
yongqiang@yongqiang:~/vulkan$
yongqiang@yongqiang:~/vulkan$ ls -l
total 338412
-rw-r--r-- 1 yongqiang yongqiang 8921 Jan 28 02:44 config.json
-rwxr-xr-x 1 yongqiang yongqiang 346518877 Jan 28 02:45 vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan$
yongqiang@yongqiang:~/vulkan$ sha256sum ./vulkansdk-linux-x86_64-1.3.239.0.tar.gz
1ac4e3b23f27570535026506fd9fcaf5ed56f8562c87cdd60afe7adf58b037e8 ./vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan$
- Extract the SDK package.
Assuming you had downloaded the package to /home/yongqiang/vulkan
:
tar xf ./vulkansdk-linux-x86_64-1.x.yy.z.tar.gz
yongqiang@yongqiang:~/vulkan$ tar xf ./vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan$ ls -l
total 338416
drwxr-xr-x 5 yongqiang yongqiang 4096 Jan 28 02:43 1.3.239.0
-rw-r--r-- 1 yongqiang yongqiang 8921 Jan 28 02:44 config.json
-rwxr-xr-x 1 yongqiang yongqiang 346518877 Jan 28 02:45 vulkansdk-linux-x86_64-1.3.239.0.tar.gz
yongqiang@yongqiang:~/vulkan$
- Install runtime dependencies if they are not already installed:
Ubuntu 22.04:
sudo apt install qtbase5-dev libxcb-xinput0 libxcb-xinerama0
Ubuntu 20.04:
sudo apt install qt5-default libxcb-xinput0 libxcb-xinerama0
Note: Packaging of the Linux Vulkan SDK has changed from a .run
file to a .tar.gz
file starting from release version 1.1.77.0
and forward. The run file previously created a VulkanSDK
directory and then expanded the SDK into the VulkanSDK/1.1.xx.y
directory. The .tar.gz
file now simply extracts the SDK into a directory of the form 1.x.yy.z
.
从发布版本 1.1.77.0
开始,Linux Vulkan SDK 的打包已从 .run
文件更改为 .tar.gz
文件。
1.4.1 Tar file contents
The table below describes the filesystem unpacked from the SDK tar file, which appears under the top-level 1.x.yy.z
directory created when the SDK was extracted. The top-level SDK directory contains a platform-specific folder; for 64-bit Intel CPU targets, it is named x86_64.
Directory | Description |
---|---|
x86_64/bin | Vulkan tools and commands |
x86_64/include | Header files useful for your Vulkan application. Includes Vulkan, shader tools, etc. |
x86_64/lib | Vulkan loader library and layer runtime libraries |
x86_64/etc/vulkan/explicit_layer.d | .json manifest files for Vulkan validation layers |
x86_64/share/vulkan | Configurations for profiles and Vulkan registry information |
config | Sample layer settings file |
source | Source files |
manifest [ˈmænɪfest]:v. 表明,显现,使人注意到 adj. 明显的,显而易见的 n. 旅客名单
yongqiang@yongqiang:~/vulkan/1.3.239.0$ ls -l
total 52
-rw-r--r-- 1 yongqiang yongqiang 944 Jan 28 02:43 LICENSE.txt
-rw-r--r-- 1 yongqiang yongqiang 304 Jan 28 02:43 README.txt
drwxr-xr-x 2 yongqiang yongqiang 4096 Jan 28 02:43 config
-rw-r--r-- 1 yongqiang yongqiang 582 Jan 28 02:23 setup-env.sh
drwxr-xr-x 21 yongqiang yongqiang 4096 Jan 28 02:22 source
-rwxr-xr-x 1 yongqiang yongqiang 25975 Jan 28 02:23 vulkansdk
drwxr-xr-x 7 yongqiang yongqiang 4096 Jan 28 02:20 x86_64
yongqiang@yongqiang:~/vulkan/1.3.239.0$
yongqiang@yongqiang:~/vulkan/1.3.239.0$ cd x86_64/
yongqiang@yongqiang:~/vulkan/1.3.239.0/x86_64$ ls -l
total 20
drwxr-xr-x 2 yongqiang yongqiang 4096 Jan 28 02:43 bin
drwxr-xr-x 3 yongqiang yongqiang 4096 Jan 28 02:20 etc
drwxr-xr-x 13 yongqiang yongqiang 4096 Jan 28 02:43 include
drwxr-xr-x 3 yongqiang yongqiang 4096 Jan 28 02:43 lib
drwxr-xr-x 4 yongqiang yongqiang 4096 Jan 28 02:43 share
yongqiang@yongqiang:~/vulkan/1.3.239.0/x86_64$
1.4.2 System file locations
If you have installed a Vulkan driver from a GPU vendor, you may wish to verify there is a .json
manifest file for that driver installed. This will normally be located in either /etc/vulkan/icd.d/
or /usr/share/vulkan/icd.d
(though other locations are possible). These directories will contain one or more Vulkan driver .json
manifest files, which are not modified by the SDK installer. Refer to the Vulkan Loader guide in the “ICD Discovery on Linux” section for detailed information on ICD and layer library search order and paths for this platform.
如果你安装了来自 GPU 供应商的 Vulkan driver,你可能希望验证安装了该驱动程序的 .json
manifest file。通常位于 /etc/vulkan/icd.d/
或 /usr/share/vulkan/icd.d
(尽管其他位置也是可能的)。这些目录将包含一个或多个 Vulkan driver .json
manifest files,SDK 安装程序不会修改这些文件。
yongqiang@yongqiang:~$ cd /etc/vulkan
yongqiang@yongqiang:/etc/vulkan$ ls
explicit_layer.d icd.d implicit_layer.d
yongqiang@yongqiang:/etc/vulkan$ cd icd.d/
yongqiang@yongqiang:/etc/vulkan/icd.d$ ls
yongqiang@yongqiang:/etc/vulkan/icd.d$
yongqiang@yongqiang:~$ cd /usr/share/vulkan/
yongqiang@yongqiang:/usr/share/vulkan$ ls
explicit_layer.d icd.d implicit_layer.d registry
yongqiang@yongqiang:/usr/share/vulkan$
yongqiang@yongqiang:/usr/share/vulkan$ cd icd.d/
yongqiang@yongqiang:/usr/share/vulkan/icd.d$ ls
intel_icd.x86_64.json lvp_icd.x86_64.json radeon_icd.x86_64.json
yongqiang@yongqiang:/usr/share/vulkan/icd.d$
1.4.3 Set up the runtime environment
Because you installed the Vulkan SDK in a location of your choice, you need to set environment variables to locate the Vulkan SDK components. These variables are identified in the table below. The VULKAN_SDK
environment variable is set to the platform-specific directory in your SDK installation (e.g. ~/vulkan/1.x.yy.z/x86_64
). The rest of the variables are set using paths relative to VULKAN_SDK
:
Environment Variable | File/Path to Include | Notes |
---|---|---|
PATH | $VULKAN_SDK/bin | SDK executables |
LD_LIBRARY_PATH | $VULKAN_SDK/lib | Dynamically-loaded libraries (e.g., layers) |
VK_LAYER_PATH | $VULKAN_SDK/etc/vulkan/explicit_layer.d | Only the SDK layer manifest .json files |
VK_ADD_LAYER_PATH | $VULKAN_SDK/etc/vulkan/explicit_layer.d | The SDK and system layer manifest .json files |
The script sets the VK_ADD_LAYER_PATH
environment variable to find the SDK layers first, followed by any layers installed on your system. VK_LAYER_PATH
overrides VK_ADD_LAYER_PATH
completely, so the script unsets VK_LAYER_PATH
if it is set before the script is executed. You can set VK_LAYER_PATH
after running the setup script if you wish to specify a custom path that does not include the system layer paths.
Use the source command to load that script into your current shell:
source ~/vulkan/1.x.yy.z/setup-env.sh
Of course, you would replace xx
and y
with the actual SDK version numbers.
Alternatively you can setup your paths by setting these environment variables yourself:
export VULKAN_SDK=~/vulkan/1.x.yy.z/x86_64
export PATH=$VULKAN_SDK/bin:$PATH
export LD_LIBRARY_PATH=$VULKAN_SDK/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export VK_LAYER_PATH=$VULKAN_SDK/etc/vulkan/explicit_layer.d
yongqiang@yongqiang:~/vulkan/1.3.239.0/x86_64$ pwd
/home/yongqiang/vulkan/1.3.239.0/x86_64
yongqiang@yongqiang:~/vulkan/1.3.239.0/x86_64$
环境变量:
export VULKAN_SDK=~/vulkan/1.3.239.0/x86_64
export PATH=$VULKAN_SDK/bin:$PATH
export LD_LIBRARY_PATH=$VULKAN_SDK/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export VK_LAYER_PATH=$VULKAN_SDK/etc/vulkan/explicit_layer.d
yongqiang@yongqiang:~$ vim ~/.bashrc
yongqiang@yongqiang:~$ cat ~/.bashrc
相同的用户打开新的终端时生效,或者手动 source ~/.bashrc
生效,仅对当前用户有效。
yongqiang@yongqiang:~$ source ~/.bashrc
1.4.4 Environment variable persistence
Note that sourcing the setup-env.sh
script or running export
commands only set the environment variables for your current shell session. They do not permanently “setup” any of this configuration for other shell sessions or future logins.
请注意,setup-env.sh
脚本或运行 export
命令只会为当前的 shell 会话设置环境变量。他们不会为其他 shell 会话或未来的登录永久地设置任何此配置。
For example, on most Ubuntu desktop systems, adding a line to your .profile
file to source the setup-env.sh
file sets up the environment variables for you for both shells and programs started from the graphical environment in a desktop session. This should make the environment variables available to desktop tools such as cmake-gui, Qt Creator, or Android Studio.
在你的 .profile
文件中添加一行以执行 setup-env.sh
文件的代码,为你从桌面图形环境启动的 shell 和程序设置环境变量。
persistence [pə(r)ˈsɪstəns]:n. 坚持,持续存在,锲而不舍,维持
1.5 Copying SDK Files to System Directories (不推荐使用)
The binaries included in the SDK are all built from public repositories. You can build each repository from source or it may just be easier to copy the files from the SDK into your system paths. There are also Ubuntu packages that makes installing these files even easier.
你可以从源代码构建每个 repository,或者将文件从 SDK 复制到你的系统路径中可能会更容易。
If you want to install SDK files to your system directories, here are some examples to do so. (Note: some system paths could be different depending on what Linux distro you use)
-
Vulkan Header Files
sudo cp -r $VULKAN_SDK/include/vulkan/ /usr/local/include/
-
Vulkan Loader Files
sudo cp -P $VULKAN_SDK/lib/libvulkan.so* /usr/local/lib/
-
Vulkan Layer Files
sudo cp $VULKAN_SDK/lib/libVkLayer_*.so /usr/local/lib/
sudo mkdir -p /usr/local/share/vulkan/explicit_layer.d
sudo cp $VULKAN_SDK/etc/vulkan/explicit_layer.d/VkLayer_*.json /usr/local/share/vulkan/explicit_layer.d
You may need to run sudo ldconfig
to refresh system loader search cache.
您可能需要运行 sudo ldconfig
来刷新系统加载程序搜索缓存。
1.6 Uninstalling the SDK
To uninstall the SDK simply remove your local installation directory. e.g.
rm -rf ~/vulkan/1.x.yy.z
1.7 Verify the SDK Installation
Verify the installation of the Vulkan SDK by running:
- Vulkan Installation Analyzer (VIA) with the command:
yongqiang@yongqiang:~$ vkvia
WARNING: lavapipe is not a conformant vulkan implementation, testing use only.
WARNING: lavapipe is not a conformant vulkan implementation, testing use only.
VIA_INFO: SDK Found! - Will attempt to run tests
VIA_INFO: Attempting to run ./vkcube in /home/yongqiang/vulkan/1.3.239.0/x86_64/bin
VIA_INFO: Command-line: ./vkcube --c 100 --suppress_popups
Selected GPU 0: llvmpipe (LLVM 12.0.0, 256 bits), type: Cpu
WARNING: lavapipe is not a conformant vulkan implementation, testing use only.
VIA_INFO: Command-line: ./vkcube --c 100 --suppress_popups --validate
WARNING : GENERAL - Message Id Number: 0 | Message Id Name: Loader Message
loader_scanned_icd_add: Driver /usr/lib/x86_64-linux-gnu/libvulkan_radeon.so supports Vulkan 1.2, but only supports loader interface version 4. Interface version 5 or newer required to support this version of Vulkan (Policy #LDP_DRIVER_7)
Objects - 1
Object[0] - VK_OBJECT_TYPE_INSTANCE, Handle 0x555c1ff71300
WARNING : GENERAL - Message Id Number: 0 | Message Id Name: Loader Message
loader_scanned_icd_add: Driver /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so supports Vulkan 1.1, but only supports loader interface version 4. Interface version 5 or newer required to support this version of Vulkan (Policy #LDP_DRIVER_7)
Objects - 1
Object[0] - VK_OBJECT_TYPE_INSTANCE, Handle 0x555c1ff71300
WARNING : GENERAL - Message Id Number: 0 | Message Id Name: Loader Message
loader_scanned_icd_add: Driver /usr/lib/x86_64-linux-gnu/libvulkan_intel.so supports Vulkan 1.2, but only supports loader interface version 4. Interface version 5 or newer required to support this version of Vulkan (Policy #LDP_DRIVER_7)
Objects - 1
Object[0] - VK_OBJECT_TYPE_INSTANCE, Handle 0x555c1ff71300
Selected GPU 0: llvmpipe (LLVM 12.0.0, 256 bits), type: Cpu
WARNING: lavapipe is not a conformant vulkan implementation, testing use only.
SUCCESS: Vulkan analysis completed properly using Vulkan 1.1
yongqiang@yongqiang:~$
- Vulkan Info with the command:
yongqiang@yongqiang:~$ vulkaninfo
WARNING: [Loader Message] Code 0 : loader_scanned_icd_add: Driver /usr/lib/x86_64-linux-gnu/libvulkan_radeon.so supports Vulkan 1.2, but only supports loader interface version 4. Interface version 5 or newer required to support this version of Vulkan (Policy #LDP_DRIVER_7)
WARNING: [Loader Message] Code 0 : loader_scanned_icd_add: Driver /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so supports Vulkan 1.1, but only supports loader interface version 4. Interface version 5 or newer required to support this version of Vulkan (Policy #LDP_DRIVER_7)
WARNING: [Loader Message] Code 0 : loader_scanned_icd_add: Driver /usr/lib/x86_64-linux-gnu/libvulkan_intel.so supports Vulkan 1.2, but only supports loader interface version 4. Interface version 5 or newer required to support this version of Vulkan (Policy #LDP_DRIVER_7)
WARNING: [Loader Message] Code 0 : Layer VK_LAYER_MESA_device_select uses API version 1.2 which is older than the application specified API version of 1.3. May cause issues.
WARNING: lavapipe is not a conformant vulkan implementation, testing use only.
==========
VULKANINFO
==========
Vulkan Instance Version: 1.3.239
Instance Extensions: count = 20
...
- Vulkan Cube with the command:
yongqiang@yongqiang:~$ vkcube
Selected GPU 0: llvmpipe (LLVM 12.0.0, 256 bits), type: Cpu
WARNING: lavapipe is not a conformant vulkan implementation, testing use only.
yongqiang@yongqiang:~$
2 Graphics Library Framework (GLFW)
GLFW (Graphics Library Framework) is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan development on the desktop. It provides a simple API for creating windows, contexts and surfaces, receiving input and events.
GLFW (Graphics Library Framework) 是一个开源轻量级工具程序库。GLFW 的主要功能是创建并管理窗口和上下文,同时还提供了处理手柄、键盘、鼠标输入的功能。
GLFW is written in C and supports Windows, macOS, X11 and Wayland.
GLFW is licensed under the zlib/libpng license.
Vulkan by itself is a platform agnostic API and does not include tools for creating a window to display the rendered results. To benefit from the cross-platform advantages of Vulkan and to avoid the horrors of Win32, we’ll use the GLFW library to create a window, which supports Windows, Linux and MacOS. There are other libraries available for this purpose, like SDL, but the advantage of GLFW is that it also abstracts away some of the other platform-specific things in Vulkan besides just window creation.
Vulkan 是一个平台无关的 API,不包含用于创建窗口以显示渲染结果的工具。为了跨平台和避免陷入 Win32 细节中去,我们将使用 GLFW 库创建窗口,它支持 Windows、Linux 和 MacOS。有其他库可完成类似功能,如 SDL,但 GLFW 的优势在于,除了窗口创建之外,它还抽象出 Vulkan 中其他一些特定于平台的内容。
2.1 安装 GLFW
https://www.glfw.org/download.html
The current version is 3.3.8, which was released on July 22, 2022.
- Source package
This package contains the complete source code with CMake build files, documentation, examples and test programs. It is the recommended download for all platforms and offers the most control.
The master branch is our integration branch for the next feature release while the 3.3-stable branch only adds bug fixes for patch releases.
- Windows pre-compiled binaries
These packages contain the GLFW header files, documentation and release mode static libraries, DLLs and import libraries for Visual C++ 2010-2019 and the 2022 preview, MinGW-w64 and plain MinGW.
Binaries for Visual C++ 2010 and plain MinGW are only available in the 32-bit package.
- macOS pre-compiled binaries
This package contains the GLFW header files, documentation and release mode static and dynamic libraries for macOS 10.8 and later. Both Intel, ARM and Universal binaries are included.
- Linux and BSD binaries
Check if your package system provides GLFW 3.3.8 or another version new enough to be useful to you.
If not, please download and compile GLFW from source. A source package is available above, or you could clone it from GitHub.
https://github.com/glfw/glfw
https://www.glfw.org/docs/latest/compile.html
We’ll be installing GLFW from the following command:
sudo apt install libglfw3-dev
3 OpenGL Mathematics (GLM)
OpenGL Mathematics (GLM) is a header only C++ mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications.
GLM provides classes and functions designed and implemented with the same naming conventions and functionality than GLSL so that anyone who knows GLSL, can use GLM as well in C++.
Unlike DirectX 12, Vulkan does not include a library for linear algebra operations, so we’ll have to download one. GLM is a nice library that is designed for use with graphics APIs and is also commonly used with OpenGL.
与 DirectX 12 不同,Vulkan 不包含用于线性代数运算的库,因此我们必须下载一个。GLM 是一个很好的库,专为与图形 API 一起使用而设计,也常与 OpenGL 一起使用。
GLM is a header-only library.
GLM 是一个只有头文件的库。
3.1 安装 GLM
https://github.com/g-truc/glm/releases
GLM is written in C++98 but can take advantage of C++11 when supported by the compiler.
It is a header-only library that can be installed from the libglm-dev
or glm-devel
package:
sudo apt install libglm-dev
4 Shader Compiler
We have just about all we need, except we’ll want a program to compile shaders from the human-readable GLSL to bytecode.
Two popular shader compilers are Khronos Group’s glslangValidator
and Google’s glslc
. The latter has a familiar GCC- and Clang-like usage, so we’ll go with that: on Ubuntu, download Google’s unofficial binaries and copy glslc
to your /usr/local/bin
. Note you may need to sudo depending on your permissions.
To test, run glslc
and it should rightfully complain we didn’t pass any shaders to compile:
yongqiang@yongqiang:~$ glslc
glslc: error: no input files
yongqiang@yongqiang:~$
shaderc/downloads.md
https://github.com/google/shaderc/blob/main/downloads.md
Debug - Linux - gcc:
yongqiang@yongqiang:~/vulkan$ ls install*
install.tgz
yongqiang@yongqiang:~/vulkan$
yongqiang@yongqiang:~/vulkan$ chmod a+x install.tgz
yongqiang@yongqiang:~/vulkan$
yongqiang@yongqiang:~/vulkan$ tar -xzvf install.tgz
...
yongqiang@yongqiang:~/vulkan$ ls install*
install.tgz
install:
bin include lib share
yongqiang@yongqiang:~/vulkan$
yongqiang@yongqiang:~/vulkan/install/bin$ pwd
/home/yongqiang/vulkan/install/bin
yongqiang@yongqiang:~/vulkan/install/bin$
yongqiang@yongqiang:~/vulkan/install/bin$ sudo cp ./glslc /usr/local/bin/
[sudo] password for yongqiang:
yongqiang@yongqiang:~/vulkan/install/bin$
5 Setting up a makefile project
Create a new directory at a convenient location with a name like VulkanTest
. Create a source file called main.cpp
and insert the following code.
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream>
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Create a new empty file called Makefile
.
CFLAGS
变量来指定编译选项。
We’ll first define a couple of variables to simplify the remainder of the file. Define a CFLAGS
variable that will specify the basic compiler flags:
CFLAGS = -std=c++17 -O2
We’re going to use modern C++ (-std=c++17
), and we’ll set optimization level to -O2
. We can remove -O2
to compile programs faster, but we should remember to place it back for release builds.
LDFLAGS
变量来指定链接选项。
Similarly, define the linker flags in a LDFLAGS
variable:
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
The flag -lglfw
is for GLFW, -lvulkan
links with the Vulkan function loader and the remaining flags are low-level system libraries that GLFW needs. The remaining flags are dependencies of GLFW itself: the threading and window management.
It is possible that the Xxf68vm
and Xi
libraries are not yet installed on your system.
安装 Xxf68vm
and Xi
libraries:
sudo apt install libxxf86vm-dev libxi-dev
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ make test
g++ -std=c++17 -O2 -o VulkanTest main.cpp -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
/usr/bin/ld: cannot find -lXxf86vm
/usr/bin/ld: cannot find -lXi
collect2: error: ld returned 1 exit status
make: *** [Makefile:5: VulkanTest] Error 1
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ sudo apt install libxxf86vm-dev libxi-dev
[sudo] password for yongqiang:
Reading package lists... Done
...
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$
Specifying the rule to compile VulkanTest
is straightforward now. Make sure to use tabs for indentation instead of spaces.
VulkanTest: main.cpp
g++ $(CFLAGS) -o VulkanTest main.cpp $(LDFLAGS)
Verify that this rule works by saving the makefile and running make
in the directory with main.cpp
and Makefile
. This should result in a VulkanTest
executable.
We’ll now define two more rules, test
and clean
, where the former will run the executable and the latter will remove a built executable:
.PHONY: test clean
test: VulkanTest
./VulkanTest
clean:
rm -f VulkanTest
Running make test
should show the program running successfully, and displaying the number of Vulkan extensions. The application should exit with the success return code (0
) when you close the empty window. You should now have a complete makefile that resembles the following:
CFLAGS = -std=c++17 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
VulkanTest: main.cpp
g++ $(CFLAGS) -o VulkanTest main.cpp $(LDFLAGS)
.PHONY: test clean
test: VulkanTest
./VulkanTest
clean:
rm -f VulkanTest
You can now use this directory as a template for your Vulkan projects. Make a copy, rename it to something like HelloTriangle
and remove all of the code in main.cpp
.
yongqiang@yongqiang:~$ mkdir vulkan_workspace
yongqiang@yongqiang:~$ cd vulkan_workspace/
yongqiang@yongqiang:~/vulkan_workspace$ mkdir vulkan_basic_test
yongqiang@yongqiang:~/vulkan_workspace$ cd vulkan_basic_test/
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ touch main.cpp
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ vim main.cpp
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ touch Makefile
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ vim Makefile
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ make clean
rm -f VulkanTest
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$ make test
g++ -std=c++17 -O2 -o VulkanTest main.cpp -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
./VulkanTest
20 extensions supported
yongqiang@yongqiang:~/vulkan_workspace/vulkan_basic_test$
The number of extensions should be non-zero.
6 Terminology
DXC: Microsoft’s Open Source DirectX Shader Compiler
Installable Client Driver (ICD): A Vulkan compatible display driver (Vulkan 兼容的显示驱动程序)
OpenGL Shading Language (GLSL)
DirectX High-Level Shading Language (HLSL)
SPIR-V: Standard Portable Intermediate Representation is a cross-API intermediate language (IL) that natively represents parallel compute and graphics programs
VMA: The Vulkan Memory Allocator library provided by AMD.
Vulkan Instance: The Vulkan API object that stores all per-application states
Vulkan Layer: A library designed to work as a plug-in for the loader. It usually serves to provide validation and debugging functionality to applications
Vulkan Loader: A library which implements the Vulkan API entry points and manages layers, extensions, and drivers. It is found in the independent hardware vendor driver installs
References
https://yongqiang.blog.csdn.net/
Vulkan Tutorial https://vulkan-tutorial.com/
Vulkan 教程 https://geek-docs.com/vulkan/vulkan-tutorial/vulkan-tutorial-index.html
https://vulkan.lunarg.com/doc/sdk/latest/linux/getting_started.html
更多推荐
所有评论(0)