Maik Klein Maik Klein - 1 month ago 9
C++ Question

What is the purpose of `binding` from `VkVertexInputBindingDescription`?

https://www.khronos.org/registry/vulkan/specs/1.0/man/html/VkVertexInputBindingDescription.html



  • binding is the binding number that this structure describes.




I am not sure what this means for example from https://github.com/SaschaWillems/Vulkan/blob/master/triangle/triangle.cpp

#define VERTEX_BUFFER_BIND_ID 0
....
vertices.inputAttributes[0].binding = VERTEX_BUFFER_BIND_ID;
vertices.inputAttributes[0].location = 0;
vertices.inputAttributes[0].format = VK_FORMAT_R32G32B32_SFLOAT;
vertices.inputAttributes[0].offset = offsetof(Vertex, position);
// Attribute location 1: Color
vertices.inputAttributes[1].binding = VERTEX_BUFFER_BIND_ID;
vertices.inputAttributes[1].location = 1;
vertices.inputAttributes[1].format = VK_FORMAT_R32G32B32_SFLOAT;
vertices.inputAttributes[1].offset = offsetof(Vertex, color);


and the vertex shader looks like this

#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inColor;

layout (binding = 0) uniform UBO
{
mat4 projectionMatrix;
mat4 modelMatrix;
mat4 viewMatrix;
} ubo;

layout (location = 0) out vec3 outColor;

out gl_PerVertex
{
vec4 gl_Position;
};


void main()
{
outColor = inColor;
gl_Position = ubo.projectionMatrix * ubo.viewMatrix * ubo.modelMatrix * vec4(inPos.xyz, 1.0);
}


Why is the
binding
0? When will it have a different value than 0? What is the purpose of
binding
?

My first idea was that it might have been a special qualifier in glsl https://www.opengl.org/wiki/Layout_Qualifier_(GLSL)#Binding_points .

But this doesn't seem to work for the vertex input qualifiers.

Update:

I think I have figured out what the purpose of binding is

void vkCmdBindVertexBuffers(
VkCommandBuffer commandBuffer,
uint32_t firstBinding,
uint32_t bindingCount,
const VkBuffer* pBuffers,
const VkDeviceSize* pOffsets);


I assume that you can have a pipeline with some backed state but it can still change the vertex input layout so that you can have different shaders per pipeline.

And then the
binding
is just a unique identifier to change the vertex layout "on the fly".

Answer

I think I have figured out what the purpose of binding is

No, you haven't, but you are close to it.

The buffer binding has the same meaning as it does when using separate attribute formats in OpenGL. There are attribute locations and there are buffer binding indices. Each attribute location has a format and offset that defines how to interpret its data. But it also has to have a way to say which buffer it uses.

In Vulkan, attributes and their formats are immutable for a particular pipeline. Attribute formats for a pipeline are established at creation time and cannot be changed. However, the buffers those attributes pull from are mutable state. They are not fixed at pipeline creation time.

binding is an index into the pBuffers array bound by vkCmdBindVertexBuffers. Each vertex attribute has a binding that says which buffer binding index the attribute gets its data from. But the buffer itself is not specified at creation time. You set it dynamically with vkCmdBindVertexBuffers.

So the binding value for an attribute is equivalent to the binding value you provide to glVertexAttribBinding.