Skip to content

Commit f729d77

Browse files
committed
feat(glsl): autodetect type over multiple snippets
1 parent 593d1fc commit f729d77

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

src/glsl/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ ${e.message}`);
465465
}
466466
}
467467

468-
export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false } = {}) {
469-
const { asts, js, originals, keys } = args.reduce((mem, { [ORIGINALS]: originals, ast }) => {
468+
export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false, ast: astOn = false } = {}) {
469+
const options = { ...TREE_SETTINGS, scope: {} };
470+
const { asts, js, originals, keys } = args.reduce((mem, { [ORIGINALS]: originals }) => {
470471
if (jsOn) {
471472
originals.forEach((original) => {
472473
mem.js = sim(original, { BuiltIn }, mem.keys);
@@ -476,8 +477,14 @@ export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false } = {}) {
476477
});
477478
});
478479
}
480+
if (glslOn || astOn) {
481+
originals.forEach((fun) => {
482+
const str = fun.toString();
483+
const ast = parse(str, options);
484+
mem.asts.push(ast);
485+
});
486+
}
479487
mem.originals.push(...originals);
480-
mem.asts.push(ast);
481488
return mem;
482489
}, { asts: [], js: undefined, keys: {}, originals: [] });
483490

test/glsl/index.js

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -384,29 +384,54 @@ float bar = baz();
384384
assert.equal(glsl.trim(), expected.trim());
385385
});
386386

387-
// it('works with joining chunks auto detection', () => {
388-
// const one = buildGLSL(() => {
389-
// let foo = uniform(vec2);
390-
// });
391-
//
392-
// const two = buildGLSL(() => {
393-
// let bar = vec2(() => {
394-
// let res = foo;
395-
// return res;
396-
// });
397-
// });
398-
//
399-
// const { glsl } = joinGLSL([one, two]);
400-
//
401-
// const expected = `
402-
// uniform vec2 foo;
403-
// vec2 bar() {
404-
// \tvec2 res = foo;
405-
// \treturn res;
406-
// }
407-
// `;
408-
//
409-
//
410-
// assert.equal(glsl.trim(), expected.trim());
411-
// });
387+
it('works with joining chunks auto detection', () => {
388+
const one = buildGLSL(() => {
389+
let foo = uniform(vec2);
390+
});
391+
392+
const two = buildGLSL(() => {
393+
let bar = vec2(() => {
394+
let res = foo;
395+
return res;
396+
});
397+
});
398+
399+
const { glsl } = joinGLSL([one, two]);
400+
401+
const expected = `
402+
uniform vec2 foo;
403+
vec2 bar() {
404+
\tvec2 res = foo;
405+
\treturn res;
406+
}
407+
`;
408+
409+
410+
assert.equal(glsl.trim(), expected.trim());
411+
});
412+
413+
it('works when joining already joined properties', () => {
414+
const one = buildGLSL(() => {
415+
const foo = vec2(1.0);
416+
});
417+
418+
const two = buildGLSL(() => {
419+
const bar = vec2(1.0);
420+
});
421+
422+
const three = buildGLSL(() => {
423+
let baz = input(vec2(1.0));
424+
});
425+
426+
const joined = joinGLSL([one, two]);
427+
const { glsl } = joinGLSL([joined, three]);
428+
429+
const expected = `
430+
const vec2 foo = vec2(1.0);
431+
const vec2 bar = vec2(1.0);
432+
in vec2 baz = vec2(1.0);
433+
`;
434+
435+
assert.equal(glsl.trim(), expected.trim());
436+
});
412437
});

test/glsl/sim.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('glsl tests', () => {
1515
this.x = x;
1616
this.y = y;
1717
}
18+
1819
valueOf() {
1920
return 5;
2021
}
@@ -26,6 +27,7 @@ describe('glsl tests', () => {
2627
this.y = y;
2728
this.z = z;
2829
}
30+
2931
valueOf() {
3032
return 5;
3133
}
@@ -38,13 +40,16 @@ describe('glsl tests', () => {
3840
this.z = z;
3941
this.w = w;
4042
}
43+
4144
valueOf() {
4245
return 5;
4346
}
4447
}
4548

4649
const jsOptions = {
47-
Vec2, Vec3, Vec4,
50+
Vec2,
51+
Vec3,
52+
Vec4,
4853
calc: calcSim
4954
};
5055
buildGLSL(() => { return {}; }, { glsl: false, js: jsOptions });
@@ -578,20 +583,20 @@ vec4 bar(vec2 x) {
578583
let foo = output(vec2(0.0));
579584
let getFoo = vec2(() => {
580585
return foo;
581-
})
586+
});
582587
let main = () => {
583588
foo = vec2(1.0);
584-
}
589+
};
585590
return { getFoo, main };
586591
};
587592
const shader2 = ({ output, vec2 }) => {
588593
let bar = output(vec2(0.0));
589594
let getBar = vec2(() => {
590595
return bar;
591-
})
596+
});
592597
let main = () => {
593598
bar = vec2(3.0);
594-
}
599+
};
595600
return { getBar, main };
596601
};
597602
const one = buildGLSL(shader1, { glsl: false });

0 commit comments

Comments
 (0)