-
Notifications
You must be signed in to change notification settings - Fork 1
/
testsuite.ts
269 lines (210 loc) · 6.56 KB
/
testsuite.ts
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { higherOrderSpiral } from "./src/runner/draws";
import { Runner } from "./src/runner/runner";
const glCanvas = document.getElementById("gl") as HTMLCanvasElement;
const gl = glCanvas.getContext("webgl2");
if (gl === null) throw new Error("problem getting the gl context");
const sourceCanvas = document.getElementById("source") as HTMLCanvasElement;
const source = sourceCanvas.getContext("2d");
console.log(source);
if (source === null) throw new Error("problem getting the source context");
/*
const grd = source.createLinearGradient(0, 0, 960, 0);
grd.addColorStop(0, "black");
grd.addColorStop(1, "white");
source.fillStyle = grd;
source.fillRect(0, 0, 960, 540);
source.fillStyle = "white";
source.fillRect(960 / 4, 540 / 4, 960 / 2, 540 / 2);
*/
const bloom = `def threshold 0.2
uniform float u_size;
fn luma(vec4 color) {
return dot(color.rgb, vec3(0.299, 0.587, 0.114));
}
fn blur5(vec2 direction, int channel) {
uv := pos / res;
off1 := vec2(1.3333333333333333) * direction;
mut color := vec4(0.);
color += frag(uv, channel) * 0.29411764705882354;
color += frag(uv + (off1 / res), channel) * 0.35294117647058826;
color += frag(uv - (off1 / res), channel) * 0.35294117647058826;
return color;
}
pr two_pass_blur(float size, int reps, int channel = -1) {
loop reps {
blur5(vec2(size, 0.), channel); refresh;
blur5(vec2(0., size), channel); refresh;
}
}
{ vec4(frag.rgb * (step(1. - luma(frag0), 1. - threshold)), frag.a); } -> 1
{ @two_pass_blur(size: 1., reps: 3, channel: 1);} -> 1
//{ vec4(1., 0., 0., 1.); } -> 1
{ frag0 + frag1; } -> 0
`;
// FIXME
const blur13Func = `fn blur13(vec2 dir, int channel = -1) {
uv := pos / res;
mut col := vec4(0.);
off1 := vec2(1.411764705882353) * dir;
off2 := vec2(3.2941176470588234) * dir;
off3 := vec2(5.176470588235294) * dir;
col += frag(channel, uv) * 0.1964825501511404;
col += frag(frag, uv + (off1 / res)) * 0.2969069646728344;
col += frag(frag, uv - (off1 / res)) * 0.2969069646728344;
col += frag(frag, uv + (off2 / res)) * 0.09447039785044732;
col += frag(frag, uv - (off2 / res)) * 0.09447039785044732;
col += frag(frag, uv + (off3 / res)) * 0.010381362401148057;
col += frag(frag, uv - (off3 / res)) * 0.010381362401148057;
return col;
}`;
const redSimple = `0 -> { frag0 * vec4(1., 0., 0., 1.); } -> 0`;
const rgbTextures = `
{ vec4(1., 0., 0., 1.); } -> 1
{ vec4(0., 1., 0., 1.); } -> 2
{ vec4(0., 0., 1., 1.); } -> 3
{ frag0 * (frag1 + frag2); }
`;
const ifElse = `
fn redOrBlue (int x) {
if (x > 0) return vec4(1., 0., 0., 1.);
else return vec4(0., 0., 1., 1.);
}
{ redOrBlue(-1); } -> 1
{ redOrBlue(1); } -> 2
0 -> { frag * (frag1 + frag2); }
`;
const forLoop = `
fn keepDividingByTwo (float x, int reps) {
mut r := 1.;
for (int i = 0; i < reps; i++) {
r /= float(2);
}
return r;
}
{ frag * keepDividingByTwo(1., 3); }
`;
const sobelFunc = `
vec4 sobel(int channel = -1) {
uv := pos / res;
w := 1. / res.x;
h := 1. / res.y;
k := vec4[](
frag(channel, uv + vec2(-w, -h)),
frag(channel, uv + vec2(0., -h)),
frag(channel, uv + vec2(w, -h)),
frag(channel, uv + vec2(-w, 0.)),
frag(channel, uv + vec2(w, 0.)),
frag(channel, uv + vec2(-w, h)),
frag(channel, uv + vec2(0., h)),
frag(channel, uv + vec2(w, h))
);
edge_h := k[2] + (2. * k[4]) + k[7] - (k[0] + (2. * k[3]) + k[5]);
edge_v := k[0] + (2. * k[1]) + k[2] - (k[5] + (2. * k[6]) + k[7]);
sob := sqrt(edge_h * edge_h + edge_v * edge_v);
return vec4(1. - sob.rgb, 1.);
}`;
const godraysFunc = `fn godrays (
vec4 col = frag,
float exposure = 1.,
float decay = 1.,
float density = 1.,
float weight = 0.01,
vec2 light_pos = vec2(.5, .5),
int num_samples = 100,
int channel = -1
) {
mut uv := pos / res;
delta_uv := (uv - light_pos) / float(num_samples) * density;
mut illumination_decay := 1.;
vec4 color = col;
for (int i = 0; i < num_samples; i++) {
uv -= delta_uv;
tex_sample := frag(channel, uv) * illumination_decay * weight;
color += tex_sample;
illumination_decay *= decay;
}
return color * exposure;
}`;
const twoPassBlurProc = `
pr two_pass_blur(float size, int reps = 2, int channel = -1) {
loop reps {
blur13(vec2(size, 0.), channel); refresh;
blur13(vec2(0., size), channel); refresh;
}
}`;
const identityMultTest = `
{ vec4(float(int[](1)[int(cos(0.))]), 0., 0., 1.); }
`;
const fullTest = [
godraysFunc,
blur13Func,
twoPassBlurProc,
sobelFunc,
`0 -> { frag; } -> 1
1 -> { @two_pass_blur(reps: 10, size: 1.); } -> 1
fn outline (int channel = -1) {
sob := sobel(channel);
avg := (sob.x + sob.y + sob.z) / 3.;
return vec4(avg, avg, avg, 1.);
}
//{ frag1 + vec4(1. - outline().rgb, 1.); }
//{ godrays(num_samples: 75); }
`,
].join("\n");
const mysteriousBlur = `def threshold 0.2
uniform float u_size;
fn luma(vec4 color) {
return dot(color.rgb, vec3(0.299, 0.587, 0.114));
}
fn blur13(vec2 dir, int channel = -1) {
uv := pos / res;
mut col := vec4(0.);
off1 := vec2(1.411764705882353) * dir;
off2 := vec2(3.2941176470588234) * dir;
off3 := vec2(5.176470588235294) * dir;
col += frag(channel, uv) * 0.1964825501511404;
col += frag(channel, uv + (off1 / res)) * 0.2969069646728344;
col += frag(channel, uv - (off1 / res)) * 0.2969069646728344;
col += frag(channel, uv + (off2 / res)) * 0.09447039785044732;
col += frag(channel, uv - (off2 / res)) * 0.09447039785044732;
col += frag(channel, uv + (off3 / res)) * 0.010381362401148057;
col += frag(channel, uv - (off3 / res)) * 0.010381362401148057;
return col;
}
pr two_pass_blur(float size, int reps, int channel = -1) {
loop reps {
blur13(vec2(size, 0.), channel); refresh;
blur13(vec2(0., size), channel); refresh;
}
}
{ vec4(frag.rgb * (step(1. - luma(frag0), 1. - threshold)), frag.a); } -> 999
999 -> { @two_pass_blur(42., 1, 999); } -> 999
{frag999.grba;} -> 1001
999 -> { @two_pass_blur(1., 1, 999); } -> 999
{frag999.gbra;} -> 1002
{frag1002;}`;
const onceTest = `once { once { 'blue'4; }}`;
const blueWrapped = `loop 9 { { frag0 + 'blue'4 / 9.; } }`;
const blueUnwrapped = `loop 9 { frag0 + 'blue'4 / 9.; }`;
const code = blueUnwrapped;
console.log(code);
let runner: Runner;
try {
runner = new Runner(gl, code, [sourceCanvas], {});
} catch (err) {
console.log(err.message);
throw "look at the logged error message";
}
let drawingFunc = higherOrderSpiral([255, 0, 0], [0, 0, 0]);
let frame = 0;
/*
const animate = (time: number) => {
runner.draw();
drawingFunc(time / 1000, frame, source, sourceCanvas);
requestAnimationFrame(animate);
frame++;
};
animate(0);
*/
drawingFunc(0, frame, source, sourceCanvas);
runner.draw();