Thank you for this excellent argument against wrapper functions.
I have updated my code sample and learned a lot.
Here is the layout binding, effectively the same as yours (the descriptocount in the wrapper was always 1)
//descriptorSetLayouts.Material
{
// Descriptor set and pipeline layouts
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings;
// Set 1: Material data
setLayoutBindings.push_back(VkDescriptorSetLayoutBinding{
0, //binding;
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, //descriptorType;
1, //descriptorCount;
VK_SHADER_STAGE_FRAGMENT_BIT, //stageFlags;
0 //pImmutableSamplers;
});
// Set 1: Sampler
setLayoutBindings.push_back(VkDescriptorSetLayoutBinding{
1, //binding;
VK_DESCRIPTOR_TYPE_SAMPLER, //descriptorType;
1, //descriptorCount;
VK_SHADER_STAGE_FRAGMENT_BIT, //stageFlags;
0 //pImmutableSamplers;
});
// Set 1: Texture
setLayoutBindings.push_back(VkDescriptorSetLayoutBinding{
2, //binding;
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, //descriptorType;
static_cast<uint32_t>(materials.size()), //descriptorCount;
VK_SHADER_STAGE_FRAGMENT_BIT, //stageFlags;
0 //pImmutableSamplers;
});
const VkDescriptorSetLayoutCreateInfo descriptorLayout =
{
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, //sType;
nullptr, //pNext;
0, //flags;
static_cast<uint32_t>(setLayoutBindings.size()), //bindingCount;
setLayoutBindings.data(), //pBindings;
};
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.Material));
}
Which leaves the binding of descriptors to the set.
Something is still missing, since the textures are not sampled at all
// Material Descriptor set
{
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, //sType;
NULL, //pNext;
descriptorPool, //descriptorPool;
1, //descriptorSetCount;
&descriptorSetLayouts.Material //pSetLayouts;
};
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSetMaterial));
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
writeDescriptorSets.push_back(VkWriteDescriptorSet{
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, // sType;
NULL, // pNext;
descriptorSetMaterial, // dstSet;
0, // dstBinding;
0, // dstArrayElement;
1, // descriptorCount;
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // descriptorType; //VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
0, // pImageInfo;
&uniformBuffer.material.descriptor, // pBufferInfo;
0 // pTexelBufferView;
});
VkDescriptorImageInfo imageInfo{
materials[0].diffuse.sampler,
VK_NULL_HANDLE,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
};
writeDescriptorSets.push_back(VkWriteDescriptorSet{
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, // sType;
NULL, // pNext;
descriptorSetMaterial, // dstSet;
1, // dstBinding;
0, // dstArrayElement;
1, // descriptorCount;
VK_DESCRIPTOR_TYPE_SAMPLER, // descriptorType;
&imageInfo, // pImageInfo;
0, // pBufferInfo;
0 // pTexelBufferView;
});
// concat textures of all materials
for (int i = 0; i < materials.size(); i++)
{
VkDescriptorImageInfo imageInfo{
VK_NULL_HANDLE, //sampler;
materials[i].diffuse.view, //imageView;
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL //imageLayout;
};
writeDescriptorSets.push_back( VkWriteDescriptorSet{
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, // sType;
NULL, // pNext;
descriptorSetMaterial, // dstSet;
2, // dstBinding;
static_cast<uint32_t>(i), // dstArrayElement;
1, // descriptorCount;
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, // descriptorType; //VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,//VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
&imageInfo, // pImageInfo;
0, // pBufferInfo;
0 // pTexelBufferView;
});
}
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
}
Interesting though, the following code snippet is the sampling I intend to do, but color remains 0;
layout (set = 1, binding = 1) uniform sampler samplerColorMap;
layout (set = 1, binding = 2) uniform texture2D diffuseTextures[15];
...
vec4 color = texture(sampler2D(diffuseTextures[pushConsts.matID], samplerColorMap), inUV);
Yet, if I interpret the sampled image as combined, all(!) scene objects sample texture 0.
layout (set = 1, binding = 1) uniform sampler samplerColorMap;
layout (set = 1, binding = 2) uniform sampler2D diffuseTextures[15];
...
vec4 color = texture(diffuseTextures[pushConsts.matID], inUV);
Do you have some suggestion to my sample?
Best Regards