Blender V5.3
pointcache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2025 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later */
5
9
10#include <algorithm>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <sys/stat.h>
15#include <sys/types.h>
16
17/* needed for directory lookup */
18#ifndef WIN32
19# include <dirent.h>
20#else
21# include "BLI_winstuff.hh"
22#endif
23
24#include "CLG_log.h"
25
26#include "MEM_guardedalloc.h"
27
28#include "DNA_ID.h"
30#include "DNA_fluid_types.h"
31#include "DNA_layer_types.h"
32#include "DNA_modifier_types.h"
34#include "DNA_object_types.h"
35#include "DNA_particle_types.h"
36#include "DNA_rigidbody_types.h"
37#include "DNA_scene_types.h"
38#include "DNA_space_types.h"
39
40#include "BLI_compression.hh"
41#include "BLI_fileops.hh"
42#include "BLI_listbase.hh"
44#include "BLI_math_vector_c.hh"
45#include "BLI_path_utils.hh"
46#include "BLI_string.hh"
47#include "BLI_string_utf8.hh"
48#include "BLI_time.hh"
49#include "BLI_utildefines.hh"
50
51#include "BLT_translation.hh"
52
53#include "BKE_appdir.hh"
54#include "BKE_cloth.hh"
55#include "BKE_collection.hh"
56#include "BKE_duplilist.hh"
57#include "BKE_dynamicpaint.h"
58#include "BKE_fluid.h"
59#include "BKE_global.hh"
60#include "BKE_lib_id.hh"
61#include "BKE_library.hh"
62#include "BKE_main.hh"
63#include "BKE_modifier.hh"
64#include "BKE_object.hh"
65#include "BKE_particle.h"
66#include "BKE_pointcache.h"
67#include "BKE_scene.hh"
68#include "BKE_softbody.h"
69
70#include "BLO_read_write.hh"
71
73
74#include "BIK_api.h"
75
76#ifdef WITH_BULLET
77# include "RBI_api.h"
78#endif
79
80#include <zstd.h>
81
82namespace blender {
83
84#define PTCACHE_DATA_FROM(data, type, from) \
85 if (data[type]) { \
86 memcpy(data[type], from, ptcache_data_size[type]); \
87 } \
88 (void)0
89
90#define PTCACHE_DATA_TO(data, type, index, to) \
91 if (data[type]) { \
92 memcpy(to, \
93 (char *)(data)[type] + ((index) ? (index) * ptcache_data_size[type] : 0), \
94 ptcache_data_size[type]); \
95 } \
96 (void)0
97
98/* could be made into a pointcache option */
99#define DURIAN_POINTCACHE_LIB_OK 1
100
101static CLG_LogRef LOG = {"physics.pointcache"};
102
103static int ptcache_data_size[] = {
104 sizeof(uint), /* BPHYS_DATA_INDEX */
105 sizeof(float[3]), /* BPHYS_DATA_LOCATION */
106 sizeof(float[3]), /* BPHYS_DATA_VELOCITY */
107 sizeof(float[4]), /* BPHYS_DATA_ROTATION */
108 sizeof(float[3]), /* BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
109 sizeof(float), /* BPHYS_DATA_SIZE */
110 sizeof(float[3]), /* BPHYS_DATA_TIMES */
111 sizeof(BoidData), /* case BPHYS_DATA_BOIDS */
112};
113
115 0,
116 sizeof(ParticleSpring),
117 sizeof(float[3]),
118};
119
120/* forward declarations */
121[[nodiscard]] static bool ptcache_file_compressed_read(PTCacheFile *pf,
122 uchar *result,
123 size_t items_num,
124 size_t item_size);
125[[nodiscard]] static bool ptcache_file_compressed_write(PTCacheFile *pf,
126 const void *data,
127 size_t items_num,
128 size_t item_size);
129[[nodiscard]] static bool ptcache_file_write(PTCacheFile *pf,
130 const void *data,
131 size_t items_num,
132 size_t item_size);
133[[nodiscard]] static bool ptcache_file_read(PTCacheFile *pf,
134 void *f,
135 size_t items_num,
136 size_t item_size);
137
138/* Common functions */
140{
141 int error = 0;
142
143 /* Custom functions should read these basic elements too! */
144 if (!error && !fread(&pf->totpoint, sizeof(uint), 1, pf->fp)) {
145 error = 1;
146 }
147
148 if (!error && !fread(&pf->data_types, sizeof(uint), 1, pf->fp)) {
149 error = 1;
150 }
151
152 return !error;
153}
155{
156 /* Custom functions should write these basic elements too! */
157 if (!fwrite(&pf->totpoint, sizeof(uint), 1, pf->fp)) {
158 return 0;
159 }
160
161 if (!fwrite(&pf->data_types, sizeof(uint), 1, pf->fp)) {
162 return 0;
163 }
164
165 return 1;
166}
169 uint count,
170 void *data)
171{
172 PTCacheExtra *extra = MEM_new<PTCacheExtra>("Point cache: extra data descriptor");
173
174 extra->type = type;
175 extra->totdata = count;
176
177 size_t size = extra->totdata * ptcache_extra_datasize[extra->type];
178
179 extra->data = MEM_new_uninitialized(size, "Point cache: extra data");
180 memcpy(extra->data, data, size);
181
182 BLI_addtail(&pm->extradata, extra);
183}
184/* Softbody functions */
185static int ptcache_softbody_write(int index, void *soft_v, void **data, int /*cfra*/)
186{
187 SoftBody *soft = static_cast<SoftBody *>(soft_v);
188 BodyPoint *bp = soft->bpoint + index;
189
192
193 return 1;
194}
196 int index, void *soft_v, void **data, float /*cfra*/, const float *old_data)
197{
198 SoftBody *soft = static_cast<SoftBody *>(soft_v);
199 BodyPoint *bp = soft->bpoint + index;
200
201 if (old_data) {
202 memcpy(bp->pos, data, sizeof(float[3]));
203 memcpy(bp->vec, data + 3, sizeof(float[3]));
204 }
205 else {
208 }
209}
211 void *soft_v,
212 void **data,
213 float cfra,
214 float cfra1,
215 float cfra2,
216 const float *old_data)
217{
218 SoftBody *soft = static_cast<SoftBody *>(soft_v);
219 BodyPoint *bp = soft->bpoint + index;
220 ParticleKey keys[4];
221 float dfra;
222
223 if (cfra1 == cfra2) {
224 return;
225 }
226
227 copy_v3_v3(keys[1].co, bp->pos);
228 copy_v3_v3(keys[1].vel, bp->vec);
229
230 if (old_data) {
231 memcpy(keys[2].co, old_data, sizeof(float[3]));
232 memcpy(keys[2].vel, old_data + 3, sizeof(float[3]));
233 }
234 else {
235 BKE_ptcache_make_particle_key(keys + 2, 0, data, cfra2);
236 }
237
238 dfra = cfra2 - cfra1;
239
240 mul_v3_fl(keys[1].vel, dfra);
241 mul_v3_fl(keys[2].vel, dfra);
242
243 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, true);
244
245 mul_v3_fl(keys->vel, 1.0f / dfra);
246
247 copy_v3_v3(bp->pos, keys->co);
248 copy_v3_v3(bp->vec, keys->vel);
249}
250static int ptcache_softbody_totpoint(void *soft_v, int /*cfra*/)
251{
252 SoftBody *soft = static_cast<SoftBody *>(soft_v);
253 return soft->totpoint;
254}
255static void ptcache_softbody_error(const ID * /*owner_id*/,
256 void * /*soft_v*/,
257 const char * /*message*/)
258{
259 /* ignored for now */
260}
261
262/* Particle functions. */
263
264void BKE_ptcache_make_particle_key(ParticleKey *key, int index, void **data, float time)
265{
268
269 /* no rotation info, so make something nice up */
270 if (data[BPHYS_DATA_ROTATION] == nullptr) {
271 vec_to_quat(key->rot, key->vel, OB_NEGX, OB_POSZ);
272 }
273 else {
275 }
276
278 key->time = time;
279}
280static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
281{
282 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
283 ParticleData *pa = psys->particles + index;
284 BoidParticle *boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : nullptr;
285 float times[3];
286 int step = psys->pointcache->step;
287
288 /* Skip some particles that are not stored in the cache. */
289 if (data[BPHYS_DATA_INDEX]) {
290 if (psys->part->flag & PART_DIED) {
291 /* Dead particles are stored when they are displayed. */
292 if (cfra < pa->time - step) {
293 return 0;
294 }
295 }
296 else {
297 /* Inclusive ranges for particle lifetime (`dietime - 1` for an inclusive end-frame). */
298 const int pa_sfra = int(pa->time) - step;
299 const int pa_efra = (int(pa->dietime) - 1) + step;
300 if (!(cfra >= pa_sfra && cfra <= pa_efra)) {
301 return 0;
302 }
303 }
304 }
305
306 times[0] = pa->time;
307 times[1] = pa->dietime;
308 times[2] = pa->lifetime;
309
317
318 if (boid) {
320 }
321
322 /* Return flag 1+1=2 for newly born particles
323 * to copy exact birth location to previously cached frame. */
324 return 1 + (pa->state.time >= pa->time && pa->prev_state.time <= pa->time);
325}
327 int index, void *psys_v, void **data, float cfra, const float *old_data)
328{
329 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
330 ParticleData *pa;
331 BoidParticle *boid;
332 float timestep = 0.04f * psys->part->timetweak;
333
334 if (index < 0 || index >= psys->totpart) {
335 return;
336 }
337
338 pa = psys->particles + index;
339 boid = (psys->part->phystype == PART_PHYS_BOIDS) ? pa->boid : nullptr;
340
341 if (cfra > pa->state.time) {
342 memcpy(&pa->prev_state, &pa->state, sizeof(ParticleKey));
343 }
344
345 if (old_data) {
346 /* old format cache */
347 pa->state = *reinterpret_cast<const ParticleKey *>(old_data);
348 return;
349 }
350
352
353 /* set frames cached before birth to birth time */
354 if (cfra < pa->time) {
355 pa->state.time = pa->time;
356 }
357 else if (cfra > pa->dietime) {
358 pa->state.time = pa->dietime;
359 }
360
361 if (data[BPHYS_DATA_SIZE]) {
363 }
364
365 if (data[BPHYS_DATA_TIMES]) {
366 float times[3];
368 pa->time = times[0];
369 pa->dietime = times[1];
370 pa->lifetime = times[2];
371 }
372
373 if (boid) {
375 }
376
377 /* determine velocity from previous location */
379 if (cfra > pa->prev_state.time) {
380 sub_v3_v3v3(pa->state.vel, pa->state.co, pa->prev_state.co);
381 mul_v3_fl(pa->state.vel, (cfra - pa->prev_state.time) * timestep);
382 }
383 else {
384 sub_v3_v3v3(pa->state.vel, pa->prev_state.co, pa->state.co);
385 mul_v3_fl(pa->state.vel, (pa->prev_state.time - cfra) * timestep);
386 }
387 }
388
389 /* default to no rotation */
391 unit_qt(pa->state.rot);
392 }
393}
395 void *psys_v,
396 void **data,
397 float cfra,
398 float cfra1,
399 float cfra2,
400 const float *old_data)
401{
402 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
403 ParticleData *pa;
404 ParticleKey keys[4];
405 float dfra, timestep = 0.04f * psys->part->timetweak;
406
407 if (index < 0 || index >= psys->totpart) {
408 return;
409 }
410
411 pa = psys->particles + index;
412
413 /* Inclusive ranges for particle lifetime (`dietime - 1` for an inclusive end-frame). */
414 const int pa_sfra = int(pa->time) - psys->pointcache->step;
415 const int pa_efra = (int(pa->dietime) - 1) + psys->pointcache->step;
416
417 /* Particle wasn't read from first cache so can't interpolate. */
418 if (!(cfra1 >= pa_sfra && cfra1 <= pa_efra)) {
419 return;
420 }
421
422 cfra = std::min(cfra, pa->dietime);
423 cfra1 = std::min(cfra1, pa->dietime);
424 cfra2 = std::min(cfra2, pa->dietime);
425
426 if (cfra1 == cfra2) {
427 return;
428 }
429
430 keys[1] = pa->state;
431 if (old_data) {
432 keys[2] = *reinterpret_cast<const ParticleKey *>(old_data);
433 }
434 else {
435 BKE_ptcache_make_particle_key(keys + 2, 0, data, cfra2);
436 }
437
438 /* determine velocity from previous location */
440 if (keys[1].time > keys[2].time) {
441 sub_v3_v3v3(keys[2].vel, keys[1].co, keys[2].co);
442 mul_v3_fl(keys[2].vel, (keys[1].time - keys[2].time) * timestep);
443 }
444 else {
445 sub_v3_v3v3(keys[2].vel, keys[2].co, keys[1].co);
446 mul_v3_fl(keys[2].vel, (keys[2].time - keys[1].time) * timestep);
447 }
448 }
449
450 /* default to no rotation */
452 unit_qt(keys[2].rot);
453 }
454
455 if (cfra > pa->time) {
456 cfra1 = std::max(cfra1, pa->time);
457 }
458
459 dfra = cfra2 - cfra1;
460
461 mul_v3_fl(keys[1].vel, dfra * timestep);
462 mul_v3_fl(keys[2].vel, dfra * timestep);
463
464 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, &pa->state, true);
465 interp_qt_qtqt(pa->state.rot, keys[1].rot, keys[2].rot, (cfra - cfra1) / dfra);
466
467 mul_v3_fl(pa->state.vel, 1.0f / (dfra * timestep));
468
469 pa->state.time = cfra;
470}
471
472static int ptcache_particle_totpoint(void *psys_v, int /*cfra*/)
473{
474 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
475 return psys->totpart;
476}
477
478static void ptcache_particle_error(const ID * /*owner_id*/,
479 void * /*psys_v*/,
480 const char * /*message*/)
481{
482 /* ignored for now */
483}
484
485static int ptcache_particle_totwrite(void *psys_v, int cfra)
486{
487 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
488 ParticleData *pa = psys->particles;
489 int p, step = psys->pointcache->step;
490 int totwrite = 0;
491
492 if (cfra == 0) {
493 return psys->totpart;
494 }
495
496 if (psys->part->flag & PART_DIED) {
497 /* Also store dead particles when they are displayed. */
498 for (p = 0; p < psys->totpart; p++, pa++) {
499 const int pa_sfra = int(pa->time) - step;
500 totwrite += (cfra >= pa_sfra);
501 }
502 }
503 else {
504 for (p = 0; p < psys->totpart; p++, pa++) {
505 /* Inclusive ranges for particle lifetime (`dietime - 1` for an inclusive end-frame). */
506 const int pa_sfra = int(pa->time) - step;
507 const int pa_efra = (int(pa->dietime) - 1) + step;
508 totwrite += (cfra >= pa_sfra) && (cfra <= pa_efra);
509 }
510 }
511
512 return totwrite;
513}
514
515static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int /*cfra*/)
516{
517 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
518
519 if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid &&
521 psys->fluid_springs)
522 {
525 }
526}
527
528static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float /*cfra*/)
529{
530 ParticleSystem *psys = static_cast<ParticleSystem *>(psys_v);
531 PTCacheExtra *extra = pm->extradata.first();
532
533 for (; extra; extra = extra->next) {
534 switch (extra->type) {
536 if (psys->fluid_springs) {
537 MEM_delete(psys->fluid_springs);
538 }
539
540 psys->fluid_springs = MEM_dupalloc(static_cast<ParticleSpring *>(extra->data));
541 psys->alloc_fluidsprings = extra->totdata;
542
543 /* Drop springs with invalid particle indices from corrupt files. */
544 const uint totpart = uint(std::max(psys->totpart, 0));
545 int springs_num = 0;
546 for (uint i = 0; i < extra->totdata; i++) {
547 const ParticleSpring &spring = psys->fluid_springs[i];
548 if (spring.particle_index[0] < totpart && spring.particle_index[1] < totpart) {
549 psys->fluid_springs[springs_num++] = spring;
550 }
551 }
552 psys->tot_fluidsprings = springs_num;
553 break;
554 }
556 break;
557 }
558 }
559}
560
561/* Cloth functions */
562static int ptcache_cloth_write(int index, void *cloth_v, void **data, int /*cfra*/)
563{
564 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
565 Cloth *cloth = clmd->clothObject;
566 ClothVertex *vert = cloth->verts + index;
567
571
572 return 1;
573}
575 int index, void *cloth_v, void **data, float /*cfra*/, const float *old_data)
576{
577 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
578 Cloth *cloth = clmd->clothObject;
579 ClothVertex *vert = cloth->verts + index;
580
581 if (old_data) {
582 memcpy(vert->x, data, sizeof(float[3]));
583 memcpy(vert->xconst, data + 3, sizeof(float[3]));
584 memcpy(vert->v, data + 6, sizeof(float[3]));
585 }
586 else {
589 PTCACHE_DATA_TO(data, BPHYS_DATA_XCONST, 0, vert->xconst);
590 }
591}
593 void *cloth_v,
594 void **data,
595 float cfra,
596 float cfra1,
597 float cfra2,
598 const float *old_data)
599{
600 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
601 Cloth *cloth = clmd->clothObject;
602 ClothVertex *vert = cloth->verts + index;
603 ParticleKey keys[4];
604 float dfra;
605
606 if (cfra1 == cfra2) {
607 return;
608 }
609
610 copy_v3_v3(keys[1].co, vert->x);
611 copy_v3_v3(keys[1].vel, vert->v);
612
613 if (old_data) {
614 memcpy(keys[2].co, old_data, sizeof(float[3]));
615 memcpy(keys[2].vel, old_data + 6, sizeof(float[3]));
616 }
617 else {
618 BKE_ptcache_make_particle_key(keys + 2, 0, data, cfra2);
619 }
620
621 dfra = cfra2 - cfra1;
622
623 mul_v3_fl(keys[1].vel, dfra);
624 mul_v3_fl(keys[2].vel, dfra);
625
626 psys_interpolate_particle(-1, keys, (cfra - cfra1) / dfra, keys, true);
627
628 mul_v3_fl(keys->vel, 1.0f / dfra);
629
630 copy_v3_v3(vert->x, keys->co);
631 copy_v3_v3(vert->v, keys->vel);
632
633 /* should vert->xconst be interpolated somehow too? - jahka */
634}
635
636static void ptcache_cloth_extra_write(void *cloth_v, PTCacheMem *pm, int /*cfra*/)
637{
638 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
639 Cloth *cloth = clmd->clothObject;
640
641 if (!is_zero_v3(cloth->average_acceleration)) {
642 ptcache_add_extra_data(pm, BPHYS_EXTRA_CLOTH_ACCELERATION, 1, cloth->average_acceleration);
643 }
644}
645static void ptcache_cloth_extra_read(void *cloth_v, PTCacheMem *pm, float /*cfra*/)
646{
647 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
648 Cloth *cloth = clmd->clothObject;
649 PTCacheExtra *extra = pm->extradata.first();
650
651 zero_v3(cloth->average_acceleration);
652
653 for (; extra; extra = extra->next) {
654 switch (extra->type) {
656 if (extra->totdata < 1) {
657 break; /* Guard against corrupt file. */
658 }
659 copy_v3_v3(cloth->average_acceleration, static_cast<const float *>(extra->data));
660 break;
661 }
663 break;
664 }
665 }
666}
667
668static int ptcache_cloth_totpoint(void *cloth_v, int /*cfra*/)
669{
670 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
671 return clmd->clothObject ? clmd->clothObject->mvert_num : 0;
672}
673
674static void ptcache_cloth_error(const ID *owner_id, void *cloth_v, const char *message)
675{
676 ClothModifierData *clmd = static_cast<ClothModifierData *>(cloth_v);
677 BLI_assert(GS(owner_id->name) == ID_OB);
678 if (clmd->hairdata == nullptr) {
679 /* If there is hair data, this modifier does not actually exist on the object. */
681 id_cast<Object *>(const_cast<ID *>(owner_id)), &clmd->modifier, "%s", message);
682 }
683}
684
685static int ptcache_dynamicpaint_totpoint(void *sd, int /*cfra*/)
686{
687 DynamicPaintSurface *surface = static_cast<DynamicPaintSurface *>(sd);
688
689 if (!surface->data) {
690 return 0;
691 }
692
693 return surface->data->total_points;
694}
695
696static void ptcache_dynamicpaint_error(const ID * /*owner_id*/,
697 void * /*sd*/,
698 const char * /*message*/)
699{
700 /* ignored for now */
701}
702
703#define DPAINT_CACHE_VERSION "1.01"
704
706{
707 DynamicPaintSurface *surface = static_cast<DynamicPaintSurface *>(dp_v);
708 bool ok = true;
709
710 /* version header */
711 ok &= ptcache_file_write(pf, DPAINT_CACHE_VERSION, 1, sizeof(char[4]));
712
713 if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
714 const int total_points = surface->data->total_points;
715
716 /* cache type */
717 const int surface_type = int(surface->type);
718 ok &= ptcache_file_write(pf, &surface_type, 1, sizeof(int));
719
720 uint in_stride;
721 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
722 in_stride = sizeof(PaintPoint);
723 }
725 in_stride = sizeof(float);
726 }
727 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
728 in_stride = sizeof(PaintWavePoint);
729 }
730 else {
731 return false;
732 }
733
734 ok &= ptcache_file_compressed_write(pf, surface->data->type_data, total_points, in_stride);
735 }
736 return ok;
737}
739{
740 DynamicPaintSurface *surface = static_cast<DynamicPaintSurface *>(dp_v);
741 char version[4];
742
743 /* version header */
744 if (!ptcache_file_read(pf, version, 1, sizeof(char[4]))) {
745 return 0;
746 }
747 if (!STREQLEN(version, DPAINT_CACHE_VERSION, 4)) {
748 CLOG_ERROR(&LOG, "Dynamic Paint: Invalid cache version: '%c%c%c%c'!", UNPACK4(version));
749 return 0;
750 }
751
752 if (surface->format != MOD_DPAINT_SURFACE_F_IMAGESEQ && surface->data) {
753 uint data_len;
754 int surface_type;
755
756 /* cache type */
757 if (!ptcache_file_read(pf, &surface_type, 1, sizeof(int))) {
758 return 0;
759 }
760
761 if (surface_type != surface->type) {
762 return 0;
763 }
764
765 /* read surface data */
766 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
767 data_len = sizeof(PaintPoint);
768 }
770 data_len = sizeof(float);
771 }
772 else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) {
773 data_len = sizeof(PaintWavePoint);
774 }
775 else {
776 return 0;
777 }
778
780 static_cast<uchar *>(surface->data->type_data),
781 surface->data->total_points,
782 data_len))
783 {
784 CLOG_ERROR(&LOG, "Dynamic Paint: Unable to read the compressed cache data");
785 return 0;
786 }
787 }
788 return 1;
789}
790
791/* Rigid Body functions */
792static int ptcache_rigidbody_write(int index, void *rb_v, void **data, int /*cfra*/)
793{
794 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
795 Object *ob = nullptr;
796
797 if (rbw->objects) {
798 ob = rbw->objects[index];
799 }
800
801 if (ob && ob->rigidbody_object) {
802 RigidBodyOb *rbo = ob->rigidbody_object;
803
804 if (rbo->type == RBO_TYPE_ACTIVE && rbo->shared->physics_object != nullptr) {
805#ifdef WITH_BULLET
806 RB_body_get_position(static_cast<rbRigidBody *>(rbo->shared->physics_object), rbo->pos);
807 RB_body_get_orientation(static_cast<rbRigidBody *>(rbo->shared->physics_object), rbo->orn);
808#endif
811 }
812 }
813
814 return 1;
815}
817 int index, void *rb_v, void **data, float /*cfra*/, const float *old_data)
818{
819 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
820 Object *ob = nullptr;
821
822 if (rbw->objects) {
823 ob = rbw->objects[index];
824 }
825
826 if (ob && ob->rigidbody_object) {
827 RigidBodyOb *rbo = ob->rigidbody_object;
828
829 if (rbo->type == RBO_TYPE_ACTIVE) {
830
831 if (old_data) {
832 memcpy(rbo->pos, data, sizeof(float[3]));
833 memcpy(rbo->orn, data + 3, sizeof(float[4]));
834 }
835 else {
838 }
839 }
840 }
841}
843 void *rb_v,
844 void **data,
845 float cfra,
846 float cfra1,
847 float cfra2,
848 const float *old_data)
849{
850 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
851 Object *ob = nullptr;
852
853 if (rbw->objects) {
854 ob = rbw->objects[index];
855 }
856
857 if (ob && ob->rigidbody_object) {
858 RigidBodyOb *rbo = ob->rigidbody_object;
859
860 if (rbo->type == RBO_TYPE_ACTIVE) {
861 /* It may be possible to improve results by taking into account velocity
862 * for interpolation using psys_interpolate_particle, however this is
863 * not currently cached. */
864 float pos[3], orn[4];
865
866 if (old_data) {
867 memcpy(pos, data, sizeof(float[3]));
868 memcpy(orn, data + 3, sizeof(float[4]));
869 }
870 else {
873 }
874
875 const float t = (cfra - cfra1) / (cfra2 - cfra1);
876 interp_v3_v3v3(rbo->pos, rbo->pos, pos, t);
877 interp_qt_qtqt(rbo->orn, rbo->orn, orn, t);
878 }
879 }
880}
881static int ptcache_rigidbody_totpoint(void *rb_v, int /*cfra*/)
882{
883 RigidBodyWorld *rbw = static_cast<RigidBodyWorld *>(rb_v);
884
885 return rbw->numbodies;
886}
887
888static void ptcache_rigidbody_error(const ID * /*owner_id*/,
889 void * /*rb_v*/,
890 const char * /*message*/)
891{
892 /* ignored for now */
893}
894
895/* Creating ID's */
896
898{
899 memset(pid, 0, sizeof(PTCacheID));
900
901 pid->owner_id = &ob->id;
902 pid->calldata = sb;
904 pid->cache = sb->shared->pointcache;
905 pid->cache_ptr = &sb->shared->pointcache;
906 pid->ptcaches = &sb->shared->ptcaches;
909
913
914 pid->write_stream = nullptr;
915 pid->read_stream = nullptr;
916
917 pid->write_extra_data = nullptr;
918 pid->read_extra_data = nullptr;
919 pid->interpolate_extra_data = nullptr;
920
923
925 pid->info_types = 0;
926
927 pid->stack_index = pid->cache->index;
928
929 pid->default_step = 1;
930 pid->max_step = 20;
932}
934{
935 memset(pid, 0, sizeof(PTCacheID));
936
937 pid->owner_id = &ob->id;
938 pid->calldata = psys;
940 pid->stack_index = psys->pointcache->index;
941 pid->cache = psys->pointcache;
942 pid->cache_ptr = &psys->pointcache;
943 pid->ptcaches = &psys->ptcaches;
944
945 if (psys->part->type != PART_HAIR) {
947 }
948
952
956
957 pid->write_stream = nullptr;
958 pid->read_stream = nullptr;
959
960 pid->write_extra_data = nullptr;
961 pid->read_extra_data = nullptr;
962 pid->interpolate_extra_data = nullptr;
963
966
967 pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_VELOCITY) |
968 (1 << BPHYS_DATA_INDEX);
969
970 if (psys->part->phystype == PART_PHYS_BOIDS) {
971 pid->data_types |= (1 << BPHYS_DATA_AVELOCITY) | (1 << BPHYS_DATA_ROTATION) |
972 (1 << BPHYS_DATA_BOIDS);
973 }
974 else if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid &&
976 {
979 }
980
981 if (psys->part->flag & PART_ROTATIONS) {
982 pid->data_types |= (1 << BPHYS_DATA_ROTATION);
983
984 if (psys->part->rotmode != PART_ROT_VEL || psys->part->avemode == PART_AVE_RAND ||
985 psys->part->avefac != 0.0f)
986 {
987 pid->data_types |= (1 << BPHYS_DATA_AVELOCITY);
988 }
989 }
990
991 pid->info_types = (1 << BPHYS_DATA_TIMES);
992
993 pid->default_step = 1;
994 pid->max_step = 20;
996}
998{
999 memset(pid, 0, sizeof(PTCacheID));
1000
1001 pid->owner_id = &ob->id;
1002 pid->calldata = clmd;
1003 pid->type = PTCACHE_TYPE_CLOTH;
1004 pid->stack_index = clmd->point_cache->index;
1005 pid->cache = clmd->point_cache;
1006 pid->cache_ptr = &clmd->point_cache;
1007 pid->ptcaches = &clmd->ptcaches;
1010
1014
1015 pid->write_stream = nullptr;
1016 pid->read_stream = nullptr;
1017
1020 pid->interpolate_extra_data = nullptr;
1021
1024
1025 pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_VELOCITY) |
1026 (1 << BPHYS_DATA_XCONST);
1027 pid->info_types = 0;
1028
1029 pid->default_step = 1;
1030 pid->max_step = 1;
1032}
1033
1035{
1036 FluidDomainSettings *fds = fmd->domain;
1037
1038 memset(pid, 0, sizeof(PTCacheID));
1039
1040 pid->owner_id = &ob->id;
1041 pid->calldata = fmd;
1042
1044 pid->stack_index = fds->point_cache[0]->index;
1045
1046 pid->cache = fds->point_cache[0];
1047 pid->cache_ptr = &(fds->point_cache[0]);
1048 pid->ptcaches = &(fds->ptcaches[0]);
1049}
1050
1052{
1053
1054 memset(pid, 0, sizeof(PTCacheID));
1055
1056 pid->owner_id = &ob->id;
1057 pid->calldata = surface;
1059 pid->cache = surface->pointcache;
1060 pid->cache_ptr = &surface->pointcache;
1061 pid->ptcaches = &surface->ptcaches;
1064
1065 pid->write_point = nullptr;
1066 pid->read_point = nullptr;
1067 pid->interpolate_point = nullptr;
1068
1071
1072 pid->write_extra_data = nullptr;
1073 pid->read_extra_data = nullptr;
1074 pid->interpolate_extra_data = nullptr;
1075
1078
1080 pid->info_types = 0;
1081
1082 pid->stack_index = pid->cache->index;
1083
1084 pid->default_step = 1;
1085 pid->max_step = 1;
1087}
1088
1090{
1091
1092 memset(pid, 0, sizeof(PTCacheID));
1093
1094 pid->owner_id = ob != nullptr ? &ob->id : nullptr;
1095 pid->calldata = rbw;
1097 pid->cache = rbw->shared->pointcache;
1098 pid->cache_ptr = &rbw->shared->pointcache;
1099 pid->ptcaches = &rbw->shared->ptcaches;
1102
1106
1107 pid->write_stream = nullptr;
1108 pid->read_stream = nullptr;
1109
1110 pid->write_extra_data = nullptr;
1111 pid->read_extra_data = nullptr;
1112 pid->interpolate_extra_data = nullptr;
1113
1116
1117 pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_ROTATION);
1118 pid->info_types = 0;
1119
1120 pid->stack_index = pid->cache->index;
1121
1122 pid->default_step = 1;
1123 pid->max_step = 1;
1125}
1126
1128{
1129 PTCacheID result = {nullptr};
1130
1131 ListBaseT<PTCacheID> pidlist;
1132 BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
1133
1134 for (PTCacheID &pid : pidlist) {
1135 if (pid.cache == cache) {
1136 result = pid;
1137 break;
1138 }
1139 }
1140
1141 pidlist.free_no_destruct();
1142
1143 return result;
1144}
1145
1146/* Callback which is used by point cache foreach() family of functions.
1147 *
1148 * Receives ID of the point cache.
1149 *
1150 * NOTE: This ID is owned by foreach() routines and can not be used outside of
1151 * the foreach loop. This means that if one wants to store them those are to be
1152 * malloced and copied over.
1153 *
1154 * If the function returns false, then foreach() loop aborts.
1155 */
1156
1158{
1159 PTCacheID pid;
1160 for (ParticleSystem *psys = object->particlesystem.first(); psys != nullptr; psys = psys->next) {
1161 if (psys->part == nullptr) {
1162 continue;
1163 }
1164 /* Check to make sure point cache is actually used by the particles. */
1165 if (ELEM(psys->part->phystype, PART_PHYS_NO, PART_PHYS_KEYED)) {
1166 continue;
1167 }
1168/* Hair needs to be included in id-list for cache edit mode to work. */
1169#if 0
1170 if ((psys->part->type == PART_HAIR) && (psys->flag & PSYS_HAIR_DYNAMICS) == 0) {
1171 continue;
1172 }
1173#endif
1174 if (psys->part->type == PART_FLUID) {
1175 continue;
1176 }
1177 BKE_ptcache_id_from_particles(&pid, object, psys);
1178 /* TODO figure out the particle modifier for this system and put it here. */
1179 if (!callback(pid, nullptr)) {
1180 return false;
1181 }
1182 }
1183 return true;
1184}
1185
1187{
1188 PTCacheID pid;
1189 for (ModifierData *md = object->modifiers.first(); md != nullptr; md = md->next) {
1190 if (md->type == eModifierType_Cloth) {
1191 BKE_ptcache_id_from_cloth(&pid, object, reinterpret_cast<ClothModifierData *>(md));
1192 if (!callback(pid, md)) {
1193 return false;
1194 }
1195 }
1196 else if (md->type == eModifierType_Fluid) {
1197 FluidModifierData *fmd = reinterpret_cast<FluidModifierData *>(md);
1198 if (fmd->type & MOD_FLUID_TYPE_DOMAIN) {
1199 BKE_ptcache_id_from_smoke(&pid, object, reinterpret_cast<FluidModifierData *>(md));
1200 if (!callback(pid, md)) {
1201 return false;
1202 }
1203 }
1204 }
1205 else if (md->type == eModifierType_DynamicPaint) {
1206 DynamicPaintModifierData *pmd = reinterpret_cast<DynamicPaintModifierData *>(md);
1207 if (pmd->canvas) {
1208 DynamicPaintSurface *surface = pmd->canvas->surfaces.first();
1209 for (; surface; surface = surface->next) {
1210 BKE_ptcache_id_from_dynamicpaint(&pid, object, surface);
1211 if (!callback(pid, md)) {
1212 return false;
1213 }
1214 }
1215 }
1216 }
1217 }
1218 return true;
1219}
1220
1221/* Return false if any of callbacks returned false. */
1223 Object *object,
1224 int duplis,
1225 PointCacheIdFn callback)
1226{
1227 PTCacheID pid;
1228
1229 if (object != nullptr) {
1230 /* Soft body. */
1231 if (object->soft != nullptr) {
1232 BKE_ptcache_id_from_softbody(&pid, object, object->soft);
1233 if (!callback(pid, nullptr)) {
1234 return false;
1235 }
1236 }
1237 /* Particle systems. */
1238 if (!foreach_object_particle_ptcache(object, callback)) {
1239 return false;
1240 }
1241 /* Modifiers. */
1242 if (!foreach_object_modifier_ptcache(object, callback)) {
1243 return false;
1244 }
1245 /* Consider all object in dupli-groups to be part of the same object,
1246 * for baking with linking dupli-groups. Once we have better overrides
1247 * this can be revisited so users select the local objects directly. */
1248 if (scene != nullptr && (duplis-- > 0) && (object->instance_collection != nullptr)) {
1249 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (object->instance_collection, current_object) {
1250 if (current_object == object) {
1251 continue;
1252 }
1253 foreach_object_ptcache(scene, current_object, duplis, callback);
1254 }
1256 }
1257 }
1258
1259 /* Rigid body. */
1260 if (scene != nullptr && (object == nullptr || object->rigidbody_object != nullptr) &&
1261 scene->rigidbody_world != nullptr)
1262 {
1263 BKE_ptcache_id_from_rigidbody(&pid, object, scene->rigidbody_world);
1264 if (!callback(pid, nullptr)) {
1265 return false;
1266 }
1267 }
1268 return true;
1269}
1270
1271void BKE_ptcache_ids_from_object(ListBaseT<PTCacheID> *lb, Object *ob, Scene *scene, int duplis)
1272{
1273 lb->first_ = lb->last_ = nullptr;
1274 foreach_object_ptcache(scene, ob, duplis, [&](PTCacheID &pid, ModifierData * /*md*/) -> bool {
1275 PTCacheID *own_pid = MEM_new_uninitialized<PTCacheID>("PTCacheID");
1276 *own_pid = pid;
1277 BLI_addtail(lb, own_pid);
1278 return true;
1279 });
1280}
1281
1283{
1284 foreach_object_ptcache(&scene, &ob, duplis, fn);
1285}
1286
1287bool BKE_ptcache_object_has(Scene *scene, Object *ob, int duplis)
1288{
1289 bool has_point_cache = false;
1290 foreach_object_ptcache(scene, ob, duplis, [&](PTCacheID & /*pid*/, ModifierData * /*md*/) {
1291 has_point_cache = true;
1292 return false;
1293 });
1294 return has_point_cache;
1295}
1296
1297/* File handling */
1298
1299static const char *ptcache_file_extension(const PTCacheID *pid)
1300{
1301 switch (pid->file_type) {
1302 default:
1304 return PTCACHE_EXT;
1305 }
1306}
1307
1311static int ptcache_frame_from_filename(const char *filename, const char *ext)
1312{
1313 const int frame_len = 6;
1314 const int ext_len = frame_len + strlen(ext);
1315 const int len = strlen(filename);
1316
1317 /* could crash if trying to copy a string out of this range */
1318 if (len > ext_len) {
1319 /* Using frame_len here gives compile error (VLA). */
1320 char num[/*frame_len*/ 6 + 1];
1321 STRNCPY(num, filename + len - ext_len);
1322
1323 return atoi(num);
1324 }
1325
1326 return -1;
1327}
1328
1329/* Takes an Object ID and returns a unique name
1330 * - id: object id
1331 * - cfra: frame for the cache, can be negative
1332 * - stack_index: index in the modifier stack. we can have cache for more than one stack_index
1333 */
1334
1335#define MAX_PTCACHE_PATH FILE_MAX
1336#define MAX_PTCACHE_FILE (FILE_MAX * 2)
1337
1339{
1340 const char *blendfile_path = BKE_main_blendfile_path_from_global();
1341 Library *lib = (pid->owner_id) ? pid->owner_id->lib : nullptr;
1342 const char *blendfile_path_lib = (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH) == 0) ?
1343 lib->runtime->filepath_abs :
1344 blendfile_path;
1345
1346 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1348
1349 if (BLI_path_is_rel(dirname)) {
1350 BLI_path_abs(dirname, blendfile_path_lib);
1351 }
1352
1353 return BLI_path_slash_ensure(dirname, MAX_PTCACHE_PATH); /* new strlen() */
1354 }
1355 if ((blendfile_path[0] != '\0') || lib) {
1356 char file[MAX_PTCACHE_PATH]; /* we don't want the dir, only the file */
1357
1358 BLI_path_split_file_part(blendfile_path_lib, file, sizeof(file));
1359 /* Remove the `.blend` extension. */
1361
1362 /* Add blend file name to pointcache dir. */
1364
1365 BLI_path_abs(dirname, blendfile_path_lib);
1366 return BLI_path_slash_ensure(dirname, MAX_PTCACHE_PATH); /* new strlen() */
1367 }
1368
1369 /* use the temp path. this is weak but better than not using point cache at all */
1370 /* temporary directory is assumed to exist and ALWAYS has a trailing slash */
1372
1373 return BLI_path_slash_ensure(dirname, MAX_PTCACHE_PATH); /* new strlen() */
1374}
1375
1377 char filepath[MAX_PTCACHE_FILE],
1378 const size_t filepath_len,
1379 const bool use_frame_number,
1380 const int cfra)
1381{
1382 size_t len = filepath_len;
1383 char *filename_ext;
1384 filename_ext = filepath + filepath_len;
1385 *filename_ext = '\0';
1386
1387 /* PointCaches are inserted in object's list on demand, we need a valid index now. */
1388 if (pid->cache->index < 0) {
1389 BLI_assert(GS(pid->owner_id->name) == ID_OB);
1391 id_cast<Object *>(pid->owner_id));
1392 }
1393
1394 const char *ext = ptcache_file_extension(pid);
1395 if (use_frame_number) {
1396 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1397 if (pid->cache->index >= 0) {
1399 filename_ext, MAX_PTCACHE_FILE - len, "_%06d_%02u%s", cfra, pid->stack_index, ext);
1400 }
1401 else {
1402 len += BLI_snprintf_rlen(filename_ext, MAX_PTCACHE_FILE - len, "_%06d%s", cfra, ext);
1403 }
1404 }
1405 else {
1407 filename_ext, MAX_PTCACHE_FILE - len, "_%06d_%02u%s", cfra, pid->stack_index, ext);
1408 }
1409 }
1410 else {
1411 if (pid->cache->flag & PTCACHE_EXTERNAL) {
1412 if (pid->cache->index >= 0) {
1414 filename_ext, MAX_PTCACHE_FILE - len, "_%02u%s", pid->stack_index, ext);
1415 }
1416 else {
1417 len += BLI_strncpy_rlen(filename_ext, ext, MAX_PTCACHE_FILE - len);
1418 }
1419 }
1420 else {
1422 filename_ext, MAX_PTCACHE_FILE - len, "_%02u%s", pid->stack_index, ext);
1423 }
1424 }
1425
1426 return len;
1427}
1428
1430 char filepath[MAX_PTCACHE_FILE],
1431 int cfra,
1432 const bool do_path,
1433 const bool do_ext)
1434{
1435 int len = 0;
1436 const char *idname;
1437 char *newname;
1438 filepath[0] = '\0';
1439 newname = filepath;
1440
1441 if ((pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
1442 const char *blendfile_path = BKE_main_blendfile_path_from_global();
1443 if (blendfile_path[0] == '\0') {
1444 return 0; /* save blend file before using disk pointcache */
1445 }
1446 }
1447
1448 /* start with temp dir */
1449 if (do_path) {
1450 len = BKE_ptcache_path(pid, filepath);
1451 newname += len;
1452 }
1453 if (pid->cache->name[0] == '\0' && (pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
1454 idname = (pid->owner_id->name + 2);
1455 /* convert chars to hex so they are always a valid filename */
1456 while ('\0' != *idname) {
1457 /* Always 2 unless there isn't enough room in the string. */
1458 const int temp = BLI_snprintf_rlen(newname, MAX_PTCACHE_FILE - len, "%02X", uint(*idname++));
1459 newname += temp;
1460 len += temp;
1461 }
1462 }
1463 else {
1464 int temp = BLI_strncpy_rlen(newname, pid->cache->name, MAX_PTCACHE_FILE - len);
1465 newname += temp;
1466 len += temp;
1467 }
1468 UNUSED_VARS(newname);
1469
1470 if (do_ext) {
1471 len += ptcache_filepath_ext_append(pid, filepath, size_t(len), true, cfra);
1472 }
1473
1474 return len; /* make sure the above string is always 16 chars */
1475}
1476
1480static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
1481{
1482 PTCacheFile *pf;
1483 FILE *fp = nullptr;
1484 char filepath[MAX_PTCACHE_FILE];
1485
1486#ifndef DURIAN_POINTCACHE_LIB_OK
1487 /* don't allow writing for linked objects */
1488 if (pid->owner_id->lib && mode == PTCACHE_FILE_WRITE) {
1489 return nullptr;
1490 }
1491#endif
1492 if ((pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
1493 const char *blendfile_path = BKE_main_blendfile_path_from_global();
1494 if (blendfile_path[0] == '\0') {
1495 return nullptr; /* save blend file before using disk pointcache */
1496 }
1497 }
1498
1499 ptcache_filepath(pid, filepath, cfra, true, true);
1500
1501 if (mode == PTCACHE_FILE_READ) {
1502 fp = BLI_fopen(filepath, "rb");
1503 }
1504 else if (mode == PTCACHE_FILE_WRITE) {
1506 fp = BLI_fopen(filepath, "wb");
1507 }
1508 else if (mode == PTCACHE_FILE_UPDATE) {
1510 fp = BLI_fopen(filepath, "rb+");
1511 }
1512
1513 if (!fp) {
1514 return nullptr;
1515 }
1516
1517 pf = MEM_new<PTCacheFile>("PTCacheFile");
1518 pf->fp = fp;
1519 pf->old_format = 0;
1520 pf->frame = cfra;
1521
1522 return pf;
1523}
1525{
1526 if (pf) {
1527 fclose(pf->fp);
1528 MEM_delete(pf);
1529 }
1530}
1531
1533 uchar *result,
1534 const size_t items_num,
1535 const size_t item_size)
1536{
1537 const size_t out_len = items_num * item_size;
1538
1539 uchar compressed_val = 0;
1540 if (!ptcache_file_read(pf, &compressed_val, 1, sizeof(uchar))) {
1541 return false;
1542 }
1543 const PointCacheCompression compressed = PointCacheCompression(compressed_val);
1544
1545 if (compressed == PTCACHE_COMPRESS_NO) {
1546 return ptcache_file_read(pf, result, out_len, sizeof(uchar));
1547 }
1548
1549 if (!ELEM(compressed,
1553 {
1554 /* We are trying to read an unsupported compression format. */
1555 return false;
1556 }
1557
1558 uint in_len;
1559 if (!ptcache_file_read(pf, &in_len, 1, sizeof(uint))) {
1560 return false;
1561 }
1562 if (in_len == 0) {
1563 return out_len == 0;
1564 }
1565 uchar *in = MEM_new_array_uninitialized<uchar>(in_len, "pointcache_compressed_buffer");
1566 if (in == nullptr) {
1567 return false;
1568 }
1569 if (!ptcache_file_read(pf, in, in_len, sizeof(uchar))) {
1570 MEM_delete(in);
1571 return false;
1572 }
1573
1574 /* Require the decompressed size to match, so no part of the result is left unwritten. */
1575 if (compressed != PTCACHE_COMPRESS_ZSTD_FILTERED) {
1576 const bool decompressed = ZSTD_decompress(result, out_len, in, in_len) == out_len;
1577 MEM_delete(in);
1578 return decompressed;
1579 }
1580
1581 /* Un-filter the decompressed data. */
1582 uchar *decomp_result = MEM_new_array_uninitialized<uchar>(out_len, "pointcache_unfilter_buffer");
1583 const bool decompressed = decomp_result &&
1584 ZSTD_decompress(decomp_result, out_len, in, in_len) == out_len;
1585 if (decompressed) {
1586 unfilter_transpose_delta(decomp_result, result, items_num, item_size);
1587 }
1588 MEM_SAFE_DELETE(decomp_result);
1589 MEM_delete(in);
1590
1591 return decompressed;
1592}
1593
1595 const void *data,
1596 size_t items_num,
1597 size_t item_size)
1598{
1599 /* Allocate space for compressed data. */
1601 const size_t data_size = items_num * item_size;
1602 size_t out_len = ZSTD_compressBound(data_size);
1603 Array<uchar> out(out_len);
1604
1605 /* Filter the data: transpose by bytes; delta-encode. */
1606 Array<uchar> filtered(data_size);
1608 static_cast<const uint8_t *>(data), filtered.data(), items_num, item_size);
1609
1610 /* Do compression: always zstd level 3. */
1611 const int zstd_level = 3;
1612 size_t res = ZSTD_compress(out.data(), out_len, filtered.data(), data_size, zstd_level);
1613 out_len = res;
1614
1615 /* Write to file. */
1616 bool ok = true;
1617 const uchar compression_val = compression;
1618 ok &= ptcache_file_write(pf, &compression_val, 1, sizeof(uchar));
1619 uint size = out_len;
1620 ok &= ptcache_file_write(pf, &size, 1, sizeof(uint));
1621 ok &= ptcache_file_write(pf, out.data(), out_len, sizeof(uchar));
1622 return ok;
1623}
1624
1625static bool ptcache_file_read(PTCacheFile *pf, void *f, size_t items_num, size_t item_size)
1626{
1627 return (fread(f, item_size, items_num, pf->fp) == items_num);
1628}
1630 const void *data,
1631 size_t items_num,
1632 size_t item_size)
1633{
1634 return (fwrite(data, item_size, items_num, pf->fp) == items_num);
1635}
1637{
1638 uint typeflag = 0;
1639 int error = 0;
1640 char bphysics[8];
1641
1642 pf->data_types = 0;
1643
1644 if (fread(bphysics, sizeof(char), 8, pf->fp) != 8) {
1645 error = 1;
1646 }
1647
1648 if (!error && !STREQLEN(bphysics, "BPHYSICS", 8)) {
1649 error = 1;
1650 }
1651
1652 if (!error && !fread(&typeflag, sizeof(uint), 1, pf->fp)) {
1653 error = 1;
1654 }
1655
1656 pf->type = (typeflag & PTCACHE_TYPEFLAG_TYPEMASK);
1657 pf->flag = (typeflag & PTCACHE_TYPEFLAG_FLAGMASK);
1658
1659 /* if there was an error set file as it was */
1660 if (error) {
1661 BLI_fseek(pf->fp, 0, SEEK_SET);
1662 }
1663
1664 return !error;
1665}
1667{
1668 const char *bphysics = "BPHYSICS";
1669 uint typeflag = pf->type + pf->flag;
1670
1671 if (fwrite(bphysics, sizeof(char), 8, pf->fp) != 8) {
1672 return 0;
1673 }
1674
1675 if (!fwrite(&typeflag, sizeof(uint), 1, pf->fp)) {
1676 return 0;
1677 }
1678
1679 return 1;
1680}
1681
1682/* Data pointer handling. */
1683
1684int BKE_ptcache_data_size(int data_type)
1685{
1686 return ptcache_data_size[data_type];
1687}
1688
1690{
1691 int data_types = pf->data_types;
1692
1693 pf->cur[BPHYS_DATA_INDEX] = (data_types & (1 << BPHYS_DATA_INDEX)) ? &pf->data.index : nullptr;
1694 pf->cur[BPHYS_DATA_LOCATION] = (data_types & (1 << BPHYS_DATA_LOCATION)) ? &pf->data.loc :
1695 nullptr;
1696 pf->cur[BPHYS_DATA_VELOCITY] = (data_types & (1 << BPHYS_DATA_VELOCITY)) ? &pf->data.vel :
1697 nullptr;
1698 pf->cur[BPHYS_DATA_ROTATION] = (data_types & (1 << BPHYS_DATA_ROTATION)) ? &pf->data.rot :
1699 nullptr;
1700 pf->cur[BPHYS_DATA_AVELOCITY] = (data_types & (1 << BPHYS_DATA_AVELOCITY)) ? &pf->data.ave :
1701 nullptr;
1702 pf->cur[BPHYS_DATA_SIZE] = (data_types & (1 << BPHYS_DATA_SIZE)) ? &pf->data.size : nullptr;
1703 pf->cur[BPHYS_DATA_TIMES] = (data_types & (1 << BPHYS_DATA_TIMES)) ? &pf->data.times : nullptr;
1704 pf->cur[BPHYS_DATA_BOIDS] = (data_types & (1 << BPHYS_DATA_BOIDS)) ? &pf->data.boids : nullptr;
1705}
1706
1708{
1709 if (pm->totpoint > 0 && pm->data[BPHYS_DATA_INDEX]) {
1710 uint *data = static_cast<uint *>(pm->data[BPHYS_DATA_INDEX]);
1711 uint mid, low = 0, high = pm->totpoint - 1;
1712
1713 if (index < *data || index > *(data + high)) {
1714 return -1;
1715 }
1716
1717 /* check simple case for continuous indexes first */
1718 if (index - *data < high && data[index - *data] == index) {
1719 return index - *data;
1720 }
1721
1722 while (low <= high) {
1723 mid = (low + high) / 2;
1724
1725 if (data[mid] > index) {
1726 high = mid - 1;
1727 }
1728 else if (data[mid] < index) {
1729 low = mid + 1;
1730 }
1731 else {
1732 return mid;
1733 }
1734 }
1735
1736 return -1;
1737 }
1738
1739 return (index < pm->totpoint ? index : -1);
1740}
1741
1743{
1744 int data_types = pm->data_types;
1745 int i;
1746
1747 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1748 cur[i] = ((data_types & (1 << i)) ? pm->data[i] : nullptr);
1749 }
1750}
1751
1753{
1754 int i;
1755
1756 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1757 if (cur[i]) {
1758 cur[i] = static_cast<char *>(cur[i]) + ptcache_data_size[i];
1759 }
1760 }
1761}
1762int BKE_ptcache_mem_pointers_seek(int point_index, PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
1763{
1764 int data_types = pm->data_types;
1765 int i, index = BKE_ptcache_mem_index_find(pm, point_index);
1766
1767 if (index < 0) {
1768 /* Can't give proper location without reallocation, so don't give any location.
1769 * Some points will be cached improperly, but this only happens with simulation
1770 * steps bigger than cache->step, so the cache has to be recalculated anyway at some point. */
1771 return 0;
1772 }
1773
1774 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1775 cur[i] = (data_types & (1 << i)) ?
1776 static_cast<char *>(pm->data[i]) + index * ptcache_data_size[i] :
1777 nullptr;
1778 }
1779
1780 return 1;
1781}
1782static bool ptcache_data_alloc(PTCacheMem *pm, const bool zero_init)
1783{
1784 const uint data_types = pm->data_types;
1785
1786 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
1787 if (data_types & (1 << i)) {
1788 pm->data[i] = zero_init ?
1789 MEM_new_array_zeroed(pm->totpoint, ptcache_data_size[i], "PTCache Data") :
1791 pm->totpoint, ptcache_data_size[i], "PTCache Data");
1792 if (pm->data[i] == nullptr) {
1793 return false;
1794 }
1795 }
1796 }
1797
1798 return true;
1799}
1801{
1802 void **data = pm->data;
1803 int i;
1804
1805 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1806 if (data[i]) {
1808 }
1809 }
1810}
1811
1813{
1814 PTCacheExtra *extra = pm->extradata.first();
1815
1816 if (extra) {
1817 for (; extra; extra = extra->next) {
1818 if (extra->data) {
1819 MEM_delete_void(extra->data);
1820 }
1821 }
1822
1823 pm->extradata.free_no_destruct();
1824 }
1825}
1826
1828{
1831}
1832
1834{
1835 if (pid->type == PTCACHE_TYPE_SOFTBODY) {
1836 return sizeof(float[6]);
1837 }
1838 if (pid->type == PTCACHE_TYPE_PARTICLES) {
1839 return sizeof(ParticleKey);
1840 }
1841 if (pid->type == PTCACHE_TYPE_CLOTH) {
1842 return sizeof(float[9]);
1843 }
1844
1845 return 0;
1846}
1847
1848static void ptcache_find_frames_around(PTCacheID *pid, uint frame, int *r_fra1, int *r_fra2)
1849{
1850 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
1851 int cfra1 = frame, cfra2 = frame + 1;
1852
1853 while (cfra1 >= pid->cache->startframe && !BKE_ptcache_id_exist(pid, cfra1)) {
1854 cfra1--;
1855 }
1856
1857 if (cfra1 < pid->cache->startframe) {
1858 cfra1 = 0;
1859 }
1860
1861 while (cfra2 <= pid->cache->endframe && !BKE_ptcache_id_exist(pid, cfra2)) {
1862 cfra2++;
1863 }
1864
1865 if (cfra2 > pid->cache->endframe) {
1866 cfra2 = 0;
1867 }
1868
1869 if (cfra1 && !cfra2) {
1870 *r_fra1 = 0;
1871 *r_fra2 = cfra1;
1872 }
1873 else {
1874 *r_fra1 = cfra1;
1875 *r_fra2 = cfra2;
1876 }
1877 }
1878 else if (pid->cache->mem_cache.first()) {
1879 PTCacheMem *pm = pid->cache->mem_cache.first();
1880 PTCacheMem *pm2 = pid->cache->mem_cache.last();
1881
1882 while (pm->next && pm->next->frame <= frame) {
1883 pm = pm->next;
1884 }
1885
1886 if (pm2->frame < frame) {
1887 pm2 = nullptr;
1888 }
1889 else {
1890 while (pm2->prev && pm2->prev->frame > frame) {
1891 pm2 = pm2->prev;
1892 }
1893 }
1894
1895 if (!pm2) {
1896 *r_fra1 = 0;
1897 *r_fra2 = pm->frame;
1898 }
1899 else {
1900 *r_fra1 = pm->frame;
1901 *r_fra2 = pm2->frame;
1902 }
1903 }
1904}
1905
1906/* Read uncompressed point data, which is stored point by point. */
1908{
1909 void *cur[BPHYS_TOT_DATA];
1911
1912 for (uint i = 0; i < pm->totpoint; i++) {
1913 for (int j = 0; j < BPHYS_TOT_DATA; j++) {
1914 if ((pm->data_types & (1 << j)) && !ptcache_file_read(pf, cur[j], 1, ptcache_data_size[j])) {
1915 return false;
1916 }
1917 }
1919 }
1920
1921 return true;
1922}
1923
1925{
1927 PTCacheMem *pm = nullptr;
1928 uint i, error = 0;
1929
1930 if (pf == nullptr) {
1931 return nullptr;
1932 }
1933
1935 error = 1;
1936 }
1937
1938 if (!error && (pf->type != pid->type || !pid->read_header(pf))) {
1939 error = 1;
1940 }
1941
1942 if (!error) {
1943 pm = MEM_new<PTCacheMem>("Pointcache mem");
1944
1945 pm->totpoint = pf->totpoint;
1946 pm->data_types = pf->data_types;
1947 pm->frame = pf->frame;
1948
1949 if (!ptcache_data_alloc(pm, false)) {
1950 error = 1;
1951 }
1952 else if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
1953 for (i = 0; !error && i < BPHYS_TOT_DATA; i++) {
1954 if (pf->data_types & (1 << i)) {
1956 pf, static_cast<uchar *>(pm->data[i]), pm->totpoint, ptcache_data_size[i]);
1957 }
1958 }
1959 }
1960 else {
1961 error = !ptcache_file_points_read(pf, pm);
1962 }
1963 }
1964
1965 if (!error && pf->flag & PTCACHE_TYPEFLAG_EXTRADATA) {
1966 uint extratype = 0;
1967
1968 while (!error && ptcache_file_read(pf, &extratype, 1, sizeof(uint))) {
1970 error = 1;
1971 break;
1972 }
1973
1974 const size_t item_size = ptcache_extra_datasize[extratype];
1975
1976 uint totdata;
1977 /* Extra data is used with int counts, reject larger counts from corrupt files. */
1978 if (!ptcache_file_read(pf, &totdata, 1, sizeof(uint)) || totdata > INT_MAX) {
1979 error = 1;
1980 break;
1981 }
1982
1983 void *data = MEM_new_array_uninitialized(totdata, item_size, "Pointcache extradata->data");
1984 if (data == nullptr) {
1985 error = 1;
1986 break;
1987 }
1988
1989 PTCacheExtra *extra = MEM_new<PTCacheExtra>("Pointcache extradata");
1990
1991 extra->type = ePointCache_ExtraDataType(extratype);
1992 extra->totdata = totdata;
1993 extra->data = data;
1994
1995 if (pf->flag & PTCACHE_TYPEFLAG_COMPRESS) {
1997 pf, static_cast<uchar *>(extra->data), extra->totdata, item_size);
1998 }
1999 else {
2000 error = !ptcache_file_read(pf, extra->data, extra->totdata, item_size);
2001 }
2002
2003 BLI_addtail(&pm->extradata, extra);
2004 }
2005 }
2006
2007 if (error && pm) {
2009 MEM_delete(pm);
2010 pm = nullptr;
2011 }
2012
2014
2015 if (error && G.debug & G_DEBUG) {
2016 printf("Error reading from disk cache\n");
2017 }
2018
2019 return pm;
2020}
2022{
2023 PTCacheFile *pf = nullptr;
2024 bool ok = true;
2025 uint i;
2026
2028
2030
2031 if (pf == nullptr) {
2032 if (G.debug & G_DEBUG) {
2033 printf("Error opening disk cache file for writing\n");
2034 }
2035 return 0;
2036 }
2037
2038 pf->data_types = pm->data_types;
2039 pf->totpoint = pm->totpoint;
2040 pf->type = pid->type;
2041 pf->flag = 0;
2042
2043 if (pm->extradata.first()) {
2045 }
2046
2048
2050 ok = false;
2051 }
2052
2053 if (ok) {
2054 for (i = 0; i < BPHYS_TOT_DATA; i++) {
2055 if (pm->data[i]) {
2057 }
2058 }
2059 }
2060
2061 if (ok && pm->extradata.first()) {
2062 PTCacheExtra *extra = pm->extradata.first();
2063
2064 for (; extra; extra = extra->next) {
2065 if (extra->data == nullptr || extra->totdata == 0) {
2066 continue;
2067 }
2068
2069 const uint extratype = uint(extra->type);
2070 ok &= ptcache_file_write(pf, &extratype, 1, sizeof(uint));
2071 ok &= ptcache_file_write(pf, &extra->totdata, 1, sizeof(uint));
2072
2074 pf, extra->data, extra->totdata, ptcache_extra_datasize[extra->type]);
2075 }
2076 }
2077
2079
2080 if (!ok && G.debug & G_DEBUG) {
2081 CLOG_ERROR(&LOG, "Error writing to disk cache");
2082 }
2083
2084 return ok;
2085}
2086
2087static int ptcache_read_stream(PTCacheID *pid, int cfra)
2088{
2090 int error = 0;
2091
2092 if (pid->read_stream == nullptr) {
2093 return 0;
2094 }
2095
2096 if (pf == nullptr) {
2097 if (G.debug & G_DEBUG) {
2098 printf("Error opening disk cache file for reading\n");
2099 }
2100 return 0;
2101 }
2102
2104 pid->error(pid->owner_id, pid->calldata, "Failed to read point cache file");
2105 error = 1;
2106 }
2107 else if (pf->type != pid->type) {
2108 pid->error(pid->owner_id, pid->calldata, "Point cache file has wrong type");
2109 error = 1;
2110 }
2111 else if (!pid->read_header(pf)) {
2112 pid->error(pid->owner_id, pid->calldata, "Failed to read point cache file header");
2113 error = 1;
2114 }
2115 else if (pf->totpoint != pid->totpoint(pid->calldata, cfra)) {
2116 pid->error(pid->owner_id, pid->calldata, "Number of points in cache does not match mesh");
2117 error = 1;
2118 }
2119
2120 if (!error) {
2122
2123 /* We have stream reading here. */
2124 if (!pid->read_stream(pf, pid->calldata)) {
2125 pid->error(pid->owner_id, pid->calldata, "Failed to read point cache file data");
2126 error = 1;
2127 }
2128 }
2129
2131
2132 return error == 0;
2133}
2134
2135static int ptcache_read(PTCacheID *pid, int cfra)
2136{
2137 PTCacheMem *pm = nullptr;
2138 int i;
2139 int *index = &i;
2140
2141 /* get a memory cache to read from */
2142 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2143 pm = ptcache_disk_frame_to_mem(pid, cfra);
2144 }
2145 else {
2146 pm = pid->cache->mem_cache.first();
2147
2148 while (pm && pm->frame != cfra) {
2149 pm = pm->next;
2150 }
2151 }
2152
2153 /* read the cache */
2154 if (pm) {
2155 int totpoint = pm->totpoint;
2156
2157 if ((pid->data_types & (1 << BPHYS_DATA_INDEX)) == 0) {
2158 int pid_totpoint = pid->totpoint(pid->calldata, cfra);
2159
2160 if (totpoint != pid_totpoint) {
2161 pid->error(pid->owner_id, pid->calldata, "Number of points in cache does not match mesh");
2162 totpoint = std::min(totpoint, pid_totpoint);
2163 }
2164 }
2165
2166 /* Ignore stored indices if this cache type does not use them, to
2167 * guard against unchecked use of them for corrupt files. */
2168 const bool use_index = (pm->data_types & (1 << BPHYS_DATA_INDEX)) &&
2169 (pid->data_types & (1 << BPHYS_DATA_INDEX));
2170
2171 void *cur[BPHYS_TOT_DATA];
2173
2174 for (i = 0; i < totpoint; i++) {
2175 if (use_index) {
2176 index = static_cast<int *>(cur[BPHYS_DATA_INDEX]);
2177 }
2178
2179 pid->read_point(*index, pid->calldata, cur, float(pm->frame), nullptr);
2180
2182 }
2183
2184 if (pid->read_extra_data && pm->extradata.first()) {
2185 pid->read_extra_data(pid->calldata, pm, float(pm->frame));
2186 }
2187
2188 /* clean up temporary memory cache */
2189 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2191 MEM_delete(pm);
2192 }
2193 }
2194
2195 return 1;
2196}
2197static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
2198{
2199 PTCacheMem *pm = nullptr;
2200 int i;
2201 int *index = &i;
2202
2203 /* get a memory cache to read from */
2204 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2205 pm = ptcache_disk_frame_to_mem(pid, cfra2);
2206 }
2207 else {
2208 pm = pid->cache->mem_cache.first();
2209
2210 while (pm && pm->frame != cfra2) {
2211 pm = pm->next;
2212 }
2213 }
2214
2215 /* read the cache */
2216 if (pm) {
2217 int totpoint = pm->totpoint;
2218
2219 if ((pid->data_types & (1 << BPHYS_DATA_INDEX)) == 0) {
2220 int pid_totpoint = pid->totpoint(pid->calldata, int(cfra));
2221
2222 if (totpoint != pid_totpoint) {
2223 pid->error(pid->owner_id, pid->calldata, "Number of points in cache does not match mesh");
2224 totpoint = std::min(totpoint, pid_totpoint);
2225 }
2226 }
2227
2228 /* Ignore stored indices if this cache type does not use them, to
2229 * guard against unchecked use of them for corrupt files. */
2230 const bool use_index = (pm->data_types & (1 << BPHYS_DATA_INDEX)) &&
2231 (pid->data_types & (1 << BPHYS_DATA_INDEX));
2232
2233 void *cur[BPHYS_TOT_DATA];
2235
2236 for (i = 0; i < totpoint; i++) {
2237 if (use_index) {
2238 index = static_cast<int *>(cur[BPHYS_DATA_INDEX]);
2239 }
2240
2241 pid->interpolate_point(
2242 *index, pid->calldata, cur, cfra, float(cfra1), float(cfra2), nullptr);
2244 }
2245
2246 if (pid->interpolate_extra_data && pm->extradata.first()) {
2247 pid->interpolate_extra_data(pid->calldata, pm, cfra, float(cfra1), float(cfra2));
2248 }
2249
2250 /* clean up temporary memory cache */
2251 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2253 MEM_delete(pm);
2254 }
2255 }
2256
2257 return 1;
2258}
2259
2260int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
2261{
2262 int cfrai = int(floor(cfra)), cfra1 = 0, cfra2 = 0;
2263 int ret = 0;
2264
2265 /* nothing to read to */
2266 if (pid->totpoint(pid->calldata, cfrai) == 0) {
2267 return 0;
2268 }
2269
2270 if (pid->cache->flag & PTCACHE_READ_INFO) {
2271 pid->cache->flag &= ~PTCACHE_READ_INFO;
2272 ptcache_read(pid, 0);
2273 }
2274
2275 /* first check if we have the actual frame cached */
2276 if (cfra == float(cfrai) && BKE_ptcache_id_exist(pid, cfrai)) {
2277 cfra1 = cfrai;
2278 }
2279
2280 /* no exact cache frame found so try to find cached frames around cfra */
2281 if (cfra1 == 0) {
2282 ptcache_find_frames_around(pid, cfrai, &cfra1, &cfra2);
2283 }
2284
2285 if (cfra1 == 0 && cfra2 == 0) {
2286 return 0;
2287 }
2288
2289 /* don't read old cache if already simulated past cached frame */
2290 if (no_extrapolate_old) {
2291 if (cfra1 == 0 && cfra2 && cfra2 <= pid->cache->simframe) {
2292 return 0;
2293 }
2294 if (cfra1 && cfra1 == cfra2) {
2295 return 0;
2296 }
2297 }
2298 else {
2299 /* avoid calling interpolate between the same frame values */
2300 if (cfra1 && cfra1 == cfra2) {
2301 cfra1 = 0;
2302 }
2303 }
2304
2305 if (cfra1) {
2306 if (pid->read_stream) {
2307 if (!ptcache_read_stream(pid, cfra1)) {
2308 return 0;
2309 }
2310 }
2311 else if (pid->read_point) {
2312 ptcache_read(pid, cfra1);
2313 }
2314 }
2315
2316 if (cfra2) {
2317 if (pid->read_stream) {
2318 if (!ptcache_read_stream(pid, cfra2)) {
2319 return 0;
2320 }
2321 }
2322 else if (pid->read_point) {
2323 if (cfra1 && cfra2 && pid->interpolate_point) {
2324 ptcache_interpolate(pid, cfra, cfra1, cfra2);
2325 }
2326 else {
2327 ptcache_read(pid, cfra2);
2328 }
2329 }
2330 }
2331
2332 if (cfra1) {
2334 }
2335 else if (cfra2) {
2337 pid->cache->simframe = cfra2;
2338 }
2339
2340 cfrai = int(cfra);
2341 /* clear invalid cache frames so that better stuff can be simulated */
2342 if (pid->cache->flag & PTCACHE_OUTDATED) {
2344 }
2345 else if (pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
2346 if (cfra <= pid->cache->last_exact) {
2348 }
2349
2350 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, std::max(cfrai, pid->cache->last_exact));
2351 }
2352
2353 return ret;
2354}
2355static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
2356{
2357 PTCacheFile *pf = nullptr;
2358 int error = 0;
2359
2361
2363
2364 if (pf == nullptr) {
2365 if (G.debug & G_DEBUG) {
2366 printf("Error opening disk cache file for writing\n");
2367 }
2368 return 0;
2369 }
2370
2371 pf->data_types = pid->data_types;
2372 pf->totpoint = totpoint;
2373 pf->type = pid->type;
2374 pf->flag = 0;
2375
2376 if (!error && (!ptcache_file_header_begin_write(pf) || !pid->write_header(pf))) {
2377 error = 1;
2378 }
2379
2380 if (!error && pid->write_stream) {
2381 pid->write_stream(pf, pid->calldata);
2382 }
2383
2385
2386 if (error && G.debug & G_DEBUG) {
2387 printf("Error writing to disk cache\n");
2388 }
2389
2390 return error == 0;
2391}
2392
2393static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
2394{
2395 PointCache *cache = pid->cache;
2396 PTCacheMem *pm = nullptr, *pm2 = nullptr;
2397 int totpoint = pid->totpoint(pid->calldata, cfra);
2398 int i, error = 0;
2399
2400 pm = MEM_new<PTCacheMem>("Pointcache mem");
2401
2402 pm->totpoint = pid->totwrite(pid->calldata, cfra);
2403 pm->data_types = cfra ? pid->data_types : pid->info_types;
2404
2405 if (!ptcache_data_alloc(pm, true)) {
2407 MEM_delete(pm);
2408 return 1;
2409 }
2410 void *cur[BPHYS_TOT_DATA];
2412
2413 if (overwrite) {
2414 if (cache->flag & PTCACHE_DISK_CACHE) {
2415 int fra = cfra - 1;
2416
2417 while (fra >= cache->startframe && !BKE_ptcache_id_exist(pid, fra)) {
2418 fra--;
2419 }
2420
2421 pm2 = ptcache_disk_frame_to_mem(pid, fra);
2422 }
2423 else {
2424 pm2 = cache->mem_cache.last();
2425 }
2426 }
2427
2428 if (pid->write_point) {
2429 for (i = 0; i < totpoint; i++) {
2430 int write = pid->write_point(i, pid->calldata, cur, cfra);
2431 if (write) {
2433
2434 void *cur2[BPHYS_TOT_DATA];
2435 /* newly born particles have to be copied to previous cached frame */
2436 if (overwrite && write == 2 && pm2 && BKE_ptcache_mem_pointers_seek(i, pm2, cur2)) {
2437 pid->write_point(i, pid->calldata, cur2, cfra);
2438 }
2439 }
2440 }
2441 }
2442
2443 if (pid->write_extra_data) {
2444 pid->write_extra_data(pid->calldata, pm, cfra);
2445 }
2446
2447 pm->frame = cfra;
2448
2449 if (cache->flag & PTCACHE_DISK_CACHE) {
2450 error += !ptcache_mem_frame_to_disk(pid, pm);
2451
2452 // if (pm) /* pm is always set */
2453 {
2455 MEM_delete(pm);
2456 }
2457
2458 if (pm2) {
2459 error += !ptcache_mem_frame_to_disk(pid, pm2);
2460 ptcache_mem_clear(pm2);
2461 MEM_delete(pm2);
2462 }
2463 }
2464 else {
2465 BLI_addtail(&cache->mem_cache, pm);
2466 }
2467
2468 return error;
2469}
2470static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
2471{
2472 PointCache *cache = pid->cache;
2473 int ofra = 0, efra = cache->endframe;
2474
2475 /* always start from scratch on the first frame */
2476 if (cfra && cfra == cache->startframe) {
2478 cache->flag &= ~PTCACHE_REDO_NEEDED;
2479 return 1;
2480 }
2481
2482 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2483 if (cfra == 0 && cache->startframe > 0) {
2484 return 1;
2485 }
2486
2487 /* find last cached frame */
2488 while (efra > cache->startframe && !BKE_ptcache_id_exist(pid, efra)) {
2489 efra--;
2490 }
2491
2492 /* find second last cached frame */
2493 ofra = efra - 1;
2494 while (ofra > cache->startframe && !BKE_ptcache_id_exist(pid, ofra)) {
2495 ofra--;
2496 }
2497 }
2498 else {
2499 PTCacheMem *pm = cache->mem_cache.last();
2500 /* don't write info file in memory */
2501 if (cfra == 0) {
2502 return 0;
2503 }
2504
2505 if (pm == nullptr) {
2506 return 1;
2507 }
2508
2509 efra = pm->frame;
2510 ofra = (pm->prev ? pm->prev->frame : efra - cache->step);
2511 }
2512
2513 if (efra >= cache->startframe && cfra > efra) {
2514 if (ofra >= cache->startframe && efra - ofra < cache->step) {
2515 /* overwrite previous frame */
2517 *overwrite = 1;
2518 }
2519 return 1;
2520 }
2521
2522 return 0;
2523}
2525{
2526 PointCache *cache = pid->cache;
2527 if (!pid->totpoint) {
2528 /* This happens when `pid->type == PTCACHE_TYPE_SMOKE_DOMAIN`. The fluid system does not
2529 * actually use the pointcache anymore for caching. */
2530 return 0;
2531 }
2532
2533 int totpoint = pid->totpoint(pid->calldata, cfra);
2534 int overwrite = 0, error = 0;
2535
2536 if (totpoint == 0 || (cfra ? pid->data_types == 0 : pid->info_types == 0)) {
2537 return 0;
2538 }
2539
2540 if (ptcache_write_needed(pid, cfra, &overwrite) == 0) {
2541 return 0;
2542 }
2543
2544 if (pid->write_stream) {
2545 ptcache_write_stream(pid, cfra, totpoint);
2546 }
2547 else if (pid->write_point) {
2548 error += ptcache_write(pid, cfra, overwrite);
2549 }
2550
2551 /* Mark frames skipped if more than 1 frame forwards since last non-skipped frame. */
2552 if (cfra - cache->last_exact == 1 || cfra == cache->startframe) {
2553 cache->last_exact = cfra;
2554 cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
2555 }
2556 /* Don't mark skipped when writing info file (frame 0) */
2557 else if (cfra) {
2558 cache->flag |= PTCACHE_FRAMES_SKIPPED;
2559 }
2560
2561 /* Update timeline cache display */
2562 if (cfra && cache->cached_frames) {
2563 cache->cached_frames[cfra - cache->startframe] = 1;
2564 }
2565
2566 cache->flag |= PTCACHE_FLAG_INFO_DIRTY;
2567
2568 return !error;
2569}
2570/* you'll need to close yourself after!
2571 * mode - PTCACHE_CLEAR_ALL,
2572 */
2573
2574/* Clears & resets. */
2575
2576void BKE_ptcache_id_clear(PTCacheID *pid, int mode, uint cfra)
2577{
2578 uint len; /* store the length of the string */
2579 uint sta, end;
2580
2581 /* mode is same as fopen's modes */
2582 DIR *dir;
2583 dirent *de;
2584 char path[MAX_PTCACHE_PATH];
2585 char filepath[MAX_PTCACHE_FILE];
2586 char path_full[MAX_PTCACHE_FILE];
2587 char ext[MAX_PTCACHE_FILE];
2588
2589 if (!pid || !pid->cache || pid->cache->flag & PTCACHE_BAKED) {
2590 return;
2591 }
2592
2593 if (pid->cache->flag & PTCACHE_IGNORE_CLEAR) {
2594 return;
2595 }
2596
2597 sta = pid->cache->startframe;
2598 end = pid->cache->endframe;
2599
2600#ifndef DURIAN_POINTCACHE_LIB_OK
2601 /* don't allow clearing for linked objects */
2602 if (pid->owner_id->lib) {
2603 return;
2604 }
2605#endif
2606
2607 /* Clear all files in the temp dir with the prefix of the ID and the `.bphys` suffix. */
2608 switch (mode) {
2609 case PTCACHE_CLEAR_ALL:
2612 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2613 BKE_ptcache_path(pid, path);
2614
2615 dir = opendir(path);
2616 if (dir == nullptr) {
2617 return;
2618 }
2619
2620 len = ptcache_filepath(pid, filepath, cfra, false, false); /* no path */
2621 /* append underscore terminator to ensure we don't match similar names
2622 * from objects whose names start with the same prefix
2623 */
2624 if (len < sizeof(filepath) - 2) {
2625 BLI_strncpy(filepath + len, "_", sizeof(filepath) - 2 - len);
2626 len += 1;
2627 }
2628
2629 ptcache_filepath_ext_append(pid, ext, 0, false, 0);
2630
2631 while ((de = readdir(dir)) != nullptr) {
2632 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
2633 if (STREQLEN(filepath, de->d_name, len)) { /* Do we have the right prefix. */
2634 if (mode == PTCACHE_CLEAR_ALL) {
2635 pid->cache->last_exact = std::min(pid->cache->startframe, 0);
2636 BLI_path_join(path_full, sizeof(path_full), path, de->d_name);
2637 BLI_delete(path_full, false, false);
2638 }
2639 else {
2640 /* read the number of the file */
2641 const int frame = ptcache_frame_from_filename(de->d_name, ext);
2642
2643 if (frame != -1) {
2644 if ((mode == PTCACHE_CLEAR_BEFORE && frame < cfra) ||
2645 (mode == PTCACHE_CLEAR_AFTER && frame > cfra))
2646 {
2647 BLI_path_join(path_full, sizeof(path_full), path, de->d_name);
2648 BLI_delete(path_full, false, false);
2649 if (pid->cache->cached_frames && frame >= sta && frame <= end) {
2650 pid->cache->cached_frames[frame - sta] = 0;
2651 }
2652 }
2653 }
2654 }
2655 }
2656 }
2657 }
2658 closedir(dir);
2659
2660 if (mode == PTCACHE_CLEAR_ALL && pid->cache->cached_frames) {
2661 memset(pid->cache->cached_frames, 0, pid->cache->cached_frames_len);
2662 }
2663 }
2664 else {
2665 PTCacheMem *pm = pid->cache->mem_cache.first();
2666 PTCacheMem *link = nullptr;
2667
2668 if (mode == PTCACHE_CLEAR_ALL) {
2669 /* We want startframe if the cache starts before zero. */
2670 pid->cache->last_exact = std::min(pid->cache->startframe, 0);
2671 for (; pm; pm = pm->next) {
2673 }
2674 pid->cache->mem_cache.free_no_destruct();
2675
2676 if (pid->cache->cached_frames) {
2677 memset(pid->cache->cached_frames, 0, pid->cache->cached_frames_len);
2678 }
2679 }
2680 else {
2681 while (pm) {
2682 if ((mode == PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
2683 (mode == PTCACHE_CLEAR_AFTER && pm->frame > cfra))
2684 {
2685 link = pm;
2686 if (pid->cache->cached_frames && pm->frame >= sta && pm->frame <= end) {
2687 pid->cache->cached_frames[pm->frame - sta] = 0;
2688 }
2690 pm = pm->next;
2691 BLI_freelinkN(&pid->cache->mem_cache, link);
2692 }
2693 else {
2694 pm = pm->next;
2695 }
2696 }
2697 }
2698 }
2699 break;
2700
2702 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2703 if (BKE_ptcache_id_exist(pid, cfra)) {
2704 ptcache_filepath(pid, filepath, cfra, true, true); /* no path */
2705 BLI_delete(filepath, false, false);
2706 }
2707 }
2708 else {
2709 PTCacheMem *pm = pid->cache->mem_cache.first();
2710
2711 for (; pm; pm = pm->next) {
2712 if (pm->frame == cfra) {
2714 BLI_freelinkN(&pid->cache->mem_cache, pm);
2715 break;
2716 }
2717 }
2718 }
2719 if (pid->cache->cached_frames && cfra >= sta && cfra <= end) {
2720 pid->cache->cached_frames[cfra - sta] = 0;
2721 }
2722 break;
2723 }
2724
2726}
2727
2729{
2730 if (!pid->cache) {
2731 return false;
2732 }
2733
2734 if (cfra < pid->cache->startframe || cfra > pid->cache->endframe) {
2735 return false;
2736 }
2737
2738 if (pid->cache->cached_frames && pid->cache->cached_frames[cfra - pid->cache->startframe] == 0) {
2739 return false;
2740 }
2741
2742 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2743 char filepath[MAX_PTCACHE_FILE];
2744
2745 ptcache_filepath(pid, filepath, cfra, true, true);
2746
2747 return BLI_exists(filepath);
2748 }
2749
2750 PTCacheMem *pm = pid->cache->mem_cache.first();
2751
2752 for (; pm; pm = pm->next) {
2753 if (pm->frame == cfra) {
2754 return true;
2755 }
2756 }
2757 return false;
2758}
2760 PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
2761{
2762 // Object *ob; /* UNUSED */
2763 PointCache *cache;
2764 /* float offset; unused for now */
2765 float time, nexttime;
2766
2767 /* TODO: this has to be sorted out once bsystem_time gets redone, */
2768 /* now caches can handle interpolating etc. too - jahka */
2769
2770 /* time handling for point cache:
2771 * - simulation time is scaled by result of bsystem_time
2772 * - for offsetting time only time offset is taken into account, since
2773 * that's always the same and can't be animated. a time-offset which
2774 * varies over time is not simple to support.
2775 * - field and motion blur offsets are currently ignored, proper solution
2776 * is probably to interpolate results from two frames for that ..
2777 */
2778
2779 cache = pid->cache;
2780
2781 if (timescale) {
2782 time = BKE_scene_ctime_get(scene);
2783 nexttime = BKE_scene_frame_to_ctime(scene, scene->r.cfra + 1);
2784
2785 *timescale = std::max(nexttime - time, 0.0f);
2786 }
2787
2788 if (startframe && endframe) {
2789 *startframe = cache->startframe;
2790 *endframe = cache->endframe;
2791 }
2792
2793 /* verify cached_frames array is up to date */
2794 if (cache->cached_frames) {
2795 if (cache->cached_frames_len != (cache->endframe - cache->startframe + 1)) {
2796 MEM_delete(cache->cached_frames);
2797 cache->cached_frames = nullptr;
2798 cache->cached_frames_len = 0;
2799 }
2800 }
2801
2802 if (cache->cached_frames == nullptr && cache->endframe > cache->startframe) {
2803 uint sta = cache->startframe;
2804 uint end = cache->endframe;
2805
2806 cache->cached_frames_len = cache->endframe - cache->startframe + 1;
2808 "cached frames array");
2809
2810 if (pid->cache->flag & PTCACHE_DISK_CACHE) {
2811 /* mode is same as fopen's modes */
2812 DIR *dir;
2813 dirent *de;
2814 char path[MAX_PTCACHE_PATH];
2815 char filepath[MAX_PTCACHE_FILE];
2816 char ext[MAX_PTCACHE_FILE];
2817 uint len; /* store the length of the string */
2818
2819 BKE_ptcache_path(pid, path);
2820
2821 len = ptcache_filepath(pid, filepath, int(cfra), false, false); /* no path */
2822
2823 dir = opendir(path);
2824 if (dir == nullptr) {
2825 return;
2826 }
2827
2828 ptcache_filepath_ext_append(pid, ext, 0, false, 0);
2829
2830 while ((de = readdir(dir)) != nullptr) {
2831 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
2832 if (STREQLEN(filepath, de->d_name, len)) { /* Do we have the right prefix. */
2833 /* read the number of the file */
2834 const int frame = ptcache_frame_from_filename(de->d_name, ext);
2835
2836 if ((frame != -1) && (frame >= sta && frame <= end)) {
2837 cache->cached_frames[frame - sta] = 1;
2838 }
2839 }
2840 }
2841 }
2842 closedir(dir);
2843 }
2844 else {
2845 PTCacheMem *pm = pid->cache->mem_cache.first();
2846
2847 while (pm) {
2848 if (pm->frame >= sta && pm->frame <= end) {
2849 cache->cached_frames[pm->frame - sta] = 1;
2850 }
2851 pm = pm->next;
2852 }
2853 }
2854 }
2855}
2856int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
2857{
2858 PointCache *cache;
2859 int reset, clear, after;
2860
2861 if (!pid->cache) {
2862 return 0;
2863 }
2864
2865 cache = pid->cache;
2866 reset = 0;
2867 clear = 0;
2868 after = 0;
2869
2870 if (mode == PTCACHE_RESET_DEPSGRAPH) {
2871 if (!(cache->flag & PTCACHE_BAKED)) {
2872
2873 after = 1;
2874 }
2875
2876 cache->flag |= PTCACHE_OUTDATED;
2877 }
2878 else if (mode == PTCACHE_RESET_BAKED) {
2879 cache->flag |= PTCACHE_OUTDATED;
2880 }
2881 else if (mode == PTCACHE_RESET_OUTDATED) {
2882 reset = 1;
2883
2884 if (cache->flag & PTCACHE_OUTDATED && !(cache->flag & PTCACHE_BAKED)) {
2885 clear = 1;
2886 cache->flag &= ~PTCACHE_OUTDATED;
2887 }
2888 }
2889
2890 if (reset) {
2892 cache->flag &= ~PTCACHE_REDO_NEEDED;
2893
2894 if (pid->type == PTCACHE_TYPE_CLOTH) {
2895 cloth_free_modifier(static_cast<ClothModifierData *>(pid->calldata));
2896 }
2897 else if (pid->type == PTCACHE_TYPE_SOFTBODY) {
2898 sbFreeSimulation(static_cast<SoftBody *>(pid->calldata));
2899 }
2900 else if (pid->type == PTCACHE_TYPE_PARTICLES) {
2902 }
2903 else if (pid->type == PTCACHE_TYPE_DYNAMICPAINT) {
2904 dynamicPaint_clearSurface(scene, static_cast<DynamicPaintSurface *>(pid->calldata));
2905 }
2906 }
2907 if (clear) {
2909 }
2910 else if (after) {
2912 }
2913
2914 return (reset || clear || after);
2915}
2916int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
2917{
2918 PTCacheID pid;
2919 int reset, skip;
2920
2921 reset = 0;
2922 skip = 0;
2923
2924 if (ob->soft) {
2925 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
2926 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2927 }
2928
2929 for (ParticleSystem &psys : ob->particlesystem) {
2930 /* children or just redo can be calculated without resetting anything */
2932 skip = 1;
2933 /* Baked cloth hair has to be checked too, because we don't want to reset */
2934 /* particles or cloth in that case -jahka */
2935 }
2936 else if (psys.clmd) {
2937 BKE_ptcache_id_from_cloth(&pid, ob, psys.clmd);
2938 if (mode == PSYS_RESET_ALL ||
2939 !(psys.part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED)))
2940 {
2941 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2942 }
2943 else {
2944 skip = 1;
2945 }
2946 }
2947
2948 if (skip == 0 && psys.part) {
2949 BKE_ptcache_id_from_particles(&pid, ob, &psys);
2950 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2951 }
2952 }
2953
2954 for (ModifierData &md : ob->modifiers) {
2955 if (md.type == eModifierType_Cloth) {
2956 BKE_ptcache_id_from_cloth(&pid, ob, reinterpret_cast<ClothModifierData *>(&md));
2957 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2958 }
2959 if (md.type == eModifierType_DynamicPaint) {
2960 DynamicPaintModifierData *pmd = reinterpret_cast<DynamicPaintModifierData *>(&md);
2961 if (pmd->canvas) {
2962 DynamicPaintSurface *surface = pmd->canvas->surfaces.first();
2963
2964 for (; surface; surface = surface->next) {
2965 BKE_ptcache_id_from_dynamicpaint(&pid, ob, surface);
2966 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2967 }
2968 }
2969 }
2970 if (md.type == eModifierType_Fluid) {
2971 FluidModifierData *fmd = reinterpret_cast<FluidModifierData *>(&md);
2972 FluidDomainSettings *fds = fmd->domain;
2973 if ((fmd->type & MOD_FLUID_TYPE_DOMAIN) && fds &&
2975 {
2976 BKE_ptcache_id_from_smoke(&pid, ob, fmd);
2977 reset |= BKE_ptcache_id_reset(scene, &pid, mode);
2978 }
2979 }
2980 }
2981
2982 if (scene->rigidbody_world && (ob->rigidbody_object || ob->rigidbody_constraint)) {
2983 if (ob->rigidbody_object) {
2985 }
2987 /* only flag as outdated, resetting should happen on start frame */
2988 pid.cache->flag |= PTCACHE_OUTDATED;
2989 }
2990
2991 if (ob->type == OB_ARMATURE) {
2992 BIK_clear_cache(ob->pose);
2993 }
2994
2995 return reset;
2996}
2997
2998/* Point Cache handling */
2999
3000PointCache *BKE_ptcache_add(ListBaseT<PointCache> *ptcaches)
3001{
3002 PointCache *cache;
3003
3004 cache = MEM_new<PointCache>("PointCache");
3005 cache->startframe = 1;
3006 cache->endframe = 250;
3007 cache->step = 1;
3008 cache->index = -1;
3009
3010 BLI_addtail(ptcaches, cache);
3011
3012 return cache;
3013}
3014
3015void BKE_ptcache_free_mem(ListBaseT<PTCacheMem> *mem_cache)
3016{
3017 PTCacheMem *pm = mem_cache->first();
3018
3019 if (pm) {
3020 for (; pm; pm = pm->next) {
3022 }
3023
3024 mem_cache->free_no_destruct();
3025 }
3026}
3028{
3030 if (cache->edit && cache->free_edit) {
3031 cache->free_edit(cache->edit);
3032 }
3033 if (cache->cached_frames) {
3034 MEM_delete(cache->cached_frames);
3035 }
3036 MEM_delete(cache);
3037}
3038void BKE_ptcache_free_list(ListBaseT<PointCache> *ptcaches)
3039{
3040 while (PointCache *cache = static_cast<PointCache *>(BLI_pophead(ptcaches))) {
3041 BKE_ptcache_free(cache);
3042 }
3043}
3044
3045static PointCache *ptcache_copy(PointCache *cache, const bool copy_data)
3046{
3047 PointCache *ncache;
3048
3049 ncache = MEM_dupalloc(cache);
3050
3051 ncache->mem_cache.clear_no_delete();
3052
3053 if (copy_data == false) {
3054 ncache->cached_frames = nullptr;
3055 ncache->cached_frames_len = 0;
3056
3057 /* flag is a mix of user settings and simulator/baking state */
3059 ncache->simframe = 0;
3060 }
3061 else {
3062 for (PTCacheMem &pm : cache->mem_cache) {
3063 PTCacheMem *pmn = MEM_dupalloc(&pm);
3064 int i;
3065
3066 for (i = 0; i < BPHYS_TOT_DATA; i++) {
3067 if (pmn->data[i]) {
3068 pmn->data[i] = MEM_dupalloc_void(pm.data[i]);
3069 }
3070 }
3071
3072 BLI_addtail(&ncache->mem_cache, pmn);
3073 }
3074
3075 if (ncache->cached_frames) {
3076 ncache->cached_frames = MEM_dupalloc(cache->cached_frames);
3077 }
3078 }
3079
3080 /* hmm, should these be copied over instead? */
3081 ncache->edit = nullptr;
3082
3083 return ncache;
3084}
3085
3086PointCache *BKE_ptcache_copy_list(ListBaseT<PointCache> *ptcaches_new,
3087 const ListBaseT<PointCache> *ptcaches_old,
3088 const int flag)
3089{
3090 PointCache *cache = ptcaches_old->first();
3091
3092 ptcaches_new->clear_no_delete();
3093
3094 for (; cache; cache = cache->next) {
3095 BLI_addtail(ptcaches_new, ptcache_copy(cache, (flag & LIB_ID_COPY_CACHES) != 0));
3096 }
3097
3098 return ptcaches_new->first();
3099}
3100
3101/* Disabled this code; this is being called on scene_update_tagged, and that in turn gets called on
3102 * every user action changing stuff, and then it runs a complete bake??? (ton) */
3103
3104/* Baking */
3105
3106void BKE_ptcache_quick_cache_all(Main *bmain, Scene *scene, ViewLayer *view_layer)
3107{
3108 PTCacheBaker baker;
3109
3110 memset(&baker, 0, sizeof(baker));
3111 baker.bmain = bmain;
3112 baker.scene = scene;
3113 baker.view_layer = view_layer;
3114 baker.bake = false;
3115 baker.render = false;
3116 baker.anim_init = false;
3118
3119 BKE_ptcache_bake(&baker);
3120}
3121
3122static void ptcache_dt_to_str(char *str, size_t str_maxncpy, double dtime)
3123{
3124 if (dtime > 60.0) {
3125 if (dtime > 3600.0) {
3127 str_maxncpy,
3128 "%ih %im %is",
3129 int(dtime / 3600),
3130 int(dtime / 60) % 60,
3131 int(dtime) % 60);
3132 }
3133 else {
3134 BLI_snprintf(str, str_maxncpy, "%im %is", int(dtime / 60) % 60, int(dtime) % 60);
3135 }
3136 }
3137 else {
3138 BLI_snprintf(str, str_maxncpy, "%is", int(dtime) % 60);
3139 }
3140}
3141
3143{
3144 const Main *bmain = baker->bmain;
3145 Scene *scene = baker->scene;
3146 ViewLayer *view_layer = baker->view_layer;
3147 Depsgraph *depsgraph = baker->depsgraph;
3148 Scene *sce_iter; /* SETLOOPER macro only */
3149 Base *base;
3150 ListBaseT<PTCacheID> pidlist;
3151 PTCacheID *pid = &baker->pid;
3152 PointCache *cache = nullptr;
3153 float frameleno = scene->r.framelen;
3154 int cfrao = scene->r.cfra;
3155 int startframe = MAXFRAME, endframe = baker->anim_init ? scene->r.sfra : scene->r.cfra;
3156 int bake = baker->bake;
3157 int render = baker->render;
3158
3159 G.is_break = false;
3160
3161 /* set caches to baking mode and figure out start frame */
3162 if (pid->owner_id) {
3163 /* cache/bake a single object */
3164 cache = pid->cache;
3165 if ((cache->flag & PTCACHE_BAKED) == 0) {
3166 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3167 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
3168
3169 /* a bit confusing, could make this work better in the UI */
3170 if (psys->part->type == PART_EMITTER) {
3172 static_cast<ParticleSystem *>(pid->calldata),
3173 &cache->startframe,
3174 &cache->endframe);
3175 }
3176 }
3177 else if (pid->type == PTCACHE_TYPE_SMOKE_HIGHRES) {
3178 /* get all pids from the object and search for smoke low res */
3179 ListBaseT<PTCacheID> pidlist2;
3180 BLI_assert(GS(pid->owner_id->name) == ID_OB);
3182 &pidlist2, id_cast<Object *>(pid->owner_id), scene, MAX_DUPLI_RECUR);
3183 for (PTCacheID &pid2 : pidlist2) {
3184 if (pid2.type == PTCACHE_TYPE_SMOKE_DOMAIN) {
3185 if (pid2.cache && !(pid2.cache->flag & PTCACHE_BAKED)) {
3186 if (bake || pid2.cache->flag & PTCACHE_REDO_NEEDED) {
3188 }
3189 if (bake) {
3190 pid2.cache->flag |= PTCACHE_BAKING;
3191 pid2.cache->flag &= ~PTCACHE_BAKED;
3192 }
3193 }
3194 }
3195 }
3196 pidlist2.free_no_destruct();
3197 }
3198
3199 if (bake || cache->flag & PTCACHE_REDO_NEEDED) {
3201 }
3202
3203 startframe = std::max(cache->last_exact, cache->startframe);
3204
3205 if (bake) {
3206 endframe = cache->endframe;
3207 cache->flag |= PTCACHE_BAKING;
3208 }
3209 else {
3210 endframe = std::min(endframe, cache->endframe);
3211 }
3212
3213 cache->flag &= ~PTCACHE_BAKED;
3214 }
3215 }
3216 else {
3217 for (SETLOOPER_VIEW_LAYER(*bmain, scene, view_layer, sce_iter, base)) {
3218 /* cache/bake everything in the scene */
3219 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
3220
3221 for (pid = pidlist.first(); pid; pid = pid->next) {
3222 cache = pid->cache;
3223 if ((cache->flag & PTCACHE_BAKED) == 0) {
3224 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3225 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
3226 /* skip hair & keyed particles */
3227 if (psys->part->type == PART_HAIR || psys->part->phystype == PART_PHYS_KEYED) {
3228 continue;
3229 }
3230
3232 static_cast<ParticleSystem *>(pid->calldata),
3233 &cache->startframe,
3234 &cache->endframe);
3235 }
3236
3237 /* XXX: workaround for regression introduced in ee3fadd, needs looking into. */
3238 if (pid->type == PTCACHE_TYPE_RIGIDBODY) {
3239 if ((cache->flag & PTCACHE_REDO_NEEDED ||
3240 (cache->flag & PTCACHE_SIMULATION_VALID) == 0) &&
3241 (render || bake))
3242 {
3244 }
3245 }
3246 else if (((cache->flag & PTCACHE_BAKED) == 0) && (render || bake)) {
3248 }
3249
3250 startframe = std::min(startframe, cache->startframe);
3251
3252 if (bake || render) {
3253 cache->flag |= PTCACHE_BAKING;
3254
3255 if (bake) {
3256 endframe = std::max(endframe, cache->endframe);
3257 }
3258 }
3259
3260 cache->flag &= ~PTCACHE_BAKED;
3261 }
3262 }
3263 pidlist.free_no_destruct();
3264 }
3265 }
3266
3267 scene->r.cfra = startframe;
3268 scene->r.framelen = 1.0;
3269
3270 /* bake */
3271
3272 bool use_timer = false;
3273 double stime, ptime, ctime, fetd;
3274 char run[32], cur[32], etd[32];
3275 int cancel = 0;
3276
3277 stime = ptime = BLI_time_now_seconds();
3278
3279 for (int fr = scene->r.cfra; fr <= endframe; fr += baker->quick_step, scene->r.cfra = fr) {
3281
3282 if (baker->update_progress) {
3283 float progress = (float(scene->r.cfra - startframe) / float(endframe - startframe));
3284 baker->update_progress(baker->bake_job, progress, &cancel);
3285 }
3286
3287 if (G.background) {
3288 printf("bake: frame %d :: %d\n", scene->r.cfra, endframe);
3289 }
3290 else {
3291 ctime = BLI_time_now_seconds();
3292
3293 fetd = (ctime - ptime) * (endframe - scene->r.cfra) / baker->quick_step;
3294
3295 if (use_timer || fetd > 60.0) {
3296 use_timer = true;
3297
3298 ptcache_dt_to_str(cur, sizeof(cur), ctime - ptime);
3299 ptcache_dt_to_str(run, sizeof(run), ctime - stime);
3300 ptcache_dt_to_str(etd, sizeof(etd), fetd);
3301
3302 printf("Baked for %s, current frame: %i/%i (%.3fs), ETC: %s\r",
3303 run,
3304 scene->r.cfra - startframe + 1,
3305 endframe - startframe + 1,
3306 ctime - ptime,
3307 etd);
3308 }
3309
3310 ptime = ctime;
3311 }
3312
3313 /* NOTE: breaking baking should leave calculated frames in cache, not clear it */
3314 if (cancel || G.is_break) {
3315 break;
3316 }
3317
3318 scene->r.cfra += 1;
3319 }
3320
3321 if (use_timer) {
3322 /* start with newline because of \r above */
3323 ptcache_dt_to_str(run, sizeof(run), BLI_time_now_seconds() - stime);
3324 printf("\nBake %s %s (%i frames simulated).\n",
3325 (cancel ? "canceled after" : "finished in"),
3326 run,
3327 scene->r.cfra - startframe);
3328 }
3329
3330 /* clear baking flag */
3331 if (pid && cache) {
3334 if (bake) {
3335 cache->flag |= PTCACHE_BAKED;
3336 /* write info file */
3337 if (cache->flag & PTCACHE_DISK_CACHE) {
3338 if (pid->type == PTCACHE_TYPE_PARTICLES) {
3339 /* Since writing this from outside the bake job, make sure the ParticleSystem and
3340 * PTCacheID is in a fully evaluated state. */
3341 PTCacheID pid_eval;
3342 Object *ob = reinterpret_cast<Object *>(pid->owner_id);
3343 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
3344 ParticleSystem *psys = static_cast<ParticleSystem *>(pid->calldata);
3345 ParticleSystem *psys_eval = psys_eval_get(depsgraph, ob, psys);
3346 BKE_ptcache_id_from_particles(&pid_eval, ob_eval, psys_eval);
3347 BKE_ptcache_write(&pid_eval, 0);
3348 }
3349 else {
3350 BKE_ptcache_write(pid, 0);
3351 }
3352 }
3353 }
3354 }
3355 else {
3356 for (SETLOOPER_VIEW_LAYER(*bmain, scene, view_layer, sce_iter, base)) {
3357 BKE_ptcache_ids_from_object(&pidlist, base->object, scene, MAX_DUPLI_RECUR);
3358
3359 for (PTCacheID &pid : pidlist) {
3360 /* skip hair particles */
3361 if (pid.type == PTCACHE_TYPE_PARTICLES &&
3362 (static_cast<ParticleSystem *>(pid.calldata))->part->type == PART_HAIR)
3363 {
3364 continue;
3365 }
3366
3367 cache = pid.cache;
3368
3369 if (baker->quick_step > 1) {
3370 cache->flag &= ~(PTCACHE_BAKING | PTCACHE_OUTDATED);
3371 }
3372 else {
3374 }
3375
3377
3378 if (bake) {
3379 cache->flag |= PTCACHE_BAKED;
3380 if (cache->flag & PTCACHE_DISK_CACHE) {
3381 if (pid.type == PTCACHE_TYPE_PARTICLES) {
3382 /* Since writing this from outside the bake job, make sure the ParticleSystem and
3383 * PTCacheID is in a fully evaluated state. */
3384 PTCacheID pid_eval;
3385 Object *ob = reinterpret_cast<Object *>(pid.owner_id);
3386 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
3387 ParticleSystem *psys = static_cast<ParticleSystem *>(pid.calldata);
3388 ParticleSystem *psys_eval = psys_eval_get(depsgraph, ob, psys);
3389 BKE_ptcache_id_from_particles(&pid_eval, ob_eval, psys_eval);
3390 BKE_ptcache_write(&pid_eval, 0);
3391 }
3392 else {
3393 BKE_ptcache_write(&pid, 0);
3394 }
3395 }
3396 }
3397 }
3398 pidlist.free_no_destruct();
3399 }
3400 }
3401
3402 scene->r.framelen = frameleno;
3403 scene->r.cfra = cfrao;
3404
3405 if (bake) { /* already on cfra unless baking */
3407 }
3408
3409 /* TODO: call redraw all windows somehow */
3410}
3411
3412/* Helpers */
3413
3415{
3416 PointCache *cache = pid->cache;
3417 PTCacheMem *pm = nullptr;
3418 ePointCache_Flag baked = cache->flag & PTCACHE_BAKED;
3419 int cfra, sfra = cache->startframe, efra = cache->endframe;
3420
3421 /* Remove possible bake flag to allow clear */
3422 cache->flag &= ~PTCACHE_BAKED;
3423
3424 /* PTCACHE_DISK_CACHE flag was cleared already */
3426
3427 /* restore possible bake flag */
3428 cache->flag |= baked;
3429
3430 for (cfra = sfra; cfra <= efra; cfra++) {
3431 pm = ptcache_disk_frame_to_mem(pid, cfra);
3432
3433 if (pm) {
3434 BLI_addtail(&pid->cache->mem_cache, pm);
3435 }
3436 }
3437}
3439{
3440 PointCache *cache = pid->cache;
3441 PTCacheMem *pm = cache->mem_cache.first();
3442 ePointCache_Flag baked = cache->flag & PTCACHE_BAKED;
3443
3444 /* Remove possible bake flag to allow clear */
3445 cache->flag &= ~PTCACHE_BAKED;
3446
3447 /* PTCACHE_DISK_CACHE flag was set already */
3449
3450 /* restore possible bake flag */
3451 cache->flag |= baked;
3452
3453 for (; pm; pm = pm->next) {
3454 if (ptcache_mem_frame_to_disk(pid, pm) == 0) {
3455 cache->flag &= ~PTCACHE_DISK_CACHE;
3456 break;
3457 }
3458 }
3459
3460 /* write info file */
3461 if (cache->flag & PTCACHE_BAKED) {
3462 BKE_ptcache_write(pid, 0);
3463 }
3464}
3466{
3467 PointCache *cache = pid->cache;
3468 int last_exact = cache->last_exact;
3469 const char *blendfile_path = BKE_main_blendfile_path_from_global();
3470
3471 if (blendfile_path[0] == '\0') {
3472 cache->flag &= ~PTCACHE_DISK_CACHE;
3473 if (G.debug & G_DEBUG) {
3474 printf("File must be saved before using disk cache!\n");
3475 }
3476 return;
3477 }
3478
3479 if (cache->cached_frames) {
3480 MEM_delete(cache->cached_frames);
3481 cache->cached_frames = nullptr;
3482 cache->cached_frames_len = 0;
3483 }
3484
3485 if (cache->flag & PTCACHE_DISK_CACHE) {
3487 }
3488 else {
3490 }
3491
3492 cache->flag ^= PTCACHE_DISK_CACHE;
3494 cache->flag ^= PTCACHE_DISK_CACHE;
3495
3496 cache->last_exact = last_exact;
3497
3498 BKE_ptcache_id_time(pid, nullptr, 0.0f, nullptr, nullptr, nullptr);
3499
3500 cache->flag |= PTCACHE_FLAG_INFO_DIRTY;
3501
3502 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
3503 if (cache->index) {
3504 BKE_object_delete_ptcache(id_cast<Object *>(pid->owner_id), cache->index);
3505 cache->index = -1;
3506 }
3507 }
3508}
3509
3510void BKE_ptcache_disk_cache_rename(PTCacheID *pid, const char *name_src, const char *name_dst)
3511{
3512 char old_name[80];
3513 int len; /* store the length of the string */
3514 /* mode is same as fopen's modes */
3515 DIR *dir;
3516 dirent *de;
3517 char path[MAX_PTCACHE_PATH];
3518 char old_filepath[MAX_PTCACHE_FILE];
3519 char new_path_full[MAX_PTCACHE_FILE];
3520 char old_path_full[MAX_PTCACHE_FILE];
3521 char ext[MAX_PTCACHE_FILE];
3522
3523 /* If both names are the same, there is nothing to do. */
3524 if (STREQ(name_src, name_dst)) {
3525 return;
3526 }
3527
3528 /* save old name */
3529 STRNCPY(old_name, pid->cache->name);
3530
3531 /* get "from" filename */
3532 STRNCPY(pid->cache->name, name_src);
3533
3534 len = ptcache_filepath(pid, old_filepath, 0, false, false); /* no path */
3535
3536 BKE_ptcache_path(pid, path);
3537 dir = opendir(path);
3538 if (dir == nullptr) {
3539 STRNCPY(pid->cache->name, old_name);
3540 return;
3541 }
3542
3543 ptcache_filepath_ext_append(pid, ext, 0, false, 0);
3544
3545 /* put new name into cache */
3546 STRNCPY(pid->cache->name, name_dst);
3547
3548 while ((de = readdir(dir)) != nullptr) {
3549 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
3550 if (STREQLEN(old_filepath, de->d_name, len)) { /* Do we have the right prefix. */
3551 /* read the number of the file */
3552 const int frame = ptcache_frame_from_filename(de->d_name, ext);
3553
3554 if (frame != -1) {
3555 BLI_path_join(old_path_full, sizeof(old_path_full), path, de->d_name);
3556 ptcache_filepath(pid, new_path_full, frame, true, true);
3557 BLI_rename_overwrite(old_path_full, new_path_full);
3558 }
3559 }
3560 }
3561 }
3562 closedir(dir);
3563
3564 STRNCPY(pid->cache->name, old_name);
3565}
3566
3568{
3569 /* TODO: */
3570 PointCache *cache = pid->cache;
3571 int len; /* store the length of the string */
3572 int info = 0;
3573 int start = MAXFRAME;
3574 int end = -1;
3575
3576 /* mode is same as fopen's modes */
3577 DIR *dir;
3578 dirent *de;
3579 char path[MAX_PTCACHE_PATH];
3580 char filepath[MAX_PTCACHE_FILE];
3581 char ext[MAX_PTCACHE_PATH];
3582
3583 if (!cache) {
3584 return;
3585 }
3586
3587 BKE_ptcache_path(pid, path);
3588
3589 len = ptcache_filepath(pid, filepath, 1, false, false); /* no path */
3590
3591 dir = opendir(path);
3592 if (dir == nullptr) {
3593 return;
3594 }
3595
3596 const char *fext = ptcache_file_extension(pid);
3597
3598 if (cache->index >= 0) {
3599 SNPRINTF(ext, "_%02d%s", cache->index, fext);
3600 }
3601 else {
3602 STRNCPY(ext, fext);
3603 }
3604
3605 while ((de = readdir(dir)) != nullptr) {
3606 if (strstr(de->d_name, ext)) { /* Do we have the right extension? */
3607 if (STREQLEN(filepath, de->d_name, len)) { /* Do we have the right prefix. */
3608 /* read the number of the file */
3609 const int frame = ptcache_frame_from_filename(de->d_name, ext);
3610
3611 if (frame != -1) {
3612 if (frame) {
3613 start = std::min(start, frame);
3614 end = std::max(end, frame);
3615 }
3616 else {
3617 info = 1;
3618 }
3619 }
3620 }
3621 }
3622 }
3623 closedir(dir);
3624
3625 if (start != MAXFRAME) {
3626 PTCacheFile *pf;
3627
3628 cache->startframe = start;
3629 cache->endframe = end;
3630 cache->totpoint = 0;
3631
3632 if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
3633 /* necessary info in every file */
3634 }
3635 /* read totpoint from info file (frame 0) */
3636 else if (info) {
3638
3639 if (pf) {
3641 if (pf->type == pid->type && pid->read_header(pf)) {
3642 cache->totpoint = pf->totpoint;
3643 cache->flag |= PTCACHE_READ_INFO;
3644 }
3645 else {
3646 cache->totpoint = 0;
3647 }
3648 }
3650 }
3651 }
3652 /* or from any old format cache file */
3653 else {
3654 float old_data[14];
3655 int elemsize = ptcache_old_elemsize(pid);
3657
3658 if (pf) {
3659 while (ptcache_file_read(pf, old_data, 1, elemsize)) {
3660 cache->totpoint++;
3661 }
3662
3664 }
3665 }
3668 }
3669
3670 /* make sure all new frames are loaded */
3671 if (cache->cached_frames) {
3672 MEM_delete(cache->cached_frames);
3673 cache->cached_frames = nullptr;
3674 cache->cached_frames_len = 0;
3675 }
3676
3677 cache->flag |= PTCACHE_FLAG_INFO_DIRTY;
3678}
3679
3681{
3682 PointCache *cache = pid->cache;
3683 int totframes = 0;
3684 char mem_info[sizeof(PointCache::info) / sizeof(*PointCache::info)];
3685
3687
3688 if (cache->flag & PTCACHE_EXTERNAL) {
3689 int cfra = cache->startframe;
3690
3691 for (; cfra <= cache->endframe; cfra++) {
3692 if (BKE_ptcache_id_exist(pid, cfra)) {
3693 totframes++;
3694 }
3695 }
3696
3697 /* smoke doesn't use frame 0 as info frame so can't check based on totpoint */
3698 if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN && totframes) {
3699 SNPRINTF_UTF8(cache->info, RPT_("%i frames found!"), totframes);
3700 }
3701 else if (totframes && cache->totpoint) {
3702 SNPRINTF_UTF8(cache->info, RPT_("%i points found!"), cache->totpoint);
3703 }
3704 else {
3705 STRNCPY_UTF8(cache->info, RPT_("No valid data to read!"));
3706 }
3707 return;
3708 }
3709
3710 if (cache->flag & PTCACHE_DISK_CACHE) {
3711 if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN) {
3712 int totpoint = pid->totpoint(pid->calldata, 0);
3713
3714 if (cache->totpoint > totpoint) {
3715 SNPRINTF_UTF8(mem_info, RPT_("%i cells + High Resolution cached"), totpoint);
3716 }
3717 else {
3718 SNPRINTF_UTF8(mem_info, RPT_("%i cells cached"), totpoint);
3719 }
3720 }
3721 else {
3722 int cfra = cache->startframe;
3723
3724 for (; cfra <= cache->endframe; cfra++) {
3725 if (BKE_ptcache_id_exist(pid, cfra)) {
3726 totframes++;
3727 }
3728 }
3729
3730 SNPRINTF_UTF8(mem_info, RPT_("%i frames on disk"), totframes);
3731 }
3732 }
3733 else {
3734 PTCacheMem *pm = cache->mem_cache.first();
3735 char formatted_tot[BLI_STR_FORMAT_INT32_GROUPED_SIZE];
3736 char formatted_mem[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE];
3737 long long int bytes = 0.0f;
3738 int i;
3739
3740 for (; pm; pm = pm->next) {
3741 for (i = 0; i < BPHYS_TOT_DATA; i++) {
3742 if (pm->data_types & (1 << i)) {
3743 /* Size of pm->data[i]. */
3744 bytes += int64_t(pm->totpoint) * ptcache_data_size[i];
3745 }
3746 }
3747
3748 for (PTCacheExtra &extra : pm->extradata) {
3749 /* Size of extra.data. */
3750 bytes += int64_t(extra.totdata) * ptcache_extra_datasize[extra.type];
3751 bytes += sizeof(PTCacheExtra);
3752 }
3753
3754 bytes += sizeof(PTCacheMem);
3755
3756 totframes++;
3757 }
3758
3759 BLI_str_format_int_grouped(formatted_tot, totframes);
3760 BLI_str_format_byte_unit(formatted_mem, bytes, false);
3761
3762 SNPRINTF_UTF8(mem_info, RPT_("%s frames in memory (%s)"), formatted_tot, formatted_mem);
3763 }
3764
3765 if (cache->flag & PTCACHE_OUTDATED) {
3766 SNPRINTF_UTF8(cache->info, RPT_("%s, cache is outdated!"), mem_info);
3767 }
3768 else if (cache->flag & PTCACHE_FRAMES_SKIPPED) {
3769 SNPRINTF_UTF8(cache->info, RPT_("%s, not exact since frame %i"), mem_info, cache->last_exact);
3770 }
3771 else {
3772 SNPRINTF_UTF8(cache->info, "%s.", mem_info);
3773 }
3774}
3775
3776void BKE_ptcache_validate(PointCache *cache, int framenr)
3777{
3778 if (cache) {
3780 cache->simframe = framenr;
3781 }
3782}
3784{
3785 if (cache) {
3787 cache->simframe = 0;
3788 cache->last_exact = std::min(cache->startframe, 0);
3789 }
3790}
3791
3792void BKE_ptcache_blend_write(BlendWriter *writer, ListBaseT<PointCache> *ptcaches)
3793{
3794 for (PointCache &cache : *ptcaches) {
3795 writer->write_struct(&cache);
3796
3797 if ((cache.flag & PTCACHE_DISK_CACHE) == 0) {
3798 for (PTCacheMem &pm : cache.mem_cache) {
3799 writer->write_struct(&pm);
3800
3801 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
3802 if (pm.data[i] && pm.data_types & (1 << i)) {
3803 if (i == BPHYS_DATA_BOIDS) {
3804 writer->write_struct_array_cast<BoidData>(pm.totpoint, pm.data[i]);
3805 }
3806 else if (i == BPHYS_DATA_INDEX) { /* Only 'cache type' to use uint values. */
3807 writer->write_uint32_array(pm.totpoint, reinterpret_cast<uint32_t *>(pm.data[i]));
3808 }
3809 else { /* All other types of caches use (vectors of) floats. */
3810 /* data_size returns bytes. */
3811 const uint32_t items_num = pm.totpoint * (BKE_ptcache_data_size(i) / sizeof(float));
3812 writer->write_float_array(items_num, reinterpret_cast<float *>(pm.data[i]));
3813 }
3814 }
3815 }
3816
3817 for (PTCacheExtra &extra : pm.extradata) {
3818 writer->write_struct(&extra);
3819 if (extra.type == BPHYS_EXTRA_FLUID_SPRINGS) {
3820 writer->write_struct_array_cast<ParticleSpring>(extra.totdata, extra.data);
3821 }
3822 else if (extra.type == BPHYS_EXTRA_CLOTH_ACCELERATION) {
3823 writer->write_struct_array_cast<vec3f>(extra.totdata, extra.data);
3824 }
3825 else if (extra.data) {
3827 }
3828 }
3829 }
3830 }
3831 }
3832}
3833
3835{
3836 bool ok = true;
3837 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
3838 if (pm->data[i] == nullptr) {
3839 /* Corrupt file missing the array for a stored data type. */
3840 if (pm->data_types & (1 << i)) {
3841 ok = false;
3842 }
3843 continue;
3844 }
3845
3846 if (i == BPHYS_DATA_BOIDS) {
3847 ok &= BLO_read_array(reader, reinterpret_cast<BoidData **>(&pm->data[i]), pm->totpoint);
3848 }
3849 else if (i == BPHYS_DATA_INDEX) { /* Only 'cache type' to use uint values. */
3850 ok &= BLO_read_array(reader, reinterpret_cast<uint32_t **>(&pm->data[i]), pm->totpoint);
3851 }
3852 else { /* All other types of caches use (vectors of) floats. */
3853 /* data_size returns bytes. */
3854 ok &= BLO_read_array(reader,
3855 reinterpret_cast<float **>(&pm->data[i]),
3856 pm->totpoint,
3857 BKE_ptcache_data_size(i) / sizeof(float));
3858 }
3859 }
3860 if (!ok) {
3861 pm->totpoint = 0;
3862 }
3863
3865
3866 for (PTCacheExtra &extra : pm->extradata) {
3867 if (extra.type == BPHYS_EXTRA_FLUID_SPRINGS) {
3869 reader, reinterpret_cast<ParticleSpring **>(&extra.data), &extra.totdata);
3870 }
3871 else if (extra.type == BPHYS_EXTRA_CLOTH_ACCELERATION) {
3873 reader, reinterpret_cast<vec3f **>(&extra.data), &extra.totdata);
3874 }
3875 else if (extra.data) {
3876 extra.data = nullptr;
3877 }
3878 }
3879}
3880
3882{
3883 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
3884 BLO_read_struct_list(reader, PTCacheMem, &cache->mem_cache);
3885 for (PTCacheMem &pm : cache->mem_cache) {
3886 direct_link_pointcache_mem(reader, &pm);
3887 }
3888 }
3889 else {
3890 cache->mem_cache.clear_no_delete();
3891 }
3892
3894 cache->simframe = 0;
3895 cache->edit = nullptr;
3896 cache->free_edit = nullptr;
3897 cache->cached_frames = nullptr;
3898 cache->cached_frames_len = 0;
3899}
3900
3902 ListBaseT<PointCache> *ptcaches,
3903 PointCache **ocache,
3904 int force_disk)
3905{
3906 if (ptcaches->first()) {
3907 BLO_read_struct_list(reader, PointCache, ptcaches);
3908 for (PointCache &cache : *ptcaches) {
3909 direct_link_pointcache(reader, &cache);
3910 if (force_disk) {
3911 cache.flag |= PTCACHE_DISK_CACHE;
3912 cache.step = 1;
3913 }
3914 }
3915
3916 BLO_read_struct_nonnull(reader, PointCache, ocache);
3917 }
3918 else {
3919 /* old "single" caches need to be linked too */
3920 if (BLO_read_struct_nonnull(reader, PointCache, ocache)) {
3921 direct_link_pointcache(reader, *ocache);
3922 if (force_disk) {
3923 (*ocache)->flag |= PTCACHE_DISK_CACHE;
3924 (*ocache)->step = 1;
3925 }
3926
3927 ptcaches->first_ = ptcaches->last_ = *ocache;
3928 }
3929 }
3930}
3931
3932} // namespace blender
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
General operations, lookup, etc. for blender objects.
#define PSYS_RESET_ALL
#define PSYS_RESET_DEPSGRAPH
#define PTCACHE_TYPEFLAG_TYPEMASK
#define PTCACHE_FILE_READ
#define PTCACHE_TYPE_SMOKE_HIGHRES
#define PTCACHE_VEL_PER_SEC
#define PTCACHE_FILE_WRITE
#define PTCACHE_CLEAR_BEFORE
#define PTCACHE_CLEAR_AFTER
#define PTCACHE_EXT
#define PTCACHE_TYPE_CLOTH
#define PTCACHE_TYPE_DYNAMICPAINT
#define PTCACHE_CLEAR_ALL
#define PTCACHE_READ_INTERPOLATED
#define PTCACHE_TYPE_PARTICLES
#define PTCACHE_TYPEFLAG_FLAGMASK
#define PTCACHE_TYPE_SOFTBODY
#define PTCACHE_READ_OLD
#define PTCACHE_TYPEFLAG_COMPRESS
#define PTCACHE_FILE_UPDATE
#define PTCACHE_CLEAR_FRAME
#define PTCACHE_TYPE_SMOKE_DOMAIN
#define PTCACHE_RESET_OUTDATED
#define PTCACHE_PATH
#define PTCACHE_TYPE_RIGIDBODY
#define PTCACHE_RESET_DEPSGRAPH
#define PTCACHE_RESET_BAKED
#define PTCACHE_READ_EXACT
#define PTCACHE_TYPEFLAG_EXTRADATA
#define SETLOOPER_VIEW_LAYER(_bmain, _sce_basis, _view_layer, _sce_iter, _base)
Definition BKE_scene.hh:50
File and directory operations.
#define BLI_path_join(...)
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE
Definition BLI_string.hh:31
#define SNPRINTF(dst, format,...)
#define BLI_STR_FORMAT_INT32_GROUPED_SIZE
Definition BLI_string.hh:28
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
Platform independent time functions.
Compatibility-like things for windows.
struct __dirstream DIR
struct dirent * readdir(DIR *dp)
int closedir(DIR *dp)
const char * dirname(char *path)
DIR * opendir(const char *path)
#define BLO_read_struct_nonnull(reader, struct_name, ptr_p)
#define BLO_read_struct_list(reader, struct_name, list)
#define RPT_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
ID and Library types, which are fundamental for SDNA.
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
#define MAXFRAME
Read Blender memory allocation and freeing.
Rigid Body API for interfacing with external Physics Engines.
#define UNPACK4(a)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define STREQLEN(a, b, n)
#define ELEM(...)
#define STREQ(a, b)
void reset()
const T * data() const
Definition BLI_array.hh:323
#define rot(x, k)
#define str(s)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
#define INT_MAX
#define floor
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
unsigned int uchar
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
int BKE_object_insert_ptcache(Object *ob)
Definition object.cc:4592
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
static void ptcache_extra_free(PTCacheMem *pm)
static bool ptcache_file_read(PTCacheFile *pf, void *f, size_t items_num, size_t item_size)
void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
static void ptcache_softbody_error(const ID *, void *, const char *)
static int ptcache_write(PTCacheID *pid, int cfra, int overwrite)
__int64 int64_t
Definition BKE_text.h:90
static bool foreach_object_particle_ptcache(Object *object, PointCacheIdFn callback)
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
static int ptcache_basic_header_write(PTCacheFile *pf)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
static int ptcache_frame_from_filename(const char *filename, const char *ext)
static bool ptcache_data_alloc(PTCacheMem *pm, const bool zero_init)
int BKE_ptcache_path(PTCacheID *pid, char *dirname)
static int ptcache_dynamicpaint_totpoint(void *sd, int)
static void ptcache_cloth_extra_write(void *cloth_v, PTCacheMem *pm, int)
static void direct_link_pointcache(BlendDataReader *reader, PointCache *cache)
static void ptcache_dynamicpaint_error(const ID *, void *, const char *)
static int ptcache_particle_totwrite(void *psys_v, int cfra)
int BKE_ptcache_object_reset(struct Scene *scene, struct Object *ob, int mode)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static int ptcache_rigidbody_write(int index, void *rb_v, void **data, int)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
static bool ptcache_file_compressed_read(PTCacheFile *pf, uchar *result, size_t items_num, size_t item_size)
static int ptcache_dynamicpaint_write(PTCacheFile *pf, void *dp_v)
static int ptcache_interpolate(PTCacheID *pid, float cfra, int cfra1, int cfra2)
void dynamicPaint_clearSurface(const struct Scene *scene, struct DynamicPaintSurface *surface)
static int ptcache_write_needed(PTCacheID *pid, int cfra, int *overwrite)
static void ptcache_particle_read(int index, void *psys_v, void **data, float cfra, const float *old_data)
void sbFreeSimulation(struct SoftBody *sb)
Definition softbody.cc:3194
void BKE_ptcache_disk_to_mem(struct PTCacheID *pid)
void vec_to_quat(float q[4], const float vec[3], short axis, short upflag)
int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *pid, int mode)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BKE_ptcache_mem_index_find(struct PTCacheMem *pm, unsigned int index)
void cloth_free_modifier(ClothModifierData *clmd)
Definition cloth.cc:445
float BKE_scene_frame_to_ctime(const Scene *scene, int frame)
Definition scene.cc:2586
static const char * ptcache_file_extension(const PTCacheID *pid)
static int ptcache_write_stream(PTCacheID *pid, int cfra, int totpoint)
@ eModifierType_DynamicPaint
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:201
void BKE_ptcache_load_external(struct PTCacheID *pid)
static void ptcache_cloth_error(const ID *owner_id, void *cloth_v, const char *message)
void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct FluidModifierData *fmd)
bool BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:1063
static void ptcache_particle_error(const ID *, void *, const char *)
bool BKE_ptcache_object_has(struct Scene *scene, struct Object *ob, int duplis)
@ PTCACHE_FILE_PTCACHE
void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd)
void MEM_delete_void(void *vmemh)
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1283
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BKE_object_delete_ptcache(Object *ob, int index)
Definition object.cc:4640
@ LIB_ID_COPY_CACHES
void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw)
MINLINE void mul_v3_fl(float r[3], float f)
static int ptcache_particle_totpoint(void *psys_v, int)
void filter_transpose_delta(const uint8_t *src, uint8_t *dst, size_t items_num, size_t item_size)
static void ptcache_rigidbody_interpolate(int index, void *rb_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
@ FLUID_DOMAIN_CACHE_REPLAY
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
void BKE_ptcache_free(struct PointCache *cache)
Global G
Definition blender.cc:57
static void ptcache_mem_clear(PTCacheMem *pm)
static bool ptcache_file_write(PTCacheFile *pf, const void *data, size_t items_num, size_t item_size)
void BKE_ptcache_blend_read_data(struct BlendDataReader *reader, ListBaseT< PointCache > *ptcaches, struct PointCache **ocache, int force_disk)
void BIK_clear_cache(struct bPose *pose)
static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int)
void unit_qt(float q[4])
static void ptcache_rigidbody_read(int index, void *rb_v, void **data, float, const float *old_data)
static int ptcache_file_header_begin_read(PTCacheFile *pf)
void BKE_ptcache_quick_cache_all(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
unsigned int uint32_t
Definition BKE_text.h:81
static void ptcache_cloth_read(int index, void *cloth_v, void **data, float, const float *old_data)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BKE_ptcache_mem_pointers_incr(void *cur[BPHYS_TOT_DATA])
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
static int ptcache_basic_header_read(PTCacheFile *pf)
static int ptcache_read_stream(PTCacheID *pid, int cfra)
unsigned char uchar
Definition BKE_text.h:58
int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
static void ptcache_cloth_extra_read(void *cloth_v, PTCacheMem *pm, float)
static int ptcache_rigidbody_totpoint(void *rb_v, int)
BMesh const char void * data
static bool ptcache_file_compressed_write(PTCacheFile *pf, const void *data, size_t items_num, size_t item_size)
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
unsigned char uint8_t
Definition BKE_text.h:79
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
static void ptcache_data_free(PTCacheMem *pm)
void BKE_ptcache_bake(struct PTCacheBaker *baker)
static int ptcache_dynamicpaint_read(PTCacheFile *pf, void *dp_v)
void BKE_ptcache_id_time(PTCacheID *pid, struct Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
static int ptcache_filepath(PTCacheID *pid, char filepath[MAX_PTCACHE_FILE], int cfra, const bool do_path, const bool do_ext)
static PointCache * ptcache_copy(PointCache *cache, const bool copy_data)
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
static size_t ptcache_filepath_ext_append(PTCacheID *pid, char filepath[MAX_PTCACHE_FILE], const size_t filepath_len, const bool use_frame_number, const int cfra)
void BKE_ptcache_ids_from_object(ListBaseT< PTCacheID > *lb, struct Object *ob, struct Scene *scene, int duplis)
static void ptcache_softbody_interpolate(int index, void *soft_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
ATTR_WARN_UNUSED_RESULT const size_t num
static int ptcache_softbody_write(int index, void *soft_v, void **data, int)
static int ptcache_cloth_totpoint(void *cloth_v, int)
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, struct Object *ob, struct DynamicPaintSurface *surface)
MINLINE void zero_v3(float r[3])
static bool foreach_object_modifier_ptcache(Object *object, PointCacheIdFn callback)
static void ptcache_rigidbody_error(const ID *, void *, const char *)
static PTCacheFile * ptcache_file_open(PTCacheID *pid, int mode, int cfra)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
static int ptcache_data_size[]
void BKE_ptcache_make_particle_key(struct ParticleKey *key, int index, void **data, float time)
size_t BLI_str_format_int_grouped(char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE], int num) ATTR_NONNULL(1)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BKE_ptcache_validate(struct PointCache *cache, int framenr)
void BKE_ptcache_foreach_object_cache(struct Object &ob, struct Scene &scene, bool duplis, PointCacheIdFn fn)
static bool ptcache_file_points_read(PTCacheFile *pf, PTCacheMem *pm)
void BKE_ptcache_free_mem(ListBaseT< PTCacheMem > *mem_cache)
void BKE_ptcache_id_from_particles(PTCacheID *pid, struct Object *ob, struct ParticleSystem *psys)
static void ptcache_find_frames_around(PTCacheID *pid, uint frame, int *r_fra1, int *r_fra2)
constexpr int MAX_DUPLI_RECUR
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void BKE_ptcache_free_list(ListBaseT< PointCache > *ptcaches)
static int ptcache_read(PTCacheID *pid, int cfra)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static bool foreach_object_ptcache(Scene *scene, Object *object, int duplis, PointCacheIdFn callback)
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static int ptcache_mem_frame_to_disk(PTCacheID *pid, PTCacheMem *pm)
static void ptcache_file_pointers_init(PTCacheFile *pf)
void BKE_ptcache_mem_to_disk(struct PTCacheID *pid)
@ ID_RECALC_PSYS_CHILD
Definition DNA_ID.h:1085
@ ID_RECALC_PSYS_REDO
Definition DNA_ID.h:1081
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static int ptcache_softbody_totpoint(void *soft_v, int)
static void ptcache_particle_interpolate(int index, void *psys_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
struct PointCache * BKE_ptcache_add(ListBaseT< PointCache > *ptcaches)
static void ptcache_add_extra_data(PTCacheMem *pm, ePointCache_ExtraDataType type, uint count, void *data)
struct PointCache * BKE_ptcache_copy_list(ListBaseT< PointCache > *ptcaches_new, const ListBaseT< PointCache > *ptcaches_old, int flag)
static CLG_LogRef LOG
Definition logging.cpp:12
double BLI_time_now_seconds()
Definition time.cc:30
void BKE_ptcache_blend_write(struct BlendWriter *writer, ListBaseT< PointCache > *ptcaches)
static void ptcache_file_close(PTCacheFile *pf)
PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache)
static int ptcache_extra_datasize[]
void psys_reset(struct ParticleSystem *psys, int mode)
static PTCacheMem * ptcache_disk_frame_to_mem(PTCacheID *pid, int cfra)
void psys_get_pointcache_start_end(struct Scene *scene, ParticleSystem *psys, int *sfra, int *efra)
void BKE_ptcache_update_info(PTCacheID *pid)
void BKE_ptcache_id_from_softbody(PTCacheID *pid, struct Object *ob, struct SoftBody *sb)
void BKE_ptcache_disk_cache_rename(struct PTCacheID *pid, const char *name_src, const char *name_dst)
int BKE_ptcache_data_size(int data_type)
static int ptcache_cloth_write(int index, void *cloth_v, void **data, int)
static int ptcache_particle_write(int index, void *psys_v, void **data, int cfra)
int BKE_ptcache_mem_pointers_seek(int point_index, struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
void BKE_ptcache_toggle_disk_cache(struct PTCacheID *pid)
static void ptcache_particle_extra_read(void *psys_v, PTCacheMem *pm, float)
void unfilter_transpose_delta(const uint8_t *src, uint8_t *dst, size_t items_num, size_t item_size)
void psys_interpolate_particle(short type, struct ParticleKey keys[4], float dt, struct ParticleKey *result, bool velocity)
Definition particle.cc:1173
@ PTCACHE_COMPRESS_ZSTD_FAST_DEPRECATED
@ PTCACHE_COMPRESS_ZSTD_SLOW_DEPRECATED
@ PTCACHE_COMPRESS_ZSTD_FILTERED
void BKE_ptcache_invalidate(struct PointCache *cache)
struct ParticleSystem * psys_eval_get(struct Depsgraph *depsgraph, struct Object *object, struct ParticleSystem *psys)
Definition particle.cc:697
static void ptcache_softbody_read(int index, void *soft_v, void **data, float, const float *old_data)
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
static int ptcache_file_header_begin_write(PTCacheFile *pf)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
static int ptcache_old_elemsize(PTCacheID *pid)
void BKE_ptcache_mem_pointers_init(struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
@ BPHYS_EXTRA_CLOTH_ACCELERATION
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void ptcache_cloth_interpolate(int index, void *cloth_v, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
static void ptcache_dt_to_str(char *str, size_t str_maxncpy, double dtime)
static void direct_link_pointcache_mem(BlendDataReader *reader, PTCacheMem *pm)
FunctionRef< bool(PTCacheID &pid, ModifierData *md)> PointCacheIdFn
#define PTCACHE_DATA_FROM(data, type, from)
Definition pointcache.cc:84
#define MAX_PTCACHE_PATH
#define MAX_PTCACHE_FILE
#define PTCACHE_DATA_TO(data, type, index, to)
Definition pointcache.cc:90
#define DPAINT_CACHE_VERSION
void RB_body_get_orientation(rbRigidBody *object, float v_out[4])
void RB_body_get_position(rbRigidBody *object, float v_out[3])
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct Object * object
void write_struct_array_cast(const int64_t array_size, const void *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_float_array(int64_t num, const float *data)
void write_uint32_array(int64_t num, const uint32_t *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
struct ClothHairData * hairdata
struct PointCache * point_cache
struct ListBaseT< PointCache > ptcaches
unsigned int mvert_num
Definition BKE_cloth.hh:74
ListBaseT< DynamicPaintSurface > surfaces
struct DynamicPaintCanvasSettings * canvas
eDynamicPaint_SurfaceFormat format
struct DynamicPaintSurface * next
struct ListBaseT< PointCache > ptcaches[2]
struct PointCache * point_cache[2]
eFluidDomain_CacheType cache_type
struct FluidDomainSettings * domain
char name[258]
Definition DNA_ID.h:433
struct Library * lib
Definition DNA_ID.h:421
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
ListBaseT< ModifierData > modifiers
ListBaseT< struct ParticleSystem > particlesystem
struct RigidBodyOb * rigidbody_object
struct bPose * pose
struct RigidBodyCon * rigidbody_constraint
struct SoftBody * soft
struct PTCacheID pid
struct Depsgraph * depsgraph
void(*) update_progress(void *data, float progress, int *cancel)
struct ViewLayer * view_layer
struct PTCacheExtra * next
ePointCache_ExtraDataType type
int(*) totwrite(void *calldata, int cfra)
unsigned int stack_index
void(*) read_extra_data(void *calldata, struct PTCacheMem *pm, float cfra)
int(*) read_stream(PTCacheFile *pf, void *calldata)
void(*) write_extra_data(void *calldata, struct PTCacheMem *pm, int cfra)
struct PointCache * cache
void(*) interpolate_point(int index, void *calldata, void **data, float cfra, float cfra1, float cfra2, const float *old_data)
unsigned int max_step
void(*) interpolate_extra_data(void *calldata, struct PTCacheMem *pm, float cfra, float cfra1, float cfra2)
unsigned int default_step
struct PointCache ** cache_ptr
int(*) write_header(PTCacheFile *pf)
struct PTCacheID * next
void(*) error(const struct ID *owner_id, void *calldata, const char *message)
int(*) read_header(PTCacheFile *pf)
unsigned int file_type
int(*) totpoint(void *calldata, int cfra)
void(*) read_point(int index, void *calldata, void **data, float cfra, const float *old_data)
int(*) write_point(int index, void *calldata, void **data, int cfra)
ListBaseT< PointCache > * ptcaches
int(*) write_stream(PTCacheFile *pf, void *calldata)
unsigned int info_types
unsigned int data_types
struct PTCacheMem * prev
struct PTCacheMem * next
ListBaseT< PTCacheExtra > extradata
struct SPHFluidSettings * fluid
eParticleRotationMode rotmode
eParticlePhysicsType phystype
eParticleAngularVelocityMode avemode
struct ClothModifierData * clmd
ListBaseT< PointCache > ptcaches
struct PointCache * pointcache
ListBaseT< PTCacheMem > mem_cache
void(*) free_edit(struct PTCacheEdit *edit)
struct PointCache * next
struct PTCacheEdit * edit
struct RigidBodyOb_Shared * shared
struct RigidBodyWorld_Shared * shared
struct PhysicsSettings physics_settings
struct RenderData r
struct RigidBodyWorld * rigidbody_world
ListBaseT< PointCache > ptcaches
struct SoftBody_Shared * shared
struct BodyPoint * bpoint
char * d_name