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