我什么时候可以释放传递给 vulkan vkCreateXXX 函数的资源和结构?

When can I free resources and structures passed to a vulkan vkCreateXXX function?

我开始学习Vulkan,想知道VkCreate[...]函数是否将structs中指向的资源复制到他自己的缓冲区中。

为了澄清我的问题,在这段代码中,我将 SPIR 着色器加载到我自己的 mkShader 结构中,然后使用 vkCreateShaderModule.[=13= 创建着色器模块]

static VkShaderModule mkVulkanCreateShaderModule(MkVulkanContext *vc,
                     const char *filename)
{
    VkShaderModule shaderModule;
    struct mkShader *shader = mkVulkanLoadShaderBinary(filename);
    VkShaderModuleCreateInfo createInfo = {0};
    createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
    createInfo.codeSize = shader->size;
    createInfo.pCode = (uint32_t *)shader->buffer;

    if (vkCreateShaderModule(vc->device, &createInfo, NULL,
                 &shaderModule) != VK_SUCCESS) {
        printf(ANSI_COLOR_RED
               "Failed to create shader module\n" ANSI_COLOR_RESET);
        assert(0);
        exit(EXIT_FAILURE);
    }
    mkVulkanFreeShaderBinary(shader);
    return shaderModule;
}

如您所见,我在创建着色器模块后使用 mkVulkanFreeShaderBinary 释放了 mkShader 结构,并且我的程序没有收到任何错误。所以我的问题是这样做是否安全,或者我必须保留 mkShader 结构直到我销毁着色器模块。而且,这是否对所有 VkCreate[...] 函数都有效,以及该信息是否在 Vulkan 规范中的任何地方。

请参阅 Vulkan 规范的 Object Lifetime

The ownership of application-owned memory is immediately acquired by any Vulkan command it is passed into. Ownership of such memory must be released back to the application at the end of the duration of the command, so that the application can alter or free this memory as soon as all the commands that acquired it have returned.

换句话说,一旦 Vulkan 函数调用 returns,您分配的任何内容都可以自由删除。此外,创建管道后,您也可以自由销毁 VkShaderModule