This article is for
Vukan系列的第二篇文章
,上一篇
articles onVulkan进行了简单介绍
, and on其与OpenGL的优劣势进行了比较
, for application developers选择图形API方面提供了建议
. The main content of this article begins below, and the Mac operating system is introduced in detail如何搭建Vulkan开发环境
.
Vulkan is a command buffer based 底层图形API接口
and can be better 利用现代GPU的强大计算能力
, thus 获得更高的渲染性能和更低的CPU开销
. Compared with OpenGL, Vulkan 提供了更详细的硬件控制
, 更高效的内存管理
, 更灵活的管线状态管理
and 多线程支持
etc. have advantages.
At the same time, Vulkan also has some challenges, including 学习曲线较陡峭
, 编程难度较大
etc. During application development, developers need to select a suitable graphics API interface according to actual needs and platform support.
1. Download the Vulkan SDK
Go to the Vulkan official website to download the Vulkan SDK for the corresponding platform:
// Vulkan官网下载对应平台的sdk
https://vulkan.lunarg.com/sdk/home
After installing the Vulkan SDK, you can run it VulkanSDK/1.3.250.0/Applications
in the directory vkcube
. If the following running interface is displayed, it means that your computer supports Vulkan.
2. Install GLFW and GLM
Vulkan is a platform-independent application graphics API interface. The ability to create local windows depends on the support of GLFW and GLM.
GLFW (Graphics Library Framework)
:
An open source library originally used to create OpenGL windows and contexts. It provides a set of cross-platform APIs that can easily create OpenGL windows, handle input events, and handle window events.
GLFW provides a set of extensions for Vulkan, making it easier for developers to create Vulkan windows and contexts, and handle window and input events, etc.GLM (OpenGL Mathematics)
:
GLM is an open source library for mathematical calculations. It provides a set of classes and functions for mathematical operations such as matrix transformation, vector calculation, and projection.
GLM is closely related to Vulkan. When developing Vulkan, GLM provides developers with mathematical operations such as matrix transformation and vector calculation to control the rendering pipeline, making it easier for developers to program 3D graphics.
// 通过homebrew进行glfw与glm
brew install glfw3
brew install glm
3. Configure Xcode
Now that the dependencies are installed, it's time to configure a basic Xcode Vulkan project.
3.1 Create a new Xcode project
Start Xcode and create a new project, select Application > Command Line Tool
the project type:
select C++ as the language used by the project:
3.2 Configure header files and library files
Open the tab page and configure the sum Build Settings
of Vulkan, glfw and glm头文件
lib文件
search path
- Will
/usr/local/include
add Header Search Paths, which is the path of Homebrew installation header files, the header files of glm and glfw3 we installed are all under this folder. vulkansdk/macOS/include
Header Search Paths will be added, which is the header file path of the Vulkan SDK installation directory.
- Will
/usr/local/lib
add Library Search Paths, which is the path of Homebrew installation library files, the library files of glm and glfw3 we installed are all under this folder. - Will
vulkansdk/macOS/lib
add Library Search Paths, which is the Vulkan SDK installation directory library file path.
3.3 Add dynamic library
Click Build Phases
the tab to add glfw3 and vulkan framework dynamic libraries.
- For glfw, open
/usr/local/lib
the directory, you can findlibglfw.3.x.dylib
files of similar form. Put this file拖拽
underLinked Frameworks and Libraries
the label; - For Vulkan, open
vulkansdk/macOS/lib
the directory,拖拽
libvulkan.1.dylib
andlibvulkan.1.x.xx.dylib
go to the Linked Frameworks and Libraries tab;
After doing the above, change Copy Files
the label:
- Set Destination to Frameworks;
- Clear the Subpath text box;
- Do not check Copy only when installing;
- Then click the + sign to add all three dynamic libraries.
3.4 Configure environment variables
On the Xcode toolbar via Product>Scheme>Edit Scheme...
the Open Arguments
tab, add the following environment variable:
VK_LAYER_PATH = /Users/xiaxl/VulkanSDK/1.3.250.0/macOS/share/vulkan/explicit_layer.d
VK_ICD_FILENAMES = /Users/xiaxl/VulkanSDK/1.3.250.0/macOS/share/vulkan/icd.d/MoltenVK_icd.json
4. Test
So far, all configurations have been completed. You can test whether the environment configuration is normal by running 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() {
// 初始化GLFW库
glfwInit();
// GLFW最早是为OpenGL设计
// 所以此处需要显式的设置GLFW阻止自动创建OpenGL上下文
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 supportedn";
// 测试glm
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
// 未主动close时持续消费点击event
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
// 销毁window
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
The running effect is as follows:
reference
Vulkan SDK download:
https://vulkan.lunarg.com/sdk/home
Official vulkan:
https://registry.khronos.org/vulkan/
Vulkan official document:
https://registry.khronos.org/vulkan/specs/1.3-extensions/pdf/vkspec.pdf
= THE END =
The article was first published on the official account "CODING Technology Pavilion". If the article is helpful to you, please pay attention to my official account.