Skip to content

Commit 7705a64

Browse files
committed
feat(glsl): support for const declaration
1 parent 75e2914 commit 7705a64

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"no-tabs": "off",
2828
"no-underscore-dangle": "off",
2929
"prefer-spread": "off",
30-
"no-shadow": "off"
30+
"no-shadow": "off",
31+
"no-unused-vars": "warn"
3132
}
3233
}

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ this can be archived with typescript, but not at runtime.
4040
- [ ] type inference
4141
- [ ] jstoGLSL transpiler great for performance
4242
- [x] generate valid glsl code
43-
- [ ] glsl polyfills
43+
- [ ] js transformations to glsl
4444
- [x] modulo as arithmetic operator
4545
- [x] pow as arithmetic operator
46-
- [ ] symbols and enums
46+
- [ ] variable index expression for vectors
4747
- [ ] destruct
48+
- [ ] symbols and enums
4849
- [ ] functional structs
4950
- [ ] inline functions for map, reduce, filter
5051
- [x] jstoGLSL simulator great for testing
51-
- [x] painting slowly to image buffer
52+
- [x] painting (slowly) to image buffer
5253
- [x] vector arithmetic (component wise) operator support
53-
- [ ] matrix algebraic operator support
54+
- [x] partly matrix algebraic operator support
5455
- [ ] multithreading
5556
- [ ] typesafety
5657

src/glsl/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ function calExp(node) {
256256
return `${name}(${args.map(handleNode).join(', ')})`;
257257
}
258258

259-
function varDec({ declarations }) {
260-
return declarations.map(handleAssign).join('; ');
259+
function varDec({ declarations, kind }) {
260+
return declarations.map((declaration) => handleAssign(declaration, kind)).join('; ');
261261
}
262262

263263
function throwError(msg, node) {
@@ -266,7 +266,7 @@ function throwError(msg, node) {
266266
throw error;
267267
}
268268

269-
function handleAssign(node) {
269+
function handleAssign(node, kind) {
270270
const { init, id } = node;
271271
let { name, typeAnnotation, qualifier: q } = id;
272272

@@ -295,12 +295,22 @@ function handleAssign(node) {
295295
allocation = handleAlloc(init, typeAnnotation, name);
296296
}
297297
}
298+
if (!typeAnnotation) {
299+
throwError(`
300+
handleAssign() 2 no type defined for ${id.name} allocation ${allocation}
301+
${JSON.stringify(node, null, '\t')}
302+
`, id);
303+
}
298304

299305
let qualifier = '';
300306
if (q) {
301307
qualifier = `${q} `;
302308
}
303-
return `${qualifier}${typeAnnotation} ${name}${allocation}`;
309+
let k = '';
310+
if (kind === 'const') {
311+
k = 'const ';
312+
}
313+
return `${k}${qualifier}${typeAnnotation} ${name}${allocation}`;
304314
}
305315

306316
function handleAlloc(init, typeAnnotation, name) {

test/glsl/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,32 @@ vec2 bar(vec2 x, float y) {
257257
assert.equal(glsl.trim(), expected.trim());
258258
});
259259

260+
261+
it('throw an error when type is forgotten.', () => {
262+
assert.throws(() => {
263+
buildGLSL(() => {
264+
let bar = vec2((x = vec2()) => {
265+
let y = normalize(x) * 10.0;
266+
return vec2(x.x, y);
267+
});
268+
});
269+
});
270+
});
271+
272+
it('const handling works.', () => {
273+
const { glsl, ast } = buildGLSL(() => {
274+
const foo = uniform(vec2(0.0));
275+
const bar = vec2(0.0);
276+
let baz = vec2(0.0);
277+
});
278+
279+
console.log('ast', ast.body[0].expression.body.body);
280+
281+
const expected = `
282+
const uniform vec2 foo = vec2(0.0);
283+
const vec2 bar = vec2(0.0);
284+
vec2 baz = vec2(0.0);
285+
`;
286+
assert.equal(glsl.trim(), expected.trim());
287+
});
260288
});

test/glsl/sim.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ float action(vec2 one, vec2 two) {
202202
assert.equal(result.w, 1);
203203
});
204204

205+
it('works fine with glsl flexible vector factories with singlee argument', () => {
206+
const shader = ({ vec4, vec2 }) => {
207+
let bar = vec4((x = vec2()) => {
208+
return vec4(vec4(x, 1.0, 1.0));
209+
});
210+
return { bar };
211+
};
212+
const { js } = buildGLSL(shader, { js: true, glsl: false });
213+
214+
const { bar } = js;
215+
216+
const result = bar(new Vec2(3, 2));
217+
assert.equal(result.x, 3);
218+
assert.equal(result.y, 2);
219+
assert.equal(result.z, 1);
220+
assert.equal(result.w, 1);
221+
});
222+
205223
it('works fine with glsl asin multiple types.', () => {
206224
const shader = ({ asin, vec2 }) => {
207225
let bar = vec2((x = vec2()) => {
@@ -252,6 +270,22 @@ float action(vec2 one, vec2 two) {
252270
assert.equal(result.w, 1);
253271
});
254272

273+
it('works when using mix.', () => {
274+
const shader = ({ mix, vec2 }) => {
275+
let bar = vec2(() => {
276+
return mix(vec2(1.0, 0.0), vec2(3.0, 6.0), 0.5);
277+
});
278+
return { bar };
279+
};
280+
const { js } = buildGLSL(shader, { js: true, glsl: false });
281+
282+
const { bar } = js;
283+
284+
const result = bar();
285+
assert.equal(result.x, 2);
286+
assert.equal(result.y, 3);
287+
});
288+
255289
it('works fine with sampler2D from array buffer.', () => {
256290

257291
const buffer = new Uint8ClampedArray(2 * 1 * 4);

0 commit comments

Comments
 (0)