Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple Vulkan buffer binding points
Shader
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inPosB;
layout (location = 3) in vec3 inColorB;
...
Application
// Input state
std::array<VkVertexInputBindingDescription,2> vertexInputBinding = {};
vertexInputBinding[0].binding = 0;
vertexInputBinding[0].stride = sizeof(Vertex);
vertexInputBinding[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
vertexInputBinding[1].binding = 1;
vertexInputBinding[1].stride = sizeof(Vertex);
vertexInputBinding[1].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
// Inpute attribute bindings describe shader attribute locations and memory layouts
std::array<VkVertexInputAttributeDescription, 4> vertexInputAttributs;
vertexInputAttributs[0].binding = 0;
vertexInputAttributs[0].location = 0;
vertexInputAttributs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
vertexInputAttributs[0].offset = offsetof(Vertex, position);
vertexInputAttributs[1].binding = 0;
vertexInputAttributs[1].location = 1;
vertexInputAttributs[1].format = VK_FORMAT_R32G32B32_SFLOAT;
vertexInputAttributs[1].offset = offsetof(Vertex, color);
vertexInputAttributs[2].binding = 1;
vertexInputAttributs[2].location = 2;
vertexInputAttributs[2].format = VK_FORMAT_R32G32B32_SFLOAT;
vertexInputAttributs[2].offset = offsetof(Vertex, position);
vertexInputAttributs[3].binding = 1;
vertexInputAttributs[3].location = 3;
vertexInputAttributs[3].format = VK_FORMAT_R32G32B32_SFLOAT;
vertexInputAttributs[3].offset = offsetof(Vertex, color);
// Vertex input state used for pipeline creation
VkPipelineVertexInputStateCreateInfo vertexInputState = {};
vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputState.vertexBindingDescriptionCount = 2;
vertexInputState.pVertexBindingDescriptions = vertexInputBinding.data();
vertexInputState.vertexAttributeDescriptionCount = 4;
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributs.data();
// Bind
vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &vertices.buffer, offsets);
vkCmdBindVertexBuffers(drawCmdBuffers[i], 1, 1, &verticesB.buffer, offsets);
@0xc0dec
Copy link

0xc0dec commented Jul 20, 2017

Many thanks! I was mistakenly assuming that attribute locations are local to each binding similar to sets and bindings for descriptor sets. So in the vertex shader I expected something like this

layout(binding = 0, location = 0) in vec3 position;
layout(binding = 1, location = 0) in vec2 texCoord;

because for descriptor sets we do this

layout (set = 0, binding = 0) uniform ViewMatrices
{
    mat4 projection;
    mat4 view;
} viewMatrices;

layout (set = 1, binding = 0) uniform ModelMatrix 
{
    mat4 model;
} modelMatrix;

Turns out attribute locations are somewhat "global" and I guess the driver just knows what bindings they belong to.

@lriki
Copy link

lriki commented Mar 22, 2019

good solution!

@zhhm156
Copy link

zhhm156 commented May 28, 2019

why not put both of vertex datas into the same buffer?It seems that they will be handled within the same shader?

@FunMiles
Copy link

FunMiles commented Aug 22, 2019

why not put both of vertex datas into the same buffer?It seems that they will be handled within the same shader?

Because there are usages where you might want to modify one of the attributes but not the other? For example, texture map coordinates. You could have a texture 'slide' across an object.
For that, though the 3D coordinates and the normals of the vertices would not change, you would need to change the texture coordinates. Probably cheaper to have two separate buffers, one with coordinates and normals and the other with the texture coordinates. It's less work than re-building a full interleaved data and copying a larger buffer from your CPU to your GPU.

@Lucodivo
Copy link

Lucodivo commented Oct 18, 2020

why not put both of vertex datas into the same buffer?It seems that they will be handled within the same shader?

I am under the impression that this is an example that teaches us how to properly use vertex input attributes and not an example that is meant to demonstrate optimal usage of buffers. I believe the you are right and using the same buffer would have been a great idea. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment