92 lines
3.7 KiB
JavaScript
92 lines
3.7 KiB
JavaScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
/**
|
|
* Copy the OpenGL GAL's data shaders and lookup textures out of a KiCad
|
|
* checkout, verbatim:
|
|
*
|
|
* common/gal/shaders/smaa_base.glsl -> common/gal/opengl/shaders/smaa_base.ts
|
|
* common/gal/opengl/SmaaAreaTex.h -> common/gal/opengl/smaa_textures.ts
|
|
* common/gal/opengl/SmaaSearchTex.h -> (same file)
|
|
*
|
|
* smaa_base.glsl is the reference SMAA implementation (Jimenez et al.), which
|
|
* the port compiles in its SMAA_GLSL_3 flavour under GLSL ES 3.00. The pass
|
|
* shaders around it are translated by hand in antialiasing.ts.
|
|
*
|
|
* Usage:
|
|
* node tools/gen_gal_shaders.mjs [path-to-kicad-source]
|
|
*/
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
import { dirname, join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
const KICAD = resolve(process.argv[2] ?? '/home/akshay/kicad-reference');
|
|
const OUT = join(HERE, '..', 'common/gal/opengl');
|
|
|
|
const HEADER = `// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
//
|
|
// GENERATED by tools/gen_gal_shaders.mjs from KiCad's`;
|
|
|
|
const smaa = readFileSync(join(KICAD, 'common/gal/shaders/smaa_base.glsl'), 'utf8');
|
|
writeFileSync(
|
|
join(OUT, 'shaders/smaa_base.ts'),
|
|
`${HEADER} common/gal/shaders/smaa_base.glsl.
|
|
// Do not edit; re-run the generator instead. The SMAA reference implementation
|
|
// (MIT-licensed, see the licence block inside), verbatim.
|
|
|
|
export const glsl_smaa_base: string = String.raw\`${smaa.replace(/`/g, '\\`').replace(/\$\{/g, '$\\{')}\`;
|
|
`,
|
|
);
|
|
|
|
function bytes(header, name) {
|
|
const src = readFileSync(join(KICAD, 'common/gal/opengl', header), 'utf8');
|
|
const body = src.slice(src.indexOf(`${name}[] = {`) + `${name}[] = {`.length);
|
|
const out = [];
|
|
for (const m of body.matchAll(/0x[0-9a-fA-F]+|\d+/g)) {
|
|
if (body.indexOf('};') !== -1 && m.index > body.indexOf('};')) break;
|
|
out.push(Number(m[0]));
|
|
}
|
|
return Uint8Array.from(out);
|
|
}
|
|
const area = bytes('SmaaAreaTex.h', 'areaTexBytes');
|
|
const search = bytes('SmaaSearchTex.h', 'searchTexBytes');
|
|
if (area.length !== 160 * 560 * 2) throw new Error(`area tex ${area.length}`);
|
|
if (search.length !== 64 * 16) throw new Error(`search tex ${search.length}`);
|
|
|
|
writeFileSync(
|
|
join(OUT, 'smaa_textures.ts'),
|
|
`${HEADER} common/gal/opengl/SmaaAreaTex.h
|
|
// and SmaaSearchTex.h. Do not edit; re-run the generator instead. The SMAA
|
|
// lookup textures (MIT-licensed, Jimenez et al.), as base64.
|
|
|
|
export const AREATEX_WIDTH = 160;
|
|
export const AREATEX_HEIGHT = 560;
|
|
export const AREATEX_PITCH = AREATEX_WIDTH * 2;
|
|
export const AREATEX_SIZE = AREATEX_HEIGHT * AREATEX_PITCH;
|
|
|
|
export const SEARCHTEX_WIDTH = 64;
|
|
export const SEARCHTEX_HEIGHT = 16;
|
|
export const SEARCHTEX_PITCH = SEARCHTEX_WIDTH;
|
|
export const SEARCHTEX_SIZE = SEARCHTEX_HEIGHT * SEARCHTEX_PITCH;
|
|
|
|
function decode(b64: string): Uint8Array {
|
|
const bin = atob(b64);
|
|
const out = new Uint8Array(bin.length);
|
|
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
return out;
|
|
}
|
|
|
|
const AREA_B64 = '${Buffer.from(area).toString('base64')}';
|
|
const SEARCH_B64 = '${Buffer.from(search).toString('base64')}';
|
|
|
|
/** \`areaTexBytes\`: 160 x 560, two bytes per texel (RG8). */
|
|
export const areaTexBytes: Uint8Array = decode(AREA_B64);
|
|
/** \`searchTexBytes\`: 64 x 16, one byte per texel (R8). */
|
|
export const searchTexBytes: Uint8Array = decode(SEARCH_B64);
|
|
`,
|
|
);
|
|
console.log(`smaa_base ${smaa.length} chars, area ${area.length} B, search ${search.length} B`);
|