本文主要是介绍如何在 ubuntu 机器上安装并编译Vulkan程序。
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install libglm-dev cmake libxcb-dri3-0 libxcb-present0 libpciaccess0 libpng-dev libxcb-keysyms1-dev libxcb-dri3-dev libx11-dev libmirclient-dev libwayland-dev libxrandr-dev libxcb-ewmh-dev
sudo apt-get install git libpython2.7
官方网站:https://vulkan.lunarg.com/ 下载SDK包:vulkan-sdk.tar.gz
cd ~/Program
mkdir vulkan
cd vulkan
tar zxf $HOME/Downloads/vulkan-sdk.tar.gz
解压后的文件目录如下:
文件夹 | 描述 |
---|---|
x86_64/bin | Vulkan工具和命令 |
x86_64/include/vulkan | 编译链接头文件 |
x86_64/lib | Vulkan加载程序库和layer运行时库 |
x86_64/etc/explicit_layer.d | .json 用于Vulkan验证层的清单文件 |
config | 样本层设置文件,系统配置说明 |
doc | Khronos SPIRV,Vulkan,Loader和WSI规范 |
examples | Vulkan程序示例 |
samples | Vulkan的示例代码 |
source | glslang和所选Vulkan组件的源和调试库 |
source ~/Program/vulkan/1.1.106.0/setup-env.sh
export VULKAN_SDK=~/vulkan/1.1.106.0/x86_64
export PATH=$VULKAN_SDK/bin:$PATH
export LD_LIBRARY_PATH=$VULKAN_SDK/lib:$LD_LIBRARY_PATH
export VK_LAYER_PATH=$VULKAN_SDK/etc/explicit_layer.d
大功告成,检查一下:输入vulkaninfo
[email protected] ~ vulkaninfo
Cannot create Vulkan instance.
/home/lunarg/sdk-build/Vulkan-Tools/vulkaninfo/vulkaninfo.c:921: failed with VK_ERROR_INCOMPATIBLE_DRIVER
wtf, 发生了什么?等等,貌似我们没有检查是否安装了驱动。。。
sudo add-apt-repository ppa:oibaf/graphics-drivers
sudo apt update
sudo apt upgrade
apt install libvulkan1 mesa-vulkan-drivers vulkan-utils
// 或者试试:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt upgrade
sudo apt install nvidia-graphics-drivers-396 nvidia-settings vulkan vulkan-utils
检查一下:输入vulkaninfo
或者:vulkaninfo --html , 在当前目录生成 vulkaninfo.html 文件,用浏览器打开,可以看到更多信息
cd Program/vulkan/1.1.160.0/examples
mkdir build
cd build
cmake ..
make
./vkcube
Vulkan本身是一个与平台无关的API,不包括用于创建显示渲染结果的窗口的工具。 为了从Vulkan的跨平台优势中受益并避免X11的限制,我们将使用GLFW库来创建一个支持Windows,Linux和MacOS的窗口。
还有其他可用于此目的的库,如SDL,但GLFW的优势在于它除了窗口创建之外,还抽象了Vulkan中一些其他特定于平台的东西。
我们将从源代码安装GLFW而不是使用软件包,因为Vulkan支持需要最新版本。 可以在官方网站上找到这些来源。
将源代码解压缩到一个方便的目录,并使用CMakeLists.txt等文件打开目录中的终端。
github地址:https://github.com/glfw/glfw
解压后进入其主目录:cmake .
有报错:
-- Looking for shmat - found
-- Found X11: /usr/lib/x86_64-linux-gnu/libX11.so
CMake Error at CMakeLists.txt:220 (message):
The Xinerama headers were not found
-- Configuring incomplete, errors occurred!
See also "/home/jh/Program/vulkan/glfw-master/CMakeFiles/CMakeOutput.log".
See also "/home/jh/Program/vulkan/glfw-master/CMakeFiles/CMakeError.log".
缺少环境:
$ ~/Program/vulkan/glfw-master sudo apt-get install libsdl2-dev
然后重新: cmake .
? ~/Program/vulkan/glfw-master cmake .
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Using X11 for window creation
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jh/Program/vulkan/glfw-master
濡染没有报错,但这个也是没有成功的:缺少Doxygen
sudo apt-get install doxygen
重新: cmake .
最后: make
成功后:sudo make install
与DirectX 12不同,Vulkan不包含用于线性代数运算的库,因此我们必须下载一个。
GLM是一个很好的库,设计用于图形API,也常用于OpenGL。
它是一个只有头的库,可以从libglm-dev包安装:
sudo apt install libglm-dev
$ gcc -o main main.cpp -lglfw3
In file included from main.cpp:2:
/usr/local/include/GLFW/glfw3.h:215:12: fatal error: vulkan/vulkan.h: 没有那个文件或目录
#include
^~~~~~~~~~~~~~~~~
compilation terminated.
当然是环境设置的问题:需要将头文件等拷贝到对应system文件夹
cd Program/vulkan/1.1.160.0/x86_64
sudo cp -r include/vulkan/ /usr/local/include/
sudo cp -P lib/libvulkan.so* /usr/local/lib/
sudo cp lib/libVkLayer_*.so /usr/local/lib/
sudo mkdir -p /usr/local/share/vulkan/explicit_layer.d
sudo cp etc/explicit_layer.d/VkLayer_*.json /usr/local/share/vulkan/explicit_layer.d
sudo ldconfig
make
g++ -std=c++17 -I/home/jh/Program/vulkan/1.1.160.0/x86_64/include -o VulkanTest main.cpp -L/home/jh/Program/vulkan/1.1.160.0/x86_64/lib -lvulkan -lglfw3
/usr/bin/ld: //usr/local/lib/libglfw3.a(vulkan.c.o): undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
/usr/bin/ld: //lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [Makefile:8:VulkanTest] 错误 1
cd /usr/lib/
ll |grep -iE “liblog”
没有文件
https://github.com/google/glog
下载解压, 进入解压后的主目录,执行:
./autogen.sh && ./configure && make && make install
如果报错:autogen.sh: 5: autogen.sh: autoreconf: not found
sudo apt-get install autoconf
sudo apt-get install automake
sudo apt-get install libtool
重新执行:./autogen.sh && ./configure && make && make install
可能还有权限问题:
-- Up-to-date: /usr/local/include/GLFW
CMake Error at cmake_install.cmake:41 (file):
file INSTALL cannot set permissions on "/usr/local/include/GLFW"
make: *** [Makefile:118:install] 错误 1
执行:
cd …
chmod a+x glog-master -R
cd glog-master
make && make install
okay,现在可以在Makefile文件中添加:-lglog
重新 make 还是会存在这个问题,笔者查阅各种资料,大部分指明在Makefile中加入 -ldl 即可,尝试后失败。
最后发现是 Makefile 文件写的有问题,改成如[#5.2]即可.
make 成功后,会在当前目录生成 VulkanTest 可执行文件:
./VulkanTest
#define GLFW_INCLUDE_VULKAN
#include
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include
#include
#include
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" << std::endl;
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
VULKAN_SDK_PATH = /home/jh/Program/vulkan/1.1.160.0/x86_64
CFLAGS = -std=c++17 -I$(VULKAN_SDK_PATH)/include
LDFLAGS = -L$(VULKAN_SDK_PATH)/lib -lvulkan `pkg-config --static --libs glfw3`
LDFLAGS += -ldl
VulkanTest: main.cpp
g++ $(CFLAGS) -o VulkanTest main.cpp $(LDFLAGS)
.PHONY: test clean
test: VulkanTest
LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib
VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/explicit_layer.d ./VulkanTest
clean:rm -f VulkanTest