Gary Allen
Gary Allen

Reputation: 1380

How does the pipeline know which descriptor layout to use when binding descriptors?

When calling vkCmdBindDescriptorSets, I have to pass the number of the first set and an array of descriptor sets that I would like bound. I then get to use whichever set I like in my shader using layout(set = X, binding = 0).

My question is the following. The descriptor set layout for the set was only specified at descriptor set creation. Yet when I bind, I can bind any descriptor set to any set number using the above function. Is it up to my to keep my shader layout and binding consistent with the layout specified amongst pipeline creation? Otherwise, how does the pipeline/shader "know" which layout my specific set is using?

Upvotes: 2

Views: 850

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474226

In Vulkan, unless otherwise noted, it's always "up to you". This is no exception.

If you attempt to render/dispatch with a pipeline and bound descriptor sets that do not have matching layouts, undefined behavior results.

The pipeline "knows" which layout you're using by fiat. The whole point of a layout is that it "lays out" the arrangement of the internal data representing how those descriptors are organized. So where "binding 2" is within whatever internal data structure the implementation uses for defining that is determined solely by the layout.

A layout is therefore kind of like a struct in C or C++. You can't pass a pointer to a struct of type B to a function that expects a pointer to a struct of type A. Well, you can if you do a bunch of casts, but when the function accesses that pointer, undefined behavior results.

The same goes for pipelines and bound descriptor sets: they must use compatible layouts, or undefined behavior results.

Upvotes: 4

Related Questions