Skip to content

Commit 7c2c333

Browse files
committed
Fix more tests
1 parent 1deab4c commit 7c2c333

File tree

7 files changed

+163
-44
lines changed

7 files changed

+163
-44
lines changed

lib/descriptor.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
var extend = require('xtend/mutable');
88
var types = require('./types');
9-
9+
var pick = require('pick-by-alias');
1010

1111
/**
1212
* Constructor of descriptor - a result of mapping a glsl node to js.
@@ -40,12 +40,7 @@ function Descriptor (str, options) {
4040

4141
//take over options
4242
if (options) {
43-
descriptor.type = options.type;
44-
descriptor.components = options.components;
45-
descriptor.visible = options.visible;
46-
descriptor.complexity = options.complexity;
47-
descriptor.include = options.include;
48-
descriptor.optimize = options.optimize;
43+
extend(descriptor, pick(options, ['type', 'components', 'visible', 'complexity', 'include', 'optimize']))
4944
}
5045

5146
//in case of undefined complexity we should opt out for average value

lib/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ GLSL.prototype.process = function (node, arg) {
235235

236236
this.addInclude(result.include)
237237

238-
239238
//invoke end
240239
if (startCall) {
241240
this.started = false
@@ -703,14 +702,20 @@ GLSL.prototype.transforms = {
703702
}
704703

705704
throw Error('Unknown operator ' + node.data)
706-
707-
return Descriptor(null)
708705
},
709706

710707
expr: function (node) {
711-
var result = node.children.map(this.process, this).join('')
708+
var complexity = 0
712709

713-
return Descriptor(result)
710+
var result = node.children.map(function (n) {
711+
var res = this.process(n)
712+
complexity += res.complexity;
713+
return res
714+
}, this).join('')
715+
716+
result = Descriptor(result, {complexity: complexity})
717+
718+
return result
714719
},
715720

716721
precision: function () {
@@ -773,6 +778,7 @@ GLSL.prototype.transforms = {
773778

774779
return: function (node) {
775780
var expr = this.process(node.children[0])
781+
776782
var result
777783
var scope = this.scopes[this.currentScope]
778784
if (scope.outArgs) {
@@ -809,8 +815,6 @@ GLSL.prototype.transforms = {
809815
},
810816

811817
binary: function (node) {
812-
var result = ''
813-
814818
var leftNode = node.children[0]
815819
var rightNode = node.children[1]
816820
var left = this.process(leftNode)
@@ -1035,7 +1039,8 @@ GLSL.prototype.transforms = {
10351039
//+x
10361040
return Descriptor(str)
10371041
}
1038-
return Descriptor(node.data + str, {type: str.type, complexity: complexity})
1042+
1043+
return this.processOperation(null, str, node.data)
10391044
},
10401045

10411046
//gl_Position, gl_FragColor, gl_FragPosition etc
@@ -1052,6 +1057,7 @@ GLSL.prototype.transforms = {
10521057
var argTypes = argValues.map(function (arg) {
10531058
return arg.type
10541059
}, this)
1060+
10551061
//if first node is an access, like a.b() - treat special access-call case
10561062
if (node.children[0].data === '.') {
10571063
var methodNode = node.children[0].children[1]

lib/operators.js

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
var Descriptor = require('./descriptor');
99

10-
var floatRE = /^-?[0-9]*(?:.[0-9]+)?(?:e-?[0-9]+)?$/i;
10+
var floatRE = RegExp('^' + require('float-regex').source + '$')
1111

1212
var operators = processOperation.operators = {
1313
'*': 'multiply',
@@ -42,18 +42,55 @@ var opComplexity = {
4242
'/': 2
4343
}
4444

45-
var opsRE = /\*|\+|\-|\/|\%|\<|\=|\>|\&|\||\!|\^|\~/;
46-
47-
4845
/**
4946
* Return rendered operation
5047
*/
5148
function processOperation (left, right, operator) {
5249
var self = this;
53-
var leftType = left.type;
50+
var leftType = left && left.type;
5451
var rightType = right.type;
5552
var operatorName = operators[operator];
5653

54+
//0. unary: i++, -x
55+
if (!left) {
56+
// scalar
57+
if (this.types[rightType].length === 1) {
58+
var a = null, b = right;
59+
var res = Descriptor(calculate(a, b, operator), {
60+
components: [calculate(a, b, operator)],
61+
type: leftType,
62+
complexity: b.complexity + (opComplexity[operator] || 1)
63+
});
64+
return res
65+
}
66+
67+
68+
// complex
69+
var outType = rightType;
70+
var vec = right;
71+
var scalar = left;
72+
var l = this.types[outType].length;
73+
if (/mat/.test(outType)) l *= this.types[this.types[outType].type].length;
74+
var operands = [];
75+
for (var i = 0; i < l; i++) {
76+
if (this.types[rightType].length == 1) {
77+
var rightOp = right, leftOp = left.components[i];
78+
}
79+
else {
80+
var rightOp = right.components[i], leftOp = left;
81+
}
82+
operands.push(calculate(leftOp, rightOp, operator));
83+
}
84+
85+
var calcStr = this.types[rightType].length == 1 ? calculate('_', 'this', operator) : calculate('this', '_', operator);
86+
return Descriptor(
87+
`${vec}.map(function (_) {return ${calcStr};}, ${scalar})`, {
88+
components: operands,
89+
type: outType,
90+
complexity: vec.complexity + l * (2) + 1
91+
});
92+
}
93+
5794
//1. scalar vs scalar
5895
if (this.types[leftType].length == 1 && this.types[rightType].length == 1) {
5996
var a = left, b = right;
@@ -86,6 +123,7 @@ function processOperation (left, right, operator) {
86123

87124
if (scalar.optimize) {
88125
var calcStr = this.types[rightType].length == 1 ? calculate('_', scalar, operator) : calculate(scalar, '_', operator);
126+
89127
return Descriptor(
90128
`${vec}.map(function (_) {return ${calcStr};})`, {
91129
components: operands,
@@ -263,23 +301,40 @@ function processOperation (left, right, operator) {
263301
opResult = eval(`${left} ${operator} ${right}`);
264302
}
265303

304+
// normalize 0 values
305+
if (floatRE.test(left)) {
306+
left = Descriptor(left)
307+
left.value = parseFloat(left)
308+
}
309+
if (floatRE.test(right)) {
310+
right = Descriptor(right)
311+
right.value = parseFloat(right)
312+
}
313+
266314
//handle ridiculous math cases like x + 0, x * 0, x + 1
267315
if (operator == '+' || operator == '-') {
268316
//0 + x
269-
if (left == 0) opResult = right;
317+
if (!left || left.value === 0) {
318+
if (operator == '-') opResult = '-' + right;
319+
else opResult = right;
320+
}
270321

271322
//x + 0
272323
if (right == 0) opResult = left;
273324
}
274325
else if (operator == '*') {
275326
//0 * x
276-
if (left == 0 || right == 0) opResult = 0;
327+
if (left == 0 || left.value === 0 || right == 0) opResult = 0;
277328

278329
//1 * x
279-
else if (parseFloat(left) === 1) opResult = right;
330+
else if (left.value === 1) opResult = right;
280331

281332
//x * 1
282-
else if (parseFloat(right) === 1) opResult = left;
333+
else if (right.value === 1) opResult = left;
334+
}
335+
else if (operator == '--' || operator == '++') {
336+
if (left) opResult = left + operator
337+
else if (right) opResult = operator + right
283338
}
284339

285340
if (opResult == null) {
@@ -302,8 +357,8 @@ function processOperation (left, right, operator) {
302357
}
303358

304359
opResult = Descriptor(opResult, {
305-
complexity: 1 + left.complexity||0 + right.complexity||0,
306-
optimize: left.optimize !== false && right.optimize !== false
360+
complexity: 1 + ((left&&left.complexity)||0) + (right.complexity||0),
361+
optimize: (left&&left.optimize) !== false && right.optimize !== false
307362
});
308363

309364
return opResult;

lib/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ function createVec3 (type, vecType) {
225225
var components = [], map = ``, include;
226226

227227
//map type, if input args are of diff type (unlikely required)
228-
if (!subType(x.components[0].type, type) || !subType(y.components[0].type, type) || !subType(z.components[0].type, type) ) {
228+
if (x.components && (!subType(x.components[0].type, type) || !subType(y.components[0].type, type) || !subType(z.components[0].type, type))) {
229229
map = `.map(${type})`;
230230
include = type;
231231
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
"main": "index.js",
66
"dependencies": {
77
"array-flatten": "^2.0.0",
8+
"float-regex": "^1.0.0",
89
"glsl-parser": "^2.0.1",
910
"glsl-tokenizer": "^2.1.5",
1011
"inherits": "^2.0.1",
12+
"pick-by-alias": "^1.2.0",
1113
"prepr": "^1.1.2",
1214
"xtend": "^4.0.1"
1315
},
1416
"devDependencies": {
1517
"almost-equal": "^1.1.0",
16-
"cln": "^1.0.0",
18+
"cln": "^1.1.0",
1719
"gl-matrix": "^2.3.2",
1820
"glslify": "^5.0.2",
1921
"glslify-promise": "^1.0.2",

test/index.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,30 +466,67 @@ test(`float x = vec2(1, 2).xy;`, function (t) {
466466
`))
467467
t.end()
468468
})
469-
test('vec2', function (t) {
470-
t.deepEqual(eval('vec2(1, 2).x;'), 1);
471-
t.deepEqual(eval('vec2(1, 2).xy;'), [1,2]);
472-
t.deepEqual(eval('vec2(1, 2).yy;'), [2,2]);
469+
470+
test('float x = 1.0; float y = -x;', function (t) {
471+
var compile = GLSL();
472+
473+
t.equal(clean(compile(t.name)), clean(`
474+
var x = 1.0;
475+
var y = -x;
476+
`))
473477
t.end()
474478
})
475-
test('vec3', function (t) {
476-
t.deepEqual(eval('vec3(1, 2, 3).x;'), 1);
477-
t.deepEqual(eval('vec3(1, 2, 3).xy;'), [1,2]);
478-
t.deepEqual(eval('vec3(1, 2, 3).xyz;'), [1,2,3]);
479-
t.deepEqual(eval('vec3(1, 2, 3).zzz;'), [3,3,3]);
479+
test('vec3 x = vec3(1.0); vec3 y = -x;', function (t) {
480+
var compile = GLSL();
481+
482+
t.equal(clean(compile(t.name)), clean(`
483+
var x = [1, 1, 1];
484+
var y = [-x[0], -x[1], -x[2]];
485+
`))
480486
t.end()
481487
})
482-
test('vec4', function (t) {
483-
t.deepEqual(eval('vec4(1, 2, 3, 4).x;'), 1);
484-
t.deepEqual(eval('vec4(1, 2, 3, 4).xy;'), [1,2]);
485-
t.deepEqual(eval('vec4(1, 2, 3, 4).xyz;'), [1,2,3]);
486-
t.deepEqual(eval('vec4(1, 2, 3, 4).xyzw;'), [1,2,3,4]);
487-
t.deepEqual(eval('vec4(1, 2, 3, 4).wwww;'), [4,4,4,4]);
488-
t.deepEqual(eval('vec4(1, 2, 3, 4).wzyx;'), [4,3,2,1]);
488+
test('vec3 x = vec3(1.0); vec3 y = 0. - x;', function (t) {
489+
var compile = GLSL();
490+
491+
t.equal(clean(compile(t.name)), clean(`
492+
var x = [1, 1, 1];
493+
var y = [-x[0], -x[1], -x[2]];
494+
`))
489495
t.end()
490496
})
497+
test('float s = 0.; --s;', function (t) {
498+
var compile = GLSL();
499+
500+
t.equal(clean(compile(t.name)),
501+
clean`
502+
var s = 0.;
503+
--s;
504+
`)
505+
506+
t.end()
507+
})
508+
test('float s = 0.; s--;', function (t) {
509+
var compile = GLSL();
491510

511+
t.equal(clean(compile(t.name)),
512+
clean`
513+
var s = 0.;
514+
s--;
515+
`)
492516

517+
t.end()
518+
})
519+
test('vec3 f() { return vec3(3.); } vec3 x = -f();', function (t) {
520+
t.equal(clean(compile(t.name)),
521+
clean`
522+
function f () {
523+
return [3., 3., 3.];
524+
};
525+
var x = f().map(function (_) {return this - _;}, null);
526+
`)
527+
528+
t.end()
529+
})
493530

494531
// `
495532
// vec2 pos;

test/vectors.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ test('vec4(vec2, vec2)', function (t) {
140140
t.end()
141141
})
142142

143+
test('vec2 swizzles', function (t) {
144+
t.deepEqual(eval('vec2(1, 2).x;'), 1);
145+
t.deepEqual(eval('vec2(1, 2).xy;'), [1,2]);
146+
t.deepEqual(eval('vec2(1, 2).yy;'), [2,2]);
147+
t.end()
148+
})
149+
test('vec3 swizzles', function (t) {
150+
t.deepEqual(eval('vec3(1, 2, 3).x;'), 1);
151+
t.deepEqual(eval('vec3(1, 2, 3).xy;'), [1,2]);
152+
t.deepEqual(eval('vec3(1, 2, 3).xyz;'), [1,2,3]);
153+
t.deepEqual(eval('vec3(1, 2, 3).zzz;'), [3,3,3]);
154+
t.end()
155+
})
156+
test('vec4 swizzles', function (t) {
157+
t.deepEqual(eval('vec4(1, 2, 3, 4).x;'), 1);
158+
t.deepEqual(eval('vec4(1, 2, 3, 4).xy;'), [1,2]);
159+
t.deepEqual(eval('vec4(1, 2, 3, 4).xyz;'), [1,2,3]);
160+
t.deepEqual(eval('vec4(1, 2, 3, 4).xyzw;'), [1,2,3,4]);
161+
t.deepEqual(eval('vec4(1, 2, 3, 4).wwww;'), [4,4,4,4]);
162+
t.deepEqual(eval('vec4(1, 2, 3, 4).wzyx;'), [4,3,2,1]);
163+
t.end()
164+
})
165+
166+
143167

144168

145169

0 commit comments

Comments
 (0)