Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

3.3 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  A material's vertex hook (`#pragma vertexoffset`) gets a derived WGSL twin, so the shader runs on the WebGPU backend as it does on WebGL.
 *        The hook is translated as a program of its own; what the engine's WGSL vertex stage injects is the function under the name it calls
 *        (`esMaterialVertex`), the material block as `mc`, and the helpers the hook uses.
 */
import { describe, it, expect, beforeAll } from 'vitest';
// @ts-expect-error — a repo tool, shipped as .mjs with no declarations
import { deriveTwin, loadTwinModule, needsTwin } from '../gen-shader-twins.mjs';

const SWAY = `#pragma shader "SwayTest"
#pragma version 300 es
#pragma domain Lit
#pragma param u_amp float default(2)
#pragma switch WILD default(off)

#pragma vertexoffset
float wobble(float x) { return sin(x * 3.0); }
void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
#ifdef WILD
    local.x += wobble(uv.y) * u_amp * 3.0;
#else
    local.x += wobble(uv.y) * u_amp;
#endif
}
#pragma end

#pragma fragment
precision mediump float;
in vec4 v_color;
in vec2 v_texCoord;
out vec4 fragColor;
void main() { fragColor = v_color; }
#pragma end
`;

let module: unknown = null;
beforeAll(async () => {
    try { module = await loadTwinModule(); } catch { module = null; }
});

describe('the twin of a vertex hook', () => {
    it('is derived with the fragment, behind the same switches, in the names the engine\'s stage injects', async (ctx) => {
        if (!module) return ctx.skip();
        const r = await deriveTwin(module, SWAY, {});
        expect(r.status).toBe('generated');
        const hook = /#pragma vertexoffset wgsl\n([\s\S]*?)#pragma end/.exec(r.text)![1]!;
        expect(hook).toContain('// GENERATED by tools/gen-shader-twins.mjs');
        expect(hook).toContain('#ifdef WILD');
        expect(hook).toContain('fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f)');
        expect(hook).toContain('mc.u_amp');
        expect(hook).not.toContain('var<uniform>');
        expect(hook).not.toContain('gl_PerVertex');
        // a program of the engine's own parser accepts what was written
        expect((module as { esshader_cookInfo(s: string, f: string): { valid: boolean } }).esshader_cookInfo(r.text, 'WILD').valid).toBe(true);
    });

    it('leaves a hook written by hand alone', async (ctx) => {
        if (!module) return ctx.skip();
        const authored = SWAY.replace('#pragma fragment\n', '#pragma vertexoffset wgsl\nfn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f) {}\n#pragma end\n\n#pragma fragment\n');
        const r = await deriveTwin(module, authored, {});
        expect(r.status).toBe('generated');
        expect(r.text.match(/#pragma vertexoffset wgsl/g)).toHaveLength(1);
        expect(r.text).not.toContain('esMaterialVertexBody');
    });

    it('is replaced with the rest when the shader is derived again', async (ctx) => {
        if (!module) return ctx.skip();
        const first = (await deriveTwin(module, SWAY, {})).text as string;
        expect(needsTwin(first)).toBe(false);
        const again = await deriveTwin(module, first, { force: true });
        expect(again.text.match(/#pragma vertexoffset wgsl/g)).toHaveLength(1);
    });
});