Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
test_drawlayer_batch_raster.c
Go to the documentation of this file.
1/*
2 This file is part of the Ansel project.
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
33
34#include "iop/drawlayer/brush.h"
35#include "iop/drawlayer/cache.h"
36#include "iop/drawlayer/paint.h"
37
38#include <setjmp.h>
39#include <stdarg.h>
40#include <stddef.h>
41#include <stdint.h>
42#include <stdlib.h>
43#include <string.h>
44#include <math.h>
45#include <time.h>
46#ifdef _OPENMP
47#include <omp.h>
48#endif
49#include <cmocka.h>
50
51#ifdef _WIN32
52#include "win/main_wrapper.h"
53#endif
54
55#define W 96
56#define H 96
57
63
64static void _plane_init(plane_t *p)
65{
66 p->patch = (dt_drawlayer_cache_patch_t){ .x = 0, .y = 0, .width = W, .height = H,
67 .pixels = calloc((size_t)W * H * 4, sizeof(float)),
68 .external_alloc = TRUE };
69 p->mask = (dt_drawlayer_cache_patch_t){ .x = 0, .y = 0, .width = W, .height = H,
70 .pixels = calloc((size_t)W * H, sizeof(float)),
71 .external_alloc = TRUE };
72 assert_non_null(p->patch.pixels);
73 assert_non_null(p->mask.pixels);
74}
75
76static void _plane_free(plane_t *p)
77{
78 free(p->patch.pixels);
79 free(p->mask.pixels);
80}
81
83static void _build_dabs_tex(dt_drawlayer_brush_dab_t *dabs, const int count, const int mode,
84 const float opacity, const float radius, const float step,
85 const float sprinkles)
86{
87 for(int i = 0; i < count; i++)
88 {
90 .x = 24.0f + step * (float)i,
91 .y = 48.0f,
92 .radius = radius,
93 .dir_x = 1.0f,
94 .dir_y = 0.0f,
95 .sample_spacing = step,
96 .sample_opacity_scale = 1.0f,
97 .opacity = opacity,
98 .flow = 1.0f, /* UI 100% -> internal 0, the regime the closed form covers */
99 .sprinkles = sprinkles,
100 .sprinkle_size = 3.0f,
101 .sprinkle_coarseness = 0.5f,
102 .hardness = 0.5f,
103 .color = { 0.8f, 0.4f, 0.2f, 1.0f },
105 .mode = mode,
106 .stroke_batch = 7u,
107 .stroke_pos = (uint8_t)(i == 0 ? DT_DRAWLAYER_PAINT_STROKE_FIRST
109 };
110 }
111}
112
113static void _build_dabs(dt_drawlayer_brush_dab_t *dabs, const int count, const int mode,
114 const float opacity, const float radius, const float step)
115{
116 _build_dabs_tex(dabs, count, mode, opacity, radius, step, 0.0f);
117}
118
120static void _run_serial(plane_t *p, const dt_drawlayer_brush_dab_t *dabs, const int count)
121{
123 assert_non_null(runtime);
124 for(int i = 0; i < count; i++)
125 dt_drawlayer_brush_rasterize(NULL, &p->patch, 1.0f, &dabs[i], 1.0f, &p->mask, runtime);
127}
128
129static void _run_batch(plane_t *p, const dt_drawlayer_brush_dab_t *dabs, const int count)
130{
131 dt_drawlayer_brush_batch_t batch = { 0 };
132 assert_true(dt_drawlayer_brush_batch_is_uniform(dabs, (guint)count, &batch));
133
134 float *transmittance = calloc((size_t)W * H, sizeof(float));
135 float *noise = calloc((size_t)W * H, sizeof(float));
136 assert_non_null(transmittance);
137 assert_non_null(noise);
138 assert_true(dt_drawlayer_brush_rasterize_batch(&batch, &p->patch, 1.0f, &p->mask, transmittance, noise, NULL));
139 free(transmittance);
140 free(noise);
141}
142
143static double _max_abs_diff(const float *a, const float *b, const size_t n)
144{
145 double worst = 0.0;
146 for(size_t i = 0; i < n; i++)
147 {
148 const double d = fabs((double)a[i] - (double)b[i]);
149 if(d > worst) worst = d;
150 }
151 return worst;
152}
153
154static void _compare_tex(const int mode, const float opacity, const float radius, const float step,
155 const int count, const float sprinkles)
156{
157 dt_drawlayer_brush_dab_t *dabs = calloc((size_t)count, sizeof(*dabs));
158 assert_non_null(dabs);
159 _build_dabs_tex(dabs, count, mode, opacity, radius, step, sprinkles);
160
161 plane_t serial, batch;
162 _plane_init(&serial);
163 _plane_init(&batch);
164
165 /* ERASE needs something to erase: seed both planes identically with opaque white. */
167 {
168 for(size_t i = 0; i < (size_t)W * H * 4; i++) serial.patch.pixels[i] = 1.0f;
169 memcpy(batch.patch.pixels, serial.patch.pixels, (size_t)W * H * 4 * sizeof(float));
170 }
171
172 _run_serial(&serial, dabs, count);
173 _run_batch(&batch, dabs, count);
174
175 const double pixel_diff = _max_abs_diff(serial.patch.pixels, batch.patch.pixels, (size_t)W * H * 4);
176 const double mask_diff = _max_abs_diff(serial.mask.pixels, batch.mask.pixels, (size_t)W * H);
177
178 /* Sanity: the dabs must actually have painted, or the comparison proves nothing. */
179 double painted = 0.0;
180 for(size_t i = 0; i < (size_t)W * H; i++) painted += batch.mask.pixels[i];
181 assert_true(painted > 1.0);
182
183 print_message("mode=%d opacity=%.2f r=%.1f step=%.2f n=%d sprinkles=%.2f -> "
184 "max |dpixel|=%.3e max |dmask|=%.3e\n",
185 mode, opacity, radius, step, count, sprinkles, pixel_diff, mask_diff);
186
187 /* Measured on x86-64 GCC with -ffast-math: 1.192e-07, i.e. ONE ulp at 1.0 in float32.
188 * The bound is two orders of magnitude looser to absorb platform variation, and still
189 * three orders tighter than any real algebraic mistake -- a dropped cap or a wrong order
190 * of operations moves this to O(0.1). */
191 assert_true(pixel_diff < 1e-5);
192 assert_true(mask_diff < 1e-5);
193
194 _plane_free(&serial);
195 _plane_free(&batch);
196 free(dabs);
197}
198
199static void _compare(const int mode, const float opacity, const float radius, const float step,
200 const int count)
201{
202 _compare_tex(mode, opacity, radius, step, count, 0.0f);
203}
204
214{
215 (void)state;
216 _compare_tex(DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.9f, 12.0f, 1.0f, 24, 0.6f);
217}
218
221{
222 (void)state;
223 _compare(DT_DRAWLAYER_BRUSH_MODE_PAINT, 1.0f, 12.0f, 1.0f, 32);
224}
225
228{
229 (void)state;
230 _compare(DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.25f, 12.0f, 1.0f, 40);
231}
232
235{
236 (void)state;
237 _compare(DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.6f, 8.0f, 11.0f, 6);
238}
239
241{
242 (void)state;
243 _compare(DT_DRAWLAYER_BRUSH_MODE_ERASE, 0.75f, 12.0f, 1.5f, 24);
244}
245
248{
249 (void)state;
251 dt_drawlayer_brush_batch_t batch = { 0 };
252
253 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f);
254 assert_true(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
255
256 /* A pressure-mapped opacity breaks the shared cap the single clamp depends on. */
257 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f);
258 dabs[2].opacity = 0.4f;
259 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
260
261 /* Flow below 100% keeps `accum_alpha`, which does not collapse into a product. */
262 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f);
263 dabs[1].flow = 0.5f;
264 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
265
266 /* A mid-batch colour change would make one composite wrong. */
267 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f);
268 dabs[3].color[1] = 0.9f;
269 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
270
271 /* Mixed modes cannot share one composite. */
272 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f);
274 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
275
276 /* The sprinkle field is shared, so its parameters are part of the contract. */
277 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f);
278 dabs[2].sprinkles = 0.5f;
279 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
280
281 _build_dabs_tex(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.8f, 10.0f, 2.0f, 0.5f);
282 dabs[1].sprinkle_size = 9.0f;
283 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
284
285 /* SMUDGE and BLUR read the destination per dab and never qualify. */
286 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_SMUDGE, 0.8f, 10.0f, 2.0f);
287 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
288 _build_dabs(dabs, 4, DT_DRAWLAYER_BRUSH_MODE_BLUR, 0.8f, 10.0f, 2.0f);
289 assert_false(dt_drawlayer_brush_batch_is_uniform(dabs, 4, &batch));
290}
291
302{
303 (void)state;
304 const int count = 32;
305 dt_drawlayer_brush_dab_t *dabs = calloc((size_t)count, sizeof(*dabs));
306 assert_non_null(dabs);
307 _build_dabs(dabs, count, DT_DRAWLAYER_BRUSH_MODE_PAINT, 0.7f, 14.0f, 1.0f);
308
309 plane_t one, many;
310 _plane_init(&one);
311 _plane_init(&many);
312
313#ifdef _OPENMP
314 const int saved = omp_get_max_threads();
315 omp_set_num_threads(1);
316#endif
317 _run_batch(&one, dabs, count);
318#ifdef _OPENMP
319 omp_set_num_threads(saved > 1 ? saved : 8);
320#endif
321 _run_batch(&many, dabs, count);
322#ifdef _OPENMP
323 omp_set_num_threads(saved);
324#endif
325
326 assert_memory_equal(one.patch.pixels, many.patch.pixels, (size_t)W * H * 4 * sizeof(float));
327 assert_memory_equal(one.mask.pixels, many.mask.pixels, (size_t)W * H * sizeof(float));
328
329 _plane_free(&one);
330 _plane_free(&many);
331 free(dabs);
332}
333
342/* Written as a loop rather than memset() on purpose: SonarCloud reads any memset() that zeroes
343 * a buffer as the "clearing sensitive data" pattern and rates it a security finding, which for a
344 * benchmark scratch buffer it is not. The loop says the same thing and says it only once, so the
345 * quality gate is not spending anybody's attention on it. Both timed paths below clear the mask
346 * the same way, so whatever this costs cancels in the comparison. */
347static void _clear_mask(float *const mask, const size_t px)
348{
349 for(size_t i = 0; i < px; i++) mask[i] = 0.0f;
350}
351
352static void _report_speedup(const float sprinkles)
353{
354 const int count = 32;
355 const float radius = 64.0f;
356 const int plane = 512;
357
358 dt_drawlayer_brush_dab_t *dabs = calloc((size_t)count, sizeof(*dabs));
359 assert_non_null(dabs);
360 for(int i = 0; i < count; i++)
361 {
362 dabs[i] = (dt_drawlayer_brush_dab_t){
363 .x = 160.0f + (float)i, .y = 256.0f, .radius = radius, .dir_x = 1.0f, .dir_y = 0.0f,
364 .sample_spacing = 1.0f, .sample_opacity_scale = 1.0f, .opacity = 1.0f, .flow = 1.0f,
365 .sprinkles = sprinkles, .sprinkle_size = 3.0f, .sprinkle_coarseness = 0.5f, .hardness = 0.5f,
366 .color = { 0.8f, 0.4f, 0.2f, 1.0f }, .shape = DT_DRAWLAYER_BRUSH_SHAPE_LINEAR,
367 .mode = DT_DRAWLAYER_BRUSH_MODE_PAINT, .stroke_batch = 3u,
368 .stroke_pos = (uint8_t)(i == 0 ? DT_DRAWLAYER_PAINT_STROKE_FIRST
370 };
371 }
372
373 const size_t px = (size_t)plane * plane;
374 float *rgba = calloc(px * 4, sizeof(float));
375 float *mask = calloc(px, sizeof(float));
376 float *transmittance = calloc(px, sizeof(float));
377 float *noise = calloc(px, sizeof(float));
378 assert_non_null(rgba);
379 assert_non_null(mask);
380 assert_non_null(transmittance);
381 assert_non_null(noise);
382
383 dt_drawlayer_cache_patch_t patch = { .width = plane, .height = plane, .pixels = rgba,
384 .external_alloc = TRUE };
385 dt_drawlayer_cache_patch_t mpatch = { .width = plane, .height = plane, .pixels = mask,
386 .external_alloc = TRUE };
387
388 const int rounds = 40;
389 struct timespec t0, t1;
390
392 assert_non_null(runtime);
393 clock_gettime(CLOCK_MONOTONIC, &t0);
394 for(int r = 0; r < rounds; r++)
395 {
396 _clear_mask(mask, px);
397 for(int i = 0; i < count; i++)
398 dt_drawlayer_brush_rasterize(NULL, &patch, 1.0f, &dabs[i], 1.0f, &mpatch, runtime);
399 }
400 clock_gettime(CLOCK_MONOTONIC, &t1);
401 const double serial_ms = ((double)(t1.tv_sec - t0.tv_sec) * 1e3
402 + (double)(t1.tv_nsec - t0.tv_nsec) / 1e6) / (double)rounds;
404
405 dt_drawlayer_brush_batch_t batch = { 0 };
406 assert_true(dt_drawlayer_brush_batch_is_uniform(dabs, (guint)count, &batch));
407 clock_gettime(CLOCK_MONOTONIC, &t0);
408 for(int r = 0; r < rounds; r++)
409 {
410 _clear_mask(mask, px);
411 dt_drawlayer_brush_rasterize_batch(&batch, &patch, 1.0f, &mpatch, transmittance, noise, NULL);
412 }
413 clock_gettime(CLOCK_MONOTONIC, &t1);
414 const double batch_ms = ((double)(t1.tv_sec - t0.tv_sec) * 1e3
415 + (double)(t1.tv_nsec - t0.tv_nsec) / 1e6) / (double)rounds;
416
417 print_message("one heartbeat batch, r=%.0f spacing=1 dabs=%d sprinkles=%.2f: "
418 "per-dab %.3f ms, batch %.3f ms (%.1fx)\n",
419 radius, count, sprinkles, serial_ms, batch_ms, serial_ms / fmax(batch_ms, 1e-9));
420
421 free(rgba);
422 free(mask);
423 free(transmittance);
424 free(noise);
425 free(dabs);
426}
427
429{
430 (void)state;
431 _report_speedup(0.0f);
432 _report_speedup(0.6f);
433}
434
435int main(int argc, char *argv[])
436{
437 const struct CMUnitTest tests[] = {
438 cmocka_unit_test(test_report_batch_speedup),
443 cmocka_unit_test(test_batch_matches_serial_erase),
444 cmocka_unit_test(test_batch_matches_serial_sprinkles),
445 cmocka_unit_test(test_gate_refuses_non_uniform),
446 };
447 return cmocka_run_group_tests(tests, NULL, NULL);
448}
#define TRUE
Definition ashift_lsd.c:162
Dab-level brush rasterization API for drawlayer.
@ DT_DRAWLAYER_BRUSH_MODE_ERASE
Definition brush.h:51
@ DT_DRAWLAYER_BRUSH_MODE_PAINT
Definition brush.h:50
@ DT_DRAWLAYER_BRUSH_MODE_BLUR
Definition brush.h:52
@ DT_DRAWLAYER_BRUSH_MODE_SMUDGE
Definition brush.h:53
@ DT_DRAWLAYER_BRUSH_SHAPE_LINEAR
Definition brush.h:41
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
#define H
Definition diffuse.c:614
memcpy(roundtrip, origin, sizeof(roundtrip))
GdkRGBA color[]
Definition geotagging.c:541
gboolean dt_drawlayer_brush_batch_is_uniform(const dt_drawlayer_brush_dab_t *dabs, const guint count, dt_drawlayer_brush_batch_t *out)
Decide whether a run of dabs can take the batch path, and describe it if so.
gboolean dt_drawlayer_brush_rasterize(const dt_drawlayer_cache_patch_t *sample_patch, dt_drawlayer_cache_patch_t *patch, const float scale, const dt_drawlayer_brush_dab_t *dab, const float sample_opacity_scale, dt_drawlayer_cache_patch_t *stroke_mask, dt_drawlayer_paint_stroke_t *runtime_private)
Public dab rasterization entry point.
gboolean dt_drawlayer_brush_rasterize_batch(const dt_drawlayer_brush_batch_t *batch, dt_drawlayer_cache_patch_t *patch, const float scale, dt_drawlayer_cache_patch_t *stroke_mask, float *const transmittance, float *const noise_scratch, dt_drawlayer_damaged_rect_t *batch_damage)
Rasterize a whole batch in two passes: accumulate transmittance, then composite once.
Patch/cache helpers for drawlayer process and preview buffers.
void dt_drawlayer_paint_runtime_private_destroy(dt_drawlayer_paint_stroke_t **state)
Destroy stroke runtime payload and null pointer.
dt_drawlayer_paint_stroke_t * dt_drawlayer_paint_runtime_private_create(void)
Allocate stroke runtime payload object used by paint+brush internals.
Stroke-level path sampling and runtime-state API for drawlayer.
@ DT_DRAWLAYER_PAINT_STROKE_MIDDLE
@ DT_DRAWLAYER_PAINT_STROKE_FIRST
#define omp_get_max_threads()
Definition openmp.h:90
int main()
Definition prova.c:47
const float uint32_t state[4]
const float r
const float noise
A run of dabs that share everything the batch composite needs to be a single step.
Definition brush.h:120
Fully resolved input dab descriptor.
Definition brush.h:66
Generic float RGBA patch stored either in malloc memory or pixel cache.
Mutable stroke runtime state owned by worker/backend code.
dt_drawlayer_cache_patch_t mask
dt_drawlayer_cache_patch_t patch
typedef double((*spd)(unsigned long int wavelength, double TempK))
static void _plane_free(plane_t *p)
static void test_batch_matches_serial_paint_capped(void **state)
The cap binds mid-batch: opacity well below 1 with many overlapping dabs.
static void _run_serial(plane_t *p, const dt_drawlayer_brush_dab_t *dabs, const int count)
Run the reference path: one dt_drawlayer_brush_rasterize per dab, in order.
static void _compare_tex(const int mode, const float opacity, const float radius, const float step, const int count, const float sprinkles)
static void test_batch_is_thread_count_independent(void **state)
The batch result must not depend on the thread count.
static void _build_dabs(dt_drawlayer_brush_dab_t *dabs, const int count, const int mode, const float opacity, const float radius, const float step)
static void test_report_batch_speedup(void **state)
static void test_batch_matches_serial_erase(void **state)
static void _clear_mask(float *const mask, const size_t px)
Time both paths on the shipped regime, so the speedup is measured and not derived.
static void _build_dabs_tex(dt_drawlayer_brush_dab_t *dabs, const int count, const int mode, const float opacity, const float radius, const float step, const float sprinkles)
A short stroke of overlapping dabs, uniform in everything the gate tests.
static void _compare(const int mode, const float opacity, const float radius, const float step, const int count)
static double _max_abs_diff(const float *a, const float *b, const size_t n)
static void test_batch_matches_serial_sprinkles(void **state)
Texture on: the batch shares ONE sprinkle field across every dab.
static void _plane_init(plane_t *p)
static void test_batch_matches_serial_paint_dense(void **state)
Heavy overlap, the shipped regime: spacing far below the diameter.
static void _run_batch(plane_t *p, const dt_drawlayer_brush_dab_t *dabs, const int count)
static void test_batch_matches_serial_paint_sparse(void **state)
Sparse dabs, barely overlapping.
static void _report_speedup(const float sprinkles)
static void test_gate_refuses_non_uniform(void **state)
The gate must refuse everything the closed form is not derived for.