Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to set the VkDescriptorImageInfo to null or have some way of skipping using a VkWriteDescriptorSet without vulkan complaining

Some of the mesh that I'll be using doesn't always have a DiffuseMap or a SpecularMap. When I try to load something without a diffuse and specular map the program crashes because there's nothing in the DiffuseMap.ImageView/SpecularMap.ImageView because it isn't pointing to anything. If I try to set the imageview/sample to VK_NULL_HANDLE the program gives me this and crashes at the vkUpdateDescriptorSets:

Validation Layer: Invalid VkImageView Object 0x0. The Vulkan spec states: If descriptorType is VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, or VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, the imageView and imageLayout members of each element of pImageInfo must be a valid VkImageView and VkImageLayout, respectively (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-VkWriteDescriptorSet-descriptorType-00326)

Then if I just try set the binding to null, I get this:

Validation Layer: vkUpdateDescriptorSets: required parameter pDescriptorWrites[2].dstSet specified as VK_NULL_HANDLE

Validation Layer: Cannot call vkUpdateDescriptorSets() on VkDescriptorSet 0x0[] that has not been allocated.

This is what the base code looks like right now. This is the area that defines the descriptor sets to make it bit easier to see what's going on:

void Mesh::CreateDescriptorSets(VulkanRenderer& Renderer)
{
    BaseMesh::CreateDescriptorSets(Renderer, *GetDescriptorSetLayout(Renderer));

VkDescriptorImageInfo DiffuseMap = {};
DiffuseMap.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
DiffuseMap.imageView = TextureList[0].textureImageView;
DiffuseMap.sampler = TextureList[0].textureSampler;

VkDescriptorImageInfo SpecularMap = {};
SpecularMap.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
SpecularMap.imageView = TextureList[1].textureImageView;
SpecularMap.sampler = TextureList[1].textureSampler;

for (size_t i = 0; i < GetSwapChainImageCount(Renderer); i++)
{
    VkDescriptorBufferInfo PositionInfo = {};
    PositionInfo.buffer = uniformBuffers[i];
    PositionInfo.offset = 0;
    PositionInfo.range = sizeof(UniformBufferObject);

    VkDescriptorBufferInfo AmbiantLightInfo = {};
    AmbiantLightInfo.buffer = AmbientLightUniformBuffers[i];
    AmbiantLightInfo.offset = 0;
    AmbiantLightInfo.range = sizeof(AmbientLightUniformBuffer);

    VkDescriptorBufferInfo LightInfo = {};
    LightInfo.buffer = LighterUniformBuffers[i];
    LightInfo.offset = 0;
    LightInfo.range = sizeof(Lighter);

    std::array<WriteDescriptorSetInfo, 5>  WriteDescriptorInfo = {};

    WriteDescriptorInfo[0].DstBinding = 0;
    WriteDescriptorInfo[0].DstSet = descriptorSets[i];
    WriteDescriptorInfo[0].DescriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    WriteDescriptorInfo[0].DescriptorBufferInfo = PositionInfo;

    WriteDescriptorInfo[1].DstBinding = 1;
    WriteDescriptorInfo[1].DstSet = descriptorSets[i];
    WriteDescriptorInfo[1].DescriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    WriteDescriptorInfo[1].DescriptorImageInfo = DiffuseMap;

    WriteDescriptorInfo[2].DstBinding = 2;
    WriteDescriptorInfo[2].DstSet = descriptorSets[i];
    WriteDescriptorInfo[2].DescriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    WriteDescriptorInfo[2].DescriptorImageInfo = SpecularMap;

    WriteDescriptorInfo[3].DstBinding = 3;
    WriteDescriptorInfo[3].DstSet = descriptorSets[i];
    WriteDescriptorInfo[3].DescriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    WriteDescriptorInfo[3].DescriptorBufferInfo = AmbiantLightInfo;

    WriteDescriptorInfo[4].DstBinding = 4;
    WriteDescriptorInfo[4].DstSet = descriptorSets[i];
    WriteDescriptorInfo[4].DescriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    WriteDescriptorInfo[4].DescriptorBufferInfo = LightInfo;

    Mesh::CreateDescriptorSetsData(Renderer, std::vector<WriteDescriptorSetInfo>(WriteDescriptorInfo.begin(), WriteDescriptorInfo.end()));
}

}

like image 357
klj Avatar asked May 09 '20 00:05

klj


People also ask

What are descriptor sets Vulkan?

A VkDescriptorSet is a pack of those pointers that are bound together. Vulkan does not allow you to bind individual resources in shaders. They have to be grouped in the sets. If you still insist on being able to bind them individually, then you will need a descriptor set for each resource.

What is pool descriptor?

A descriptor pool maintains a pool of descriptors, from which descriptor sets are allocated. Descriptor pools are externally synchronized, meaning that the application must not allocate and/or free descriptor sets from the same pool in multiple threads simultaneously.


1 Answers

Until Vulkan 1.2, Vulkan did not recognize the possibility of a descriptor being "empty". When a descriptor set is created, the descriptors are (mostly) uninitialized. It's OK to have a set with an uninitialized descriptor, so long as the pipeline which consumes it does not statically use the descriptor. Since you are presumably trying to use the same pipeline for objects with diffuse maps and objects without them, your shader reads from the image based on a variable you provide. That represents static use of the descriptor, so you need an image there.

The typical way to deal with this is to create a tiny image of a reasonable format and stuff that into the descriptor. You can use the same image for essentially any "null" texture you want to use.

Vulkan 1.2, as part of the VK_EXT_descriptor_indexing extension promoted to core, allows for the possibility of partially bound descriptors. Basically, if the descriptorBindingPartiallyBound feature is available and requested, then you can allocate a descriptor set using the VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT bit. This means that it's OK to leave a descriptor undefined so long as it is not dynamically used.

So you simply wouldn't write a value for that descriptor.

Of course, this requires 1.2 (or the aforementioned extension), as well as requesting the feature.

like image 157
Nicol Bolas Avatar answered Sep 27 '22 17:09

Nicol Bolas