Visual Studio 导致错误的优化

jgovgodb  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(107)
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);

我真的很难理解这个问题。如果我在Visual Studio中启用了优化,那么textureDescriptor.imageLayout行,可能还有textureDescriptor的其余部分,就会被优化掉,这会导致Vulkan中的错误。如果我注解掉它下面的printf,那就没有问题了。我怀疑编译器检测到imageLayout正在被使用,并且没有删除它。
我甚至需要优化吗?如果需要,我如何阻止它删除代码?

tktrz96b

tktrz96b1#

textureDescriptor并没有被“优化掉”。它是一个 * 堆栈变量 *,在你把它交给Vulkan之前,它的生存期就结束了。
你必须以某种方式来创建这些对象,这种方式要比创建它们的块更有效,它需要调用vkUpdateDescriptorSets

相关问题