Skip to content

Commit d0bb1bf

Browse files
committed
feat(glsl): support for glsl 3.0 in and out
1 parent 92f6a41 commit d0bb1bf

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

src/glsl/builtin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,22 @@ export class BuiltIn {
248248
return undefined;
249249
}
250250

251+
output() {
252+
return undefined;
253+
}
254+
251255
uniform() {
252256
return readOnlyView(BLOCKED);
253257
}
254258

259+
attribute() {
260+
return readOnlyView(BLOCKED);
261+
}
262+
263+
input() {
264+
return readOnlyView(BLOCKED);
265+
}
266+
255267
int(...args) {
256268
return checkType(args, Number) ?? args[0];
257269
}

src/glsl/index.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const ORIGINALS = Symbol('Originals');
1010
const qualifiers = [
1111
'varying',
1212
'uniform',
13-
'attribute'
13+
'attribute',
14+
'input',
15+
'output'
1416
];
1517

1618
const TREE_SETTINGS = {
@@ -167,11 +169,21 @@ function retSta({ argument }) {
167169
return `return ${handleNode(argument)}`;
168170
}
169171

172+
function prepareQualifier(qualifier) {
173+
if (qualifier === 'input') {
174+
return 'in';
175+
}
176+
if (qualifier === 'output') {
177+
return 'out';
178+
}
179+
return qualifier;
180+
}
181+
170182
function ident(node) {
171183
let { name, typeAnnotation, qualifier } = node;
172184
let q = '';
173185
if (qualifier) {
174-
q = `${qualifier} `;
186+
q = `${prepareQualifier(qualifier)} `;
175187
}
176188
let t = '';
177189
if (typeAnnotation) {
@@ -305,7 +317,7 @@ function handleAssign(node, kind) {
305317

306318
let qualifier = '';
307319
if (q) {
308-
qualifier = `${q} `;
320+
qualifier = `${prepareQualifier(q)} `;
309321
} else if (kind === 'const') {
310322
qualifier = 'const ';
311323
}
@@ -370,6 +382,25 @@ function handleBody(body, tabCount = 0) {
370382
.join('\n');
371383
}
372384

385+
// function replaceGenType(node) {
386+
// console.log('replaceGenType', node);
387+
// return node;
388+
// }
389+
//
390+
// function handleGenTypes(body) {
391+
// return body.map((node) => {
392+
// if (node.right && node.right.init && node.right.init.returnType === 'genType') {
393+
// return [
394+
// replaceGenType(JSON.parse(JSON.stringify(node)), 'float'),
395+
// replaceGenType(JSON.parse(JSON.stringify(node)), 'vec2'),
396+
// replaceGenType(JSON.parse(JSON.stringify(node)), 'vec3'),
397+
// replaceGenType(JSON.parse(JSON.stringify(node)), 'vec4')
398+
// ];
399+
// }
400+
// return [node];
401+
// }).flat(1);
402+
// }
403+
373404
export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined } = {}) {
374405
// console.log('fun', fun.toString());
375406

@@ -387,7 +418,7 @@ export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined }
387418
const { body } = node.body[0].expression;
388419

389420
body.body = body.body.filter(({ type }) => (type !== 'ReturnStatement'));
390-
421+
// body.body = handleGenTypes(body.body);
391422
let sh = handleBody(body);
392423

393424
sh = sh.split('\n').map((s) => {

test/glsl/index.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ vec2 bar(vec2 x, float y) {
270270
});
271271

272272
it('const handling works.', () => {
273-
const { glsl, ast } = buildGLSL(() => {
273+
const { glsl } = buildGLSL(() => {
274274
const foo = uniform(vec2(0.0));
275275
const bar = vec2(0.0);
276276
let baz = vec2(0.0);
@@ -297,4 +297,45 @@ void fnFVoid() {
297297
`;
298298
assert.equal(glsl.trim(), expected.trim());
299299
});
300+
301+
it('interpret gentype works.', () => {
302+
const { glsl, ast } = buildGLSL(() => {
303+
const foo = gentype((x = gentype()) => {
304+
let res = gentype(x * 5.0);
305+
return res;
306+
});
307+
});
308+
309+
const expected = `
310+
gentype foo(gentype x) {
311+
\tgentype res = x * 5.0;
312+
\treturn res;
313+
}
314+
`;
315+
assert.equal(glsl.trim(), expected.trim());
316+
});
317+
318+
it('works glsl 3.0 in and out.', () => {
319+
const { glsl } = buildGLSL(() => {
320+
let foo = input(vec2);
321+
322+
let bar = output(vec2);
323+
324+
let baz = (x = vec2(), y = float()) => {
325+
x = normalize(x);
326+
bar = x;
327+
};
328+
});
329+
330+
const expected = `
331+
in vec2 foo;
332+
out vec2 bar;
333+
void baz(vec2 x, float y) {
334+
\tx = normalize(x);
335+
\tbar = x;
336+
}
337+
`;
338+
339+
assert.equal(glsl.trim(), expected.trim());
340+
});
300341
});

0 commit comments

Comments
 (0)