Vulkan(4):动态统一缓冲区描述符

08/18/2020

游戏对象不同

struct GameObject
{
   
	struct Matrices
	{
   
		glm::mat4 projection;
		glm::mat4 view;
		glm::mat4 model; 				//每个物体都有自己的模型矩阵
	};
	struct UniformBuffer				//每一个物体必须有自己的统一缓冲区
	{
   
		VkBuffer buffer;
		VkDeviceMemory memory;
		VkDescriptorSet descriptroSet;	//每一个物体有不同的描述符集合,存放着不同model,texture数据。
	};
	Texture2D texture;					//不同的纹理贴图
}

通常游戏对象都有自己的大小,朝向和位置,所以区分变换对象时候,改变的是统一缓冲区的glm::mat4 model的值。由于着色器需要通过描述符集合来访问统一缓冲区,所以每一个物体都必须有描述符集合,其中描述符集合存放的统一缓冲区和组合图像采样器是不一样的。

描述多个物体

std::array<GameObject,2> cube; //创建两个正方体

//可以共用一个描述符布局,一个uniform buffer 另一个combined image sampler
std::array<VkDescriptorSetLayoutBinding,2> setLayoutBindings{
   };
setLayoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
setLayoutBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;

//池子的大小
//描述符池根据描述符布局数量而创建
std::array<VkDescriptorPoolSize, 2> descriptorPoolSizes{
   };
descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;

//池子可以限制集合的总数量
VkDescriptorPoolCreateInfo descriptorPoolCI = {
   };
descriptorPoolCI.maxSets = static_cast<uint32_t>(cubes.size());


//创建集合,可以使用相同的布局和池子,但是每一个物体需要一个集合供命令缓冲区使用

for (auto &cube: cubes) 
{
   

	// Allocates an empty descriptor set without actual descriptors from the pool using the set layout
		VkDescriptorSetAllocateInfo allocateInfo{
   };
		allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
		allocateInfo.descriptorPool = descriptorPool;
		allocateInfo.descriptorSetCount = 1;
		allocateInfo.pSetLayouts = &descriptorSetLayout;
		VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocateInfo, &cube.descriptorSet));

		// Update the descriptor set with the actual descriptors matching shader bindings set in the layout
		std::array<VkWriteDescriptorSet, 2> writeDescriptorSets{
   };

		/*
			Binding 0: Object matrices Uniform buffer
		*/
		writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
		writeDescriptorSets[0].dstSet = cube.descriptorSet;
		writeDescriptorSets[0].dstBinding = 0;
		writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
		writeDescriptorSets[0].pBufferInfo = &cube.uniformBuffer.descriptor;
		writeDescriptorSets[0].descriptorCount = 1;

		/*
			Binding 1: Object texture
		*/
		writeDescriptorSets[1
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值