[譯]Vulkan教程(01)入門
[譯]Vulkan教程(01)入門
接下來我將翻譯(https://vulkan-tutorial.com)上的Vulkan教程。這可能是我學習Vulkan的最好方式,但不是最理想的方式。
我會用“driver(驅動程式)”這樣的方式翻譯某些關鍵詞語,在後續的文字中,則只使用英文。這可以減少歧義,且使譯文易讀。
Introduction 入門
- About
- E-book
- Tutorial structure
About 關於
This tutorial will teach you the basics of using the Vulkan graphics and compute API. Vulkan is a new API by the Khronos group (known for OpenGL) that provides a much better abstraction of modern graphics cards. This new interface allows you to better describe what your application intends to do, which can lead to better performance and less surprising driver behavior compared to existing APIs like OpenGL and Direct3D. The ideas behind Vulkan are similar to those of Direct3D 12 and Metal, but Vulkan has the advantage of being fully cross-platform and allows you to develop for Windows, Linux and Android at the same time.
本教程講授Vulkan圖形和計算API的基本使用。Vulkan是Khronos委員會(他們也是維護OpenGL的那幫人)提出的新的API,它提供了對現代圖形卡的更好的抽象描述。這個新的介面,允許你更好地描述你的app(應用程式)想做的事。與已有的API(比如OpenGL和Direct3D)相比,這可以帶來更好的效能,可以減少driver(驅動程式)的意外行為。Vulkan背後的思想與Direct3D 12和Metal相似,但是Vulkan有跨平臺的優勢,允許你同時為Windows、Linux和Android開發。
However, the price you pay for these benefits is that you have to work with a significantly more verbose API. Every detail related to the graphics API needs to be set up from scratch by your application, including initial frame buffer creation and memory management for objects like buffers and texture images. The graphics driver will do a lot less hand holding, which means that you will have to do more work in your application to ensure correct behavior.
但是,為了得到這些好處,你需要付出的代價是,你不得不用冗長得多的API來工作。與圖形API相關的每個細節,都需要在你的app中從零開始設定好。這包括:初始化幀快取,對物件(例如buffer(快取)和texture image(紋理影象))的記憶體管理。圖形driver的握手行為會少很多,這意味著你不得不在你的app裡做更多工作,以確保(driver的)正確行為。
The takeaway message here is that Vulkan is not for everyone. It is targeted at programmers who are enthusiastic about high performance computer graphics, and are willing to put some work in. If you are more interested in game development, rather than computer graphics, then you may wish to stick to OpenGL or Direct3D, which will not be deprecated in favor of Vulkan anytime soon. Another alternative is to use an engine like Unreal Engine or Unity, which will be able to use Vulkan while exposing a much higher level API to you.
在此得到的結論是,Vulkan不是給所有人的。它的目標人群是狂熱的且願意為之潛心工作的高效能運算機圖形程式設計師。如果你對遊戲開發(而不是計算機圖形)更感興趣,那麼你可能希望用OpenGL或Direct3D,它們不會為了支援Vulkan而很快被廢棄。另一個選擇是使用引擎(例如Unreal或Unity),它們將能使用Vulkan,且對你暴露高階得多的API。
With that out of the way, let's cover some prerequisites for following this tutorial:
- A graphics card and driver compatible with Vulkan (NVIDIA, AMD, Intel)
- Experience with C++ (familiarity with RAII, initializer lists)
- A compiler compatible with C++11 (Visual Studio 2013+, GCC 4.8+)
- Some existing experience with 3D computer graphics
把上述事情放在一邊,我們來介紹一些後續教程需要的先決條件:
- 一個圖形卡和相容Vulkan的driver(NVIDIA、AMD、Intel)
- 使用C++的經驗(熟悉RAII,初始化list)
- 相容C++11的編譯器(Visual Studio 2013以上,GCC 4.8以上)
- 3D計算機圖形的一些經驗
This tutorial will not assume knowledge of OpenGL or Direct3D concepts, but it does require you to know the basics of 3D computer graphics. It will not explain the math behind perspective projection, for example. See this online book for a great introduction of computer graphics concepts. Some other great computer graphics resources are:
- Ray tracing in one weekend
- Physically Based Rendering book
- Vulkan being used in a real engine in the open-source Quake and DOOM 3
本教程不要求讀者知道OpenGL或Direct3D裡的概念,但要求讀者知道3D計算機圖形基礎知識。例如,本教程不會解釋透視投影背後的數學。你可以參考這個不錯的線上書來學計算機圖形概念入門知識。其他一些不錯的計算機圖形學習資源有:
- 光線追蹤一週末搞定
- 基於物理的渲染
- 用在真實引擎裡的開源專案Quake和DOOM 3裡的Vulkan
You can use C instead of C++ if you want, but you will have to use a different linear algebra library and you will be on your own in terms of code structuring. We will use C++ features like classes and RAII to organize logic and resource lifetimes. There is also an alternative version of this tutorial available for Rust developers.
如果你喜歡,你可以用C代替C++,但是你將不得不用其他的線性代數庫,且自己負責程式碼結構問題。我們將使用C++的特性(例如類和RAII)來組織邏輯和資源的生命週期。本教程還有一個給Rust開發者的版本。
To make it easier to follow along for developers using other programming languages, and to get some experience with the base API we'll be using the original C API to work with Vulkan. If you are using C++, however, you may prefer using the newer Vulkan-Hpp bindings that abstract some of the dirty work and help prevent certain classes of errors.
為了讓使用其他程式語言的開發者更容易跟進,也為了體驗基礎API,我們將使用原始C語言API來編寫Vulkan程式。但如果你用C++,你可能更喜歡這個更新的Vulkan-Hpp繫結,它搞定了一些煩人的工作,有助於避免某些錯誤。
E-book 電子書
If you prefer to read this tutorial as an e-book, then you can download an EPUB or PDF version here:
- EPUB
如果你更喜歡以電子書的形式閱讀本教程,你可以下載EPUB或PDF版本:
- EPUB
Tutorial structure 教程結構
We'll start with an overview of how Vulkan works and the work we'll have to do to get the first triangle on the screen. The purpose of all the smaller steps will make more sense after you've understood their basic role in the whole picture. Next, we'll set up the development environment with the Vulkan SDK, the GLM library for linear algebra operations and GLFW for window creation. The tutorial will cover how to set these up on Windows with Visual Studio, and on Ubuntu Linux with GCC.
首先,我們將大概看一看,Vulkan是如何工作的,如何在螢幕上顯示第一個三角形。等你理解了每個小步驟在全域性的作用,它們就顯得有道理了。然後,我們將配置開發環境,包括Vulkan SDK,用於線性代數操作的GLM庫和用於建立視窗的GLFW。本教程會介紹如何在Windows上用Visual Studio做這些,也會介紹如何在Ubuntu Linux上用GCC做這些。
After that we'll implement all of the basic components of a Vulkan program that are necessary to render your first triangle. Each chapter will follow roughly the following structure:
- Introduce a new concept and its purpose
- Use all of the relevant API calls to integrate it into your program
- Abstract parts of it into helper functions
之後,我們將實現Vulkan程式的所有基礎元件,用以渲染你的第一個三角形。每一章的基本結構如下:
- 介紹新概念及其目的
- 使用相關API,將其整合到你的程式
- 將其提取封裝到輔助函式
Although each chapter is written as a follow-up on the previous one, it is also possible to read the chapters as standalone articles introducing a certain Vulkan feature. That means that the site is also useful as a reference. All of the Vulkan functions and types are linked to the specification, so you can click them to learn more. Vulkan is a very new API, so there may be some shortcomings in the specification itself. You are encouraged to submit feedback to this Khronos repository.
雖然每一章都是作為上一章的續集,讀者仍可將其視作單獨介紹某個Vulkan特性的文章。也就是說,本網站也當作參考文獻來用。說明書中有所有的Vulkan函式和型別,你可以點選連結來了解更多。Vulkan是個很新的API,所以說明書中可能有一些不足。歡迎讀者在這個Khronos倉庫提交反饋。
As mentioned before, the Vulkan API has a rather verbose API with many parameters to give you maximum control over the graphics hardware. This causes basic operations like creating a texture to take a lot of steps that have to be repeated every time. Therefore we'll be creating our own collection of helper functions throughout the tutorial.
如前所述,Vulkan API十分冗長,引數很多,這給了你對圖形硬體的最大控制權力。這導致每次建立紋理都要走很多步驟。因此本教程中我們將建立一些我們自己的輔助函式。
Every chapter will also conclude with a link to the full code listing up to that point. You can refer to it if you have any doubts about the structure of the code, or if you're dealing with a bug and want to compare. All of the code files have been tested on graphics cards from multiple vendors to verify correctness. Each chapter also has a comment section at the end where you can ask any questions that are relevant to the specific subject matter. Please specify your platform, driver version, source code, expected behavior and actual behavior to help us help you.
每章結尾都會給個連結,展示本章的完整程式碼。如果你對程式碼結構有疑問,或者你在處理bug,想找個對比,可以參考它。所有的程式碼檔案都已經在多個廠商的圖形卡上測試過,以驗證其正確性。每章也有評論區,你可以就相關問題提問。請說明你的平臺,驅動版本,原始碼,期望的行為和實際的行為,好利於我們幫助你。
This tutorial is intended to be a community effort. Vulkan is still a very new API and best practices have not really been established yet. If you have any type of feedback on the tutorial and site itself, then please don't hesitate to submit an issue or pull request to the GitHub repository. You can watch the repository to be notified of updates to the tutorial.
本教程打算成為一個社群的嘗試。Vulkan還是個很新的API,最佳實踐還沒有被髮布出來。如果你對本教程或網站本身有任何反饋,請不要猶豫,在Github倉庫提交issue或pull request就好。你可以watch這個倉庫,這樣就會收到本教程的更新。
After you've gone through the ritual of drawing your very first Vulkan powered triangle onscreen, we'll start expanding the program to include linear transformations, textures and 3D models.
在你按慣例經歷了在螢幕上用Vulkan繪製你的第一個三角形後,我們將開始擴充套件這個程式,納入線性變換,紋理和3D模型。
If you've played with graphics APIs before, then you'll know that there can be a lot of steps until the first geometry shows up on the screen. There are many of these initial steps in Vulkan, but you'll see that each of the individual steps is easy to understand and does not feel redundant. It's also important to keep in mind that once you have that boring looking triangle, drawing fully textured 3D models does not take that much extra work, and each step beyond that point is much more rewarding.
如果你之前玩過圖形API,你會知道在第一個幾何體顯示到螢幕上之前,會有很多個步驟。Vulkan中有很多這樣的初始化步驟,但你會看到,每個步驟都很容易理解,也不冗長。要記住重要的一點是,一旦你得到了那個無趣的三角形,渲染有紋理的3D模型就不需要很多其他工作了,而且之後的每一步都會很令人期待。
If you encounter any problems while following the tutorial, then first check the FAQ to see if your problem and its solution is already listed there. If you are still stuck after that, then feel free to ask for help in the comment section of the closest related chapter.
如果你在學習本教程時遇到任何問題,請先看看FAQ裡是不是已經有了相應的問題和答案了。如果還是沒有解決,請在相關章節的評論區大膽提問。
Ready to dive into the future of high performance graphics APIs? Let's go!
準備好深入未來的高效能圖形API了?出發!
- Next
- 下一章
&n