Vulkan:在vk*CreateInfo结构中,sType的意义是什么?

时间:2022-12-09 18:52:00

In all of the create info structs (vk*CreateInfo) in the new Vulkan API, there is ALWAYS a .sType member. Why is this there if the value can only be one thing? Also the Vulkan specification is very explicit that you can only use vk*CreateInfo structs as parameters for their corresponding vkCreate* function. It seems a little redundant. I can see that if the driver was passing this struct straight to the GPU, you might need to have it (I did notice it is always the first member). But this seems like a really bad idea for the app to do it because if the driver did it, apps would be much less error prone, and prepending an int to a struct doesn't seems like an extremely computational inefficient operation. I just don't see why it exists.

在新Vulkan API中的所有create info structs (vk*CreateInfo)中,始终都有一个. stype成员。如果值只能是一件事,为什么会出现这种情况?此外,Vulkan规范非常明确,您只能使用vk*CreateInfo结构作为相应的vkCreate*函数的参数。这似乎有点多余。我可以看到,如果司机将这个结构体直接传递给GPU,您可能需要它(我确实注意到它总是第一个成员)。但是对于应用来说,这似乎是一个非常糟糕的想法,因为如果驱动程序这么做,应用程序就不会那么容易出错,并且在struct前挂入int并不像一个计算效率极低的操作。我就是不明白它为什么存在。

TL;DR
    Why do the vk*CreateInfo structs have the .sType member?

为什么vk*CreateInfo结构具有.sType成员?

1 个解决方案

#1


22  

So that the API can be changed without breaking backwards compatibility.

这样可以在不破坏向后兼容性的情况下更改API。

If version 1.1 of Vulkan wants to expand on the creation of, for example, command buffer pools, how would it do that? Well, they could add a whole new entrypoint: vkCreateCommandPool2. But this function would have almost the exact same signature as vkCreateCommandPool; the only difference is that they take different pCreateInfo structures.

如果Vulkan的1.1版本想要扩展创建,例如命令缓冲池,它怎么做呢?他们可以添加一个全新的入口点:vkCreateCommandPool2。但是这个函数的签名与vkCreateCommandPool几乎完全相同;唯一的区别是它们采用不同的pCreateInfo结构。

So instead, all you have to do is declare a VkCommandPoolCreateInfo2 structure. And then declare that vkCreateCommandPool can take either one. How would the implementation tell which one you passed in?

因此,您只需声明一个VkCommandPoolCreateInfo2结构。然后声明vkCreateCommandPool可以取任何一个。实现如何告诉您输入的是哪个?

Because the first 4 bytes of any such structure is sType. They can test that value. If the value is VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, then it's the old structure. If it's VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO_2, then it's the new one.

因为任何这样的结构的前4个字节都是sType。他们可以测试这个值。如果值是VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,则是旧的结构。如果是VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO_2,那么它就是新的。

This also makes it easier for extensions to fully override a CreateInfo structure. The pNext field is for augmenting an API with additional parameters. With sType, an extension can change existing parameters.

这也使扩展更容易完全覆盖CreateInfo结构。pNext字段用于使用附加参数扩展API。使用sType,扩展可以更改现有的参数。

#1


22  

So that the API can be changed without breaking backwards compatibility.

这样可以在不破坏向后兼容性的情况下更改API。

If version 1.1 of Vulkan wants to expand on the creation of, for example, command buffer pools, how would it do that? Well, they could add a whole new entrypoint: vkCreateCommandPool2. But this function would have almost the exact same signature as vkCreateCommandPool; the only difference is that they take different pCreateInfo structures.

如果Vulkan的1.1版本想要扩展创建,例如命令缓冲池,它怎么做呢?他们可以添加一个全新的入口点:vkCreateCommandPool2。但是这个函数的签名与vkCreateCommandPool几乎完全相同;唯一的区别是它们采用不同的pCreateInfo结构。

So instead, all you have to do is declare a VkCommandPoolCreateInfo2 structure. And then declare that vkCreateCommandPool can take either one. How would the implementation tell which one you passed in?

因此,您只需声明一个VkCommandPoolCreateInfo2结构。然后声明vkCreateCommandPool可以取任何一个。实现如何告诉您输入的是哪个?

Because the first 4 bytes of any such structure is sType. They can test that value. If the value is VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, then it's the old structure. If it's VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO_2, then it's the new one.

因为任何这样的结构的前4个字节都是sType。他们可以测试这个值。如果值是VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,则是旧的结构。如果是VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO_2,那么它就是新的。

This also makes it easier for extensions to fully override a CreateInfo structure. The pNext field is for augmenting an API with additional parameters. With sType, an extension can change existing parameters.

这也使扩展更容易完全覆盖CreateInfo结构。pNext字段用于使用附加参数扩展API。使用sType,扩展可以更改现有的参数。