-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbilateral.cl
239 lines (190 loc) · 6.76 KB
/
bilateral.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// get pixel
inline float
GETPIX (__global float *in_values, int w, int h, int i, int j){
if(i<0){
i=0;
}
if(i>=w){
i=w-1;
}
if(j<0){
j=0;
}
if(j>=h){
j=h-1;
}
return in_values[i+w*j];
}
/* No buffer version */
__kernel void
bilateral_filtering_no_buffer(__global __read_only float *in_values,
__global __write_only float *out_values,
__local float *buffer,
__local float *spatial, __global __read_only float *spatial_dif,
int w, int h, float sigma,
int buf_w, int buf_h,
const int halo)
{
// Global position of output pixel
const int x = get_global_id(0);
const int y = get_global_id(1);
if ((y < h) && (x < w)) { // stay in bounds
float num = 0;
float den = 0;
float pixel = in_values[x+w*y];
int idx = 0;
for (int i = -halo; i<=halo; ++i){
for (int j = -halo; j<=halo; ++j){
float tmp_p = GETPIX(in_values, w, h, x+i, y+j);
float dif = tmp_p-pixel;
//float value = exp(-0.5*(i*i+j*j)/9) * exp(-0.5*(dif*dif)/(sigma*sigma));
float value = spatial_dif[idx] * exp(-0.5*(dif*dif)/(sigma*sigma));
num += tmp_p*value;
den += value;
++idx;
}
}
out_values[ x + w*y ] = num/den;
}
}
/* With buffer version */
__kernel void
bilateral_filtering_buffer(__global __read_only float *in_values,
__global __write_only float *out_values,
__local float *buffer,
__local float *spatial, __global __read_only float *spatial_dif,
int w, int h, float sigma,
int buf_w, int buf_h,
const int halo)
{
// Global position of output pixel
const int x = get_global_id(0);
const int y = get_global_id(1);
// Local position relative to (0, 0) in workgroup
const int lx = get_local_id(0);
const int ly = get_local_id(1);
// coordinates of the upper left corner of the buffer in image
// space, including halo
const int buf_corner_x = x - lx - halo;
const int buf_corner_y = y - ly - halo;
// coordinates of our pixel in the local buffer
const int buf_x = lx + halo;
const int buf_y = ly + halo;
// 1D index of thread within our work-group
const int idx_1D = ly * get_local_size(0) + lx;
const int y_lim = get_global_size(1);
// Decide whether it is odd number group
const int group_id_y = get_group_id(1);
const int group_id_x = get_group_id(0);
const bool isOdd = (group_id_x%2==1);
// Write buffer
const int size = (2*halo+1);
if (idx_1D<buf_w){
for (int row = 0; row < buf_h; ++row) {
buffer[row * buf_w + idx_1D] = \
GETPIX(in_values, w, h, buf_corner_x + idx_1D, buf_corner_y + row);
}
}
if (idx_1D<size){
for (int row=0; row<size; ++row){
spatial[idx_1D + row*size] = spatial_dif[idx_1D + row*size];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
// Each thread in the valid region (x < w, y < h) should calculate the weighted average
if ((y < h) && (x < w)) { // stay in bounds
float num = 0;
float den = 0;
float pixel = in_values[x+w*y];
int idx = 0;
for (int i = -halo; i<=halo; ++i){
for (int j = -halo; j<=halo; ++j){
// get value of neighbourhood
float tmp_p = buffer[buf_x+i + buf_w*(buf_y+j)];
float dif = tmp_p-pixel;
float value = spatial[idx] * exp(-0.5*(dif*dif)/(sigma*sigma));
num += tmp_p*value;
den += value;
++idx;
}
}
out_values[ x + w*y ] = num/den;
}
}
/* Buffer version with index trick */
__kernel void
bilateral_filtering_index(__global __read_only float *in_values,
__global __write_only float *out_values,
__local float *buffer, int mod,
__local float *spatial, __global __read_only float *spatial_dif,
int w, int h, float sigma,
int buf_w, int buf_h,
const int halo)
{
// Global position of output pixel
const int x = get_global_id(0);
int y = get_global_id(1);
// Local position relative to (0, 0) in workgroup
const int lx = get_local_id(0);
const int ly = get_local_id(1);
// coordinates of the upper left corner of the buffer in image
// space, including halo
const int buf_corner_x = x - lx - halo;
int buf_corner_y = y - ly - halo;
// coordinates of our pixel in the local buffer
const int buf_x = lx + halo;
//const int buf_y = ly + halo;
// 1D index of thread within our work-group
const int idx_1D = ly * get_local_size(0) + lx;
//const int local_x = get_local_size(0);
const int local_y = get_local_size(1);
const int size = (2*halo+1);
// Write spatial difference gaussian
if (idx_1D<size){
for (int row=0; row<size; ++row){
spatial[idx_1D + row*size] = spatial_dif[idx_1D + row*size];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
// Iterate the column
for (int base = 0; base < h; base += local_y){
// Write buffer
if (idx_1D < buf_w){
// First iteration write buffer
if (base==0) {
for (int row = 0; row < buf_h; ++row) {
buffer[row * buf_w + idx_1D] = \
GETPIX(in_values, w, h, buf_corner_x + idx_1D, buf_corner_y + row);
}
}
// Rest of the iterations reuse part of the buffer
else {
for (int row = buf_h - local_y; row < buf_h; ++row) {
buffer[((buf_corner_y + halo + row) & mod) * buf_w + idx_1D] = GETPIX(in_values, w, h, buf_corner_x + idx_1D, buf_corner_y + row);
}
}
}
barrier(CLK_LOCAL_MEM_FENCE);
if ((y < h) && (x < w)) {
float num = 0;
float den = 0;
float pixel = in_values[x+w*y];
int idx = 0;
for (int i = -halo; i<=halo; ++i){
for (int j = -halo; j<=halo; ++j){
// get value of neighbourhood
float tmp_p = buffer[buf_x+i + (( y + halo + j ) & mod) * buf_w];
float dif = (tmp_p-pixel);
float value = spatial[idx] * exp(-0.5*(dif*dif)/(sigma*sigma));
num += tmp_p*value;
den += value;
++idx;
}
}
out_values[ x + w*y ] = num/den;
}
y += local_y;
buf_corner_y += local_y;
barrier(CLK_LOCAL_MEM_FENCE);
}
}