-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_code.cl
More file actions
96 lines (75 loc) · 3.35 KB
/
kernel_code.cl
File metadata and controls
96 lines (75 loc) · 3.35 KB
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
void kernel calcMandelbrotSet(int width, int height, float2 re, float2 im, unsigned int maxIter, global unsigned int* iterations,
global float2* zs) {
int ind = get_global_id(0);
int2 p = (int2)(ind % width, ind / width);
float2 C = (float2)(re.x + (re.y - re.x) * (p.x / (float)width), im.x + (im.y - im.x) * (p.y / (float)height));
float2 Z = 0.f;
unsigned int n = 0;
while (Z.x * Z.x + Z.y * Z.y < 4.f && n < maxIter) {
Z = (float2)((Z.x - Z.y) * (Z.x + Z.y), 2.f * Z.x * Z.y);
Z += C;
++n;
}
iterations[ind] = n;
zs[ind] = Z;
}
void kernel calcJuliaSet(int width, int height, float2 re, float2 im, unsigned int maxIter, global unsigned int* iterations,
global float2* zs, float2 C) {
int ind = get_global_id(0);
int2 p = (int2)(ind % width, ind / width);
float2 Z = (float2)(re.x + (re.y - re.x) * (p.x / (float)width), im.x + (im.y - im.x) * (p.y / (float)height));
unsigned int n = 0;
while (Z.x * Z.x + Z.y * Z.y < 4.f && n < maxIter) {
Z = (float2)((Z.x - Z.y) * (Z.x + Z.y), 2.f * Z.x * Z.y);
Z += C;
++n;
}
iterations[ind] = n;
zs[ind] = Z;
}
void kernel calcBurningShip(int width, int height, float2 re, float2 im, unsigned int maxIter, global unsigned int* iterations,
global float2* zs) {
int ind = get_global_id(0);
int2 p = (int2)(ind % width, ind / width);
float2 C = (float2)(re.x + (re.y - re.x) * (p.x / (float)width), im.x + (im.y - im.x) * (p.y / (float)height));
float2 Z = 0.f;
unsigned int n = 0;
while (Z.x * Z.x + Z.y * Z.y < 4.f && n < maxIter) {
Z = (float2)(Z.x * Z.x - Z.y * Z.y + C.x, 2.f * fabs(Z.x * Z.y) + C.y);
++n;
}
iterations[ind] = n;
zs[ind] = Z;
}
void kernel calcNewtonPools(int width, int height, float2 re, float2 im, unsigned int maxIter, global unsigned int* iterations,
global float2* zs) {
int ind = get_global_id(0);
int2 p = (int2)(ind % width, ind / width);
float2 C = (float2)(re.x + (re.y - re.x) * (p.x / (float)width), im.x + (im.y - im.x) * (p.y / (float)height));
float2 Z = C;
float2 copyC = C;
float2 temp = (float2)(0.f, 0.f);
unsigned int n = 0;
do {
Z = (float2)((2.f / 3.f) * Z.x + ((Z.x * Z.x - Z.y * Z.y) / (3.f * ((Z.x * Z.x + Z.y * Z.y) * (Z.x * Z.x + Z.y * Z.y)))),
(2.f / 3.f) * Z.y * (1.f - (Z.x / ((Z.x * Z.x + Z.y * Z.y) * (Z.x * Z.x + Z.y * Z.y)))));
temp = Z - copyC;
copyC = Z;
++n;
} while (temp.x * temp.x + temp.y * temp.y > 0.01f && n < maxIter);
iterations[ind] = n;
zs[ind] = Z;
}
void kernel draw(int width, int height, unsigned int maxIter, int shift, int paletteSize, constant unsigned int* iterations,
constant float2* zs, constant uchar4* palette, global uchar4* pixels) {
int ind = get_global_id(0);
unsigned int n = iterations[ind];
if (n == maxIter) {
pixels[ind] = 0;
return;
}
float2 Z = zs[ind];
float smoothed = log10(Z.x * Z.x + Z.y * Z.y);
unsigned short colorIndex = (unsigned short)(shift + sqrt(n + 10.f - smoothed) * 128.f) % paletteSize;
pixels[ind] = palette[colorIndex];
}