问题描述
我开始学习 Vulkan,想知道 VkCreate[...] 函数是否会将结构中指向的资源复制到自己的缓冲区中.
I'm starting to learn Vulkan, and want to know if VkCreate[...] functions copy the resources pointed in structs into his own buffers.
为了澄清我的问题,在这段代码中,我将一个 SPIR 着色器加载到我自己的 mkShader 结构中,然后我使用 vkCreateShaderModule
创建了着色器模块.
To clarify my question, in this code I load a SPIR shader into my own mkShader struct and then I create the shadermodule with vkCreateShaderModule
.
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 规范中的任何位置.
As you can see I'm freeing the mkShader struct with mkVulkanFreeShaderBinary
after shader module creation and I'm not receiving any error from my program. So my question is if this is safe to do, or I have to keep the mkShader struct until I destroy the shader module. And also, if this is valid to all VkCreate[...] functions or not, and if this information is anywhere in the Vulkan spec.
推荐答案
参见 Vulkan 规范的对象生命周期.
应用程序拥有的内存的所有权会立即被传入的任何 Vulkan 命令获取.必须在命令持续时间结束时将此类内存的所有权释放回应用程序,以便应用程序可以在获取它的所有命令返回后立即更改或释放此内存.
换句话说,只要 Vulkan 函数调用返回,您分配的任何内容都可以自由删除.此外,一旦您创建了管道,您也可以随意销毁 VkShaderModule
.
In other words, anything you allocate you are free to delete as soon as a Vulkan function call returns. Additionally, once you've created your pipeline, you're free to destroy the VkShaderModule
too.
这篇关于我什么时候可以释放传递给 vulkan vkCreateXXX 函数的资源和结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!