mzm.sh

First Triangle

The first triangle is a rite of passage.

In OpenGL it takes maybe 30 lines. In Vulkan it takes closer to 400: a window, a surface, a physical device, a logical device, a swap chain, image views, a render pass, a pipeline layout, a graphics pipeline, framebuffers, a command pool, command buffers, semaphores, fences — and then, finally, three vertices and a draw call.

Each of those 400 lines is a lesson.

The pipeline

Vulkan's graphics pipeline is fixed at creation time. You specify the vertex input layout, the primitive topology, the viewport, the rasterizer settings, multisampling, depth/stencil state, color blending, and the shader stages — all at once, all upfront. No changing blend modes mid-frame without creating a new pipeline object.

This is verbose. It is also honest: the GPU needs to know all of this before it can compile the pipeline into hardware-specific instructions.

// vertex shader
#version 450

vec2 positions[3] = vec2[](
    vec2( 0.0, -0.5),
    vec2( 0.5,  0.5),
    vec2(-0.5,  0.5)
);

void main() {
    gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
}

The fragment shader returns red. That's it. That's the milestone.