Backends
php-vio supports multiple GPU backends. Each backend implements the same vtable interface, so your rendering code works unchanged across platforms.
Selecting a Backend
// Auto-select best available (default)
$ctx = vio_create("auto", ["width" => 800, "height" => 600]);
// Explicit selection
$ctx = vio_create("opengl", [...]);
$ctx = vio_create("vulkan", [...]);
$ctx = vio_create("metal", [...]);
$ctx = vio_create("d3d11", [...]); // Windows only
$ctx = vio_create("d3d12", [...]); // Windows only
$ctx = vio_create("null", [...]); // No-op backend for testingAuto-selection is platform-specific:
| Platform | Priority |
|---|---|
| macOS | Metal > OpenGL |
| Windows | D3D12 > D3D11 > Vulkan > OpenGL |
| Linux | Vulkan > OpenGL |
"auto" skips a backend that reports VIO_FEATURE_3D_PIPELINE = 0 when a later candidate has one, ; on Linux machines without a Vulkan driver it resolves to OpenGL. "null" is never auto-selected. Override globally with the vio.default_backend INI setting.
With vio_create("auto", ["prefer" => "performance" | "quality" | "compat", "require" => [VIO_FEATURE_*, …]]) the fixed priority list is replaced by a ranking of each backend's preferred adapter (vendor profile, discrete before integrated, software rasterizers last, required features) – see vio_rank_backends(). "benchmark" => true measures the top candidates once and caches the result (vio_benchmark_backends()). vio_backend_info()["selected_by"] tells how a context was chosen.
On macOS, Vulkan (via MoltenVK) is supported but opt-in – you must request it explicitly.
OpenGL (3.0 – 4.6)
The most complete backend: every feature path exists here first.
| Property | Value |
|---|---|
| API | OpenGL, negotiated 4.6 → 3.0, core profile from 3.2 (vio_gl_info() shows what you got) |
| Loader | GLAD (vendored) |
| Platforms | macOS, Linux, Windows |
| Headless | ✅ via a hidden window |
OpenGL is the fallback on every platform. Compute, storage images and vertex-stage storage need a 4.3+ context (never on macOS, where Apple's GL stops at 4.1); the feature ladder lists which flag needs which core version or extension.
TIP
On macOS, OpenGL is deprecated by Apple but still functional at version 4.1. For production macOS apps, consider Metal.
Vulkan
Modern, low-overhead GPU API with explicit resource management.
| Property | Value |
|---|---|
| API | Vulkan 1.3 (or 1.2 + dynamic rendering + synchronization2) |
| Memory | Vulkan Memory Allocator (VMA) |
| Platforms | Linux, Windows, macOS (MoltenVK) |
| Headless | ✅ |
Vulkan 1.3 – or 1.2 with VK_KHR_dynamic_rendering and VK_KHR_synchronization2 – is required; a device without them is not chosen. The 3D pipeline compiles GLSL → SPIR-V → Vulkan GLSL (SPIRV-Cross) → SPIR-V and follows the D3D conventions (render-target row 0 = top, depth 0..1). vsync => false selects the IMMEDIATE present mode (MAILBOX → FIFO fallback).
macOS Setup (MoltenVK)
Vulkan on macOS requires MoltenVK as a translation layer:
brew install molten-vk
# Set environment variables
export VK_DRIVER_FILES=/usr/local/etc/vulkan/icd.d/MoltenVK_icd.json
export DYLD_LIBRARY_PATH=/usr/local/libWARNING
macOS System Integrity Protection (SIP) strips DYLD_LIBRARY_PATH from child processes. This means Vulkan may not work when PHP is launched from certain contexts (e.g., IDE terminals). Run directly from a terminal session.
Metal
Apple's native GPU API. Best performance on macOS.
| Property | Value |
|---|---|
| API | Metal |
| Language | Objective-C with ARC |
| Platforms | macOS only |
| Headless | ✅ |
Metal is compiled with Objective-C (vio_metal.m) and uses CAMetalLayer for presentation. It is only available when built with --with-metal.
Metal 2D Pipeline
Since v1.4.0, Metal has a complete native 2D rendering pipeline:
- MSL shaders for shapes (color-only) and sprites (textured)
- Font atlas support via R8Unorm textures with swizzle (1,1,1,R)
- Dynamic vertex buffer growth
- Scissor clipping with HiDPI scaling
- Pixel readback via
vio_read_pixels()andvio_save_screenshot()
Performance
examples/bench_backends.php draws the same five scenes on every backend the build can open: 1280x720, headless, vsync off, 600 frames after 60 warm-up frames. The Mac tables give the frame time from vio_begin() to the return of vio_end() in microseconds, as median / 99th percentile. Measured with php-vio 2.44.1 and PHP 8.5.
Apple M5 (macOS 27.2)
| Scene | Metal | OpenGL 4.1 |
|---|---|---|
| 2D: 500 rects | 66 / 188 | 142 / 258 |
| 2D: 200 rects + 200 rounded rects + 50 texts | 185 / 211 | 284 / 325 |
| 2D: 1000 rects + 100 texts | 277 / 296 | 468 / 593 |
| 3D: 1000 draws, 2 uniforms each | 231 / 249 | 616 / 675 |
| 3D: 10 000 instances, 1 draw | 130 / 146 | 113 / 189 |
MacBook Pro, Intel Core i9-9880H (macOS 26.7) — Metal on either GPU (VIO_METAL_ADAPTER); OpenGL runs on the integrated GPU.
| Scene | Metal, Radeon Pro 5500M | Metal, UHD 630 | OpenGL 4.1, UHD 630 |
|---|---|---|---|
| 2D: 500 rects | 158 / 286 | 279 / 439 | 441 / 1145 |
| 2D: 200 rects + 200 rounded rects + 50 texts | 542 / 706 | 574 / 749 | 878 / 1542 |
| 2D: 1000 rects + 100 texts | 836 / 1165 | 937 / 1262 | 666 / 854 |
| 3D: 1000 draws, 2 uniforms each | 574 / 800 | 703 / 877 | 1194 / 1496 |
| 3D: 10 000 instances, 1 draw | 170 / 273 | 743 / 842 | 1290 / 1429 |
Windows 11, NVIDIA GeForce RTX 2080 — here the table gives the time to issue the frame (vio_begin() to vio_draw_2d()), median in microseconds. Headless Vulkan and OpenGL contexts copy every presented frame back to the host, which makes their vio_end() take longer than on Direct3D, so the frame time would not compare the backends.
| Scene | Direct3D 12 | Direct3D 11 | Vulkan 1.3 | OpenGL 4.6 |
|---|---|---|---|---|
| 2D: 500 rects | 139 | 126 | 180 | 672 |
| 2D: 200 rects + 200 rounded rects + 50 texts | 491 | 453 | 669 | 941 |
| 2D: 1000 rects + 100 texts | 634 | 601 | 825 | 986 |
| 3D: 1000 draws, 2 uniforms each | 831 | 434 | 1146 | 1028 |
| 3D: 10 000 instances, 1 draw | 44 | 123 | 122 | 92 |
| On both Macs Metal is the faster backend in most scenes and has the steadier frame times; OpenGL is ahead in two cells (10 000 instances on the M5 by the median, the text-heavy 2D scene on the Intel Mac). On the RTX 2080 Direct3D 11 issues a frame fastest in four of the five scenes and Direct3D 12 when one draw covers 10 000 instances; Vulkan and OpenGL spend more CPU time per frame. These numbers are CPU-side times of small scenes, each from one run on a desktop machine: they compare the cost of issuing draws, not GPU throughput. Run the script on your own machine before choosing a backend: |
php -d extension=vio examples/bench_backends.php # every backend
php -d extension=vio examples/bench_backends.php metal,opengl 600 --json=bench.jsonDirect3D 11
Immediate-mode DirectX backend. Broad hardware compatibility on Windows.
| Property | Value |
|---|---|
| API | Direct3D 11 (Feature Level 11.0 / 11.1) |
| Shader Model | 5.0 |
| Platforms | Windows only |
| Headless | ✅ via WARP software renderer |
D3D11 uses an immediate-mode rendering model – draw commands are executed directly through ID3D11DeviceContext. This makes it simpler than D3D12 while still providing good performance. In headless mode, the backend automatically selects the WARP software renderer (Windows Advanced Rasterization Platform).
Shader Pipeline
GLSL shaders are automatically compiled through a multi-stage pipeline:
GLSL → SPIR-V (glslang) → HLSL (SPIRV-Cross) → DXBC (D3DCompile)You write standard GLSL – the backend handles the rest. The transpiled HLSL targets Shader Model 5.0 (vs_5_0, ps_5_0).
Build Flags
configure --enable-vio --with-glfw=C:\deps\glfw --with-d3d11Required libraries: d3d11.lib, dxgi.lib, d3dcompiler.lib, dxguid.lib.
TIP
D3D11 is a good choice for Windows applications that need to support older hardware or Windows 7/8.
Direct3D 12
Modern, low-level DirectX backend with explicit resource management.
| Property | Value |
|---|---|
| API | Direct3D 12 |
| Shader Model | 5.1 (FXC); 6.x with DXC via vio_create(['shader_model' => 6]) |
| Platforms | Windows 10+ only |
| Headless | ✅ via WARP software renderer |
D3D12 is the highest-priority backend on Windows. It uses explicit command lists, fence-based GPU synchronization, and a double-buffered swapchain with per-frame command allocators. This gives the driver less overhead and enables better GPU utilization.
Key Differences from D3D11
| Aspect | D3D11 | D3D12 |
|---|---|---|
| Rendering model | Immediate | Command list recording |
| Synchronization | Implicit | Explicit (fence-based) |
| Buffer alignment | 16-byte (constant buffers) | 256-byte (constant buffers) |
| Pipeline state | Separate state objects | Monolithic PSO |
| Root signature | N/A | Shared across pipelines |
| Min. Windows | 7 | 10 |
Shader Pipeline
Same multi-stage pipeline as D3D11, but targets Shader Model 5.1 – or, with vio_create(['shader_model' => 6]) and dxcompiler.dll / dxil.dll, the highest Shader Model 6.x the device and DXC accept (DXIL). Ray tracing, mesh shaders, work graphs and several shader features need Shader Model 6; work graphs usually also need the D3D12 Agility SDK ('agility_sdk' => $dir). vio_swapchain_info() reports the shader model and the Agility SDK in use.
GLSL → SPIR-V (glslang) → HLSL (SPIRV-Cross) → DXBC (D3DCompile)Build Flags
configure --enable-vio --with-glfw=C:\deps\glfw --with-d3d12Required libraries: d3d12.lib, dxgi.lib, d3dcompiler.lib, dxguid.lib.
TIP
D3D12 is auto-selected as the preferred backend on Windows. Use "d3d11" explicitly if you need broader hardware support.
Null Backend
A no-op backend that accepts all calls but produces no output. Useful for:
- Unit testing without GPU access
- CI environments
- Benchmarking PHP-side logic
$ctx = vio_create("null", ["width" => 64, "height" => 64]);Querying Available Backends
// Number of registered backends
$count = vio_backend_count();
// List all backend names
$names = vio_backends(); // e.g. ["opengl", "vulkan", "metal", "null"]
// Get active backend name
$active = vio_backend_name($ctx); // e.g. "opengl"Backend Feature Matrix
Source of truth: each backend's supports_feature(); tests/core/074_backend_capability_matrix.phpt pins it. Query at runtime with vio_supports_feature() and vio_feature_info() (native or emulated). Many flags also depend on the device and driver – the table shows what a backend can provide, not what every GPU has.
| Feature | OpenGL | D3D11 | D3D12 | Vulkan | Metal |
|---|---|---|---|---|---|
| 3D pipeline, instancing, native 2D batch | ✅ | ✅ | ✅ | ✅ | ✅ |
| Render targets: HDR / depth-only / MSAA / cube / MRT (up to 8) | ✅ | ✅ | ✅ | ✅ | ✅ (MRT untested) |
Layered targets, VIO_RT_ALL_LAYERS, multiple viewports | ✅ | ✅ | ✅ | ✅ | ✅ |
| Depth of a colour target, raw readback | ✅ | ✅ | ✅ | ✅ | ✅ (untested) |
| Render targets as storage images | ❌ | ❌ | ✅ | ✅ | ❌ |
| Compute, storage images, async compute | ✅ (GL ≥ 4.3) | ✅ | ✅ | ✅ | ✅ |
| Vertex- and fragment-stage storage | ✅ (GL ≥ 4.3) | ✅ | ✅ | ✅ | ✅ |
| Geometry shaders | ✅ (GL ≥ 3.2) | ✅ | ✅ | ✅ | ✅ emulated (compute) |
| Tessellation | ✅ (GL ≥ 4.0) | ✅ | ✅ | ✅ | ✅ (compute-based) |
| Multiview | ✅ | ✅ emulated (instancing) | ✅ | ✅ | ✅ emulated (instancing) |
| Stencil, texture arrays, BC compression, KTX2 | ✅ | ✅ | ✅ | ✅ | ✅ |
| ASTC textures | with KHR_texture_compression_astc_ldr | ❌ | ❌ | with textureCompressionASTC_LDR | ✅ (Apple GPUs) |
| Indirect draws | ✅ (GL ≥ 4.0) | ✅ | ✅ | ✅ | ✅ |
| Bundles | replay | ✅ native | ✅ native | ✅ native | replay |
| GPU timestamps | ✅ (GL ≥ 3.3) | ✅ | ✅ | ✅ | ✅ |
| On-disk shader cache | ✅ (GL ≥ 4.1) | ✅ | ✅ | ✅ | ✅ |
| Frame latency (waitable swapchain) | ❌ | ✅ | ✅ | ❌ | ✅ |
| HDR10 output | ❌ | ✅ | ✅ | ✅ | ✅ (EDR displays) |
| Subgroups / quad ops | ✅ | ❌ | ✅ (SM 6) | ✅ | ✅ |
| Barycentrics, float16 | ✅ | ❌ | ✅ | ✅ | ✅ |
| 64-bit atomics | ❌ | ❌ | ✅ (SM 6.6) | ✅ | ❌ |
| Variable rate shading (rate / per primitive / image) | ❌ | ❌ | ✅ | ✅ | ❌ (rate maps instead) |
| Bindless textures | ❌ | ❌ | ✅ | ✅ | ✅ |
| Mesh / task shaders | ❌ | ❌ | ✅ (SM 6.5) | ✅ | ✅ (Metal 3) |
| Ray queries | ❌ | ❌ | ✅ (DXR 1.1) | ✅ | ✅ |
| Ray tracing pipelines | ❌ | ❌ | ✅ (DXR 1.0) | ✅ | ❌ |
| Shader execution reordering, opacity micromaps | ❌ | ❌ | ✅ (DXR 1.2) | ✅ | ❌ |
| Sampler feedback (GLSL path / hardware) | ✅ / ❌ | ✅ / ❌ | ✅ / ✅ | ✅ / ❌ | ✅ / ❌ |
| Cooperative matrices | ❌ | ❌ | ❌ | ✅ | ✅ |
| Work graphs | ❌ | ❌ | ✅ (SM 6.8) | ❌ | ❌ |
| Long vectors | ❌ | ❌ | ✅ (SM 6.9, HLSL kernels) | ✅ | ❌ |
| Rasterization rate maps | – | – | – | – | ✅ |
| Portable upscaling | ✅ | ✅ | ✅ | ✅ | ✅ (+ MetalFX spatial) |
| Native upscalers (FSR 3.1, DLSS) | ❌ | ❌ | ✅ | ✅ | ❌ |
vio_read_pixels(), recording / streaming capture | ✅ | ✅ | ✅ | ✅ | ✅ |
The null backend reports no GPU features.
Row order of render targets differs (OpenGL row 0 = bottom, D3D/Metal/Vulkan row 0 = top) – see Render Targets → Orientation.