CSharpGL(56)[译]Vulkan入门(转)

Posted strugglerisnd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CSharpGL(56)[译]Vulkan入门(转)相关的知识,希望对你有一定的参考价值。

CSharpGL(56)[译]Vulkan入门

本文是对(http://ogldev.atspace.co.uk/www/tutorial50/tutorial50.html)的翻译,作为学习Vulkan的一次尝试。

不翻译的话,每次都在看第一句,那就学不完了。 

Background 背景

You‘ve probably heard by now quite a bit about Vulkan, the new Graphics API from Khronos (the non profit organization responsible for the development of OpenGL).

你可能听过Vulkan,Khronos创建的新的图形API。Khronos是负责开发OpenGL的非盈利组织。

Vulkan was announced in Feb-2016 and after 24 years with OpenGL it is a completely new standard and a departure from the current model.

在OpenGL出现24年后,Vulkan于2016年2月被发布。它放弃了当前的模型,它是全新的标准。

I won‘t go into many details about the various features of Vulkan only to say that in comparison to OpenGL it is much more low level and provides a lot of power and performance opportunities for the developer.

我不会上来就抛出一堆新的特性的细节。简单来说,相比OpenGL,Vulkan底层得多,给开发者提供了巨大的能量和提升性能的机会。

But with great power comes great responsibility.

但是能力越大,责任就越大。

The developer has to take charge of various aspects such as command buffer, synchronization and memory management that were previously the sole responsibility of the driver.

开发者必须负责各种方面,例如命令缓存、同步、内存管理,这些之前都是驱动的责任。

Through the unique knowledge that the developer has about the way the application is structured, the usage of the Vulkan API can be tailored in a way to increase the overall performance of the system.

基于开发者对应用程序结构的知识,他可以调整Vulkan API的用法,获得更高的系统性能。

 

The thing that surprises people the most, IMHO, about Vulkan is the amount of code that must be written only to get the first triangle on the screen.

最让人吃惊的,恕我直言,是在屏幕上显示第一个三角形所需的巨大代码量。

Comparing this to the few lines we had to write in OpenGL in the first few tutorials this is a major change and becomes a challenge when one tries to write a tutorial about it.

相比在最初的教程中我们写的那几行OpenGL代码,这是很大的改变,也是写Vulkan教程的难点。

Therefore, as always with OGLDEV, I‘ll try to present the material step by step.

因此,像OGLDEV往常一样,我将一步一步地展开本教程。

We will develop our first triangle demo in a few tutorials, making additional progress in each one.

我们将在多个教程中完成第一个三角形示例,在每个教程中进展一点点。

In addition, instead of laying out the dozens of APIs in one long piece of code I‘ll present a simple software design that I hope will make it simpler for you to understand without imposing too much restrictions on your future apps.

另外,我不展示一大段API,而是展示有设计思路的软件。我希望这能便于读者理解。

Consider this an educational design which you are free to throw away later.

这个设计以教学为目的,等你学会了,就可以扔掉了。

 

We will study the core components of Vulkan one by one as we make progress through the code so at this point I just want to present a diagram of the general picture:

随着代码,我们将一个个地学习Vulkan核心组件。现在我们先看一下概览图:

 技术图片

 

This diagram is by all means not a complete representation.

当然这个图并没有展示所有的组件。

It includes only the major components that will probably be present in most applications.

它只包含会在大部分应用程序中使用的主要组件。

The connectors between the objects represent the dependencies between them at creation or enumeration time.

对象之间的连线表示在创建或枚举时的依赖关系。

For example, in order to create a surface you need an instance object and when you enumerate the physical devices on your system you also need an instance.

例如,为了创建一个surface,你需要一个instance对象;当你枚举你系统上的物理设备时,你也需要一个instance对象。

The two colors roughly describe the software design that we will use.

两种颜色粗略地描述了我们将使用的软件设计方案。

The dark red objects will go into something I call the "core" and the light green objects will go into the "app".

暗红色对象将属于“core”,浅绿色对象将属于“app”。

We will later see why this makes sense.

我们稍后再看为什么是这样。

The application code that you will write will actually inherit from "app" and all of its members will be available for you for further use.

应用程序代码将继承自app,app的所有成员以后都将可用。

I hope this design will provide a solid base to develop future Vulkan tutorials.

我希望这样的设计能提供一个坚实的基础,用于开发将来的Vulkan教程。

 

System Setup 系统安装

The first thing we need to do is to make sure your system supports Vulkan and get everything ready for development.

我们要做的第一件事,是确保你的系统支持Vulkan,准备好开发所需的一切。

You need to verify that your graphics card supports Vulkan and install the latest drivers for it.

你需要验证你的图形卡是否支持Vulkan,并安装最新的驱动程序。

Since Vulkan is still new it‘s best to check for drivers updates often because hardware vendors will probably fix a lot of bugs before everything stabilizes.

由于Vulkan还很新,最好经常检查驱动更新,因为硬件厂商可能会在驱动稳定前修复很多bug。

Since there are many GPUs available I can‘t provide much help here.

由于有太多种GPU,我这里爱莫能助。

Updating/installing the driver on Windows should be fairly simple.

在Windows上更新/安装驱动应该相当简单。

On Linux the process may be a bit more involved.

在Linux上,就有点难缠。

My main development system is Linux Fedora and I have a GT710 card by NVIDIA.

我的开发系统是Linux的Fedora版本,显卡是NVIDIA的GT710。

NVIDIA provide a binary run file which can only be installed from the command line.

NVIDIA提供一个二进制运行文件,只能从命令行安装。

Other vendors have their own processes.

其他厂商有各自的方式。

On Linux you can use the ‘lspci‘ to scan your system for devices and see what GPU you have.

在Linux上你可以用‘lspci‘命令扫描你的系统,看看有哪些设备,用的什么GPU。

You can use the ‘-v‘, ‘-vv‘ and ‘-vvv‘ options to get increasingly more info on your devices.

你可以用‘-v‘、‘-vv‘、‘-vvv‘来得到你的设备的越来越详细的信息。

 

The second thing we need is the Vulkan SDK by Khronos, available here.

第二件事,我们需要Khronos的Vulkan SDK,可在此下载。

The SDK includes the headers and libraries we need as well as many samples that you can use to get more info beyond what this tutorial provides.

SDK包含头文件和库文件,很多示例,比本教程多得多的信息。

At the time of writing this the latest version is 1.0.30.0 and I urge you to update often because the SDK is in active development.

写作本文时最新版本是1.0.30.0,我推荐读者时常更新,因为SDK还处于活跃地开发中。

That version number will be used throughout the next few sections so make sure you change it according to the version you have.

接下来的章节都将使用这个版本号,所以,根据你的版本号,相应地替换之。

 

Linux

Khronos provides a package only for Ubuntu in the form of an executable run file.

Khronos只给Ubuntu提供了一个可执行文件。

Executing this file should install everything for you but on Fedora I encoutered some difficulties so I used the following procedure (which is also forward looking in terms of writing the code later):

执行这个文件就可以安装所需的一切。但是在Fedora上我遇到了一些困难,所以我用下述步骤(也是预览一下代码):

  • bash$ chmod +x vulkansdk-linux-x86_64-1.0.30.0.run
  • base$ ./vulkansdk-linux-x86_64-1.0.30.0.run --target VulkanSDK-1.0.30.0 --noexec
  • base$ ln -s ~/VulkanSDK-1.0.30/1.0.30.0 ~/VulkanSDK

The above commands extract the contents of the package without running its internal scripts.

上述命令提取出包的内容,并执行里面的脚本。

After extraction the directory VulkanSDK-1.0.30.0 will contain a directory called 1.0.30.0 where the actual content of the package will be located.

提取完成后,文件夹VulkanSDK-1.0.30.0会包含一个子文件夹1.0.30.0,里面是实际的内容。

Let‘s assume I ran the above commands in my home directory (a.k.a in bash as ‘~‘) so we should end up with a ‘~/VulkanSDK‘ symbolic link to the directory with the actual content (directories such as ‘source‘, ‘samples‘, etc).

假定我是在home文件夹下运行的上述命令(即‘~‘),那么会有一个‘~/VulkanSDK‘符号链接到实际内容(文件夹‘source‘、‘samples‘等)。

This link makes it easier to switch your development environment to newer versions of the SDK.

这个链接使得切换到SDK的新版本更容易。

It points to the location of the headers and libraries that we need.

它指向我们需要的头文件和库文件的位置。

We will see later how to connect them to the rest of the system. Now do the following:

稍后我们将看到如何将它们连接到系统的其他部分。现在执行下述命令:

  • bash$ cd VulkanSDK/1.0.30.0
  • bash$ ./build_examples.sh

If everything went well the examples were built into ‘examples/build‘.

如果一切顺利,示例会出现在文件夹‘examples/build‘

To run the examples you must first cd into that directory.

为运行示例,你首先要进入这个文件夹。

You can now run ‘./cube‘ and ‘./vulkaninfo‘ to make sure Vulkan runs on your system and get some useful information on the driver.

你现在可以运行‘./cube‘和‘./vulkaninfo‘命令来确认Vulkan跑在你的系统上了,还可以得到一些驱动的有用信息。

Hopefully everything is OK so far so we want to create some symbolic links that will make the files we need for development easily accessible from our working environment.

单元一切顺利,目前我们想创建一些符号链接,方便我们使用开发过程中会用到的文件。

Change to the root user (by executing ‘su‘ and entering the root password) and execute the following:

跳到root用户(执行‘su‘命令,输入root密码),执行下述命令:

  • bash# ln -s /home/<your username>/VulkanSDK/x86_x64/include/vulkan /usr/include
  • base# ln -s /home/<your username>/VulkanSDK/x86_x64/lib/libvulkan.so.1 /usr/lib64
  • base# ln -s /usr/lib64/libvulkan.so.1 /usr/lib64/libvulkan.so

What we did in the above three commands is to create a symbolic link from /usr/include to the vulkan header directory.

上述3个命令,创建了一个从/usr/include到Vulkan头文件夹的符号链接。

We also created a couple of symbolic links to the shared object files against which we are going to link our executables.

我们还创建了一些共享对象的符号链接,今后会将我们的程序链接到这些共享对象。

以上是关于CSharpGL(56)[译]Vulkan入门(转)的主要内容,如果未能解决你的问题,请参考以下文章

CSharpGL(57)[译]Vulkan清空屏幕

[译]Vulkan教程(20)重建交换链

[译]Vulkan教程(08)逻辑设备和队列

[译]Vulkan教程(21)顶点input描述

CSharpGL(31)[译]OpenGL渲染管道那些事

[译]Vulkan教程(03)开发环境