-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
194 lines (167 loc) · 5.92 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var test = require('tape');
var regl = require('regl');
var transform = require('../');
var iota = require('iota-array');
var glsl = require('glslify');
var show = require('ndarray-show');
var ndarray = require('ndarray');
var ndFft = require('ndarray-fft');
var pool = require('ndarray-scratch');
var almostEqual = require('almost-equal');
var ndt = require('ndarray-tests');
var ops = require('ndarray-ops');
var seed = 1;
function random () {
seed = (seed * 9301 + 49297) % 233280;
return seed / 233280;
}
test('regl', function (t) {
var canvas = document.createElement('canvas');
canvas.width = 8;
canvas.height = 8;
regl({
canvas: canvas,
extensions: ['oes_texture_float'],
onDone: function (err, regl) {
if (err) {
t.notOk('fail');
t.end();
}
var fbos;
function createOperator (frag) {
return regl({
vert: `
precision mediump float;
attribute vec2 xy;
void main () {
gl_Position = vec4(xy, 0, 1);
}
`,
frag: frag,
uniforms: {
resolution: regl.prop('resolution'),
forward: regl.prop('forward'),
subtransformSize: regl.prop('subtransformSize'),
horizontal: regl.prop('horizontal'),
normalization: regl.prop('normalization'),
src: (ctx, props) => fbos[props.input],
},
attributes: {xy: [-4, -4, 4, -4, 0, 4]},
framebuffer: (ctx, props) => fbos[props.output],
depth: {enable: false},
count: 3
});
}
var operators = {
fft: createOperator(glsl(`
precision highp float;
#pragma glslify: fft = require(../fft)
uniform sampler2D src;
uniform vec2 resolution;
uniform float subtransformSize;
uniform bool horizontal, forward;
void main () {
gl_FragColor = fft(src, resolution, subtransformSize, horizontal, forward);
}
`)),
untangle: createOperator(glsl(`
precision highp float;
#pragma glslify: untangle = require(../untangle)
uniform sampler2D src;
uniform vec2 resolution;
uniform bool horizontal;
uniform float normalization;
void main () {
gl_FragColor = untangle(src, resolution, horizontal, normalization);
}
`)),
tangle: createOperator(glsl(`
precision highp float;
#pragma glslify: tangle = require(../tangle)
uniform sampler2D src;
uniform vec2 resolution;
uniform bool horizontal;
uniform float normalization;
void main () {
gl_FragColor = tangle(src, resolution, horizontal, normalization);
}
`)),
};
function read(fbo) {
var data;
fbo.use(() => data = regl.read());
return data;
}
function execute (phases) {
for (var i = 0; i < phases.length; i++) {
operators[phases[i].operation](phases[i].passes);
}
}
function testFFT (M, N, tol) {
var label = 'size: ' + M + ' x ' + N + ': ';
var i, j, forward, inverse;
var M2 = M / 2 + 1;
var N2 = N / 2 + 1;
var input = new Array(M * N * 4).fill(0).map(d => random() - 0.5);
var Ar0 = ndarray(new Float32Array(input.slice()), [M, N, 4]);
var Ar = ndarray(new Float32Array(input.slice()), [M, N, 4]);
var Ai = ndarray(new Float32Array(input.slice().fill(0)), [M, N, 4]);
for (j = 0; j < 4; j++) {
ndFft(1, Ar.pick(null, null, j), Ai.pick(null, null, j));
}
ops.divseq(Ar, Math.sqrt(M * N));
ops.divseq(Ai, Math.sqrt(M * N));
fbos = [0, 1, 2].map(function () {
return regl.framebuffer({
color: regl.texture({
type: 'float',
format: 'rgba',
data: ndarray(input, [M, N, 4]).transpose(1, 0),
width: N,
height: M
})
})
});
forward = transform({width: N, height: M, input: 0, ping: 1, pong: 2, output: 0, forward: true, splitNormalization: true});
inverse = transform({width: N, height: M, input: 0, ping: 1, pong: 2, output: 0, forward: false, splitNormalization: true});
execute(forward);
B = ndarray(read(fbos[0]), [M, N, 4]);
t.ok(ndt.equal(B.hi(M, N2 - 1).lo(0, 1), Ar.hi(M, N2 - 1).lo(0, 1), tol), label + 'FFT(A)[ 0:M, 1:N/2-1 ]');
t.ok(ndt.equal(B.lo(0, N2), Ai.hi(M, N2 - 1).lo(0, 1), tol), label + 'FFT(A)[ 0:M, N/2:N ]:');
t.ok(ndt.equal(B.hi(M2, 1), Ar.hi(M2, 1), tol), label + 'FFT(A)[ 0:M/2, 0:1 ]');
t.ok(ndt.equal(B.hi(M, 1).lo(M2, 0), Ai.hi(M2 - 1, 1).lo(1, 0), tol), label + 'FFT(A)[ M/2:M, 0:1 ]');
t.ok(ndt.equal(B.hi(M2, N2).lo(0, N2 - 1), Ar.hi(M2, N2).lo(0, N2 - 1), tol), label + 'FFT(A)[ 0:M/2, N/2-1:N/2 ]');
t.ok(ndt.equal(B.hi(M, N2).lo(M2, N2 - 1), Ai.hi(M2 - 1, N2).lo(1, N2 - 1), tol), label + 'FFT(A)[ M/2:M, N/2-1:N ]');
execute(inverse);
B = ndarray(read(fbos[0]), [M, N, 4]);
t.ok(ndt.equal(B, Ar0, 4e-4), label + 'A[ 0:M, 0:N ]');
fbos.forEach(fbo => fbo.destroy());
}
testFFT(4, 4, 1e-6);
testFFT(8, 4, 5e-6);
testFFT(16, 4, 1e-5);
testFFT(32, 4, 5e-5);
testFFT(64, 4, 5e-5);
testFFT(4, 8, 5e-6);
testFFT(8, 8, 5e-6);
testFFT(16, 8, 1e-5);
testFFT(32, 8, 5e-5);
testFFT(64, 8, 5e-5);
testFFT(4, 16, 1e-5);
testFFT(8, 16, 1e-5);
testFFT(16, 16, 1e-5);
testFFT(32, 16, 5e-5);
testFFT(64, 16, 1e-4);
testFFT(4, 32, 5e-5);
testFFT(8, 32, 5e-5);
testFFT(16, 32, 5e-5);
testFFT(32, 32, 5e-5);
testFFT(64, 32, 1e-4);
testFFT(256, 256, 5e-4);
testFFT(512, 256, 1e-3);
testFFT(256, 512, 1e-3);
regl.destroy();
t.end();
}
});
});