[译]Vulkan教程(01)入门

[译]Vulkan教程(01)入门

接下来我将翻译(https://vulkan-tutorial.com)上的Vulkan教程。这可能是我学习Vulkan的最好方式,但不是最理想的方式。

我会用“driver(驱动程序)”这样的方式翻译某些关键词语,在后续的文字中,则只使用英文。这可以减少歧义,且使译文易读。

Introduction 入门

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 (NVIDIAAMDIntel)
  • 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:

本教程不要求读者知道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或PDF版本:

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了?出发!

 

posted @ 2019-06-20 15:30  BIT祝威  阅读(6546)  评论(0编辑  收藏  举报