Skip to content

About

Raylib Agentic Optimization

Resources

Stars

19 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

raylib_aop — Agentically Optimized raylib backends

Drop-in Vulkan, Metal, and Direct3D 12 backends for raylib — developed and validated agentically (image-equivalence gated against the stock OpenGL backend, performance-campaigned per platform).

This repo is an optional add-on that lives entirely outside the raylib tree. It builds raylib with one of the optimized backends without editing a single file in your raylib checkout: sources are staged and patched inside raylib_aop/build/, compiled there, and only the finished libraylib.a is copied into raylib/src/ — the location every raylib example and consumer already expects. Your raylib clone stays pristine and fully revertible.

Each backend is a single-header sibling of rlgl.h implementing the exact same rlgl API, so every raylib program and stock GLSL shader works unmodified:

Backend Header Platform Status
Vulkan 1.3+ src/rlvk.h Windows / Linux / macOS (MoltenVK or KosmicKrisp) Image gate green vs GL on all three OSes; beats GL on most scenes (up to 16.4× NVIDIA, 8.4× RADV on draw calls)
Metal src/rlmtl.h macOS Image gate green (most bit-exact backend); beats GL on 18/19 scenes (up to 16.8× draw calls)
Direct3D 12 src/rldx12.h Windows Image gate green; wins every heavy scene vs both GL and Vulkan

Details, architecture decisions, and full campaign logs: CLAUDE.md and docs/.

Performance vs stock raylib (rlgl)

Numbers from the raylib_tests performance suite (raylib_tests/performance/report_comparison_*.html): the 19 curated raylib examples run at full speed (frame cap / vsync / present-sync neutralized), all backends of one table captured back-to-back in one machine-state window. The number shown is the median frame time in ms (CPU wall time, EndDrawing to EndDrawing) and the multiplier is rlgl's median divided by the backend's, so >1x means the backend is faster than stock OpenGL. Both sides built at the campaign config (-O2 -flto + PGO on gcc platforms, plain -O2 on macOS where Apple clang has no PGO).

° marks a scene at a backend's floor (median within 15% of its own bench_idle): the frame finishes its real work faster than one present/loop turnaround, so the ratio compares present plumbing rather than rendering cost. The heavy scenes without ° are the meaningful ones.

Each table is one machine: its rlgl column and its backend column were captured on the same GPU, driver, and OS, back-to-back. The multipliers are only meaningful within a table. Do not compare a multiplier or a millisecond value from one table against another; different machines, drivers, and capture protocols make those numbers incomparable.

rlgl vs rldx12 - Windows / NVIDIA (Direct3D 12)

Example rlgl ms rldx12 ms rldx12 vs rlgl
shapes_basic_shapes 0.087 0.060° 1.45x°
textures_tiled_drawing 0.058° 0.057° 1.02x°
textures_particles_blending 0.081 0.058° 1.40x°
shapes_starfield_effect 0.057° 0.057° 1.00x°
models_loading 0.054° 0.074 0.73x°
models_heightmap_rendering 0.052° 0.052° 1.00x°
models_skybox_rendering 0.052° 0.061° 0.85x°
models_waving_cubes 1.410 0.705 2.00x
models_first_person_maze 0.049° 0.061° 0.80x°
core_3d_camera_free 0.056° 0.056° 1.00x°
shaders_basic_lighting 0.082 0.070 1.17x
shaders_basic_pbr 0.090 0.077 1.17x
shaders_mandelbrot_set 0.069 0.067 1.03x
shaders_raymarching_rendering 0.228 0.251 0.91x
performance_stress_test 9.672 4.481 2.16x
performance_stress_test_direct 16.872 14.525 1.16x
bench_idle 0.053° 0.057° 0.93x°
bench_drawcalls 9.823 0.588 16.71x
bench_instanced 0.117 0.100 1.17x

rldx12 faster on 11/19 scenes.

RTX 4090, driver 595.97, Windows 11, 3 x 10 s per scene, captured 2026-08-16 in the same window as the rlvk column below. rldx12 wins every heavy scene; its losses are all ° scenes, where a ~15-20 us fixed present-chain cost (a known target) decides the ratio.

rlgl vs rlvk - Windows / NVIDIA (Vulkan)

Example rlgl ms rlvk ms rlvk vs rlgl
shapes_basic_shapes 0.087 0.050 1.74x
textures_tiled_drawing 0.058° 0.043° 1.35x°
textures_particles_blending 0.081 0.046° 1.76x°
shapes_starfield_effect 0.057° 0.050 1.14x°
models_loading 0.054° 0.041° 1.32x°
models_heightmap_rendering 0.052° 0.041° 1.27x°
models_skybox_rendering 0.052° 0.039° 1.33x°
models_waving_cubes 1.410 0.764 1.85x
models_first_person_maze 0.049° 0.039° 1.26x°
core_3d_camera_free 0.056° 0.045° 1.24x°
shaders_basic_lighting 0.082 0.052 1.58x
shaders_basic_pbr 0.090 0.053 1.70x
shaders_mandelbrot_set 0.069 0.045° 1.53x°
shaders_raymarching_rendering 0.228 0.233 0.98x
performance_stress_test 9.672 4.906 1.97x
performance_stress_test_direct 16.872 17.158 0.98x
bench_idle 0.053° 0.042° 1.26x°
bench_drawcalls 9.823 0.599 16.40x
bench_instanced 0.117 0.092 1.27x

rlvk faster on 17/19 scenes.

RTX 4090, driver 595.97, Windows 11, 3 x 10 s per scene, captured 2026-08-16.

rlgl vs rlvk - Linux / AMD (Vulkan, Mesa RADV)

Example rlgl ms rlvk ms rlvk vs rlgl
shapes_basic_shapes 0.095 0.057 1.67x
textures_tiled_drawing 0.092 0.054 1.70x
textures_particles_blending 0.094 0.057 1.65x
shapes_starfield_effect 0.094 0.055 1.71x
models_loading 0.096 0.054 1.78x
models_heightmap_rendering 0.094 0.057 1.65x
models_skybox_rendering 0.179 0.191 0.94x
models_waving_cubes 1.969 0.826 2.38x
models_first_person_maze 0.179 0.191 0.94x
core_3d_camera_free 0.180 0.191 0.94x
shaders_basic_lighting 0.106 0.068 1.56x
shaders_basic_pbr 0.111 0.071 1.56x
shaders_mandelbrot_set 0.197 0.215 0.92x
shaders_raymarching_rendering 0.397 0.400 0.99x
performance_stress_test 13.781 6.177 2.23x
performance_stress_test_direct 23.519 22.659 1.04x
bench_idle 0.075 0.059 1.27x
bench_drawcalls 5.433 0.650 8.36x
bench_instanced 0.136 0.093 1.46x

rlvk faster on 14/19 scenes.

RX 7900 XTX, RADV Mesa 26.1.6, Arch Linux, 3 x 10 s per scene, captured 2026-08-08. Wins land on the heavy scenes (draw calls, batch stress, animated cubes, instancing); the sub-0.2 ms scenes where rlgl leads sit within a few microseconds of each other.

rlgl vs rlmtl - macOS / Apple Silicon (Metal)

Example rlgl ms rlmtl ms rlmtl vs rlgl
shapes_basic_shapes 0.134° 0.041 3.27x°
textures_tiled_drawing 0.145° 0.040 3.61x°
textures_particles_blending 0.164° 0.071 2.32x°
shapes_starfield_effect 0.142° 0.042 3.41x°
models_loading 0.368 0.033° 11.30x°
models_heightmap_rendering 0.401 0.047 8.43x
models_skybox_rendering 0.359 0.039 9.28x
models_waving_cubes 4.039 0.803 5.03x
models_first_person_maze 0.123° 0.035° 3.51x°
core_3d_camera_free 0.359 0.041 8.82x
shaders_basic_lighting 0.269 0.082 3.26x
shaders_basic_pbr 0.287 0.177 1.62x
shaders_mandelbrot_set 0.827 0.236 3.50x
shaders_raymarching_rendering 3.500 4.014 0.87x
performance_stress_test 11.668 5.965 1.96x
performance_stress_test_direct 247.350 231.606 1.07x
bench_idle 0.157° 0.031° 5.00x°
bench_drawcalls 7.958 0.474 16.79x
bench_instanced 1.667 0.114 14.58x

rlmtl faster on 18/19 scenes.

Apple M5, macOS 26.5, captured 2026-08-18 with the interleaved-cycle protocol (10 cycles, column order rotated per cycle, 60 s per sample on the two orbit-heavy scenes and 10 s elsewhere, each cell the mean of its 10 samples). rlmtl here is the shipping configuration: the stock GLSL -> shaderc -> SPIRV-Cross -> MSL pipeline on every scene except performance_stress_test_direct, whose fragment shader is precompiled offline by tools/nir2msl (Mesa's NIR -> MSL compiler) and injected via RLMTL_MSL_OVERRIDE. The single rlgl win is shaders_raymarching_rendering.

Requirements

  • A raylib checkout (stock upstream master; this repo was developed against raylib v6.0-dev)
  • vulkan: Vulkan SDK (Windows: VULKAN_SDK set) or system loader (Linux: libvulkan, macOS: Homebrew vulkan-loader + molten-vk). Runtime GLSL needs shaderc_shared (dlopen'd, optional — without it only the embedded default shader is available).
  • metal: macOS with Homebrew spirv-cross (static-linked); shaderc for runtime GLSL (dlopen'd).
  • dx12: Windows (MSYS2/MinGW shell), inbox d3d12/dxgi/d3dcompiler; Vulkan SDK recommended for shaderc + spirv-cross DLLs (runtime-loaded).

Usage

Clone next to your raylib checkout:

Developer/
  raylib/       <- stock raylib (untouched)
  raylib_aop/   <- this repo

Then:

cd raylib_aop
./build.sh metal          # or: vulkan | dx12

That's it. The script:

  1. copies raylib/src into raylib_aop/build/<backend>/src (fresh each run),
  2. overlays the backend headers and applies patches/ to the staged copy only,
  3. builds libraylib.a there (make GRAPHICS=<backend> RAYLIB_LIBTYPE=STATIC),
  4. copies the result to raylib/src/libraylib.a.

Options:

./build.sh vulkan /path/to/raylib              # explicit raylib location
./build.sh metal ../raylib CUSTOM_CFLAGS=-g    # extra args pass through to make
./build.sh clean                               # drop the staging trees

On Windows, run from an MSYS2/MinGW shell (mingw32-make is used automatically).

Linking your program / the raylib examples

The produced libraylib.a replaces the OpenGL dependency with the backend's own. Link with the backend libs instead of GL:

  • vulkan — Windows: -lraylib -lvulkan-1 -lgdi32 -lwinmm; Linux: -lraylib -lvulkan -lX11 -lm -lpthread -ldl -lrt (-lX11 is required: nothing pulls it in transitively without -lGL); macOS: -lraylib -L/opt/homebrew/lib -lvulkan + the usual Cocoa frameworks.
  • metal — -lraylib -L/opt/homebrew/lib -lspirv-cross-c -lspirv-cross-msl -lspirv-cross-hlsl -lspirv-cross-glsl -lspirv-cross-cpp -lspirv-cross-reflect -lspirv-cross-util -lspirv-cross-core -lc++ -framework Cocoa -framework IOKit -framework CoreAudio -framework CoreVideo -framework Metal -framework QuartzCore -framework Foundation -framework AppKit
  • dx12 — -lraylib -ld3d12 -ldxgi -ldxguid -ld3dcompiler -lgdi32 -lwinmm

Example (raylib examples dir, macOS Metal):

cd ../raylib/examples
make core/core_basic_window LDLIBS="<metal libs above>"

What the patches change

patches/ contains only what is strictly necessary to build raylib with these backends — no general optimizations or fixes to raylib itself.

  • patches/0001-backend-glue.patch — the three integration points a non-GL backend needs, all compile-time-gated so a build without GRAPHICS=... is bit-identical to stock: src/rcore.c selects the backend implementation header and attaches the window surface after rlglInit; src/platforms/rcore_desktop_glfw.c creates the surface (GLFW_NO_API, per-backend present); src/Makefile gains the three GRAPHICS= sections and their link lines.

Testing

The image-equivalence and performance suites live in the sibling raylib_tests repo. Their capture hooks are separate raylib patches (raylib_tests/patches/) that compose cleanly with this repo's patches in either order. To prepare an in-place tree for the test scripts (which build inside the raylib tree):

./build.sh overlay /path/to/raylib-test-clone      # explicitly modifies THAT tree
cd /path/to/raylib-test-clone && git apply ../raylib_tests/patches/*.patch

Use a throwaway clone for that — the whole point of this repo is that your working raylib stays stock.

Repo layout

src/            rlvk.h, rlmtl.h, rldx12.h, rlvk_shaders.h (embedded SPIR-V), shaders/ (GLSL sources)
patches/        backend glue + core optimizations, applied to staged raylib sources
tools/nir2msl/  offline GLSL->NIR->MSL precompiler (Mesa kosmicomp) for RLMTL_MSL_OVERRIDE
docs/           design trackers, plans, and the API consistency audit
build/          staging + build output (gitignored)
CLAUDE.md       agent instructions + full backend architecture documentation

About

Raylib Agentic Optimization

Resources

Stars

19 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages