Home > OS >  Optimizations causing errors
Optimizations causing errors

Time:10-25

std::vector<VkWriteDescriptorSet> writeDescriptorSets;

    for (int index = 0; index < descriptorBindings.size(); index  )
    {
        VkWriteDescriptorSet writeDescriptorSet = {};

        // Binding 0 : Uniform buffer
        writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
        writeDescriptorSet.dstSet = descriptorSet;
        // Binds this uniform buffer to binding point 0
        writeDescriptorSet.dstBinding = index;
        writeDescriptorSet.descriptorCount = descriptorBindings[index].Count;
        writeDescriptorSet.pNext = nullptr;
        writeDescriptorSet.pTexelBufferView = nullptr;

        if (descriptorBindings[index].Type == DescriptorType::UniformBuffer)
        {
            VkDescriptorBufferInfo uniformBufferDescriptor = {};
            uniformBufferDescriptor.buffer = descriptorBindings[index].UniformBuffer->buffer;
            uniformBufferDescriptor.offset = 0;
            uniformBufferDescriptor.range = descriptorBindings[index].UniformBuffer->size;

            writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
            writeDescriptorSet.pBufferInfo = &uniformBufferDescriptor;
        }
        else if (descriptorBindings[index].Type == DescriptorType::TextureSampler)
        {
            VkDescriptorImageInfo textureDescriptor = {};
            textureDescriptor.imageView = descriptorBindings[index].Texture->imageView->imageView;              // The image's view (images are never directly accessed by the shader, but rather through views defining subresources)
            textureDescriptor.sampler = descriptorBindings[index].Texture->sampler;         // The sampler (Telling the pipeline how to sample the texture, including repeat, border, etc.)
            textureDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;   // The current layout of the image (Note: Should always fit the actual use, e.g. shader read)
            //printf("%d\n", textureDescriptor.imageLayout);
            writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
            writeDescriptorSet.pImageInfo = &textureDescriptor;
        }

        writeDescriptorSets.push_back(writeDescriptorSet);
    }

    vkUpdateDescriptorSets(logicalDevice, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);

I am really scratching my head over this. If I enabled optimizations inside Visual Studio then the textureDescriptor.imageLayout line, and probably the rest of the textureDescriptor, gets optimized out and it causes errors in Vulkan. If I comment out the printf below it then no problem. I suspect that the compiler detects that imageLayout is being used and doesn't get rid of it.

Do I even need optimizations? If so how can I prevent it from removing that code?

CodePudding user response:

textureDescriptor is not being "optimized out". It's a stack variable whose lifetime ended before you ever give it to Vulkan.

You're going to have to create those objects in some kind of way that will outlive the block in which they were created. It needs to the call to vkUpdateDescriptorSets.

  • Related