Skip to content

Rendering ​

Architecture ​

PHPolygon uses a layered rendering architecture with backend-agnostic command lists:

Game Code / Scene
      ↓  (builds)
RenderCommandList        ← pure PHP data, no GPU calls
      ↓  (executed by)
┌──────────────────┬──────────────────┬──────────────────┬──────────────────┐
│  VioRenderer3D   │ OpenGLRenderer3D │ VulkanRenderer3D │  NullRenderer3D  │
│   (primary)      │   (fallback)     │   (standalone)   │ (headless/tests) │
└──────────────────┴──────────────────┴──────────────────┴──────────────────┘

Game code never touches GPU APIs directly. The Renderer3DSystem collects MeshRenderer + Transform3D components and builds a RenderCommandList that the active backend executes.

Render Interfaces ​

RenderContextInterface           ← base: beginFrame, endFrame, clear, setViewport
├── Renderer2DInterface          ← 2D backend (VioRenderer2D, or NanoVG Renderer2D as fallback)
└── Renderer3DInterface          ← 3D backend (VioRenderer3D, or OpenGL / Vulkan standalone)

INFO

The 2D renderer is production-ready with VIO as the primary backend and NanoVG/OpenGL as fallback - Code Tycoon (released on Steam) ships with it. The 3D renderer runs through VioRenderer3D on every platform; Code Rescue is the first 3D game built on it.

3D Render Backends ​

BackendClassStatusNotes
Vio (hardware)VioRenderer3DProductionPrimary 3D backend on every platform. php-vio dispatches to Metal (macOS), D3D11 / D3D12 (Windows), Vulkan or OpenGL; vio_backend_name() tells which
OpenGLOpenGLRenderer3DFallbackvia php-glfw when php-vio is unavailable. Context ladder GL 4.6 down to a GL 3.0 floor; GPU instancing and the post-process chain need GL 3.3 and degrade below it
VulkanVulkanRenderer3DStandalonevia php-vulkan, when php-vio is not loaded or EngineConfig::$useNative3D opts in
NullNullRenderer3DAvailableHeadless/CI testing - stores commands for assertions

The former standalone Metal renderer is retired: php-vio's own Metal backend is the macOS path, so renderBackend3D: 'metal' lands in VioRenderer3D. Direct3D 11/12 are reached only through php-vio.

2D Renderers:

BackendClassStatusNotes
Vio (hardware)VioRenderer2DProductionPrimary 2D backend via php-vio
NanoVG (OpenGL)Renderer2DProductionFallback 2D backend via php-glfw
GDGdRenderer2DAvailableSoftware renderer for visual regression tests
NullNullRenderer2DAvailableHeadless/CI testing

All backends implement Renderer3DInterface / Renderer2DInterface and execute the same command lists. Game code is fully backend-agnostic.

Metal vs OpenGL 2D Performance ​

Benchmarked on Apple M2 Pro (1280x720, VSync off). Measures draw + flush time only.

ScenarioMetalOpenGLDelta
500 rects192 us179 us+7%
200 rects + 200 rounded rects + 50 text323 us313 us+3%
1000 rects + 100 text301 us374 us-20%

Metal is slightly slower on simple scenes but 20% faster on heavy scenes with 1000+ draw calls. Tail latency (p95/p99) is 30-40% better with Metal, meaning fewer frame time spikes.

On Intel Macs the difference is more dramatic because macOS translates OpenGL calls through a Metal compatibility layer – using Metal directly eliminates this overhead.

Backend Auto-Selection

The engine auto-detects available backends at startup:

if extension_loaded('vio') && !headless:
    → VioMainWindow, VioInput, VioRenderer2D, VioRenderer3D, VioTextureManager, VioAudioBackend
    → vio_create('auto') picks the platform's best backend (EngineConfig::$vioBackend overrides):
      Metal on macOS, D3D12 / D3D11 on Windows, Vulkan on Linux, OpenGL as fallback
else:
    → GLFW Window, GLFW Input, NanoVG Renderer2D
    → 3D backend from config: 'opengl' | 'vulkan' | 'null'
if headless:
    → NullWindow, NullRenderer2D, NullRenderer3D, NullTextureManager

When php-vio is available, it is always preferred for all rendering, input, and audio subsystems. The GLFW/NanoVG/OpenGL stack serves as a fallback when php-vio is not installed.

Fallback Font Chain ​

VioRenderer2D supports a per-glyph fallback font system for locales that need additional Unicode coverage (e.g. CJK, Hangul):

php
// Register fallback fonts for a base font
$r2d->addFallbackFont('inter-semibold', 'noto-sans-sc');
$r2d->addFallbackFont('inter-semibold', 'noto-sans-kr');

// Pre-rasterise the glyph atlases of loaded fonts at commonly used sizes
// (one worker thread per atlas) to avoid stutter on first use
$r2d->warmFonts(['inter-semibold', 'noto-sans-sc'], [15.0, 26.0]);

// Remove all fallback registrations (e.g. when switching to a non-CJK locale)
$r2d->clearFallbackFonts();

The primary font renders first; fallback fonts then fill in any missing glyphs on a per-glyph basis. measureText() uses the full chain for width calculation. The Vio font atlas is 4096x4096 with multi-range Unicode support (Latin, Cyrillic, Greek, CJK, Hangul, Vietnamese).

The engine automatically loads Noto Sans SC and Noto Sans KR as fallback fonts from resources/fonts/noto-sans-cjk/ when CJK locale support is needed. UIContext text rendering automatically benefits from any fallback fonts configured on the renderer.

3D Render Pipeline ​

The OpenGL backend uses a multi-pass system:

  1. Shadow pass - render depth from directional light's perspective (Cascade Shadow Maps when enabled)
  2. Opaque pass - draw all meshes with alpha >= 1.0
  3. Transparent pass - draw meshes with alpha < 1.0 (sorted back-to-front by mesh sphere centre, not pivot)
  4. Skybox pass - cubemap background
  5. Post-process - TAA, color grading, vignette, FXAA (when enabled in GraphicsSettings)

Transparency Scope

Only DrawMesh draws produced by Renderer3DSystem are depth-sorted. DrawMeshInstanced batches (e.g. from InstancedTerrainSystem) cannot be per-instance sorted without defeating batching; transparent materials passed to instanced systems are rejected with a one-shot warning.

Render Commands ​

Commands are plain PHP value objects appended to the RenderCommandList:

CommandPurpose
SetCameraView + projection matrices
SetAmbientLightAmbient color + intensity
SetDirectionalLightDirectional light (VioRenderer3D shades the first 4 per frame)
AddPointLightPoint light (Renderer3DSystem keeps the 8 nearest in view; VioRenderer3D takes up to 512 through its light grid where the backend supports it)
AddSpotLight / AddRectLightSpot light with cone and penumbra / rectangular area light
DrawMeshSingle mesh draw
DrawMeshInstancedGPU-instanced mesh (many copies, one draw call)
SetSkyboxCubemap skybox
SetFogDistance fog
SetShaderOverride active shader for subsequent draws
SetWaveAnimationWave vertex animation (amplitude, frequency, phase)
SetWindGlobal wind direction + intensity (drives cloth + foliage sway)
SetSkyColorsSky gradient (sky color, horizon color)
SetSnowCoverPer-frame snow accumulation factor (0..1)
SetGroundWetnessPer-frame rain wetness factor (0..1)
SetEnvironmentMapPBR environment map
DrawOutlineScreen-space outline around a mesh
SetShaderParameter / SetShaderTextureUniforms and samplers a game declared for its own shader snippets

The full list with every parameter is in the Render Commands reference.

Materials ​

Materials define PBR surface properties plus four orthogonal extension sets: carpaint (clearcoat + flakes + IBL), procedural normal patterns, procedural surface-wear patterns, and vertex-shader cloth. Every extension defaults to off, so existing materials keep rendering identically.

php
use PHPolygon\Rendering\Material;
use PHPolygon\Rendering\MaterialRegistry;
use PHPolygon\Rendering\Color;

// Plain PBR
MaterialRegistry::register('brick', new Material(
    albedo: new Color(0.7, 0.3, 0.2),
    roughness: 0.8,
    metallic: 0.0,
));

MaterialRegistry::register('chrome', new Material(
    albedo: new Color(0.9, 0.9, 0.9),
    roughness: 0.1,
    metallic: 1.0,
    useEnvironmentMap: true,  // sample IBL cubemap for reflection
));

Carpaint ​

Material::carpaint(...) is the convenience factory for paint with a clearcoat lobe + metallic flake jitter + IBL reflection. Used by the Car prefab's default materials.

php
MaterialRegistry::register('car_paint_red', Material::carpaint(
    new Color(0.85, 0.15, 0.15),
    metallic: 0.7,
    roughness: 0.30,
));

The proc_mode = 10 carpaint shader path is auto-selected when the material id has the car_paint_ prefix.

Procedural Normal + Surface Patterns ​

Both pattern systems are evaluated in-shader from a string id - no texture uploads, no external image files. Tangent space is derived per-fragment via screen-space derivatives so meshes do not need to ship tangent buffers.

FieldPattern set
normalPatternbricks, bumps, orange_peel, hammered, hexagons, wood_grain, scratches, cracked, noise, skin, cobblestone, roof_tiles (constants on NormalPattern)
surfacePatternworn_paint, rust, brushed_metal, polished_rings, skin, marble, sandstone (constants on SurfacePattern)
php
MaterialRegistry::register('castle_wall', new Material(
    albedo: new Color(0.55, 0.50, 0.45),
    roughness: 0.85,
    normalPattern: 'bricks',
    normalScale: 4.0,
    surfacePattern: 'worn_paint',
    surfaceScale: 2.0,
    surfaceIntensity: 0.6,
));

Wetness (SSR Surrogate) ​

wetness (0 = dry, 1 = soaked) is a forward-renderer stand-in for screen-space reflections. It reduces effective roughness, darkens albedo, and amplifies IBL on up-facing fragments so wet asphalt / polished floors read as reflective without a G-buffer ray-march pass.

php
MaterialRegistry::register('asphalt_wet', new Material(
    albedo: new Color(0.15, 0.15, 0.16),
    roughness: 0.9,
    wetness: 0.7,
));

Vertex-Shader Cloth ​

Material::cloth(...) opts a material into procedural per-vertex sway. Anchor weight is derived from local Y over the mesh's AABB so the top stays still and the bottom swings - good enough for background-character trenchcoats, capes, banners, hanging cables. No CPU simulation, no extra render passes.

php
use PHPolygon\Rendering\Command\SetWind;
use PHPolygon\Math\Vec3;

MaterialRegistry::register('coat_fabric', Material::cloth(
    new Color(0.20, 0.18, 0.22),
    strength: 0.10,
    frequency: 1.4,
));

// Drive global wind via the SetWind command, once per frame
$commandList->add(new SetWind(
    direction: new Vec3(0.0, 0.0, 1.0),
    intensity: 0.6,
));

The cloth animation runs in the vertex stage; its formula is kept identical between the OpenGL and Vio shader sources (locked by ClothShaderConsistencyTest). Real physical cloth (Bullet SoftBody / GPU compute solver) is documented in docs/rfcs/compute-pipeline.md.

GPU Instancing ​

When the same mesh appears multiple times, use DrawMeshInstanced for a single GPU draw call:

php
$matrices = [];
foreach ($buildingPositions as $pos) {
    $matrices[] = Mat4::translation($pos->x, $pos->y, $pos->z);
}
$commandList->add(new DrawMeshInstanced('building', 'brick', $matrices, isStatic: true));

The isStatic flag caches the matrix buffer across frames for maximum performance.

For repeating static meshes that are ordinary entities, set MeshRenderer::$batchable: after culling, Renderer3DSystem merges what is left of one mesh + material into a single DrawMeshInstanced. Entities that moved, transparent materials and single survivors keep their own DrawMesh.

Material Presets ​

PHPolygon ships with built-in material presets for common surface types:

ClassMaterials
WoodMaterialsWood texture variants
MetalMaterialsMetal texture variants
FabricMaterialsFabric texture variants
ThatchMaterialsThatch texture variants

Procedural Sky ​

The engine includes ProceduralSky for skybox generation without external textures (gradient, sun, stars). Use with ProceduralCubemap and CubemapRegistry for PBR environment lighting.

Shadow Mapping ​

ShadowMapRenderer provides depth-only shadow passes from directional lights. CloudShadowRenderer adds cloud shadow projection.

Released under the MIT License.