Skip to content

Commit 1deab4c

Browse files
committed
Refactor tests
1 parent da38843 commit 1deab4c

19 files changed

+2579
-2791
lines changed

lib/stdlib.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ function fract (x) {
230230
function mod (x, y) {
231231
if (x.length) {
232232
if (y.length) return x.map(function (x, i) {
233+
if (x === 0 || y[i] === 0) return 0
233234
return x % y[i];
234235
});
235236
return x.map(function (x, i) {
237+
if (x === 0 || y === 0) return 0
236238
return x % y;
237239
});
238240
}
@@ -290,6 +292,7 @@ function mix (x, y, a) {
290292
}
291293

292294
function step (edge, x) {
295+
if (!x && !edge) return 0
293296
if (x.length) {
294297
if (edge.length) return x.map(function (x, i) {
295298
return step(edge[i], x);
@@ -306,6 +309,7 @@ step.type = function (node) {
306309
}
307310

308311
function smoothstep (edge0, edge1, x) {
312+
if (!x && !edge0 && !edge1) return 0
309313
if (x.length) {
310314
if (edge0.length) return x.map(function (x, i) {
311315
return smoothstep(edge0[i], edge1[i], x);
@@ -314,7 +318,6 @@ function smoothstep (edge0, edge1, x) {
314318
return smoothstep(edge0, edge1, x);
315319
});
316320
}
317-
318321
var t = Math.min(Math.max((x - edge0) / (edge1 - edge0), 0.0), 1.0);
319322
return t * t * (3.0 - 2.0 * t);
320323
}

lib/types.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ exports.void = function () {
3232
return '';
3333
}
3434

35-
3635
function bool (node) {
3736
if (node == null) return Descriptor(false, {type: 'bool', complexity: 0});
3837

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"ndarray": "^1.0.18",
2323
"stream-array": "^1.1.1",
2424
"string-to-stream": "^1.0.1",
25-
"tst": "^1.3.2"
25+
"tape": "^4.9.1",
26+
"to-float32": "^1.0.1"
2627
},
2728
"browserify": {
2829
"transform": [
@@ -38,9 +39,9 @@
3839
"lib": "./lib"
3940
},
4041
"scripts": {
41-
"test": "node --harmony test/index.js",
42+
"test": "node test/index.js",
4243
"test:browser": "budo test/index.js",
43-
"benchmark": "node --harmony --use-strict test/benchmark.js"
44+
"benchmark": "node test/benchmark.js"
4445
},
4546
"repository": {
4647
"type": "git",

test/api.js

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
var tokenize = require('glsl-tokenizer/string')
2+
var parse = require('glsl-parser/direct')
3+
var GLSL = require('../')
4+
var compile = GLSL.compile
5+
var TokenStream = require('glsl-tokenizer/stream')
6+
var ParseStream = require('glsl-parser/stream')
7+
var CompileStream = require('../stream')
8+
var test = require('tape')
9+
var StringStream = require('stream-array')
10+
var Sink = require('stream').Writable
11+
var eval = require('./util/eval')
12+
var clean = require('cln')
13+
var glsl = require('glslify')
14+
15+
16+
var compile = GLSL({})
17+
18+
19+
//examplary source, containing all possible tokens
20+
var source = `
21+
precision mediump float;
22+
attribute vec2 uv, xy = vec2(1);
23+
attribute vec4 color;
24+
varying vec4 fColor, twoColors[2];
25+
uniform vec2 uScreenSize = vec2(1,1);
26+
float coeff = 1.0, coeff2 = coeff + 1.0, a[2], b[3][2] = float[3](a, a, a);
27+
28+
int count (float num);
29+
30+
void main (void) {
31+
fColor = color;
32+
vec2 position = coeff * vec2(uv.x, -uv.y);
33+
position.x *= uScreenSize.y / uScreenSize.x;
34+
xy.xy *= uv.yx;
35+
gl_Position = vec4(position.yx / 2.0, 0, 1);
36+
gl_FragColor[0] = gl_FragCoord[0] / gl_Position.length();
37+
return;
38+
}
39+
40+
/* just a test function */
41+
int count (in float num) {
42+
int sum = 0;
43+
for (int i = 0; i < 10; i++) {
44+
sum += i;
45+
if (i > 4) continue;
46+
else break;
47+
48+
discard;
49+
}
50+
int i = 0;
51+
while (i < 10) {
52+
--sum;
53+
}
54+
do {
55+
sum += i < 5 ? (i > 2 ? 1 : 2) : 0;
56+
}
57+
while (i < 10);
58+
return sum;
59+
}
60+
`;
61+
62+
var result = `
63+
var uv = [0, 0], xy = [1, 1];
64+
var color = [0, 0, 0, 0];
65+
var fColor = [0, 0, 0, 0], twoColors = [[0, 0, 0, 0], [0, 0, 0, 0]];
66+
var uScreenSize = [1, 1];
67+
var coeff = 1.0, coeff2 = coeff + 1.0, a = [0, 0], b = [a, a, a];
68+
69+
function main () {
70+
fColor = color;
71+
var position = [coeff * uv[0], coeff * -uv[1]];
72+
position[0] *= uScreenSize[1] / uScreenSize[0];
73+
xy = [xy[0] * uv[1], xy[1] * uv[0]];
74+
gl_Position = [position[1] / 2.0, position[0] / 2.0, 0, 1];
75+
gl_FragColor[0] = gl_FragCoord[0] / 4;
76+
return;
77+
};
78+
79+
function count (num) {
80+
var sum = 0;
81+
for (var i = 0; i < 10; i++) {
82+
sum += i;
83+
if (i > 4) {
84+
continue;
85+
} else {
86+
break;
87+
};
88+
89+
discard();
90+
};
91+
var i = 0;
92+
while (i < 10) {
93+
--sum;
94+
};
95+
do {
96+
sum += i < 5 ? (i > 2 ? 1 : 2) : 0;
97+
} while (i < 10);
98+
return sum;
99+
};`;
100+
101+
var shortResult = `
102+
var uv = attributes['uv'], xy = attributes['xy'];
103+
var color = attributes['color'];
104+
var fColor = varying['fColor'], twoColors = varying['twoColors'];
105+
var uScreenSize = uniforms['uScreenSize'];
106+
var coeff = 1.0, coeff2 = coeff + 1.0, a = [0, 0], b = [a, a, a];
107+
108+
function main () {
109+
fColor = color;
110+
var position = [coeff * uv[0], coeff * -uv[1]];
111+
position[0] *= uScreenSize[1] / uScreenSize[0];
112+
xy = [xy[0] * uv[1], xy[1] * uv[0]];
113+
gl_Position = [position[1] / 2.0, position[0] / 2.0, 0, 1];
114+
gl_FragColor[0] = gl_FragCoord[0] / 4;
115+
return;
116+
};
117+
118+
function count (num) {
119+
var sum = 0;
120+
for (var i = 0; i < 10; i++) {
121+
sum += i;
122+
if (i > 4) {
123+
continue;
124+
} else {
125+
break;
126+
};
127+
128+
discard();
129+
};
130+
var i = 0;
131+
while (i < 10) {
132+
--sum;
133+
};
134+
do {
135+
sum += i < 5 ? (i > 2 ? 1 : 2) : 0;
136+
} while (i < 10);
137+
return sum;
138+
};
139+
`;
140+
141+
142+
test('Direct', function (t) {
143+
t.equal(clean(compile(source)).split('\n')[7], clean(result).split('\n')[7]);
144+
// t.equal(clean(compile(source)), clean(result));
145+
146+
t.end()
147+
});
148+
149+
test('Stream', function (t) {
150+
var res = '';
151+
152+
StringStream(source.split('\n').map(function(v){return v + '\n'}))
153+
.pipe(TokenStream())
154+
// .on('data', function (chunk) {
155+
// console.log(chunk);
156+
// })
157+
.pipe(ParseStream())
158+
.pipe(CompileStream())
159+
.on('end', function() {
160+
t.equal(clean(res), clean(result))
161+
t.end();
162+
})
163+
164+
//to release data
165+
.pipe(Sink({
166+
objectMode: true,
167+
write: function (data, enc, cb) {
168+
res += data + '\n';
169+
cb();
170+
}
171+
}))
172+
});
173+
174+
test('Detect attributes, uniforms, varying', function (t) {
175+
var compiler = new GLSL({
176+
attribute: function (name) { return `attributes['${name}']`;},
177+
uniform: function (name) { return `uniforms['${name}']`;},
178+
varying: function (name) { return `varying['${name}']`;}
179+
}).compiler;
180+
181+
var result = compiler.compile(source);
182+
183+
// t.equal(clean(result).split('\n')[5], clean(shortResult).split('\n')[5]);
184+
t.equal(clean(result), clean(shortResult));
185+
186+
t.deepEqual(Object.keys(compiler.attributes), ['uv', 'xy', 'color']);
187+
188+
t.deepEqual(Object.keys(compiler.varyings), ['fColor', 'twoColors']);
189+
190+
t.deepEqual(Object.keys(compiler.uniforms), ['uScreenSize']);
191+
192+
t.end()
193+
});

test/builtins.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
// `
3+
// if (intensity < 0.0)
4+
// discard;
5+
// `
6+
// `
7+
// // In the vertex language, the built-ins are intrinsically declared as:
8+
// in int gl_VertexID;
9+
// in int gl_InstanceID;
10+
// out gl_PerVertex {
11+
// vec4 gl_Position;
12+
// float gl_PointSize;
13+
// float gl_ClipDistance[];
14+
// };
15+
16+
17+
// // In the geometry language, the built-in variables are intrinsically declared as:
18+
// in gl_PerVertex {
19+
// vec4 gl_Position;
20+
// float gl_PointSize;
21+
// float gl_ClipDistance[];
22+
// } gl_in[];
23+
// in int gl_PrimitiveIDIn;
24+
// in int gl_InvocationID;
25+
// out gl_PerVertex {
26+
// vec4 gl_Position;
27+
// float gl_PointSize;
28+
// float gl_ClipDistance[];
29+
// };
30+
// out int gl_PrimitiveID;
31+
// out int gl_Layer;
32+
// out int gl_ViewportIndex;
33+
34+
35+
// // In the fragment language, built-in variables are intrinsically declared as:
36+
// in vec4 gl_FragCoord;
37+
// in bool gl_FrontFacing;
38+
// in float gl_ClipDistance[];
39+
// in vec2 gl_PointCoord;
40+
// in int gl_PrimitiveID;
41+
// in int gl_SampleID;
42+
// in vec2 gl_SamplePosition;
43+
// in int gl_SampleMaskIn[];
44+
// in int gl_Layer;
45+
// in int gl_ViewportIndex;
46+
// out float gl_FragDepth;
47+
// out int gl_SampleMask[];
48+
49+
50+
// //
51+
// // Implementation-dependent constants. The example values below
52+
// // are the minimum values allowed for these maximums.
53+
// //
54+
// const ivec3 gl_MaxComputeWorkGroupCount = { 65535, 65535, 65535 };
55+
// const ivec3 gl_MaxComputeWorkGroupSize = { 1024, 1024, 64 };
56+
// const int gl_MaxComputeUniformComponents = 1024;
57+
// const int gl_MaxComputeTextureImageUnits = 16;
58+
// const int gl_MaxComputeImageUniforms = 8;
59+
// const int gl_MaxComputeAtomicCounters = 8;
60+
// const int gl_MaxComputeAtomicCounterBuffers = 8;
61+
// const int gl_MaxVertexAttribs = 16;
62+
// const int gl_MaxVertexUniformComponents = 1024;
63+
// const int gl_MaxVaryingComponents = 60;
64+
// const int gl_MaxVertexOutputComponents = 64;
65+
// const int gl_MaxGeometryInputComponents = 64;
66+
// const int gl_MaxGeometryOutputComponents = 128;
67+
// const int gl_MaxFragmentInputComponents = 128;
68+
// const int gl_MaxVertexTextureImageUnits = 16;
69+
// const int gl_MaxCombinedTextureImageUnits = 96;
70+
// const int gl_MaxTextureImageUnits = 16;
71+
// const int gl_MaxImageUnits = 8;
72+
// const int gl_MaxCombinedImageUnitsAndFragmentOutputs = 8; // deprecated
73+
// const int gl_MaxCombinedShaderOutputResources = 8;
74+
// const int gl_MaxImageSamples = 0;
75+
// const int gl_MaxVertexImageUniforms = 0;
76+
// const int gl_MaxTessControlImageUniforms = 0;
77+
// const int gl_MaxTessEvaluationImageUniforms = 0;
78+
// const int gl_MaxGeometryImageUniforms = 0;
79+
// const int gl_MaxFragmentImageUniforms = 8;
80+
// const int gl_MaxCombinedImageUniforms = 8;
81+
// const int gl_MaxFragmentUniformComponents = 1024;
82+
// const int gl_MaxDrawBuffers = 8;
83+
// const int gl_MaxClipDistances = 8;
84+
// const int gl_MaxGeometryTextureImageUnits = 16;
85+
// const int gl_MaxGeometryOutputVertices = 256;
86+
// const int gl_MaxGeometryTotalOutputComponents = 1024;
87+
// const int gl_MaxGeometryUniformComponents = 1024;
88+
// const int gl_MaxGeometryVaryingComponents = 64; // deprecated
89+
// const int gl_MaxTessControlInputComponents = 128;
90+
// const int gl_MaxTessControlOutputComponents = 128;
91+
// const int gl_MaxTessControlTextureImageUnits = 16;
92+
// const int gl_MaxTessControlUniformComponents = 1024;
93+
// const int gl_MaxTessControlTotalOutputComponents = 4096;
94+
// const int gl_MaxTessEvaluationInputComponents = 128;
95+
// const int gl_MaxTessEvaluationOutputComponents = 128;
96+
// const int gl_MaxTessEvaluationTextureImageUnits = 16;
97+
// const int gl_MaxTessEvaluationUniformComponents = 1024;
98+
// const int gl_MaxTessPatchComponents = 120;
99+
// const int gl_MaxPatchVertices = 32;
100+
// const int gl_MaxTessGenLevel = 64;
101+
// const int gl_MaxViewports = 16;
102+
// const int gl_MaxVertexUniformVectors = 256;
103+
// const int gl_MaxFragmentUniformVectors = 256;
104+
// const int gl_MaxVaryingVectors = 15;
105+
// const int gl_MaxVertexAtomicCounters = 0;
106+
// const int gl_MaxTessControlAtomicCounters = 0;
107+
// const int gl_MaxTessEvaluationAtomicCounters = 0;
108+
// const int gl_MaxGeometryAtomicCounters = 0;
109+
// const int gl_MaxFragmentAtomicCounters = 8;
110+
// const int gl_MaxCombinedAtomicCounters = 8;
111+
// const int gl_MaxAtomicCounterBindings = 1;
112+
// const int gl_MaxVertexAtomicCounterBuffers = 0;
113+
// const int gl_MaxTessControlAtomicCounterBuffers = 0;
114+
// const int gl_MaxTessEvaluationAtomicCounterBuffers = 0;
115+
// const int gl_MaxGeometryAtomicCounterBuffers = 0;
116+
// const int gl_MaxFragmentAtomicCounterBuffers = 1;
117+
// const int gl_MaxCombinedAtomicCounterBuffers = 1;
118+
// const int gl_MaxAtomicCounterBufferSize = 32;
119+
// const int gl_MinProgramTexelOffset = -8;
120+
// const int gl_MaxProgramTexelOffset = 7;
121+
// const int gl_MaxTransformFeedbackBuffers = 4;
122+
// const int gl_MaxTransformFeedbackInterleavedComponents = 64;
123+
// `
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)