## 20.2. Vertex Input Description

Applications specify vertex input attribute and vertex input binding descriptions as part of graphics pipeline creation. The `VkGraphicsPipelineCreateInfo`::`pVertexInputState` points to a structure of type `VkPipelineVertexInputStateCreateInfo`.

The `VkPipelineVertexInputStateCreateInfo` structure is defined as:

    typedef struct VkPipelineVertexInputStateCreateInfo {
        VkStructureType                             sType;
        const void*                                 pNext;
        VkPipelineVertexInputStateCreateFlags       flags;
        uint32_t                                    vertexBindingDescriptionCount;
        const VkVertexInputBindingDescription*      pVertexBindingDescriptions;
        uint32_t                                    vertexAttributeDescriptionCount;
        const VkVertexInputAttributeDescription*    pVertexAttributeDescriptions;
    } VkPipelineVertexInputStateCreateInfo;

- `sType` is the type of this structure.
- `pNext` is `NULL` or a pointer to an extension-specific structure.
- `flags` is reserved for future use.
- `vertexBindingDescriptionCount` is the number of vertex binding descriptions provided in `pVertexBindingDescriptions`.
- `pVertexBindingDescriptions` is a pointer to an array of `VkVertexInputBindingDescription` structures.
- `vertexAttributeDescriptionCount` is the number of vertex attribute descriptions provided in `pVertexAttributeDescriptions`.
- `pVertexAttributeDescriptions` is a pointer to an array of `VkVertexInputAttributeDescription` structures.

Each vertex input binding is specified by an instance of the `VkVertexInputBindingDescription` structure.

The `VkVertexInputBindingDescription` structure is defined as:

    typedef struct VkVertexInputBindingDescription {
        uint32_t             binding;
        uint32_t             stride;
        VkVertexInputRate    inputRate;
    } VkVertexInputBindingDescription;

- `binding` is the binding number that this structure describes.
- `stride` is the distance in bytes between two consecutive elements within the buffer.

- `inputRate` specifies whether vertex attribute addressing is a function of the vertex index or of the instance index. Possible values include:

      typedef enum VkVertexInputRate {
          VK_VERTEX_INPUT_RATE_VERTEX = 0,
          VK_VERTEX_INPUT_RATE_INSTANCE = 1,
      } VkVertexInputRate;

  - `VK_VERTEX_INPUT_RATE_VERTEX` indicates that vertex attribute addressing is a function of the vertex index.
  - `VK_VERTEX_INPUT_RATE_INSTANCE` indicates that vertex attribute addressing is a function of the instance index.

Each vertex input attribute is specified by an instance of the `VkVertexInputAttributeDescription` structure.

The `VkVertexInputAttributeDescription` structure is defined as:

    typedef struct VkVertexInputAttributeDescription {
        uint32_t    location;
        uint32_t    binding;
        VkFormat    format;
        uint32_t    offset;
    } VkVertexInputAttributeDescription;

- `location` is the shader binding location number for this attribute.
- `binding` is the binding number which this attribute takes its data from.
- `format` is the size and type of the vertex attribute data.
- `offset` is a byte offset of this attribute relative to the start of an element in the vertex input binding.

To bind vertex buffers to a command buffer for use in subsequent draw commands, call:

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

- `commandBuffer` is the command buffer into which the command is recorded.
- `firstBinding` is the index of the first vertex input binding whose state is updated by the command.
- `bindingCount` is the number of vertex input bindings whose state is updated by the command.
- `pBuffers` is a pointer to an array of buffer handles.
- `pOffsets` is a pointer to an array of buffer offsets.

The values taken from elements i of `pBuffers` and `pOffsets` replace the current state for the vertex input binding `firstBinding` + i, for i in [0, `bindingCount`). The vertex input binding is updated to start at the offset indicated by `pOffsets`[i] from the start of the buffer `pBuffers`[i]. All vertex input attributes that use each of these bindings will use these updated addresses in their address calculations for subsequent draw commands.

The address of each attribute for each `vertexIndex` and `instanceIndex` is calculated as follows:

- Let attribDesc be the member of `VkPipelineVertexInputStateCreateInfo`::`pVertexAttributeDescriptions` with `VkVertexInputAttributeDescription`::`location` equal to the vertex input attribute number.
- Let bindingDesc be the member of `VkPipelineVertexInputStateCreateInfo`::`pVertexBindingDescriptions` with `VkVertexInputAttributeDescription`::`binding` equal to attribDesc.binding.
- Let `vertexIndex` be the index of the vertex within the draw (a value between `firstVertex` and `firstVertex`+`vertexCount` for `vkCmdDraw`, or a value taken from the index buffer for `vkCmdDrawIndexed`), and let `instanceIndex` be the instance number of the draw (a value between `firstInstance` and `firstInstance`+`instanceCount`).

    bufferBindingAddress = buffer[binding].baseAddress + offset[binding];

    if (bindingDesc.inputRate == VK_VERTEX_INPUT_RATE_VERTEX)
        vertexOffset = vertexIndex * bindingDesc.stride;
    else
        vertexOffset = instanceIndex * bindingDesc.stride;

    attribAddress = bufferBindingAddress + vertexOffset + attribDesc.offset;

For each attribute, raw data is extracted starting at `attribAddress` and is converted from the `VkVertexInputAttributeDescription`’s `format` to either to floating-point, unsigned integer, or signed integer based on the base type of the format; the base type of the format must match the base type of the input variable in the shader. If `format` is a packed format, `attribAddress` must be a multiple of the size in bytes of the whole attribute data type as described in Packed Formats. Otherwise, `attribAddress` must be a multiple of the size in bytes of the component type indicated by `format` (see Formats). If the format does not include G, B, or A components, then those are filled with (0,0,1) as needed (using either 1.0f or integer 1 based on the format) for attributes that are not 64-bit data types. The number of components in the vertex shader input variable need not exactly match the number of components in the format. If the vertex shader has fewer components, the extra components are discarded.