# 2. 图形绘制流水的基本原理和实践 (1)

#### Vulkan 初始化

如果考虑复杂的 Vulkan 使用场景 一般只需要一个 Vulkan 实例，逻辑设备可以有多个

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011100314.png)

Layer 类似于函数重载 ![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011109825.png)

Vulkan 队列 ![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011113713.png)

创建 Swapchain 属于 Vulkan 的一个拓展 ![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011115457.png)

#### 渲染主循环

顶点缓存的优化是，可以把经常要变化的坐标和法线放在一个 Buffer，而 UV 和其他属性放一个 Buffer

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011135916.png)

索引缓存避免跳转过大，造成 Cache miss ![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011138268.png)

Draw ，Draw Index 绘制，Draw Indirect 是间接绘制，不需要知道顶点的数量，不用绑定顶点数据，是通过 Buffer 里的数据实时生成顶点。

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011139967.png)

Vulkan 的 NDC 坐标

深度的算法是 revert z，z 越大深度越小，存在深度图里的值越小。当 z 为 0 时，深度图里存 1。 ![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011142208.png)

View port 绘制区域一般和纹理区域是一致的， 想要一张图里画多张图的时候，要设置 Viewport 到对应绘制区域，比如 CSM

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011344246.png)

裁剪选项

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011346135.png)

浮点数精度，位置需要 32 位，颜色 8 位就够了

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011350359.png)

片段输出

当在 Frag 里不做深度修改的时候，可以在片段之前就做深度剔除。 但这是 PC 端常用的方式，移动端的 Tile-base 没法知道深度是否会修改，所以这方法没有用？

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011351517.png)

RenderPass 内 Vulkan 的绑定流程 ![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011354732.png)

深度纹理最终 pass 是不需要写入操作的，最终 Pass 只提供读取就行。 一个 Command Buffer 里的所有指令是同步的

![image.png](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202306011410391.png)

### 参考资料

* Vulkan 参考资料
  * 《Vulkan应用开发指南》
  * [Brief guide to Vulkan layers (renderdoc.org)](https://renderdoc.org/vulkan-layer-guide.html)
  * [Vulkan in 30 minutes (renderdoc.org)](https://renderdoc.org/vulkan-in-30-minutes.html)
  * [vulkan11-reference-guide.pdf (khronos.org)](https://www.khronos.org/files/vulkan11-reference-guide.pdf)
  * [Vulkan® 1.3.246 - A Specification (khronos.org)](https://registry.khronos.org/vulkan/specs/1.3/html/)
  * [Vulkan 教程|极客教程 (geek-docs.com)](https://geek-docs.com/vulkan/vulkan-tutorial/vulkan-tutorial-index.html)
* Vulkan示例代码
  * [GitHub - SaschaWillems/Vulkan: Examples and demos for the new Vulkan API](https://github.com/SaschaWillems/Vulkan)
  * [GitHub - KhronosGroup/Vulkan-Samples: One stop solution for all Vulkan samples](https://github.com/KhronosGroup/Vulkan-Samples)
  * [GitHub - google/filament: Filament is a real-time physically based rendering engine for Android, iOS, Windows, Linux, macOS, and WebGL2](https://github.com/google/filament)
* Vulkan调试工具
  * RenderDoc
  * Nvidia Nsight Tool
  * AMD Radeon GPU Profiler • Qualcomm Snapdragon Profiler • Arm Mobile Studio


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://okzkx.gitbook.io/blogs/game-tech/engine/tutorials/games-106/2.-tu-xing-hui-zhi-liu-shui-de-ji-ben-yuan-li-he-shi-jian-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
