pNext和sType用法
刚接触 Vulkan 的你可能会注意到 Vulkan Spec 中无处不在的pNext
和sType
变量。 pNext
主要用于扩展功能的衍生结构体。void* pNext
在结构体之间创建链接,loader、layer和驱动使用VkStructureType sType
来获知传入的pNext
结构体类型。
两种基本结构体
Vulkan API 提供了两种基本结构:VkBaseInStructure
和VkBaseOutStructure
,用来遍历结构指针链表。
对loader、layer和驱动程序来说,VkBaseInStructure
的In
指pNext
是const *
只读的,VkBaseOutStructure
的Out
指pNext
是用来将数据返回给应用程序。
设置pNext示例
// An example with two simple structures, "a" and "b"
typedef struct VkA {
VkStructureType sType;
void* pNext;
uint32_t value;
} VkA;
typedef struct VkB {
VkStructureType sType;
void* pNext;
uint32_t value;
} VkB;
// A Vulkan Function that takes struct "a" as an argument
// This function is in charge of populating the values
void vkGetValue(VkA* pA);
// Define "a" and "b" and set their sType
struct VkB b = {};
b.sType = VK_STRUCTURE_TYPE_B;
struct VkA a = {};
a.sType = VK_STRUCTURE_TYPE_A;
// Set the pNext pointer from "a" to "b"
a.pNext = (void*)&b;
// Pass "a" to the function
vkGetValue(&a);
// Use the values which were both set from vkGetValue()
printf("VkA value = %u \n", a.value);
printf("VkB value = %u \n", b.value);