mandelbrot/mandelbrot.cl

47 lines
1.3 KiB
Common Lisp
Raw Normal View History

2019-09-30 20:49:24 +02:00
__kernel void mandelbrot(__global unsigned char *out_buff, unsigned int iter, double cx, double cy, double xs, double ys, unsigned int width, unsigned int height)
2019-06-05 00:09:56 +02:00
{
const int idx = get_global_id(0);
unsigned int i;
double z_real, z_imag;
double z2_real, z2_imag;
double pt_real, pt_imag;
2019-09-30 20:49:24 +02:00
double color_scale;
unsigned int r, g, b;
2019-06-05 00:09:56 +02:00
const unsigned int pix_x = idx % width;
const unsigned int pix_y = (unsigned int)idx / width;
const double val_per_x_pixel = xs / (width - 1);
const double val_per_y_pixel = ys / (height - 1);
pt_real = (((double)pix_x - (double)(width - 1) / 2)) * val_per_x_pixel + cx;
pt_imag = (((double)pix_y - (double)(height - 1) / 2)) * val_per_y_pixel + cy;
z_real = 0.0;
z_imag = 0.0;
for (i = 0; i < iter; i++) {
z2_real = z_real * z_real - z_imag * z_imag;
z2_imag = 2.0 * z_real * z_imag;
z_real = z2_real + pt_real;
z_imag = z2_imag + pt_imag;
if ((z_real * z_real + z_imag * z_imag) >= 4.0)
break;
}
2019-09-30 20:49:24 +02:00
color_scale = (double)(i) / (double)iter;
if (i != iter) {
r = (unsigned char)(255.0 * sqrt((1-color_scale)));
g = (unsigned char)(255.0 * color_scale*color_scale);
b = (unsigned char)(255.0 * color_scale);
} else {
r = g = b = 0;
}
out_buff[idx*3] = r;
out_buff[idx*3+1] = g;
out_buff[idx*3+2] = b;
2019-06-05 00:09:56 +02:00
}