[译]Vulkan教程(04)基础代码

General structure 通用结构

In the previous chapter you've created a Vulkan project with all of the proper configuration and tested it with the sample code. In this chapter we're starting from scratch with the following code:

在之前的章节你已经创建了一个Vulkan项目,配置好了项目属性,用示例代码进行了测试。本章我们将从零开始写代码,先从下面的代码开始:

 #include <vulkan/vulkan.h>

 #include <iostream>
#include <stdexcept>
#include <functional>
#include <cstdlib> class HelloTriangleApplication {
public:
void run() {
initVulkan();
mainLoop();
cleanup();
} private:
void initVulkan() { } void mainLoop() { } void cleanup() { }
}; int main() {
HelloTriangleApplication app; try {
app.run();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
} return EXIT_SUCCESS;
}

We first include the Vulkan header from the LunarG SDK, which provides the functions, structures and enumerations. The stdexcept and iostream headers are included for reporting and propagating errors. The functional headers will be used for a lambda functions in the resource management section. The cstdlibheader provides the EXIT_SUCCESS and EXIT_FAILURE macros.

首先,我们include了Vulkan头文件,它来自LunarG SDK,他提供函数、结构体和枚举。stdexcept 和iostream 头文件用于报告和传播错误。functional 头文件用于资源管理小节中要介绍到的lambda函数。cstdlib头文件提供了EXIT_SUCCESS 和EXIT_FAILURE 宏。

The program itself is wrapped into a class where we'll store the Vulkan objects as private class members and add functions to initiate each of them, which will be called from the initVulkan function. Once everything has been prepared, we enter the main loop to start rendering frames. We'll fill in the mainLoop function to include a loop that iterates until the window is closed in a moment. Once the window is closed and mainLoop returns, we'll make sure to deallocate the resources we've used in the cleanup function.

这个程序封装了一个class,我们将Vulkan对象保存为private成员,添加了初始化它们的函数,在initVulkan 函数中调用这些初始化函数。一旦一切就绪,我们就进入主循环,开始渲染帧。我们将在mainLoop 函数中循环,直到窗口关闭为止。一旦窗口关闭,mainLoop 返回时,我们将确保在cleanup 函数中销毁使用过的资源。

If any kind of fatal error occurs during execution then we'll throw a std::runtime_error exception with a descriptive message, which will propagate back to the main function and be printed to the command prompt. To handle a variety of standard exception types as well, we catch the more general std::exception. One example of an error that we will deal with soon is finding out that a certain required extension is not supported.

如果执行过程中发生任何error,那么我们将抛出一个std::runtime_error 异常和描述性消息,它会被传回main 函数,打印到控制台。为了处理各种标准异常类型,我们捕捉更泛型的std::exception。我们很快就要处理的一个error的例子是,发现某个要求的扩展不被支持。

Roughly every chapter that follows after this one will add one new function that will be called from initVulkanand one or more new Vulkan objects to the private class members that need to be freed at the end in cleanup.

粗略地说,本章之后的每篇教程都会添加被initVulkan函数调用的新函数,以及添加1个或多个Vulkan对象(作为private成员),这些成员最后都要在cleanup函数中被释放。

Resource management 资源管理

Just like each chunk of memory allocated with malloc requires a call to free, every Vulkan object that we create needs to be explicitly destroyed when we no longer need it. In modern C++ code it is possible to do automatic resource management through the utilities in the <memory> header, but I've chosen to be explicit about allocation and deallocation of Vulkan objects in this tutorial. After all, Vulkan's niche is to be explicit about every operation to avoid mistakes, so it's good to be explicit about the lifetime of objects to learn how the API works.

正如每块用malloc 申请的内存都需要用free释放,当我们不需要它们的时候,我们创建的每个Vulkan对象都需要显式地被销毁。在现代C++代码中,可以用<memory>头文件实现自动化的资源管理,但在本教程中我选择了显式地申请和销毁Vulkan对象。毕竟,Vulkan的利基是,无论什么操作都是显式的,以避免错误。所以显式地处理对象的生命周期,对于学习这个API的工作方式是有益的。

After following this tutorial, you could implement automatic resource management by overloading std::shared_ptr for example. Using RAII to your advantage is the recommended approach for larger Vulkan programs, but for learning purposes it's always good to know what's going on behind the scenes.

学习完本教程后,你可以实现自动资源管理(例如通过std::shared_ptr )。对于大型的Vulkan程序,使用 RAII是一种推荐方式,但是为了学习,知道场面背后在发生什么,总是好的。

Vulkan objects are either created directly with functions like vkCreateXXX, or allocated through another object with functions like vkAllocateXXX. After making sure that an object is no longer used anywhere, you need to destroy it with the counterparts vkDestroyXXX and vkFreeXXX. The parameters for these functions generally vary for different types of objects, but there is one parameter that they all share: pAllocator. This is an optional parameter that allows you to specify callbacks for a custom memory allocator. We will ignore this parameter in the tutorial and always pass nullptr as argument.

Vulkan对象要么是用vkCreateXXX这样的函数直接创建,要么是用vkAllocateXXX这样的函数通过其他对象申请。在确定一个对象不会再被使用后,你需要用对应的vkDestroyXXX 或vkFreeXXX函数销毁它。不同对象的这些函数的参数区别很大,但是它们都有一个共同的参数:pAllocator。这是一个可选参数,它允许你标识一个回调函数,用于以自定义的方式分配内存。本教程中我们将忽略这个参数,总是传给它nullptr 。

Integrating GLFW 集成GLFW

Vulkan works perfectly fine without a creating a window if you want to use it off-screen rendering, but it's a lot more exciting to actually show something! First replace the #include <vulkan/vulkan.h> line with

如果你想用Vulkan搞离屏渲染,那么不用创建窗口,Vulkan也可以工作得很好。但是显示点东西才刚令人兴奋!首先用下述代码替换#include <vulkan/vulkan.h>

 #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

That way GLFW will include its own definitions and automatically load the Vulkan header with it. Add a initWindow function and add a call to it from the run function before the other calls. We'll use that function to initialize GLFW and create a window.

这样,GLFW将包含它自己的定义,且自动加载Vulkan头文件。添加initWindow 函数,在run 函数调用其他函数前,先调用它。我们将用这个函数初始化GLFW,并创建窗口。

 void run() {
initWindow();
initVulkan();
mainLoop();
cleanup();
} private:
void initWindow() { }

The very first call in initWindow should be glfwInit(), which initializes the GLFW library. Because GLFW was originally designed to create an OpenGL context, we need to tell it to not create an OpenGL context with a subsequent call:

initWindow 函数中第一个函数调用应该是glfwInit(),它初始化GLFW库。因为GLFW最初是被设计为创建OpenGL上下文而用的,我们需要在接下来的函数调用中告诉它不要创建OpenGL上下文:

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

Because handling resized windows takes special care that we'll look into later, disable it for now with another window hint call:

因为处理大小可变的窗口要消耗更多精力,我们稍后再谈它,现在暂且禁用它:

glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

All that's left now is creating the actual window. Add a GLFWwindow* window; private class member to store a reference to it and initialize the window with:

现在剩下的就是创建实际的窗口了。添加private成员GLFWwindow* window;,以保存对窗口的引用。用下述代码初始化窗口:

window = glfwCreateWindow(, , "Vulkan", nullptr, nullptr);

The first three parameters specify the width, height and title of the window. The fourth parameter allows you to optionally specify a monitor to open the window on and the last parameter is only relevant to OpenGL.

前3个参数标明窗口的宽度、高度和标题。第4个参数允许你可选地标明在哪个显示器上打开窗口,最后一个参数只与OpenGL有关。

It's a good idea to use constants instead of hardcoded width and height numbers because we'll be referring to these values a couple of times in the future. I've added the following lines above the HelloTriangleApplicationclass definition:

用常量代替硬编码的宽度和高度数值是个好主意,因为我们将多次引用这些数据。我在类定义之前添加了下述2行:

 const int WIDTH = ;
const int HEIGHT = ;

and replaced the window creation call with

用下述代码代替了创建窗口的函数调用:

window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);

You should now have a initWindow function that looks like this:

现在你应该有个长这样的initWindow 函数:

 void initWindow() {
glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
}

To keep the application running until either an error occurs or the window is closed, we need to add an event loop to the mainLoop function as follows:

为了让app持续运行,直到发生错误或窗口关闭,我们需要在mainLoop 函数中添加一个事件循环,如下代码所示:

 void mainLoop() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
}

This code should be fairly self-explanatory. It loops and checks for events like pressing the X button until the window has been closed by the user. This is also the loop where we'll later call a function to render a single frame.

这个代码是不言自明的。它循环检查事件(例如点击X按钮),直到窗口被用户关闭为止。在这个循环里,我们稍后会调用另一个函数来渲染一帧。

Once the window is closed, we need to clean up resources by destroying it and terminating GLFW itself. This will be our first cleanup code:

一旦窗口被关闭,我们需要销毁资源,关闭GLFW。下面是我们最初的cleanup 代码:

 void cleanup() {
glfwDestroyWindow(window); glfwTerminate();
}

When you run the program now you should see a window titled Vulkan show up until the application is terminated by closing the window. Now that we have the skeleton for the Vulkan application, let's create the first Vulkan object!

运行此程序,现在你可以看到一个标题为Vulkan 的窗口显示出来,直到你通过关闭按钮终结它。既然我们有了Vulkan程序的骨架,我们来创建第一个Vulkan对象吧!

C++ code 完整C++代码

[译]Vulkan教程(04)基础代码的更多相关文章

  1. [译]Vulkan教程(13)图形管道基础之Shader模块

    [译]Vulkan教程(13)图形管道基础之Shader模块 Shader modules Unlike earlier APIs, shader code in Vulkan has to be s ...

  2. [译]Vulkan教程(12)图形管道基础之入门

    [译]Vulkan教程(12)图形管道基础之入门 Introduction 入门 Over the course of the next few chapters we'll be setting u ...

  3. [译]Vulkan教程(16)图形管道基础之总结

    [译]Vulkan教程(16)图形管道基础之总结 Conclusion 总结 We can now combine all of the structures and objects from the ...

  4. [译]Vulkan教程(15)图形管道基础之RenderPass

    [译]Vulkan教程(15)图形管道基础之RenderPass Render passes Setup 设置 Before we can finish creating the pipeline, ...

  5. [译]Vulkan教程(14)图形管道基础之固定功能

    [译]Vulkan教程(14)图形管道基础之固定功能 Fixed functions 固定功能 The older graphics APIs provided default state for m ...

  6. [译]Vulkan教程(18)命令buffers

    [译]Vulkan教程(18)命令buffers Command buffers 命令buffer Commands in Vulkan, like drawing operations and me ...

  7. [译]Vulkan教程(10)交换链

    [译]Vulkan教程(10)交换链 Vulkan does not have the concept of a "default framebuffer", hence it r ...

  8. [译]Vulkan教程(09)窗口表面

    [译]Vulkan教程(09)窗口表面 Since Vulkan is a platform agnostic API, it can not interface directly with the ...

  9. [译]Vulkan教程(07)物理设备和队列家族

    [译]Vulkan教程(07)物理设备和队列家族 Selecting a physical device 选择一个物理设备 After initializing the Vulkan library ...

随机推荐

  1. Git下载安装及github基本操作

    Windows下Git下载 官网提供的Git下载地址: 官网下载地址,一般直接从官网下载会出现无反应等情况,这里提供windows32和64位版本的百度网盘资源:windows下的Git下载地址.有需 ...

  2. 【React】377- 实现 React 中的状态自动保存

    点击上方"前端自习课"关注,学习起来~ 作者:陈俊宇 https://github.com/CJY0208 什么是状态保存? 假设有下述场景: 移动端中,用户访问了一个列表页,上拉 ...

  3. Electron node integration enabled 设置

    解决办法 参考博客:https://blog.csdn.net/hwytree/article/details/103167175

  4. 【hibernate】常用注解

    [hibernate]常用注解 转载:https://www.cnblogs.com/yangchongxing/p/10357118.html 目录 ======================== ...

  5. 在 ASP.NET Core 中使用 Serilog 进行日志记录

    目录 从 NuGet 安装 Serilog 在 Main函数 中配置 Serilog 在项目中使用 Serilog 进行日志输出 从 NuGet 安装 Serilog 核心的包是 Serilog 和 ...

  6. python错误处理—try…catch…finally、调用栈分析

    高级语言包括python一般都内置了一套try…catch…finally的错误处理机制: >>> try: ... print('try...') ... r = 10 / 0 . ...

  7. DC8: Vulnhub Walkthrough

    镜像下载链接: https://www.vulnhub.com/entry/dc-8,367/#download 主机扫描: http://10.10.202.131/?nid=2%27 http:/ ...

  8. Android 线性布局 LinearLayout

    垂直布局 vertical <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  9. Graylog 环境搭建与使用

    graylog 官网地址: www.graylog.org graylog 简易架构图: graylog 生产架构图: graylog 有两个版本,1.企业版:2.开源版 下面使用开源版本搭建日志中心 ...

  10. https类型接口,关闭认证(verify=False)情况下消除告警的方法

    一.问题描述 当我们测试接口的协议为https时,当关闭了认证(verify=False)的时候,我们测试通过了,但是使用有一条警告显示,最终生成的测试报告也不美观,如下图 二.解决办法 我们可以在封 ...