从`VkVertexInputBindingDescription`中绑定`的目的是什么?

Mai*_*ein 8 c++ vulkan

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

  • binding是此结构描述的绑定号.

我不确定这是什么意思,例如来自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);
Run Code Online (Sandbox Code Playgroud)

并且顶点着色器看起来像这样

#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);
}
Run Code Online (Sandbox Code Playgroud)

为什么是binding0?什么时候它的值不是0?目的是binding什么?

我的第一个想法是它可能是glsl https://www.opengl.org/wiki/Layout_Qualifier_(GLSL)#Binding_points中的特殊限定符.

但这似乎不适用于顶点输入限定符.

更新:

我想我已经弄清楚绑定的目的是什么

void vkCmdBindVertexBuffers(
    VkCommandBuffer                             commandBuffer,
    uint32_t                                    firstBinding,
    uint32_t                                    bindingCount,
    const VkBuffer*                             pBuffers,
    const VkDeviceSize*                         pOffsets);
Run Code Online (Sandbox Code Playgroud)

我假设您可以拥有一个支持状态的管道,但它仍然可以更改顶点输入布局,以便每个管道可以有不同的着色器.

然后binding它只是一个"动态"改变顶点布局的唯一标识符.

Nic*_*las 6

我想我已经弄清楚绑定的目的是什么

不,您还没有,但是您已经很接近了。

缓冲区绑定的含义与在OpenGL中使用单独的属性格式时的含义相同。有属性位置,并且有缓冲区绑定索引。每个属性位置都有一个格式和偏移量,用于定义如何解释其数据。但是它还必须有一种方法来说明它使用哪个缓冲区。

在Vulkan中,属性及其格式对于特定管道是不可变的。管道的属性格式是在创建时建立的,无法更改。但是,这些属性从中提取的缓冲区是可变状态。它们在管道创建时并不固定。

bindingpBuffers由所绑定的数组的索引vkCmdBindVertexBuffers。每个顶点属性都有一个binding,表示该属性从哪个缓冲区绑定索引获取其数据。但是缓冲区本身未在创建时指定。您可以使用进行动态设置vkCmdBindVertexBuffers

因此,binding属性binding值等于您提供的值glVertexAttribBinding