wiki of Vulkan

Prepare

1、Introduction

Vulkan is a new API of the Khronos group (known as OpenGL) that provides a better abstraction for modern graphics cards. The idea behind Vulkan is similar to that of Direct3D 12 and Metal,

Advantage:

  • Better performance and lower driver overhead,
  • Vulkan has the advantage of being completely cross-platform, allowing you to develop for Windows, Linux and Android at the same time.

Disadvantages:

  • A more verbose API must be used. Every detail related to the graphics API needs to be set up by your application from scratch, including initial frame buffer creation and memory management of objects such as buffers and texture images.
  • Very limited error checking and debugging capabilities. (
    Allows extensive checks to be enabled through a function called the verification layer. The verification layer is a piece of code that can be inserted between the API and the graphics driver to perform operations such as running additional checks on function parameters and tracking memory management issues. )

2、Triangle process

  • Create a VkInstance
  • Select the supported graphics card (VkPhysicalDevice)
  • Create VkDevice and VkQueue for drawing and presentation
  • Create windows, window surfaces and swap chains
  • Pack the swap chain image into VkImageView
  • Create a rendering pass, specify the rendering target and purpose, create a frame buffer for the rendering pass
  • Set up graphics pipeline
  • Allocate and record the command buffer with drawing commands for each possible swap chain image
  • Draw the frame by acquiring the image, submitting the correct drawing command buffer and returning the image to the swap chain.

3. Coding Convention

All Vulkan functions, enumerations, and structures are defined in the vulkan.h header, which is included in the Vulkan SDK developed by LunarG.

Function: There is a lowercase vk prefix,
Enumerations and structures: Such types have a Vk prefix,
Enumeration value: There is a VK_ prefix.
The API makes extensive use of structures to provide parameters for functions.

Many structures in Vulkan require that the type of the structure be explicitly specified in the sType member.

Draw a triangle

1. Configuration

1.1 General Architecture

The program itself is wrapped in a class, where we will store Vulkan objects as private class members and add functions to start each of them, which will be called from the initVulkan function. After everything is ready, we enter the main loop to start rendering frames. We will fill the mainLoop function to contain a loop that will continue to iterate until the window is closed later. Once the window is closed and mainLoop returns, we will make sure to release the resources we used in the cleanup function.

Vulkan 中的对象创建函数参数遵循的一般模式是:

  • 指向带有创建信息的结构体的指针
  • 指向自定义分配器回调的指针,在本教程中始终为 nullptr
  • 指向存储新对象句柄的变量的指针

Resource Management

Vulkan objects are either created directly using functions such as vkCreateXXX, or allocated through another object using functions such as vkAllocateXXX. After ensuring that an object is no longer used anywhere, you need to use the corresponding vkDestroyXXX and vkFreeXXX to destroy it. The parameters of these functions are usually different for different types of objects, but they all share one parameter: pAllocator. This is an optional parameter that allows you to specify a callback for the custom memory allocator. We will ignore this parameter in the tutorial and always pass nullptr as a parameter.

验证层

验证层是可选组件,可与 Vulkan 函数调用挂钩来应用其他操作。 验证层中的常见操作有:

  • 根据规范检查参数值以检测误用
  • 跟踪对象的创建和销毁以发现资源泄漏
  • 通过跟踪调用源自的线程来检查线程安全
  • 将每个调用及其参数记录到标准输出
  • 跟踪 Vulkan 调用来进行分析和重放

这些验证层可以自由堆叠以包含您感兴趣的所有调试功能。

物理设备

  • vkGetPhysicalDeviceProperties 查询名称、类型和支持的 Vulkan 版本等基本设备属性。

  • 可以使用 vkGetPhysicalDeviceFeatures 查询对纹理压缩、64 位浮点数和多视口渲染(对 VR 有用)等可选功能的支持:

2、Presentation显示

2.2交换链Swap chain

Vulkan 没有“默认帧缓冲区”的概念,因此它需要一个基础设施来拥有我们将渲染的缓冲区,然后我们才能在屏幕上将它们可视化。 此基础结构称为交换链,必须在 Vulkan 中显式创建。 交换链本质上是一个等待呈现在屏幕上的图像队列。 我们的应用程序将获取这样一个图像来绘制它,然后将其返回到队列中。 队列的工作原理以及从队列中呈现图像的条件取决于交换链的设置方式,但交换链的一般目的是使图像的呈现与屏幕的刷新率同步。

交换链三种属性:

  • 基本表面功能(交换链中的最小/最大图像数,图像的最小/最大宽度和高度)
  • 表面格式(像素格式、色彩空间)
  • 可用的演示模式(演示模式可以说是交换链最重要的设置,因为它代表了在屏幕上显示图像的实际条件。)
    • VK_PRESENT_MODE_IMMEDIATE_KHR:您的应用程序提交的图像会立即传输到屏幕,这可能会导致撕裂(tearing)。

    • VK_PRESENT_MODE_FIFO_KHR:队列、会堵塞,垂直空白
      交换链是一个队列,当显示器刷新时,显示器从队列前面取一张图像,程序在队列后面插入渲染图像。如果队列已满,则程序必须等待。这与现代游戏中的垂直同步最为相似。显示刷新的那一刻被称为“垂直空白”。
      垂直同步

    • VK_PRESENT_MODE_FIFO_RELAXED_KHR:队列、堵塞时直接传输到达图像,撕裂
      如果应用程序延迟并且队列在最后一个垂直空白处为空,则此模式与前一种模式不同。图像最终到达时立即传输,而不是等待下一个垂直空白。这可能会导致可见的撕裂。

    • VK_PRESENT_MODE_MAILBOX_KHR:这是第二种模式的另一种变体。当队列已满时,不会阻塞应用程序,而是将已经排队的图像简单地替换为较新的图像。此模式可用于在避免撕裂的同时尽可能快地渲染帧,从而导致比标准垂直同步更少的延迟问题。这就是俗称的“三重缓冲”,尽管单独存在三个缓冲并不一定意味着帧率已解锁。

总结
Swap chain—— 一种队列形式的缓冲区;
1、应用程序作为生产者会获取图像进行绘制,然后将其返还给交换链图像队列,等待屏幕消费。
2、VK_PRESENT_MODE_FIFO_KHR是一定存在的模式,VK_PRESENT_MODE_MAILBOX_KHR是不考虑能源消耗时的最佳模式。
3、流程:查找交换链的支持–>查询交换链支持的详情(基本表面功能、表面格式、显示模式)–>设置交换链模式(属性:表面格式、显示模式、交换范围)–>创建交换链–>检索交换链图像

GLFW 在测量尺寸时使用两种单位:像素和屏幕坐标。但是 Vulkan 使用像素,因此交换链范围也必须以像素为单位指定。

2.3图像视图

图像视图实际上是图像的视图。 它描述了如何访问图像以及要访问图像的哪个部分,在本章中,我们将编写一个 createImageViews 函数,该函数为交换链中的每个图像创建一个基本的图像视图,以便我们稍后可以将它们用作颜色目标

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值