Initialising a Vertex Buffer

A vertex buffer stores vertex array data

The vertex shader previously created has two vertex attributes as input: a four element vector for the vertex's position and a two element vector for the vertex's texture coordinates. These are vertex attributes and represent the properties of the vertex which the shader can process. The application needs to create a vertex buffer to store the vertex data, which will be fed into the shader's vertex attribute variables.

What is a Vertex Buffer?

A vertex buffer is a reserved section of memory which stores vertex array data. In this simple vertex buffer, each element has two sets of co-ordinates:

  • a set of 3D homogenous position co-ordinates: (x, y, z, w). These determine where the vertex is located in 3D space.
  • a set of texture co-ordinates: (u, v). These tell the fragment shader where to sample the texture in order to determine how to colour all of the fragments within the primitive.

These sets of co-ordinates provide all of the required information about a vertex.

Example: initVertexBuffers()

In the guide example, the vertex data is tiny, just the three vertices of the triangle. The initial vertex data is hardcoded but in more complex applications it would be loaded in from a 3D model file.

The actual memory object is created using a helper function called createBuffer().

The createBuffer function will perform the following steps:

  1. Create a VkBuffer object
  2. Allocate a portion of memory which matches the buffer size requirements
  3. Map the allocated memory
  4. Copy the data into the allocated memory
  5. Bind the buffer object to this memory.