Daniel
Daniel

Reputation: 51

How update multiply descriptorSets at once

I have multiply render targets from my swapchain and create a descriptorSet per target. The idea ist to create one commandBuffer per renderTarget und bind the equivalent descriptor. Now I want to update the descriptorSets with one single call of updateDescriptorSets and used:

std::vector<vk::WriteDescriptorSet> writes;

for( UINT32 index = 0; index < descriptorSets.size(); ++index )
{
    writes.push_back( 
        vk::WriteDescriptorSet( descriptorSets[index], 0, 0, 1, 
                                vk::DescriptorType::eStorageImage,
                                &vk::DescriptorImageInfo( vk::Sampler(), imageViews[index], vk::ImageLayout::eGeneral ),
                                nullptr,
                                nullptr ) );
}

device.updateDescriptorSets( writes.size(), writes.data(), 0, nullptr );

With this approach only the last renderTarget in the queue presents the wanted result. The others produce just black screens. But when i call updateDescriptorSets multiply times all works like expected:

std::vector<vk::WriteDescriptorSet> writes;

for( UINT32 index = 0; index < descriptorSets.size(); ++index )
{
    writes.push_back( 
        vk::WriteDescriptorSet( descriptorSets[index], 0, 0, 1, 
                                vk::DescriptorType::eStorageImage,
                                &vk::DescriptorImageInfo( vk::Sampler(), imageViews[index], vk::ImageLayout::eGeneral ),
                                nullptr,
                                nullptr ) );
   device.updateDescriptorSets( writes.size(), writes.data(), 0, nullptr );
   writes.clear();
}

I thought that i can update multiply descriptorSets at once. So it is not possible or what else could be my error.

PS: I use the c++ Header frome the vulkan SDK

Upvotes: 2

Views: 158

Answers (1)

Daniel
Daniel

Reputation: 51

The Bug is indeed in the application code.

The problem lies in the creation of the vk::DescriptorImageInfo. In both code examples the struct only exits in the scope of the for-loop but than only a pointer to the struct is copied in the vk::WriteDescriptorSet struct.

When the updateDescriptorSets in the first example processes the data in the structs, the relevant data is out of scope and the pointers are invalid. Coincidentally the application uses always the same memory space in every iteration of the loop and so the pointers points to the same invalid space where the data of the last loop iteration still exits. This is why the last render target shows the expected result.

Upvotes: 2

Related Questions