// Create descriptor set layout for PBR textures
std::array<vk::DescriptorSetLayoutBinding, 5> bindings{
// Base color texture
vk::DescriptorSetLayoutBinding{
.binding = 0,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment
},
// Metallic-roughness texture
vk::DescriptorSetLayoutBinding{
.binding = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment
},
// Normal map
vk::DescriptorSetLayoutBinding{
.binding = 2,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment
},
// Occlusion map
vk::DescriptorSetLayoutBinding{
.binding = 3,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment
},
// Emissive map
vk::DescriptorSetLayoutBinding{
.binding = 4,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment
}
};
vk::DescriptorSetLayoutCreateInfo layoutInfo{
.bindingCount = static_cast<uint32_t>(bindings.size()),
.pBindings = bindings.data()
};
vk::raii::DescriptorSetLayout descriptorSetLayout(device, layoutInfo);
// For each material, create a descriptor set and update it with the material's textures
for (const auto& material : model.materials) {
// Allocate descriptor set from the descriptor pool
vk::DescriptorSetAllocateInfo allocInfo{
.descriptorPool = descriptorPool,
.descriptorSetCount = 1,
.pSetLayouts = &*descriptorSetLayout
};
vk::raii::DescriptorSet descriptorSet = std::move(vk::raii::DescriptorSets(device, allocInfo).front());
// Update descriptor set with texture image views and samplers
std::vector<vk::WriteDescriptorSet> descriptorWrites;
if (material.baseColorTexture) {
vk::DescriptorImageInfo imageInfo{
.sampler = material.baseColorTexture->sampler,
.imageView = material.baseColorTexture->imageView,
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal
};
vk::WriteDescriptorSet write{
.dstSet = *descriptorSet,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.pImageInfo = &imageInfo
};
descriptorWrites.push_back(write);
}
// Similar writes for other textures
// ...
device.updateDescriptorSets(descriptorWrites, {});
// Store the descriptor set with the material for later use during rendering
material.descriptorSet = *descriptorSet;
}