o
odinpkg.dev
packages / library / worlds_best_vulkan_triangle

worlds_best_vulkan_triangle

b3d86bdlibrary

The best* version of the classic Vulkan triangle example.

0BSD · updated 2 years ago

The World's Best Vulkan Triangle

Quick Start

git clone --recursive https://codeberg.org/aaaash/worlds_best_vulkan_triangle.git
cd worlds_best_vulkan_triangle
odin run . -debug # make sure you run in debug, debug mode compiles the shaders

Introduction

You've seen Vulkan triangles. The rainbow triangle is usually people's first introduction to Vulkan, and it can be a pretty daunting one. People are often taken aback by the amount of code it takes to draw a triangle. However, I'd argue that, especially for C/C++ code, a lot of these examples have a very low signal-to-noise ratio.

Odin puts us in a unique position to write a better triangle. The language's clean syntax makes low-level code much clearer. Odin zeroes struct fields by default, so the tedious work of setting all fields of a configuration or CreateInfo struct to zero is unnecessary. Finally, my Idiomatic Odin-Vulkan bindings library makes many minor improvements to the API. With these changes, I hope the triangle example can be made more clear, and that the code can get closer to the truth of low-level graphics.

This code is designed to be extended into a larger game or application. There are a couple changes to the typical triangle here that I've found useful in my game:

  • Pipeline Caching is implemented properly.
  • The codebase autogenerates VertexInputAttributeDescriptions using Odin's built-in reflection.
  • Swapchain items are structured in #soa format so they can be directly written to via the API.
  • Error-handling is done through a try proc group, which wraps vulkan functions quite nicely.
  • Shaders are compiled in debug mode, but in release mode, SPIR-V is statically included via #load.