mandelbrot/mandelbrot.cl
2019-06-05 00:09:56 +02:00

35 lines
954 B
Common Lisp

__kernel void mandelbrot(__global unsigned int *out_buff, unsigned int iter, double cx, double cy, double xs, double ys, unsigned int width, unsigned int height)
{
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;
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;
}
out_buff[idx] = i;
}