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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
This file is part of darktable,
copyright (c) 2012-2025 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
float4
image_to_grid(
const float4 p,
const int4 size,
const float4 sigma)
{
return (float4)(
clamp(p.x/sigma.x, 0.0f, size.x-1.0f),
clamp(p.y/sigma.y, 0.0f, size.y-1.0f),
clamp(p.z/sigma.z, 0.0f, size.z-1.0f), 0.0f);
}
void
atomic_add_f(
global float *val,
const float delta)
{
#ifdef NVIDIA_SM_20
// buys me another 3x--10x over the `algorithmic' improvements in the splat kernel below,
// depending on configuration (sigma_s and sigma_r)
float res = 0;
asm volatile ("atom.global.add.f32 %0, [%1], %2;" : "=f"(res) : "l"(val), "f"(delta));
#else
union
{
float f;
unsigned int i;
}
old_val;
union
{
float f;
unsigned int i;
}
new_val;
global volatile unsigned int *ival = (global volatile unsigned int *)val;
do
{
// the following is equivalent to old_val.f = *val. however, as according to the opencl standard
// we can not rely on global buffer val to be consistently cached (relaxed memory consistency) we
// access it via a slower but consistent atomic operation.
old_val.i = atomic_add(ival, 0);
new_val.f = old_val.f + delta;
}
while (atomic_cmpxchg (ival, old_val.i, new_val.i) != old_val.i);
#endif
}
kernel void
zero(
global float *grid,
const int width,
const int height)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
grid[x + width*y] = 0.0f;
}
kernel void
splat(
read_only image2d_t in,
global float *grid,
const int width,
const int height,
const int sizex,
const int sizey,
const int sizez,
const float sigma_s,
const float sigma_r,
local int *gi,
local float *accum)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
const int lszx = get_local_size(0);
const int i = get_local_id(0);
const int j = get_local_id(1);
int li = lszx*j + i;
int4 size = (int4)(sizex, sizey, sizez, 0);
float4 sigma = (float4)(sigma_s, sigma_s, sigma_r, 0);
int ox = 1;
int oy = size.x;
int oz = size.y*size.x;
if(x < width && y < height)
{
// splat into downsampled grid
const float4 pixel = read_imagef (in, samplerc, (int2)(x, y));
float L = pixel.x;
float4 p = (float4)(x, y, L, 0);
float4 gridp = image_to_grid(p, size, sigma);
int4 xi = min(size - 2, (int4)(gridp.x, gridp.y, gridp.z, 0));
float fx = gridp.x - xi.x;
float fy = gridp.y - xi.y;
float fz = gridp.z - xi.z;
// first accumulate into local memory
gi[li] = xi.x + oy*xi.y + oz*xi.z;
float contrib = 100.0f/(sigma_s*sigma_s);
li *= 8;
accum[li++] = contrib * (1.0f-fx) * (1.0f-fy) * (1.0f-fz);
accum[li++] = contrib * ( fx) * (1.0f-fy) * (1.0f-fz);
accum[li++] = contrib * (1.0f-fx) * ( fy) * (1.0f-fz);
accum[li++] = contrib * ( fx) * ( fy) * (1.0f-fz);
accum[li++] = contrib * (1.0f-fx) * (1.0f-fy) * ( fz);
accum[li++] = contrib * ( fx) * (1.0f-fy) * ( fz);
accum[li++] = contrib * (1.0f-fx) * ( fy) * ( fz);
accum[li++] = contrib * ( fx) * ( fy) * ( fz);
}
else
{
gi[li] = -1;
}
barrier(CLK_LOCAL_MEM_FENCE);
if(i != 0) return;
// non-logarithmic reduction..
// but we also need to take care of where to accumulate (only merge where gi[.] == gi[.])
li = lszx*j;
int lii = 8*li;
int oldgi = gi[li];
float tmp[8];
for(int k=0;k<8;k++)
tmp[k] = accum[lii+k];
for(int ii=1; ii < lszx && oldgi != -1; ii++)
{
li = lszx*j + ii;
lii = 8*li;
if(gi[li] != oldgi)
{
atomic_add_f(grid + oldgi, tmp[0]);
atomic_add_f(grid + oldgi+ox, tmp[1]);
atomic_add_f(grid + oldgi+oy, tmp[2]);
atomic_add_f(grid + oldgi+oy+ox, tmp[3]);
atomic_add_f(grid + oldgi+oz, tmp[4]);
atomic_add_f(grid + oldgi+oz+ox, tmp[5]);
atomic_add_f(grid + oldgi+oz+oy, tmp[6]);
atomic_add_f(grid + oldgi+oz+oy+ox, tmp[7]);
oldgi = gi[li];
for(int k=0;k<8;k++)
tmp[k] = accum[lii+k];
}
else
{
for(int k=0;k<8;k++)
tmp[k] += accum[lii+k];
}
}
if(oldgi == -1) return;
atomic_add_f(grid + oldgi, tmp[0]);
atomic_add_f(grid + oldgi+ox, tmp[1]);
atomic_add_f(grid + oldgi+oy, tmp[2]);
atomic_add_f(grid + oldgi+oy+ox, tmp[3]);
atomic_add_f(grid + oldgi+oz, tmp[4]);
atomic_add_f(grid + oldgi+oz+ox, tmp[5]);
atomic_add_f(grid + oldgi+oz+oy, tmp[6]);
atomic_add_f(grid + oldgi+oz+oy+ox, tmp[7]);
}
kernel void
blur_line_z(
global const float *ibuf,
global float *obuf,
const int offset1,
const int offset2,
const int offset3,
const int size1,
const int size2,
const int size3)
{
const int k = get_global_id(0);
const int j = get_global_id(1);
if(k >= size1 || j >= size2) return;
const float w1 = 4.0f/16.0f;
const float w2 = 2.0f/16.0f;
int index = k*offset1 + j*offset2;
float tmp1 = ibuf[index];
obuf[index] = w1*ibuf[index + offset3] + w2*ibuf[index + 2*offset3];
index += offset3;
float tmp2 = ibuf[index];
obuf[index] = w1*(ibuf[index + offset3] - tmp1) + w2*ibuf[index + 2*offset3];
index += offset3;
for(int i=2;i<size3-2;i++)
{
const float tmp3 = ibuf[index];
obuf[index] =
+ w1*(ibuf[index + offset3] - tmp2)
+ w2*(ibuf[index + 2*offset3] - tmp1);
index += offset3;
tmp1 = tmp2;
tmp2 = tmp3;
}
const float tmp3 = ibuf[index];
obuf[index] = w1*(ibuf[index + offset3] - tmp2) - w2*tmp1;
index += offset3;
obuf[index] = - w1*tmp3 - w2*tmp2;
}
kernel void
blur_line(
global const float *ibuf,
global float *obuf,
const int offset1,
const int offset2,
const int offset3,
const int size1,
const int size2,
const int size3)
{
const int k = get_global_id(0);
const int j = get_global_id(1);
if(k >= size1 || j >= size2) return;
const float w0 = 6.0f/16.0f;
const float w1 = 4.0f/16.0f;
const float w2 = 1.0f/16.0f;
int index = k*offset1 + j*offset2;
float tmp1 = ibuf[index];
obuf[index] = ibuf[index]*w0 + w1*ibuf[index + offset3] + w2*ibuf[index + 2*offset3];
index += offset3;
float tmp2 = ibuf[index];
obuf[index] = ibuf[index]*w0 + w1*(ibuf[index + offset3] + tmp1) + w2*ibuf[index + 2*offset3];
index += offset3;
for(int i=2;i<size3-2;i++)
{
const float tmp3 = ibuf[index];
obuf[index] = ibuf[index]*w0
+ w1*(ibuf[index + offset3] + tmp2)
+ w2*(ibuf[index + 2*offset3] + tmp1);
index += offset3;
tmp1 = tmp2;
tmp2 = tmp3;
}
const float tmp3 = ibuf[index];
obuf[index] = ibuf[index]*w0 + w1*(ibuf[index + offset3] + tmp2) + w2*tmp1;
index += offset3;
obuf[index] = ibuf[index]*w0 + w1*tmp3 + w2*tmp2;
}
kernel void
slice_to_output(
read_only image2d_t in,
read_only image2d_t target,
write_only image2d_t out,
global float *grid,
const int width,
const int height,
const int sizex,
const int sizey,
const int sizez,
const float sigma_s,
const float sigma_r,
const float detail)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
// detail: 0 is leave as is, -1 is bilateral filtered, +1 is contrast boost
const float norm = -detail * sigma_r * 0.04f;
const int ox = 1;
const int oy = sizex;
const int oz = sizey*sizex;
int4 size = (int4)(sizex, sizey, sizez, 0);
float4 sigma = (float4)(sigma_s, sigma_s, sigma_r, 0);
float4 pixel = read_imagef (in, samplerc, (int2)(x, y));
float4 pixel2 = read_imagef (target, samplerc, (int2)(x, y));
float L = pixel.x;
float4 p = (float4)(x, y, L, 0);
float4 gridp = image_to_grid(p, size, sigma);
int4 gridi = min(size - 2, (int4)(gridp.x, gridp.y, gridp.z, 0));
float fx = gridp.x - gridi.x;
float fy = gridp.y - gridi.y;
float fz = gridp.z - gridi.z;
// trilinear lookup (wouldn't read/write access to 3d textures be cool)
// could actually use an array of 2d textures, these only require opencl 1.2
const int gi = gridi.x + sizex*(gridi.y + sizey*gridi.z);
const float Ldiff =
grid[gi] * (1.0f - fx) * (1.0f - fy) * (1.0f - fz) +
grid[gi+ox] * ( fx) * (1.0f - fy) * (1.0f - fz) +
grid[gi+oy] * (1.0f - fx) * ( fy) * (1.0f - fz) +
grid[gi+ox+oy] * ( fx) * ( fy) * (1.0f - fz) +
grid[gi+oz] * (1.0f - fx) * (1.0f - fy) * ( fz) +
grid[gi+ox+oz] * ( fx) * (1.0f - fy) * ( fz) +
grid[gi+oy+oz] * (1.0f - fx) * ( fy) * ( fz) +
grid[gi+ox+oy+oz] * ( fx) * ( fy) * ( fz);
pixel2.x = fmax(0.0f, pixel2.x + norm * Ldiff);
write_imagef (out, (int2)(x, y), pixel2);
}
kernel void
slice(
read_only image2d_t in,
write_only image2d_t out,
global float *grid,
const int width,
const int height,
const int sizex,
const int sizey,
const int sizez,
const float sigma_s,
const float sigma_r,
const float detail)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
// detail: 0 is leave as is, -1 is bilateral filtered, +1 is contrast boost
const float norm = -detail * sigma_r * 0.04f;
const int ox = 1;
const int oy = sizex;
const int oz = sizey*sizex;
int4 size = (int4)(sizex, sizey, sizez, 0);
float4 sigma = (float4)(sigma_s, sigma_s, sigma_r, 0);
float4 pixel = read_imagef (in, samplerc, (int2)(x, y));
float L = pixel.x;
float4 p = (float4)(x, y, L, 0);
float4 gridp = image_to_grid(p, size, sigma);
int4 gridi = min(size - 2, (int4)(gridp.x, gridp.y, gridp.z, 0));
float fx = gridp.x - gridi.x;
float fy = gridp.y - gridi.y;
float fz = gridp.z - gridi.z;
// trilinear lookup (wouldn't read/write access to 3d textures be cool)
// could actually use an array of 2d textures, these only require opencl 1.2
const int gi = gridi.x + sizex*(gridi.y + sizey*gridi.z);
const float Ldiff =
grid[gi] * (1.0f - fx) * (1.0f - fy) * (1.0f - fz) +
grid[gi+ox] * ( fx) * (1.0f - fy) * (1.0f - fz) +
grid[gi+oy] * (1.0f - fx) * ( fy) * (1.0f - fz) +
grid[gi+ox+oy] * ( fx) * ( fy) * (1.0f - fz) +
grid[gi+oz] * (1.0f - fx) * (1.0f - fy) * ( fz) +
grid[gi+ox+oz] * ( fx) * (1.0f - fy) * ( fz) +
grid[gi+oy+oz] * (1.0f - fx) * ( fy) * ( fz) +
grid[gi+ox+oy+oz] * ( fx) * ( fy) * ( fz);
pixel.x = fmax(0.0f, L + norm * Ldiff);
write_imagef (out, (int2)(x, y), pixel);
}
|