Blender V5.3
ocean.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <algorithm>
13#include <cmath>
14#include <cstdlib>
15
16#include <cstring>
17
18#include "MEM_guardedalloc.h"
19
20#include "DNA_modifier_types.h"
21#include "DNA_scene_types.h"
22
23#include "BLI_math_vector_c.hh"
24#include "BLI_path_utils.hh"
25#include "BLI_rand_c.hh"
26#include "BLI_task_c.hh"
27#include "BLI_utildefines.hh"
28
29#include "BKE_image.hh"
30#include "BKE_image_format.hh"
31#include "BKE_ocean.h"
32#include "ocean_intern.h"
33
34#include "IMB_imbuf.hh"
35#include "IMB_imbuf_types.hh"
36#include "IMB_interp.hh"
37
38#include "RE_texture.h"
39
40#include "BLI_hash_c.hh"
41
42namespace blender {
43
44#ifdef WITH_OCEANSIM
45
46/* Ocean code */
47
48static float nextfr(RNG *rng, float min, float max)
49{
50 return BLI_rng_get_float(rng) * (min - max) + max;
51}
52
53static float gaussRand(RNG *rng)
54{
55 /* NOTE: to avoid numerical problems with very small numbers, we make these variables
56 * single-precision floats, but later we call the double-precision log() and sqrt() functions
57 * instead of logf() and sqrtf(). */
58 float x;
59 float y;
60 float length2;
61
62 do {
63 x = nextfr(rng, -1, 1);
64 y = nextfr(rng, -1, 1);
65 length2 = x * x + y * y;
66 } while (length2 >= 1 || length2 == 0);
67
68 return x * sqrtf(-2.0f * logf(length2) / length2);
69}
70
74MINLINE float catrom(float p0, float p1, float p2, float p3, float f)
75{
76 return 0.5f * ((2.0f * p1) + (-p0 + p2) * f + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * f * f +
77 (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * f * f * f);
78}
79
80MINLINE float omega(float k, float depth)
81{
82 return sqrtf(GRAVITY * k * tanhf(k * depth));
83}
84
85/* modified Phillips spectrum */
86static float Ph(Ocean *o, float kx, float kz)
87{
88 float tmp;
89 float k2 = kx * kx + kz * kz;
90
91 if (k2 == 0.0f) {
92 return 0.0f; /* no DC component */
93 }
94
95 /* damp out the waves going in the direction opposite the wind */
96 tmp = (o->_wx * kx + o->_wz * kz) / sqrtf(k2);
97 if (tmp < 0) {
98 tmp *= o->_damp_reflections;
99 }
100
101 return o->_A * expf(-1.0f / (k2 * (o->_L * o->_L))) * expf(-k2 * (o->_l * o->_l)) *
102 powf(fabsf(tmp), o->_wind_alignment) / (k2 * k2);
103}
104
105static void compute_eigenstuff(OceanResult *ocr, float jxx, float jzz, float jxz)
106{
107 float a, b, qplus, qminus;
108 a = jxx + jzz;
109 b = sqrt((jxx - jzz) * (jxx - jzz) + 4 * jxz * jxz);
110
111 ocr->Jminus = 0.5f * (a - b);
112 ocr->Jplus = 0.5f * (a + b);
113
114 qplus = (ocr->Jplus - jxx) / jxz;
115 qminus = (ocr->Jminus - jxx) / jxz;
116
117 a = sqrt(1 + qplus * qplus);
118 b = sqrt(1 + qminus * qminus);
119
120 ocr->Eplus[0] = 1.0f / a;
121 ocr->Eplus[1] = 0.0f;
122 ocr->Eplus[2] = qplus / a;
123
124 ocr->Eminus[0] = 1.0f / b;
125 ocr->Eminus[1] = 0.0f;
126 ocr->Eminus[2] = qminus / b;
127}
128
129/*
130 * instead of Complex.h
131 * in fftw.h "fftw_complex" typedefed as double[2]
132 * below you can see functions are needed to work with such complex numbers.
133 */
134static void init_complex(fftw_complex cmpl, float real, float image)
135{
136 cmpl[0] = real;
137 cmpl[1] = image;
138}
139
140static void add_comlex_c(fftw_complex res, const fftw_complex cmpl1, const fftw_complex cmpl2)
141{
142 res[0] = cmpl1[0] + cmpl2[0];
143 res[1] = cmpl1[1] + cmpl2[1];
144}
145
146static void mul_complex_f(fftw_complex res, const fftw_complex cmpl, float f)
147{
148 res[0] = cmpl[0] * double(f);
149 res[1] = cmpl[1] * double(f);
150}
151
152static void mul_complex_c(fftw_complex res, const fftw_complex cmpl1, const fftw_complex cmpl2)
153{
154 fftwf_complex temp;
155 temp[0] = cmpl1[0] * cmpl2[0] - cmpl1[1] * cmpl2[1];
156 temp[1] = cmpl1[0] * cmpl2[1] + cmpl1[1] * cmpl2[0];
157 res[0] = temp[0];
158 res[1] = temp[1];
159}
160
161static float real_c(fftw_complex cmpl)
162{
163 return cmpl[0];
164}
165
166static float image_c(fftw_complex cmpl)
167{
168 return cmpl[1];
169}
170
171static void conj_complex(fftw_complex res, const fftw_complex cmpl1)
172{
173 res[0] = cmpl1[0];
174 res[1] = -cmpl1[1];
175}
176
177static void exp_complex(fftw_complex res, fftw_complex cmpl)
178{
179 float r = expf(cmpl[0]);
180
181 res[0] = cosf(cmpl[1]) * r;
182 res[1] = sinf(cmpl[1]) * r;
183}
184
185float BKE_ocean_jminus_to_foam(float jminus, float coverage)
186{
187 float foam = jminus * -0.005f + coverage;
188 CLAMP(foam, 0.0f, 1.0f);
189 return foam;
190}
191
192void BKE_ocean_eval_uv(Ocean *oc, OceanResult *ocr, float u, float v)
193{
194 int i0, i1, j0, j1;
195 float frac_x, frac_z;
196 float uu, vv;
197
198 /* first wrap the texture so 0 <= (u, v) < 1 */
199 u = fmodf(u, 1.0f);
200 v = fmodf(v, 1.0f);
201
202 if (u < 0) {
203 u += 1.0f;
204 }
205 if (v < 0) {
206 v += 1.0f;
207 }
208
209 BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
210
211 uu = u * oc->_M;
212 vv = v * oc->_N;
213
214 i0 = int(floor(uu));
215 j0 = int(floor(vv));
216
217 i1 = (i0 + 1);
218 j1 = (j0 + 1);
219
220 frac_x = uu - i0;
221 frac_z = vv - j0;
222
223 i0 = i0 % oc->_M;
224 j0 = j0 % oc->_N;
225
226 i1 = i1 % oc->_M;
227 j1 = j1 % oc->_N;
228
229# define BILERP(m) \
230 interpf(interpf(m[i1 * oc->_N + j1], m[i0 * oc->_N + j1], frac_x), \
231 interpf(m[i1 * oc->_N + j0], m[i0 * oc->_N + j0], frac_x), \
232 frac_z)
233
234 {
235 if (oc->_do_disp_y) {
236 ocr->disp[1] = BILERP(oc->_disp_y);
237 }
238
239 if (oc->_do_normals) {
240 ocr->normal[0] = BILERP(oc->_N_x);
241 ocr->normal[1] = oc->_N_y /* BILERP(oc->_N_y) (MEM01) */;
242 ocr->normal[2] = BILERP(oc->_N_z);
243 }
244
245 if (oc->_do_chop) {
246 ocr->disp[0] = BILERP(oc->_disp_x);
247 ocr->disp[2] = BILERP(oc->_disp_z);
248 }
249 else {
250 ocr->disp[0] = 0.0;
251 ocr->disp[2] = 0.0;
252 }
253
254 if (oc->_do_jacobian) {
255 compute_eigenstuff(ocr, BILERP(oc->_Jxx), BILERP(oc->_Jzz), BILERP(oc->_Jxz));
256 }
257 }
258# undef BILERP
259
260 BLI_rw_mutex_unlock(&oc->oceanmutex);
261}
262
263void BKE_ocean_eval_uv_catrom(Ocean *oc, OceanResult *ocr, float u, float v)
264{
265 int i0, i1, i2, i3, j0, j1, j2, j3;
266 float frac_x, frac_z;
267 float uu, vv;
268
269 /* first wrap the texture so 0 <= (u, v) < 1 */
270 u = fmod(u, 1.0f);
271 v = fmod(v, 1.0f);
272
273 if (u < 0) {
274 u += 1.0f;
275 }
276 if (v < 0) {
277 v += 1.0f;
278 }
279
280 BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
281
282 uu = u * oc->_M;
283 vv = v * oc->_N;
284
285 i1 = int(floor(uu));
286 j1 = int(floor(vv));
287
288 i2 = (i1 + 1);
289 j2 = (j1 + 1);
290
291 frac_x = uu - i1;
292 frac_z = vv - j1;
293
294 i1 = i1 % oc->_M;
295 j1 = j1 % oc->_N;
296
297 i2 = i2 % oc->_M;
298 j2 = j2 % oc->_N;
299
300 i0 = (i1 - 1);
301 i3 = (i2 + 1);
302 i0 = i0 < 0 ? i0 + oc->_M : i0;
303 i3 = i3 >= oc->_M ? i3 - oc->_M : i3;
304
305 j0 = (j1 - 1);
306 j3 = (j2 + 1);
307 j0 = j0 < 0 ? j0 + oc->_N : j0;
308 j3 = j3 >= oc->_N ? j3 - oc->_N : j3;
309
310# define INTERP(m) \
311 catrom(catrom(m[i0 * oc->_N + j0], \
312 m[i1 * oc->_N + j0], \
313 m[i2 * oc->_N + j0], \
314 m[i3 * oc->_N + j0], \
315 frac_x), \
316 catrom(m[i0 * oc->_N + j1], \
317 m[i1 * oc->_N + j1], \
318 m[i2 * oc->_N + j1], \
319 m[i3 * oc->_N + j1], \
320 frac_x), \
321 catrom(m[i0 * oc->_N + j2], \
322 m[i1 * oc->_N + j2], \
323 m[i2 * oc->_N + j2], \
324 m[i3 * oc->_N + j2], \
325 frac_x), \
326 catrom(m[i0 * oc->_N + j3], \
327 m[i1 * oc->_N + j3], \
328 m[i2 * oc->_N + j3], \
329 m[i3 * oc->_N + j3], \
330 frac_x), \
331 frac_z)
332
333 {
334 if (oc->_do_disp_y) {
335 ocr->disp[1] = INTERP(oc->_disp_y);
336 }
337 if (oc->_do_normals) {
338 ocr->normal[0] = INTERP(oc->_N_x);
339 ocr->normal[1] = oc->_N_y /* INTERP(oc->_N_y) (MEM01) */;
340 ocr->normal[2] = INTERP(oc->_N_z);
341 }
342 if (oc->_do_chop) {
343 ocr->disp[0] = INTERP(oc->_disp_x);
344 ocr->disp[2] = INTERP(oc->_disp_z);
345 }
346 else {
347 ocr->disp[0] = 0.0;
348 ocr->disp[2] = 0.0;
349 }
350
351 if (oc->_do_jacobian) {
352 compute_eigenstuff(ocr, INTERP(oc->_Jxx), INTERP(oc->_Jzz), INTERP(oc->_Jxz));
353 }
354 }
355# undef INTERP
356
357 BLI_rw_mutex_unlock(&oc->oceanmutex);
358}
359
360void BKE_ocean_eval_xz(Ocean *oc, OceanResult *ocr, float x, float z)
361{
362 BKE_ocean_eval_uv(oc, ocr, x / oc->_Lx, z / oc->_Lz);
363}
364
365void BKE_ocean_eval_xz_catrom(Ocean *oc, OceanResult *ocr, float x, float z)
366{
367 BKE_ocean_eval_uv_catrom(oc, ocr, x / oc->_Lx, z / oc->_Lz);
368}
369
370void BKE_ocean_eval_ij(Ocean *oc, OceanResult *ocr, int i, int j)
371{
372 BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
373
374 i = abs(i) % oc->_M;
375 j = abs(j) % oc->_N;
376
377 ocr->disp[1] = oc->_do_disp_y ? float(oc->_disp_y[i * oc->_N + j]) : 0.0f;
378
379 if (oc->_do_chop) {
380 ocr->disp[0] = oc->_disp_x[i * oc->_N + j];
381 ocr->disp[2] = oc->_disp_z[i * oc->_N + j];
382 }
383 else {
384 ocr->disp[0] = 0.0f;
385 ocr->disp[2] = 0.0f;
386 }
387
388 if (oc->_do_normals) {
389 ocr->normal[0] = oc->_N_x[i * oc->_N + j];
390 ocr->normal[1] = oc->_N_y /* oc->_N_y[i * oc->_N + j] (MEM01) */;
391 ocr->normal[2] = oc->_N_z[i * oc->_N + j];
392
393 normalize_v3(ocr->normal);
394 }
395
396 if (oc->_do_jacobian) {
397 compute_eigenstuff(
398 ocr, oc->_Jxx[i * oc->_N + j], oc->_Jzz[i * oc->_N + j], oc->_Jxz[i * oc->_N + j]);
399 }
400
401 BLI_rw_mutex_unlock(&oc->oceanmutex);
402}
403
404struct OceanSimulateData {
405 Ocean *o;
406 float t;
407 float scale;
408 float chop_amount;
409};
410
411static void ocean_compute_htilda(void *__restrict userdata,
412 const int i,
413 const TaskParallelTLS *__restrict /*tls*/)
414{
415 OceanSimulateData *osd = static_cast<OceanSimulateData *>(userdata);
416 const Ocean *o = osd->o;
417 const float scale = osd->scale;
418 const float t = osd->t;
419
420 int j;
421
422 /* Note the <= _N/2 here, see the FFTW documentation
423 * about the mechanics of the complex->real fft storage. */
424 for (j = 0; j <= o->_N / 2; j++) {
425 fftw_complex exp_param1;
426 fftw_complex exp_param2;
427 fftw_complex conj_param;
428
429 init_complex(exp_param1, 0.0, omega(o->_k[i * (1 + o->_N / 2) + j], o->_depth) * t);
430 init_complex(exp_param2, 0.0, -omega(o->_k[i * (1 + o->_N / 2) + j], o->_depth) * t);
431 exp_complex(exp_param1, exp_param1);
432 exp_complex(exp_param2, exp_param2);
433 conj_complex(conj_param, o->_h0_minus[i * o->_N + j]);
434
435 mul_complex_c(exp_param1, o->_h0[i * o->_N + j], exp_param1);
436 mul_complex_c(exp_param2, conj_param, exp_param2);
437
438 add_comlex_c(o->_htilda[i * (1 + o->_N / 2) + j], exp_param1, exp_param2);
439 mul_complex_f(o->_fft_in[i * (1 + o->_N / 2) + j], o->_htilda[i * (1 + o->_N / 2) + j], scale);
440 }
441}
442
443static void ocean_compute_displacement_y(TaskPool *__restrict pool, void * /*taskdata*/)
444{
445 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
446 const Ocean *o = osd->o;
447
448 fftw_execute(o->_disp_y_plan);
449}
450
451static void ocean_compute_displacement_x(TaskPool *__restrict pool, void * /*taskdata*/)
452{
453 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
454 const Ocean *o = osd->o;
455 const float scale = osd->scale;
456 const float chop_amount = osd->chop_amount;
457 int i, j;
458
459 for (i = 0; i < o->_M; i++) {
460 for (j = 0; j <= o->_N / 2; j++) {
461 fftw_complex mul_param;
462 fftw_complex minus_i;
463
464 init_complex(minus_i, 0.0, -1.0);
465 init_complex(mul_param, -scale, 0);
466 mul_complex_f(mul_param, mul_param, chop_amount);
467 mul_complex_c(mul_param, mul_param, minus_i);
468 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
469 mul_complex_f(mul_param,
470 mul_param,
471 ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
472 0.0f :
473 o->_kx[i] / o->_k[i * (1 + o->_N / 2) + j]));
474 init_complex(o->_fft_in_x[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
475 }
476 }
477 fftw_execute(o->_disp_x_plan);
478}
479
480static void ocean_compute_displacement_z(TaskPool *__restrict pool, void * /*taskdata*/)
481{
482 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
483 const Ocean *o = osd->o;
484 const float scale = osd->scale;
485 const float chop_amount = osd->chop_amount;
486 int i, j;
487
488 for (i = 0; i < o->_M; i++) {
489 for (j = 0; j <= o->_N / 2; j++) {
490 fftw_complex mul_param;
491 fftw_complex minus_i;
492
493 init_complex(minus_i, 0.0, -1.0);
494 init_complex(mul_param, -scale, 0);
495 mul_complex_f(mul_param, mul_param, chop_amount);
496 mul_complex_c(mul_param, mul_param, minus_i);
497 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
498 mul_complex_f(mul_param,
499 mul_param,
500 ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
501 0.0f :
502 o->_kz[j] / o->_k[i * (1 + o->_N / 2) + j]));
503 init_complex(o->_fft_in_z[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
504 }
505 }
506 fftw_execute(o->_disp_z_plan);
507}
508
509static void ocean_compute_jacobian_jxx(TaskPool *__restrict pool, void * /*taskdata*/)
510{
511 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
512 const Ocean *o = osd->o;
513 const float chop_amount = osd->chop_amount;
514 int i, j;
515
516 for (i = 0; i < o->_M; i++) {
517 for (j = 0; j <= o->_N / 2; j++) {
518 fftw_complex mul_param;
519
520 // init_complex(mul_param, -scale, 0);
521 init_complex(mul_param, -1, 0);
522
523 mul_complex_f(mul_param, mul_param, chop_amount);
524 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
525 mul_complex_f(mul_param,
526 mul_param,
527 ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
528 0.0f :
529 o->_kx[i] * o->_kx[i] / o->_k[i * (1 + o->_N / 2) + j]));
530 init_complex(o->_fft_in_jxx[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
531 }
532 }
533 fftw_execute(o->_Jxx_plan);
534
535 for (i = 0; i < o->_M; i++) {
536 for (j = 0; j < o->_N; j++) {
537 o->_Jxx[i * o->_N + j] += 1.0;
538 }
539 }
540}
541
542static void ocean_compute_jacobian_jzz(TaskPool *__restrict pool, void * /*taskdata*/)
543{
544 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
545 const Ocean *o = osd->o;
546 const float chop_amount = osd->chop_amount;
547 int i, j;
548
549 for (i = 0; i < o->_M; i++) {
550 for (j = 0; j <= o->_N / 2; j++) {
551 fftw_complex mul_param;
552
553 // init_complex(mul_param, -scale, 0);
554 init_complex(mul_param, -1, 0);
555
556 mul_complex_f(mul_param, mul_param, chop_amount);
557 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
558 mul_complex_f(mul_param,
559 mul_param,
560 ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
561 0.0f :
562 o->_kz[j] * o->_kz[j] / o->_k[i * (1 + o->_N / 2) + j]));
563 init_complex(o->_fft_in_jzz[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
564 }
565 }
566 fftw_execute(o->_Jzz_plan);
567
568 for (i = 0; i < o->_M; i++) {
569 for (j = 0; j < o->_N; j++) {
570 o->_Jzz[i * o->_N + j] += 1.0;
571 }
572 }
573}
574
575static void ocean_compute_jacobian_jxz(TaskPool *__restrict pool, void * /*taskdata*/)
576{
577 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
578 const Ocean *o = osd->o;
579 const float chop_amount = osd->chop_amount;
580 int i, j;
581
582 for (i = 0; i < o->_M; i++) {
583 for (j = 0; j <= o->_N / 2; j++) {
584 fftw_complex mul_param;
585
586 // init_complex(mul_param, -scale, 0);
587 init_complex(mul_param, -1, 0);
588
589 mul_complex_f(mul_param, mul_param, chop_amount);
590 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
591 mul_complex_f(mul_param,
592 mul_param,
593 ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
594 0.0f :
595 o->_kx[i] * o->_kz[j] / o->_k[i * (1 + o->_N / 2) + j]));
596 init_complex(o->_fft_in_jxz[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
597 }
598 }
599 fftw_execute(o->_Jxz_plan);
600}
601
602static void ocean_compute_normal_x(TaskPool *__restrict pool, void * /*taskdata*/)
603{
604 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
605 const Ocean *o = osd->o;
606 int i, j;
607
608 for (i = 0; i < o->_M; i++) {
609 for (j = 0; j <= o->_N / 2; j++) {
610 fftw_complex mul_param;
611
612 init_complex(mul_param, 0.0, -1.0);
613 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
614 mul_complex_f(mul_param, mul_param, o->_kx[i]);
615 init_complex(o->_fft_in_nx[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
616 }
617 }
618 fftw_execute(o->_N_x_plan);
619}
620
621static void ocean_compute_normal_z(TaskPool *__restrict pool, void * /*taskdata*/)
622{
623 OceanSimulateData *osd = static_cast<OceanSimulateData *>(BLI_task_pool_user_data(pool));
624 const Ocean *o = osd->o;
625 int i, j;
626
627 for (i = 0; i < o->_M; i++) {
628 for (j = 0; j <= o->_N / 2; j++) {
629 fftw_complex mul_param;
630
631 init_complex(mul_param, 0.0, -1.0);
632 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
633 mul_complex_f(mul_param, mul_param, o->_kz[i]);
634 init_complex(o->_fft_in_nz[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
635 }
636 }
637 fftw_execute(o->_N_z_plan);
638}
639
640bool BKE_ocean_is_valid(const Ocean *o)
641{
642 return o->_k != nullptr;
643}
644
645void BKE_ocean_simulate(Ocean *o, float t, float scale, float chop_amount)
646{
647 TaskPool *pool;
648
649 OceanSimulateData osd;
650
651 scale *= o->normalize_factor;
652
653 osd.o = o;
654 osd.t = t;
655 osd.scale = scale;
656 osd.chop_amount = chop_amount;
657
659
660 BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE);
661
662 /* Note about multi-threading here: we have to run a first set of computations (htilda one)
663 * before we can run all others, since they all depend on it.
664 * So we make a first parallelized forloop run for htilda,
665 * and then pack all other computations into a set of parallel tasks.
666 * This is not optimal in all cases,
667 * but remains reasonably simple and should be OK most of the time. */
668
669 /* compute a new htilda */
670 TaskParallelSettings settings;
672 settings.use_threading = (o->_M > 16);
673 BLI_task_parallel_range(0, o->_M, &osd, ocean_compute_htilda, &settings);
674
675 if (o->_do_disp_y) {
676 BLI_task_pool_push(pool, ocean_compute_displacement_y, nullptr, false, nullptr);
677 }
678
679 if (o->_do_chop) {
680 BLI_task_pool_push(pool, ocean_compute_displacement_x, nullptr, false, nullptr);
681 BLI_task_pool_push(pool, ocean_compute_displacement_z, nullptr, false, nullptr);
682 }
683
684 if (o->_do_jacobian) {
685 BLI_task_pool_push(pool, ocean_compute_jacobian_jxx, nullptr, false, nullptr);
686 BLI_task_pool_push(pool, ocean_compute_jacobian_jzz, nullptr, false, nullptr);
687 BLI_task_pool_push(pool, ocean_compute_jacobian_jxz, nullptr, false, nullptr);
688 }
689
690 if (o->_do_normals) {
691 BLI_task_pool_push(pool, ocean_compute_normal_x, nullptr, false, nullptr);
692 BLI_task_pool_push(pool, ocean_compute_normal_z, nullptr, false, nullptr);
693 o->_N_y = 1.0f / scale;
694 }
695
697
698 BLI_rw_mutex_unlock(&o->oceanmutex);
699
700 BLI_task_pool_free(pool);
701}
702
703static void set_height_normalize_factor(Ocean *oc)
704{
705 float res = 1.0;
706 float max_h = 0.0;
707
708 int i, j;
709
710 if (!oc->_do_disp_y) {
711 return;
712 }
713
714 oc->normalize_factor = 1.0;
715
716 BKE_ocean_simulate(oc, 0.0, 1.0, 0);
717
718 BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
719
720 for (i = 0; i < oc->_M; i++) {
721 for (j = 0; j < oc->_N; j++) {
722 max_h = std::max<double>(max_h, fabs(oc->_disp_y[i * oc->_N + j]));
723 }
724 }
725
726 BLI_rw_mutex_unlock(&oc->oceanmutex);
727
728 if (max_h == 0.0f) {
729 max_h = 0.00001f; /* just in case ... */
730 }
731
732 res = 1.0f / (max_h);
733
734 oc->normalize_factor = res;
735}
736
738{
739 Ocean *oc = MEM_new_zeroed<Ocean>("ocean sim data");
740
741 BLI_rw_mutex_init(&oc->oceanmutex);
742
743 return oc;
744}
745
746bool BKE_ocean_ensure(OceanModifierData *omd, const int resolution)
747{
748 if (omd->ocean) {
749 /* Check that the ocean has the same resolution than we want now. */
750 if (omd->ocean->_M == resolution * resolution) {
751 return false;
752 }
753
754 BKE_ocean_free(omd->ocean);
755 }
756
757 omd->ocean = BKE_ocean_add();
758 BKE_ocean_init_from_modifier(omd->ocean, omd, resolution);
759 return true;
760}
761
762bool BKE_ocean_init_from_modifier(Ocean *ocean, OceanModifierData const *omd, const int resolution)
763{
764 short do_heightfield, do_chop, do_normals, do_jacobian, do_spray;
765
766 do_heightfield = true;
767 do_chop = (omd->chop_amount > 0);
768 do_normals = (omd->flag & MOD_OCEAN_GENERATE_NORMALS);
769 do_jacobian = (omd->flag & MOD_OCEAN_GENERATE_FOAM);
770 do_spray = do_jacobian && (omd->flag & MOD_OCEAN_GENERATE_SPRAY);
771
772 BKE_ocean_free_data(ocean);
773
774 return BKE_ocean_init(ocean,
775 resolution * resolution,
776 resolution * resolution,
777 omd->spatial_size,
778 omd->spatial_size,
779 omd->wind_velocity,
780 omd->smallest_wave,
781 1.0,
782 omd->wave_direction,
783 omd->damp,
784 omd->wave_alignment,
785 omd->depth,
786 omd->time,
787 omd->spectrum,
788 omd->fetch_jonswap,
789 omd->sharpen_peak_jonswap,
790 do_heightfield,
791 do_chop,
792 do_spray,
793 do_normals,
794 do_jacobian,
795 omd->seed);
796}
797
798bool BKE_ocean_init(Ocean *o,
799 int M,
800 int N,
801 float Lx,
802 float Lz,
803 float V,
804 float l,
805 float A,
806 float w,
807 float damp,
808 float alignment,
809 float depth,
810 float time,
811 int spectrum,
812 float fetch_jonswap,
813 float sharpen_peak_jonswap,
814 short do_height_field,
815 short do_chop,
816 short do_spray,
817 short do_normals,
818 short do_jacobian,
819 int seed)
820{
821 int i, j, ii;
822
823 BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE);
824
825 o->_M = M;
826 o->_N = N;
827 o->_V = V;
828 o->_l = l;
829 o->_A = A;
830 o->_w = w;
831 o->_damp_reflections = 1.0f - damp;
832 o->_wind_alignment = alignment * 10.0f;
833 o->_depth = depth;
834 o->_Lx = Lx;
835 o->_Lz = Lz;
836 o->_wx = cos(w);
837 o->_wz = -sin(w); /* wave direction */
838 o->_L = V * V / GRAVITY; /* largest wave for a given velocity V */
839 o->time = time;
840
841 /* Spectrum to use. */
842 o->_spectrum = spectrum;
843
844 /* Common JONSWAP parameters. */
845 o->_fetch_jonswap = fetch_jonswap;
846 o->_sharpen_peak_jonswap = sharpen_peak_jonswap * 10.0f;
847
848 /* NOTE: most modifiers don't account for failure to allocate.
849 * In this case however a large resolution can easily perform large allocations that fail,
850 * support early exiting in this case. */
851 if ((o->_k = MEM_new_array_uninitialized<float>(size_t(M) * (1 + size_t(N) / 2), "ocean_k")) &&
852 (o->_h0 = MEM_new_array_uninitialized<fftw_complex>(size_t(M) * size_t(N), "ocean_h0")) &&
853 (o->_h0_minus = MEM_new_array_uninitialized<fftw_complex>(size_t(M) * size_t(N),
854 "ocean_h0_minus")) &&
855 (o->_kx = MEM_new_array_uninitialized<float>(size_t(o->_M), "ocean_kx")) &&
856 (o->_kz = MEM_new_array_uninitialized<float>(size_t(o->_N), "ocean_kz")))
857 {
858 /* Success. */
859 }
860 else {
861 MEM_SAFE_DELETE(o->_k);
862 MEM_SAFE_DELETE(o->_h0);
863 MEM_SAFE_DELETE(o->_h0_minus);
864 MEM_SAFE_DELETE(o->_kx);
865 MEM_SAFE_DELETE(o->_kz);
866
867 BLI_rw_mutex_unlock(&o->oceanmutex);
868 return false;
869 }
870
871 o->_do_disp_y = do_height_field;
872 o->_do_normals = do_normals;
873 o->_do_spray = do_spray;
874 o->_do_chop = do_chop;
875 o->_do_jacobian = do_jacobian;
876
877 /* make this robust in the face of erroneous usage */
878 if (o->_Lx == 0.0f) {
879 o->_Lx = 0.001f;
880 }
881
882 if (o->_Lz == 0.0f) {
883 o->_Lz = 0.001f;
884 }
885
886 /* The +VE components and DC. */
887 for (i = 0; i <= o->_M / 2; i++) {
888 o->_kx[i] = 2.0f * float(M_PI) * i / o->_Lx;
889 }
890
891 /* The -VE components. */
892 for (i = o->_M - 1, ii = 0; i > o->_M / 2; i--, ii++) {
893 o->_kx[i] = -2.0f * float(M_PI) * ii / o->_Lx;
894 }
895
896 /* The +VE components and DC. */
897 for (i = 0; i <= o->_N / 2; i++) {
898 o->_kz[i] = 2.0f * float(M_PI) * i / o->_Lz;
899 }
900
901 /* The -VE components. */
902 for (i = o->_N - 1, ii = 0; i > o->_N / 2; i--, ii++) {
903 o->_kz[i] = -2.0f * float(M_PI) * ii / o->_Lz;
904 }
905
906 /* pre-calculate the k matrix */
907 for (i = 0; i < o->_M; i++) {
908 for (j = 0; j <= o->_N / 2; j++) {
909 o->_k[size_t(i) * (1 + o->_N / 2) + j] = sqrt(o->_kx[i] * o->_kx[i] + o->_kz[j] * o->_kz[j]);
910 }
911 }
912
913 RNG *rng = BLI_rng_new(seed);
914
915 for (i = 0; i < o->_M; i++) {
916 for (j = 0; j < o->_N; j++) {
917 /* This ensures we get a value tied to the surface location, avoiding dramatic surface
918 * change with changing resolution.
919 * Explicitly cast to signed int first to ensure consistent behavior on all processors,
920 * since behavior of `float` to `uint` cast is undefined in C. */
921 const int hash_x = o->_kx[i] * 360.0f;
922 const int hash_z = o->_kz[j] * 360.0f;
923 int new_seed = seed + BLI_hash_int_2d(hash_x, hash_z);
924
925 BLI_rng_seed(rng, new_seed);
926 float r1 = gaussRand(rng);
927 float r2 = gaussRand(rng);
928
929 fftw_complex r1r2;
930 init_complex(r1r2, r1, r2);
931 switch (o->_spectrum) {
933 mul_complex_f(o->_h0[i * o->_N + j],
934 r1r2,
935 sqrt(BLI_ocean_spectrum_jonswap(o, o->_kx[i], o->_kz[j]) / 2.0f));
936 mul_complex_f(o->_h0_minus[i * o->_N + j],
937 r1r2,
938 sqrt(BLI_ocean_spectrum_jonswap(o, -o->_kx[i], -o->_kz[j]) / 2.0f));
939 break;
941 mul_complex_f(
942 o->_h0[i * o->_N + j],
943 r1r2,
944 sqrt(BLI_ocean_spectrum_texelmarsenarsloe(o, o->_kx[i], o->_kz[j]) / 2.0f));
945 mul_complex_f(
946 o->_h0_minus[i * o->_N + j],
947 r1r2,
948 sqrt(BLI_ocean_spectrum_texelmarsenarsloe(o, -o->_kx[i], -o->_kz[j]) / 2.0f));
949 break;
951 mul_complex_f(o->_h0[i * o->_N + j],
952 r1r2,
953 sqrt(BLI_ocean_spectrum_piersonmoskowitz(o, o->_kx[i], o->_kz[j]) / 2.0f));
954 mul_complex_f(
955 o->_h0_minus[i * o->_N + j],
956 r1r2,
957 sqrt(BLI_ocean_spectrum_piersonmoskowitz(o, -o->_kx[i], -o->_kz[j]) / 2.0f));
958 break;
959 default:
960 mul_complex_f(o->_h0[i * o->_N + j], r1r2, sqrt(Ph(o, o->_kx[i], o->_kz[j]) / 2.0f));
961 mul_complex_f(
962 o->_h0_minus[i * o->_N + j], r1r2, sqrt(Ph(o, -o->_kx[i], -o->_kz[j]) / 2.0f));
963 break;
964 }
965 }
966 }
967
968 o->_fft_in = MEM_new_array_uninitialized<fftw_complex>(size_t(o->_M) * (1 + size_t(o->_N) / 2),
969 "ocean_fft_in");
970 o->_htilda = MEM_new_array_uninitialized<fftw_complex>(size_t(o->_M) * (1 + size_t(o->_N) / 2),
971 "ocean_htilda");
972
974
975 if (o->_do_disp_y) {
976 o->_disp_y = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N),
977 "ocean_disp_y");
978 o->_disp_y_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in, o->_disp_y, FFTW_ESTIMATE);
979 }
980
981 if (o->_do_normals) {
983 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_nx");
985 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_nz");
986
987 o->_N_x = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N), "ocean_N_x");
988 // o->_N_y = (float *) fftwf_malloc(o->_M * o->_N * sizeof(float)); /* (MEM01) */
989 o->_N_z = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N), "ocean_N_z");
990
991 o->_N_x_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_nx, o->_N_x, FFTW_ESTIMATE);
992 o->_N_z_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_nz, o->_N_z, FFTW_ESTIMATE);
993 }
994
995 if (o->_do_chop) {
997 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_x");
999 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_z");
1000
1001 o->_disp_x = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N),
1002 "ocean_disp_x");
1003 o->_disp_z = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N),
1004 "ocean_disp_z");
1005
1006 o->_disp_x_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_x, o->_disp_x, FFTW_ESTIMATE);
1007 o->_disp_z_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_z, o->_disp_z, FFTW_ESTIMATE);
1008 }
1009 if (o->_do_jacobian) {
1011 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_jxx");
1013 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_jzz");
1015 size_t(o->_M) * (1 + size_t(o->_N) / 2), "ocean_fft_in_jxz");
1016
1017 o->_Jxx = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N), "ocean_Jxx");
1018 o->_Jzz = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N), "ocean_Jzz");
1019 o->_Jxz = MEM_new_array_uninitialized<double>(size_t(o->_M) * size_t(o->_N), "ocean_Jxz");
1020
1021 o->_Jxx_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jxx, o->_Jxx, FFTW_ESTIMATE);
1022 o->_Jzz_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jzz, o->_Jzz, FFTW_ESTIMATE);
1023 o->_Jxz_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jxz, o->_Jxz, FFTW_ESTIMATE);
1024 }
1025
1027
1028 BLI_rw_mutex_unlock(&o->oceanmutex);
1029
1030 set_height_normalize_factor(o);
1031
1032 BLI_rng_free(rng);
1033
1034 return true;
1035}
1036
1037void BKE_ocean_free_data(Ocean *oc)
1038{
1039 if (!oc) {
1040 return;
1041 }
1042
1043 BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_WRITE);
1044
1046
1047 if (oc->_do_disp_y) {
1048 fftw_destroy_plan(oc->_disp_y_plan);
1049 MEM_delete(oc->_disp_y);
1050 }
1051
1052 if (oc->_do_normals) {
1053 MEM_delete(oc->_fft_in_nx);
1054 MEM_delete(oc->_fft_in_nz);
1055 fftw_destroy_plan(oc->_N_x_plan);
1056 fftw_destroy_plan(oc->_N_z_plan);
1057 MEM_delete(oc->_N_x);
1058 // fftwf_free(oc->_N_y); /* (MEM01) */
1059 MEM_delete(oc->_N_z);
1060 }
1061
1062 if (oc->_do_chop) {
1063 MEM_delete(oc->_fft_in_x);
1064 MEM_delete(oc->_fft_in_z);
1065 fftw_destroy_plan(oc->_disp_x_plan);
1066 fftw_destroy_plan(oc->_disp_z_plan);
1067 MEM_delete(oc->_disp_x);
1068 MEM_delete(oc->_disp_z);
1069 }
1070
1071 if (oc->_do_jacobian) {
1072 MEM_delete(oc->_fft_in_jxx);
1073 MEM_delete(oc->_fft_in_jzz);
1074 MEM_delete(oc->_fft_in_jxz);
1075 fftw_destroy_plan(oc->_Jxx_plan);
1076 fftw_destroy_plan(oc->_Jzz_plan);
1077 fftw_destroy_plan(oc->_Jxz_plan);
1078 MEM_delete(oc->_Jxx);
1079 MEM_delete(oc->_Jzz);
1080 MEM_delete(oc->_Jxz);
1081 }
1082
1084
1085 if (oc->_fft_in) {
1086 MEM_delete(oc->_fft_in);
1087 }
1088
1089 /* check that ocean data has been initialized */
1090 if (oc->_htilda) {
1091 MEM_delete(oc->_htilda);
1092 MEM_delete(oc->_k);
1093 MEM_delete(oc->_h0);
1094 MEM_delete(oc->_h0_minus);
1095 MEM_delete(oc->_kx);
1096 MEM_delete(oc->_kz);
1097 }
1098
1099 BLI_rw_mutex_unlock(&oc->oceanmutex);
1100}
1101
1102void BKE_ocean_free(Ocean *oc)
1103{
1104 if (!oc) {
1105 return;
1106 }
1107
1109 BLI_rw_mutex_end(&oc->oceanmutex);
1110
1111 MEM_delete(oc);
1112}
1113
1114# undef GRAVITY
1115
1116/* ********* Baking/Caching ********* */
1117
1118# define CACHE_TYPE_DISPLACE 1
1119# define CACHE_TYPE_FOAM 2
1120# define CACHE_TYPE_NORMAL 3
1121# define CACHE_TYPE_SPRAY 4
1122# define CACHE_TYPE_SPRAY_INVERSE 5
1123
1124static void cache_filepath(
1125 char *filepath, const char *dirname, const char *relbase, int frame, int type)
1126{
1127 char cachepath[FILE_MAX];
1128 const char *filename;
1129
1130 switch (type) {
1131 case CACHE_TYPE_FOAM:
1132 filename = "foam_";
1133 break;
1134 case CACHE_TYPE_NORMAL:
1135 filename = "normal_";
1136 break;
1137 case CACHE_TYPE_SPRAY:
1138 filename = "spray_";
1139 break;
1140 case CACHE_TYPE_SPRAY_INVERSE:
1141 filename = "spray_inverse_";
1142 break;
1143 case CACHE_TYPE_DISPLACE:
1144 default:
1145 filename = "disp_";
1146 break;
1147 }
1148
1149 BLI_path_join(cachepath, sizeof(cachepath), dirname, filename);
1150
1152 filepath, cachepath, relbase, nullptr, frame, R_IMF_IMTYPE_OPENEXR, true, true, "");
1153 BLI_assert_msg(errors.is_empty(),
1154 "Path parsing errors should only occur when a variable map is provided.");
1155 UNUSED_VARS_NDEBUG(errors);
1156}
1157
1158/* silly functions but useful to inline when the args do a lot of indirections */
1159MINLINE void rgb_to_rgba_unit_alpha(float r_rgba[4], const float rgb[3])
1160{
1161 r_rgba[0] = rgb[0];
1162 r_rgba[1] = rgb[1];
1163 r_rgba[2] = rgb[2];
1164 r_rgba[3] = 1.0f;
1165}
1166MINLINE void value_to_rgba_unit_alpha(float r_rgba[4], const float value)
1167{
1168 r_rgba[0] = value;
1169 r_rgba[1] = value;
1170 r_rgba[2] = value;
1171 r_rgba[3] = 1.0f;
1172}
1173
1175{
1176 int i, f = 0;
1177
1178 if (!och) {
1179 return;
1180 }
1181
1182 if (och->ibufs_disp) {
1183 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1184 if (och->ibufs_disp[f]) {
1185 IMB_freeImBuf(och->ibufs_disp[f]);
1186 }
1187 }
1188 MEM_delete(och->ibufs_disp);
1189 }
1190
1191 if (och->ibufs_foam) {
1192 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1193 if (och->ibufs_foam[f]) {
1194 IMB_freeImBuf(och->ibufs_foam[f]);
1195 }
1196 }
1197 MEM_delete(och->ibufs_foam);
1198 }
1199
1200 if (och->ibufs_spray) {
1201 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1202 if (och->ibufs_spray[f]) {
1203 IMB_freeImBuf(och->ibufs_spray[f]);
1204 }
1205 }
1206 MEM_delete(och->ibufs_spray);
1207 }
1208
1209 if (och->ibufs_spray_inverse) {
1210 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1211 if (och->ibufs_spray_inverse[f]) {
1212 IMB_freeImBuf(och->ibufs_spray_inverse[f]);
1213 }
1214 }
1215 MEM_delete(och->ibufs_spray_inverse);
1216 }
1217
1218 if (och->ibufs_norm) {
1219 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1220 if (och->ibufs_norm[f]) {
1221 IMB_freeImBuf(och->ibufs_norm[f]);
1222 }
1223 }
1224 MEM_delete(och->ibufs_norm);
1225 }
1226
1227 if (och->time) {
1228 MEM_delete(och->time);
1229 }
1230 MEM_delete(och);
1231}
1232
1233void BKE_ocean_cache_eval_uv(OceanCache *och, OceanResult *ocr, int f, float u, float v)
1234{
1235 int res_x = och->resolution_x;
1236 int res_y = och->resolution_y;
1237 float result[4];
1238
1239 u = fmod(u, 1.0);
1240 v = fmod(v, 1.0);
1241
1242 if (u < 0) {
1243 u += 1.0f;
1244 }
1245 if (v < 0) {
1246 v += 1.0f;
1247 }
1248
1249 const float texel_x = u * res_x - 0.5f;
1250 const float texel_y = v * res_y - 0.5f;
1251 if (och->ibufs_disp[f]) {
1252 imbuf::interpolate_bilinear_fl(och->ibufs_disp[f], result, texel_x, texel_y);
1253 copy_v3_v3(ocr->disp, result);
1254 }
1255
1256 if (och->ibufs_foam[f]) {
1257 imbuf::interpolate_bilinear_fl(och->ibufs_foam[f], result, texel_x, texel_y);
1258 ocr->foam = result[0];
1259 }
1260
1261 if (och->ibufs_spray[f]) {
1262 imbuf::interpolate_bilinear_fl(och->ibufs_spray[f], result, texel_x, texel_y);
1263 copy_v3_v3(ocr->Eplus, result);
1264 }
1265
1266 if (och->ibufs_spray_inverse[f]) {
1267 imbuf::interpolate_bilinear_fl(och->ibufs_spray_inverse[f], result, texel_x, texel_y);
1268 copy_v3_v3(ocr->Eminus, result);
1269 }
1270
1271 if (och->ibufs_norm[f]) {
1272 imbuf::interpolate_bilinear_fl(och->ibufs_norm[f], result, texel_x, texel_y);
1273 copy_v3_v3(ocr->normal, result);
1274 }
1275}
1276
1277void BKE_ocean_cache_eval_ij(OceanCache *och, OceanResult *ocr, int f, int i, int j)
1278{
1279 const int res_x = och->resolution_x;
1280 const int res_y = och->resolution_y;
1281
1282 if (i < 0) {
1283 i = -i;
1284 }
1285 if (j < 0) {
1286 j = -j;
1287 }
1288
1289 i = i % res_x;
1290 j = j % res_y;
1291
1292 if (och->ibufs_disp[f]) {
1293 copy_v3_v3(ocr->disp, &och->ibufs_disp[f]->float_data()[4 * (res_x * j + i)]);
1294 }
1295
1296 if (och->ibufs_foam[f]) {
1297 ocr->foam = och->ibufs_foam[f]->float_data()[4 * (res_x * j + i)];
1298 }
1299
1300 if (och->ibufs_spray[f]) {
1301 copy_v3_v3(ocr->Eplus, &och->ibufs_spray[f]->float_data()[4 * (res_x * j + i)]);
1302 }
1303
1304 if (och->ibufs_spray_inverse[f]) {
1305 copy_v3_v3(ocr->Eminus, &och->ibufs_spray_inverse[f]->float_data()[4 * (res_x * j + i)]);
1306 }
1307
1308 if (och->ibufs_norm[f]) {
1309 copy_v3_v3(ocr->normal, &och->ibufs_norm[f]->float_data()[4 * (res_x * j + i)]);
1310 }
1311}
1312
1313OceanCache *BKE_ocean_init_cache(const char *bakepath,
1314 const char *relbase,
1315 int start,
1316 int end,
1317 float wave_scale,
1318 float chop_amount,
1319 float foam_coverage,
1320 float foam_fade,
1321 int resolution)
1322{
1323 OceanCache *och = MEM_new_zeroed<OceanCache>("ocean cache data");
1324
1325 och->bakepath = bakepath;
1326 och->relbase = relbase;
1327
1328 och->start = start;
1329 och->end = end;
1330 och->duration = (end - start) + 1;
1331 och->wave_scale = wave_scale;
1332 och->chop_amount = chop_amount;
1333 och->foam_coverage = foam_coverage;
1334 och->foam_fade = foam_fade;
1335 och->resolution_x = resolution * resolution;
1336 och->resolution_y = resolution * resolution;
1337
1338 och->ibufs_disp = MEM_new_array_zeroed<ImBuf *>(och->duration,
1339 "displacement imbuf pointer array");
1340 och->ibufs_foam = MEM_new_array_zeroed<ImBuf *>(och->duration, "foam imbuf pointer array");
1341 och->ibufs_spray = MEM_new_array_zeroed<ImBuf *>(och->duration, "spray imbuf pointer array");
1342 och->ibufs_spray_inverse = MEM_new_array_zeroed<ImBuf *>(och->duration,
1343 "spray_inverse imbuf pointer array");
1344 och->ibufs_norm = MEM_new_array_zeroed<ImBuf *>(och->duration, "normal imbuf pointer array");
1345
1346 och->time = nullptr;
1347
1348 return och;
1349}
1350
1351void BKE_ocean_simulate_cache(OceanCache *och, int frame)
1352{
1353 char filepath[FILE_MAX];
1354
1355 /* ibufs array is zero based, but filenames are based on frame numbers */
1356 /* still need to clamp frame numbers to valid range of images on disk though */
1357 CLAMP(frame, och->start, och->end);
1358 const int f = frame - och->start; /* shift to 0 based */
1359
1360 /* if image is already loaded in mem, return */
1361 if (och->ibufs_disp[f] != nullptr) {
1362 return;
1363 }
1364
1365 /* Use default color spaces since we know for sure cache
1366 * files were saved with default settings too. */
1367
1368 cache_filepath(filepath, och->bakepath, och->relbase, frame, CACHE_TYPE_DISPLACE);
1369 och->ibufs_disp[f] = IMB_load_image_from_filepath(filepath, ImBufFlags::Zero);
1370
1371 cache_filepath(filepath, och->bakepath, och->relbase, frame, CACHE_TYPE_FOAM);
1372 och->ibufs_foam[f] = IMB_load_image_from_filepath(filepath, ImBufFlags::Zero);
1373
1374 cache_filepath(filepath, och->bakepath, och->relbase, frame, CACHE_TYPE_SPRAY);
1375 och->ibufs_spray[f] = IMB_load_image_from_filepath(filepath, ImBufFlags::Zero);
1376
1377 cache_filepath(filepath, och->bakepath, och->relbase, frame, CACHE_TYPE_SPRAY_INVERSE);
1378 och->ibufs_spray_inverse[f] = IMB_load_image_from_filepath(filepath, ImBufFlags::Zero);
1379
1380 cache_filepath(filepath, och->bakepath, och->relbase, frame, CACHE_TYPE_NORMAL);
1381 och->ibufs_norm[f] = IMB_load_image_from_filepath(filepath, ImBufFlags::Zero);
1382}
1383
1384void BKE_ocean_bake(Ocean *o,
1385 OceanCache *och,
1386 void (*update_cb)(void *, float progress, int *cancel),
1387 void *update_cb_data)
1388{
1389 /* NOTE(@ideasman42): some of these values remain uninitialized unless certain options
1390 * are enabled, take care that #BKE_ocean_eval_ij() initializes a member before use. */
1391 OceanResult ocr;
1392
1393 ImageFormatData imf = {};
1394
1395 int f, i = 0, x, y, cancel = 0;
1396 float progress;
1397
1398 ImBuf *ibuf_foam, *ibuf_disp, *ibuf_normal, *ibuf_spray, *ibuf_spray_inverse;
1399 float *prev_foam;
1400 int res_x = och->resolution_x;
1401 int res_y = och->resolution_y;
1402 char filepath[FILE_MAX];
1403 // RNG *rng;
1404
1405 if (!o) {
1406 return;
1407 }
1408
1409 if (o->_do_jacobian) {
1410 prev_foam = MEM_new_array_zeroed<float>(size_t(res_x) * size_t(res_y),
1411 "previous frame foam bake data");
1412 }
1413 else {
1414 prev_foam = nullptr;
1415 }
1416
1417 // rng = BLI_rng_new(0);
1418
1419 /* setup image format */
1420 imf.imtype = R_IMF_IMTYPE_OPENEXR;
1421 imf.depth = R_IMF_CHAN_DEPTH_16;
1422 imf.exr_codec = R_IMF_EXR_CODEC_ZIP;
1423
1424 for (f = och->start, i = 0; f <= och->end; f++, i++) {
1425
1426 /* create a new imbuf to store image for this frame */
1427 ibuf_foam = IMB_allocImBuf(res_x, res_y, ImBufFlags::FloatData);
1428 ibuf_disp = IMB_allocImBuf(res_x, res_y, ImBufFlags::FloatData);
1429 ibuf_normal = IMB_allocImBuf(res_x, res_y, ImBufFlags::FloatData);
1430 ibuf_spray = IMB_allocImBuf(res_x, res_y, ImBufFlags::FloatData);
1431 ibuf_spray_inverse = IMB_allocImBuf(res_x, res_y, ImBufFlags::FloatData);
1432
1433 BKE_ocean_simulate(o, och->time[i], och->wave_scale, och->chop_amount);
1434
1435 /* add new foam */
1436 float *ibuf_disp_data = ibuf_disp->float_data_for_write();
1437 float *ibuf_foam_data = ibuf_foam->float_data_for_write();
1438 float *ibuf_spray_data = ibuf_spray->float_data_for_write();
1439 float *ibuf_spray_inverse_data = ibuf_spray_inverse->float_data_for_write();
1440 float *ibuf_normal_data = ibuf_normal->float_data_for_write();
1441 for (y = 0; y < res_y; y++) {
1442 for (x = 0; x < res_x; x++) {
1443
1444 BKE_ocean_eval_ij(o, &ocr, x, y);
1445
1446 /* add to the image */
1447 rgb_to_rgba_unit_alpha(&ibuf_disp_data[4 * (res_x * y + x)], ocr.disp);
1448
1449 if (o->_do_jacobian) {
1450 /* TODO(@ideasman42): cleanup unused code. */
1451
1452 float /* r, */ /* UNUSED */ pr = 0.0f, foam_result;
1453 float neg_disp, neg_eplus;
1454
1455 ocr.foam = BKE_ocean_jminus_to_foam(ocr.Jminus, och->foam_coverage);
1456
1457 /* accumulate previous value for this cell */
1458 if (i > 0) {
1459 pr = prev_foam[res_x * y + x];
1460 }
1461
1462 // r = BLI_rng_get_float(rng); /* UNUSED */ /* randomly reduce foam */
1463
1464 // pr = pr * och->foam_fade; /* overall fade */
1465
1466 /* Remember ocean coord system is Y up!
1467 * break up the foam where height (Y) is low (wave valley),
1468 * and X and Z displacement is greatest. */
1469
1470 neg_disp = ocr.disp[1] < 0.0f ? 1.0f + ocr.disp[1] : 1.0f;
1471 neg_disp = neg_disp < 0.0f ? 0.0f : neg_disp;
1472
1473 /* foam, 'ocr.Eplus' only initialized with do_jacobian */
1474 neg_eplus = ocr.Eplus[2] < 0.0f ? 1.0f + ocr.Eplus[2] : 1.0f;
1475 neg_eplus = neg_eplus < 0.0f ? 0.0f : neg_eplus;
1476
1477 if (pr < 1.0f) {
1478 pr *= pr;
1479 }
1480
1481 pr *= och->foam_fade * (0.75f + neg_eplus * 0.25f);
1482
1483 /* A full clamping should not be needed! */
1484 foam_result = min_ff(pr + ocr.foam, 1.0f);
1485
1486 prev_foam[res_x * y + x] = foam_result;
1487
1488 // foam_result = min_ff(foam_result, 1.0f);
1489
1490 value_to_rgba_unit_alpha(&ibuf_foam_data[4 * (res_x * y + x)], foam_result);
1491
1492 /* spray map baking */
1493 if (o->_do_spray) {
1494 rgb_to_rgba_unit_alpha(&ibuf_spray_data[4 * (res_x * y + x)], ocr.Eplus);
1495 rgb_to_rgba_unit_alpha(&ibuf_spray_inverse_data[4 * (res_x * y + x)], ocr.Eminus);
1496 }
1497 }
1498
1499 if (o->_do_normals) {
1500 rgb_to_rgba_unit_alpha(&ibuf_normal_data[4 * (res_x * y + x)], ocr.normal);
1501 }
1502 }
1503 }
1504
1505 /* write the images */
1506 cache_filepath(filepath, och->bakepath, och->relbase, f, CACHE_TYPE_DISPLACE);
1507 if (false == BKE_imbuf_write(ibuf_disp, filepath, &imf)) {
1508 printf("Cannot save Displacement File Output to %s\n", filepath);
1509 }
1510
1511 if (o->_do_jacobian) {
1512 cache_filepath(filepath, och->bakepath, och->relbase, f, CACHE_TYPE_FOAM);
1513 if (false == BKE_imbuf_write(ibuf_foam, filepath, &imf)) {
1514 printf("Cannot save Foam File Output to %s\n", filepath);
1515 }
1516
1517 if (o->_do_spray) {
1518 cache_filepath(filepath, och->bakepath, och->relbase, f, CACHE_TYPE_SPRAY);
1519 if (false == BKE_imbuf_write(ibuf_spray, filepath, &imf)) {
1520 printf("Cannot save Spray File Output to %s\n", filepath);
1521 }
1522
1523 cache_filepath(filepath, och->bakepath, och->relbase, f, CACHE_TYPE_SPRAY_INVERSE);
1524 if (false == BKE_imbuf_write(ibuf_spray_inverse, filepath, &imf)) {
1525 printf("Cannot save Spray Inverse File Output to %s\n", filepath);
1526 }
1527 }
1528 }
1529
1530 if (o->_do_normals) {
1531 cache_filepath(filepath, och->bakepath, och->relbase, f, CACHE_TYPE_NORMAL);
1532 if (false == BKE_imbuf_write(ibuf_normal, filepath, &imf)) {
1533 printf("Cannot save Normal File Output to %s\n", filepath);
1534 }
1535 }
1536
1537 IMB_freeImBuf(ibuf_disp);
1538 IMB_freeImBuf(ibuf_foam);
1539 IMB_freeImBuf(ibuf_normal);
1540 IMB_freeImBuf(ibuf_spray);
1541 IMB_freeImBuf(ibuf_spray_inverse);
1542
1543 progress = (f - och->start) / float(och->duration);
1544
1545 update_cb(update_cb_data, progress, &cancel);
1546
1547 if (cancel) {
1548 if (prev_foam) {
1549 MEM_delete(prev_foam);
1550 }
1551 // BLI_rng_free(rng);
1552 return;
1553 }
1554 }
1555
1556 // BLI_rng_free(rng);
1557 if (prev_foam) {
1558 MEM_delete(prev_foam);
1559 }
1560 och->baked = 1;
1561}
1562
1563#else /* WITH_OCEANSIM */
1564
1565float BKE_ocean_jminus_to_foam(float /*jminus*/, float /*coverage*/)
1566{
1567 return 0.0f;
1568}
1569
1570void BKE_ocean_eval_uv(Ocean * /*oc*/, OceanResult * /*ocr*/, float /*u*/, float /*v*/) {}
1571
1572/* use catmullrom interpolation rather than linear */
1573void BKE_ocean_eval_uv_catrom(Ocean * /*oc*/, OceanResult * /*ocr*/, float /*u*/, float /*v*/) {}
1574
1575void BKE_ocean_eval_xz(Ocean * /*oc*/, OceanResult * /*ocr*/, float /*x*/, float /*z*/) {}
1576
1577void BKE_ocean_eval_xz_catrom(Ocean * /*oc*/, OceanResult * /*ocr*/, float /*x*/, float /*z*/) {}
1578
1579void BKE_ocean_eval_ij(Ocean * /*oc*/, OceanResult * /*ocr*/, int /*i*/, int /*j*/) {}
1580
1581void BKE_ocean_simulate(Ocean * /*o*/, float /*t*/, float /*scale*/, float /*chop_amount*/) {}
1582
1584{
1585 Ocean *oc = MEM_new_zeroed<Ocean>("ocean sim data");
1586
1587 return oc;
1588}
1589
1591 int /*M*/,
1592 int /*N*/,
1593 float /*Lx*/,
1594 float /*Lz*/,
1595 float /*V*/,
1596 float /*l*/,
1597 float /*A*/,
1598 float /*w*/,
1599 float /*damp*/,
1600 float /*alignment*/,
1601 float /*depth*/,
1602 float /*time*/,
1603 int /*spectrum*/,
1604 float /*fetch_jonswap*/,
1605 float /*sharpen_peak_jonswap*/,
1606 short /*do_height_field*/,
1607 short /*do_chop*/,
1608 short /*do_spray*/,
1609 short /*do_normals*/,
1610 short /*do_jacobian*/,
1611 int /*seed*/)
1612{
1613 return false;
1614}
1615
1617
1619{
1620 if (!oc) {
1621 return;
1622 }
1623 MEM_delete(oc);
1624}
1625
1626/* ********* Baking/Caching ********* */
1627
1629{
1630 if (!och) {
1631 return;
1632 }
1633
1634 MEM_delete(och);
1635}
1636
1638 OceanCache * /*och*/, OceanResult * /*ocr*/, int /*f*/, float /*u*/, float /*v*/)
1639{
1640}
1641
1643 OceanCache * /*och*/, OceanResult * /*ocr*/, int /*f*/, int /*i*/, int /*j*/)
1644{
1645}
1646
1647OceanCache *BKE_ocean_init_cache(const char * /*bakepath*/,
1648 const char * /*relbase*/,
1649 int /*start*/,
1650 int /*end*/,
1651 float /*wave_scale*/,
1652 float /*chop_amount*/,
1653 float /*foam_coverage*/,
1654 float /*foam_fade*/,
1655 int /*resolution*/)
1656{
1657 OceanCache *och = MEM_new_zeroed<OceanCache>("ocean cache data");
1658
1659 return och;
1660}
1661
1662void BKE_ocean_simulate_cache(OceanCache * /*och*/, int /*frame*/) {}
1663
1665 OceanCache * /*och*/,
1666 void (*update_cb)(void *, float progress, int *cancel),
1667 void * /*update_cb_data*/)
1668{
1669 /* unused */
1670 (void)update_cb;
1671}
1672
1674 OceanModifierData const * /*omd*/,
1675 int /*resolution*/)
1676{
1677 return true;
1678}
1679
1680#endif /* WITH_OCEANSIM */
1681
1683{
1685 omd->oceancache = nullptr;
1686 omd->cached = false;
1687}
1688
1689} // namespace blender
#define M_PI
#define MINLINE
#define FILE_MAX
#define BLI_path_join(...)
Random number functions.
#define THREAD_LOCK_READ
#define THREAD_LOCK_WRITE
const char * dirname(char *path)
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar length2() const
Return the length of the vector squared.
Definition btVector3.h:251
#define logf(x)
#define expf(x)
#define powf(x, y)
#define fmodf(x, y)
#define tanhf(x)
#define sin
#define cos
#define abs
#define floor
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline float2 fmod(const float2 a, const float b)
#define M
#define N
double real
Definition Precision.h:14
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
std::string filename(const std::string_view &str, size_t pos)
float4 interpolate_bilinear_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:59
BMesh Iterators.
MINLINE float min_ff(float a, float b)
@ MOD_OCEAN_SPECTRUM_PIERSON_MOSKOWITZ
@ MOD_OCEAN_SPECTRUM_TEXEL_MARSEN_ARSLOE
void BKE_ocean_eval_uv_catrom(struct Ocean *oc, struct OceanResult *ocr, float u, float v)
Definition ocean.cc:1573
float BKE_ocean_jminus_to_foam(float jminus, float coverage)
Definition ocean.cc:1565
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_ocean_eval_uv(struct Ocean *oc, struct OceanResult *ocr, float u, float v)
Definition ocean.cc:1570
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition BLI_hash_c.hh:53
ATTR_WARN_UNUSED_RESULT const BMLoop * l
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
void BKE_ocean_free_modifier_cache(struct OceanModifierData *omd)
Definition ocean.cc:1682
void BKE_ocean_free(struct Ocean *oc)
Definition ocean.cc:1618
void BKE_ocean_eval_xz(struct Ocean *oc, struct OceanResult *ocr, float x, float z)
Definition ocean.cc:1575
void BKE_ocean_bake(struct Ocean *o, struct OceanCache *och, void(*update_cb)(void *, float progress, int *cancel), void *update_cb_data)
Definition ocean.cc:1664
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition threads.cc:470
void BKE_ocean_free_data(struct Ocean *oc)
Definition ocean.cc:1616
struct RNG * BLI_rng_new(unsigned int seed)
Definition rand.cc:41
struct OceanCache * BKE_ocean_init_cache(const char *bakepath, const char *relbase, int start, int end, float wave_scale, float chop_amount, float foam_coverage, float foam_fade, int resolution)
Definition ocean.cc:1647
void BLI_rw_mutex_init(ThreadRWMutex *mutex)
Definition threads.cc:465
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:460
void BKE_ocean_eval_ij(struct Ocean *oc, struct OceanResult *ocr, int i, int j)
Definition ocean.cc:1579
ImBuf * IMB_load_image_from_filepath(const char *filepath, ImBufFlags flags, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:201
void IMB_freeImBuf(ImBuf *ibuf)
float BLI_ocean_spectrum_texelmarsenarsloe(const struct Ocean *oc, float kx, float kz)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition threads.cc:480
void BLI_rng_seed(struct RNG *rng, unsigned int seed) ATTR_NONNULL(1)
Definition rand.cc:60
float BLI_ocean_spectrum_piersonmoskowitz(const struct Ocean *oc, float kx, float kz)
float BLI_ocean_spectrum_jonswap(const struct Ocean *oc, float kx, float kz)
sqrt(x)+1/max(0
void BKE_ocean_eval_xz_catrom(struct Ocean *oc, struct OceanResult *ocr, float x, float z)
Definition ocean.cc:1577
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:55
bool BKE_ocean_init_from_modifier(struct Ocean *ocean, struct OceanModifierData const *omd, int resolution)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:90
void BLI_rw_mutex_end(ThreadRWMutex *mutex)
Definition threads.cc:485
void BKE_ocean_cache_eval_uv(struct OceanCache *och, struct OceanResult *ocr, int f, float u, float v)
Definition ocean.cc:1637
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:404
MINLINE float normalize_v3(float n[3])
bool BKE_ocean_is_valid(const struct Ocean *o)
void BKE_ocean_free_cache(struct OceanCache *och)
Definition ocean.cc:1628
void BKE_ocean_cache_eval_ij(struct OceanCache *och, struct OceanResult *ocr, int f, int i, int j)
Definition ocean.cc:1642
void BKE_ocean_simulate_cache(struct OceanCache *och, int frame)
Definition ocean.cc:1662
Vector< bke::path_templates::Error > BKE_image_path_from_imtype(char *filepath, const char *base, const char *relbase, const bke::path_templates::VariableMap *template_variables, int frame, char imtype, bool use_ext, bool use_frames, const char *suffix)
void BLI_thread_unlock(int type)
Definition threads.cc:336
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
bool BKE_ocean_ensure(struct OceanModifierData *omd, int resolution)
bool BKE_imbuf_write(ImBuf *ibuf, const char *filepath, const ImageFormatData *imf)
void BLI_thread_lock(int type)
Definition threads.cc:331
void BKE_ocean_simulate(struct Ocean *o, float t, float scale, float chop_amount)
Definition ocean.cc:1581
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
struct Ocean * BKE_ocean_add()
Definition ocean.cc:1583
bool BKE_ocean_init(struct Ocean *o, int M, int N, float Lx, float Lz, float V, float l, float A, float w, float damp, float alignment, float depth, float time, int spectrum, float fetch_jonswap, float sharpen_peak_jonswap, short do_height_field, short do_chop, short do_spray, short do_normals, short do_jacobian, int seed)
Definition ocean.cc:1590
#define fabsf
#define sqrtf
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
CCL_NAMESPACE_BEGIN struct Window V