I'm creating a 2D game engine using Vulkan.
I've been looking at how to draw different textures (each GameObject can contain its own texture and can be different from others). In OpenGL you call glBindTexture and in vulkan I have seen that there are people who say that you can create a descriptor for each texture and call vkCmdBindDescriptorSets for each. But I have read that doing this has a high cost.
The way I'm doing it is to use only 1 descriptor for the Sampler2D and use a VkDescriptorImageInfo vector where I add each VkDescriptorImageInfo for each texture and assign the vector in pImageInfo.
VkWriteDescriptorSet samplerDescriptorSet;
samplerDescriptorSet.pNext = NULL;
samplerDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
samplerDescriptorSet.dstSet = descriptorSets[i];
samplerDescriptorSet.dstBinding = 1;
samplerDescriptorSet.dstArrayElement = 0;
samplerDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
samplerDescriptorSet.descriptorCount = static_cast<uint32_t>(samplerDescriptors.size());
samplerDescriptorSet.pImageInfo = samplerDescriptors.data();
//samplerDescriptors is the vector
Using this, I can skip creating and binding a descriptor for each texture but now I need an array of Samplers in fragment shader. I can't use sampler2DArray because each texture have different sizes so I decided to use an array of Samplers2D (Sampler2D textures[n]). The problem with this is that I don't want to set a max number of textures to use.
I found a way to do it dynamically using:
#extension GL_EXT_nonuniform_qualifier : enable
layout(binding = 1) uniform sampler2D texSampler[];
I never used this before and don't know if is efficient or not. Anyways there is still a problem with this. Now I need to set the number of descriptor count when I create the descriptor layout and again, I don't want to set a max number you can use:
VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
samplerLayoutBinding.binding = 1;
samplerLayoutBinding.descriptorCount = 999999; <<<< HERE
samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
samplerLayoutBinding.pImmutableSamplers = nullptr;
samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
Having said that. How can I solve this? Or which is the correct way to do this efficiently?
If you need more information, just ask.
Thanks in advance!