# WebGPU vs WebGL2

> The same scene, two games, two renderers, side by side. The scene never learns which one draws it.

Example `backend-compare`, running at https://nacatamalon.com/en/examples/backend-compare. The whole catalogue: https://nacatamalon.com/llms/examples.md.

One scene function, two games, two renderers, running at the same time on this page. Sprites, a lit and shiny mesh, a floor, particles and a `dither` post chain, all drawn by both. The scene never learns which backend it is on.

The two are held to **pixel parity** by a comparison suite, not by eye. Where they still differ it is by a handful of pixels at a quantisation boundary or a shadow's penumbra. If your browser has no WebGPU the left panel says so: the fallback demonstrating itself.

## Code

`index.ts`:

```ts
import { start } from './host/start';

const root = document.querySelector<HTMLElement>('#app')!;

root.style.cssText = 'display:grid;grid-template-columns:1fr 1fr;gap:8px';

start(root, 'WEBGPU', 'WebGPU');
start(root, 'WEBGL2', 'WebGL2');
```

`components/exhibit.ts`:

```ts
import { createMesh, createParticles, createSprite, useLoadParticles, useLoadTexture, usePlaneGeometry, useUpdate, useSphereGeometry } from 'nacatamalon';

export const Exhibit = () => {
    // Three dimensions: a ground and a model that turns.
    createMesh({
        geometry: usePlaneGeometry({
            width: 12,
            depth: 12
        }),
        tint: '#39404f'
    });

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 1,
            segments: 28,
            rings: 14
        }),
        transform: {
            y: 1.1
        },
        tint: '#e8e8ee',
        specular: '#ffffff',
        shininess: 28,
    });

    useUpdate((delta) => {
        ball.transform.rotationY += delta * 0.6;
    });

    // Two dimensions: a sprite over the world, in pixels.
    createSprite({
        texture: useLoadTexture({
            src: '/example-assets/assets/gammix.png'
        }),
        width: 56,
        height: 56,
        zIndex: 5,
        transform: {
            x: 40,
            y: 40,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // Particles, worked out here and drawn one call for the lot. Seeded, so the two halves are
    // running the same cloud and not merely a similar one.
    createParticles({
        effect: useLoadParticles({
            src: '/example-assets/effects/smoke.particles'
        }),
        transform: {
            x: 190,
            y: 170
        },
        seed: 4,
        zIndex: 4,
    });
};
```

`host/start.ts`:

```ts
import { createGame } from 'nacatamalon';

import * as Settings from '../settings';
import { Game } from '../scenes/demo';

export const start = (root: HTMLElement, renderer: 'WEBGPU' | 'WEBGL2', label: string) => {
    const wrap = document.createElement('div');

    wrap.style.cssText = 'display:flex;flex-direction:column;gap:4px;min-width:0';

    const tag = document.createElement('div');

    tag.textContent = label;
    tag.style.cssText = 'font:11px ui-monospace,monospace;color:#8ee6a0;letter-spacing:.08em';

    const host = document.createElement('div');

    wrap.append(tag, host);
    root.appendChild(wrap);

    // A browser with no WebGPU cannot run the left half, and that is content rather than a
    // failure: it is the fallback, shown. Say so in the panel instead of throwing.
    if (renderer === 'WEBGPU' && !('gpu' in navigator)) {
        tag.textContent = label + '  ·  unavailable here';
        tag.style.color = '#e0663c';
        host.style.cssText = 'height:220px;display:grid;place-items:center;border:1px dashed #3a3f4b;' +
            'color:#6b7280;font:12px ui-monospace,monospace';
        host.textContent = 'this browser has no WebGPU';

        return;
    }

    const game = createGame(host, {
        width: Settings.WIDTH,
        height: Settings.HEIGHT,
        background: '#0a0a12',
        renderer
    });

    // Declare Scenes
    game({
        Game
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useAmbientLight, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';
import { dither, usePostProcess, POST_EFFECTS } from 'nacatamalon/effects';

import { Exhibit } from '../components/exhibit';

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        transform: {
            y: 2.4,
            z: 5.5,
            rotationX: -0.32
        }
    });
    // The level of the shade and the sun, side by side: an object gives off as many as it asks for.
    useAmbientLight({
        color: '#26304a',
        intensity: 0.4
    });
    useDirectionalLight({
        color: '#fff0d0',
        intensity: 0.9,
        ambient: 0.1,
        transform: {
            rotationX: -0.7,
            rotationY: 0.4
        }
    });

    useSpawn(Exhibit)();
    // And a full-screen effect over all of it. The engine's own effects carry both languages, so the
    // frame comes out the same either way.
    usePostProcess({
        ...dither({
            levels: POST_EFFECTS.COLOR_LEVELS.genesis
        })
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 300;
export const HEIGHT = 220;
```
