56 float *
const restrict interpolated
58 float *
const restrict clipping_mask
67 const float *
const restrict input = (
const float *
const restrict)ivoid;
68 float *
const restrict output = (
float *
const restrict)
ovoid;
80 for(
int c = 0; c < 3; c++)
82 clipvaln[c] = clips[c] / (
DT_HL_KNEE_DET * fmaxf(normalization[c], 1e-9f));
93 for(
int c = 0; c < 3; c++)
136 for(
size_t i = 0;
i < npix;
i++)
140 maskb[
i] = (clipping_mask[
i * 4 + 3] >= 1e-3f);
158 for(
int region_index = 0; region_index < nreg; region_index++)
169 const float *remosaic_input = input;
170 float *input_corr = NULL;
179 remosaic_input = input_corr;
214 const uint8_t(*
const xtrans)[6] = (
const uint8_t(*
const)[6])piece->
dsc_in.
xtrans;
225 const float *
const restrict input = (
const float *
const restrict)ivoid;
226 float *
const restrict output = (
float *
const restrict)
ovoid;
230 int32_t
lookup[6][6][32] = { { { 0 } } };
237 for(
int c = 0; c < 3; c++)
239 clipvaln[c] = clips[c] / (
DT_HL_KNEE_DET * fmaxf(normalization[c], 1e-9f));
249 for(
int c = 0; c < 3; c++)
288 for(
size_t i = 0;
i < npix;
i++)
292 maskb[
i] = (clipping_mask[
i * 4 + 3] >= 1e-3f);
304 for(
int region_index = 0; region_index < nreg; region_index++)
312 const float *remosaic_input = input;
313 float *input_corr = NULL;
322 remosaic_input = input_corr;
363 float *
const restrict interpolated,
float *
const restrict clipping_mask,
371 const uint8_t(*
const xtrans)[6] = (filters == 9u) ? (
const uint8_t(*
const)[6])piece->
dsc_in.
xtrans : NULL;
376 *remosaic_input_out = input;
377 *input_corr_out = NULL;
382 memcpy(knee, knee_pre,
sizeof(knee));
385 for(
int c = 0; c < 3; c++)
387 clipvaln[c] = clips[c] / (
DT_HL_KNEE_DET * fmaxf(normalization[c], 1e-9f));
394 const size_t npix =
size;
404 for(
size_t i = 0;
i < npix;
i++)
407 maskb[
i] = (clipping_mask[
i * 4 + 3] >= 1e-3f);
416 for(
int region_index = 0; region_index < nreg; region_index++)
430 *remosaic_input_out = input_corr;
431 *input_corr_out = input_corr;
439#define HL_CL_RELEASE(mem_obj) \
442 dt_opencl_release_mem_object(mem_obj); \
468 const int devid = pipe->
devid;
470 const size_t n_in = (size_t)roi_in->
width * roi_in->
height;
471 const size_t n_out = (size_t)roi_out->
width * roi_out->
height;
481 if(cl_err != CL_SUCCESS)
goto error;
485 || (filters != 9u &&
process_harmonic_bayer(self, pipe, piece, host_in, host_out, roi_in, roi_out, clips)))
523 cl_mem mask_buf, cl_mem *corr_out,
const dt_iop_roi_t *
const roi_in,
529 const int devid = pipe->
devid;
534 const int is_xtrans = (filters == 9u);
542 cl_mem member = NULL;
543 cl_mem depth_dev = NULL;
545 uint8_t *h_seed = NULL;
546 uint8_t *h_member = NULL;
555 memcpy(knee, knee_pre,
sizeof(knee));
558 for(
int c = 0; c < 3; c++)
560 clipvaln[c] = clips[c] / (
DT_HL_KNEE_DET * fmaxf(norm[c], 1e-9f));
569 for(
int c = 0; c < 3; c++) memcpy(lift + c *
DT_HL_KNEE_BINS, knee[c].lift,
sizeof(knee[c].lift));
577 const cl_float4 clip4 = { { clipvaln[0], clipvaln[1], clipvaln[2], 1.f } };
578 const cl_float4 wb4 = { { norm[0], norm[1], norm[2], 1.f } };
595 if(cl_err != CL_SUCCESS)
goto out;
605 is_xtrans, knee_clipraw, knee);
606 if(cl_err != CL_SUCCESS)
goto out;
616 if(!seed || !member || !h_seed || !h_member || !depth)
629 if(cl_err != CL_SUCCESS)
goto out;
633 if(cl_err != CL_SUCCESS)
goto out;
648 if(cl_err != CL_SUCCESS)
goto out;
655 const char *override_env = getenv(
"HL_CL_CPU_PX");
656 if(override_env) cpu_px = (size_t)strtoull(override_env, NULL, 10);
657 for(
int region_index = 0; region_index < nreg && cl_err == CL_SUCCESS; region_index++)
661 const size_t region_px = (size_t)(regions[region_index].rx1 - regions[region_index].rx0 + 1)
662 * (size_t)(regions[region_index].ry1 - regions[region_index].ry0 + 1);
663 if(region_px <= cpu_px)
666 cl_err = _region_cpu_offload_cl(devid, global_data, interp_buf, mask_buf, depth_dev,
width,
672 cl_err = _region_guided_filter_cl(devid, global_data, interp_buf, mask_buf, depth_dev,
width,
677 if(cl_err == CL_SUCCESS)
691 if(cl_err == CL_SUCCESS)
716 const int devid = pipe->
devid;
727 const int is_xtrans = (filters == 9u);
731 cl_mem interpolated = NULL;
732 cl_mem clipping_mask = NULL;
734 cl_mem clips_cl = NULL;
735 cl_mem normalization_final = NULL;
736 cl_mem dev_xtrans = NULL;
737 cl_mem lookup_cl = NULL;
738 cl_mem corr_cl = NULL;
739 cl_mem det_clips_cl = NULL;
740 float *h_interp = NULL;
741 float *h_mask = NULL;
743 float *input_corr = NULL;
744 const float *remosaic_input = NULL;
758 int32_t
lookup[6][6][32] = { { { 0 } } };
772 if(cl_err != CL_SUCCESS)
goto fallback;
776 is_xtrans ? (
const uint8_t(*
const)[6])piece->
dsc_in.
xtrans : NULL, norm_host);
778 if(
IS_NULL_PTR(normalization_final))
goto fallback;
785 for(
int c = 0; c < 3; c++) knee_clipraw[c] = clips[c] /
DT_HL_KNEE_DET;
790 size_t korigin[3] = { 0, 0, 0 };
791 size_t kregion[3] = { (size_t)
width, (
size_t)
height, 1 };
793 if(cl_err != CL_SUCCESS)
800 is_xtrans, knee_clipraw, knee, pipe);
802 if(cl_err != CL_SUCCESS)
goto fallback;
807 for(
int c = 0; c < 3; c++)
824 &normalization_final);
849 &normalization_final);
858 if(cl_err != CL_SUCCESS)
goto fallback;
866 cl_mem corr_buf = NULL;
867 size_t origin[3] = { 0, 0, 0 };
868 size_t region1[3] = { (size_t)
width, (
size_t)
height, 1 };
870 if(gpu_err == CL_SUCCESS)
872 if(gpu_err == CL_SUCCESS)
874 if(gpu_err == CL_SUCCESS)
877 if(gpu_err == CL_SUCCESS)
884 if(!getenv(
"HL_MIDDLE_AB"))
893 clips, norm_host, dev_xtrans, knee);
900 if(gpu_err == CL_SUCCESS && corr_buf)
911 if(gpu_err == CL_SUCCESS && getenv(
"HL_MIDDLE_AB"))
917 if(gpu_interp && host_interp && host_mask && host_raw
926 const float *remosaic_ptr = NULL;
927 float *input_corr_ab = NULL;
929 norm_host, &remosaic_ptr, &input_corr_ab, knee))
931 float max_diff = 0.f;
932 double sum_diff = 0.0;
933 size_t arg_index = 0;
934 for(
size_t i = 0;
i < npix * 4;
i++)
936 const float diff = fabsf(gpu_interp[
i] - host_interp[
i]);
944 fprintf(stderr,
"[hl middle AB] max=%.3e mean=%.3e at px=(%llu,%llu) c=%llu gpu=%f cpu=%f\n", max_diff,
945 sum_diff / (
double)(npix * 4), (
unsigned long long)((arg_index / 4) %
width),
946 (
unsigned long long)((arg_index / 4) /
width), (
unsigned long long)(arg_index % 4),
947 gpu_interp[arg_index], host_interp[arg_index]);
968 if(restore_err == CL_SUCCESS && gpu_err == CL_SUCCESS)
971 if(restore_err == CL_SUCCESS)
974 else if(restore_err == CL_SUCCESS)
978 if(restore_err == CL_SUCCESS)
1010 if(restore_err == CL_SUCCESS)
1020 if(restore_err != CL_SUCCESS)
1026 cl_err = restore_err;
1030 else if(gpu_err == CL_SUCCESS)
1035 if(restore_err == CL_SUCCESS)
1037 if(restore_err != CL_SUCCESS)
1043 cl_err = restore_err;
1051 if(gpu_err == CL_SUCCESS)
goto remosaic;
1066 if(cl_err != CL_SUCCESS)
goto fallback;
1068 if(cl_err != CL_SUCCESS)
goto fallback;
1072 &remosaic_input, &input_corr, knee))
1077 if(cl_err != CL_SUCCESS)
goto fallback;
1083 cl_mem remosaic_in_cl = dev_in;
1085 remosaic_in_cl = corr_cl;
1086 else if(remosaic_input != h_raw && remosaic_input != NULL)
1091 if(cl_err != CL_SUCCESS)
goto fallback;
1092 remosaic_in_cl = corr_cl;
1097 const int clip_floor_on =
TRUE;
1109 &normalization_final);
1128 const int clip_floor_on =
TRUE;
1140 &normalization_final);
1151 if(cl_err != CL_SUCCESS)
goto fallback;
1171 "[opencl_highlights] harmonic GPU gather failed (%i), falling back to the host roundtrip\n", cl_err);
static void error(char *msg)
void _hl_gauss_cache_flush(void)
static float lookup(read_only image2d_t lut, const float x)
const dt_colormatrix_t dt_aligned_pixel_t out
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
void dt_print(dt_debug_thread_t thread, const char *msg,...)
#define dt_pixelpipe_cache_alloc_align(size, pipe)
#define dt_pixelpipe_cache_free_align(mem)
#define dt_pixelpipe_cache_alloc_align_float(pixels, pipe)
#define __DT_CLONE_TARGETS__
#define for_four_channels(_var,...)
#define __OMP_PARALLEL_FOR__(...)
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
__DT_CLONE_TARGETS__ void _compute_laplacian_normalization(const float *const restrict input, const dt_iop_roi_t *const roi_in, const uint32_t filters, const uint8_t(*const xtrans)[6], dt_aligned_pixel_t normalization)
__DT_CLONE_TARGETS__ void _remosaic_and_replace_xtrans(const float *const restrict input, const float *const restrict input_raw, const float *const restrict interpolated, const float *const restrict clipping_mask, float *const restrict output, const dt_aligned_pixel_t white_balance, const dt_aligned_pixel_t clips, const int clip_is_floor, const dt_iop_roi_t *const roi_in, const uint8_t(*const xtrans)[6], const size_t width, const size_t height)
__DT_CLONE_TARGETS__ void _interpolate_and_mask(const float *const restrict input, float *const restrict interpolated, float *const restrict clipping_mask, const dt_aligned_pixel_t clips_in, const dt_aligned_pixel_t det_scale, const dt_aligned_pixel_t white_balance, const uint32_t filters, const size_t width, const size_t height)
__DT_CLONE_TARGETS__ void _remosaic_and_replace(const float *const restrict input, const float *const restrict input_raw, const float *const restrict interpolated, const float *const restrict clipping_mask, float *const restrict output, const dt_aligned_pixel_t white_balance, const dt_aligned_pixel_t clips, const int clip_is_floor, const uint32_t filters, const size_t width, const size_t height)
__DT_CLONE_TARGETS__ void _interpolate_and_mask_xtrans(const float *const restrict input, float *const restrict interpolated, float *const restrict clipping_mask, const dt_aligned_pixel_t clips, const dt_aligned_pixel_t white_balance, const dt_iop_roi_t *const roi_in, const int32_t lookup[6][6][32], const uint8_t(*const xtrans)[6], const size_t width, const size_t height)
__DT_CLONE_TARGETS__ void _build_xtrans_bilinear_lookup(int32_t lookup[6][6][32], const dt_iop_roi_t *const roi_in, const uint8_t(*const xtrans)[6])
uint32_t dt_dev_get_roi_filters(const dt_dev_pixelpipe_iop_t *const piece, const dt_iop_roi_t *const roi_in)
static float kernel(const float *x, const float *y)
__DT_CLONE_TARGETS__ void _hl_knee_apply_interpolated(float *const restrict interpolated, const size_t npix, const dt_aligned_pixel_t clipvaln, const dt_aligned_pixel_t wb4, const _hl_knee_curve_t curves[3])
__DT_CLONE_TARGETS__ void _hl_knee_estimate(const float *const restrict input, const size_t width, const size_t height, const uint32_t filters, const dt_iop_roi_t *const roi_in, const uint8_t(*const xtrans)[6], const dt_aligned_pixel_t clipval_raw, _hl_knee_curve_t curves[3], const dt_dev_pixelpipe_t *pipe)
__DT_CLONE_TARGETS__ void _hl_knee_apply_cfa(const float *const restrict input, float *const restrict input_corr, const size_t width, const size_t height, const uint32_t filters, const dt_iop_roi_t *const roi_in, const uint8_t(*const xtrans)[6], const dt_aligned_pixel_t clipval_raw, const _hl_knee_curve_t curves[3])
cl_int _hl_knee_estimate_cl(const int devid, void *gd_void, cl_mem dev_in, const size_t width, const size_t height, const uint32_t filters, const dt_iop_roi_t *const roi_in, cl_mem dev_xtrans, const int is_xtrans, const dt_aligned_pixel_t clipval_raw, _hl_knee_curve_t curves[3], const dt_dev_pixelpipe_t *pipe)
cl_int _hl_knee_apply_cfa_cl(const int devid, void *gd_void, cl_mem dev_in, cl_mem dev_out, const size_t width, const size_t height, const uint32_t filters, const dt_iop_roi_t *const roi_in, cl_mem dev_xtrans, const int is_xtrans, const dt_aligned_pixel_t clipval_raw, const _hl_knee_curve_t curves[3])
float dt_aligned_pixel_t[4]
int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
void * dt_opencl_alloc_device_buffer(const int devid, const size_t size)
int dt_opencl_enqueue_copy_buffer_to_image(const int devid, cl_mem src_buffer, cl_mem dst_image, size_t offset, size_t *origin, size_t *region)
int dt_opencl_copy_device_to_host(const int devid, void *host, void *device, const int width, const int height, const int bpp)
void * dt_opencl_alloc_device(const int devid, const int width, const int height, const int bpp)
void * dt_opencl_copy_host_to_device_constant(const int devid, const size_t size, void *host)
int dt_opencl_write_buffer_to_device(const int devid, void *host, void *device, const size_t offset, const size_t size, const int blocking)
int dt_opencl_read_buffer_from_device(const int devid, void *host, void *device, const size_t offset, const size_t size, const int blocking)
int dt_opencl_set_kernel_arg(const int dev, const int kernel, const int num, const size_t size, const void *arg)
gboolean dt_opencl_finish(const int devid)
int dt_opencl_enqueue_copy_image_to_buffer(const int devid, cl_mem src_image, cl_mem dst_buffer, size_t *origin, size_t *region, size_t offset)
void dt_opencl_release_mem_object(cl_mem mem)
int dt_opencl_write_host_to_device(const int devid, void *host, void *device, const int width, const int height, const int bpp)
#define DT_OPENCL_DEFAULT_ERROR
cl_int process_harmonic_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out, const dt_aligned_pixel_t clips)
__DT_CLONE_TARGETS__ int process_harmonic_bayer(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const restrict ivoid, void *const restrict ovoid, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out, const dt_aligned_pixel_t clips)
#define HL_CL_RELEASE(mem_obj)
static __DT_CLONE_TARGETS__ int _harmonic_reconstruct_host(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const float *const restrict input, float *const restrict interpolated, float *const restrict clipping_mask, const dt_iop_roi_t *const roi_in, const dt_aligned_pixel_t clips, const dt_aligned_pixel_t normalization, const float **remosaic_input_out, float **input_corr_out, const _hl_knee_curve_t knee_pre[3])
static cl_int _harmonic_cl_roundtrip(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out, const dt_aligned_pixel_t clips)
__DT_CLONE_TARGETS__ int process_harmonic_xtrans(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const restrict ivoid, void *const restrict ovoid, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out, const dt_aligned_pixel_t clips)
static cl_int _harmonic_reconstruct_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, cl_mem raw_buf, cl_mem interp_buf, cl_mem mask_buf, cl_mem *corr_out, const dt_iop_roi_t *const roi_in, const dt_aligned_pixel_t clips, const dt_aligned_pixel_t norm, cl_mem dev_xtrans, const _hl_knee_curve_t knee_pre[3])
void _region_guided_filter(float *const restrict interp, const float *const restrict mask, const float *const restrict depth, const int width, const _hl_region_t *const region, const dt_dev_pixelpipe_t *pipe, const float solid_color, const int max_iter, const float noise_level)
int _segment_clipped_regions(const uint8_t *const restrict maskb, const float *const restrict depth, const int width, const int height, const float pad_factor, const int pad_min, const int pad_max, _hl_region_t **regions_out)
void _hf_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _region_guided_filter_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _knee_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _joint_core_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _selfdome_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _aniso_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _cf_harmonic_fill_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _cf_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _region_blur_cl_selftest(const int devid, const dt_dev_pixelpipe_t *pipe)
void _cf_joint_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
void _sp_chol_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
static cl_mem _sp_cl_upload(const int devid, const void *data, const size_t bytes)
#define DT_HL_CL_CPU_REGION_PX
dt_iop_buffer_dsc_t dsc_in
struct dt_iop_module_t *void * data
int kernel_hl_knee_apply_interp
int kernel_highlights_remosaic_and_replace
int kernel_highlights_bilinear_and_mask_xtrans
int kernel_highlights_box_blur
int kernel_highlights_remosaic_and_replace_xtrans
int kernel_highlights_bilinear_and_mask
dt_iop_global_data_t * global_data
Region of interest passed through the pixelpipe.
typedef double((*spd)(unsigned long int wavelength, double TempK))