11

A lot of the structures used in Vulkan have a sType member, which identifies the type of the structure, and a pNext member, for extension-specific structures. This answer explains quite well what the sType member is for, and why it is needed. It briefly touches on pNext, although I'm not sure I understand the rationale behind it.

If the first member of every structure is sType, couldn't extensions just define their own structure types when they need different/extra parameters?

Community
  • 1
  • 1
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62

1 Answers1

12

As clearly stated in the specification:

Any parameter that is a structure containing a void* pNext member must have a value of pNext that is either NULL, or points to a valid structure that is defined by an enabled extension.

It's for extensions.

If the first member of every structure is sType, couldn't extensions just define their own structure types when they need different/extra parameters?

That wouldn't be extensible.

There is only one sType field. So how could two extensions extend the same API with new values? Similarly, how could an old extension work alongside new versions of Vulkan that itself uses a different data structure identified by sType.

With pNext, you don't have that problem. Each extension data structure will not only have its own internal sType field, but it will no doubt also have its own pNext field. So multiple extensions can extend the same data structures.

sType doesn't need this, because it will only be changed in higher versions of Vulkan.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    Oh, so if every extension structure starts with sType and pNext, you can have a linked list of extension-specific data? – Andrew Williamson Apr 12 '16 at 04:01
  • 1
    @AndrewWilliamson: I thought the name `pNext` made that rather obvious. – Nicol Bolas Apr 12 '16 at 04:01
  • 3
    Yeah, every time I ask a question and you answer it, I feel a bit more like an idiot... – Andrew Williamson Apr 12 '16 at 04:02
  • 1
    @NicolBolas I'm ready to sound like a complete doofus, but I've always wondered what the `p` and the `pp` at the beginning of some struct memebers stood for in Vulkan. (I assume that the `s` here stands for struct/structure) – Jerfov2 Jul 10 '16 at 22:47
  • 2
    @OnaFriday p for pointer, and pp for pointer to pointer(s). You can add as many 'p's as you need for the corresponding levels of pointer indirection. – Andrew Williamson Jul 17 '16 at 07:25
  • 1
    @Jerfov2 This is [Hungarian notation](https://en.m.wikipedia.org/wiki/Hungarian_notation), a sort of inline type documentation used, for example, throughout the Win32 API. Whether it is a good idea is another question. – Alex Shpilkin Feb 26 '18 at 18:07