-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswap_kernels.cl
112 lines (94 loc) · 3.38 KB
/
swap_kernels.cl
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
float dist(uint4 x, uint4 y) {
float LUM_WEIGHT = 10;
float F[3] = {0.114f, 0.587f, 0.299f};
float t = 0;
float lx = 0;
float ly = 0;
for (int i = 0; i < 3; ++i) {
float xi = x[i] * F[i];
float yi = y[i] * F[i];
float d = xi - yi;
t += d * d;
lx += xi;
ly += yi;
}
float l = lx - ly;
return t + l * l * LUM_WEIGHT;
}
void conditional_swap(__read_only image2d_t source,
__read_only image2d_t palette,
__write_only image2d_t destination,
sampler_t sampler,
int2 loc_a,
int2 loc_b)
{
uint4 src_a = (read_imageui(source, sampler, loc_a));
uint4 src_b = (read_imageui(source, sampler, loc_b));
uint4 pal_a = (read_imageui(palette, sampler, loc_a));
uint4 pal_b = (read_imageui(palette, sampler, loc_b));
float stay_dist = dist(src_a, pal_a) + dist(src_b, pal_b);
float swap_dist = dist(src_a, pal_b) + dist(src_b, pal_a);
if (swap_dist < stay_dist) {
int2 tmp = loc_a;
loc_a = loc_b;
loc_b = tmp;
}
write_imageui(destination, loc_a, pal_a);
write_imageui(destination, loc_b, pal_b);
}
__kernel void even_horizontal(__read_only image2d_t source,
__read_only image2d_t palette,
__write_only image2d_t destination,
sampler_t sampler,
int width,
int height)
{
int gid = get_global_id(0);
int row = (2 * gid) / width;
int col = (2 * gid) % width;
int2 a = (int2) (col, row);
int2 b = (int2) (col + 1, row);// Assumes even width
conditional_swap(source, palette, destination, sampler, a, b);
}
__kernel void odd_horizontal(__read_only image2d_t source,
__read_only image2d_t palette,
__write_only image2d_t destination,
sampler_t sampler,
int width,
int height)
{
int gid = get_global_id(0);
int row = (2 * gid) / width;
int col = (2 * gid) % width;
int2 a = (int2) (col + 1, row); // Assumes even width
int2 b = (int2) ((col + 2) % width, row); // Wrap around to col 0
conditional_swap(source, palette, destination, sampler, a, b);
}
__kernel void even_vertical(__read_only image2d_t source,
__read_only image2d_t palette,
__write_only image2d_t destination,
sampler_t sampler,
int width,
int height)
{
int gid = get_global_id(0);
int row = 2 * (gid / width);
int col = gid % width;
int2 a = (int2) (col, row);
int2 b = (int2) (col, row + 1);// Assumes even height
conditional_swap(source, palette, destination, sampler, a, b);
}
__kernel void odd_vertical(__read_only image2d_t source,
__read_only image2d_t palette,
__write_only image2d_t destination,
sampler_t sampler,
int width,
int height)
{
int gid = get_global_id(0);
int row = 2 * (gid / width);
int col = gid % width;
int2 a = (int2) (col, row + 1); // Assumes even height
int2 b = (int2) (col, (row + 2) % height); // Wrap around to row 0
conditional_swap(source, palette, destination, sampler, a, b);
}