Refactor: move PointCache .blend I/O to blenkernel

Ref T76372.
This commit is contained in:
Jacques Lucke
2020-11-06 14:40:49 +01:00
parent 6bca9d8c11
commit 7dd76329e1
4 changed files with 149 additions and 149 deletions

View File

@@ -91,6 +91,8 @@ struct RigidBodyWorld;
struct Scene;
struct SoftBody;
struct ViewLayer;
struct BlendWriter;
struct BlendDataReader;
/* temp structure for read/write */
typedef struct PTCacheData {
@@ -374,6 +376,14 @@ void BKE_ptcache_validate(struct PointCache *cache, int framenr);
/* Set correct flags after unsuccessful simulation step */
void BKE_ptcache_invalidate(struct PointCache *cache);
/********************** .blend File I/O *********************/
void BKE_ptcache_blend_write(struct BlendWriter *writer, struct ListBase *ptcaches);
void BKE_ptcache_blend_read_data(struct BlendDataReader *reader,
struct ListBase *ptcaches,
struct PointCache **ocache,
int force_disk);
#ifdef __cplusplus
}
#endif

View File

@@ -44,6 +44,7 @@
#include "DNA_simulation_types.h"
#include "BLI_blenlib.h"
#include "BLI_endian_switch.h"
#include "BLI_math.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"
@@ -67,6 +68,8 @@
#include "BKE_scene.h"
#include "BKE_softbody.h"
#include "BLO_read_write.h"
#include "BIK_api.h"
#ifdef WITH_BULLET
@@ -3779,3 +3782,124 @@ void BKE_ptcache_invalidate(PointCache *cache)
cache->last_exact = MIN2(cache->startframe, 0);
}
}
static const char *ptcache_data_struct[] = {
"", // BPHYS_DATA_INDEX
"", // BPHYS_DATA_LOCATION
"", // BPHYS_DATA_VELOCITY
"", // BPHYS_DATA_ROTATION
"", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
"", // BPHYS_DATA_SIZE:
"", // BPHYS_DATA_TIMES:
"BoidData", // case BPHYS_DATA_BOIDS:
};
static const char *ptcache_extra_struct[] = {
"",
"ParticleSpring",
"vec3f",
};
void BKE_ptcache_blend_write(BlendWriter *writer, ListBase *ptcaches)
{
LISTBASE_FOREACH (PointCache *, cache, ptcaches) {
BLO_write_struct(writer, PointCache, cache);
if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
LISTBASE_FOREACH (PTCacheMem *, pm, &cache->mem_cache) {
BLO_write_struct(writer, PTCacheMem, pm);
for (int i = 0; i < BPHYS_TOT_DATA; i++) {
if (pm->data[i] && pm->data_types & (1 << i)) {
if (ptcache_data_struct[i][0] == '\0') {
BLO_write_raw(writer, MEM_allocN_len(pm->data[i]), pm->data[i]);
}
else {
BLO_write_struct_array_by_name(
writer, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
}
}
}
LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
if (ptcache_extra_struct[extra->type][0] == '\0') {
continue;
}
BLO_write_struct(writer, PTCacheExtra, extra);
BLO_write_struct_array_by_name(
writer, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
}
}
}
}
}
static void direct_link_pointcache_cb(BlendDataReader *reader, void *data)
{
PTCacheMem *pm = data;
for (int i = 0; i < BPHYS_TOT_DATA; i++) {
BLO_read_data_address(reader, &pm->data[i]);
/* the cache saves non-struct data without DNA */
if (pm->data[i] && ptcache_data_struct[i][0] == '\0' &&
BLO_read_requires_endian_switch(reader)) {
/* data_size returns bytes. */
int tot = (BKE_ptcache_data_size(i) * pm->totpoint) / sizeof(int);
int *poin = pm->data[i];
BLI_endian_switch_int32_array(poin, tot);
}
}
BLO_read_list(reader, &pm->extradata);
LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
BLO_read_data_address(reader, &extra->data);
}
}
static void direct_link_pointcache(BlendDataReader *reader, PointCache *cache)
{
if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
BLO_read_list_cb(reader, &cache->mem_cache, direct_link_pointcache_cb);
}
else {
BLI_listbase_clear(&cache->mem_cache);
}
cache->flag &= ~PTCACHE_SIMULATION_VALID;
cache->simframe = 0;
cache->edit = NULL;
cache->free_edit = NULL;
cache->cached_frames = NULL;
cache->cached_frames_len = 0;
}
void BKE_ptcache_blend_read_data(BlendDataReader *reader,
ListBase *ptcaches,
PointCache **ocache,
int force_disk)
{
if (ptcaches->first) {
BLO_read_list(reader, ptcaches);
LISTBASE_FOREACH (PointCache *, cache, ptcaches) {
direct_link_pointcache(reader, cache);
if (force_disk) {
cache->flag |= PTCACHE_DISK_CACHE;
cache->step = 1;
}
}
BLO_read_data_address(reader, ocache);
}
else if (*ocache) {
/* old "single" caches need to be linked too */
BLO_read_data_address(reader, ocache);
direct_link_pointcache(reader, *ocache);
if (force_disk) {
(*ocache)->flag |= PTCACHE_DISK_CACHE;
(*ocache)->step = 1;
}
ptcaches->first = ptcaches->last = *ocache;
}
}

View File

@@ -2746,90 +2746,6 @@ void blo_do_versions_key_uidgen(Key *key)
/** \name Read ID: Particle Settings
* \{ */
/* update this also to writefile.c */
static const char *ptcache_data_struct[] = {
"", /* BPHYS_DATA_INDEX */
"", /* BPHYS_DATA_LOCATION */
"", /* BPHYS_DATA_VELOCITY */
"", /* BPHYS_DATA_ROTATION */
"", /* BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
"", /* BPHYS_DATA_SIZE: */
"", /* BPHYS_DATA_TIMES: */
"BoidData", /* case BPHYS_DATA_BOIDS: */
};
static void direct_link_pointcache_cb(BlendDataReader *reader, void *data)
{
PTCacheMem *pm = data;
for (int i = 0; i < BPHYS_TOT_DATA; i++) {
BLO_read_data_address(reader, &pm->data[i]);
/* the cache saves non-struct data without DNA */
if (pm->data[i] && ptcache_data_struct[i][0] == '\0' &&
BLO_read_requires_endian_switch(reader)) {
/* data_size returns bytes. */
int tot = (BKE_ptcache_data_size(i) * pm->totpoint) / sizeof(int);
int *poin = pm->data[i];
BLI_endian_switch_int32_array(poin, tot);
}
}
BLO_read_list(reader, &pm->extradata);
LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
BLO_read_data_address(reader, &extra->data);
}
}
static void direct_link_pointcache(BlendDataReader *reader, PointCache *cache)
{
if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
BLO_read_list_cb(reader, &cache->mem_cache, direct_link_pointcache_cb);
}
else {
BLI_listbase_clear(&cache->mem_cache);
}
cache->flag &= ~PTCACHE_SIMULATION_VALID;
cache->simframe = 0;
cache->edit = NULL;
cache->free_edit = NULL;
cache->cached_frames = NULL;
cache->cached_frames_len = 0;
}
static void direct_link_pointcache_list(BlendDataReader *reader,
ListBase *ptcaches,
PointCache **ocache,
int force_disk)
{
if (ptcaches->first) {
BLO_read_list(reader, ptcaches);
LISTBASE_FOREACH (PointCache *, cache, ptcaches) {
direct_link_pointcache(reader, cache);
if (force_disk) {
cache->flag |= PTCACHE_DISK_CACHE;
cache->step = 1;
}
}
BLO_read_data_address(reader, ocache);
}
else if (*ocache) {
/* old "single" caches need to be linked too */
BLO_read_data_address(reader, ocache);
direct_link_pointcache(reader, *ocache);
if (force_disk) {
(*ocache)->flag |= PTCACHE_DISK_CACHE;
(*ocache)->step = 1;
}
ptcaches->first = ptcaches->last = *ocache;
}
}
static void lib_link_particlesystems(BlendLibReader *reader,
Object *ob,
ID *id,
@@ -2941,7 +2857,7 @@ static void direct_link_particlesystems(BlendDataReader *reader, ListBase *parti
psys->clmd->solver_result = NULL;
}
direct_link_pointcache_list(reader, &psys->ptcaches, &psys->pointcache, 0);
BKE_ptcache_blend_read_data(reader, &psys->ptcaches, &psys->pointcache, 0);
if (psys->clmd) {
psys->clmd->point_cache = psys->pointcache;
}
@@ -3411,7 +3327,7 @@ static void direct_link_modifiers(BlendDataReader *reader, ListBase *lb, Object
BLO_read_data_address(reader, &clmd->sim_parms);
BLO_read_data_address(reader, &clmd->coll_parms);
direct_link_pointcache_list(reader, &clmd->ptcaches, &clmd->point_cache, 0);
BKE_ptcache_blend_read_data(reader, &clmd->ptcaches, &clmd->point_cache, 0);
if (clmd->sim_parms) {
if (clmd->sim_parms->presets > 10) {
@@ -3460,7 +3376,7 @@ static void direct_link_modifiers(BlendDataReader *reader, ListBase *lb, Object
fmd->domain->effector_weights = BKE_effector_add_weights(NULL);
}
direct_link_pointcache_list(
BKE_ptcache_blend_read_data(
reader, &(fmd->domain->ptcaches[0]), &(fmd->domain->point_cache[0]), 1);
/* Manta sim uses only one cache from now on, so store pointer convert */
@@ -3523,7 +3439,7 @@ static void direct_link_modifiers(BlendDataReader *reader, ListBase *lb, Object
LISTBASE_FOREACH (DynamicPaintSurface *, surface, &pmd->canvas->surfaces) {
surface->canvas = pmd->canvas;
surface->data = NULL;
direct_link_pointcache_list(reader, &(surface->ptcaches), &(surface->pointcache), 1);
BKE_ptcache_blend_read_data(reader, &(surface->ptcaches), &(surface->pointcache), 1);
BLO_read_data_address(reader, &surface->effector_weights);
if (surface->effector_weights == NULL) {
@@ -3767,11 +3683,11 @@ static void direct_link_object(BlendDataReader *reader, Object *ob)
* We should only do this when sb->shared == NULL, because those pointers
* are always set (for compatibility with older Blenders). We mustn't link
* the same pointcache twice. */
direct_link_pointcache_list(reader, &sb->ptcaches, &sb->pointcache, false);
BKE_ptcache_blend_read_data(reader, &sb->ptcaches, &sb->pointcache, false);
}
else {
/* link caches */
direct_link_pointcache_list(reader, &sb->shared->ptcaches, &sb->shared->pointcache, false);
BKE_ptcache_blend_read_data(reader, &sb->shared->ptcaches, &sb->shared->pointcache, false);
}
}
BLO_read_data_address(reader, &ob->fluidsimSettings); /* NT */
@@ -4357,7 +4273,7 @@ static void direct_link_scene(BlendDataReader *reader, Scene *sce)
* We should only do this when rbw->shared == NULL, because those pointers
* are always set (for compatibility with older Blenders). We mustn't link
* the same pointcache twice. */
direct_link_pointcache_list(reader, &rbw->ptcaches, &rbw->pointcache, false);
BKE_ptcache_blend_read_data(reader, &rbw->ptcaches, &rbw->pointcache, false);
/* make sure simulation starts from the beginning after loading file */
if (rbw->pointcache) {
@@ -4371,7 +4287,7 @@ static void direct_link_scene(BlendDataReader *reader, Scene *sce)
rbw->shared->physics_world = NULL;
/* link caches */
direct_link_pointcache_list(reader, &rbw->shared->ptcaches, &rbw->shared->pointcache, false);
BKE_ptcache_blend_read_data(reader, &rbw->shared->ptcaches, &rbw->shared->pointcache, false);
/* make sure simulation starts from the beginning after loading file */
if (rbw->shared->pointcache) {

View File

@@ -809,56 +809,6 @@ static void write_userdef(BlendWriter *writer, const UserDef *userdef)
}
}
/* update this also to readfile.c */
static const char *ptcache_data_struct[] = {
"", // BPHYS_DATA_INDEX
"", // BPHYS_DATA_LOCATION
"", // BPHYS_DATA_VELOCITY
"", // BPHYS_DATA_ROTATION
"", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
"", // BPHYS_DATA_SIZE:
"", // BPHYS_DATA_TIMES:
"BoidData", // case BPHYS_DATA_BOIDS:
};
static const char *ptcache_extra_struct[] = {
"",
"ParticleSpring",
"vec3f",
};
static void write_pointcaches(BlendWriter *writer, ListBase *ptcaches)
{
LISTBASE_FOREACH (PointCache *, cache, ptcaches) {
BLO_write_struct(writer, PointCache, cache);
if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
LISTBASE_FOREACH (PTCacheMem *, pm, &cache->mem_cache) {
BLO_write_struct(writer, PTCacheMem, pm);
for (int i = 0; i < BPHYS_TOT_DATA; i++) {
if (pm->data[i] && pm->data_types & (1 << i)) {
if (ptcache_data_struct[i][0] == '\0') {
BLO_write_raw(writer, MEM_allocN_len(pm->data[i]), pm->data[i]);
}
else {
BLO_write_struct_array_by_name(
writer, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
}
}
}
LISTBASE_FOREACH (PTCacheExtra *, extra, &pm->extradata) {
if (ptcache_extra_struct[extra->type][0] == '\0') {
continue;
}
BLO_write_struct(writer, PTCacheExtra, extra);
BLO_write_struct_array_by_name(
writer, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
}
}
}
}
}
static void write_particlesystems(BlendWriter *writer, ListBase *particles)
{
LISTBASE_FOREACH (ParticleSystem *, psys, particles) {
@@ -899,7 +849,7 @@ static void write_particlesystems(BlendWriter *writer, ListBase *particles)
BLO_write_struct(writer, ClothCollSettings, psys->clmd->coll_parms);
}
write_pointcaches(writer, &psys->ptcaches);
BKE_ptcache_blend_write(writer, &psys->ptcaches);
}
}
@@ -1055,7 +1005,7 @@ static void write_modifiers(BlendWriter *writer, ListBase *modbase)
BLO_write_struct(writer, ClothSimSettings, clmd->sim_parms);
BLO_write_struct(writer, ClothCollSettings, clmd->coll_parms);
BLO_write_struct(writer, EffectorWeights, clmd->sim_parms->effector_weights);
write_pointcaches(writer, &clmd->ptcaches);
BKE_ptcache_blend_write(writer, &clmd->ptcaches);
}
else if (md->type == eModifierType_Fluid) {
FluidModifierData *fmd = (FluidModifierData *)md;
@@ -1064,14 +1014,14 @@ static void write_modifiers(BlendWriter *writer, ListBase *modbase)
BLO_write_struct(writer, FluidDomainSettings, fmd->domain);
if (fmd->domain) {
write_pointcaches(writer, &(fmd->domain->ptcaches[0]));
BKE_ptcache_blend_write(writer, &(fmd->domain->ptcaches[0]));
/* create fake pointcache so that old blender versions can read it */
fmd->domain->point_cache[1] = BKE_ptcache_add(&fmd->domain->ptcaches[1]);
fmd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE | PTCACHE_FAKE_SMOKE;
fmd->domain->point_cache[1]->step = 1;
write_pointcaches(writer, &(fmd->domain->ptcaches[1]));
BKE_ptcache_blend_write(writer, &(fmd->domain->ptcaches[1]));
if (fmd->domain->coba) {
BLO_write_struct(writer, ColorBand, fmd->domain->coba);
@@ -1108,7 +1058,7 @@ static void write_modifiers(BlendWriter *writer, ListBase *modbase)
}
/* write caches and effector weights */
LISTBASE_FOREACH (DynamicPaintSurface *, surface, &pmd->canvas->surfaces) {
write_pointcaches(writer, &(surface->ptcaches));
BKE_ptcache_blend_write(writer, &(surface->ptcaches));
BLO_write_struct(writer, EffectorWeights, surface->effector_weights);
}
@@ -1265,7 +1215,7 @@ static void write_object(BlendWriter *writer, Object *ob, const void *id_address
ob->soft->ptcaches = ob->soft->shared->ptcaches;
BLO_write_struct(writer, SoftBody, ob->soft);
BLO_write_struct(writer, SoftBody_Shared, ob->soft->shared);
write_pointcaches(writer, &(ob->soft->shared->ptcaches));
BKE_ptcache_blend_write(writer, &(ob->soft->shared->ptcaches));
BLO_write_struct(writer, EffectorWeights, ob->soft->effector_weights);
}
@@ -1500,7 +1450,7 @@ static void write_scene(BlendWriter *writer, Scene *sce, const void *id_address)
BLO_write_struct(writer, RigidBodyWorld_Shared, sce->rigidbody_world->shared);
BLO_write_struct(writer, EffectorWeights, sce->rigidbody_world->effector_weights);
write_pointcaches(writer, &(sce->rigidbody_world->shared->ptcaches));
BKE_ptcache_blend_write(writer, &(sce->rigidbody_world->shared->ptcaches));
}
BKE_previewimg_blend_write(writer, sce->preview);