Vulkan再探(12):输入附件

本文详细介绍了Vulkan中的输入附件,包括子通道输入附件的定义、创建图像、附件描述、子通道创建及其依赖性。此外,还讨论了管道设置、着色器如何读取输入附件,以及附件内容的初始化。最后,通过图解和知识补充,阐述了管道阶段、图像使用和着色器的工作原理。
摘要由CSDN通过智能技术生成

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 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值