Rendering a Triangle (Metal Part 2)
Rendering a primitive triangle using the GPU, a vertices array, and a memory buffer. Writing a vertex function to access the individual vertices and passing them down to the fragment stage shader.

Source Code
References
Table of Content
Vertex Buffer
Rendering Primitives MTL Buffer
Define an array of vectors representing the vertices we want to draw in the screen.
// counter clock wise to define the face
var vertices: [float3] = [
float3(0, 1, 0), // top mid
float3(-1, -1, 0), // bot left
float3(1, -1, 0), // top right
]
Declare the memory buffer which will be used to hold the vertices information in the GPU.
var vertexBuffer: MTLBuffer!;
Initialize the buffer by creating it off of the device reference.
The content is going to be the vertices array, the total memory amount is going to be the stride of an individual vertex times the amount of vertices.
func createBuffers() {
let vertexMemSize = MemoryLayout<float3>.stride
vertexBuffer = device?.makeBuffer(bytes: vertices, length: vertexMemSize * vertices.count, options: [])
}
Vertex Shader
Using attributes for arguments Metal-Shading-Language-Specification
We need to lookup vertex data in the buffer manually, by using the attribute [[buffer(0)]].
The attribute [[vertex_id]] allows us to get the vertex index in the buffer.
vertex float4 basic_vertex_shader(
device float3 *vertices [[ buffer(0) ]], // access the vertices buffer at buffer with index 0
uint vertexID [[ vertex_id ]] // get the vertex id, which corresponds to the index of the vertex in the buffer
){
return float4(vertices[vertexID], 1); // return the vertex position in homogeneous screen space
}
Draw Primitives
We set the buffer to the encoder, so that the GPU can use it when executing the vertex function.
Drawing the primitives needs a specific primitive type, where we start to consider vertices, and how many vertices we will draw.
// send info to render command encoder
renderCommandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
// draw a triangle using the vertices in the buffer
renderCommandEncoder?.drawPrimitives(type: MTLPrimitiveType.triangle, vertexStart: 0, vertexCount: vertices.count)
Result

