[譯]Vulkan教程(03)開發環境html
這是我翻譯(https://vulkan-tutorial.com)上的Vulkan教程的第3篇。linux
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.ios
本章咱們將配置Vulkan開發環境,安裝一些有用的庫。咱們使用的全部工具(編譯器除外)都是在Windows、Linux和MacOS上兼容的,可是安裝它們的步驟有點不一樣,因此咱們分別描述之。c++
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.git
若是你在Windows上開發,那麼我假設你在使用Visual studio 2017編譯代碼。你也能夠用Visual Studio 2013或2015,只是步驟可能稍微有點不一樣。github
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.web
開發Vulkan應用程序所需的最重要的組件是SDK。它包含頭文件,標準驗證層,調試工具和Vulkan函數的加載器。加載器在在運行時查詢driver中的函數,這與OpenGL的GLEW相似——若是你熟悉它的話。json
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 網站下載,按鈕就在頁面下邊。你不用建立帳戶,但有了帳戶你能夠獲得一些對你有用的額外文檔。app
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.exe
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. 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頭文件。你能夠自由探索這些文件,可是本教程中就不談它們了。
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 也是與之兼容的。
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是個只有頭文件的庫,因此下載最新版保存到合適的位置上。你應該會看到相似這樣的目錄結構:
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了!
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。
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。
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
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
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.cpp
and 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 CFLAGS
variable 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/lib
directory. 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.
你已經準備好開啓真正的探險了。
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。
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文件,你將看到以下畫面:
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
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
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:
/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./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 選項卡,而後:
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 vulkan
frameworks. 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).
/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.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框架。爲了讓事情簡單點,咱們將動態庫添加到項目裏(若是你想用靜態框架,你能夠查看這些庫的文檔)。
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
> Scheme
> Edit Scheme...
, and in the Arguments
tab add the two following environment variables:
vulkansdk/macOS/etc/vulkan/icd.d/MoltenVK_icd.json
vulkansdk/macOS/etc/vulkan/explicit_layer.d
最後一件事,你要設置幾個環境變量。在Xcode工具欄,選擇Product > Scheme> Edit Scheme...,,在Arguments 選項卡添加2個環境變量,以下所示:
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.
你已準備就緒,能夠開始真傢伙了。