summaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/demos/fractal/Mandelbrot.cl
blob: 2fc959c2417cbcd478b5c5b5df4c0d1867f6aa37 (plain)
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
#ifdef DOUBLE_FP
    #ifdef AMD_FP
        #pragma OPENCL EXTENSION cl_amd_fp64 : enable
    #else
        #pragma OPENCL EXTENSION cl_khr_fp64 : enable
    #endif
    typedef double varfloat;
#else
    typedef float varfloat;
#endif

/**
 * For a description of this algorithm please refer to
 * http://en.wikipedia.org/wiki/Mandelbrot_set
 * @author Michael Bien
 */
kernel void mandelbrot(
        const int width,        const int height,
        const varfloat x0,      const varfloat y0,
        const varfloat rangeX,  const varfloat rangeY,
        global uint *output,    global uint *colorMap,
        const int colorMapSize, const int maxIterations) {

    unsigned int ix = get_global_id(0);
    unsigned int iy = get_global_id(1);

    varfloat r = x0 + ix * rangeX / width;
    varfloat i = y0 + iy * rangeY / height;

    varfloat x = 0;
    varfloat y = 0;

    varfloat magnitudeSquared = 0;
    int iteration = 0;

    while (magnitudeSquared < 4 && iteration < maxIterations) {
        varfloat x2 = x*x;
        varfloat y2 = y*y;
        y = 2 * x * y + i;
        x = x2 - y2 + r;
        magnitudeSquared = x2+y2;
        iteration++;
    }

    if (iteration == maxIterations)  {
        output[iy * width + ix] = 0;
    }else {
        varfloat alpha = (varfloat)iteration / maxIterations;
        int colorIndex = (int)(alpha * colorMapSize);
        output[iy * width + ix] = colorMap[colorIndex];
      // monochrom
      //  output[iy * width + ix] = 255*iteration/maxIterations;
    }

}