Make lattice deform safe for threading

Lattice deformation used to store some runtime data
inside of lattice datablock itself. It's something
which is REALLY bad. Ideally DNA shouldn't contain
and runtime data.

For now solved it in a way that initialization of
lattice deform will create a structure which contains
lattice object for which deformation is calculating
and that runtime data which used to be stored in
lattice datablock itself.

It works really fine for mesh deform modifier, but
there's still runtime data stored in particle system
DNA, It didn't look something easy to be solved, so
leaving this as-is for now.

--
svn merge -r58277:58278 -r58795:58796 ^/branches/soc-2013-depsgraph_mt
This commit is contained in:
2013-08-19 10:11:48 +00:00
parent b9ae749480
commit c46cbc602e
13 changed files with 109 additions and 90 deletions

View File

@@ -49,9 +49,14 @@ void BKE_lattice_free(struct Lattice *lt);
void BKE_lattice_make_local(struct Lattice *lt);
void calc_lat_fudu(int flag, int res, float *r_fu, float *r_du);
void init_latt_deform(struct Object *oblatt, struct Object *ob);
void calc_latt_deform(struct Object *, float co[3], float weight);
void end_latt_deform(struct Object *);
struct LatticeDeformData;
struct LatticeDeformData *init_latt_deform(struct Object *oblatt, struct Object *ob)
#ifdef __GNUC__
__attribute__((warn_unused_result))
#endif
;
void calc_latt_deform(struct LatticeDeformData *lattice_deform_data, float co[3], float weight);
void end_latt_deform(struct LatticeDeformData *lattice_deform_data);
int object_deform_mball(struct Object *ob, struct ListBase *dispbase);
void outside_lattice(struct Lattice *lt);

View File

@@ -54,6 +54,7 @@ struct MCol;
struct MFace;
struct MVert;
struct IpoCurve;
struct LatticeDeformData;
struct LinkNode;
struct KDTree;
struct RNG;
@@ -258,7 +259,7 @@ void psys_set_current_num(Object *ob, int index);
/* UNUSED */
// struct Object *psys_find_object(struct Scene *scene, struct ParticleSystem *psys);
struct Object *psys_get_lattice(struct ParticleSimulationData *sim);
struct LatticeDeformData *psys_create_lattice_deform_data(struct ParticleSimulationData *sim);
int psys_in_edit_mode(struct Scene *scene, struct ParticleSystem *psys);
int psys_check_enabled(struct Object *ob, struct ParticleSystem *psys);

View File

@@ -1330,7 +1330,7 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
psys_check_group_weights(part);
psys->lattice = psys_get_lattice(&sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
/* gather list of objects or single object */
if (part->ren_as == PART_DRAW_GR) {
@@ -1567,9 +1567,9 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
if (obcopylist)
MEM_freeN(obcopylist);
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice = NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
}

View File

@@ -307,7 +307,13 @@ void BKE_lattice_make_local(Lattice *lt)
}
}
void init_latt_deform(Object *oblatt, Object *ob)
typedef struct LatticeDeformData {
Object *object;
float *latticedata;
float latmat[4][4];
} LatticeDeformData;
LatticeDeformData *init_latt_deform(Object *oblatt, Object *ob)
{
/* we make an array with all differences */
Lattice *lt = oblatt->data;
@@ -317,27 +323,30 @@ void init_latt_deform(Object *oblatt, Object *ob)
float *fp, imat[4][4];
float fu, fv, fw;
int u, v, w;
float *latticedata;
float latmat[4][4];
LatticeDeformData *lattice_deform_data;
if (lt->editlatt) lt = lt->editlatt->latt;
bp = lt->def;
fp = lt->latticedata = MEM_mallocN(sizeof(float) * 3 * lt->pntsu * lt->pntsv * lt->pntsw, "latticedata");
fp = latticedata = MEM_mallocN(sizeof(float) * 3 * lt->pntsu * lt->pntsv * lt->pntsw, "latticedata");
/* for example with a particle system: (ob == NULL) */
if (ob == NULL) {
/* in deformspace, calc matrix */
invert_m4_m4(lt->latmat, oblatt->obmat);
invert_m4_m4(latmat, oblatt->obmat);
/* back: put in deform array */
invert_m4_m4(imat, lt->latmat);
invert_m4_m4(imat, latmat);
}
else {
/* in deformspace, calc matrix */
invert_m4_m4(imat, oblatt->obmat);
mul_m4_m4m4(lt->latmat, imat, ob->obmat);
mul_m4_m4m4(latmat, imat, ob->obmat);
/* back: put in deform array */
invert_m4_m4(imat, lt->latmat);
invert_m4_m4(imat, latmat);
}
for (w = 0, fw = lt->fw; w < lt->pntsw; w++, fw += lt->dw) {
@@ -358,10 +367,18 @@ void init_latt_deform(Object *oblatt, Object *ob)
}
}
}
lattice_deform_data = MEM_mallocN(sizeof(LatticeDeformData), "Lattice Deform Data");
lattice_deform_data->latticedata = latticedata;
lattice_deform_data->object = oblatt;
copy_m4_m4(lattice_deform_data->latmat, latmat);
return lattice_deform_data;
}
void calc_latt_deform(Object *ob, float co[3], float weight)
void calc_latt_deform(LatticeDeformData *lattice_deform_data, float co[3], float weight)
{
Object *ob = lattice_deform_data->object;
Lattice *lt = ob->data;
float u, v, w, tu[4], tv[4], tw[4];
float vec[3];
@@ -375,7 +392,7 @@ void calc_latt_deform(Object *ob, float co[3], float weight)
if (lt->editlatt) lt = lt->editlatt->latt;
if (lt->latticedata == NULL) return;
if (lattice_deform_data->latticedata == NULL) return;
if (lt->vgroup[0] && dvert) {
defgrp_index = defgroup_name_index(ob, lt->vgroup);
@@ -383,7 +400,7 @@ void calc_latt_deform(Object *ob, float co[3], float weight)
}
/* co is in local coords, treat with latmat */
mul_v3_m4v3(vec, lt->latmat, co);
mul_v3_m4v3(vec, lattice_deform_data->latmat, co);
/* u v w coords */
@@ -456,7 +473,7 @@ void calc_latt_deform(Object *ob, float co[3], float weight)
idx_u = idx_v;
}
madd_v3_v3fl(co, &lt->latticedata[idx_u * 3], u);
madd_v3_v3fl(co, &lattice_deform_data->latticedata[idx_u * 3], u);
if (defgrp_index != -1)
weight_blend += (u * defvert_find_weight(dvert + idx_u, defgrp_index));
@@ -472,15 +489,12 @@ void calc_latt_deform(Object *ob, float co[3], float weight)
}
void end_latt_deform(Object *ob)
void end_latt_deform(LatticeDeformData *lattice_deform_data)
{
Lattice *lt = ob->data;
if (lattice_deform_data->latticedata)
MEM_freeN(lattice_deform_data->latticedata);
if (lt->editlatt) lt = lt->editlatt->latt;
if (lt->latticedata)
MEM_freeN(lt->latticedata);
lt->latticedata = NULL;
MEM_freeN(lattice_deform_data);
}
/* calculations is in local space of deformed object
@@ -815,13 +829,14 @@ void curve_deform_vector(Scene *scene, Object *cuOb, Object *target,
void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
float (*vertexCos)[3], int numVerts, const char *vgroup, float fac)
{
LatticeDeformData *lattice_deform_data;
int a;
int use_vgroups;
if (laOb->type != OB_LATTICE)
return;
init_latt_deform(laOb, target);
lattice_deform_data = init_latt_deform(laOb, target);
/* check whether to use vertex groups (only possible if target is a Mesh)
* we want either a Mesh with no derived data, or derived data with
@@ -855,16 +870,16 @@ void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
weight = defvert_find_weight(dvert, defgrp_index);
if (weight > 0.0f)
calc_latt_deform(laOb, vertexCos[a], weight * fac);
calc_latt_deform(lattice_deform_data, vertexCos[a], weight * fac);
}
}
}
else {
for (a = 0; a < numVerts; a++) {
calc_latt_deform(laOb, vertexCos[a], fac);
calc_latt_deform(lattice_deform_data, vertexCos[a], fac);
}
}
end_latt_deform(laOb);
end_latt_deform(lattice_deform_data);
}
int object_deform_mball(Object *ob, ListBase *dispbase)

View File

@@ -221,12 +221,12 @@ Object *psys_find_object(Scene *scene, ParticleSystem *psys)
}
#endif
Object *psys_get_lattice(ParticleSimulationData *sim)
struct LatticeDeformData *psys_create_lattice_deform_data(ParticleSimulationData *sim)
{
Object *lattice = NULL;
struct LatticeDeformData *lattice_deform_data = NULL;
if (psys_in_edit_mode(sim->scene, sim->psys) == 0) {
Object *lattice = NULL;
ModifierData *md = (ModifierData *)psys_get_modifier(sim->ob, sim->psys);
for (; md; md = md->next) {
@@ -237,10 +237,10 @@ Object *psys_get_lattice(ParticleSimulationData *sim)
}
}
if (lattice)
init_latt_deform(lattice, NULL);
lattice_deform_data = init_latt_deform(lattice, NULL);
}
return lattice;
return lattice_deform_data;
}
void psys_disable_all(Object *ob)
{
@@ -2513,7 +2513,7 @@ static int psys_threads_init_path(ParticleThread *threads, Scene *scene, float c
ctx->cfra = cfra;
ctx->editupdate = editupdate;
psys->lattice = psys_get_lattice(&ctx->sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&ctx->sim);
/* cache all relevant vertex groups if they exist */
ctx->vg_length = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_LENGTH);
@@ -2974,7 +2974,7 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra)
psys_free_path_cache(psys, psys->edit);
cache = psys->pathcache = psys_alloc_path_cache_buffers(&psys->pathcachebufs, totpart, steps + 1);
psys->lattice = psys_get_lattice(sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(sim);
ma = give_current_material(sim->ob, psys->part->omat);
if (ma && (psys->part->draw_col == PART_DRAW_COL_MAT))
copy_v3_v3(col, &ma->r);
@@ -3079,9 +3079,9 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra)
}
/* lattices have to be calculated separately to avoid mixups between effector calculations */
if (psys->lattice) {
if (psys->lattice_deform_data) {
for (k = 0, ca = cache[p]; k <= steps; k++, ca++)
calc_latt_deform(psys->lattice, ca->co, 1.0f);
calc_latt_deform(psys->lattice_deform_data, ca->co, 1.0f);
}
}
@@ -3112,9 +3112,9 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra)
psys->totcached = totpart;
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice = NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
if (vg_effector)
@@ -4166,8 +4166,8 @@ void psys_get_particle_on_path(ParticleSimulationData *sim, int p, ParticleKey *
/* TODO: proper velocity handling */
}
if (psys->lattice && edit == 0)
calc_latt_deform(psys->lattice, state->co, 1.0f);
if (psys->lattice_deform_data && edit == 0)
calc_latt_deform(psys->lattice_deform_data, state->co, 1.0f);
}
}
}
@@ -4402,8 +4402,8 @@ int psys_get_particle_state(ParticleSimulationData *sim, int p, ParticleKey *sta
unit_m4(mat);
do_child_modifiers(sim, NULL, key1, key1->rot, cpa, cpa->fuv, mat, state, t);
if (psys->lattice)
calc_latt_deform(sim->psys->lattice, state->co, 1.0f);
if (psys->lattice_deform_data)
calc_latt_deform(psys->lattice_deform_data, state->co, 1.0f);
}
else {
if (pa->state.time == cfra || ELEM(part->phystype, PART_PHYS_NO, PART_PHYS_KEYED))
@@ -4461,8 +4461,8 @@ int psys_get_particle_state(ParticleSimulationData *sim, int p, ParticleKey *sta
}
}
if (sim->psys->lattice)
calc_latt_deform(sim->psys->lattice, state->co, 1.0f);
if (sim->psys->lattice_deform_data)
calc_latt_deform(sim->psys->lattice_deform_data, state->co, 1.0f);
}
return 1;
@@ -4678,9 +4678,9 @@ void psys_apply_hair_lattice(Scene *scene, Object *ob, ParticleSystem *psys)
sim.psys = psys;
sim.psmd = psys_get_modifier(ob, psys);
psys->lattice = psys_get_lattice(&sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
if (psys->lattice) {
if (psys->lattice_deform_data) {
ParticleData *pa = psys->particles;
HairKey *hkey;
int p, h;
@@ -4693,13 +4693,13 @@ void psys_apply_hair_lattice(Scene *scene, Object *ob, ParticleSystem *psys)
hkey = pa->hair;
for (h = 0; h < pa->totkey; h++, hkey++) {
mul_m4_v3(hairmat, hkey->co);
calc_latt_deform(psys->lattice, hkey->co, 1.0f);
calc_latt_deform(psys->lattice_deform_data, hkey->co, 1.0f);
mul_m4_v3(imat, hkey->co);
}
}
end_latt_deform(psys->lattice);
psys->lattice = NULL;
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
/* protect the applied shape */
psys->flag |= PSYS_EDITED;

View File

@@ -1514,9 +1514,9 @@ void psys_threads_free(ParticleThread *threads)
if (ctx->vg_roughe)
MEM_freeN(ctx->vg_roughe);
if (ctx->sim.psys->lattice) {
end_latt_deform(ctx->sim.psys->lattice);
ctx->sim.psys->lattice= NULL;
if (ctx->sim.psys->lattice_deform_data) {
end_latt_deform(ctx->sim.psys->lattice_deform_data);
ctx->sim.psys->lattice_deform_data = NULL;
}
/* distribution */
@@ -4108,7 +4108,7 @@ static void save_hair(ParticleSimulationData *sim, float UNUSED(cfra))
invert_m4_m4(ob->imat, ob->obmat);
psys->lattice= psys_get_lattice(sim);
psys->lattice_deform_data= psys_create_lattice_deform_data(sim);
if (psys->totpart==0) return;
@@ -4479,7 +4479,7 @@ static void cached_step(ParticleSimulationData *sim, float cfra)
if (part->randsize > 0.0f)
pa->size *= 1.0f - part->randsize * PSYS_FRAND(p + 1);
psys->lattice= psys_get_lattice(sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(sim);
dietime = pa->dietime;
@@ -4494,9 +4494,9 @@ static void cached_step(ParticleSimulationData *sim, float cfra)
else
pa->alive = PARS_ALIVE;
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice= NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
if (PSYS_FRAND(p) > disp)
@@ -4777,9 +4777,9 @@ static void system_step(ParticleSimulationData *sim, float cfra)
update_children(sim);
/* cleanup */
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice= NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
}

View File

@@ -4569,7 +4569,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
pdd->ma_col = ma_col;
}
psys->lattice = psys_get_lattice(&sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
/* circles don't use drawdata, so have to add a special case here */
if ((pdd || draw_as == PART_DRAW_CIRC) && draw_as != PART_DRAW_PATH) {
@@ -4895,9 +4895,9 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
pdd->flag &= ~PARTICLE_DRAW_DATA_UPDATED;
}
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice = NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
if (pdd) {

View File

@@ -68,10 +68,6 @@ typedef struct Lattice {
struct MDeformVert *dvert;
char vgroup[64]; /* multiply the influence, MAX_VGROUP_NAME */
/* used while deforming, always free and NULL after use */
float *latticedata;
float latmat[4][4];
struct EditLatt *editlatt;
} Lattice;

View File

@@ -272,7 +272,9 @@ typedef struct ParticleSystem {
struct DerivedMesh *hair_in_dm, *hair_out_dm; /* input/output for cloth simulation */
struct Object *target_ob;
struct Object *lattice;
struct LatticeDeformData *lattice_deform_data; /* run-time only lattice deformation data */
struct Object *parent; /* particles from global space -> parent space */
struct ListBase targets; /* used for keyed and boid physics */

View File

@@ -869,7 +869,7 @@ static DerivedMesh *explodeMesh(ExplodeModifierData *emd,
/* getting back to object space */
invert_m4_m4(imat, ob->obmat);
psmd->psys->lattice = psys_get_lattice(&sim);
psmd->psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
/* duplicate & displace vertices */
ehi = BLI_edgehashIterator_new(vertpahash);
@@ -973,9 +973,9 @@ static DerivedMesh *explodeMesh(ExplodeModifierData *emd,
CDDM_tessfaces_to_faces(explode);
explode->dirty |= DM_DIRTY_NORMALS;
if (psmd->psys->lattice) {
end_latt_deform(psmd->psys->lattice);
psmd->psys->lattice = NULL;
if (psmd->psys->lattice_deform_data) {
end_latt_deform(psmd->psys->lattice_deform_data);
psmd->psys->lattice_deform_data = NULL;
}
return explode;

View File

@@ -261,7 +261,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
maxloop += totloop;
}
psys->lattice = psys_get_lattice(&sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
if (psys->flag & (PSYS_HAIR_DONE | PSYS_KEYED) || psys->pointcache->flag & PTCACHE_BAKED) {
float min_r[3], max_r[3];
@@ -378,9 +378,9 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
CDDM_calc_edges(result);
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice = NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
if (size)

View File

@@ -1741,7 +1741,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
}
if (path_nbr == 0)
psys->lattice = psys_get_lattice(&sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
/* 3. start creating renderable things */
for (a=0, pa=pars; a<totpart+totchild; a++, pa++, seed++) {
@@ -2094,9 +2094,9 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
psys->flag &= ~PSYS_DRAWING;
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice= NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
if (path_nbr && (ma->mode_l & MA_TANGENT_STR)==0)

View File

@@ -140,7 +140,7 @@ static void pointdensity_cache_psys(Render *re, PointDensity *pd, Object *ob, Pa
invert_m4_m4(ob->imat, ob->obmat);
total_particles = psys->totpart+psys->totchild;
psys->lattice=psys_get_lattice(&sim);
psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
pd->point_tree = BLI_bvhtree_new(total_particles, 0.0, 4, 6);
alloc_point_data(pd, total_particles, data_used);
@@ -215,9 +215,9 @@ static void pointdensity_cache_psys(Render *re, PointDensity *pd, Object *ob, Pa
BLI_bvhtree_balance(pd->point_tree);
dm->release(dm);
if (psys->lattice) {
end_latt_deform(psys->lattice);
psys->lattice = NULL;
if (psys->lattice_deform_data) {
end_latt_deform(psys->lattice_deform_data);
psys->lattice_deform_data = NULL;
}
psys_render_restore(ob, psys);