Skip to content

Commit 0fbc3da

Browse files
committed
feat(glsl): merge multiple main functions
1 parent 7925f7f commit 0fbc3da

File tree

2 files changed

+80
-33
lines changed

2 files changed

+80
-33
lines changed

src/glsl/index.js

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,28 @@ function handleBody(body, tabCount = 0) {
401401
// }).flat(1);
402402
// }
403403

404+
function handeAst(node) {
405+
const { body } = node.body[0].expression;
406+
407+
body.body = body.body.filter(({ type }) => (type !== 'ReturnStatement'));
408+
// body.body = handleGenTypes(body.body);
409+
let sh = handleBody(body);
410+
411+
sh = sh.split('\n').map((s) => {
412+
const last = s[s.length - 1];
413+
if (!s.length) {
414+
return s;
415+
}
416+
if (last === '{' || last === '}' || last === ';' || last === '/') {
417+
return s;
418+
}
419+
return `${s};`;
420+
}).join('\n');
421+
422+
// console.log('\n' + sh + '\n');
423+
return `${sh}\n`;
424+
}
425+
404426
export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined } = {}) {
405427
// console.log('fun', fun.toString());
406428

@@ -415,25 +437,7 @@ export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined }
415437
}
416438

417439
if (glsl) {
418-
const { body } = node.body[0].expression;
419-
420-
body.body = body.body.filter(({ type }) => (type !== 'ReturnStatement'));
421-
// body.body = handleGenTypes(body.body);
422-
let sh = handleBody(body);
423-
424-
sh = sh.split('\n').map((s) => {
425-
const last = s[s.length - 1];
426-
if (!s.length) {
427-
return s;
428-
}
429-
if (last === '{' || last === '}' || last === ';' || last === '/') {
430-
return s;
431-
}
432-
return `${s};`;
433-
}).join('\n');
434-
435-
// console.log('\n' + sh + '\n');
436-
text = `${sh}\n`;
440+
text = handeAst(node);
437441
}
438442

439443
if (js) {
@@ -462,17 +466,7 @@ ${e.message}`);
462466
}
463467

464468
export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false } = {}) {
465-
const { glsls, js, originals, keys } = args.reduce((mem, { glsl, [ORIGINALS]: originals }) => {
466-
if (!glsl && glslOn) {
467-
if (originals.length === 1) {
468-
glsl = buildGLSL(originals[0], { glsl: true }).glsl;
469-
} else {
470-
glsl = joinGLSL(originals, { glsl: true }).glsl;
471-
}
472-
}
473-
if (glsl) {
474-
mem.glsls.push(glsl);
475-
}
469+
const { asts, js, originals, keys } = args.reduce((mem, { [ORIGINALS]: originals, ast }) => {
476470
if (jsOn) {
477471
originals.forEach((original) => {
478472
mem.js = sim(original, { BuiltIn }, mem.keys);
@@ -483,10 +477,39 @@ export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false } = {}) {
483477
});
484478
}
485479
mem.originals.push(...originals);
480+
mem.asts.push(ast);
486481
return mem;
487-
}, { glsls: [], js: undefined, keys: {}, originals: [] });
482+
}, { asts: [], js: undefined, keys: {}, originals: [] });
483+
484+
let glsl;
485+
if (glslOn) {
486+
const { body, mains } = asts.reduce((mem, node) => {
487+
let { body: { body } } = node.body[0].expression;
488+
let main = body[0].declarations.find(({ id }) => (id && id.name === 'main'));
489+
body[0].declarations = body[0].declarations.filter(({ id }) => (!id || id.name !== 'main'));
490+
491+
mem.body.push(...body);
492+
if (main) {
493+
mem.mains.push(main);
494+
}
495+
return mem;
496+
}, { body: [], mains: [] });
497+
498+
if (mains.length) {
499+
const initBody = mains.reduce((res, { init: { body } }) => {
500+
res.push(...body.body);
501+
return res;
502+
}, []);
503+
504+
const main = mains[0];
505+
main.init.body.body = initBody;
506+
507+
body.push({ declarations: [main], type: 'VariableDeclaration', kind: 'let' });
508+
}
509+
510+
glsl = handeAst({ body: [{ expression: { body: { body } } }] });
511+
}
488512

489-
const glsl = glsls.length ? glsls.join('\n') : undefined;
490513
if (js) {
491514
Object.entries(keys).forEach(([key, value]) => {
492515
if (!js[key]) {

test/glsl/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ vec2[2] foo; foo[0] = vec2(1.0, 2.0); foo[1] = vec(3.0, 4.0);
246246

247247
const expected = `
248248
uniform vec2 foo;
249-
250249
vec2 bar(vec2 x, float y) {
251250
\tx = normalize(x);
252251
\treturn vec2(x.x, y);
@@ -338,4 +337,29 @@ void baz(vec2 x, float y) {
338337

339338
assert.equal(glsl.trim(), expected.trim());
340339
});
340+
341+
it('works with merging multiple main functions', () => {
342+
const one = buildGLSL(() => {
343+
let main = fun(() => {
344+
const foo = vec2(1.0);
345+
});
346+
});
347+
348+
const two = buildGLSL(() => {
349+
let main = fun(() => {
350+
const bar = vec2(1.0);
351+
});
352+
});
353+
354+
const { glsl } = joinGLSL([one, two]);
355+
356+
const expected = `
357+
void main() {
358+
\tconst vec2 foo = vec2(1.0);
359+
\tconst vec2 bar = vec2(1.0);
360+
}
361+
`;
362+
363+
assert.equal(glsl.trim(), expected.trim());
364+
});
341365
});

0 commit comments

Comments
 (0)