Skip to content

permutationlock/vulkan_hello_triangle_c99

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A basic Vulkan application in C99 (or later)

A C implementation of a simple rotating anti-aliased square based on the Vulkan Tutorial. There are a few significant changes from the tutorial:

  • Vulkan 1.3 direct rendering is used in lieu of render pass objects;
  • the volk dynamic loader is used in lieu of directly linking the libvulkan.so shared library.

The coding style and project organization was inpsired by Zig, Chris Wellons' blog, and raylib.

Requirements

To build the project you will need a C compiler that supports C99 or later, as well as a GLSL to SPIR-V compiler. The project supports Linux and Windows targets. There are ZERO build dependencies beyond a libc for your target.

The project vendors and builds volk, a slightly modified version of GLFW, and optionally winpthreads. Also included are headers for Vulkan, xkbcommon, X11, Wayland, and the Wayland protocols.

To run the application you will need the Vulkan loader and a Vulkan driver for your graphics card. To run executables built with ENABLE_VALIDATION_LAYERS defined (which is the case in the default CFLAGS) you will need to have the Vulkan validation layers installed as well.

To run on Linux you will also need either an X11 server or a Wayland compositor, along with the corresponding shared libraries.

Building

The project builds with a single simple Makefile.

Building on Linux

By default make will build a debug app with gcc as the C compiler and glslc as the shader compiler.

make -j5
./vulkan_app

To use a different compiler you can modify the appropriate environment variable.

make CC="clang" -j5
./vulkan_app

With a little define hack to wrangle the glibc headers, you can even build an application using the awesome and simple cproc compiler1.

make CC="cproc" CFLAGS="-D\"__attribute__(x)=\"" \
    GLFW_FLAGS="-D\"__attribute__(x)=\"" -j5
./vulkan_app

You can easily cross-compile a Windows application from Linux with the Mingw-w64 toolchain.

make CC="x86_64-w64-mingw32-gcc" CFLAGS="-std=c99 -O2" LDFLAGS="-mwindows" \
    GLFW_CFLAGS="-std=c99 -O2 -DNDEBUG" \
    SHADERFLAGS="--target-env=vulkan1.3 -O" -j5

You can also cross-compile a Windows application with zig cc (or any clang toolchain with a Windows Mingw target).

make LOCALWINPTHREADS="YES" CC="zig cc -target x86_64-windows-gnu" \
    LDFLAGS="-lkernel32 -luser32 -lgdi32 -Wl,--subsystem,windows"

Building on Windows

On a Windows machine you can compile an application with Mingw-w64.

make CFLAGS="-std=c99 -O2" GLFW_CFLAGS="-std=c99 -O2 -DNDEBUG" \
    LDFLAGS="-mwindows" SHADERFLAGS="--target-env=vulkan1.3 -O" -j5
./vulkan_app.exe

You can also build a Windows app with Zig.

make LOCALWINPTHREADS="YES" CC="zig cc" \
    LDLFAGS="-lkernel32 -luser32 -lgdi32 -Wl,--subsystem,windows"
mv vulkan_app vulkan_app.exe
./vulkan_app.exe

You can also cross-compile a Linux application from Windows.

make CC="zig cc -target x86_64-linux-gnu" CFLAGS="-std=c99 -O2" \
    GLFW_CFLAGS="-std=c99 -O2 -DNDEBUG" \
    SHADERFLAGS="--target-env=vulkan1.3 -O" -j5

Footnotes

  1. A small patch to QBE is required to increase the maximum identifier length.