Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
rawdenoiseai.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 Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/*
20 * Neural raw (CFA-domain) denoiser.
21 *
22 * Runs a sigma-map-conditioned U-Net (common/nn_model.{h,c}) on the mosaiced
23 * buffer, before demosaicing, where sensor noise is still per-sensel
24 * independent. The network was trained on synthetic Poisson-Gaussian noise
25 * drawn from the community noise-profile database, conditioned on the
26 * per-pixel noise standard deviation — so one set of weights serves every
27 * profiled camera, Bayer and X-Trans alike, and a newly profiled camera is
28 * supported without retraining. Training pipeline:
29 * https://github.com/aurelienpierreeng/ansel-denoise
30 *
31 * The sigma map is computed from the per-channel variance line
32 * Var(x) = a*x + b of the matched noise profile at the image ISO, applied in
33 * the post-rawprepare normalized domain — the exact domain the profiles were
34 * fitted in and the training used. Do NOT copy denoiseprofile's white-balance
35 * adjustments here: those compensate its post-demosaic, post-WB position.
36 *
37 * User parameters:
38 * - "strength": opacity of the correction, an alpha blend of the inferred
39 * noise residual: out = in + strength * (denoised - in).
40 * - model version / size (large|half|quarter network width) / variant (single-scale
41 * or multiscale) select the weights file
42 * denoise-<size>-<single|multi>-<version>.anselnn.
43 * - "global correction" and the per-channel R/G/B corrections scale the
44 * sigma map (see the calibration note above the params struct).
45 *
46 * Weights are loaded once per session from the user config dir (override for
47 * testing) or <datadir>/. Without a weights file the module stays disabled.
48 * The training counterpart of every inference step lives in the
49 * ansel-denoise repository: _k_assemble() <-> dataset.py,
50 * _k_bin_planes() <-> cfa.bin_mosaic_torch()/bin_sigma_torch(),
51 * the coarse->fine guide flow <-> train.ms_forward(), and
52 * _apply_low_band_anchor() <-> cfa.fuse_low_bands(). Keep them in sync.
53 */
54
55#ifdef HAVE_CONFIG_H
56#include "config.h"
57#endif
58#include "widgets/bauhaus.h"
59#include "common/paths.h" // DT_PATH_MAX
60#include "system/macros.h"
61#include "system/openmp.h"
63#include "system/mem_alloc.h"
64#include "common/logging.h"
66#include <json-glib/json-glib.h>
69#include "common/imagebuf.h"
70#include "common/nn_model.h"
72#include "common/opencl.h"
73#include "develop/imageop.h"
74#include "develop/imageop_gui.h"
76#include "develop/tiling.h"
77#include "iop/iop_api.h"
78
79#include <gtk/gtk.h>
80#include <stdlib.h>
82#include "widgets/label.h"
83
84#define DT_RAWDENOISEAI_MODEL_LEN 128
85
87
88/* Model version: pins which trained network a history entry uses, so results
89 * stay reproducible across app updates. A new release that retrains the net
90 * appends a value here (and ships the matching weights file) instead of
91 * replacing v1 — old edits keep rendering with the model they were made on. */
96
97/* Model size: same architecture family, different width of the FINE net
98 * (32 / 16 / 8; the coarse chroma net of a multiscale model stays at 32 for
99 * every size — it runs on the superpixel-binned image and costs a few
100 * percent). large is the reference quality (practical with OpenCL), half is
101 * ~4x cheaper for the CPU path, quarter ~4x cheaper again for very weak
102 * hardware and near-realtime editing. The outputs are NOT interchangeable,
103 * hence a user parameter rather than a silent runtime choice. */
105{
106 DT_RAWDENOISEAI_LARGE = 0, // $DESCRIPTION: "large"
107 DT_RAWDENOISEAI_HALF = 1, // $DESCRIPTION: "half"
108 DT_RAWDENOISEAI_QUARTER = 2, // $DESCRIPTION: "quarter"
110
111/* Model variant: single-scale (the fine mosaic net alone — fast, no
112 * low-frequency chroma handling) vs multiscale (coarse chroma net guiding
113 * the fine net, plus the hybrid low-band fusion — high quality). */
115{
116 DT_RAWDENOISEAI_SINGLE = 0, // $DESCRIPTION: "single-scale"
117 DT_RAWDENOISEAI_MULTI = 1, // $DESCRIPTION: "multiscale"
119
120#define DT_RAWDENOISEAI_NUM_VERSIONS 1
121#define DT_RAWDENOISEAI_NUM_SIZES 3
122#define DT_RAWDENOISEAI_NUM_SCALES 2
123
124// filename components per enum value; the weights file is
125// denoise-<size>-<single|multi>-<version>.anselnn
126static const char *const _version_tag[DT_RAWDENOISEAI_NUM_VERSIONS] = { "v1" };
127static const char *const _size_tag[DT_RAWDENOISEAI_NUM_SIZES] = { "large", "half", "quarter" };
128static const char *const _scale_tag[DT_RAWDENOISEAI_NUM_SCALES] = { "single", "multi" };
129
130/* The shipped noise profiles understate the true mosaic-domain sigma by an
131 * exact factor 2 before any demosaic effect: tools/noise/noiseprofile.c
132 * estimates sigma as MAD/0.6745 of the HH band of a decimated lifting Haar
133 * whose normalization is HH = (x00 - x01 - x10 + x11)/4, so std(HH) = sigma/2
134 * for iid noise (the orthonormal Haar assumed by the MAD rule divides by 2).
135 * The gnuplot fit in ansel-gen-noiseprofile squares that std into (a, b)
136 * without correction, so every profile carries 1/4 of the physical variance.
137 * Historical consumers (denoiseprofile) were tuned end to end around these
138 * units; this module is the first to treat (a, b) as absolute physical
139 * variance, so the correction lives here — the shared profile database must
140 * stay consistent with a decade of fits and cannot change.
141 *
142 * The remaining, channel-dependent part of the deviation (the profiles are
143 * fitted on demosaiced pixels, and interpolation averages away high-frequency
144 * noise — most on the dense green lattice) is exposed as the per-channel
145 * corrections below, calibrated by measuring flat-region noise on raw mosaics
146 * against the profile prediction: 253 profiled cameras (one raw.pixls.us
147 * sample each) plus 64 images across ISO 64-12800 on three local bodies.
148 * Cross-camera medians (estimator-bias corrected): R 1.41, G 1.97, B 1.48
149 * after the factor 2 above; the deviation is ISO-stable.
150 *
151 * The whole correction is carried by the three GUI sliders and nothing
152 * else: what the user sees is exactly what multiplies the profile sigma
153 * (times the global correction). No hidden constants, no model-side
154 * multiplication — a model's cfg may document the sigma convention it was
155 * trained under, but the module never applies it behind the user's back
156 * (a hidden factor stacked with visible sliders is how the calibration got
157 * silently applied twice — the yellow-cast field bug). The slider defaults
158 * are the calibration itself: 2 x the sweep medians above. */
159
161{
162 float strength; // $MIN: 0.0 $MAX: 1.0 $DEFAULT: 0.85 $DESCRIPTION: "strength"
163 dt_iop_rawdenoiseai_version_t version; // $DEFAULT: DT_RAWDENOISEAI_V1 $DESCRIPTION: "model version"
164 dt_iop_rawdenoiseai_size_t size; // $DEFAULT: DT_RAWDENOISEAI_QUARTER $DESCRIPTION: "model size"
165 float noise_level; // $MIN: 0.0 $MAX: 2.0 $DEFAULT: 1.0 $DESCRIPTION: "global correction"
166 float sigma_red; // $MIN: 0.5 $MAX: 8.0 $DEFAULT: 2.82 $DESCRIPTION: "red correction"
167 float sigma_green; // $MIN: 0.5 $MAX: 8.0 $DEFAULT: 3.94 $DESCRIPTION: "green correction"
168 float sigma_blue; // $MIN: 0.5 $MAX: 8.0 $DEFAULT: 2.96 $DESCRIPTION: "blue correction"
169 dt_iop_rawdenoiseai_scale_t scale_variant; // $DEFAULT: DT_RAWDENOISEAI_MULTI $DESCRIPTION: "model variant"
170 /* Empty: use the shipped model selected by (version, size, scale) above.
171 * Otherwise the basename of a .anselnn in the user config dir, which
172 * overrides all three. Stored by NAME, never by list position: the set of
173 * files on disk changes between sessions, and an index would silently
174 * re-point every history entry that used it (same reason colorin stores
175 * its ICC filename). */
178
193
195{
196 float strength; // opacity of the correction: out = in + strength * (denoised - in)
197 float noise_level; // scales the sigma map fed to the network (1.0 = trust the profile)
198 float sigma_scale[3]; // per-channel demosaic-bias correction, applied on top of noise_level
199 float a[3], b[3]; // noise variance line per RGB channel, normalized domain
200 dt_nn_model_t *model; // resolved from (version, variant); NULL disables the piece
202
204{
205 // lazily loaded, cached for the session; guarded by lock. tried[][] records
206 // a load attempt so a missing file is probed only once.
209 // user models from the config dir, keyed by basename; a NULL value records
210 // a failed load so a broken file is probed once, like tried[][] above
211 GHashTable *custom;
212 dt_pthread_mutex_t lock;
213#ifdef HAVE_OPENCL
214 dt_nn_cl_t *nn_cl; // U-Net kernel handles, from rawdenoiseai.cl
215 // device-resident glue kernels: the whole tile runs dev_in -> dev_out with
216 // no mid-tile host round-trip (command-queue syncs dominate GPU cost)
219#endif
221
222/* Thread-safe lazy loader: returns the model for (version, size, scale),
223 * loading it on first request from
224 * <configdir>/denoise-<size>-<single|multi>-<version>.anselnn (user
225 * override) or <datadir>/ (shipped). NULL if the file is absent or invalid.
226 * Runs on the pipeline thread via commit_params, hence the mutex. */
229{
230 if((int)ver < 0 || (int)ver >= DT_RAWDENOISEAI_NUM_VERSIONS || (int)sz < 0
231 || (int)sz >= DT_RAWDENOISEAI_NUM_SIZES || (int)sc < 0 || (int)sc >= DT_RAWDENOISEAI_NUM_SCALES)
232 return NULL;
233
235 if(!gd->tried[ver][sz][sc])
236 {
237 gd->tried[ver][sz][sc] = TRUE;
238 char name[64];
239 snprintf(name, sizeof(name), "denoise-%s-%s-%s.anselnn", _size_tag[sz], _scale_tag[sc], _version_tag[ver]);
240
241 char dir[DT_PATH_MAX] = { 0 };
242 char path[DT_PATH_MAX] = { 0 };
243 char err[256] = "";
244 dt_loc_get_user_config_dir(dir, sizeof(dir));
245 dt_concat_path_file(path, dir, name);
246 gd->models[ver][sz][sc] = dt_nn_model_load(path, err, sizeof(err));
247 if(!gd->models[ver][sz][sc])
248 {
249 dt_loc_get_datadir(dir, sizeof(dir));
250 dt_concat_path_file(path, dir, name);
251 gd->models[ver][sz][sc] = dt_nn_model_load(path, err, sizeof(err));
252 }
253 if(gd->models[ver][sz][sc])
254 dt_print(DT_DEBUG_ALWAYS, "[rawdenoiseai] loaded %s\n", path);
255 else
256 dt_print(DT_DEBUG_ALWAYS, "[rawdenoiseai] %s unavailable (%s)\n", name, err);
257 }
258 dt_nn_model_t *m = gd->models[ver][sz][sc];
260 return m;
261}
262
263/* A user model: any .anselnn dropped in the config dir under a name that is
264 * not one of the shipped ones. Cached by basename for the session, with a
265 * NULL entry recording a failed load so a broken file is probed once. Same
266 * mutex as the shipped matrix — this also runs on the pipeline thread. */
268{
269 if(!base || !*base || strchr(base, '/') || strchr(base, '\\')) return NULL;
270
272 if(!gd->custom) gd->custom = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
273 dt_nn_model_t *m = NULL;
274 if(!g_hash_table_lookup_extended(gd->custom, base, NULL, (gpointer *)&m))
275 {
276 char dir[DT_PATH_MAX] = { 0 };
277 char path[DT_PATH_MAX] = { 0 };
278 char err[256] = "";
279 dt_loc_get_user_config_dir(dir, sizeof(dir));
280 dt_concat_path_file(path, dir, base);
281 m = dt_nn_model_load(path, err, sizeof(err));
282 g_hash_table_insert(gd->custom, g_strdup(base), m);
283 if(m)
284 dt_print(DT_DEBUG_ALWAYS, "[rawdenoiseai] loaded user model %s\n", path);
285 else
286 dt_print(DT_DEBUG_ALWAYS, "[rawdenoiseai] user model %s unusable (%s)\n", base, err);
287 }
289 return m;
290}
291
292/* Basenames of every .anselnn in the config dir, sorted, shipped names
293 * excluded (those are overrides of the matrix, already reachable through the
294 * size/variant combos). Caller frees with g_list_free_full(l, g_free). */
295static GList *_list_custom_models(void)
296{
297 char dir[DT_PATH_MAX] = { 0 };
298 dt_loc_get_user_config_dir(dir, sizeof(dir));
299 GDir *d = g_dir_open(dir, 0, NULL);
300 if(!d) return NULL;
301 GList *out = NULL;
302 const gchar *fn;
303 while((fn = g_dir_read_name(d)))
304 {
305 if(!g_str_has_suffix(fn, ".anselnn")) continue;
306 gboolean shipped = FALSE;
307 for(int v = 0; v < DT_RAWDENOISEAI_NUM_VERSIONS && !shipped; v++)
308 for(int z = 0; z < DT_RAWDENOISEAI_NUM_SIZES && !shipped; z++)
309 for(int c = 0; c < DT_RAWDENOISEAI_NUM_SCALES && !shipped; c++)
310 {
311 char name[64];
312 snprintf(name, sizeof(name), "denoise-%s-%s-%s.anselnn", _size_tag[z], _scale_tag[c], _version_tag[v]);
313 shipped = !g_strcmp0(fn, name);
314 }
315 if(!shipped) out = g_list_prepend(out, g_strdup(fn));
316 }
317 g_dir_close(d);
318 return g_list_sort(out, (GCompareFunc)g_strcmp0);
319}
320
321const char *name()
322{
323 return _("raw denoise (AI)");
324}
325
326const char **description(struct dt_iop_module_t *self)
327{
328 return dt_iop_set_description(self,
329 _("denoise the raw picture with a neural network conditioned "
330 "on the camera noise profile"),
331 _("corrective"), _("linear, raw, scene-referred"), _("linear, raw"),
332 _("linear, raw, scene-referred"));
333}
334
339
341{
342 return IOP_GROUP_REPAIR;
343}
344
349
357
358/* Route the executor's scratch through the pixelpipe cache arena, per the
359 * project rule that pixel buffers never come from bare malloc. nn_model
360 * cannot include darktable.h (it is deliberately pipeline-free), so the
361 * arena is injected here, once, before any pipeline runs. */
362/* Per-tile scratch REGION: the whole working set of one process() call —
363 * input planes, output plane and every executor tensor — is reserved from the
364 * pixelpipe arena as ONE allocation, up front, sized by the executor's exact
365 * ledger plus an internal-fragmentation margin. The executor's alloc/free
366 * churn then happens inside the region through the sub-allocator below and is
367 * invisible to the arena: no interleaving with cache entries, no fragmenting
368 * of the arena's free runs, and the tiling engine's largest-free-run cap
369 * guarantees the single reservation fits by construction. Memory is planned,
370 * reserved, then executed within — never discovered at runtime; if the
371 * reservation itself fails, the tile fails BEFORE any compute.
372 *
373 * The sub-allocator is a trivial first-fit block list: at most a dozen live
374 * tensors exist at once, all sized in whole planes. The region pointer is
375 * thread-local because the darkroom's full and preview pipes may run
376 * process() concurrently. */
377#define NN_REGION_MAX_BLOCKS 32
378// the two-ended layout (churn bottom-up, skips top-down) achieves the
379// ledger's live peak exactly; the slack only covers per-block 64-byte
380// alignment crumbs
381#define NN_REGION_SLACK 1.02f
382
383typedef struct nn_region_t
384{
385 char *base;
386 size_t size;
387 struct
388 {
389 size_t off, len;
393
394static __thread nn_region_t *_nn_region = NULL;
395
396static void *_region_alloc(nn_region_t *r, size_t bytes, int long_lived)
397{
398 bytes = (bytes + 63) & ~(size_t)63; // keep 64-byte alignment inside the region
399 if(r->n_blocks >= NN_REGION_MAX_BLOCKS) return NULL;
400 size_t off = (size_t)-1;
401 int at = 0;
402 if(!long_lived)
403 {
404 // churn packs bottom-up, first fit; blocks are kept sorted by offset
405 size_t gap = 0;
406 for(int i = 0; i <= r->n_blocks; i++)
407 {
408 const size_t end = (i < r->n_blocks) ? r->blocks[i].off : r->size;
409 if(end - gap >= bytes)
410 {
411 off = gap;
412 at = i;
413 break;
414 }
415 if(i == r->n_blocks) break;
416 gap = r->blocks[i].off + r->blocks[i].len;
417 }
418 }
419 else
420 {
421 // long-lived blocks (skip connections) pack top-down, last fit, so they
422 // never split the churn area mid-region — this is what lets the region be
423 // sized at exactly planes + the ledger's live peak, no slack
424 size_t gap_end = r->size;
425 for(int i = r->n_blocks; i >= 0; i--)
426 {
427 const size_t gap_start = (i > 0) ? r->blocks[i - 1].off + r->blocks[i - 1].len : 0;
428 if(gap_end - gap_start >= bytes)
429 {
430 off = gap_end - bytes;
431 at = i;
432 break;
433 }
434 if(i > 0) gap_end = r->blocks[i - 1].off;
435 }
436 }
437 if(off == (size_t)-1)
438 {
439 size_t live = 0, largest_gap = 0, gap = 0;
440 for(int i = 0; i <= r->n_blocks; i++)
441 {
442 const size_t end = (i < r->n_blocks) ? r->blocks[i].off : r->size;
443 if(end - gap > largest_gap) largest_gap = end - gap;
444 if(i == r->n_blocks) break;
445 live += r->blocks[i].len;
446 gap = r->blocks[i].off + r->blocks[i].len;
447 }
449 "[rawdenoiseai] region alloc failed: %" G_GSIZE_FORMAT " bytes (%s), region %" G_GSIZE_FORMAT
450 ", live %" G_GSIZE_FORMAT " in %d blocks, largest gap %" G_GSIZE_FORMAT "\n",
451 bytes, long_lived ? "long-lived" : "churn", r->size, live, r->n_blocks, largest_gap);
452 return NULL;
453 }
454 for(int i = r->n_blocks; i > at; i--) r->blocks[i] = r->blocks[i - 1];
455 r->blocks[at].off = off;
456 r->blocks[at].len = bytes;
457 r->n_blocks++;
458 return r->base + off;
459}
460
461static void _region_free(nn_region_t *r, void *p)
462{
463 const size_t off = (size_t)((char *)p - r->base);
464 for(int i = 0; i < r->n_blocks; i++)
465 if(r->blocks[i].off == off)
466 {
467 for(int j = i; j < r->n_blocks - 1; j++) r->blocks[j] = r->blocks[j + 1];
468 r->n_blocks--;
469 return;
470 }
471}
472
473static void *_nn_arena_alloc(size_t bytes, int long_lived)
474{
475 if(_nn_region) return _region_alloc(_nn_region, bytes, long_lived);
477}
478
479static void _nn_arena_free(void *p)
480{
482 if(r && (char *)p >= r->base && (char *)p < r->base + r->size)
483 {
484 _region_free(r, p);
485 return;
486 }
488}
489
490/* The pipe whose process() is running on this thread, for the executor's
491 * cancel hook: a forward outlives the pipe's interest in it as soon as the
492 * history changes (module switched off, slider moved), and the pipe's kill
493 * switch is otherwise only read once the module returns. Thread-local for the
494 * same reason as _nn_region. */
495static __thread const dt_dev_pixelpipe_t *_nn_pipe = NULL;
496
497static int _nn_cancelled(void)
498{
500}
501
503{
507 dt_pthread_mutex_init(&gd->lock, NULL);
508#ifdef HAVE_OPENCL
509 gd->nn_cl = dt_nn_cl_create(39); // rawdenoiseai.cl, from programs.conf
510 gd->k_assemble = dt_opencl_create_kernel(39, "nn_assemble");
511 gd->k_bin_planes = dt_opencl_create_kernel(39, "nn_bin_planes");
512 gd->k_residual = dt_opencl_create_kernel(39, "nn_residual");
513 gd->k_upsample_n = dt_opencl_create_kernel(39, "nn_upsample_n");
514 gd->k_bin16_mdv = dt_opencl_create_kernel(39, "nn_bin16_mdv");
515 gd->k_avg2x2 = dt_opencl_create_kernel(39, "nn_avg2x2");
516 gd->k_floor_fuse = dt_opencl_create_kernel(39, "nn_floor_fuse");
517 gd->k_fuse_step = dt_opencl_create_kernel(39, "nn_fuse_step");
518 gd->k_bilerp_add = dt_opencl_create_kernel(39, "nn_bilerp_add");
519 gd->k_blend_crop = dt_opencl_create_kernel(39, "nn_blend_crop");
520#endif
521 module->data = gd;
522 // models are loaded lazily on first use per (version, variant)
523}
524
526{
527 // the hooks point into this module's code: leaving them set after dlclose
528 // would leave the core library with dangling function pointers
529 dt_nn_set_allocator(NULL, NULL);
530 dt_nn_set_cancel(NULL);
532 if(gd)
533 {
534 for(int v = 0; v < DT_RAWDENOISEAI_NUM_VERSIONS; v++)
535 for(int sz = 0; sz < DT_RAWDENOISEAI_NUM_SIZES; sz++)
536 for(int sc = 0; sc < DT_RAWDENOISEAI_NUM_SCALES; sc++) dt_nn_model_free(gd->models[v][sz][sc]);
537#ifdef HAVE_OPENCL
549#endif
550 if(gd->custom)
551 {
552 GHashTableIter it;
553 gpointer k, v;
554 g_hash_table_iter_init(&it, gd->custom);
555 while(g_hash_table_iter_next(&it, &k, &v))
557 g_hash_table_destroy(gd->custom);
558 gd->custom = NULL;
559 }
561 }
562 free(module->data);
563 module->data = NULL;
564}
565
566/* The size a new history entry gets, from what the machine can actually run:
567 * half where OpenCL will carry it, quarter on CPU alone — the only size that
568 * stays interactive there. The variant is multiscale either way; the coarse
569 * chroma pass is worth most exactly where capacity is scarce (it buys quarter
570 * ~2.6 dB of low-frequency chroma error and large almost nothing), so the
571 * hardware picks the width, not the variant. */
576
577/* Supported when the input is mosaiced and the model a new entry would DEFAULT
578 * to can be loaded (i.e. weights are installed). Probing that exact model and
579 * not a fixed one is what keeps the enable button honest on an installation
580 * carrying only part of the matrix. A specific (version, size, variant) the
581 * user selects that turns out to be missing is handled per-piece by
582 * copy-through. */
589
591{
592 /* Pick the default size from what the machine can actually run, because a
593 * user who enables the module without knowing what it is must get a result
594 * in seconds — not a frozen application. The variant is multiscale in both
595 * cases: it is what keeps the smaller networks free of low-frequency chroma
596 * blotches, and that matters most exactly where capacity is scarce (see
597 * doc/rawdenoiseai.md — the coarse pass buys quarter ~2.6 dB of chroma
598 * error and large almost nothing).
599 *
600 * OpenCL present: half, ~4x the quarter cost but clearly better.
601 * CPU only: quarter, the only size that stays interactive on a CPU.
602 *
603 * dt_opencl_is_enabled() reflects both the build and the user's preference,
604 * and is a static stub returning 0 without HAVE_OPENCL. Users who want a
605 * different trade-off still pick any size by hand; this only seeds a NEW
606 * history entry, so existing edits keep whatever they were created with. */
608 d->size = _default_size();
609 d->scale_variant = DT_RAWDENOISEAI_MULTI;
610
611 module->hide_enable_button = !_rawdenoiseai_supported(module);
612 module->default_enabled = 0;
613}
614
615gboolean force_enable(struct dt_iop_module_t *self, const gboolean current_state)
616{
617 // history sanitization: an entry pasted onto a non-mosaic image, or loaded
618 // without a model available, is forced off at history-read time
619 return current_state && _rawdenoiseai_supported(self);
620}
621
622/* Fill d->a/d->b from the best noise profile for this image: exact ISO match,
623 * interpolation between the bracketing profiled ISOs, clamping outside the
624 * profiled range, generic Poissonian when the camera has no profiles. Mirrors
625 * the training-time ProfileSampler semantics (clamped interpolation). */
627{
628 GList *profiles = dt_noiseprofile_get_matching(&self->dev->image_storage);
630 const float iso = self->dev->image_storage.exif_iso;
631
632 if(profiles)
633 {
634 dt_noiseprofile_t *first = (dt_noiseprofile_t *)profiles->data;
635 dt_noiseprofile_t *last = NULL;
636 interpolated = *first; // clamp below the profiled range
637 for(GList *iter = profiles; iter; iter = g_list_next(iter))
638 {
639 dt_noiseprofile_t *current = (dt_noiseprofile_t *)iter->data;
640 if(current->iso == iso)
641 {
642 interpolated = *current;
643 break;
644 }
645 if(last && last->iso < iso && current->iso > iso)
646 {
647 interpolated.iso = iso;
648 dt_noiseprofile_interpolate(last, current, &interpolated);
649 break;
650 }
651 interpolated = *current; // clamp above the profiled range
652 last = current;
653 }
654 }
655 for(int k = 0; k < 3; k++)
656 {
657 d->a[k] = interpolated.a[k];
658 d->b[k] = interpolated.b[k];
659 }
660 g_list_free_full(profiles, dt_noiseprofile_free);
661}
662
665{
668
669 d->strength = p->strength;
670 d->noise_level = p->noise_level;
671 d->sigma_scale[0] = p->sigma_red;
672 d->sigma_scale[1] = p->sigma_green;
673 d->sigma_scale[2] = p->sigma_blue;
674 // neural inference is among the most expensive nodes of the pipe: always
675 // materialize this piece's output in the RAM cache (same policy as
676 // diffuse/atrous), so interactive edits downstream never re-run it
677 piece->cache_output_on_ram = TRUE;
678 _fetch_noise_profile(self, d);
679
681 /* A named user model wins over the shipped matrix. If it has gone missing
682 * since the edit was made, the piece is disabled rather than silently
683 * rendered through a DIFFERENT network: the two are not interchangeable,
684 * and quietly substituting one would change the picture without telling
685 * anyone. The GUI keeps showing the name so the situation is legible. */
686 const gboolean want_custom = gd && p->custom_model[0];
687 d->model = want_custom ? _get_custom_model(gd, p->custom_model)
688 : gd ? _get_model(gd, p->version, p->size, p->scale_variant)
689 : NULL;
690
692 "[rawdenoiseai] commit: camera '%s' iso %.0f model %s-%s-%s%s -> "
693 "a=(%.3g %.3g %.3g) b=(%.3g %.3g %.3g) strength %.2f noise level %.2f "
694 "channel correction (%.2f %.2f %.2f)\n",
696 _size_tag[CLAMP(p->size, 0, DT_RAWDENOISEAI_NUM_SIZES - 1)],
697 _scale_tag[CLAMP(p->scale_variant, 0, DT_RAWDENOISEAI_NUM_SCALES - 1)],
698 _version_tag[CLAMP(p->version, 0, DT_RAWDENOISEAI_NUM_VERSIONS - 1)],
699 d->model ? (want_custom ? " (user model)" : "") : " (missing)",
700 d->a[0], d->a[1], d->a[2], d->b[0], d->b[1], d->b[2], p->strength, p->noise_level, p->sigma_red,
701 p->sigma_green, p->sigma_blue);
702
703 // plane-layout contract with the training repo: a model that does not
704 // match what _assemble_planes builds must be treated as missing, not fed
705 if(d->model)
706 {
707 const int fine_in = dt_nn_model_in_channels(d->model);
708 const int c_out = dt_nn_model_coarse_out_channels(d->model);
709 const gboolean ok = c_out > 0 ? (dt_nn_model_coarse_in_channels(d->model) == 6 && c_out == 3 && fine_in == 8)
710 : (fine_in == 5);
711 if(!ok)
712 {
714 "[rawdenoiseai] model plane layout unsupported "
715 "(fine_in %d coarse_out %d) — module disabled\n",
716 fine_in, c_out);
717 d->model = NULL;
718 }
719 }
720 if(!d->model || !(p->strength > 0.0f)) piece->enabled = 0;
721}
722
723static unsigned _align_lcm(const unsigned a, const unsigned b)
724{
725 if(a == 0 || b == 0) return a > b ? a : b;
726 unsigned x = a, y = b;
727 while(y)
728 {
729 const unsigned t = x % y;
730 x = y;
731 y = t;
732 }
733 return a / x * b;
734}
735
738{
740 // per input pixel (4 bytes): 5 input planes + 1 output plane + network
741 // scratch, all float32. factor is relative to the unit buffer size.
742 float extra = 6.0f;
743 float extra_cl = 6.0f;
744 unsigned overlap = 0;
745 if(d && d->model)
746 {
747 // input planes + output plane (in_ch is 8 for a multi-scale model), the
748 // coarse buffers at 1/bin^2, and the executor scratch (which already
749 // includes the coarse net's share for unet-ms)
750 const gboolean xtrans_cfa = piece->dsc_in.filters == 9u;
751 const int bin = dt_nn_model_bin(d->model, xtrans_cfa);
752 const float planes = (float)(dt_nn_model_in_channels(d->model) + 1)
753 + (bin > 1 ? 12.0f / (float)(bin * bin) : 0.0f);
754 /* Host: process() reserves the tile's whole working set as ONE arena
755 * region — the (in_ch + 1) planes plus the executor's ledger peak times
756 * the sub-allocator's fragmentation margin. The factor declares exactly
757 * that reservation, so the tiling plan, the reservation and the execution
758 * are the same number (the coarse-stage module buffers, 12/bin^2, are the
759 * only separate short-lived arena entries). Expressed as a factor of the
760 * input image size — floats per input pixel — never absolute bytes. */
761 extra = planes + NN_REGION_SLACK * dt_nn_unet_scratch_per_px(d->model);
762 /* Device: no region — buffers are device-side. The CL executor's sequence
763 * differs (it materializes the decoder concat), and the CL path adds
764 * three device planes of its own (dev_den, plus the buffer-format copies
765 * dev_in_buf/dev_out_buf) and the fusion grids (~0.13 plane); count all
766 * of it explicitly rather than letting it ride inside factor_cl's
767 * padding headroom. */
768 extra_cl = planes + dt_nn_unet_scratch_per_px_cl(d->model) + 3.2f;
769
770 /* Overlap is set from the MEASURED seam profile, not from the impulse
771 * response. An earlier revision sized it at 48 px because the trained
772 * model's response to a delta decays to 1.4e-6 of peak by radius 32; that
773 * reasoning is wrong for this purpose. A tile boundary does not remove one
774 * tap, it removes an entire half-plane of context, and the summed
775 * contribution of that half-plane is orders of magnitude above any single
776 * tap. Measured on DSCF9668.RAF (X-Trans, 3 tiles across), CPU untiled vs
777 * GPU tiled, as a fraction of full scale:
778 *
779 * overlap 48 -> seam peaks at 0.17-0.23 %, i.e. 238-317x the interior
780 * difference, and does not return to it until ~50 px in
781 * overlap 192 -> seam peaks at 0.006-0.016 %, 5.5-14.5x interior
782 *
783 * The 48 px case is visible; it is the tile grid users report. The decay
784 * measured with 48 px of overlap reaches the interior baseline exactly
785 * 48 px inside the owned region, so a boundary pixel needs 48 + 48 = 96 px
786 * of context: that is the fine net's effective receptive field, and the
787 * floor for the single-scale model.
788 *
789 * The multiscale model adds the coarse stage, whose receptive field is its
790 * own (in binned pixels) times `bin`, so it needs substantially more. 192
791 * is the value actually validated above. Note X-Trans multiscale reached it
792 * only by accident: the engine rounds the requested overlap UP to a
793 * multiple of lcm(xalign, yalign), which is 192 there, so a request of 96
794 * silently became 192. Bayer multiscale aligns to 64 and would have stayed
795 * at 128. Ask for what the measurement supports instead of relying on an
796 * alignment that varies with the sensor. */
797 overlap = (bin > 1) ? 192 : 96;
798 }
799 tiling->factor = 2.0f + extra;
800 /* The GPU budget must be more conservative than the CPU one, for two
801 * reasons the tiling engine cannot see. First, every buffer here is
802 * allocated at the alignment-padded tile size, up to (align-1) larger per
803 * axis than the tile the engine budgeted — only for the ragged last tile of
804 * a row or column now that xalign below equals the alignment, so an interior
805 * tile is padded by nothing at all and this term is pure headroom. Second,
806 * the engine hands out up to 100 % of the device's reported memory, and a
807 * budget that spends all of VRAM never allocates in practice (display,
808 * driver and allocator overhead) — CPU RAM overcommits, VRAM does not,
809 * and the U-Net scratch dominates this factor so the whole budget is
810 * exact where other modules' estimates carry implicit slack. 1.4 covers
811 * the worst realistic padding inflation (~1.15) times an allocation
812 * headroom (~1.2); the cost of overshooting is a smaller tile, the cost
813 * of undershooting is CL_MEM_OBJECT_ALLOCATION_FAILURE and a silent
814 * fallback of the whole tile to CPU. */
815 tiling->factor_cl = 2.0f + extra_cl * 1.4f;
816 tiling->maxbuf = 1.0f;
817 // device-resident weight blob (up to ~38 MB) plus fixed slack
818 tiling->overhead = 64u * 1024u * 1024u;
819 tiling->overlap = overlap;
820 /* Tiles must preserve the CFA phase — and, for a multi-scale model, every
821 * other LATTICE this module lays over its input: the superpixel binning of
822 * the coarse stage (period `bin`) and the low-band fusion pyramid (period
823 * DT_NN_FUSION_COARSEST). All of them are anchored to the tile's own origin,
824 * because that is the only origin process() is handed. The tiling engine
825 * places tile origins on multiples of lcm(xalign, yalign), so anything short
826 * of the full lattice period lets two different tile grids bin the same
827 * sensels into differently-phased superpixels — which changes the coarse
828 * guide, and with it the result, EVERYWHERE inside the tile rather than only
829 * near its seams. No amount of overlap fixes that; only aligning the origins
830 * does. It is why the same edit rendered differently on CPU and GPU: the two
831 * budget their memory differently, so they tile differently, so they binned
832 * on different lattices.
833 *
834 * dt_nn_model_alignment() is the one function that owns those periods (the
835 * fine net's stride pyramid, the coarse net's binned one, and the fusion
836 * bands) — ask it rather than re-deriving any of them here, or the next
837 * lattice added to the model silently reopens this bug. Aligning to the FULL
838 * value also means an interior tile needs no reflect padding at all, so no
839 * mirrored data is folded into the fusion's per-tile statistics. */
840 const gboolean xtrans = piece->dsc_in.filters == 9u;
841 unsigned align = xtrans ? 6u : 2u;
842 if(d && d->model) align = _align_lcm(align, (unsigned)dt_nn_model_alignment(d->model));
843 tiling->xalign = align;
844 tiling->yalign = align;
845}
846
847// mirror-reflect coordinate into [0, n): same row/column parity is preserved
848// for even n-1 steps; the CFA color planes are computed from the SOURCE
849// sensel, so the network always sees consistent (value, color) pairs even
850// where reflection breaks the periodic layout (X-Trans borders).
851static inline __attribute__((always_inline)) int _reflect(int v, int n)
852{
853 if(n == 1) return 0;
854 while(v < 0 || v >= n)
855 {
856 if(v < 0) v = -v;
857 if(v >= n) v = 2 * n - 2 - v;
858 }
859 return v;
860}
861
862/* Total sigma scale per CFA color: global correction x the per-channel
863 * slider, nothing else — the GUI values ARE the conditioning (see the
864 * calibration comment at the top of this file). */
865static void _sigma_scale(const dt_iop_rawdenoiseai_data_t *d, float scale[3])
866{
867 for(int c = 0; c < 3; c++) scale[c] = d->noise_level * d->sigma_scale[c];
868}
869
870/* Build the coarse stage's 6 input planes [R, G, B, sigmaR, sigmaG, sigmaB]
871 * from the assembled fine planes 0-3 (count-weighted superpixel means; the
872 * binning itself is dt_nn_bin_planes, the bit-exact contract with the
873 * training repo). Coarse sigma is the analytic sigma of the mean of n
874 * sensels: scale[c] * sqrt((a*x + b) / n). Shared verbatim by the CPU and
875 * OpenCL paths. */
877static void _k_bin_planes(const float *const nn_in, float *const coarse_in, float *const cnt,
878 const int pw, const int ph, const int bin,
879 const dt_iop_rawdenoiseai_data_t *const d)
880{
881 const int cw = pw / bin, chh = ph / bin;
882 const size_t cplane = (size_t)cw * chh;
883 dt_nn_bin_planes(nn_in, pw, ph, bin, coarse_in, cnt);
884 float scale[3];
885 _sigma_scale(d, scale);
887 for(int c = 0; c < 3; c++)
888 {
889 const float *const mean = coarse_in + (size_t)c * cplane;
890 const float *const n_c = cnt + (size_t)c * cplane;
891 float *const sigma = coarse_in + (size_t)(3 + c) * cplane;
892 for(size_t i = 0; i < cplane; i++)
893 {
894 const float n = n_c[i] > 1.0f ? n_c[i] : 1.0f;
895 const float var = (d->a[c] * MAX(mean[i], 0.0f) + d->b[c]) / n;
896 sigma[i] = scale[c] * sqrtf(MAX(var, 1e-12f));
897 }
898 }
899}
900
901/* Build the network's 5 input planes [mosaic, R, G, B one-hot, sigma] from the
902 * mosaic, reflect-padded from (width, height) to (pw, ph). Shared verbatim by
903 * the CPU and OpenCL paths so both feed the network identical data. */
905static void _k_assemble(const float *const in, float *const nn_in, const int width, const int height,
906 const int pw, const int ph, const dt_iop_rawdenoiseai_data_t *const d,
907 const uint32_t filters, const uint8_t (*const xtrans)[6],
908 const dt_iop_roi_t *const roi)
909{
910 const size_t plane = (size_t)pw * ph;
911 float scale[3];
912 _sigma_scale(d, scale);
914 for(int y = 0; y < ph; y++)
915 {
916 const int sy = _reflect(y, height);
917 const float *const irow = in + (size_t)sy * width;
918 float *const mosaic = nn_in + (size_t)y * pw;
919 float *const onehot_r = nn_in + plane + (size_t)y * pw;
920 float *const onehot_g = nn_in + 2 * plane + (size_t)y * pw;
921 float *const onehot_b = nn_in + 3 * plane + (size_t)y * pw;
922 float *const sigma = nn_in + 4 * plane + (size_t)y * pw;
923 for(int x = 0; x < pw; x++)
924 {
925 const int sx = _reflect(x, width);
926 const float v = irow[sx];
927 int c = (filters == 9u) ? FCxtrans(sy, sx, roi, xtrans) : (int)FC(sy, sx, filters);
928 if(c < 0 || c > 2) c = 1; // both greens share the G statistics; clamp junk CFA data
929 mosaic[x] = v;
930 onehot_r[x] = (c == 0) ? 1.0f : 0.0f;
931 onehot_g[x] = (c == 1) ? 1.0f : 0.0f;
932 onehot_b[x] = (c == 2) ? 1.0f : 0.0f;
933 const float var = d->a[c] * MAX(v, 0.0f) + d->b[c];
934 sigma[x] = scale[c] * sqrtf(MAX(var, 1e-12f));
935 }
936 }
937}
938
939
940/* ---- CPU kernels ------------------------------------------------------
941 * One function per OpenCL kernel in rawdenoiseai.cl, same name, same
942 * arguments in the same order, so process() and process_cl() read as the same
943 * procedure and a change to one has an obvious counterpart in the other.
944 * Every pixel loop in this module lives in one of these. */
945
946/* Per-channel Bayer densities: the fraction of a block's sensels carrying each
947 * colour. Shared with the OpenCL twins, which take them as dens0/dens1/dens2.
948 * Bayer values are used for both CFA families, matching the torch reference. */
949static const float DT_NN_FUSION_DENS[3] = { 0.25f, 0.5f, 0.25f };
950
951/* chi^2-quantile guard: the local mean of a squared noise term over a 3x3 cell
952 * neighbourhood has ~9 effective samples, so pure-noise cells fluctuate up to
953 * ~2x their expectation. Must equal the literal in nn_floor_fuse/nn_fuse_step. */
954#define DT_NN_FUSION_T_CHI2 2.5
955
956/* mirrors nn_residual: the network predicts what to REMOVE, so the denoised
957 * signal is input minus head. Applied by the caller on BOTH devices — the
958 * executor writes the raw head output either way. */
960static void _k_residual(const float *const in, const float *const head, float *const out, const size_t n)
961{
963 for(size_t i = 0; i < n; i++) out[i] = in[i] - head[i];
964}
965
966/* mirrors nn_blend_crop: strength is the opacity of the correction, so the
967 * output lerps between the original mosaic and the denoised one while dropping
968 * the alignment padding. */
970static void _k_blend_crop(const float *const in, const float *const den, float *const out, const int width,
971 const int height, const int pw, const float strength)
972{
974 for(int y = 0; y < height; y++)
975 {
976 const float *const src_in = in + (size_t)y * width;
977 const float *const src_nn = den + (size_t)y * pw;
978 float *const dst = out + (size_t)y * width;
979 for(int x = 0; x < width; x++) dst[x] = src_in[x] + strength * (src_nn[x] - src_in[x]);
980 }
981}
982
983/* mirrors nn_bin16_mdv: count-weighted per-channel mean of the mosaic, the
984 * denoised plane and sigma^2 over each 16x16 block. */
986static void _k_bin16_mdv(const float *const nn_in, const float *const denoised, float *const M,
987 float *const D, float *const V, const int pw, const int ph)
988{
989 const size_t plane = (size_t)pw * ph;
990 const float *const sig = nn_in + 4 * plane;
991 const int cw = pw / DT_NN_FUSION_FINEST, chh = ph / DT_NN_FUSION_FINEST;
992 const size_t p0 = (size_t)cw * chh;
993 for(int c = 0; c < 3; c++)
994 {
995 const float *const oh = nn_in + (size_t)(1 + c) * plane;
997 for(int cy = 0; cy < chh; cy++)
998 for(int cx = 0; cx < cw; cx++)
999 {
1000 float sm = 0.f, sd = 0.f, sv = 0.f, cnt = 0.f;
1001 for(int y = cy * DT_NN_FUSION_FINEST; y < (cy + 1) * DT_NN_FUSION_FINEST; y++)
1002 for(int x = cx * DT_NN_FUSION_FINEST; x < (cx + 1) * DT_NN_FUSION_FINEST; x++)
1003 {
1004 const size_t i = (size_t)y * pw + x;
1005 sm += nn_in[i] * oh[i];
1006 sd += denoised[i] * oh[i];
1007 sv += sig[i] * sig[i] * oh[i];
1008 cnt += oh[i];
1009 }
1010 const float n = cnt > 1.f ? cnt : 1.f;
1011 const size_t o = (size_t)c * p0 + (size_t)cy * cw + cx;
1012 M[o] = sm / n;
1013 D[o] = sd / n;
1014 V[o] = sv / n;
1015 }
1016 }
1017}
1018
1019/* mirrors nn_avg2x2: 2x2 average pooling of a 3-plane grid. Per-channel counts
1020 * are uniform at these scales, so a plain mean of four children equals
1021 * re-binning from full resolution. */
1023static void _k_avg2x2(const float *const in, float *const out, const int sw, const int sh, const size_t p0)
1024{
1025 const int w2 = sw / 2, h2 = sh / 2;
1026 for(int c = 0; c < 3; c++)
1027 {
1028 const float *const src = in + (size_t)c * p0;
1029 float *const dst = out + (size_t)c * p0;
1030 for(int y = 0; y < h2; y++)
1031 for(int x = 0; x < w2; x++)
1032 dst[(size_t)y * w2 + x]
1033 = 0.25f
1034 * (src[(size_t)(2 * y) * sw + 2 * x] + src[(size_t)(2 * y) * sw + 2 * x + 1]
1035 + src[(size_t)(2 * y + 1) * sw + 2 * x] + src[(size_t)(2 * y + 1) * sw + 2 * x + 1]);
1036 }
1037}
1038
1039/* Hybrid low-band fusion (mirrors cfa.fuse_low_bands in the training repo):
1040 * per-band self-calibrated Wiener weights at scales 16/32, pure measurement
1041 * at the coarsest band. All band corrections are upsampled BILINEARLY
1042 * (align_corners=false, matching torch F.interpolate): nearest upsampling
1043 * turned the per-block measurement noise (sigma/sqrt(n), non-negligible on
1044 * very noisy images) into visible checkers of colored squares. The finest
1045 * fusion band is 16 px for the same reason. The coarsest band is the
1046 * n-averaged measurement outright, so the hallucination-free guarantee
1047 * holds. The pyramid is always 16/32/64 — the same bands the training
1048 * reference fuses — because dt_nn_model_alignment() guarantees a padded tile
1049 * that divides by the coarsest one. Bayer channel densities are used for both
1050 * CFA families, matching the torch reference. */
1051static inline __attribute__((always_inline)) float _bilerp_tap(const float *const p, const int w,
1052 const int h, const float fx,
1053 const float fy)
1054{
1055 const float cx = fx < 0.f ? 0.f : (fx > w - 1.f ? w - 1.f : fx);
1056 const float cy = fy < 0.f ? 0.f : (fy > h - 1.f ? h - 1.f : fy);
1057 const int x0 = (int)cx, y0 = (int)cy;
1058 const int x1 = x0 + 1 < w ? x0 + 1 : x0;
1059 const int y1 = y0 + 1 < h ? y0 + 1 : y0;
1060 const float ax = cx - x0, ay = cy - y0;
1061 const float top = p[(size_t)y0 * w + x0] * (1.f - ax) + p[(size_t)y0 * w + x1] * ax;
1062 const float bot = p[(size_t)y1 * w + x0] * (1.f - ax) + p[(size_t)y1 * w + x1] * ax;
1063 return top * (1.f - ay) + bot * ay;
1064}
1065
1066// dst (fw x fh) = bilinear upsample of src (sw x sh) by integer factor f
1068static void _upsample_bilinear(const float *const src, const int sw, const int sh, const int f, float *const dst)
1069{
1070 const int fw = sw * f, fh = sh * f;
1071 for(int y = 0; y < fh; y++)
1072 {
1073 const float sy = (y + 0.5f) / f - 0.5f;
1074 for(int x = 0; x < fw; x++) dst[(size_t)y * fw + x] = _bilerp_tap(src, sw, sh, (x + 0.5f) / f - 0.5f, sy);
1075 }
1076}
1077
1078/* Number of pyramid levels between the finest and the coarsest fusion band.
1079 * Constant by construction — both are fixed by the training reference. */
1080static inline int _fusion_levels(void)
1081{
1082 int n = 1;
1083 for(int s = DT_NN_FUSION_FINEST; s < DT_NN_FUSION_COARSEST; s *= 2) n++;
1084 return n;
1085}
1086
1087/* Returns 0 on success, non-zero if the fusion could not run — the caller must
1088 * treat that as a failed tile, exactly as process_cl() does when a device
1089 * buffer for the same pyramid cannot be allocated. Rendering the tile with the
1090 * fine network's raw low band instead would be a silent, tile-shaped quality
1091 * regression, and would differ from whatever the other device did. */
1093/* mirrors nn_floor_fuse: structure-gated blend — the measurement owns every
1094 * cell whose own mean-removed local energy is noise-sized (the dilution
1095 * guarantee), the model owns structured cells, because a box average across an
1096 * edge mixes both sides into a saturated outline. The gate reads the
1097 * MEASUREMENT, not the model discrepancy: D-M cannot tell a real edge from the
1098 * model drifting on flat content. REQUIRES a model trained with the
1099 * DC-ownership loss; one from the older fused loss drifts in deep shadows. */
1101static void _k_floor_fuse(const float *const M, const float *const D, const float *const V,
1102 float *const fused, const int sw, const int sh, const size_t p0, const int S)
1103{
1104 for(int c = 0; c < 3; c++)
1105 {
1106 const double vscale = 1.0 / (DT_NN_FUSION_DENS[c] * S * S);
1107 const float *const Mp = M + (size_t)c * p0;
1108 const float *const Dp = D + (size_t)c * p0;
1109 const float *const Vp = V + (size_t)c * p0;
1110 float *const fs = fused + (size_t)c * p0;
1111 for(int y = 0; y < sh; y++)
1112 for(int x = 0; x < sw; x++)
1113 {
1114 // structure = blur3((M - blur3(M))^2): insensitive to smooth gradients,
1115 // unlike a plain window variance
1116 double structure = 0.0;
1117 for(int ny = -1; ny <= 1; ny++)
1118 for(int nx = -1; nx <= 1; nx++)
1119 {
1120 const int cy2 = CLAMP(y + ny, 0, sh - 1), cx2 = CLAMP(x + nx, 0, sw - 1);
1121 double mean = 0.0;
1122 for(int dy = -1; dy <= 1; dy++)
1123 for(int dx = -1; dx <= 1; dx++)
1124 {
1125 const int yy = CLAMP(cy2 + dy, 0, sh - 1), xx = CLAMP(cx2 + dx, 0, sw - 1);
1126 mean += Mp[(size_t)yy * sw + xx];
1127 }
1128 const double mloc = Mp[(size_t)cy2 * sw + cx2] - mean / 9.0;
1129 structure += mloc * mloc;
1130 }
1131 const size_t i = (size_t)y * sw + x;
1132 // this cell's own mean sigma^2, not the tile's (cfa.fuse_low_bands)
1133 const double vn = (double)Vp[i] * vscale;
1134 structure = structure / 9.0 - DT_NN_FUSION_T_CHI2 * vn;
1135 if(structure < 0.0) structure = 0.0;
1136 const float w = (float)(structure / (structure + vn + 1e-20));
1137 fs[i] = w * Dp[i] + (1.f - w) * Mp[i];
1138 }
1139 }
1140}
1141
1142/* mirrors nn_fuse_step: upsample the running fusion and add the finer band,
1143 * weighted per cell by a Wiener gain on the band discrepancy. `ups` is scratch
1144 * for two upsampled planes. */
1146static void _k_fuse_step(const float *const fused_c, const float *const Mf, const float *const Df,
1147 const float *const Vf, const float *const Mc, const float *const Dc,
1148 float *const fused_f, float *const ups, const int sw, const int sh,
1149 const size_t p0, const int sc)
1150{
1151 const int fw = sw * 2, fh = sh * 2;
1152 for(int c = 0; c < 3; c++)
1153 {
1154 const float *const Dfp = Df + (size_t)c * p0;
1155 const float *const Mfp = Mf + (size_t)c * p0;
1156 const float *const Vfp = Vf + (size_t)c * p0;
1157 float *const upD = ups, *const upM = ups + p0;
1158 _upsample_bilinear(Dc + (size_t)c * p0, sw, sh, 2, upD);
1159 _upsample_bilinear(Mc + (size_t)c * p0, sw, sh, 2, upM);
1160 /* Var(mean_s - up(mean_2s)) = Var(mean_s) * 3/4 once the covariance with the
1161 * 2x2 parent is folded in, which is what this reciprocal difference is; so
1162 * the scale-s cell mean is the right sigma^2 for the whole term. */
1163 const double vscale
1164 = 1.0 / (DT_NN_FUSION_DENS[c] * sc * sc) - 1.0 / (DT_NN_FUSION_DENS[c] * 4.0 * sc * sc);
1165 _upsample_bilinear(fused_c + (size_t)c * p0, sw, sh, 2, fused_f + (size_t)c * p0);
1166 float *const fs = fused_f + (size_t)c * p0;
1167 for(int y = 0; y < fh; y++)
1168 for(int x = 0; x < fw; x++)
1169 {
1170 // per-cell Wiener weight from the 3x3-smoothed band discrepancy
1171 double acc = 0.0;
1172 int n = 0;
1173 for(int dy = -1; dy <= 1; dy++)
1174 for(int dx = -1; dx <= 1; dx++)
1175 {
1176 const int yy = CLAMP(y + dy, 0, fh - 1), xx = CLAMP(x + dx, 0, fw - 1);
1177 const size_t j = (size_t)yy * fw + xx;
1178 const double d = (double)(Dfp[j] - upD[j]) - (double)(Mfp[j] - upM[j]);
1179 acc += d * d;
1180 n++;
1181 }
1182 const size_t i = (size_t)y * fw + x;
1183 const double vn = (double)Vfp[i] * vscale;
1184 double vm = acc / n - DT_NN_FUSION_T_CHI2 * vn;
1185 if(vm < 0.0) vm = 0.0;
1186 const float w = (float)(vn / (vn + vm + 1e-20));
1187 fs[i] += w * (Dfp[i] - upD[i]) + (1.f - w) * (Mfp[i] - upM[i]);
1188 }
1189 }
1190}
1191
1192/* mirrors nn_bilerp_add: scatter (fused - D16) bilinearly upsampled from the
1193 * level-0 grid onto the denoised plane, on whichever colour plane owns each
1194 * sensel. `scratch` is reused as the correction plane. */
1196static void _k_bilerp_add(const float *const fused, const float *const D16, float *const scratch,
1197 const float *const nn_in, float *const denoised, const int pw, const int ph,
1198 const size_t p0, const int cw0, const int ch0)
1199{
1200 const size_t plane = (size_t)pw * ph;
1201 const float inv = 1.f / (float)DT_NN_FUSION_FINEST;
1202 for(int c = 0; c < 3; c++)
1203 {
1204 float *const cs = scratch + (size_t)c * p0;
1205 const float *const fa = fused + (size_t)c * p0;
1206 const float *const d0 = D16 + (size_t)c * p0;
1207 for(size_t i = 0; i < p0; i++) cs[i] = fa[i] - d0[i];
1208 }
1210 for(int y = 0; y < ph; y++)
1211 {
1212 const float sy = (y + 0.5f) * inv - 0.5f;
1213 for(int x = 0; x < pw; x++)
1214 {
1215 const size_t i = (size_t)y * pw + x;
1216 for(int c = 0; c < 3; c++)
1217 if(nn_in[(size_t)(1 + c) * plane + i] > 0.0f)
1218 {
1219 denoised[i] += _bilerp_tap(scratch + (size_t)c * p0, cw0, ch0, (x + 0.5f) * inv - 0.5f, sy);
1220 break;
1221 }
1222 }
1223 }
1224}
1225
1226static int _apply_low_band_anchor(const float *const nn_in, float *const denoised, const int pw, const int ph,
1227 const int scale)
1228{
1229 if(scale <= 0) return 0;
1230 // The pyramid is 16/32/64, fixed by the training reference. The padded tile
1231 // divides by 64 because dt_nn_model_alignment() folds DT_NN_FUSION_COARSEST
1232 // in for any model that declares an anchor; deriving the number of levels
1233 // from the tile size instead is what used to make the render depend on how
1234 // the pipe happened to tile (i.e. on the machine, and on CPU vs GPU).
1235 const int S = DT_NN_FUSION_COARSEST;
1236 if(pw % S || ph % S) return 0;
1237 const int cw0 = pw / 16, ch0 = ph / 16; // level-0 grid (scale 16)
1238 const size_t p0 = (size_t)cw0 * ch0;
1239 // slots: M/D/V per level (16, 32, 64) + fused ping-pong + upsample scratch
1240 float *const buf = dt_pixelpipe_cache_alloc_align_float_cache(p0 * 3 * (3 * 3 + 3), 0);
1241 if(IS_NULL_PTR(buf)) return 1;
1242 float *lv[9]; // lv[3 * level + {0: mosaic, 1: denoised, 2: sigma^2}]
1243 for(int k = 0; k < 9; k++) lv[k] = buf + (size_t)k * p0 * 3;
1244 float *fusedA = buf + p0 * 3 * 9, *fusedB = fusedA + p0 * 3, *ups = fusedB + p0 * 3;
1245
1246 _k_bin16_mdv(nn_in, denoised, lv[0], lv[1], lv[2], pw, ph);
1247 const int nlev = _fusion_levels();
1248 int w_l = cw0, h_l = ch0;
1249 for(int k = 1; k < nlev; k++)
1250 {
1251 for(int md = 0; md < 3; md++) _k_avg2x2(lv[3 * (k - 1) + md], lv[3 * k + md], w_l, h_l, p0);
1252 w_l /= 2;
1253 h_l /= 2;
1254 }
1255
1256 // Coarse-to-fine fusion with LOCAL weights (mirrors cfa.fuse_low_bands).
1257 // Two distinct gates (see the training repo for the full rationale):
1258 // - floor band: anchor to the measurement wherever the MEASUREMENT itself
1259 // is smooth at this scale (local variance vs noise); the model-vs-
1260 // measurement discrepancy cannot distinguish a real edge from the model
1261 // drifting on flat content;
1262 // - soft bands: per-cell Wiener on the band discrepancy with a chi^2
1263 // guard (T = 2.5, ~9 effective samples per 3x3 cell neighbourhood).
1264 int sw = w_l, sh = h_l;
1265 // FLOOR: structure-gated blend (mirrors cfa.fuse_low_bands) — the
1266 // measurement owns every cell where its own mean-removed local energy is
1267 // noise-sized (the dilution guarantee), the model owns structured cells
1268 // (a box average across an edge mixes both sides -> saturated outline).
1269 // REQUIRES a model trained with the DC-ownership loss: models from the
1270 // older fused loss drift in deep shadows and must not be used with this
1271 // code.
1272 _k_floor_fuse(lv[3 * (nlev - 1)], lv[3 * (nlev - 1) + 1], lv[3 * (nlev - 1) + 2], fusedA, sw, sh, p0,
1273 DT_NN_FUSION_FINEST << (nlev - 1));
1274 for(int k = nlev - 2; k >= 0; k--)
1275 {
1276 _k_fuse_step(fusedA, lv[3 * k], lv[3 * k + 1], lv[3 * k + 2], lv[3 * (k + 1)], lv[3 * (k + 1) + 1], fusedB,
1277 ups, sw, sh, p0, DT_NN_FUSION_FINEST << k);
1278 float *tmp = fusedA;
1279 fusedA = fusedB;
1280 fusedB = tmp;
1281 sw *= 2;
1282 sh *= 2;
1283 }
1284
1285 _k_bilerp_add(fusedA, lv[1], fusedB, nn_in, denoised, pw, ph, p0, cw0, ch0);
1287 return 0;
1288}
1289
1292 const void *const ivoid, void *const ovoid)
1293{
1294 const dt_iop_roi_t *const roi = &piece->roi_in;
1296 dt_nn_model_t *const model = d->model;
1297
1298 const int width = roi->width, height = roi->height;
1299 const float *const in = (const float *)ivoid;
1300 float *const out = (float *)ovoid;
1301
1302 if(!model || !(d->strength > 0.0f))
1303 {
1305 return 0;
1306 }
1307
1308 /* The Bayer CFA lookup below is tile-local (FC() has no ROI awareness), so it
1309 * needs the word already rotated to this ROI's phase — see the CFA-phase rule
1310 * in CLAUDE.md. The X-Trans branch stays self-correcting: it takes the raw
1311 * table plus `roi` and adds the offset itself, and dt_dev_get_roi_filters()
1312 * no-ops on X-Trans, so `filters == 9u` still identifies it. */
1313 const uint32_t filters = dt_dev_get_roi_filters(piece, roi);
1314 const uint8_t(*const xtrans)[6] = (const uint8_t(*const)[6])piece->dsc_in.xtrans;
1315
1316 // reflect-pad to the network alignment
1317 const int align = dt_nn_model_alignment(model);
1318 const int pw = (width + align - 1) / align * align;
1319 const int ph = (height + align - 1) / align * align;
1320 const size_t plane = (size_t)pw * ph;
1321
1322 /* The tile's whole working set — input planes, output plane, executor
1323 * scratch — is ONE arena reservation, made before any compute, sized by the
1324 * executor's exact ledger plus the sub-allocator's fragmentation margin.
1325 * This is the same quantity tiling_callback declares, so the plan, the
1326 * reservation and the execution are one number. */
1327 const int in_ch = dt_nn_model_in_channels(model);
1328 nn_region_t region = { 0 };
1329 region.size = ((plane * (in_ch + 1) * sizeof(float) + 63) & ~(size_t)63)
1330 + (size_t)(NN_REGION_SLACK * (float)dt_nn_unet_scratch_bytes(model, pw, ph));
1331 region.size = (region.size + 63) & ~(size_t)63;
1333 if(IS_NULL_PTR(region.base))
1334 {
1335 dt_print(DT_DEBUG_ALWAYS, "[rawdenoiseai] region alloc failed for %dx%d tile (%.1f MB)\n", pw, ph,
1336 region.size / 1048576.0);
1338 return 1;
1339 }
1340 _nn_region = &region;
1341 _nn_pipe = pipe;
1342 float *const nn_in = _region_alloc(&region, plane * in_ch * sizeof(float), 0);
1343 float *const nn_out = _region_alloc(&region, plane * sizeof(float), 0);
1344 // by construction these cannot fail: the region was sized for them
1345
1346 _k_assemble(in, nn_in, width, height, pw, ph, d, filters, xtrans, roi);
1347
1348 int rc = 0;
1349 const int bin = dt_nn_model_bin(model, filters == 9u);
1350 if(bin > 1)
1351 {
1352 // coarse (low-frequency chroma) pass: denoise the superpixel-binned RGB
1353 // and inject the nearest-upsampled result as guide planes 5-7 of the
1354 // fine network's input (mirrors ms_forward() in ansel-denoise train.py)
1355 const int cw = pw / bin, chh = ph / bin;
1356 const size_t cplane = (size_t)cw * chh;
1357 float *const coarse_in = dt_pixelpipe_cache_alloc_align_float_cache(cplane * 6, 0);
1358 float *const cnt = dt_pixelpipe_cache_alloc_align_float_cache(cplane * 3, 0);
1359 float *const coarse_out = dt_pixelpipe_cache_alloc_align_float_cache(cplane * 3, 0);
1360 if(IS_NULL_PTR(coarse_in) || IS_NULL_PTR(cnt) || IS_NULL_PTR(coarse_out))
1361 rc = 1;
1362 else
1363 {
1364 _k_bin_planes(nn_in, coarse_in, cnt, pw, ph, bin, d);
1365 rc = dt_nn_unet_apply_stage(model, 1, coarse_in, coarse_out, cw, chh, 0);
1366 // the coarse head predicts the correction to the binned RGB planes
1367 if(!rc) _k_residual(coarse_in, coarse_out, coarse_out, cplane * 3);
1368 if(!rc) dt_nn_upsample_nearest(coarse_out, 3, cw, chh, bin, nn_in + plane * 5);
1369 }
1370 if(!IS_NULL_PTR(coarse_in)) dt_pixelpipe_cache_free_align(coarse_in);
1372 if(!IS_NULL_PTR(coarse_out)) dt_pixelpipe_cache_free_align(coarse_out);
1373 }
1374
1375 // 3. fine pass writes the RAW noise prediction; the residual is a kernel of
1376 // ours, sequenced exactly as process_cl() sequences nn_residual
1377 if(!rc) rc = dt_nn_unet_apply_stage(model, 0, nn_in, nn_out, pw, ph, 0);
1378 if(!rc) _k_residual(nn_in, nn_out, nn_out, plane);
1379 if(!rc) rc = _apply_low_band_anchor(nn_in, nn_out, pw, ph, dt_nn_model_anchor(model));
1380 // a cancelled forward falls through both branches: the pipe no longer wants
1381 // this output and discards it on our error, so there is nothing to log or write
1382 if(rc && rc != DT_NN_CANCELLED)
1383 {
1384 dt_print(DT_DEBUG_ALWAYS, "[rawdenoiseai] inference failed (%d) on %dx%d tile, scratch %.1f MB\n", rc, pw, ph,
1385 dt_nn_unet_scratch_bytes(model, pw, ph) / 1048576.0);
1387 }
1388 else if(!rc)
1389 {
1390 _k_blend_crop(in, nn_out, out, width, height, pw, d->strength);
1391 }
1392
1393 _nn_region = NULL;
1394 _nn_pipe = NULL;
1396 return rc;
1397}
1398
1399#ifdef HAVE_OPENCL
1400/* GPU path. Step for step the same procedure as process(), each _k_* function
1401 * there having the kernel of the same name here; the whole tile runs
1402 * dev_in -> dev_out with no mid-tile host round-trip, since command-queue syncs
1403 * dominate GPU cost, except for the cancel checks between U-Net levels.
1404 * Returns FALSE on any failure so the pipeline falls back to CPU, or aborts
1405 * when the failure is the pipe's killswitch. */
1407 cl_mem dev_in, cl_mem dev_out)
1408{
1409 const dt_iop_roi_t *const roi = &piece->roi_in;
1412 dt_nn_model_t *const model = d->model;
1413 const int devid = pipe->devid;
1414
1415 if(!model || !gd || !gd->nn_cl || !(d->strength > 0.0f)) return FALSE;
1416 _nn_pipe = pipe;
1417
1418 const int width = roi->width, height = roi->height;
1419 // pre-shifted for the kernel's tile-local Bayer branch, exactly as in process()
1420 const uint32_t filters = dt_dev_get_roi_filters(piece, roi);
1421 const int is_xtrans = filters == 9u;
1422
1423 const int align = dt_nn_model_alignment(model);
1424 const int pw = (width + align - 1) / align * align;
1425 const int ph = (height + align - 1) / align * align;
1426 const size_t plane = (size_t)pw * ph;
1427 const int in_ch = dt_nn_model_in_channels(model);
1428 const int bin = dt_nn_model_bin(model, is_xtrans);
1429 const int cw = pw / bin, chh = ph / bin;
1430 const size_t cplane = (size_t)cw * chh;
1431 const int anchor = dt_nn_model_anchor(model);
1432 const int gw = pw / 16, gh = ph / 16;
1433 const size_t p16 = (size_t)gw * gh;
1434
1435 float scale[3];
1436 _sigma_scale(d, scale);
1437
1438 /* The whole tile runs on the device: assembly, binning, both network
1439 * stages, residuals, low-band fusion and the final blend are enqueued as
1440 * one chain with no mid-tile readback — command-queue syncs dominate GPU
1441 * cost, not arithmetic. The CPU path (process()) is the bit-parity
1442 * reference for every step. */
1443 gboolean success = FALSE;
1444 int err = CL_SUCCESS;
1445 cl_mem dev_planes = dt_opencl_alloc_device_buffer(devid, plane * in_ch * sizeof(float));
1446 cl_mem dev_noise = dt_opencl_alloc_device_buffer(devid, plane * sizeof(float));
1447 cl_mem dev_den = dt_opencl_alloc_device_buffer(devid, plane * sizeof(float));
1448 cl_mem dev_xtrans = dt_opencl_alloc_device_buffer(devid, 36);
1449 /* The pipeline hands modules their I/O as image2d objects (CL_R float here),
1450 * but every kernel in this chain addresses plain float buffers — passing an
1451 * image where a buffer is declared makes clSetKernelArg fail with
1452 * CL_INVALID_MEM_OBJECT and the enqueue with CL_INVALID_KERNEL_ARGS. Convert
1453 * at the endpoints only: one device-side image->buffer copy of the input, one
1454 * buffer->image copy of the blended result. The interior chain is untouched
1455 * and stays device-resident. */
1456 cl_mem dev_in_buf = dt_opencl_alloc_device_buffer(devid, (size_t)width * height * sizeof(float));
1457 cl_mem dev_out_buf = dt_opencl_alloc_device_buffer(devid, (size_t)width * height * sizeof(float));
1458 cl_mem dev_cin = NULL, dev_chead = NULL, dev_cden = NULL;
1459 // M/D/V per level (16, 32, 64) then the fused ping-pong pair
1460 cl_mem grids[11] = { NULL };
1461 if(!dev_planes || !dev_noise || !dev_den || !dev_xtrans || !dev_in_buf || !dev_out_buf) goto cleanup;
1462 {
1463 size_t origin[3] = { 0, 0, 0 };
1464 size_t region[3] = { (size_t)width, (size_t)height, 1 };
1465 err = dt_opencl_enqueue_copy_image_to_buffer(devid, dev_in, dev_in_buf, origin, region, 0);
1466 if(err != CL_SUCCESS) goto cleanup;
1467 }
1468 if(bin > 1)
1469 {
1470 dev_cin = dt_opencl_alloc_device_buffer(devid, cplane * 6 * sizeof(float));
1471 dev_chead = dt_opencl_alloc_device_buffer(devid, cplane * 3 * sizeof(float));
1472 dev_cden = dt_opencl_alloc_device_buffer(devid, cplane * 3 * sizeof(float));
1473 if(!dev_cin || !dev_chead || !dev_cden) goto cleanup;
1474 }
1475 // same gate as the CPU path's _apply_low_band_anchor(), so both devices fuse
1476 // or skip together — the alignment guarantees the modulo holds
1477 const int do_anchor = anchor > 0 && pw % DT_NN_FUSION_COARSEST == 0 && ph % DT_NN_FUSION_COARSEST == 0;
1478 if(do_anchor)
1479 {
1480 for(int k = 0; k < 11; k++)
1481 {
1482 grids[k] = dt_opencl_alloc_device_buffer(devid, p16 * 3 * sizeof(float));
1483 if(!grids[k]) goto cleanup;
1484 }
1485 }
1486
1487 {
1488 unsigned char xtrans_host[36]; // staging copy: the CL API takes a non-const pointer
1489 memcpy(xtrans_host, piece->dsc_in.xtrans, sizeof(xtrans_host));
1490 if(dt_opencl_write_buffer_to_device(devid, xtrans_host, dev_xtrans, 0, 36, CL_TRUE) != CL_SUCCESS)
1491 goto cleanup;
1492 }
1493
1494 // 1. assemble the base planes (reflect pad + one-hot + sigma)
1495 {
1496 const int K = gd->k_assemble;
1497 const unsigned int f = filters;
1498 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &dev_in_buf);
1499 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &dev_planes);
1500 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(int), &width);
1501 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(int), &height);
1502 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(int), &pw);
1503 dt_opencl_set_kernel_arg(devid, K, 5, sizeof(int), &ph);
1504 dt_opencl_set_kernel_arg(devid, K, 6, sizeof(unsigned int), &f);
1505 dt_opencl_set_kernel_arg(devid, K, 7, sizeof(cl_mem), &dev_xtrans);
1506 dt_opencl_set_kernel_arg(devid, K, 8, sizeof(int), &is_xtrans);
1507 dt_opencl_set_kernel_arg(devid, K, 9, sizeof(int), &roi->x);
1508 dt_opencl_set_kernel_arg(devid, K, 10, sizeof(int), &roi->y);
1509 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 11 + c, sizeof(float), &d->a[c]);
1510 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 14 + c, sizeof(float), &d->b[c]);
1511 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 17 + c, sizeof(float), &scale[c]);
1512 size_t sizes[3] = { ROUNDUPDWD(pw, devid), ROUNDUPDHT(ph, devid), 1 };
1513 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1514 if(err != CL_SUCCESS) goto cleanup;
1515 }
1516
1517 // 2. coarse chroma pass (multiscale models)
1518 if(bin > 1)
1519 {
1520 const int K = gd->k_bin_planes;
1521 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &dev_planes);
1522 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &dev_cin);
1523 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(int), &pw);
1524 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(int), &ph);
1525 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(int), &bin);
1526 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 5 + c, sizeof(float), &d->a[c]);
1527 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 8 + c, sizeof(float), &d->b[c]);
1528 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 11 + c, sizeof(float), &scale[c]);
1529 size_t sizes[3] = { ROUNDUPDWD(cw, devid), ROUNDUPDHT(chh * 3, devid), 1 };
1530 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1531 if(err != CL_SUCCESS) goto cleanup;
1532
1533 err = dt_nn_unet_apply_stage_cl(model, 1, gd->nn_cl, devid, dev_cin, dev_chead, cw, chh);
1534 if(err != CL_SUCCESS) goto cleanup;
1535 const int KR = gd->k_residual;
1536 const int n3 = (int)(cplane * 3);
1537 dt_opencl_set_kernel_arg(devid, KR, 0, sizeof(cl_mem), &dev_cin);
1538 dt_opencl_set_kernel_arg(devid, KR, 1, sizeof(cl_mem), &dev_chead);
1539 dt_opencl_set_kernel_arg(devid, KR, 2, sizeof(cl_mem), &dev_cden);
1540 dt_opencl_set_kernel_arg(devid, KR, 3, sizeof(int), &n3);
1541 size_t sz[3] = { ROUNDUPDWD(n3, devid), 1, 1 };
1542 err = dt_opencl_enqueue_kernel_2d(devid, KR, sz);
1543 if(err != CL_SUCCESS) goto cleanup;
1544 cl_mem guide_src = dev_cden;
1545 const int KU = gd->k_upsample_n;
1546 const int three = 3;
1547 const cl_long dst_off = (cl_long)plane * 5;
1548 dt_opencl_set_kernel_arg(devid, KU, 0, sizeof(cl_mem), &guide_src);
1549 dt_opencl_set_kernel_arg(devid, KU, 1, sizeof(cl_mem), &dev_planes);
1550 dt_opencl_set_kernel_arg(devid, KU, 2, sizeof(int), &cw);
1551 dt_opencl_set_kernel_arg(devid, KU, 3, sizeof(int), &chh);
1552 dt_opencl_set_kernel_arg(devid, KU, 4, sizeof(int), &bin);
1553 dt_opencl_set_kernel_arg(devid, KU, 5, sizeof(int), &three);
1554 dt_opencl_set_kernel_arg(devid, KU, 6, sizeof(cl_long), &dst_off);
1555 size_t sizes_u[3] = { ROUNDUPDWD(pw, devid), ROUNDUPDHT(ph * 3, devid), 1 };
1556 err = dt_opencl_enqueue_kernel_2d(devid, KU, sizes_u);
1557 if(err != CL_SUCCESS) goto cleanup;
1558 }
1559
1560 // 3. fine pass (raw noise) + residual -> denoised plane
1561 err = dt_nn_unet_apply_stage_cl(model, 0, gd->nn_cl, devid, dev_planes, dev_noise, pw, ph);
1562 // a cancelled forward fails the tile like any error, and the pipeline does
1563 // not fall back to CPU for a run it abandoned
1564 if(err == DT_NN_CANCELLED) goto cleanup;
1565 if(err != CL_SUCCESS)
1566 {
1567 dt_print(DT_DEBUG_OPENCL, "[rawdenoiseai] GPU inference failed on %dx%d tile, falling back to CPU\n", pw, ph);
1568 goto cleanup;
1569 }
1570 {
1571 const int KR = gd->k_residual;
1572 const int n1 = (int)plane;
1573 dt_opencl_set_kernel_arg(devid, KR, 0, sizeof(cl_mem), &dev_planes);
1574 dt_opencl_set_kernel_arg(devid, KR, 1, sizeof(cl_mem), &dev_noise);
1575 dt_opencl_set_kernel_arg(devid, KR, 2, sizeof(cl_mem), &dev_den);
1576 dt_opencl_set_kernel_arg(devid, KR, 3, sizeof(int), &n1);
1577 size_t sz[3] = { ROUNDUPDWD(n1, devid), 1, 1 };
1578 err = dt_opencl_enqueue_kernel_2d(devid, KR, sz);
1579 if(err != CL_SUCCESS) goto cleanup;
1580 }
1581
1582 // 4. hybrid low-band fusion, entirely on device
1583 if(do_anchor)
1584 {
1585 {
1586 const int K = gd->k_bin16_mdv;
1587 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &dev_planes);
1588 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &dev_den);
1589 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(cl_mem), &grids[0]);
1590 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(cl_mem), &grids[1]);
1591 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(cl_mem), &grids[2]);
1592 dt_opencl_set_kernel_arg(devid, K, 5, sizeof(int), &pw);
1593 dt_opencl_set_kernel_arg(devid, K, 6, sizeof(int), &ph);
1594 size_t sizes[3] = { ROUNDUPDWD(gw, devid), ROUNDUPDHT(gh * 3, devid), 1 };
1595 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1596 if(err != CL_SUCCESS) goto cleanup;
1597 }
1598 const int nlev = _fusion_levels();
1599 int lw = gw, lh = gh;
1600 for(int k = 1; k < nlev; k++)
1601 {
1602 for(int md = 0; md < 3; md++)
1603 {
1604 const int K = gd->k_avg2x2;
1605 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &grids[3 * (k - 1) + md]);
1606 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &grids[3 * k + md]);
1607 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(int), &lw);
1608 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(int), &lh);
1609 size_t sizes[3] = { ROUNDUPDWD(lw / 2, devid), ROUNDUPDHT((lh / 2) * 3, devid), 1 };
1610 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1611 if(err != CL_SUCCESS) goto cleanup;
1612 }
1613 lw /= 2;
1614 lh /= 2;
1615 }
1616 // floor band: structure-gated blend (see the CPU comment)
1617 int fA = 9, fB = 10; // fused ping-pong, past the 3 x 3 level grids
1618 {
1619 const int K = gd->k_floor_fuse;
1620 const int S = 16 << (nlev - 1);
1621 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &grids[3 * (nlev - 1)]);
1622 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &grids[3 * (nlev - 1) + 1]);
1623 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(cl_mem), &grids[fA]);
1624 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(cl_mem), &grids[3 * (nlev - 1) + 2]);
1625 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(int), &lw);
1626 dt_opencl_set_kernel_arg(devid, K, 5, sizeof(int), &lh);
1627 dt_opencl_set_kernel_arg(devid, K, 6, sizeof(int), &S);
1628 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 7 + c, sizeof(float), &DT_NN_FUSION_DENS[c]);
1629 size_t sizes[3] = { ROUNDUPDWD(lw, devid), ROUNDUPDHT(lh * 3, devid), 1 };
1630 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1631 if(err != CL_SUCCESS) goto cleanup;
1632 }
1633 for(int k = nlev - 2; k >= 0; k--)
1634 {
1635 const int fw = lw * 2, fh = lh * 2, sc = 16 << k;
1636 {
1637 const int K = gd->k_fuse_step;
1638 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &grids[fA]);
1639 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &grids[3 * k]);
1640 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(cl_mem), &grids[3 * k + 1]);
1641 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(cl_mem), &grids[3 * (k + 1)]);
1642 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(cl_mem), &grids[3 * (k + 1) + 1]);
1643 dt_opencl_set_kernel_arg(devid, K, 5, sizeof(cl_mem), &grids[fB]);
1644 dt_opencl_set_kernel_arg(devid, K, 6, sizeof(cl_mem), &grids[3 * k + 2]);
1645 dt_opencl_set_kernel_arg(devid, K, 7, sizeof(int), &fw);
1646 dt_opencl_set_kernel_arg(devid, K, 8, sizeof(int), &fh);
1647 dt_opencl_set_kernel_arg(devid, K, 9, sizeof(int), &sc);
1648 for(int c = 0; c < 3; c++) dt_opencl_set_kernel_arg(devid, K, 10 + c, sizeof(float), &DT_NN_FUSION_DENS[c]);
1649 size_t sizes[3] = { ROUNDUPDWD(fw, devid), ROUNDUPDHT(fh * 3, devid), 1 };
1650 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1651 if(err != CL_SUCCESS) goto cleanup;
1652 }
1653 const int t = fA;
1654 fA = fB;
1655 fB = t;
1656 lw = fw;
1657 lh = fh;
1658 }
1659 {
1660 const int K = gd->k_bilerp_add;
1661 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &grids[fA]);
1662 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &grids[1]);
1663 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(cl_mem), &dev_planes);
1664 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(cl_mem), &dev_den);
1665 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(int), &pw);
1666 dt_opencl_set_kernel_arg(devid, K, 5, sizeof(int), &ph);
1667 size_t sizes[3] = { ROUNDUPDWD(pw, devid), ROUNDUPDHT(ph, devid), 1 };
1668 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1669 if(err != CL_SUCCESS) goto cleanup;
1670 }
1671 }
1672
1673 // 5. strength blend + crop straight into the pipeline output
1674 {
1675 const int K = gd->k_blend_crop;
1676 dt_opencl_set_kernel_arg(devid, K, 0, sizeof(cl_mem), &dev_in_buf);
1677 dt_opencl_set_kernel_arg(devid, K, 1, sizeof(cl_mem), &dev_den);
1678 dt_opencl_set_kernel_arg(devid, K, 2, sizeof(cl_mem), &dev_out_buf);
1679 dt_opencl_set_kernel_arg(devid, K, 3, sizeof(int), &width);
1680 dt_opencl_set_kernel_arg(devid, K, 4, sizeof(int), &height);
1681 dt_opencl_set_kernel_arg(devid, K, 5, sizeof(int), &pw);
1682 dt_opencl_set_kernel_arg(devid, K, 6, sizeof(float), &d->strength);
1683 size_t sizes[3] = { ROUNDUPDWD(width, devid), ROUNDUPDHT(height, devid), 1 };
1684 err = dt_opencl_enqueue_kernel_2d(devid, K, sizes);
1685 if(err != CL_SUCCESS) goto cleanup;
1686 }
1687 {
1688 // hand the result back in the pipeline's format (see the entry copy above)
1689 size_t origin[3] = { 0, 0, 0 };
1690 size_t region[3] = { (size_t)width, (size_t)height, 1 };
1691 err = dt_opencl_enqueue_copy_buffer_to_image(devid, dev_out_buf, dev_out, 0, origin, region);
1692 if(err != CL_SUCCESS) goto cleanup;
1693 }
1694 success = TRUE;
1695
1696cleanup:
1697 if(dev_planes) dt_opencl_release_mem_object(dev_planes);
1698 if(dev_noise) dt_opencl_release_mem_object(dev_noise);
1699 if(dev_den) dt_opencl_release_mem_object(dev_den);
1700 if(dev_xtrans) dt_opencl_release_mem_object(dev_xtrans);
1701 if(dev_in_buf) dt_opencl_release_mem_object(dev_in_buf);
1702 if(dev_out_buf) dt_opencl_release_mem_object(dev_out_buf);
1703 if(dev_cin) dt_opencl_release_mem_object(dev_cin);
1704 if(dev_chead) dt_opencl_release_mem_object(dev_chead);
1705 if(dev_cden) dt_opencl_release_mem_object(dev_cden);
1706 for(int k = 0; k < 11; k++)
1707 if(grids[k]) dt_opencl_release_mem_object(grids[k]);
1708 _nn_pipe = NULL;
1709 return success;
1710}
1711#endif // HAVE_OPENCL
1712
1714{
1715 dt_iop_default_init(module);
1716}
1717
1723
1725{
1726 dt_free_align(piece->data);
1727 piece->data = NULL;
1728}
1729
1730/* The combo lists "(shipped model)" then whatever .anselnn files the config
1731 * dir holds. The VALUE written to params is the basename, never the index —
1732 * see the params comment. gui_update re-derives the index from the name and
1733 * appends an entry for a name that is no longer on disk, so an edit made with
1734 * a since-removed model still shows what it wants instead of silently
1735 * displaying the wrong one. */
1737{
1738 if(dt_gui_widgets_suppressed()) return;
1741 const int idx = dt_bauhaus_combobox_get(w);
1742 const char *txt = idx > 0 ? dt_bauhaus_combobox_get_text(w) : NULL;
1743 if(txt)
1744 g_strlcpy(p->custom_model, txt, sizeof(p->custom_model));
1745 else
1746 p->custom_model[0] = '\0';
1747 dt_dev_add_history_item(self->dev, self, TRUE, TRUE);
1748}
1749
1751{
1754 dt_bauhaus_combobox_clear(g->custom_model);
1755 dt_bauhaus_combobox_add(g->custom_model, _("(shipped model)"));
1756 GList *files = _list_custom_models();
1757 int sel = 0, i = 0;
1758 for(GList *l = files; l; l = g_list_next(l))
1759 {
1760 dt_bauhaus_combobox_add(g->custom_model, (const char *)l->data);
1761 i++;
1762 if(!g_strcmp0((const char *)l->data, p->custom_model)) sel = i;
1763 }
1764 // the edit names a file that is not there any more: keep it visible
1765 if(p->custom_model[0] && !sel)
1766 {
1767 dt_bauhaus_combobox_add(g->custom_model, p->custom_model);
1768 sel = i + 1;
1769 }
1770 g_list_free_full(files, g_free);
1771 dt_bauhaus_combobox_set(g->custom_model, sel);
1772 // the shipped-matrix combos are meaningless while a user model is selected
1773 const gboolean shipped = !p->custom_model[0];
1774 gtk_widget_set_sensitive(g->version, shipped);
1775 gtk_widget_set_sensitive(g->size, shipped);
1776 gtk_widget_set_sensitive(g->scale_variant, shipped);
1777}
1778
1780{
1784 gtk_stack_set_visible_child_name(GTK_STACK(self->gui->widget), self->hide_enable_button ? "unsupported" : "raw");
1785
1786 // rescan on every panel update: the user may have dropped a file in since
1788
1789 // two-line status: the selected model (warn if its weights are missing) and
1790 // the noise profile the sigma map will use
1791 const gboolean have_model = gd && (p->custom_model[0] ? _get_custom_model(gd, p->custom_model)
1792 : _get_model(gd, p->version, p->size, p->scale_variant));
1793 GList *profiles = dt_noiseprofile_get_matching(&self->dev->image_storage);
1794 gchar *prof = profiles
1795 ? g_strdup_printf(_("noise profile: %s at ISO %d"), self->dev->image_storage.camera_makermodel,
1796 (int)self->dev->image_storage.exif_iso)
1797 : g_strdup(_("no noise profile for this camera — using the generic profile"));
1798 gchar *label = have_model
1799 ? g_strdup(prof)
1800 : g_strdup_printf(_("selected model (%s %s, %s) is not installed — module inactive\n%s"),
1801 _size_tag[CLAMP(p->size, 0, DT_RAWDENOISEAI_NUM_SIZES - 1)],
1802 _scale_tag[CLAMP(p->scale_variant, 0, DT_RAWDENOISEAI_NUM_SCALES - 1)],
1803 _version_tag[CLAMP(p->version, 0, DT_RAWDENOISEAI_NUM_VERSIONS - 1)], prof);
1804 gtk_label_set_text(GTK_LABEL(g->profile_label), label);
1806 g_free(label);
1807 g_free(prof);
1808 g_list_free_full(profiles, dt_noiseprofile_free);
1809}
1810
1812{
1814
1815 GtkWidget *box_raw = self->gui->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, DT_GUI_BOX_SPACING);
1816
1817 g->strength = dt_bauhaus_slider_from_params(self, "strength");
1818 dt_bauhaus_slider_set_digits(g->strength, 3);
1819 dt_bauhaus_slider_set_format(g->strength, "%");
1820 gtk_widget_set_tooltip_text(g->strength, _("opacity of the noise removal: blends between the original\n"
1821 "image (0%) and the fully denoised result (100%).\n"
1822 "lower it to keep some residual grain"));
1823
1824 g->version = dt_bauhaus_combobox_from_params(self, "version");
1825 gtk_widget_set_tooltip_text(g->version, _("neural model version. Older edits keep their original\n"
1826 "version so their result never changes across updates."));
1827
1828 g->size = dt_bauhaus_combobox_from_params(self, "size");
1829 gtk_widget_set_tooltip_text(g->size, _("network width. large: reference quality, practical on GPU\n"
1830 "(OpenCL) and the default there. half: ~4x faster.\n"
1831 "quarter: ~4x faster again, the default without OpenCL\n"
1832 "and the choice for weak hardware or near-realtime editing."));
1833
1834 g->scale_variant = dt_bauhaus_combobox_from_params(self, "scale_variant");
1835 gtk_widget_set_tooltip_text(g->scale_variant, _("single-scale: the fine full-resolution pass only — fast,\n"
1836 "no low-frequency chroma handling. multiscale: adds the\n"
1837 "coarse chroma pass and the low-band fusion — high quality,\n"
1838 "recommended for high ISO."));
1839
1841 dt_bauhaus_widget_set_label(g->custom_model, N_("custom model"));
1842 gtk_box_pack_start(GTK_BOX(box_raw), g->custom_model, TRUE, TRUE, 0);
1843 gtk_widget_set_tooltip_text(g->custom_model,
1844 _("use a neural model of your own instead of the shipped ones.\n"
1845 "drop a .anselnn file into your Ansel config directory and it\n"
1846 "appears here; the edit records the file NAME, so it keeps\n"
1847 "pointing at the same model as the folder changes."));
1848 g_signal_connect(G_OBJECT(g->custom_model), "value-changed", G_CALLBACK(_custom_model_callback), self);
1849
1850 gtk_box_pack_start(GTK_BOX(box_raw), dt_ui_section_label_new(_("noise profile correction")), FALSE, FALSE, 0);
1851
1852 g->profile_label = dt_ui_label_new("");
1853 gtk_label_set_line_wrap(GTK_LABEL(g->profile_label), TRUE);
1854 gtk_box_pack_start(GTK_BOX(box_raw), g->profile_label, FALSE, FALSE, 0);
1855
1856 g->noise_level = dt_bauhaus_slider_from_params(self, "noise_level");
1857 dt_bauhaus_slider_set_digits(g->noise_level, 3);
1858 dt_bauhaus_slider_set_format(g->noise_level, "%");
1859 gtk_widget_set_tooltip_text(g->noise_level, _("global scale on the assumed noise amplitude, relative to the\n"
1860 "calibrated noise for this camera at this ISO (100% trusts the\n"
1861 "calibration). raise it if noise remains, lower it if fine\n"
1862 "detail is eaten"));
1863
1864 dt_gui_new_collapsible_section(&g->cs, "plugins/darkroom/rawdenoiseai/expand_channel",
1865 _("per-channel corrections"), GTK_BOX(box_raw), GTK_PACK_START);
1866 self->gui->widget = GTK_WIDGET(g->cs.container); // sliders below pack into the section
1867
1868 const char *sigma_tooltip = _("per-channel correction of the camera noise profile, applied on top\n"
1869 "of the global correction. Profiles are measured after demosaicing,\n"
1870 "which averages away part of the noise — most on the dense green\n"
1871 "lattice — while this module sees the raw sensor noise at full\n"
1872 "strength. The defaults are calibrated against raw-mosaic\n"
1873 "measurements over 253 cameras.");
1874 g->sigma_red = dt_bauhaus_slider_from_params(self, "sigma_red");
1875 dt_bauhaus_slider_set_digits(g->sigma_red, 3);
1876 dt_bauhaus_slider_set_format(g->sigma_red, "%");
1877 gtk_widget_set_tooltip_text(g->sigma_red, sigma_tooltip);
1878
1879 g->sigma_green = dt_bauhaus_slider_from_params(self, "sigma_green");
1880 dt_bauhaus_slider_set_digits(g->sigma_green, 3);
1881 dt_bauhaus_slider_set_format(g->sigma_green, "%");
1882 gtk_widget_set_tooltip_text(g->sigma_green, sigma_tooltip);
1883
1884 g->sigma_blue = dt_bauhaus_slider_from_params(self, "sigma_blue");
1885 dt_bauhaus_slider_set_digits(g->sigma_blue, 3);
1886 dt_bauhaus_slider_set_format(g->sigma_blue, "%");
1887 gtk_widget_set_tooltip_text(g->sigma_blue, sigma_tooltip);
1888
1889 self->gui->widget = box_raw; // done packing into the collapsible section
1890
1891 GtkWidget *label_unsupported = dt_ui_label_new(_("AI raw denoising needs a mosaiced raw image\n"
1892 "and an installed rawdenoiseai model file."));
1893
1894 self->gui->widget = gtk_stack_new();
1895 gtk_stack_set_homogeneous(GTK_STACK(self->gui->widget), FALSE);
1896 gtk_stack_add_named(GTK_STACK(self->gui->widget), label_unsupported, "unsupported");
1897 gtk_stack_add_named(GTK_STACK(self->gui->widget), box_raw, "raw");
1898}
1899
1901{
1903}
1904// clang-format off
1905// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
1906// vim: shiftwidth=2 expandtab tabstop=2 cindent
1907// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
1908// clang-format on
static double * inv
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
void cleanup(dt_imageio_module_format_t *self)
Definition avif.c:170
#define m
Definition basecurve.c:283
void dt_bauhaus_slider_set_digits(GtkWidget *widget, int val)
Definition bauhaus.c:3457
void dt_bauhaus_combobox_clear(GtkWidget *widget)
Definition bauhaus.c:2045
int dt_bauhaus_combobox_get(GtkWidget *widget)
Definition bauhaus.c:2190
const char * dt_bauhaus_combobox_get_text(GtkWidget *widget)
Definition bauhaus.c:2018
void dt_bauhaus_combobox_set(GtkWidget *widget, const int pos)
Definition bauhaus.c:2144
void dt_bauhaus_widget_set_label(GtkWidget *widget, const char *label)
Definition bauhaus.c:1545
GtkWidget * dt_bauhaus_combobox_new(dt_bauhaus_t *bh, dt_gui_module_t *self)
Definition bauhaus.c:1746
void dt_bauhaus_slider_set_format(GtkWidget *widget, const char *format)
Definition bauhaus.c:3521
void dt_bauhaus_combobox_add(GtkWidget *widget, const char *text)
Definition bauhaus.c:1872
void dt_gui_new_collapsible_section(dt_gui_collapsible_section_t *cs, const char *confname, const char *label, GtkBox *parent, GtkPackType pack)
Create a collapsible section and pack it into the parent box.
void dt_gui_update_collapsible_section(dt_gui_collapsible_section_t *cs)
@ IOP_CS_RAW
static const float x
const float f
const int t
const float v
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
Definition colorspaces.h:98
const dt_colormatrix_t dt_aligned_pixel_t out
const float top
static const dt_colormatrix_t M
static float strength(float value, float strength)
Definition colorzones.c:431
#define S(V, params)
gboolean dt_image_needs_demosaic(const dt_image_t *img)
struct dt_bauhaus_t * dt_bauhaus_get_global(void)
Definition darktable.c:649
static int FCxtrans(const int row, const int col, global const unsigned char(*const xtrans)[6])
static int FC(const int row, const int col, const unsigned int filters)
#define dt_dev_add_history_item(dev, module, enable, redraw)
void dt_iop_params_t
Definition dev_history.h:44
void default_input_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
static int dt_pthread_mutex_unlock(dt_pthread_mutex_t *mutex) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:127
static int dt_pthread_mutex_init(dt_pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
Initialise a mutex. With mutexattr NULL – which is how 54 of the 56 call sites in this tree spell it ...
Definition dtpthread.h:104
static int dt_pthread_mutex_destroy(dt_pthread_mutex_t *mutex)
Definition dtpthread.h:132
static int dt_pthread_mutex_lock(dt_pthread_mutex_t *mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:117
void dt_loc_get_datadir(char *datadir, size_t bufsize)
void dt_loc_get_user_config_dir(char *configdir, size_t bufsize)
memcpy(roundtrip, origin, sizeof(roundtrip))
dt_iop_roi_t in
Definition geometry.h:117
#define DT_GUI_MODULE(x)
static void dt_iop_image_copy_by_size(float *const __restrict__ out, const float *const __restrict__ in, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.h:91
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
void dt_iop_default_init(dt_iop_module_t *module)
Definition imageop.c:308
const char ** dt_iop_set_description(dt_iop_module_t *module, const char *main_text, const char *purpose, const char *input, const char *process, const char *output)
Definition imageop.c:1909
void dt_iop_request_focus(dt_iop_module_t *module)
Move darkroom focus to module, or clear it with NULL.
@ IOP_FLAGS_SUPPORTS_BLENDING
Definition imageop.h:186
@ IOP_FLAGS_ALLOW_TILING
Definition imageop.h:188
@ IOP_GROUP_REPAIR
Definition imageop.h:159
GtkWidget * dt_bauhaus_slider_from_params(dt_iop_module_t *self, const char *param)
GtkWidget * dt_bauhaus_combobox_from_params(dt_iop_module_t *self, const char *param)
#define IOP_GUI_FREE
Definition imageop_gui.h:96
static dt_iop_gui_data_t * dt_iop_gui_data(const struct dt_iop_module_t *m)
The module's GUI data blob, NULL-safe for headless callers: IOP process() implementations read it for...
Definition imageop_gui.h:81
#define IOP_GUI_ALLOC(module)
Definition imageop_gui.h:93
void *const ovoid
const char * model
GtkWidget * dt_ui_section_label_new(const gchar *str)
Definition label.c:114
GtkWidget * dt_ui_label_new(const gchar *str)
Definition label.c:125
static const dt_dev_pixelpipe_t * pipe
Definition layers.h:49
static const dt_dev_pixelpipe_t const dt_dev_pixelpipe_iop_t * piece
Definition layers.h:50
static const dt_iop_drawlayer_params_t * params
Definition layers.h:80
#define w2
Definition lmmse.c:60
@ DT_DEBUG_OPENCL
Definition logging.h:57
@ DT_DEBUG_PARAMS
Definition logging.h:71
@ DT_DEBUG_ALWAYS
Definition logging.h:49
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.
float *const restrict const size_t k
#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
#define dt_free_align(ptr)
Release memory from dt_alloc_align() and set ptr to NULL.
Definition mem_alloc.h:214
static void * dt_calloc_align(size_t size)
dt_alloc_align() followed by a zero fill.
Definition mem_alloc.h:225
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
#define o
g_free(manifest_path)
#define DT_MODULE_INTROSPECTION(MODVER, PARAMSTYPE)
DT_MODULE() for a module whose params struct is introspected.
static float gh(const float f)
void dt_nn_cl_destroy(dt_nn_cl_t *cl)
Definition nn_model.c:1133
int dt_nn_unet_apply_stage_cl(const dt_nn_model_t *m, int stage, dt_nn_cl_t *cl, int devid, cl_mem dev_in, cl_mem dev_out, int width, int height)
Definition nn_model.c:1395
int dt_nn_model_anchor(const dt_nn_model_t *m)
Definition nn_model.c:450
void dt_nn_model_free(dt_nn_model_t *m)
Definition nn_model.c:413
int dt_nn_model_in_channels(const dt_nn_model_t *m)
Definition nn_model.c:424
int dt_nn_model_coarse_in_channels(const dt_nn_model_t *m)
Definition nn_model.c:440
dt_nn_cl_t * dt_nn_cl_create(int program)
Definition nn_model.c:1123
size_t dt_nn_unet_scratch_bytes(const dt_nn_model_t *m, int width, int height)
Definition nn_model.c:744
int dt_nn_unet_apply_stage(const dt_nn_model_t *m, int stage, const float *in, float *out, int width, int height, int apply_residual)
Definition nn_model.c:1044
void dt_nn_set_cancel(dt_nn_cancel_f cancel_fn)
Definition nn_model.c:86
float dt_nn_unet_scratch_per_px(const dt_nn_model_t *m)
Definition nn_model.c:715
dt_nn_model_t * dt_nn_model_load(const char *path, char *err, size_t err_len)
Definition nn_model.c:245
__DT_CLONE_TARGETS__ void dt_nn_upsample_nearest(const float *in, int ch, int w, int h, int factor, float *out)
Definition nn_model.c:1093
int dt_nn_model_coarse_out_channels(const dt_nn_model_t *m)
Definition nn_model.c:445
float dt_nn_unet_scratch_per_px_cl(const dt_nn_model_t *m)
Definition nn_model.c:720
int dt_nn_model_alignment(const dt_nn_model_t *m)
Definition nn_model.c:468
__DT_CLONE_TARGETS__ void dt_nn_bin_planes(const float *planes, int pw, int ph, int bin, float *out_rgb, float *out_cnt)
Definition nn_model.c:1057
int dt_nn_model_bin(const dt_nn_model_t *m, const int is_xtrans)
Definition nn_model.c:434
void dt_nn_set_allocator(dt_nn_alloc_f alloc_fn, dt_nn_free_f free_fn)
Definition nn_model.c:77
#define DT_NN_CANCELLED
Definition nn_model.h:78
#define DT_NN_FUSION_COARSEST
Definition nn_model.h:111
#define DT_NN_FUSION_FINEST
Definition nn_model.h:110
const dt_noiseprofile_t dt_noiseprofile_generic
void dt_noiseprofile_interpolate(const dt_noiseprofile_t *const p1, const dt_noiseprofile_t *const p2, dt_noiseprofile_t *out)
void dt_noiseprofile_free(gpointer data)
GList * dt_noiseprofile_get_matching(const dt_image_t *cimg)
int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
Definition opencl.c:2644
void * dt_opencl_alloc_device_buffer(const int devid, const size_t size)
Definition opencl.c:3060
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:2792
int dt_opencl_create_kernel(const int prog, const char *name)
Definition opencl.c:2538
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:2828
int dt_opencl_is_enabled(void)
Definition opencl.c:3384
void dt_opencl_free_kernel(const int kernel)
Definition opencl.c:2581
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:2635
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:2780
void dt_opencl_release_mem_object(cl_mem mem)
Definition opencl.c:2895
#define ROUNDUPDHT(a, b)
Definition opencl.h:87
#define ROUNDUPDWD(a, b)
Definition opencl.h:86
#define __OMP_PARALLEL_FOR__(...)
Definition openmp.h:95
#define DT_PATH_MAX
Buffer size for a filesystem path anywhere in Ansel.
Definition paths.h:57
void dt_concat_path_file(char destination[4096], const char path[4096], const char *const file)
Append a constant filename to a variable, stack-based, fixed-sized, directory, and add a / in-between...
Definition darktable.c:2683
const char * name
Definition pdf.h:90
void dt_iop_buffer_dsc_update_bpp(dt_iop_buffer_dsc_t *dsc)
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
#define dt_pixelpipe_cache_alloc_align_cache(size, id)
#define dt_pixelpipe_cache_free_align(mem)
static gboolean dt_dev_pixelpipe_has_shutdown(const dt_dev_pixelpipe_t *pipe)
static dt_nn_model_t * _get_custom_model(dt_iop_rawdenoiseai_global_data_t *gd, const char *base)
void init(dt_iop_module_t *module)
const char ** description(struct dt_iop_module_t *self)
int default_group()
static const char *const _scale_tag[2]
__DT_CLONE_TARGETS__ int process(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid)
static void _region_free(nn_region_t *r, void *p)
#define DT_NN_FUSION_T_CHI2
static __DT_CLONE_TARGETS__ void _k_bin16_mdv(const float *const nn_in, const float *const denoised, float *const M, float *const D, float *const V, const int pw, const int ph)
void reload_defaults(dt_iop_module_t *module)
static int _apply_low_band_anchor(const float *const nn_in, float *const denoised, const int pw, const int ph, const int scale)
static void _sigma_scale(const dt_iop_rawdenoiseai_data_t *d, float scale[3])
void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *params, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
#define NN_REGION_MAX_BLOCKS
static __thread const dt_dev_pixelpipe_t * _nn_pipe
dt_iop_rawdenoiseai_scale_t
@ DT_RAWDENOISEAI_MULTI
@ DT_RAWDENOISEAI_SINGLE
void gui_update(dt_iop_module_t *self)
Refresh GUI controls from current params and configuration.
static int _nn_cancelled(void)
#define NN_REGION_SLACK
static __DT_CLONE_TARGETS__ void _upsample_bilinear(const float *const src, const int sw, const int sh, const int f, float *const dst)
static void _nn_arena_free(void *p)
static __DT_CLONE_TARGETS__ void _k_residual(const float *const in, const float *const head, float *const out, const size_t n)
void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
static gboolean _rawdenoiseai_supported(dt_iop_module_t *module)
static GList * _list_custom_models(void)
const char * name()
void gui_init(dt_iop_module_t *self)
static void _custom_model_populate(dt_iop_module_t *self)
static __DT_CLONE_TARGETS__ void _k_bin_planes(const float *const nn_in, float *const coarse_in, float *const cnt, const int pw, const int ph, const int bin, const dt_iop_rawdenoiseai_data_t *const d)
#define DT_RAWDENOISEAI_MODEL_LEN
static __DT_CLONE_TARGETS__ void _k_assemble(const float *const in, float *const nn_in, const int width, const int height, const int pw, const int ph, const dt_iop_rawdenoiseai_data_t *const d, const uint32_t filters, const uint8_t(*const xtrans)[6], const dt_iop_roi_t *const roi)
static __thread nn_region_t * _nn_region
void tiling_callback(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe, const struct dt_dev_pixelpipe_iop_t *piece, struct dt_develop_tiling_t *tiling)
static __DT_CLONE_TARGETS__ void _k_bilerp_add(const float *const fused, const float *const D16, float *const scratch, const float *const nn_in, float *const denoised, const int pw, const int ph, const size_t p0, const int cw0, const int ch0)
void gui_cleanup(dt_iop_module_t *self)
static void _fetch_noise_profile(dt_iop_module_t *self, dt_iop_rawdenoiseai_data_t *d)
void cleanup_global(dt_iop_module_so_t *module)
__DT_CLONE_TARGETS__ static __DT_CLONE_TARGETS__ void _k_floor_fuse(const float *const M, const float *const D, const float *const V, float *const fused, const int sw, const int sh, const size_t p0, const int S)
static void _custom_model_callback(GtkWidget *w, dt_iop_module_t *self)
int default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece)
void input_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
static const char *const _version_tag[1]
int flags()
dt_iop_rawdenoiseai_size_t
@ DT_RAWDENOISEAI_LARGE
@ DT_RAWDENOISEAI_QUARTER
@ DT_RAWDENOISEAI_HALF
static const char *const _size_tag[3]
static void * _nn_arena_alloc(size_t bytes, int long_lived)
gboolean force_enable(struct dt_iop_module_t *self, const gboolean current_state)
void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
static unsigned _align_lcm(const unsigned a, const unsigned b)
static void * _region_alloc(nn_region_t *r, size_t bytes, int long_lived)
#define DT_RAWDENOISEAI_NUM_SCALES
void init_global(dt_iop_module_so_t *module)
static __DT_CLONE_TARGETS__ void _k_avg2x2(const float *const in, float *const out, const int sw, const int sh, const size_t p0)
static __DT_CLONE_TARGETS__ void _k_blend_crop(const float *const in, const float *const den, float *const out, const int width, const int height, const int pw, const float strength)
dt_iop_rawdenoiseai_version_t
@ DT_RAWDENOISEAI_V1
int process_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)
#define DT_RAWDENOISEAI_NUM_SIZES
static dt_nn_model_t * _get_model(dt_iop_rawdenoiseai_global_data_t *gd, dt_iop_rawdenoiseai_version_t ver, dt_iop_rawdenoiseai_size_t sz, dt_iop_rawdenoiseai_scale_t sc)
static int _fusion_levels(void)
static const float DT_NN_FUSION_DENS[3]
static __DT_CLONE_TARGETS__ void _k_fuse_step(const float *const fused_c, const float *const Mf, const float *const Df, const float *const Vf, const float *const Mc, const float *const Dc, float *const fused_f, float *const ups, const int sw, const int sh, const size_t p0, const int sc)
#define DT_RAWDENOISEAI_NUM_VERSIONS
static dt_iop_rawdenoiseai_size_t _default_size(void)
#define lw
Definition retouch.c:1121
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
const float sigma
const float r
dt_image_t image_storage
Definition develop.h:225
char camera_makermodel[128]
Definition image.h:391
float exif_iso
Definition image.h:379
unsigned int channels
Definition format.h:83
GtkWidget * widget
Definition imageop_gui.h:47
dt_iop_global_data_t * data
Definition imageop.h:238
int32_t hide_enable_button
Definition imageop.h:270
dt_iop_params_t * default_params
Definition imageop.h:333
struct dt_iop_module_gui_t * gui
Definition imageop.h:346
struct dt_develop_t * dev
Definition imageop.h:311
dt_iop_global_data_t * global_data
Definition imageop.h:337
dt_iop_params_t * params
Definition imageop.h:333
dt_nn_model_t * models[1][3][2]
dt_gui_collapsible_section_t cs
dt_iop_rawdenoiseai_version_t version
dt_iop_rawdenoiseai_size_t size
dt_iop_rawdenoiseai_scale_t scale_variant
Region of interest passed through the pixelpipe.
Definition format.h:49
int width
Definition format.h:50
int height
Definition format.h:50
dt_aligned_pixel_t a
dt_aligned_pixel_t b
struct nn_region_t::@204005242210332105254067302307172354074061017313 blocks[32]
#define __DT_CLONE_TARGETS__
typedef double((*spd)(unsigned long int wavelength, double TempK))
#define MAX(a, b)
Definition thinplate.c:29
gboolean dt_gui_widgets_suppressed(void)
#define DT_GUI_BOX_SPACING