08/20/2020
文章目录
输入附件
输入附件来自于上一个通道相同片段位置上
子通道输入附件定义
typedef struct VkSubpassDescription
{
VkSubpassDescriptionFlags flags;
VkPielineBindPoint pipelineBindPoint;
uint32_t inputAttachmentCount; //多个输入附件
const VkAttachmentReference* pInputAttachments;
uint32_t colorAttachmentCount;
const VkAttachmentReference* pColorAttachements; //颜色附件
const VkAttachmentReference* pResolveAttachements;
const VkAttachmentReference* pDepthStencilAttachement; //只有一个深度模板附件
uint32_t preserveAttachmentCount;
const uint32_t* pPreserveAttachments;
};
- 每个子通道可以有一个或多个输入附件,这些附件是你可以在片段着色器中读取的附件
创建图像作为输入附件
struct FrameBufferAttachment {
VkImage image;
VkDeviceMemory memory;
VkImageView view;
VkFormat format;
};
const VkFormat colorFormat = VK_FORMAT_R8G8B8A8_UNORM;
//帧缓冲区需要的附件,主要是创建图像,更改图像布局,最后是创建图像视图
attachments.resize(swapChain.imageCount);
for (auto i = 0; i < attachments.size(); i++) {
createAttachment(colorFormat, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &attachments[i].color);
createAttachment(depthFormat, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, &attachments[i].depth);
}
创建图像需要描述为INPUT_ATTACHMENT
附件描述
创建附件之后,渲染管道需要对附件描述
std::array<VkAttachmentDescription, 3> attachments{
};
//交换链图像的格式 --- 后备缓冲区
//最后转换到可以展示的图像格式
//这是一个颜色附件,是R8G8B8A8_UNORM类型的单采样图像,我们希望在渲染通道开始对它清除,并在完成之后保存它。它以Undefined布局开始,并且在完成时将它设置为PRESENT_SRC状态
attachments[0].format = swapChain.colorFormat;
attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; //开始时清除
attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; //完成之后保存
attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; //
attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
//颜色附件 ---
attachments[1].format = colorFormat;
attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachments[1].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
//深度附件 --- 深度缓冲区
attachments[2].format = depthFormat;
attachments[2].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[2].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[2].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[2].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachments[2].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
创建子通道
std::array<VkSubpassDescription,2> subpassDescriptions{
};
/*
第一个子通道
装载两个附件,一个颜色一个深度附件的引用
通过attchments数组中的下标来引用,和对应的布局,参见上面的附件内容
*/
VkAttachmentReference colorReference