Introduction
sokol is a set of minimal, dependency-free C headers that provide cross-platform abstractions for graphics rendering, audio playback, input, and application lifecycle. Each header works standalone, following the STB single-file library philosophy.
What sokol Does
- sokol_gfx.h: 3D rendering API over Metal, D3D11, OpenGL 3.3, GLES3, and WebGPU
- sokol_app.h: application window, input events, and main loop on all desktop, mobile, and web targets
- sokol_audio.h: low-latency audio output via a push-model callback
- sokol_fetch.h: asynchronous file loading for native and web (WASM) environments
- sokol_gl.h: OpenGL 1.x style immediate-mode rendering layer on top of sokol_gfx
Architecture Overview
Each sokol header is a single C file with an implementation guard (SOKOL_IMPL). The graphics layer (sokol_gfx) uses a resource-pool model where applications create handles to buffers, images, shaders, and pipelines. Draw calls are recorded into render passes. The backend translates these to native API calls at minimal overhead, targeting zero allocations in the hot path.
Self-Hosting & Configuration
- Copy the needed .h files into your project; no build system required beyond a C compiler
- Define SOKOL_IMPL in exactly one .c file before including the headers
- Select backend with defines: SOKOL_METAL, SOKOL_D3D11, SOKOL_GLCORE, SOKOL_GLES3, or SOKOL_WGPU
- Shader compilation uses the companion sokol-shdc tool (GLSL to cross-compiled bytecode)
- Language bindings available for Zig, Odin, Nim, and Rust via code generation
Key Features
- Zero external dependencies; each header compiles in seconds with any C99 compiler
- First-class WebAssembly and Emscripten support for browser deployment
- Shader cross-compiler generates optimized backend-specific code from annotated GLSL
- Debug inspection layer validates API usage and reports errors at runtime
- Stable API maintained since 2018 with careful backwards compatibility
Comparison with Similar Tools
- bgfx — larger library with more backends and built-in tools; sokol is simpler to integrate
- SDL — broader scope (windowing, input, audio); sokol_gfx provides a modern GPU abstraction SDL lacks
- GLFW — window/input only; sokol_app covers similar ground plus integrates with sokol_gfx
- raylib — higher-level with built-in shapes and textures; sokol is lower-level and leaner
- wgpu-native — WebGPU-only; sokol supports legacy backends too
FAQ
Q: Can I use sokol in C++ projects? A: Yes. The headers are valid C99 and C++; they compile cleanly in both modes.
Q: How do I handle shaders? A: Write annotated GLSL, compile with sokol-shdc, and include the generated header containing bytecode for each backend.
Q: Is sokol suitable for commercial games? A: Yes. Its zlib license permits any use, and several shipped indie games use it.
Q: Does sokol support compute shaders? A: The WebGPU backend exposes compute; other backends currently focus on rasterization.