-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal-fs.glsl
426 lines (383 loc) · 9.31 KB
/
fractal-fs.glsl
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#version 330
/*
* Copyright (C) 2015, 2016, 2017, 2018 Martin Lambers <marlam@marlam.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if HAVE_ARB_GPU_SHADER5
# extension GL_ARB_gpu_shader5 : require
# define PRECISE precise
#else
# define PRECISE
#endif
/*
*
* PART 1: floating point type with basic operations
*
*/
/* This implements basic arithmetic operations for a FLOAT type, which can be
* one of the following:
* - float: single precision floating point, natively supported
* - double: double precision floating point, natively supported
* - doublefloat: emulated extended precision based on a pair of floats
* - doubledouble: emulated extended precision based on a pair of doubles
* The emulated types are implemented using algorithms from the following papers:
* - "A Floating-Point Technique for Extending the Available Precision" by T. J.
* Dekker
* - "Library for Double-Double and Quad-Double Arithmetic" by Yozo Hida, Xiaoye
* S. Li, David H. Bailey
* - "Extended-Precision Floating-Point Numbers for GPU Computation" by Andrew
* Thall
*/
// FLOAT_TYPE uses the same values as float_type in the C++ source
#if (FLOAT_TYPE == 0)
# define FLOAT float
# define FLOAT_EMU 0
#elif (FLOAT_TYPE == 1)
# extension GL_ARB_gpu_shader_fp64 : require
# define FLOAT double
# define FLOAT_EMU 0
#elif (FLOAT_TYPE == 2)
# define FLOAT vec2
# define FLOAT_EMU 1
# define BASEFLOAT float
# define BASEVEC2 vec2
# define BASEVEC4 vec4
const float SPLIT = 4097.0; // (1 << 12) + 1;
#elif (FLOAT_TYPE == 3)
# extension GL_ARB_gpu_shader_fp64 : require
# define FLOAT dvec2
# define FLOAT_EMU 1
# define BASEFLOAT double
# define BASEVEC2 dvec2
# define BASEVEC4 dvec4
const double SPLIT = 134217729.0LF; // (1 << 27) + 1;
#endif
/* Building blocks for emulation based on pairs */
#if FLOAT_EMU
BASEVEC2 two_add(BASEFLOAT a, BASEFLOAT b)
{
PRECISE BASEFLOAT s = a + b;
PRECISE BASEFLOAT v = s - a;
PRECISE BASEFLOAT e = (a - (s - v)) + (b - v);
return BASEVEC2(s, e);
}
BASEVEC2 quick_two_add(BASEFLOAT a, BASEFLOAT b) // requires abs(a) >= abs(b)
{
PRECISE BASEFLOAT s = a + b;
PRECISE BASEFLOAT e = b - (s - a);
return BASEVEC2(s, e);
}
BASEVEC4 two_add_comp(BASEVEC2 a, BASEVEC2 b)
{
PRECISE BASEVEC2 s = a + b;
PRECISE BASEVEC2 v = s - a;
PRECISE BASEVEC2 e = (a - (s - v)) + (b - v);
return BASEVEC4(s.x, e.x, s.y, e.y);
}
BASEVEC4 two_sub_comp(BASEVEC2 a, BASEVEC2 b)
{
PRECISE BASEVEC2 s = a - b;
PRECISE BASEVEC2 v = s - a;
PRECISE BASEVEC2 e = (a - (s - v)) - (b + v);
return BASEVEC4(s.x, e.x, s.y, e.y);
}
BASEVEC2 split(BASEFLOAT a)
{
PRECISE BASEFLOAT t = SPLIT * a;
PRECISE BASEFLOAT b_hi = t - (t - a);
PRECISE BASEFLOAT b_lo = a - b_hi;
return BASEVEC2(b_hi, b_lo);
}
BASEVEC4 split_comp(BASEVEC2 a)
{
PRECISE BASEVEC2 t = SPLIT * a;
PRECISE BASEVEC2 b_hi = t - (t - a);
PRECISE BASEVEC2 b_lo = a - b_hi;
return BASEVEC4(b_hi.x, b_lo.x, b_hi.y, b_lo.y);
}
BASEVEC2 two_mul(BASEVEC2 ab)
{
PRECISE BASEFLOAT p = ab.x * ab.y;
PRECISE BASEVEC4 s = split_comp(ab);
PRECISE BASEFLOAT e = ((s.x * s.z - p) + s.x * s.w + s.y * s.z) + s.y * s.w;
return BASEVEC2(p, e);
}
BASEVEC2 two_sqr(BASEFLOAT a)
{
PRECISE BASEFLOAT p = a * a;
PRECISE BASEVEC2 s = split(a);
PRECISE BASEFLOAT e = ((s.x * s.x - p) + BASEFLOAT(2) * s.x * s.y) + s.y * s.y;
return BASEVEC2(p, e);
}
BASEVEC2 emu_add(BASEVEC2 a, BASEFLOAT b)
{
PRECISE BASEVEC2 s = two_add(a.x, b);
s.y += a.y;
PRECISE BASEVEC2 r = quick_two_add(s.x, s.y);
return r;
}
BASEVEC2 emu_add(BASEVEC2 a, BASEVEC2 b)
{
PRECISE BASEVEC4 st = two_add_comp(a, b);
st.y += st.z;
st.xy = quick_two_add(st.x, st.y);
st.y += st.w;
st.xy = quick_two_add(st.x, st.y);
return st.xy;
}
BASEVEC2 emu_sub(BASEVEC2 a, BASEVEC2 b)
{
PRECISE BASEVEC4 st = two_sub_comp(a, b);
st.y += st.z;
st.xy = quick_two_add(st.x, st.y);
st.y += st.w;
st.xy = quick_two_add(st.x, st.y);
return st.xy;
}
BASEVEC2 emu_mul(BASEVEC2 a, BASEFLOAT b)
{
PRECISE BASEVEC2 p = two_mul(BASEVEC2(a.x, b));
p.y += a.y * b;
PRECISE BASEVEC2 r = quick_two_add(p.x, p.y);
return r;
}
BASEVEC2 emu_mul(BASEVEC2 a, BASEVEC2 b)
{
PRECISE BASEVEC2 p = two_mul(BASEVEC2(a.x, b.x));
p.y += dot(a, b.yx);
PRECISE BASEVEC2 r = quick_two_add(p.x, p.y);
return r;
}
BASEVEC2 emu_div(BASEVEC2 a, BASEVEC2 b)
{
PRECISE BASEFLOAT q0 = a.x / b.x;
PRECISE BASEVEC2 r = emu_sub(a, emu_mul(b, q0));
PRECISE BASEFLOAT q1 = r.x / b.x;
r = emu_sub(r, emu_mul(b, q1));
PRECISE BASEFLOAT q2 = r.x / b.x;
r = emu_add(quick_two_add(q0, q1), q2);
return r;
}
BASEVEC2 emu_sqr(BASEFLOAT a)
{
PRECISE BASEVEC2 p = two_sqr(a);
PRECISE BASEVEC2 r = quick_two_add(p.x, p.y);
return r;
}
BASEVEC2 emu_sqr(BASEVEC2 a)
{
PRECISE BASEVEC2 p = two_sqr(a.x);
p.y += BASEFLOAT(2) * a.x * a.y;
PRECISE BASEVEC2 s = quick_two_add(p.x, p.y);
return s;
}
BASEVEC2 emu_sqrt(BASEVEC2 a)
{
PRECISE BASEFLOAT x = inversesqrt(a.x);
PRECISE BASEFLOAT ax = a.x * x;
PRECISE BASEFLOAT diff = emu_sub(a, emu_sqr(ax)).x;
PRECISE BASEFLOAT prod = diff * x * BASEFLOAT(0.5);
PRECISE BASEVEC2 r = two_add(prod, ax);
return r;
}
#endif
/* Unify operations for native and emulated types. Necessary because
* there is no operator overloading in GLSL. */
FLOAT to_FLOAT(float x)
{
#if FLOAT_EMU
return FLOAT(x, BASEFLOAT(0));
#else
return FLOAT(x);
#endif
}
float to_float(FLOAT x)
{
#if FLOAT_EMU
return float(x.x);
#else
return float(x);
#endif
}
FLOAT xabs(FLOAT a)
{
#if FLOAT_EMU
return (a.x < BASEFLOAT(0) ? -a : a);
#else
return abs(a);
#endif
}
int xcmp(FLOAT a, float b)
{
#if FLOAT_EMU
if (a.x < b || (a.x == b && a.y < BASEFLOAT(0)))
return -1;
else if (a.x == b && a.y == 0.0)
return 0;
else //if (a.x > b || (a.x == b && a.y > BASEFLOAT(0.0)))
return +1;
#else
return (a < b ? -1 : a > b ? +1 : 0);
#endif
}
FLOAT xadd(FLOAT a, FLOAT b)
{
#if FLOAT_EMU
return emu_add(a, b);
#else
return a + b;
#endif
}
FLOAT xsub(FLOAT a, FLOAT b)
{
#if FLOAT_EMU
return emu_sub(a, b);
#else
return a - b;
#endif
}
FLOAT xmul(FLOAT a, FLOAT b)
{
#if FLOAT_EMU
return emu_mul(a, b);
#else
return a * b;
#endif
}
FLOAT xdiv(FLOAT a, FLOAT b)
{
#if FLOAT_EMU
return emu_div(a, b);
#else
return a / b;
#endif
}
FLOAT xsqr(FLOAT a)
{
#if FLOAT_EMU
return emu_sqr(a);
#else
return a * a;
#endif
}
FLOAT xsqrt(FLOAT a)
{
#if FLOAT_EMU
return emu_sqrt(a);
#else
return sqrt(a);
#endif
}
/*
*
* PART 2: complex math based on the FLOAT type
*
*/
struct complex_t {
FLOAT re, im;
};
complex_t add(complex_t a, complex_t b)
{
return complex_t(xadd(a.re, b.re), xadd(a.im, b.im));
}
complex_t sub(complex_t a, complex_t b)
{
return complex_t(xsub(a.re, b.re), xsub(a.im, b.im));
}
complex_t mul(complex_t a, complex_t b)
{
return complex_t(xsub(xmul(a.re, b.re), xmul(a.im, b.im)),
xadd(xmul(a.im, b.re), xmul(a.re, b.im)));
}
complex_t sqr(complex_t a)
{
complex_t r;
r.re = xsub(xsqr(a.re), xsqr(a.im));
r.im = xmul(a.re, a.im);
r.im = xadd(r.im, r.im);
return r;
}
complex_t powui(complex_t a, int i) // i >= 1
{
complex_t r = a;
int j = 1;
while (i >= 2 * j) {
r = sqr(r);
j *= 2;
}
for (int k = j; k < i; k++) {
r = mul(r, a);
}
return r;
}
FLOAT abs_sqr(complex_t a)
{
return xadd(xsqr(a.re), xsqr(a.im));
}
FLOAT abs(complex_t a)
{
return xsqrt(abs_sqr(a));
}
/*
*
* PART 3: the fractal set
*
*/
// MANDELBROT_POWER: >= 2
// MANDELBROT_LN_POWER: ln(MANDELBROT_POWER)
// MANDELBROT_MAX_ITERATIONS: e.g. 256
// MANDELBROT_BAILOUT: e.g. 4
// MANDELBROT_SMOOTH: 0 or 1
#define M_LN2 0.69314718055994530942
float fractal(complex_t c)
{
int i = 0;
complex_t z = complex_t(to_FLOAT(0), to_FLOAT(0));
FLOAT abssqrz;
do {
z = add(powui(z, MANDELBROT_POWER), c);
i++;
abssqrz = abs_sqr(z);
}
while (xcmp(abssqrz, MANDELBROT_BAILOUT) < 0
&& i < MANDELBROT_MAX_ITERATIONS);
float ret = 0.0;
if (i < MANDELBROT_MAX_ITERATIONS) {
#if MANDELBROT_SMOOTH
ret = float(i) - log(log(to_float(xsqrt(abssqrz))) / M_LN2) / MANDELBROT_LN_POWER;
ret /= float(MANDELBROT_MAX_ITERATIONS - 1);
#else
ret = float(i) / float(MANDELBROT_MAX_ITERATIONS - 1);
#endif
}
return ret;
}
/*
*
* PART 4: the main function
*
*/
smooth in vec2 vxy;
uniform FLOAT x0;
uniform FLOAT xw;
uniform FLOAT y0;
uniform FLOAT yw;
layout(location = 0) out float fcolor;
void main(void)
{
FLOAT re = xadd(x0, xmul(to_FLOAT(vxy.x), xw));
FLOAT im = xadd(y0, xmul(to_FLOAT(vxy.y), yw));
fcolor = fractal(complex_t(re, im));
}