[译]Vulkan教程(17)帧缓存

Framebuffers 帧缓存

We‘ve talked a lot about framebuffers in the past few chapters and we‘ve set up the render pass to expect a single framebuffer with the same format as the swap chain images, but we haven‘t actually created any yet.

我们在过去的章节谈论过很多次帧缓存了,我们已经设置了render pass,希望有一个帧缓存with与交换链image相同的格式,但是我们还没有创建帧缓存。

The attachments specified during render pass creation are bound by wrapping them into a VkFramebuffer object. A framebuffer object references all of the VkImageView objects that represent the attachments. In our case that will be only a single one: the color attachment. However, the image that we have to use for the attachment depends on which image the swap chain returns when we retrieve one for presentation. That means that we have to create a framebuffer for all of the images in the swap chain and use the one that corresponds to the retrieved image at drawing time.

在创建render pass时指定的附件,通过一个VkFramebuffer 对象关联起来。帧缓存对象引用所有的VkImageView 对象that代表附件。在我们的案例中只有1个,即颜色附件。但是,用作附件的image依赖于交换链返回哪个image when我们检索一个for呈现。这意味着,我们必须创建一个帧缓存for交换链的每个image,在绘制时使用与检索到的image对应的那个帧缓存。

To that end, create another std::vector class member to hold the framebuffers:

为此,创建另一个类成员std::vector  to记录这些帧缓存。

std::vector<VkFramebuffer> swapChainFramebuffers;

We‘ll create the objects for this array in a new function createFramebuffers that is called from initVulkan right after creating the graphics pipeline:

我们将为此数组创建对象在新函数createFramebuffers that被initVulkan 调用after创建图形管道:

void initVulkan() {
    createInstance();
    setupDebugCallback();
    createSurface();
    pickPhysicalDevice();
    createLogicalDevice();
    createSwapChain();
    createImageViews();
    createRenderPass();
    createGraphicsPipeline();
    createFramebuffers();
}

...

void createFramebuffers() {

}

Start by resizing the container to hold all of the framebuffers:

开始,调整容器大小to记录所有的帧缓存:

void createFramebuffers() {
    swapChainFramebuffers.resize(swapChainImageViews.size());
}

We‘ll then iterate through the image views and create framebuffers from them:

然后我们遍历image视图,为它们创建帧缓存:

for (size_t i = 0; i < swapChainImageViews.size(); i++) {
    VkImageView attachments[] = {
        swapChainImageViews[i]
    };

    VkFramebufferCreateInfo framebufferInfo = {};
    framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    framebufferInfo.renderPass = renderPass;
    framebufferInfo.attachmentCount = 1;
    framebufferInfo.pAttachments = attachments;
    framebufferInfo.width = swapChainExtent.width;
    framebufferInfo.height = swapChainExtent.height;
    framebufferInfo.layers = 1;

    if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
        throw std::runtime_error("failed to create framebuffer!");
    }
}

As you can see, creation of framebuffers is quite straightforward. We first need to specify with which renderPassthe framebuffer needs to be compatible. You can only use a framebuffer with the render passes that it is compatible with, which roughly means that they use the same number and type of attachments.

如你所见,帧缓存的创建过程是十分直观的。我们首先要指定帧缓存需要与哪个renderPass兼容。你只能用与render pass兼容的帧缓存,基本上意思是它们的附件的数量和类型相同。

The attachmentCount and pAttachments parameters specify the VkImageView objects that should be bound to the respective attachment descriptions in the render pass pAttachment array.

attachmentCount 和pAttachments 参数指定VkImageView 对象that应当被绑定到相应的附件描述信息in render pass pAttachment 数组。

The width and height parameters are self-explanatory and layers refers to the number of layers in image arrays. Our swap chain images are single images, so the number of layers is 1.

width 和height 参数是不言自明的,layers 指image数组中的layer的数量。我们的交换链image是单image,所以layer数量为1

We should delete the framebuffers before the image views and render pass that they are based on, but only after we‘ve finished rendering:

我们应当在删除image视图和render pass之前删除帧缓存,但是要在完成渲染之后:

void cleanup() {
    for (auto framebuffer : swapChainFramebuffers) {
        vkDestroyFramebuffer(device, framebuffer, nullptr);
    }

    ...
}

We‘ve now reached the milestone where we have all of the objects that are required for rendering. In the next chapter we‘re going to write the first actual drawing commands.

我们现在到达了一个里程碑where我们有了所有的对象that被要求用于渲染。下一章我们要写第一个实际的绘制命令。

C++ code / Vertex shader / Fragment shader

原文地址:https://www.cnblogs.com/bitzhuwei/p/Vulkan-Tutorial-17-Framebuffers.html

时间: 2024-08-11 04:46:23

[译]Vulkan教程(17)帧缓存的相关文章

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

Swap chain recreation 重建交换链 Introduction 入门 The application we have now successfully draws a triangle, but there are some circumstances that it isn't handling properly yet. It is possible for the window surface to change such that the swap chain is n

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

这是我翻译(https://vulkan-tutorial.com)上的Vulkan教程的第3篇. In this chapter we'll set up your environment for developing Vulkan applications and install some useful libraries. All of the tools we'll use, with the exception of the compiler, are compatible with

[译]Vulkan教程(06)验证层

What are validation layers? 什么是验证层? The Vulkan API is designed around the idea of minimal driver overhead and one of the manifestations of that goal is that there is very limited error checking in the API by default. Even mistakes as simple as settin

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

Introduction 入门 After selecting a physical device to use we need to set up a logical device to interface with it. The logical device creation process is similar to the instance creation process and describes the features we want to use. We also need

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

Vertex input description 顶点input描述 Introduction 入门 In the next few chapters, we're going to replace the hardcoded vertex data in the vertex shader with a vertex buffer in memory. We'll start with the easiest approach of creating a CPU visible buffer

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 responsibl

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

阅读目录(Content) Background 背景 System Setup 系统安装 Linux Windows Building and Running 建设和运行 Linux Windows General Comments 基础命令 Code Structure 代码结构 Source walkthru 源代码浏览 CSharpGL(56)[译]Vulkan入门 本文是对(http://ogldev.atspace.co.uk/www/tutorial50/tutorial50.ht

[CSDN] 从FRAGMENT到PIXEL(framebuffer 帧缓存)

http://blog.csdn.net/leonwei/article/details/4171870 从FRAGMENT到PIXEL(framebuffer 帧缓存) 1.帧缓存包括颜色.scissor.alpha.stencil.depth这些缓存,所以帧缓存不是一片缓存,而是所有这些缓存的组合,帧缓存它对应了屏幕上的每一个pixel(不是真正的pixel,而是一个fragment所对应的位置)的各种这些信息(颜色.ZBUFFER.等等),几何体的fragment时没有帧缓存的,帧缓存时屏

【OpenGL】OpenGL帧缓存对象(FBO:Frame Buffer Object) 【转】

http://blog.csdn.net/xiajun07061225/article/details/7283929/ OpenGL Frame BufferObject(FBO) Overview: 在OpenGL渲染管线中,几何数据和纹理经过多次转化和多次测试,最后以二维像素的形式显示在屏幕上.OpenGL管线的最终渲染目的地被称作帧缓存 (framebuffer).帧缓冲是一些二维数组和OpenG所使用的存储区的集合:颜色缓存.深度缓存.模板缓存和累计缓存.一般情况下,帧缓存完全 由wi