1. 程式人生 > >[譯]Vulkan教程(03)開發環境

[譯]Vulkan教程(03)開發環境

[譯]Vulkan教程(03)開發環境

這是我翻譯(https://vulkan-tutorial.com)上的Vulkan教程的第3篇。

 

 

In this chapter we'll set up your environment for developing Vulkan applications and install some useful libraries. All of the tools we'll use, with the exception of the compiler, are compatible with Windows, Linux and MacOS, but the steps for installing them differ a bit, which is why they're described separately here.

本章我們將配置Vulkan開發環境,安裝一些有用的庫。我們使用的所有工具(編譯器除外)都是在Windows、Linux和MacOS上相容的,但是安裝它們的步驟有點不同,所以我們分別描述之。

Windows

If you're developing for Windows, then I will assume that you are using Visual Studio 2017 to compile your code. You may also use Visual Studio 2013 or 2015, but the steps may be a bit different.

如果你在Windows上開發,那麼我假設你在使用Visual studio 2017編譯程式碼。你也可以用Visual Studio 2013或2015,只是步驟可能稍微有點不同。

Vulkan SDK

The most important component you'll need for developing Vulkan applications is the SDK. It includes the headers, standard validation layers, debugging tools and a loader for the Vulkan functions. The loader looks up the functions in the driver at runtime, similarly to GLEW for OpenGL - if you're familiar with that.

開發Vulkan應用程式所需的最重要的元件是SDK。它包含標頭檔案,標準驗證層,除錯工具和Vulkan函式的載入器。載入器在在執行時查詢driver中的函式,這與OpenGL的GLEW類似——如果你熟悉它的話。

The SDK can be downloaded from the LunarG website using the buttons at the bottom of the page. You don't have to create an account, but it will give you access to some additional documentation that may be useful to you.

SDK可以從LunarG 網站下載,按鈕就在頁面下邊。你不用建立賬戶,但有了賬戶你可以得到一些對你有用的額外文件。

 

Proceed through the installation and pay attention to the install location of the SDK. The first thing we'll do is verify that your graphics card and driver properly support Vulkan. Go to the directory where you installed the SDK, open the Bin directory and run the cube.exe demo. You should see the following:

安裝SDK,注意一下安裝的位置。首先我們要驗證你的圖形卡和driver是否支援Vulkan。進入你安裝SDK的資料夾,開啟Bin 資料夾,執行cube.exe 示例。你應該會看到如下內容:

 

 

If you receive an error message then ensure that your drivers are up-to-date, include the Vulkan runtime and that your graphics card is supported. See the introduction chapter for links to drivers from the major vendors.

如果你看到錯誤資訊,那麼確保你的driver已經更新到最新版本,有Vulkan執行時,且你的圖形卡被Vulkan支援。在introduction chapter入門章節有連結,可以檢視主要廠商的driver。

There is another program in this directory that will be useful for development. The glslangValidator.exeprogram will be used to compile shaders from the human-readable GLSL to bytecode. We'll cover this in depth in the shader modules chapter. The Bin directory also contains the binaries of the Vulkan loader and the validation layers, while the Lib directory contains the libraries.

這個目錄裡還有一個有用的程式。程式glslangValidator.exe會被用於編譯shader,將人類可讀的GLSL 轉換為位元組碼。我們將在shader modules 章節細說。資料夾Bin 還有Vulkan載入器和驗證層的二進位制檔案,而資料夾Lib 包含一些庫。

The Doc directory contains useful information about the Vulkan SDK and an offline version of the entire Vulkan specification. Lastly, there's the Include directory that contains the Vulkan headers. Feel free to explore the other files, but we won't need them for this tutorial.

資料夾Doc 包含Vulkan SDK的有用資訊,還有一個離線版本的Vulkan說明書。資料夾Include 包含Vulkan標頭檔案。你可以自由探索這些檔案,但是本教程中就不談它們了。

GLFW

As mentioned before, 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,它沒有建立視窗的工具。為了享受Vulkan的好處,也為了避免Win32的恐怖,我們將使用GLFW library來建立視窗,它支援Windows、Linux和MacOS。還有其他的庫也能做到這一點,例如SDL,但是GLFW的優勢是,它也抽象了Vulkan中一些除建立視窗外的其它跨平臺的東西。

You can find the latest release of GLFW on the official website. In this tutorial we'll be using the 64-bit binaries, but you can of course also choose to build in 32 bit mode. In that case make sure to link with the Vulkan SDK binaries in the Lib32 directory instead of Lib. After downloading it, extract the archive to a convenient location. I've chosen to create a Libraries directory in the Visual Studio directory under documents. Don't worry about there not being a libvc-2017 folder, the libvc-2015 one is compatible.

你可以在官網official website上找到最新的GLFW。本教程中我們將使用64位二進位制檔案,但是你當然可以選擇構建一個32位的。如果那樣,確保連結到Vulkan SDK的資料夾Lib32 ,而不是Lib。下載之後,將其解壓縮到一個合適的位置。我選擇了在Visual Studio資料夾下建立一個Libraries 資料夾。如果沒有資料夾libvc-2017 ,別擔心,資料夾libvc-2015 也是與之相容的。

 

 

GLM

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, so just download the latest version and store it in a convenient location. You should have a directory structure similar to the following now:

GLM是個只有標頭檔案的庫,所以下載最新版儲存到合適的位置上。你應該會看到類似這樣的目錄結構:

 

 

Setting up Visual Studio 配置VS

Now that you've installed all of the dependencies we can set up a basic Visual Studio project for Vulkan and write a little bit of code to make sure that everything works.

既然你已經安裝了所有的依賴庫,我們可以為Vulkan配置Visual Studio專案了。我們還將寫一點點程式碼來確保一切能正常工作。

Start Visual Studio and create a new Windows Desktop Wizard project by entering a name and pressing OK.

啟動Visual Studio,建立新Windows Desktop Wizard 專案,輸入專案名,點選OK。

 

 

Make sure that Console Application (.exe) is selected as application type so that we have a place to print debug messages to, and check Empty Project to prevent Visual Studio from adding boilerplate code.

確保選中Console Application (.exe)為應用程式型別,這樣我們就有一個地方可以列印除錯資訊,勾選Empty Project 以避免Visual Studio新增樣板程式碼。

 

 

 

Press OK to create the project and add a C++ source file. You should already know how to do that, but the steps are included here for completeness.

點選OK ,建立專案和一個C++原始碼檔案。你應該已經知道如何做了,但這裡為了完整起見,都陳列出來了。

 

 

 

Now add the following code to the file. Don't worry about trying to understand it right now; we're just making sure that you can compile and run Vulkan applications. We'll start from scratch in the next chapter.

現在在檔案中新增下述程式碼。別擔心,現在不理解程式碼也沒關係,我們只是為了確保你能編譯和執行Vulkan應用程式。下一章我們將從零開始(寫程式碼)。

 1 #define GLFW_INCLUDE_VULKAN
 2 #include <GLFW/glfw3.h>
 3 
 4 #define GLM_FORCE_RADIANS
 5 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
 6 #include <glm/vec4.hpp>
 7 #include <glm/mat4x4.hpp>
 8 
 9 #include <iostream>
10 
11 int main() {
12     glfwInit();
13 
14     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
15     GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
16 
17     uint32_t extensionCount = 0;
18     vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
19 
20     std::cout << extensionCount << " extensions supported" << std::endl;
21 
22     glm::mat4 matrix;
23     glm::vec4 vec;
24     auto test = matrix * vec;
25 
26     while(!glfwWindowShouldClose(window)) {
27         glfwPollEvents();
28     }
29 
30     glfwDestroyWindow(window);
31 
32     glfwTerminate();
33 
34     return 0;
35 }

Let's now configure the project to get rid of the errors. Open the project properties dialog and ensure that All Configurations is selected, because most of the settings apply to both Debug and Release mode.

現在我們來配置專案,以搞定這些錯誤。開啟專案屬性對話方塊,確保選中All Configurations ,因為大部分配置都同時應用到Debug 和Release 模式。

 

 

 

Go to C++ -> General -> Additional Include Directories and press <Edit...> in the dropdown box.

選擇C++ -> General -> Additional Include Directories ,點選下拉框的<Edit...>。

 

 

Add the header directories for Vulkan, GLFW and GLM:

新增標頭檔案夾(Vulkan、GLFW和GLM):

 

 

Next, open the editor for library directories under Linker -> General:

下一步,開啟Linker -> General下的庫資料夾編輯器:

 

 

And add the locations of the object files for Vulkan and GLFW:

新增Vulkan和GLFW的物件檔案的位置:

 

 

Go to Linker -> Input and press <Edit...> in the Additional Dependencies dropdown box.

選擇Linker -> Input ,點選Additional Dependencies 下拉框的<Edit...>:

 

 

Enter the names of the Vulkan and GLFW object files:

輸入Vulkan和GLFW物件檔案的名字:

 

 

And finally change the compiler to support C++17 features:

最後修改編譯器以支援C++17特性:

 

 

You can now close the project properties dialog. If you did everything right then you should no longer see any more errors being highlighted in the code.

現在你可以關閉專案屬性對話方塊。如果你一切都做對了,那麼你應該不會再看到程式碼中有高亮的錯誤。

Finally, ensure that you are actually compiling in 64 bit mode:

最後,確保你在64位模式下編譯:

 

 

Press F5 to compile and run the project and you should see a command prompt and a window pop up like this:

點選F5 來編譯和執行專案,你應該會看到一個命令列和一個視窗彈出來:

 

 

The number of extensions should be non-zero. Congratulations, you're all set for playing with Vulkan!

擴充套件數量應該為非零數字。恭喜,你已經準備好玩Vulkan了!

Linux

These instructions will be aimed at Ubuntu users, but you may be able to follow along by compiling the LunarG SDK yourself and changing the apt commands to the package manager commands that are appropriate for you. You should already have a version of GCC installed that supports modern C++ (4.8 or later). You also need both CMake and make.

下述教程是給Ubuntu使用者的,但是你可以自己編譯LunarG SDK,從apt命令跳轉到適合你的包管理器命令,這樣也可以使用下面的教程。你應該已經有一版支援C++(4.8或更高)的GCC安裝好了。你還需要CMake和make。

Vulkan SDK

The most important component you'll need for developing Vulkan applications is the SDK. It includes the headers, standard validation layers, debugging tools and a loader for the Vulkan functions. The loader looks up the functions in the driver at runtime, similarly to GLEW for OpenGL - if you're familiar with that.

開發Vulkan應用程式所需的最重要的元件是SDK。它包含標頭檔案,標準驗證層,除錯工具和Vulkan函式的載入器。載入器在在執行時查詢driver中的函式,這與OpenGL的GLEW類似——如果你熟悉它的話。

The SDK can be downloaded from the LunarG website using the buttons at the bottom of the page. You don't have to create an account, but it will give you access to some additional documentation that may be useful to you.

SDK可以從LunarG 網站下載,按鈕就在頁面下邊。你不用建立賬戶,但有了賬戶你可以得到一些對你有用的額外文件。

 

 

Open a terminal in the directory where you've downloaded the .tar.gz archive and extract it:

開啟終端,跳到你下載.tar.gz 壓縮包的資料夾下,解壓縮它:

tar -xzf vulkansdk-linux-x86_64-xxx.tar.gz

It will extract all of the files in the SDK to a subdirectory with the SDK version as name in the working directory. Move the directory to a convenient place and take note of its path. Open a terminal in the root directory of the SDK, which will contain files like build_examples.sh.

它會將SDK的所有檔案解壓到子資料夾下,以SDK版本為資料夾名。將這個資料夾移動到方便的地方,注意它的路徑。開啟終端,在SDK的根資料夾下,會包含檔案build_examples.sh。

The samples in the SDK and one of the libraries that you will later use for your program depend on the XCB library. This is a C library that is used to interface with the X Window System. It can be installed in Ubuntu from the libxcb1-dev package. You also need the generic X development files that come with the xorg-dev package.

SDK中的示例,和你稍後會用到的一個庫,依賴於XCB庫。這是一個用於與X視窗系統互動的C庫。在Ubuntu中它可以用libxcb1-dev 包下載。你還需要隨xorg-dev 包的通用X開發檔案。

sudo apt install libxcb1-dev xorg-dev

You can now build the Vulkan examples in the SDK by running:

現在你可以編譯SDK中的Vulkan示例了:

./build_examples.sh

If compilation was successful, then you should now have a ./examples/build/vkcube executable. Run it from the examples/build directory with ./vkcube and ensure that you see the following pop up in a window:

如果編譯成功,那麼你現在應該有一個可執行的./examples/build/vkcube 檔案。從examples/build 資料夾,用./vkcube執行它,確保你看到下述彈出視窗:

 

 

If you receive an error message then ensure that your drivers are up-to-date, include the Vulkan runtime and that your graphics card is supported. See the introduction chapter for links to drivers from the major vendors.

如果你看到錯誤資訊,那麼確保你的driver已經更新到最新版本,有Vulkan執行時,且你的圖形卡被Vulkan支援。在introduction chapter入門章節有連結,可以檢視主要廠商的driver。

GLFW

As mentioned before, Vulkan by itself is a platform agnostic API and does not include tools for creation a window to display the rendered results. To benefit from the cross-platform advantages of Vulkan and to avoid the horrors of X11, 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,它沒有建立視窗的工具。為了享受Vulkan的好處,也為了避免X11的恐怖,我們將使用GLFW library來建立視窗,它支援Windows、Linux和MacOS。還有其他的庫也能做到這一點,例如SDL,但是GLFW的優勢是,它也抽象了Vulkan中一些除建立視窗外的其它跨平臺的東西。

We'll be installing GLFW from source instead of using a package, because the Vulkan support requires a recent version. You can find the sources on the official website. Extract the source code to a convenient directory and open a terminal in the directory with files like CMakeLists.txt.

我們不用包來安裝GLWF,而是從原始碼安裝它,因為Vulkan需要最近的版本。你可以在官網official website上找到原始碼。解壓縮原始碼到方便的資料夾,開啟終端,找到有CMakeLists.txt的資料夾。

Run the following commands to generate a makefile and compile GLFW:

執行下述命令,生成makefile,編譯GLFW:

1 cmake .
2 make

You may see a warning stating Could NOT find Vulkan, but you can safely ignore this message. If compilation was successful, then you can install GLFW into the system libraries by running:

你可能看到一個警告說“Could NOT find Vulkan”(無法找到Vulkan),但是你可以安全地忽略這個訊息。如果編譯成功,那麼你可以安裝GLFW到系統庫:

sudo make install

GLM

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中。

It is a header-only library that can be installed from the libglm-dev package:

它是隻有標頭檔案的庫,可以從libglm-dev 包安裝:

sudo apt install libglm-dev

Setting up a makefile project 配置makefile專案

Now that you have installed all of the dependencies, we can set up a basic makefile project for Vulkan and write a little bit of code to make sure that everything works.

既然你已經安裝了所有的依賴,我們可以設定一個基礎的Vulkan的makefile專案,寫一小點程式碼來確保一切能順利工作。

Create a new directory at a convenient location with a name like VulkanTest. Create a source file called main.cppand insert the following code. Don't worry about trying to understand it right now; we're just making sure that you can compile and run Vulkan applications. We'll start from scratch in the next chapter.

在合適的位置建立一個新資料夾,起名VulkanTest之類的都可以。建立一個原始碼檔案,命名為main.cpp,插入下述程式碼。別擔心,現在不用理解這些程式碼,我們只是要確保你能編譯和執行Vulkan應用程式。下一章我們將從零開始(寫程式碼)。

 1 #define GLFW_INCLUDE_VULKAN
 2 #include <GLFW/glfw3.h>
 3  
 4 #define GLM_FORCE_RADIANS
 5 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
 6 #include <glm/vec4.hpp>
 7 #include <glm/mat4x4.hpp>
 8  
 9 #include <iostream>
10  
11 int main() {
12     glfwInit();
13  
14     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
15     GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
16  
17     uint32_t extensionCount = 0;
18     vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
19  
20     std::cout << extensionCount << " extensions supported" << std::endl;
21  
22     glm::mat4 matrix;
23     glm::vec4 vec;
24     auto test = matrix * vec;
25  
26     while(!glfwWindowShouldClose(window)) {
27         glfwPollEvents();
28     }
29  
30     glfwDestroyWindow(window);
31  
32     glfwTerminate();
33  
34     return 0;
35 }

Next, we'll write a makefile to compile and run this basic Vulkan code. Create a new empty file called Makefile. I will assume that you already have some basic experience with makefiles, like how variables and rules work. If not, you can get up to speed very quickly with this tutorial.

下一步,我們將寫一個makefile來編譯和執行這個基礎的Vulkan程式碼。建立一個新的空檔案,命名為Makefile。我假設你已經對makefile有一些瞭解,例如變數和規則是如何工作的。如果沒有,這個教程可以讓你很快上手。

We'll first define a couple of variables to simplify the remainder of the file. Define a VULKAN_SDK_PATH variable that refers to the location of the x86_64 directory in the LunarG SDK, for example:

我們先來定義幾個變數來簡化檔案。例如,定義VULKAN_SDK_PATH 變數,指向LunarG SDK的x86_64 資料夾的位置:

VULKAN_SDK_PATH = /home/user/VulkanSDK/x.x.x.x/x86_64

Make sure to replace user with your own username and x.x.x.x with the right version. Next, define a CFLAGSvariable that will specify the basic compiler flags:

確保替換user 為你自己的使用者名稱,替換x.x.x.x為正確的版本號。下一步,定義CFLAGS變數,標識基礎編譯器標誌:

CFLAGS = -std=c++17 -I$(VULKAN_SDK_PATH)/include

We're going to use modern C++ (-std=c++17), and we need to be able to locate vulkan.h in the LunarG SDK.

我們計劃使用現代C++(-std=c++17),我們需要能夠定位LunarG SDK中的vulkan.h檔案。

Similarly, define the linker flags in a LDFLAGS variable:

相似的,定義LDFLAGS 變數,表示連結標誌:

LDFLAGS = -L$(VULKAN_SDK_PATH)/lib `pkg-config --static --libs glfw3` -lvulkan

The first flag specifies that we want to be able to find libraries like libvulkan.so in the LunarG SDK's x86_64/libdirectory. The second component invokes pkg-config to automatically retrieve all of the linker flags necessary to build an application with GLFW. Finally, -lvulkan links with the Vulkan function loader that comes with the LunarG SDK.

第一個標誌說明,我們想要在LunarG SDK的x86_64/lib資料夾下找到libvulkan.so 這樣的庫。第二個元件呼叫pkg-config ,來自動地檢索所有需要用於和GLFW一起編譯成應用程式的連結標誌。

Specifying the rule to compile VulkanTest is straightforward now. Make sure to use tabs for indentation instead of spaces.

編寫編譯VulkanTest 的規則就很直截了當了。確保使用tab鍵來縮排,不要用空格。

1 VulkanTest: main.cpp
2     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.

儲存這個makefile檔案,在此資料夾下,配合main.cpp 和Makefile,執行make命令,以驗證這個規則是否正常工作。這應該會產生一個VulkanTest 可執行檔案。

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:

現在我們再定義2個規則,test 和clean。前者會執行可執行檔案,後者會清除已編譯的可執行檔案。

1 .PHONY: test clean
2  
3 test: VulkanTest
4     ./VulkanTest
5  
6 clean:
7     rm -f VulkanTest

You will find that make clean works perfectly fine, but make test will most likely fail with the following error message:

你將發現make clean 工作得很好,但是make test 很可能失敗,並提供下述錯誤訊息:

./VulkanTest: error while loading shared libraries: libvulkan.so.1: cannot open shared object file: No such file or directory

That's because libvulkan.so is not installed as system library. To alleviate this problem, explicitly specify the library loading path using the LD_LIBRARY_PATH environment variable:

這是因為libvulkan.so沒有被安裝為系統庫。為緩解這個問題,用環境變數LD_LIBRARY_PATH顯式地宣告這個庫的載入路徑:

1 test: VulkanTest
2     LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib ./VulkanTest

The program should now run successfully, and display the number of Vulkan extensions. The application should exit with the success return code (0) when you close the empty window. However, there is one more variable that you need to set. We will start using validation layers in Vulkan and you need to tell the Vulkan library where to load these from using the VK_LAYER_PATH variable:

現在程式應該能成功運行了,顯示出Vulkan擴充套件的數量。當你關閉空視窗時,這個應用程式應該退出並返回程式碼(0)。但是,你還需要設定一個變數。我們將開始在Vulkan中使用驗證層,你需要用VK_LAYER_PATH變數告訴Vulkan庫去哪兒載入這些:

1 test: VulkanTest
2     LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/vulkan/explicit_layer.d ./VulkanTest

You should now have a complete makefile that resembles the following:

現在你應該有一個完整的makefile了,其內容如下:

 1 VULKAN_SDK_PATH = /home/user/VulkanSDK/x.x.x.x/x86_64
 2  
 3 CFLAGS = -std=c++17 -I$(VULKAN_SDK_PATH)/include
 4 LDFLAGS = -L$(VULKAN_SDK_PATH)/lib `pkg-config --static --libs glfw3` -lvulkan
 5  
 6 VulkanTest: main.cpp
 7     g++ $(CFLAGS) -o VulkanTest main.cpp $(LDFLAGS)
 8  
 9 .PHONY: test clean
10  
11 test: VulkanTest
12     LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/vulkan/explicit_layer.d ./VulkanTest
13  
14 clean:
15     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.

現在你可以將此資料夾作為你的Vulkan專案的模板了。複製一份,重新命名為HelloTriangle 之類,刪掉main.cpp中所有程式碼。

Before we move on, let's explore the Vulkan SDK a bit more. There is another program in it that will be very useful for development. The x86_64/bin/glslangValidator program will be used to compile shaders from the human-readable GLSL to bytecode. We'll cover this in depth in the shader modules chapter.

繼續之前,我們來探索一下Vulkan SDK。有一個程式會很有用。程式x86_64/bin/glslangValidator 會被用於將shader從人類可讀的GLSL 編譯為位元組碼。我們將在shader modules 章節細說。

The Doc directory contains useful information about the Vulkan SDK and an offline version of the entire Vulkan specification. Feel free to explore the other files, but we won't need them for this tutorial.

資料夾Doc 包含Vulkan SDK的有用資訊,還有一個離線版本的Vulkan說明書。你可以自由探索這些檔案,但是本教程中就不談它們了。

You are now all set for the real adventure.

你已經準備好開啟真正的探險了。

MacOS

These instructions will assume you are using Xcode and the Homebrew package manager. Also, keep in mind that you will need at least MacOS version 10.11, and your device needs to support the Metal API.

本段教程將假設你在使用Xcode和Homebrew package manager。另外,記住你將需要最新的MacOS版本10.11,且你的裝置需要支援Metal API。

Vulkan SDK

The most important component you'll need for developing Vulkan applications is the SDK. It includes the headers, standard validation layers, debugging tools and a loader for the Vulkan functions. The loader looks up the functions in the driver at runtime, similarly to GLEW for OpenGL - if you're familiar with that.

開發Vulkan應用程式所需的最重要的元件是SDK。它包含標頭檔案,標準驗證層,除錯工具和Vulkan函式的載入器。載入器在在執行時查詢driver中的函式,這與OpenGL的GLEW類似——如果你熟悉它的話。

The SDK can be downloaded from the LunarG website using the buttons at the bottom of the page. You don't have to create an account, but it will give you access to some additional documentation that may be useful to you.

SDK可以從LunarG 網站下載,按鈕就在頁面下邊。你不用建立賬戶,但有了賬戶你可以得到一些對你有用的額外文件。

 

 

The SDK version for MacOS internally uses MoltenVK. There is no native support for Vulkan on MacOS, so what MoltenVK does is actually act as a layer that translates Vulkan API calls to Apple's Metal graphics framework. With this you can take advantage of debugging and performance benefits of Apple's Metal framework.

實際上MacOS的SDK版本使用MoltenVK。MacOS沒有對Vulkan的本地支援,所以MoltenVK實際上是在將Vulkan API呼叫翻譯到Apple的Metal圖形框架。藉此,你可以利用Apple的Metal框架的除錯和效能優勢。

After downloading it, simply extract the contents to a folder of your choice (keep in mind you will need to reference it when creating your projects on Xcode). Inside the extracted folder, in the Applications folder you should have some executable files that will run a few demos using the SDK. Run the cube executable and you will see the following:

下載後,解壓縮到一個合適的資料夾(記住你會在用Xcode建立專案的時候引用它)。在解壓的資料夾內,在Applications 資料夾內,你應該有一些可執行檔案,它們是使用SDK的示例。執行cube檔案,你將看到如下畫面:

 

 

GLFW

As mentioned before, Vulkan by itself is a platform agnostic API and does not include tools for creation a window to display the rendered results. 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,它沒有建立視窗的工具。我們將使用GLFW library來建立視窗,它支援Windows、Linux和MacOS。還有其他的庫也能做到這一點,例如SDL,但是GLFW的優勢是,它也抽象了Vulkan中一些除建立視窗外的其它跨平臺的東西。

To install GLFW on MacOS we will use the Homebrew package manager. Vulkan support for MacOS is still not fully available on the current (at the time of this writing) stable version 3.2.1. Therefore we will install the latest version of the glfw3 package using:

我們將用Homebrew包管理器來在MacOS上安裝GLFW。Vulkan對MacOS的支援還不完全,寫作本教程時的版本是3.2.1。因此我們將安裝最新的glfw3 包:

brew install glfw3 --HEAD

GLM

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.

Vulkan沒有用於線性代數操作的庫,所以我們不得不下載一個。GLM 是一個為圖形API設計的庫,廣泛用於OpenGL中。

It is a header-only library that can be installed from the glm package:

它是隻有標頭檔案的庫,可以從glm 包安裝:

brew install glm

Setting up Xcode 配置Xcode

Now that all the dependencies are installed we can set up a basic Xcode project for Vulkan. Most of the instructions here are essentially a lot of "plumbing" so we can get all the dependencies linked to the project. Also, keep in mind that during the following instructions whenever we mention the folder vulkansdk we are refering to the folder where you extracted the Vulkan SDK.

既然你已經安裝了所有的依賴,我們可以設定一個基礎的Vulkan的Xcode專案。這裡大多數指導本質上都是很多“管道”,所以我們可以讓所有依賴連結到這個專案。另外,記住下述指導中,無論何時我們提到vulkansdk 資料夾,我們知道是你解壓縮Vulkan SDK的資料夾。

Start Xcode and create a new Xcode project. On the window that will open select Application > Command Line Tool.

啟動Xcode,建立一個新的Xcode專案。在視窗上選擇Application > Command Line Tool。

 

 

Select Next, write a name for the project and for Language select C++.

選擇Next,輸入一個專案名,Language 選擇C++。

 

 

Press Next and the project should have been created. Now, let's change the code in the generated main.cpp file to the following code:

點選Next ,專案就應該創建出來了。現在,讓我們在生成的main.cpp 檔案中修改程式碼為如下所示:

 1 #define GLFW_INCLUDE_VULKAN
 2 #include <GLFW/glfw3.h>
 3  
 4 #define GLM_FORCE_RADIANS
 5 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
 6 #include <glm/vec4.hpp>
 7 #include <glm/mat4x4.hpp>
 8  
 9 #include <iostream>
10  
11 int main() {
12     glfwInit();
13  
14     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
15     GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
16  
17     uint32_t extensionCount = 0;
18     vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
19  
20     std::cout << extensionCount << " extensions supported" << std::endl;
21  
22     glm::mat4 matrix;
23     glm::vec4 vec;
24     auto test = matrix * vec;
25  
26     while(!glfwWindowShouldClose(window)) {
27         glfwPollEvents();
28     }
29  
30     glfwDestroyWindow(window);
31  
32     glfwTerminate();
33  
34     return 0;
35 }

Keep in mind you are not required to understand all this code is doing yet, we are just setting up some API calls to make sure everything is working.

記住,你現在還不需要理解這些程式碼,我們只是要設定一些API呼叫來確保一切工作正常。

Xcode should already be showing some errors such as libraries it cannot find. We will now start configuring the project to get rid of those errors. On the Project Navigator panel select your project. Open the Build Settings tab and then:

  • Find the Header Search Paths field and add a link to /usr/local/include (this is where Homebrew installs headers, so the glm and glfw3 header files should be there) and a link to vulkansdk/macOS/include for the Vulkan headers.
  • Find the Library Search Paths field and add a link to /usr/local/lib (again, this is where Homebrew installs libraries, so the glm and glfw3 lib files should be there) and a link to vulkansdk/macOS/lib.

Xcode應該已經顯示一些錯誤,例如庫沒有找到。我們現在將開始配置專案來搞定這些錯誤。在Project Navigator 面板選擇你的專案,開啟Build Settings 選項卡,然後:

  • 找到欄位Header Search Paths ,新增連結到/usr/local/include(這是Homebrew安裝標頭檔案的位置,所以glm和glfw3的標頭檔案應該在這裡)和vulkansdk/macOS/include(這是關於Vulkan標頭檔案的)。
  • 找到欄位Library Search Paths ,新增連結到/usr/local/lib(這是Homebrew安裝庫的位置,所以glm和glfw3的lib檔案應該在這裡)和vulkansdk/macOS/lib。

It should look like so (obviously, paths will be different depending on where you placed on your files):

這應該看起來是這樣的(顯然,基於你放置檔案的位置,路徑會有所不同):

 

 

Now, in the Build Phases tab, on Link Binary With Libraries we will add both the glfw3 and the vulkanframeworks. To make things easier we will be adding he dynamic libraries in the project (you can check the documentation of these libraries if you want to use the static frameworks).

  • For glfw open the folder /usr/local/lib and there you will find a file name like libglfw.3.x.dylib ("x" is the library's version number, it might be different depending on when you downloaded the package from Homebrew). Simply drag that file to the Linked Frameworks and Libraries tab on Xcode.
  • For vulkan, go to vulkansdk/macOS/lib. Do the same for the file both files libvulkan.1.dylib and libvulkan.1.x.xx.dylib (where "x" will be the version number of the the SDK you downloaded).

現在,在Build Phases 選項卡,在Link Binary With Libraries 上,我們新增glfw3 和vulkan框架。為了讓事情簡單點,我們將動態庫新增到專案裡(如果你想用靜態框架,你可以檢視這些庫的文件)。

  • 對於glfw,開啟資料夾/usr/local/lib,你會發現一個檔案libglfw.3.x.dylib("x"是庫的版本號,基於你從Homebrew下載的包,這個版本號可能不同)。拖拽這個檔案到Xcode的Linked Frameworks和Libraries選項卡。
  • 對於Vulkan,開啟資料夾vulkansdk/macOS/lib。對檔案libvulkan.1.dylib 和libvulkan.1.x.xx.dylib (其中"x"是你下載的SDK的版本號)做上述相同的拖拽操作。

After adding those libraries, in the same tab on Copy Files change Destination to "Frameworks", clear the subpath and deselect "Copy only when installing". Click on the "+" sign and add all those three frameworks here as well.

添加了這些庫後,在這個tab選項卡里的Copy Files 上,將Destination 修改為"Frameworks",清空subpath,取消選擇"Copy only when installing"。點選"+"符號,新增所有這3個框架。

Your Xcode configuration should look like:

你的Xcode配置應該如下圖所示:

 

 

The last thing you need to setup are a couple of environment variables. On Xcode toolbar go to Product > SchemeEdit Scheme..., and in the Arguments tab add the two following environment variables:

  • VK_ICD_FILENAMES = vulkansdk/macOS/etc/vulkan/icd.d/MoltenVK_icd.json
  • VK_LAYER_PATH = vulkansdk/macOS/etc/vulkan/explicit_layer.d

最後一件事,你要設定幾個環境變數。在Xcode工具欄,選擇Product > Scheme> Edit Scheme...,,在Arguments 選項卡新增2個環境變數,如下所示:

  • VK_ICD_FILENAMES = vulkansdk/macOS/etc/vulkan/icd.d/MoltenVK_icd.json
  • VK_LAYER_PATH = vulkansdk/macOS/etc/vulkan/explicit_layer.d

It should look like so:

這應該如下圖所示:

 

 

Finally, you should be all set! Now if you run the project (remembering to setting the build configuration to Debug or Release depending on the configuration you chose) you should see the following:

最後,大功告成!現在如果你執行專案(記得根據你選擇配置來選擇Debug或Release),你應該看到下圖所示的畫面:

 

 

The number of extensions should be non-zero. The other logs are from the libraries, you might get different messages from those depending on your configuration.

擴充套件的數目應該是非零的。其他的日誌來自庫,根據你的配置不同,你可能得到不同的訊息。

You are now all set for the real thing.

你已準備就緒,可以開始真傢伙了。

  • Previous上一章

 

  • Next下一章

&n