Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
process.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with darktable. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19// Top-level Bayer/X-Trans CPU drivers and the hybrid OpenCL driver. (implementation; see process.h for the public
20// API.)
21
22#include "common/logging.h"
23#include "system/macros.h"
24#include "system/openmp.h"
25#include "system/simd.h"
30#include "iop/highlights/blur.h"
32#include "iop/highlights/knee.h"
37#include <math.h>
38#include <stdlib.h>
39#include <string.h>
40
43 const dt_dev_pixelpipe_iop_t *piece, const void *const restrict ivoid,
44 void *const restrict ovoid, const dt_iop_roi_t *const roi_in,
45 const dt_iop_roi_t *const roi_out, const dt_aligned_pixel_t clips)
46{
47 int err_code = 0;
48
49 // Every CFA helper below (normalization, knee estimate/apply, Bayer gather, remosaic) reads
50 // FC(row, col, filters) with tile-local row/col (0-based within this buffer, no roi offset
51 // added), so filters must be pre-shifted for roi_in's crop position here -- mirrors
52 // demosaic.c's tile-local algorithms. dt_dev_get_roi_filters() returns the shifted Bayer word,
53 // 9u unchanged for X-Trans, and 0 for already-demosaiced (non-raw / sRAW) input. The
54 // reconstruction between the gather and the remosaic is CFA-agnostic; only those two endpoints
55 // branch on `cfa` below.
56 const uint32_t filters = dt_dev_get_roi_filters(piece, roi_in);
57 const dt_hl_cfa_t cfa = _hl_cfa_strategy(filters);
58 const uint8_t(*const xtrans)[6]
59 = (cfa == HL_CFA_XTRANS) ? (const uint8_t(*const)[6])piece->dsc_in.xtrans : NULL;
60
61 const size_t height = roi_in->height;
62 const size_t width = roi_in->width;
63 const size_t size = roi_in->width * roi_in->height;
64
65 float *const restrict interpolated
66 = dt_pixelpipe_cache_alloc_align_float(size * 4, pipe); // [R, G, B, norm] for each pixel
67 float *const restrict clipping_mask
68 = dt_pixelpipe_cache_alloc_align_float(size * 4, pipe); // [R, G, B, norm] for each pixel
69
70 if(IS_NULL_PTR(interpolated) || IS_NULL_PTR(clipping_mask))
71 {
72 err_code = 1;
73 goto error;
74 }
75
76 const float *const restrict input = (const float *const restrict)ivoid;
77 float *const restrict output = (float *const restrict)ovoid;
78 dt_aligned_pixel_t normalization = { 1.f, 1.f, 1.f, 1.f };
79 _compute_laplacian_normalization(input, roi_in, filters, xtrans, normalization);
80
81 // Rolloff estimation FIRST (raw-based, mask-independent): its engagement decides, per
82 // channel, whether the detection extends into the band (the band override,
83 // DT_HL_BAND_OVR = 0.9, compile-time). Only channels with a
84 // MEASURED rolloff get the override -- on hard-clipping sensors the band is trustworthy
85 // data and stays valid. The knee reads `input` as a raw mosaic; it is meaningless for
86 // already-demosaiced input and would misread a 4-channel buffer, so it never runs in the
87 // passthrough case (knee stays disengaged there, det_scale stays unit).
88 const gboolean allow_knee = (cfa != HL_CFA_PASSTHROUGH);
89 _hl_knee_curve_t knee[3] = { 0 };
90 dt_aligned_pixel_t clipvaln = { 1.f, 1.f, 1.f, 1.f };
91 dt_aligned_pixel_t knee_clipraw = { 1.f, 1.f, 1.f, 1.f };
92 for(int c = 0; c < 3; c++)
93 {
94 clipvaln[c] = clips[c] / (DT_HL_KNEE_DET * fmaxf(normalization[c], 1e-9f));
95 knee_clipraw[c] = clips[c] / DT_HL_KNEE_DET;
96 }
97
98 // FLOW step 2 (knee): estimate the per-channel sensor-rolloff inverse from the raw mosaic (step-2 maths
99 // annotated on _hl_knee_estimate below). Runs on the raw values, before the gather, so the correction
100 // is mask-independent; applied to the interpolated planes just below via _hl_knee_apply_interpolated.
101 int knee_on = 0;
102 if(allow_knee)
103 {
104 _hl_knee_estimate(input, width, height, filters, roi_in, xtrans, knee_clipraw, knee, pipe);
105 knee_on = knee[0].engaged || knee[1].engaged || knee[2].engaged;
106 }
107
108 dt_aligned_pixel_t det_scale = { 1.f, 1.f, 1.f, 1.f };
109 for(int c = 0; c < 3; c++)
110 if(knee[c].engaged) det_scale[c] = DT_HL_BAND_OVR;
111
112 dt_aligned_pixel_t eff_clips;
113 for_four_channels(c) eff_clips[c] = clips[c] * det_scale[c];
114
115 // FLOW step 1a (gather): bilinear interpolation of the raw mosaic into [R, G, B, norm] planes + the
116 // binary per-channel clip masks -- the article's "interpolate + masks" node, input to every later step.
117 // Only this endpoint branches on the CFA: the Bayer gather multiplies clips by det_scale internally,
118 // the X-Trans gather takes the pre-multiplied eff_clips and interpolates through a 6x6 lookup.
119 switch(cfa)
120 {
121 case HL_CFA_BAYER:
122 _interpolate_and_mask(input, interpolated, clipping_mask, clips, det_scale, normalization, filters, width,
123 height);
124 break;
125 case HL_CFA_XTRANS:
126 {
127 int32_t lookup[6][6][32] = { { { 0 } } };
128 _build_xtrans_bilinear_lookup(lookup, roi_in, xtrans);
129 _interpolate_and_mask_xtrans(input, interpolated, clipping_mask, eff_clips, normalization, roi_in, lookup,
130 xtrans, width, height);
131 break;
132 }
134 // Non-raw / sRAW: no demosaic, just copy the RGB planes through + build masks. Knee is off, so
135 // clips is the plain threshold (eff_clips == clips here).
136 _interpolate_and_mask_passthrough(input, interpolated, clipping_mask, clips, normalization, width, height);
137 break;
138 }
139 // No mask feathering in this mode: the masks stay BINARY end to end. The per-channel
140 // validity masks define measurement validity for every fit (feathering them reclassified
141 // rim-clipped photosites -- raw values biased at the detection threshold -- as valid anchors
142 // and dragged oblique rims toward the clip level), and the compositing alpha is a hard
143 // switch (measured equivalent to the feathered composite once validity is binary and clipped
144 // raw values are floors -- see the graveyard of the companion article).
145
146 // Rolloff pre-correction of the working planes (the estimation ran before the gather; the
147 // lift is value-based and independent of the mask, so band values -- including any the
148 // override reclassified as reconstructable -- carry their corrected level, which the region
149 // gather then freezes into the per-pixel floors clip0).
150 if(knee_on) _hl_knee_apply_interpolated(interpolated, size, clipvaln, normalization, knee);
151
152 // MATHS BRIDGE -- Step 1 (segmentation + depth), article "The algorithm" step 1: the any-clip mask's
153 // Euclidean distance transform gives each clipped pixel its depth delta(x) (distance to the nearest
154 // valid pixel); connected-component segmentation then groups clipped pixels into regions, each
155 // carrying its reconstruction radius R = max delta over the region.
156 //
157 // Per-pixel reconstruction depth = distance from each clipped pixel to the nearest valid one
158 // (Euclidean distance transform of the any-clip mask). A hole's reconstruction radius is the max
159 // of this over the hole -- its true "reach needed", independent of the bbox shape.
160 const size_t npix = (size_t)width * height;
161 float *const restrict depth = dt_pixelpipe_cache_alloc_align_float(npix, pipe);
162 if(!depth)
163 {
164 err_code = 1;
165 goto error;
166 }
167 uint8_t *const restrict maskb = (uint8_t *)dt_pixelpipe_cache_alloc_align(npix, pipe);
168 if(!maskb)
169 {
171 err_code = 1;
172 goto error;
173 }
175 for(size_t i = 0; i < npix; i++)
176 {
177 // seed the distance transform: clipped pixels = +inf (to be filled with delta), valid = 0
178 depth[i] = (clipping_mask[i * 4 + 3] > 0.5f) ? (float)DT_DISTANCE_TRANSFORM_MAX : 0.f;
179 maskb[i] = (clipping_mask[i * 4 + 3] >= 1e-3f); // binary any-clip mask for the connected-component pass
180 }
181 dt_image_distance_transform(NULL, depth, width, height, 0.f,
182 DT_DISTANCE_TRANSFORM_NONE); // depth[] <- delta(x) (EDT)
183
184 // Segment the clipped areas into connected regions and reconstruct each at full resolution with a
185 // coarse->fine full-value guided filter (only clipped neighbourhoods are touched). Each region is
186 // padded by its reconstruction radius (the deepest clip-to-valid distance), so the padding gives
187 // the colour-line fit a valid rim as far out as the deepest pixel needs, and no farther.
188 const dt_iop_highlights_data_t *const data = (const dt_iop_highlights_data_t *)piece->data;
189 _hl_region_t *regions = NULL;
190 // 8-neighbour connected components; pad = ceil(1.25 * R) clamped to [8, 256] px around each region
191 const int nreg = _segment_clipped_regions(maskb, depth, width, height, 1.25f, 8, 256, &regions);
192
193
194 // FLOW steps 3-8 (per region): reconstruct each connected clipped region on its padded window. Regions
195 // are independent (their padded read boxes were merged when they overlapped, in _segment_clipped_regions),
196 // so this loop is embarrassingly parallel across regions and linear in the total padded area.
197 for(int region_index = 0; region_index < nreg; region_index++)
198 _region_guided_filter(interpolated, clipping_mask, depth, width, &regions[region_index], pipe,
199 data->solid_color, data->iterations, data->noise_level, _hl_floor_gate(clips),
200 dt_dev_get_module_scale(pipe, roi_in));
201
202
203 free(regions);
206
207 // The composition reads `input` back for unmasked pixels, so the band correction must also go
208 // through a corrected CFA copy -- otherwise the output band would keep the biased values the
209 // reconstruction no longer agrees with (the seam would reappear at the detection contour).
210 const float *remosaic_input = input;
211 float *input_corr = NULL;
212
213 if(knee_on)
214 {
215 input_corr = dt_pixelpipe_cache_alloc_align_float(size, pipe);
216
217 if(!IS_NULL_PTR(input_corr))
218 {
219 _hl_knee_apply_cfa(input, input_corr, width, height, filters, roi_in, xtrans, knee_clipraw, knee);
220 remosaic_input = input_corr;
221 }
222 }
223
224 // FLOW: remosaic + composite (the flowchart's terminal node). Scatter the reconstructed RGB back onto
225 // the CFA grid: out = opacity*rec + (1 - opacity)*base with opacity the binary any-clip mask, and
226 // (clip_is_floor = TRUE here) base = max(raw, rec) on a clipped photosite -- so the reconstruction can
227 // only lift a rolloff-biased sample toward its true level, never pull a valid one down. remosaic_input
228 // is the knee-corrected CFA when the knee engaged (so unmasked pixels match the reconstruction's basis).
229 // Second and last CFA-branching endpoint, mirroring the gather above.
230 switch(cfa)
231 {
232 case HL_CFA_BAYER:
233 _remosaic_and_replace(remosaic_input, input, interpolated, clipping_mask, output, normalization, clips,
234 TRUE, filters, width, height);
235 break;
236 case HL_CFA_XTRANS:
237 _remosaic_and_replace_xtrans(remosaic_input, input, interpolated, clipping_mask, output, normalization,
238 clips, TRUE, roi_in, xtrans, width, height);
239 break;
241 // Non-raw / sRAW: composite the reconstructed RGB straight back, per channel. remosaic_input ==
242 // input here (the knee never ran, so no corrected CFA copy exists).
243 _remosaic_and_replace_passthrough(remosaic_input, input, interpolated, clipping_mask, output,
244 normalization, clips, TRUE, width, height);
245 break;
246 }
247
248 if(!IS_NULL_PTR(input_corr)) dt_pixelpipe_cache_free_align(input_corr);
249
250error:;
251 dt_pixelpipe_cache_free_align(interpolated);
252 dt_pixelpipe_cache_free_align(clipping_mask);
254 (void)roi_out;
255 return err_code;
256}
257
258// ============================ OpenCL ============================
259
260#ifdef HAVE_OPENCL
261
262// Shared host middle of the harmonic reconstruction: knee estimation/correction, distance
263// transform, segmentation and the per-region rebuild -- everything between the gather and the
264// remosaic, CFA-agnostic. Used by the OpenCL hybrid driver after its GPU gather; the CPU
265// drivers keep their historical inline copies (same code, kept verbatim to avoid touching the
266// validated path -- unify when the CPU drivers next change).
267// On success *remosaic_input_out points to `input` or to a knee-corrected CFA copy
268// (*input_corr_out, caller frees with dt_pixelpipe_cache_free_align).
269//
270// MATHS/PIPELINE BRIDGE -- the CPU "middle" of the OpenCL pipe (article §"The OpenCL pipe": GPU gather
271// and GPU remosaic bracket a host middle). It runs the once-per-image steps BETWEEN the gather and the
272// remosaic on host planes the GPU already produced: step 2 knee application (_hl_knee_apply_interpolated),
273// step 1b depth + segmentation (distance transform + _segment_clipped_regions), then steps 3-8 per region
274// via the CPU _region_guided_filter. Identical code to process_harmonic's middle (kept as a
275// separate copy to avoid touching the validated CPU driver); it is the fallback the device middle
276// (_harmonic_reconstruct_cl) drops to when the GPU middle cannot run.
279 const dt_dev_pixelpipe_iop_t *piece, const float *const restrict input,
280 float *const restrict interpolated, float *const restrict clipping_mask,
281 const dt_iop_roi_t *const roi_in, const dt_aligned_pixel_t clips,
282 const dt_aligned_pixel_t normalization, const float **remosaic_input_out,
283 float **input_corr_out, const _hl_knee_curve_t knee_pre[3])
284{
285 // _hl_knee_apply_cfa below reads FC(row, col, filters) with tile-local row/col, so filters
286 // must be pre-shifted for roi_in's crop position (mirrors process_harmonic).
287 const uint32_t filters = dt_dev_get_roi_filters(piece, roi_in);
288 const uint8_t(*const xtrans)[6] = (filters == 9u) ? (const uint8_t(*const)[6])piece->dsc_in.xtrans : NULL;
289 const size_t width = roi_in->width;
290 const size_t height = roi_in->height;
291 const size_t size = width * height;
292
293 *remosaic_input_out = input;
294 *input_corr_out = NULL;
295
296 // the knee was estimated by the caller BEFORE the gather (its engagement drives the band
297 // override of the detection); reuse the curves here
298 _hl_knee_curve_t knee[3];
299 memcpy(knee, knee_pre, sizeof(knee));
300 dt_aligned_pixel_t clipvaln = { 1.f, 1.f, 1.f, 1.f };
301 dt_aligned_pixel_t knee_clipraw = { 1.f, 1.f, 1.f, 1.f };
302 for(int c = 0; c < 3; c++)
303 {
304 clipvaln[c] = clips[c] / (DT_HL_KNEE_DET * fmaxf(normalization[c], 1e-9f));
305 knee_clipraw[c] = clips[c] / DT_HL_KNEE_DET;
306 }
307 const int knee_on = knee[0].engaged || knee[1].engaged || knee[2].engaged;
308
309 if(knee_on) _hl_knee_apply_interpolated(interpolated, size, clipvaln, normalization, knee);
310
311 const size_t npix = size;
312 float *const restrict depth = dt_pixelpipe_cache_alloc_align_float(npix, pipe);
313 if(!depth) return 1;
314 uint8_t *const restrict maskb = (uint8_t *)dt_pixelpipe_cache_alloc_align(npix, pipe);
315 if(!maskb)
316 {
318 return 1;
319 }
321 for(size_t i = 0; i < npix; i++)
322 {
323 depth[i] = (clipping_mask[i * 4 + 3] > 0.5f) ? (float)DT_DISTANCE_TRANSFORM_MAX : 0.f;
324 maskb[i] = (clipping_mask[i * 4 + 3] >= 1e-3f);
325 }
327
328 const dt_iop_highlights_data_t *const data = (const dt_iop_highlights_data_t *)piece->data;
329 _hl_region_t *regions = NULL;
330 const int nreg = _segment_clipped_regions(maskb, depth, width, height, 1.25f, 8, 256, &regions);
331
332 // FLOW steps 3-8 (per region): CPU per-region reconstruction, same call as the CPU drivers.
333 for(int region_index = 0; region_index < nreg; region_index++)
334 _region_guided_filter(interpolated, clipping_mask, depth, width, &regions[region_index], pipe,
335 data->solid_color, data->iterations, data->noise_level, _hl_floor_gate(clips),
336 dt_dev_get_module_scale(pipe, roi_in));
337
338
339 free(regions);
342
343 if(knee_on)
344 {
345 float *input_corr = dt_pixelpipe_cache_alloc_align_float(size, pipe);
346 if(!IS_NULL_PTR(input_corr))
347 {
348 _hl_knee_apply_cfa(input, input_corr, width, height, filters, roi_in, xtrans, knee_clipraw, knee);
349 *remosaic_input_out = input_corr;
350 *input_corr_out = input_corr;
351 }
352 }
353
355 return 0;
356}
357
358#define HL_CL_RELEASE(mem_obj) \
359 do \
360 { \
361 dt_opencl_release_mem_object(mem_obj); \
362 (mem_obj) = NULL; \
363 } while(0)
364
365// Harmonic transposition on an OpenCL pipe: hybrid CPU-orchestrated, stage 1.
366// The reconstruction's heart is CPU by design (sparse Cholesky factorizations, per-region
367// segmentation and orchestration), so the module roundtrips the single-channel raw through
368// the host and runs the exact CPU pipeline -- the output is BIT-IDENTICAL to the CPU path
369// by construction, and the pipe keeps its CL chain (up/downstream modules stay on the GPU,
370// no scheduler-level fallback). Stage 2 (planned) slots GPU kernels into this driver where
371// they pay: the gather/remosaic kernels already exist from the a-trous path, and the
372// region moment blurs + harmonic fills are the dominant remaining cost -- at the price of
373// bit-identity with the CPU, so it must go through the full validation protocol.
374// Stage-1 fallback: full host roundtrip running the exact CPU driver (bit-identical to the
375// CPU pipe by construction). Used when any GPU gather/remosaic step fails.
376//
377// PIPELINE BRIDGE (article §"The OpenCL pipe", the bit-identical fallback): the single-channel raw
378// crosses the bus ONCE to the host (dt_opencl_copy_device_to_host), the whole CPU driver
379// (process_harmonic -- all 8 steps, gather through remosaic, CFA selected internally) runs on it, and the
380// result is written back once (dt_opencl_write_host_to_device). No GPU kernels of this mode are used, so
381// the output
382// is byte-for-byte the CPU path; the surrounding pipe still stays on the GPU (no scheduler-level fallback).
383static cl_int _harmonic_cl_roundtrip(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe,
384 const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
385 const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out,
386 const dt_aligned_pixel_t clips)
387{
388 const int devid = pipe->devid;
389 const size_t n_in = (size_t)roi_in->width * roi_in->height;
390 const size_t n_out = (size_t)roi_out->width * roi_out->height;
391
392 float *host_in = dt_pixelpipe_cache_alloc_align_float(n_in, pipe);
393 float *host_out = dt_pixelpipe_cache_alloc_align_float(n_out, pipe);
394 cl_int cl_err = DT_OPENCL_DEFAULT_ERROR;
395
396 if(IS_NULL_PTR(host_in) || IS_NULL_PTR(host_out)) goto error;
397
398 // bus crossing 1/2: pull the raw mosaic down to the host
399 cl_err = dt_opencl_copy_device_to_host(devid, host_in, dev_in, roi_in->width, roi_in->height, sizeof(float));
400 if(cl_err != CL_SUCCESS) goto error;
401
402 // run the exact CPU driver (all 8 steps, CFA selected internally) on the host copy -> bit-identical to
403 // the CPU pipe
404 if(process_harmonic(self, pipe, piece, host_in, host_out, roi_in, roi_out, clips))
405 {
407 goto error;
408 }
409
410 // bus crossing 2/2: push the reconstructed CFA back to the device
411 cl_err
412 = dt_opencl_write_host_to_device(devid, host_out, dev_out, roi_out->width, roi_out->height, sizeof(float));
413
414error:
417 return cl_err;
418}
419
420// Stage 2: the gather (normalization reduce, bilinear interpolation + clip mask, mask
421// feathering) and the scatter (remosaic) run on the GPU with the kernels shared with the
422// a-trous path; the reconstruction middle (knee, segmentation, regions -- the solvers are CPU
423// by design) runs on downloaded host planes. Any GPU failure falls back to the stage-1
424// roundtrip above.
425
426// GPU middle of the harmonic pipeline: knee estimation + application, segmentation support
427// (byte masks down, depth up -- the EDT and flood fill stay on the host, exact), and the
428// per-region reconstruction, all on device buffers. Returns CL_SUCCESS when the whole middle
429// ran on the GPU; any failure leaves the caller to run the host middle instead. corr_out
430// receives the knee-corrected 1-channel CFA buffer when the knee engages (caller releases).
431//
432// PIPELINE BRIDGE (article §"The OpenCL pipe", the device-resident middle): the once-per-image steps
433// run on device buffers -- step 2 knee, step 1b segmentation SUPPORT (only the byte seed/member masks
434// come down and the depth plane goes up; the Euclidean distance transform and connected-component flood
435// fill stay on the host because they are inherently serial), then steps 3-8 per region. The per-region
436// loop ROUTES each region by size: big regions stay device-resident (_region_guided_filter_cl); regions
437// at or below DT_HL_CL_CPU_REGION_PX cross the bus once and take the CPU path (_region_cpu_offload_cl),
438// because a device region pays ~1000 kernel launches that a small hole cannot amortize. Only byte masks,
439// the depth plane and small reduction partials ever cross the bus.
440static cl_int _harmonic_reconstruct_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe,
441 const dt_dev_pixelpipe_iop_t *piece, cl_mem raw_buf, cl_mem interp_buf,
442 cl_mem mask_buf, cl_mem *corr_out, const dt_iop_roi_t *const roi_in,
443 const dt_aligned_pixel_t clips, const dt_aligned_pixel_t norm,
444 cl_mem dev_xtrans, const _hl_knee_curve_t knee_pre[3])
445{
447 const dt_iop_highlights_data_t *const data = (const dt_iop_highlights_data_t *)piece->data;
448 const int devid = pipe->devid;
449 const uint32_t filters = piece->dsc_in.filters;
450 const int width = roi_in->width;
451 const int height = roi_in->height;
452 const size_t npix = (size_t)width * height;
453 const int is_xtrans = (filters == 9u);
454 cl_int cl_err = DT_OPENCL_DEFAULT_ERROR;
455
456 if(data->noise_level > 0.f) return cl_err; // grain epilogue is not ported
457
458 size_t sizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
459
460 cl_mem seed = NULL;
461 cl_mem member = NULL;
462 cl_mem depth_dev = NULL;
463 cl_mem corr = NULL;
464 uint8_t *h_seed = NULL;
465 uint8_t *h_member = NULL;
466 float *depth = NULL;
467 _hl_region_t *regions = NULL;
468 *corr_out = NULL;
469
470 {
471 // the knee was estimated by the caller BEFORE the gather (its engagement drives the band
472 // override of the detection); reuse the curves here
473 _hl_knee_curve_t knee[3];
474 memcpy(knee, knee_pre, sizeof(knee));
475 dt_aligned_pixel_t clipvaln = { 1.f, 1.f, 1.f, 1.f };
476 dt_aligned_pixel_t knee_clipraw = { 1.f, 1.f, 1.f, 1.f };
477 for(int c = 0; c < 3; c++)
478 {
479 clipvaln[c] = clips[c] / (DT_HL_KNEE_DET * fmaxf(norm[c], 1e-9f));
480 knee_clipraw[c] = clips[c] / DT_HL_KNEE_DET;
481 }
482 const int knee_on = knee[0].engaged || knee[1].engaged || knee[2].engaged;
483
484 if(knee_on)
485 {
486 // band correction on the interpolated RGBN planes (reconstruction fits unbiased data)
487 float lift[3 * DT_HL_KNEE_BINS];
488 for(int c = 0; c < 3; c++) memcpy(lift + c * DT_HL_KNEE_BINS, knee[c].lift, sizeof(knee[c].lift));
489 cl_mem dev_lift = _sp_cl_upload(devid, lift, sizeof(lift));
490 if(!dev_lift)
491 {
493 goto out;
494 }
495 const int kernel = global_data->kernel_hl_knee_apply_interp;
496 const cl_float4 clip4 = { { clipvaln[0], clipvaln[1], clipvaln[2], 1.f } };
497 const cl_float4 wb4 = { { norm[0], norm[1], norm[2], 1.f } };
498 const cl_int4 engaged_flags = { { knee[0].engaged, knee[1].engaged, knee[2].engaged, 0 } };
499 const float knee_lo = DT_HL_KNEE_LO;
500 const float knee_det = DT_HL_KNEE_DET;
501 const int bins = DT_HL_KNEE_BINS;
502 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &interp_buf);
503 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(int), &width);
504 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), &height);
505 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(cl_float4), &clip4);
506 dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(cl_float4), &wb4);
507 dt_opencl_set_kernel_arg(devid, kernel, 5, sizeof(cl_mem), &dev_lift);
508 dt_opencl_set_kernel_arg(devid, kernel, 6, sizeof(cl_int4), &engaged_flags);
509 dt_opencl_set_kernel_arg(devid, kernel, 7, sizeof(float), &knee_lo);
510 dt_opencl_set_kernel_arg(devid, kernel, 8, sizeof(float), &knee_det);
511 dt_opencl_set_kernel_arg(devid, kernel, 9, sizeof(int), &bins);
512 cl_err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
514 if(cl_err != CL_SUCCESS) goto out;
515
516 // corrected CFA copy for the remosaic composition
517 corr = dt_opencl_alloc_device_buffer(devid, sizeof(float) * npix);
518 if(!corr)
519 {
521 goto out;
522 }
523 cl_err = _hl_knee_apply_cfa_cl(devid, global_data, raw_buf, corr, width, height, filters, roi_in, dev_xtrans,
524 is_xtrans, knee_clipraw, knee);
525 if(cl_err != CL_SUCCESS) goto out;
526 }
527 }
528
529 // ---- segmentation support: byte masks down, exact host EDT + flood fill, depth up ----
530 seed = dt_opencl_alloc_device_buffer(devid, npix);
531 member = dt_opencl_alloc_device_buffer(devid, npix);
532 h_seed = (uint8_t *)dt_pixelpipe_cache_alloc_align(npix, pipe);
533 h_member = (uint8_t *)dt_pixelpipe_cache_alloc_align(npix, pipe);
534 depth = dt_pixelpipe_cache_alloc_align_float(npix, pipe);
535 if(!seed || !member || !h_seed || !h_member || !depth)
536 {
538 goto out;
539 }
540 {
541 const int kernel = global_data->kernel_hl_mask_pack;
542 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &mask_buf);
543 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &seed);
544 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(cl_mem), &member);
545 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), &width);
546 dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(int), &height);
547 cl_err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
548 if(cl_err != CL_SUCCESS) goto out;
549 }
550 cl_err = dt_opencl_read_buffer_from_device(devid, h_seed, seed, 0, npix, CL_TRUE);
551 if(cl_err == CL_SUCCESS) cl_err = dt_opencl_read_buffer_from_device(devid, h_member, member, 0, npix, CL_TRUE);
552 if(cl_err != CL_SUCCESS) goto out;
553
555 for(size_t i = 0; i < npix; i++) depth[i] = h_seed[i] ? (float)DT_DISTANCE_TRANSFORM_MAX : 0.f;
557
558 const int nreg = _segment_clipped_regions(h_member, depth, width, height, 1.25f, 8, 256, &regions);
559
560 depth_dev = dt_opencl_alloc_device_buffer(devid, sizeof(float) * npix);
561 if(!depth_dev)
562 {
564 goto out;
565 }
566 cl_err = dt_opencl_write_buffer_to_device(devid, depth, depth_dev, 0, sizeof(float) * npix, CL_TRUE);
567 if(cl_err != CL_SUCCESS) goto out;
568
569 {
570 // ROUTING THRESHOLD (article §"The OpenCL pipe"): DT_HL_CL_CPU_REGION_PX (~1 Mpx padded window),
571 // env-overridable for tuning. It is the padded-window pixel count above which the ~1000-launch GPU
572 // per-region path pays off; below it, one bus crossing to the CPU is cheaper.
573 size_t cpu_px = DT_HL_CL_CPU_REGION_PX;
574 const char *override_env = getenv("HL_CL_CPU_PX");
575 if(override_env) cpu_px = (size_t)strtoull(override_env, NULL, 10);
576 for(int region_index = 0; region_index < nreg && cl_err == CL_SUCCESS; region_index++)
577 {
578 // routing key = the PADDED read-window area (rx0..rx1 x ry0..ry1), the same window either path
579 // reconstructs -- not the bare clipped bbox
580 const size_t region_px = (size_t)(regions[region_index].rx1 - regions[region_index].rx0 + 1)
581 * (size_t)(regions[region_index].ry1 - regions[region_index].ry0 + 1);
582 if(region_px <= cpu_px)
583 {
584 // small region: cross the bus once and reconstruct on the CPU (bit-identical to the CPU driver)
585 cl_err = _region_cpu_offload_cl(devid, global_data, interp_buf, mask_buf, depth_dev, width,
586 &regions[region_index], pipe, data->solid_color, data->iterations,
587 data->noise_level, _hl_floor_gate(clips),
588 dt_dev_get_module_scale(pipe, roi_in));
589 }
590 else
591 // big region: stay device-resident for the whole rebuild
592 cl_err = _region_guided_filter_cl(devid, global_data, interp_buf, mask_buf, depth_dev, width,
593 &regions[region_index], pipe, data->solid_color, _hl_floor_gate(clips),
594 dt_dev_get_module_scale(pipe, roi_in));
595 }
596 }
597
598 if(cl_err == CL_SUCCESS)
599 {
600 dt_opencl_finish(devid);
601 }
602
603out:
604 _hl_gauss_cache_flush(); // the CPU-offloaded regions run _region_blur on this thread
611 free(regions);
612 if(cl_err == CL_SUCCESS)
613 *corr_out = corr;
614 else
616 return cl_err;
617}
618
620 const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
621 const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out,
622 const dt_aligned_pixel_t clips)
623{
624 _sp_chol_cl_selftest(pipe->devid, self->global_data, pipe);
625 _region_blur_cl_selftest(pipe->devid, pipe);
628 _cf_stage_cl_selftest(pipe->devid, self->global_data, pipe);
629 _hf_stage_cl_selftest(pipe->devid, self->global_data, pipe);
632 _knee_cl_selftest(pipe->devid, self->global_data, pipe);
633 _aniso_stage_cl_selftest(pipe->devid, self->global_data, pipe);
636
638 const int devid = pipe->devid;
639 // _hl_knee_estimate_cl and the hl_knee_* kernels are self-correcting: they take this raw
640 // filters value PLUS roi_in->x/y as separate kernel args and add them themselves. The shared
641 // interpolate_and_mask/remosaic_and_replace Bayer kernels (and the host-side
642 // _compute_laplacian_normalization call below) have no roi offset arg at all -- they need
643 // filters pre-shifted for roi_in's crop position instead (mirrors the CPU driver's fix).
644 const uint32_t filters = piece->dsc_in.filters;
645 const uint32_t filters_shifted = dt_dev_get_roi_filters(piece, roi_in);
646 const int width = roi_in->width;
647 const int height = roi_in->height;
648 const size_t npix = (size_t)width * height;
649 const int is_xtrans = (filters == 9u);
650 // Non-raw / sRAW passthrough: already-demosaiced 4-channel RGB input. The gather is a device plane
651 // copy, the remosaic a per-channel composite, and the knee is disabled (it would misread a 4-channel
652 // buffer as a mosaic) -- which also makes the reconstruction middle fully CFA-agnostic and reusable.
653 const int is_passthrough = (filters == 0u);
654 const size_t in_channels = is_passthrough ? 4 : 1; // channel count of dev_in for the host download below
655
656 size_t sizes[] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
657
658 cl_mem interpolated = NULL;
659 cl_mem clipping_mask = NULL;
660 cl_mem temp = NULL;
661 cl_mem clips_cl = NULL;
662 cl_mem normalization_final = NULL;
663 cl_mem dev_xtrans = NULL;
664 cl_mem lookup_cl = NULL;
665 cl_mem corr_cl = NULL;
666 cl_mem det_clips_cl = NULL;
667 float *h_interp = NULL;
668 float *h_mask = NULL;
669 float *h_raw = NULL;
670 float *input_corr = NULL;
671 const float *remosaic_input = NULL;
672 cl_int cl_err = DT_OPENCL_DEFAULT_ERROR;
673
674 interpolated = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float) * 4);
675 clipping_mask = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float) * 4);
676 temp = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float) * 4);
677 clips_cl = dt_opencl_copy_host_to_device_constant(devid, 4 * sizeof(float), (float *)clips);
678 if(IS_NULL_PTR(interpolated) || IS_NULL_PTR(clipping_mask) || IS_NULL_PTR(temp) || IS_NULL_PTR(clips_cl))
679 goto fallback;
680
681 if(is_xtrans)
682 {
683 dev_xtrans = dt_opencl_copy_host_to_device_constant(devid, sizeof(piece->dsc_in.xtrans),
684 (void *)piece->dsc_in.xtrans);
685 int32_t lookup[6][6][32] = { { { 0 } } };
686 _build_xtrans_bilinear_lookup(lookup, roi_in, (const uint8_t(*const)[6])piece->dsc_in.xtrans);
687 lookup_cl = dt_opencl_copy_host_to_device_constant(devid, sizeof(lookup), lookup);
688 if(IS_NULL_PTR(dev_xtrans) || IS_NULL_PTR(lookup_cl)) goto fallback;
689 }
690
691 // ---- per-channel normalization: computed on the HOST with the exact CPU function ----
692 // The raw is needed on the host anyway (knee estimation reads the mosaic), and the GPU
693 // max-reduce kernels are not bit-faithful to _compute_laplacian_normalization: the tiny
694 // normalization difference shifted the clip mask by a few hundred pixels and the whole
695 // reconstruction with it. Downloading first keeps the mask identical to the CPU path.
696 h_raw = dt_pixelpipe_cache_alloc_align_float(npix * in_channels, pipe);
697 if(IS_NULL_PTR(h_raw)) goto fallback;
698 cl_err = dt_opencl_copy_device_to_host(devid, h_raw, dev_in, width, height, in_channels * sizeof(float));
699 if(cl_err != CL_SUCCESS) goto fallback;
700
701 // filters_shifted is 0 for the passthrough case -> _compute_laplacian_normalization reads h_raw as a
702 // 4-channel RGB buffer (its filters==0 branch), matching the CPU passthrough path exactly.
703 dt_aligned_pixel_t norm_host = { 1.f, 1.f, 1.f, 1.f };
704 _compute_laplacian_normalization(h_raw, roi_in, filters_shifted,
705 is_xtrans ? (const uint8_t(*const)[6])piece->dsc_in.xtrans : NULL, norm_host);
706 normalization_final = dt_opencl_copy_host_to_device_constant(devid, 4 * sizeof(float), norm_host);
707 if(IS_NULL_PTR(normalization_final)) goto fallback;
708
709 // ---- rolloff estimation FIRST (raw-based): its per-channel engagement drives the band
710 // override of the detection thresholds, exactly like the CPU drivers ----
711 // Zero-initialized so the passthrough path (which skips estimation) leaves every channel disengaged;
712 // the knee is meaningless on already-demosaiced RGB and reads the raw as a mosaic, so it never runs.
713 _hl_knee_curve_t knee[3] = { 0 };
714 if(!is_passthrough)
715 {
716 dt_aligned_pixel_t knee_clipraw = { 1.f, 1.f, 1.f, 1.f };
717 for(int c = 0; c < 3; c++) knee_clipraw[c] = clips[c] / DT_HL_KNEE_DET;
718
719 // the knee kernels read the raw as a BUFFER; dev_in is an image2d -> copy first
720 cl_mem knee_raw = dt_opencl_alloc_device_buffer(devid, sizeof(float) * npix);
721 if(IS_NULL_PTR(knee_raw)) goto fallback;
722 size_t korigin[3] = { 0, 0, 0 };
723 size_t kregion[3] = { (size_t)width, (size_t)height, 1 };
724 cl_err = dt_opencl_enqueue_copy_image_to_buffer(devid, dev_in, knee_raw, korigin, kregion, 0);
725 if(cl_err != CL_SUCCESS)
726 {
728 goto fallback;
729 }
730
731 cl_err = _hl_knee_estimate_cl(devid, global_data, knee_raw, width, height, filters, roi_in, dev_xtrans,
732 is_xtrans, knee_clipraw, knee, pipe);
734 if(cl_err != CL_SUCCESS) goto fallback;
735 }
736
737 dt_aligned_pixel_t eff_clips;
738 for_four_channels(c) eff_clips[c] = clips[c];
739 for(int c = 0; c < 3; c++)
740 if(knee[c].engaged) eff_clips[c] = clips[c] * DT_HL_BAND_OVR;
741 det_clips_cl = dt_opencl_copy_host_to_device_constant(devid, 4 * sizeof(float), eff_clips);
742 if(IS_NULL_PTR(det_clips_cl)) goto fallback;
743
744 // ---- gather: bilinear interpolation + clip mask, then 5x5 feathering ----
745 if(is_xtrans)
746 {
747 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 0, sizeof(cl_mem),
748 &dev_in);
749 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 1, sizeof(cl_mem),
750 &interpolated);
751 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 2, sizeof(cl_mem),
752 &clipping_mask);
753 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 3, sizeof(cl_mem),
754 &det_clips_cl);
755 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 4, sizeof(cl_mem),
756 &normalization_final);
758 &width);
760 &height);
762 &roi_in->x);
764 &roi_in->y);
765 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 9, sizeof(cl_mem),
766 &dev_xtrans);
767 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask_xtrans, 10, sizeof(cl_mem),
768 &lookup_cl);
770 }
771 else if(is_passthrough)
772 {
773 // Non-raw / sRAW: plane copy + per-channel clip mask, written straight into clipping_mask (harmonic
774 // masks stay binary -- no 5x5 feathering). The knee is off, so det_clips_cl holds the plain clips.
776 sizeof(cl_mem), &dev_in);
778 sizeof(cl_mem), &interpolated);
780 sizeof(cl_mem), &clipping_mask);
782 sizeof(cl_mem), &det_clips_cl);
784 sizeof(cl_mem), &normalization_final);
786 &width);
788 &height);
790 sizes);
791 }
792 else
793 {
794 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 0, sizeof(cl_mem), &dev_in);
795 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 1, sizeof(cl_mem),
796 &interpolated);
797 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 2, sizeof(cl_mem),
798 &clipping_mask);
799 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 3, sizeof(cl_mem),
800 &det_clips_cl);
801 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 4, sizeof(cl_mem),
802 &normalization_final);
803 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 5, sizeof(int),
804 &filters_shifted);
805 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 6, sizeof(int),
806 &roi_out->width);
807 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_bilinear_and_mask, 7, sizeof(int),
808 &roi_out->height);
809 cl_err = dt_opencl_enqueue_kernel_2d(devid, global_data->kernel_highlights_bilinear_and_mask, sizes);
810 }
811 if(cl_err != CL_SUCCESS) goto fallback;
812
813 // ---- GPU middle first: knee + segmentation support + per-region reconstruction on device
814 // buffers; only byte masks, the depth plane and reduction partials cross the bus ----
815 {
816 cl_mem raw_buf = dt_opencl_alloc_device_buffer(devid, sizeof(float) * npix);
817 cl_mem interp_buf = dt_opencl_alloc_device_buffer(devid, sizeof(float) * npix * 4);
818 cl_mem mask_buf = dt_opencl_alloc_device_buffer(devid, sizeof(float) * npix * 4);
819 cl_mem corr_buf = NULL;
820 size_t origin[3] = { 0, 0, 0 };
821 size_t region1[3] = { (size_t)width, (size_t)height, 1 };
822 cl_int gpu_err = (raw_buf && interp_buf && mask_buf) ? CL_SUCCESS : DT_OPENCL_DEFAULT_ERROR;
823 // raw_buf holds the single-channel raw mosaic for the knee; the passthrough path has no knee and no
824 // mosaic (dev_in is 4-channel), so skip this copy -- the middle never reads raw_buf when knee is off.
825 if(gpu_err == CL_SUCCESS && !is_passthrough)
826 gpu_err = dt_opencl_enqueue_copy_image_to_buffer(devid, dev_in, raw_buf, origin, region1, 0);
827 if(gpu_err == CL_SUCCESS)
828 gpu_err = dt_opencl_enqueue_copy_image_to_buffer(devid, interpolated, interp_buf, origin, region1, 0);
829 if(gpu_err == CL_SUCCESS)
830 gpu_err = dt_opencl_enqueue_copy_image_to_buffer(devid, clipping_mask, mask_buf, origin, region1, 0);
831 int staged = 0; // 1 = the three images below were released and must be re-created
832 if(gpu_err == CL_SUCCESS)
833 {
834 // the middle works on the buffers: release the three full-image images (~1.7 GB on a
835 // 36 Mpx raw) so the region planes and stage temporaries fit in vRAM; the two that are
836 // consumed downstream are re-created from the buffers right after. The HL_MIDDLE_AB
837 // diagnostic keeps them alive instead: its reference run needs the PRISTINE planes.
838 dt_opencl_finish(devid); // the async image->buffer copies must land first
839 if(!getenv("HL_MIDDLE_AB"))
840 {
841 HL_CL_RELEASE(temp);
842 HL_CL_RELEASE(interpolated);
843 HL_CL_RELEASE(clipping_mask);
844 staged = 1;
845 }
846 // preference 1: the device-resident middle (steps 2 + 1b + 3-8 on device, small regions offloaded)
847 gpu_err = _harmonic_reconstruct_cl(self, pipe, piece, raw_buf, interp_buf, mask_buf, &corr_buf, roi_in,
848 clips, norm_host, dev_xtrans, knee);
849 }
850 // materialize the knee-corrected mosaic BEFORE restoring the working images: a failure
851 // here must take the same pristine re-gather road as a mid-middle failure. Falling
852 // through with the reconstruction already copied back would hand the host middle
853 // knee-lifted, partially reconstructed planes -- the knee would be applied twice and
854 // the regions re-solved on reconstructed anchors, silently.
855 if(gpu_err == CL_SUCCESS && corr_buf)
856 {
857 corr_cl = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float));
858 if(corr_cl)
859 gpu_err = dt_opencl_enqueue_copy_buffer_to_image(devid, corr_buf, corr_cl, 0, origin, region1);
860 else
861 gpu_err = DT_OPENCL_DEFAULT_ERROR;
862 if(gpu_err != CL_SUCCESS) HL_CL_RELEASE(corr_cl);
863 }
864 // HL_MIDDLE_AB=1 (diagnostic): run the host middle on the pristine planes (kept alive
865 // above) and print its divergence from the device middle; the device result still ships
866 if(gpu_err == CL_SUCCESS && getenv("HL_MIDDLE_AB"))
867 {
868 float *gpu_interp = dt_pixelpipe_cache_alloc_align_float(npix * 4, pipe);
869 float *host_interp = dt_pixelpipe_cache_alloc_align_float(npix * 4, pipe);
870 float *host_mask = dt_pixelpipe_cache_alloc_align_float(npix * 4, pipe);
871 float *host_raw = dt_pixelpipe_cache_alloc_align_float(npix, pipe);
872 if(gpu_interp && host_interp && host_mask && host_raw
873 && dt_opencl_read_buffer_from_device(devid, gpu_interp, interp_buf, 0, sizeof(float) * npix * 4, CL_TRUE)
874 == CL_SUCCESS
875 && dt_opencl_copy_device_to_host(devid, host_interp, interpolated, width, height, sizeof(float) * 4)
876 == CL_SUCCESS
877 && dt_opencl_copy_device_to_host(devid, host_mask, clipping_mask, width, height, sizeof(float) * 4)
878 == CL_SUCCESS
879 && dt_opencl_copy_device_to_host(devid, host_raw, dev_in, width, height, sizeof(float)) == CL_SUCCESS)
880 {
881 const float *remosaic_ptr = NULL;
882 float *input_corr_ab = NULL;
883 if(!_harmonic_reconstruct_host(self, pipe, piece, host_raw, host_interp, host_mask, roi_in, clips,
884 norm_host, &remosaic_ptr, &input_corr_ab, knee))
885 {
886 float max_diff = 0.f;
887 double sum_diff = 0.0;
888 size_t arg_index = 0;
889 for(size_t i = 0; i < npix * 4; i++)
890 {
891 const float diff = fabsf(gpu_interp[i] - host_interp[i]);
892 if(diff > max_diff)
893 {
894 max_diff = diff;
895 arg_index = i;
896 }
897 sum_diff += (double)diff;
898 }
899 fprintf(stderr, "[hl middle AB] max=%.3e mean=%.3e at px=(%llu,%llu) c=%llu gpu=%f cpu=%f\n", max_diff,
900 sum_diff / (double)(npix * 4), (unsigned long long)((arg_index / 4) % width),
901 (unsigned long long)((arg_index / 4) / width), (unsigned long long)(arg_index % 4),
902 gpu_interp[arg_index], host_interp[arg_index]);
903 }
904 dt_pixelpipe_cache_free_align(input_corr_ab);
905 }
910 }
911 // restore the images for the downstream blend/remosaic -- ONLY when they were released:
912 // an early staging failure leaves them alive and pristine, and reallocating over the
913 // live handles would leak them (~1.7 GB) while doubling vRAM demand under the very
914 // pressure that made staging fail. On success, interpolated carries
915 // the reconstruction and the mask is untouched (buffer copy-back). On FAILURE the middle
916 // may have partially scattered and knee-lifted interp_buf, so the host fallback must NOT
917 // reuse it: re-run the interpolation + mask blur from the still-alive inputs instead.
918 if(staged)
919 {
920 interpolated = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float) * 4);
921 clipping_mask = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float) * 4);
922 cl_int restore_err = (interpolated && clipping_mask) ? CL_SUCCESS : DT_OPENCL_DEFAULT_ERROR;
923 if(restore_err == CL_SUCCESS && gpu_err == CL_SUCCESS)
924 {
925 restore_err = dt_opencl_enqueue_copy_buffer_to_image(devid, interp_buf, interpolated, 0, origin, region1);
926 if(restore_err == CL_SUCCESS)
927 restore_err = dt_opencl_enqueue_copy_buffer_to_image(devid, mask_buf, clipping_mask, 0, origin, region1);
928 }
929 else if(restore_err == CL_SUCCESS)
930 {
931 temp = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float) * 4);
932 if(!temp) restore_err = DT_OPENCL_DEFAULT_ERROR;
933 if(restore_err == CL_SUCCESS)
934 {
935 if(is_xtrans)
936 {
937 const int kernel = global_data->kernel_highlights_bilinear_and_mask_xtrans;
938 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &dev_in);
939 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &interpolated);
940 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(cl_mem), &temp);
941 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(cl_mem), &clips_cl);
942 dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(cl_mem), &normalization_final);
943 dt_opencl_set_kernel_arg(devid, kernel, 5, sizeof(int), &roi_out->width);
944 dt_opencl_set_kernel_arg(devid, kernel, 6, sizeof(int), &roi_out->height);
945 dt_opencl_set_kernel_arg(devid, kernel, 7, sizeof(int), &roi_in->x);
946 dt_opencl_set_kernel_arg(devid, kernel, 8, sizeof(int), &roi_in->y);
947 dt_opencl_set_kernel_arg(devid, kernel, 9, sizeof(cl_mem), &dev_xtrans);
948 dt_opencl_set_kernel_arg(devid, kernel, 10, sizeof(cl_mem), &lookup_cl);
949 restore_err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
950 }
951 else
952 {
953 const int kernel = global_data->kernel_highlights_bilinear_and_mask;
954 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &dev_in);
955 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &interpolated);
956 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(cl_mem), &temp);
957 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(cl_mem), &clips_cl);
958 dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(cl_mem), &normalization_final);
959 dt_opencl_set_kernel_arg(devid, kernel, 5, sizeof(int), &filters_shifted);
960 dt_opencl_set_kernel_arg(devid, kernel, 6, sizeof(int), &roi_out->width);
961 dt_opencl_set_kernel_arg(devid, kernel, 7, sizeof(int), &roi_out->height);
962 restore_err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
963 }
964 }
965 if(restore_err == CL_SUCCESS)
966 {
967 const int kernel = global_data->kernel_highlights_box_blur;
968 dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), &temp);
969 dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), &clipping_mask);
970 dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), &roi_out->width);
971 dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), &roi_out->height);
972 restore_err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
973 }
974 }
975 if(restore_err != CL_SUCCESS)
976 {
981 cl_err = restore_err;
982 goto fallback;
983 }
984 }
985 else if(gpu_err == CL_SUCCESS)
986 {
987 // diagnostic mode kept the images alive: publish the device result into them now
988 cl_int restore_err
989 = dt_opencl_enqueue_copy_buffer_to_image(devid, interp_buf, interpolated, 0, origin, region1);
990 if(restore_err == CL_SUCCESS)
991 restore_err = dt_opencl_enqueue_copy_buffer_to_image(devid, mask_buf, clipping_mask, 0, origin, region1);
992 if(restore_err != CL_SUCCESS)
993 {
998 cl_err = restore_err;
999 goto fallback;
1000 }
1001 }
1003 dt_opencl_release_mem_object(interp_buf);
1006 if(gpu_err == CL_SUCCESS) goto remosaic; // device middle succeeded -> straight to the GPU remosaic
1007 // GPU middle unavailable (fp64 device, grain requested, oversized hole...): host middle below
1008 HL_CL_RELEASE(corr_cl);
1009 dt_print(DT_DEBUG_OPENCL, "[opencl_highlights] harmonic GPU middle failed (%i), using the host middle\n",
1010 gpu_err);
1011 }
1012
1013 // ---- host-middle path (preference 2): the GPU gather succeeded but the device middle could not run.
1014 // Pull the gathered working planes down (the raw h_raw is already resident) so the CPU middle can
1015 // run the same steps 2 + 1b + 3-8 it would on a CPU pipe ----
1016 h_interp = dt_pixelpipe_cache_alloc_align_float(npix * 4, pipe);
1017 h_mask = dt_pixelpipe_cache_alloc_align_float(npix * 4, pipe);
1018 if(IS_NULL_PTR(h_interp) || IS_NULL_PTR(h_mask)) goto fallback;
1019
1020 cl_err = dt_opencl_copy_device_to_host(devid, h_interp, interpolated, width, height, sizeof(float) * 4);
1021 if(cl_err != CL_SUCCESS) goto fallback;
1022 cl_err = dt_opencl_copy_device_to_host(devid, h_mask, clipping_mask, width, height, sizeof(float) * 4);
1023 if(cl_err != CL_SUCCESS) goto fallback;
1024
1025 // ---- CPU middle: knee, segmentation, per-region reconstruction ----
1026 if(_harmonic_reconstruct_host(self, pipe, piece, h_raw, h_interp, h_mask, roi_in, clips, norm_host,
1027 &remosaic_input, &input_corr, knee))
1028 goto fallback;
1029
1030 // ---- upload the reconstructed planes back to the device so the GPU remosaic below closes the pipe ----
1031 cl_err = dt_opencl_write_host_to_device(devid, h_interp, interpolated, width, height, sizeof(float) * 4);
1032 if(cl_err != CL_SUCCESS) goto fallback;
1033
1034remosaic:;
1035 // FLOW: GPU remosaic + composite (the pipe's terminal node, reached from either middle). Pick the base
1036 // CFA the composite reads on unmasked sites: the knee-corrected copy (corr_cl / input_corr) when the
1037 // knee engaged, else the pristine dev_in -- so valid pixels match the reconstruction's basis.
1038 cl_mem remosaic_in_cl = dev_in;
1039 if(corr_cl)
1040 remosaic_in_cl = corr_cl;
1041 else if(remosaic_input != h_raw && remosaic_input != NULL)
1042 {
1043 corr_cl = dt_opencl_alloc_device(devid, sizes[0], sizes[1], sizeof(float));
1044 if(IS_NULL_PTR(corr_cl)) goto fallback;
1045 cl_err = dt_opencl_write_host_to_device(devid, input_corr, corr_cl, width, height, sizeof(float));
1046 if(cl_err != CL_SUCCESS) goto fallback;
1047 remosaic_in_cl = corr_cl;
1048 }
1049
1050 if(is_xtrans)
1051 {
1052 const int clip_floor_on = TRUE; // clipped raw values are floors, never blend targets
1053 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 0, sizeof(cl_mem),
1054 &remosaic_in_cl);
1055 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 1, sizeof(cl_mem),
1056 &dev_in);
1057 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 2, sizeof(cl_mem),
1058 &interpolated);
1059 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 3, sizeof(cl_mem),
1060 &clipping_mask);
1061 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 4, sizeof(cl_mem),
1062 &dev_out);
1063 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 5, sizeof(cl_mem),
1064 &normalization_final);
1065 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 6, sizeof(cl_mem),
1066 &clips_cl);
1068 &clip_floor_on);
1070 &width);
1072 &height);
1074 &roi_in->x);
1076 &roi_in->y);
1077 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace_xtrans, 12, sizeof(cl_mem),
1078 &dev_xtrans);
1080 }
1081 else if(is_passthrough)
1082 {
1083 // Non-raw / sRAW: per-channel composite straight back (no CFA). remosaic_in_cl == dev_in (no knee,
1084 // so no corrected copy). clip_is_floor stays TRUE, matching the CPU passthrough remosaic.
1085 const int clip_floor_on = TRUE;
1087 sizeof(cl_mem), &remosaic_in_cl);
1089 sizeof(cl_mem), &dev_in);
1091 sizeof(cl_mem), &interpolated);
1093 sizeof(cl_mem), &clipping_mask);
1095 sizeof(cl_mem), &dev_out);
1097 sizeof(cl_mem), &normalization_final);
1099 sizeof(cl_mem), &clips_cl);
1101 sizeof(int), &clip_floor_on);
1103 sizeof(int), &width);
1105 sizeof(int), &height);
1107 sizes);
1108 }
1109 else
1110 {
1111 const int clip_floor_on = TRUE; // clipped raw values are floors, never blend targets
1112 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 0, sizeof(cl_mem),
1113 &remosaic_in_cl);
1114 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 1, sizeof(cl_mem),
1115 &dev_in);
1116 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 2, sizeof(cl_mem),
1117 &interpolated);
1118 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 3, sizeof(cl_mem),
1119 &clipping_mask);
1120 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 4, sizeof(cl_mem),
1121 &dev_out);
1122 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 5, sizeof(cl_mem),
1123 &normalization_final);
1124 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 6, sizeof(cl_mem),
1125 &clips_cl);
1126 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 7, sizeof(int),
1127 &clip_floor_on);
1128 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 8, sizeof(int),
1129 &filters_shifted);
1130 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 9, sizeof(int), &width);
1131 dt_opencl_set_kernel_arg(devid, global_data->kernel_highlights_remosaic_and_replace, 10, sizeof(int), &height);
1132 cl_err = dt_opencl_enqueue_kernel_2d(devid, global_data->kernel_highlights_remosaic_and_replace, sizes);
1133 }
1134 if(cl_err != CL_SUCCESS) goto fallback;
1135
1136 // success: release and return
1137 HL_CL_RELEASE(clips_cl);
1138 HL_CL_RELEASE(det_clips_cl);
1139 HL_CL_RELEASE(normalization_final);
1140 HL_CL_RELEASE(interpolated);
1141 HL_CL_RELEASE(clipping_mask);
1142 HL_CL_RELEASE(temp);
1143 HL_CL_RELEASE(dev_xtrans);
1144 HL_CL_RELEASE(lookup_cl);
1145 HL_CL_RELEASE(corr_cl);
1150 return CL_SUCCESS;
1151
1152fallback:
1154 "[opencl_highlights] harmonic GPU gather failed (%i), falling back to the host roundtrip\n", cl_err);
1155 HL_CL_RELEASE(clips_cl);
1156 HL_CL_RELEASE(det_clips_cl);
1157 HL_CL_RELEASE(normalization_final);
1158 HL_CL_RELEASE(interpolated);
1159 HL_CL_RELEASE(clipping_mask);
1160 HL_CL_RELEASE(temp);
1161 HL_CL_RELEASE(dev_xtrans);
1162 HL_CL_RELEASE(lookup_cl);
1163 HL_CL_RELEASE(corr_cl);
1168 // preference 3 (last resort): a GPU gather/remosaic step failed -> the bit-identical host roundtrip.
1169 return _harmonic_cl_roundtrip(self, pipe, piece, dev_in, dev_out, roi_in, roi_out, clips);
1170}
1171#endif // HAVE_OPENCL
static void error(char *msg)
Definition ashift_lsd.c:202
#define TRUE
Definition ashift_lsd.c:162
void _hl_gauss_cache_flush(void)
Definition blur.c:47
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
static float lookup(read_only image2d_t lut, const float x)
const dt_colormatrix_t dt_aligned_pixel_t out
@ DT_DISTANCE_TRANSFORM_NONE
float dt_image_distance_transform(float *const restrict src, float *const restrict out, const size_t width, const size_t height, const float clip, const dt_distance_transform_t mode)
#define DT_DISTANCE_TRANSFORM_MAX
__DT_CLONE_TARGETS__ void _interpolate_and_mask_passthrough(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 size_t width, const size_t height)
Definition gather.c:424
__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)
Definition gather.c:223
__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)
Definition gather.c:488
__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)
Definition gather.c:67
__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)
Definition gather.c:457
__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)
Definition gather.c:317
__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])
Definition gather.c:277
__DT_CLONE_TARGETS__ void _remosaic_and_replace_passthrough(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 size_t width, const size_t height)
Definition gather.c:514
static dt_hl_cfa_t _hl_cfa_strategy(const uint32_t filters)
Definition gather.h:74
dt_hl_cfa_t
Definition gather.h:67
@ HL_CFA_PASSTHROUGH
Definition gather.h:70
@ HL_CFA_BAYER
Definition gather.h:68
@ HL_CFA_XTRANS
Definition gather.h:69
uint32_t dt_dev_get_roi_filters(const dt_dev_pixelpipe_iop_t *const piece, const dt_iop_roi_t *const roi_in)
Definition imageop.c:139
float dt_dev_get_module_scale(const dt_dev_pixelpipe_t *const pipe, const dt_iop_roi_t *const roi_in)
Definition imageop.c:134
void *const ovoid
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])
Definition knee.c:517
__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)
Definition knee.c:107
__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])
Definition knee.c:555
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)
Definition knee.c:586
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])
Definition knee.c:919
@ DT_DEBUG_OPENCL
Definition logging.h:57
void dt_print(dt_debug_thread_t thread, const char *msg,...) __attribute__((format(printf
Print to stdout when thread is enabled, prefixed with seconds since startup.
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
size_t size
Definition mipmap_cache.c:3
int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
Definition opencl.c:2554
void * dt_opencl_alloc_device_buffer(const int devid, const size_t size)
Definition opencl.c:2970
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)
Definition opencl.c:2702
int dt_opencl_copy_device_to_host(const int devid, void *host, void *device, const int width, const int height, const int bpp)
Definition opencl.c:2581
void * dt_opencl_alloc_device(const int devid, const int width, const int height, const int bpp)
Definition opencl.c:2894
void * dt_opencl_copy_host_to_device_constant(const int devid, const size_t size, void *host)
Definition opencl.c:2750
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)
Definition opencl.c:2738
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)
Definition opencl.c:2727
int dt_opencl_set_kernel_arg(const int dev, const int kernel, const int num, const size_t size, const void *arg)
Definition opencl.c:2545
gboolean dt_opencl_finish(const int devid)
Definition opencl.c:1671
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)
Definition opencl.c:2690
void dt_opencl_release_mem_object(cl_mem mem)
Definition opencl.c:2805
int dt_opencl_write_host_to_device(const int devid, void *host, void *device, const int width, const int height, const int bpp)
Definition opencl.c:2634
#define DT_OPENCL_DEFAULT_ERROR
Definition opencl.h:61
#define ROUNDUPDHT(a, b)
Definition opencl.h:86
#define ROUNDUPDWD(a, b)
Definition opencl.h:85
#define __OMP_PARALLEL_FOR__(...)
Definition openmp.h:95
#define dt_pixelpipe_cache_alloc_align(size, pipe)
#define dt_pixelpipe_cache_free_align(mem)
#define dt_pixelpipe_cache_alloc_align_float(pixels, pipe)
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)
Definition process.c:619
__DT_CLONE_TARGETS__ int process_harmonic(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)
Definition process.c:42
#define HL_CL_RELEASE(mem_obj)
Definition process.c:358
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])
Definition process.c:278
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)
Definition process.c:383
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])
Definition process.c:440
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, const float floor_gate, const float module_scale)
Definition region.c:151
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)
Definition selftests.c:920
void _region_guided_filter_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:1940
void _knee_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:2046
void _joint_core_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:1400
void _selfdome_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:1168
void _aniso_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:1598
void _cf_harmonic_fill_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:195
void _cf_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:564
void _region_blur_cl_selftest(const int devid, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:145
void _chromaticity_gradient_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:1786
void _cf_joint_stage_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:302
void _sp_chol_cl_selftest(const int devid, void *gd_void, const dt_dev_pixelpipe_t *pipe)
Definition selftests.c:41
DT_ALIGNED_PIXEL float dt_aligned_pixel_t[4]
Definition simd.h:53
#define for_four_channels(_var,...)
Definition simd.h:89
static cl_mem _sp_cl_upload(const int devid, const void *data, const size_t bytes)
static float _hl_floor_gate(const float clips[4])
#define DT_HL_CL_CPU_REGION_PX
#define DT_HL_KNEE_DET
#define DT_HL_BAND_OVR
#define DT_HL_KNEE_LO
#define DT_HL_KNEE_BINS
dt_iop_buffer_dsc_t dsc_in
struct dt_iop_module_t *void * data
uint32_t filters
Definition format.h:89
uint8_t xtrans[6][6]
Definition format.h:99
dt_iop_global_data_t * global_data
Definition imageop.h:337
Region of interest passed through the pixelpipe.
Definition format.h:49
int width
Definition format.h:50
int height
Definition format.h:50
#define __DT_CLONE_TARGETS__
typedef double((*spd)(unsigned long int wavelength, double TempK))