【问题标题】:What is the purpose of `binding` from `VkVertexInputBindingDescription`?`VkVertexInputBindingDescription` 中的`binding` 的目的是什么?
【发布时间】:2017-03-19 22:05:52
【问题描述】:

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);

顶点着色器看起来像这样

#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);
}

为什么binding 0?什么时候它的值会不同于 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);

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

然后binding 只是一个唯一标识符,用于“动态”更改顶点布局。

【问题讨论】:

    标签: c++ vulkan


    【解决方案1】:

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

    不,你没有,但你已经接近它了。

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

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

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

    所以属性的binding 值为equivalent to the binding value you provide to glVertexAttribBinding.

    【讨论】:

    • 如果顶点数据没有在vkCmdBindVertexBuffers 调用中交错,是否打算多次使用相同的VkBuffer 和不同的偏移量?谢谢。
    • @ale64bit:如果您的顶点数据没有交错但确实存在于同一个VkBuffer 中,那么是的,该缓冲区将在您的vkCmdBindVertexBuffers 调用中出现多次。
    猜你喜欢
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2010-11-07
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多