A detailed explanation of how to build a Vulkan development environment for Mac

This article is for Vukan系列的第二篇文章, 上一篇articles on Vulkan进行了简单介绍, 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

Vulkan SDK Download

After installing the Vulkan SDK, you can run it VulkanSDK/1.3.250.0/Applicationsin the directory vkcube. If the following running interface is displayed, it means that your computer supports Vulkan.
Vulkan SDK installation complete verification

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 Toolthe project type:
Create a new Xcode project, select Application>Command Line Tool project
select C++ as the language used by the project:
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 Settingsof Vulkan, glfw and glm头文件lib文件search path

  • Will /usr/local/includeadd 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/includeHeader Search Paths will be added, which is the header file path of the Vulkan SDK installation directory.

search path for header files

  • Will /usr/local/libadd 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/libadd Library Search Paths, which is the Vulkan SDK installation directory library file path.The search path of the lib file

3.3 Add dynamic library

Click Build Phasesthe tab to add glfw3 and vulkan framework dynamic libraries.

  • For glfw, open /usr/local/libthe directory, you can find libglfw.3.x.dylibfiles of similar form. Put this file 拖拽under Linked Frameworks and Librariesthe label;
  • For Vulkan, open vulkansdk/macOS/libthe directory, 拖拽 libvulkan.1.dyliband libvulkan.1.x.xx.dylibgo to the Linked Frameworks and Libraries tab;

After doing the above, change Copy Filesthe 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.

Add glfw3 and vulkan framework dynamic library

3.4 Configure environment variables

On the Xcode toolbar via Product>Scheme>Edit Scheme...the Open Argumentstab, 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

Configure environment variables

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:
running result

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.

Guess you like

Origin blog.csdn.net/aiwusheng/article/details/131479577