mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
ab0e6fda8b
dispose() freed the world and region layers only; the ocean, arcs, outline, pick texture and framebuffer, status textures, shared buffers and programs stayed on the context, which React keeps alive across a remount, so each visit to the Atlas left them behind.
342 lines
11 KiB
TypeScript
342 lines
11 KiB
TypeScript
import { vi } from 'vitest';
|
|
|
|
/**
|
|
* A hand-written stand-in for WebGL2RenderingContext, for the Atlas globe
|
|
* renderer. jsdom has no WebGL, so this fake keeps just enough state to tell
|
|
* what the renderer asked for: which program and vertex array a draw ran with,
|
|
* the uniform values each program held at that moment, which buffers were
|
|
* created, filled and deleted, and what went into the status textures.
|
|
*
|
|
* Uniforms and attributes are read from the GLSL sources the renderer links,
|
|
* so a uniform the renderer sets but no shader declares shows up as a call
|
|
* with an undefined location.
|
|
*/
|
|
|
|
export interface FakeShader {
|
|
kind: 'shader';
|
|
type: number;
|
|
src: string;
|
|
}
|
|
|
|
export type UniformValue = number | number[];
|
|
|
|
export interface FakeProgram {
|
|
kind: 'program';
|
|
id: number;
|
|
srcs: string[];
|
|
uniforms: string[];
|
|
attribs: string[];
|
|
attribLoc: Map<string, number>;
|
|
values: Record<string, UniformValue>;
|
|
}
|
|
|
|
export interface FakeUniformLoc {
|
|
kind: 'uniform';
|
|
name: string;
|
|
program: FakeProgram;
|
|
}
|
|
|
|
export interface FakeObject {
|
|
kind: 'buffer' | 'vao' | 'texture' | 'framebuffer';
|
|
id: number;
|
|
data?: ArrayBufferView | null;
|
|
}
|
|
|
|
export interface FakeDraw {
|
|
call: 'drawElements' | 'drawArrays' | 'drawArraysInstanced';
|
|
program: FakeProgram | null;
|
|
vao: FakeObject | null;
|
|
framebuffer: FakeObject | null;
|
|
mode: number;
|
|
count: number;
|
|
instances: number;
|
|
values: Record<string, UniformValue>;
|
|
}
|
|
|
|
export interface FakeGlOptions {
|
|
/** Whether a shader with this source compiles. Default: all do. */
|
|
compileOk?: (src: string) => boolean;
|
|
shaderLog?: string | null;
|
|
linkOk?: boolean;
|
|
programLog?: string | null;
|
|
/**
|
|
* Location of an attribute of the program with this fragment shader; null
|
|
* leaves it out of the active list (as a compiler drops an unused one).
|
|
*/
|
|
attribLocation?: (fs: string, name: string, index: number) => number | null;
|
|
/** RGBA the pick pass reads back. */
|
|
pixel?: [number, number, number, number];
|
|
}
|
|
|
|
export const GL = {
|
|
COMPILE_STATUS: 0x8b81,
|
|
LINK_STATUS: 0x8b82,
|
|
ACTIVE_UNIFORMS: 0x8b86,
|
|
ACTIVE_ATTRIBUTES: 0x8b89,
|
|
VERTEX_SHADER: 0x8b31,
|
|
FRAGMENT_SHADER: 0x8b30,
|
|
ARRAY_BUFFER: 0x8892,
|
|
ELEMENT_ARRAY_BUFFER: 0x8893,
|
|
STATIC_DRAW: 0x88e4,
|
|
TEXTURE_2D: 0x0de1,
|
|
RGBA8: 0x8058,
|
|
RGBA: 0x1908,
|
|
UNSIGNED_BYTE: 0x1401,
|
|
TEXTURE_MIN_FILTER: 0x2801,
|
|
TEXTURE_MAG_FILTER: 0x2800,
|
|
NEAREST: 0x2600,
|
|
FLOAT: 0x1406,
|
|
DEPTH_TEST: 0x0b71,
|
|
BLEND: 0x0be2,
|
|
SRC_ALPHA: 0x0302,
|
|
ONE_MINUS_SRC_ALPHA: 0x0303,
|
|
ONE: 1,
|
|
TRIANGLES: 4,
|
|
TRIANGLE_STRIP: 5,
|
|
TEXTURE0: 0x84c0,
|
|
TEXTURE1: 0x84c1,
|
|
UNSIGNED_INT: 0x1405,
|
|
FRAMEBUFFER: 0x8d40,
|
|
COLOR_ATTACHMENT0: 0x8ce0,
|
|
COLOR_BUFFER_BIT: 0x4000,
|
|
} as const;
|
|
|
|
const UNIFORM_RE = /^\s*uniform\s+\w+\s+(\w+)\s*;/gm;
|
|
const ATTRIB_RE = /^in\s+\w+\s+(a_\w+)\s*;/gm;
|
|
|
|
function namesOf(re: RegExp, src: string): string[] {
|
|
const out: string[] = [];
|
|
for (const m of src.matchAll(re)) if (!out.includes(m[1])) out.push(m[1]);
|
|
return out;
|
|
}
|
|
|
|
export function createFakeGl(opts: FakeGlOptions = {}) {
|
|
let nextId = 1;
|
|
const make = (kind: FakeObject['kind']): FakeObject => ({ kind, id: nextId++ });
|
|
|
|
const programs: FakeProgram[] = [];
|
|
const draws: FakeDraw[] = [];
|
|
const buffers: FakeObject[] = [];
|
|
const textures: FakeObject[] = [];
|
|
const framebuffers: FakeObject[] = [];
|
|
const bound = new Map<number, FakeObject | null>();
|
|
const statusUploads: Uint8Array[] = [];
|
|
const enabled = new Set<number>();
|
|
let program: FakeProgram | null = null;
|
|
let vao: FakeObject | null = null;
|
|
let fbo: FakeObject | null = null;
|
|
|
|
const store = (loc: FakeUniformLoc | null | undefined, v: UniformValue): void => {
|
|
if (loc) loc.program.values[loc.name] = v;
|
|
};
|
|
const record = (call: FakeDraw['call'], mode: number, count: number, instances: number): void => {
|
|
draws.push({
|
|
call,
|
|
program,
|
|
vao,
|
|
framebuffer: fbo,
|
|
mode,
|
|
count,
|
|
instances,
|
|
values: program ? { ...program.values } : {},
|
|
});
|
|
};
|
|
|
|
const gl = {
|
|
...GL,
|
|
createShader: vi.fn((type: number): FakeShader => ({ kind: 'shader', type, src: '' })),
|
|
shaderSource: vi.fn((s: FakeShader, src: string) => {
|
|
s.src = src;
|
|
}),
|
|
compileShader: vi.fn((_s: FakeShader) => undefined),
|
|
getShaderParameter: vi.fn((s: FakeShader, pname: number) =>
|
|
pname === GL.COMPILE_STATUS ? (opts.compileOk ? opts.compileOk(s.src) : true) : null
|
|
),
|
|
getShaderInfoLog: vi.fn(() => (opts.shaderLog === undefined ? 'shader log' : opts.shaderLog)),
|
|
deleteShader: vi.fn((_s: FakeShader) => undefined),
|
|
deleteProgram: vi.fn((_p: FakeProgram) => undefined),
|
|
createProgram: vi.fn((): FakeProgram => {
|
|
const p: FakeProgram = {
|
|
kind: 'program',
|
|
id: nextId++,
|
|
srcs: [],
|
|
uniforms: [],
|
|
attribs: [],
|
|
attribLoc: new Map(),
|
|
values: {},
|
|
};
|
|
programs.push(p);
|
|
return p;
|
|
}),
|
|
attachShader: vi.fn((p: FakeProgram, s: FakeShader) => {
|
|
p.srcs.push(s.src);
|
|
}),
|
|
linkProgram: vi.fn((p: FakeProgram) => {
|
|
const [vs = '', fs = ''] = p.srcs;
|
|
p.uniforms = namesOf(UNIFORM_RE, `${vs}\n${fs}`);
|
|
namesOf(ATTRIB_RE, vs).forEach((name, i) => {
|
|
const loc = opts.attribLocation ? opts.attribLocation(fs, name, i) : i;
|
|
if (loc === null) return;
|
|
p.attribs.push(name);
|
|
p.attribLoc.set(name, loc);
|
|
});
|
|
}),
|
|
getProgramParameter: vi.fn((p: FakeProgram, pname: number) => {
|
|
if (pname === GL.LINK_STATUS) return opts.linkOk ?? true;
|
|
// One more than there are, so the renderer meets a null info as well.
|
|
if (pname === GL.ACTIVE_UNIFORMS) return p.uniforms.length + 1;
|
|
if (pname === GL.ACTIVE_ATTRIBUTES) return p.attribs.length + 1;
|
|
return null;
|
|
}),
|
|
getProgramInfoLog: vi.fn(() => (opts.programLog === undefined ? 'program log' : opts.programLog)),
|
|
getActiveUniform: vi.fn((p: FakeProgram, i: number) =>
|
|
i < p.uniforms.length ? { name: p.uniforms[i], size: 1, type: 0 } : null
|
|
),
|
|
getUniformLocation: vi.fn((p: FakeProgram, name: string): FakeUniformLoc => ({
|
|
kind: 'uniform',
|
|
name,
|
|
program: p,
|
|
})),
|
|
getActiveAttrib: vi.fn((p: FakeProgram, i: number) =>
|
|
i < p.attribs.length ? { name: p.attribs[i], size: 1, type: 0 } : null
|
|
),
|
|
getAttribLocation: vi.fn((p: FakeProgram, name: string) => p.attribLoc.get(name) ?? -1),
|
|
createBuffer: vi.fn(() => {
|
|
const b = make('buffer');
|
|
buffers.push(b);
|
|
return b;
|
|
}),
|
|
bindBuffer: vi.fn((target: number, b: FakeObject | null) => {
|
|
bound.set(target, b);
|
|
}),
|
|
bufferData: vi.fn((target: number, data: ArrayBufferView) => {
|
|
const b = bound.get(target);
|
|
if (b) b.data = data;
|
|
}),
|
|
deleteBuffer: vi.fn((_b: FakeObject | null) => undefined),
|
|
createVertexArray: vi.fn(() => make('vao')),
|
|
bindVertexArray: vi.fn((v: FakeObject | null) => {
|
|
vao = v;
|
|
}),
|
|
deleteVertexArray: vi.fn((_v: FakeObject | null) => undefined),
|
|
enableVertexAttribArray: vi.fn((_loc: number) => undefined),
|
|
vertexAttribPointer: vi.fn(
|
|
(_loc: number, _size: number, _type: number, _normalized: boolean, _stride: number, _offset: number) => undefined
|
|
),
|
|
vertexAttribDivisor: vi.fn((_loc: number, _divisor: number) => undefined),
|
|
vertexAttrib2f: vi.fn((_loc: number | undefined, _x: number, _y: number) => undefined),
|
|
createTexture: vi.fn(() => {
|
|
const t = make('texture');
|
|
textures.push(t);
|
|
return t;
|
|
}),
|
|
bindTexture: vi.fn((target: number, t: FakeObject | null) => {
|
|
bound.set(target, t);
|
|
}),
|
|
texImage2D: vi.fn(
|
|
(
|
|
_target: number,
|
|
_level: number,
|
|
_internal: number,
|
|
_w: number,
|
|
_h: number,
|
|
_border: number,
|
|
_format: number,
|
|
_type: number,
|
|
_data: ArrayBufferView | null
|
|
) => undefined
|
|
),
|
|
texParameteri: vi.fn((_target: number, _pname: number, _value: number) => undefined),
|
|
texSubImage2D: vi.fn(
|
|
(
|
|
_target: number,
|
|
_level: number,
|
|
_x: number,
|
|
_y: number,
|
|
_w: number,
|
|
_h: number,
|
|
_format: number,
|
|
_type: number,
|
|
data: Uint8Array
|
|
) => {
|
|
statusUploads.push(data.slice());
|
|
}
|
|
),
|
|
activeTexture: vi.fn((_unit: number) => undefined),
|
|
deleteTexture: vi.fn((_t: FakeObject | null) => undefined),
|
|
createFramebuffer: vi.fn(() => {
|
|
const f = make('framebuffer');
|
|
framebuffers.push(f);
|
|
return f;
|
|
}),
|
|
bindFramebuffer: vi.fn((_target: number, f: FakeObject | null) => {
|
|
fbo = f;
|
|
}),
|
|
framebufferTexture2D: vi.fn(
|
|
(_target: number, _attachment: number, _texTarget: number, _t: FakeObject | null, _level: number) => undefined
|
|
),
|
|
deleteFramebuffer: vi.fn((_f: FakeObject | null) => undefined),
|
|
useProgram: vi.fn((p: FakeProgram | null) => {
|
|
program = p;
|
|
}),
|
|
uniform1f: vi.fn((loc: FakeUniformLoc | null | undefined, v: number) => store(loc, v)),
|
|
uniform1i: vi.fn((loc: FakeUniformLoc | null | undefined, v: number) => store(loc, v)),
|
|
uniform2f: vi.fn((loc: FakeUniformLoc | null | undefined, a: number, b: number) => store(loc, [a, b])),
|
|
uniform4f: vi.fn((loc: FakeUniformLoc | null | undefined, a: number, b: number, c: number, d: number) =>
|
|
store(loc, [a, b, c, d])
|
|
),
|
|
uniform3fv: vi.fn((loc: FakeUniformLoc | null | undefined, v: ArrayLike<number>) => store(loc, Array.from(v))),
|
|
uniform4fv: vi.fn((loc: FakeUniformLoc | null | undefined, v: ArrayLike<number>) => store(loc, Array.from(v))),
|
|
viewport: vi.fn((_x: number, _y: number, _w: number, _h: number) => undefined),
|
|
enable: vi.fn((cap: number) => {
|
|
enabled.add(cap);
|
|
}),
|
|
disable: vi.fn((cap: number) => {
|
|
enabled.delete(cap);
|
|
}),
|
|
blendFuncSeparate: vi.fn((_a: number, _b: number, _c: number, _d: number) => undefined),
|
|
clearColor: vi.fn((_r: number, _g: number, _b: number, _a: number) => undefined),
|
|
clear: vi.fn((_mask: number) => undefined),
|
|
drawArrays: vi.fn((mode: number, _first: number, count: number) => record('drawArrays', mode, count, 1)),
|
|
drawElements: vi.fn((mode: number, count: number) => record('drawElements', mode, count, 1)),
|
|
drawArraysInstanced: vi.fn((mode: number, _first: number, count: number, instances: number) =>
|
|
record('drawArraysInstanced', mode, count, instances)
|
|
),
|
|
readPixels: vi.fn(
|
|
(_x: number, _y: number, _w: number, _h: number, _format: number, _type: number, px: Uint8Array) => {
|
|
if (opts.pixel) px.set(opts.pixel);
|
|
}
|
|
),
|
|
};
|
|
|
|
return {
|
|
gl,
|
|
programs,
|
|
draws,
|
|
buffers,
|
|
textures,
|
|
framebuffers,
|
|
statusUploads,
|
|
enabled,
|
|
/** The program linked with this fragment shader. */
|
|
programWith(fs: string): FakeProgram {
|
|
const p = programs.find((x) => x.srcs[1] === fs);
|
|
if (!p) throw new Error('no program with that fragment shader');
|
|
return p;
|
|
},
|
|
/** Draws run with the program of this fragment shader. */
|
|
drawsWith(fs: string): FakeDraw[] {
|
|
return draws.filter((d) => d.program?.srcs[1] === fs);
|
|
},
|
|
/** The buffer that was filled with exactly this array. */
|
|
bufferOf(data: ArrayBufferView): FakeObject | undefined {
|
|
return buffers.find((b) => b.data === data);
|
|
},
|
|
canvas(): HTMLCanvasElement {
|
|
return { getContext: vi.fn(() => gl) } as unknown as HTMLCanvasElement;
|
|
},
|
|
};
|
|
}
|
|
|
|
export type FakeGl = ReturnType<typeof createFakeGl>;
|