Modifiers: ported Cloth DerivedMesh → Mesh

The modifier is still quite slow; this could be due to caches being written
to a CoW datablock instead of the original one. More investigation is
needed.
This commit is contained in:
Sybren A. Stüvel 2018-05-18 17:32:22 +02:00
parent e89fa4c85b
commit f5d911f8b0
5 changed files with 80 additions and 72 deletions

View File

@ -37,8 +37,8 @@
struct Object;
struct Scene;
struct Mesh;
struct MFace;
struct DerivedMesh;
struct ClothModifierData;
struct CollisionModifierData;
struct Depsgraph;
@ -227,7 +227,7 @@ void cloth_free_contacts(ColliderContacts *collider_contacts, int totcolliders);
void cloth_free_modifier_extern (struct ClothModifierData *clmd );
void cloth_free_modifier (struct ClothModifierData *clmd );
void cloth_init (struct ClothModifierData *clmd );
void clothModifier_do(struct ClothModifierData *clmd, struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3]);
void clothModifier_do(struct ClothModifierData *clmd, struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct Mesh *dm, float (*vertexCos)[3]);
int cloth_uses_vgroup(struct ClothModifierData *clmd);

View File

@ -33,6 +33,7 @@
#include "DNA_cloth_types.h"
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "BLI_utildefines.h"
@ -43,10 +44,11 @@
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_bvhutils.h"
#include "BKE_cloth.h"
#include "BKE_effect.h"
#include "BKE_global.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_pointcache.h"
@ -58,13 +60,13 @@
/* Prototypes for internal functions.
*/
static void cloth_to_object (Object *ob, ClothModifierData *clmd, float (*vertexCos)[3]);
static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm );
static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr, int first);
static void cloth_from_mesh ( ClothModifierData *clmd, Mesh *mesh );
static int cloth_from_object(Object *ob, ClothModifierData *clmd, Mesh *mesh, float framenr, int first);
static void cloth_update_springs( ClothModifierData *clmd );
static void cloth_update_verts( Object *ob, ClothModifierData *clmd, DerivedMesh *dm );
static void cloth_update_spring_lengths( ClothModifierData *clmd, DerivedMesh *dm );
static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm );
static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm );
static void cloth_update_verts( Object *ob, ClothModifierData *clmd, Mesh *mesh );
static void cloth_update_spring_lengths( ClothModifierData *clmd, Mesh *mesh );
static int cloth_build_springs ( ClothModifierData *clmd, Mesh *mesh );
static void cloth_apply_vgroup ( ClothModifierData *clmd, Mesh *mesh );
/******************************************************************************
*
@ -320,7 +322,7 @@ void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
BKE_ptcache_id_clear(&pid, PTCACHE_CLEAR_AFTER, framenr);
}
static int do_init_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *result, int framenr)
static int do_init_cloth(Object *ob, ClothModifierData *clmd, Mesh *result, int framenr)
{
PointCache *cache;
@ -348,7 +350,7 @@ static int do_init_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *resul
return 1;
}
static int do_step_cloth(struct Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, DerivedMesh *result, int framenr)
static int do_step_cloth(struct Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, Mesh *result, int framenr)
{
ClothVertex *verts = NULL;
Cloth *cloth;
@ -360,7 +362,7 @@ static int do_step_cloth(struct Depsgraph *depsgraph, Object *ob, ClothModifierD
/* simulate 1 frame forward */
cloth = clmd->clothObject;
verts = cloth->verts;
mvert = result->getVertArray(result);
mvert = result->mvert;
/* force any pinned verts to their constrained location. */
for (i = 0; i < clmd->clothObject->mvert_num; i++, verts++) {
@ -403,7 +405,7 @@ static int do_step_cloth(struct Depsgraph *depsgraph, Object *ob, ClothModifierD
/************************************************
* clothModifier_do - main simulation function
************************************************/
void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scene *scene, Object *ob, DerivedMesh *dm, float (*vertexCos)[3])
void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scene *scene, Object *ob, Mesh *mesh, float (*vertexCos)[3])
{
PointCache *cache;
PTCacheID pid;
@ -419,7 +421,7 @@ void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scen
BKE_ptcache_id_time(&pid, scene, framenr, &startframe, &endframe, &timescale);
clmd->sim_parms->timescale= timescale * clmd->sim_parms->time_scale;
if (clmd->sim_parms->reset || (clmd->clothObject && dm->getNumVerts(dm) != clmd->clothObject->mvert_num)) {
if (clmd->sim_parms->reset || (clmd->clothObject && mesh->totvert != clmd->clothObject->mvert_num)) {
clmd->sim_parms->reset = 0;
cache->flag |= PTCACHE_OUTDATED;
BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
@ -441,12 +443,12 @@ void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scen
}
/* initialize simulation data if it didn't exist already */
if (!do_init_cloth(ob, clmd, dm, framenr))
if (!do_init_cloth(ob, clmd, mesh, framenr))
return;
if (framenr == startframe) {
BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
do_init_cloth(ob, clmd, dm, framenr);
do_init_cloth(ob, clmd, mesh, framenr);
BKE_ptcache_validate(cache, framenr);
cache->flag &= ~PTCACHE_REDO_NEEDED;
clmd->clothObject->last_frame= framenr;
@ -491,7 +493,7 @@ void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scen
/* do simulation */
BKE_ptcache_validate(cache, framenr);
if (!do_step_cloth(depsgraph, ob, clmd, dm, framenr)) {
if (!do_step_cloth(depsgraph, ob, clmd, mesh, framenr)) {
BKE_ptcache_invalidate(cache);
}
else
@ -674,7 +676,7 @@ int cloth_uses_vgroup(ClothModifierData *clmd)
*
**/
/* can be optimized to do all groups in one loop */
static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
static void cloth_apply_vgroup ( ClothModifierData *clmd, Mesh *mesh )
{
int i = 0;
int j = 0;
@ -684,11 +686,11 @@ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
/* float goalfac = 0; */ /* UNUSED */
ClothVertex *verts = NULL;
if (!clmd || !dm) return;
if (!clmd || !mesh) return;
clothObj = clmd->clothObject;
mvert_num = dm->getNumVerts(dm);
mvert_num = mesh->totvert;
verts = clothObj->verts;
@ -708,7 +710,7 @@ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
verts->flags &= ~CLOTH_VERT_FLAG_PINNED;
verts->flags &= ~CLOTH_VERT_FLAG_NOSELFCOLL;
dvert = dm->getVertData ( dm, i, CD_MDEFORMVERT );
dvert = CustomData_get(&mesh->vdata, i, CD_MDEFORMVERT);
if ( dvert ) {
for ( j = 0; j < dvert->totweight; j++ ) {
if (( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass-1)) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )) {
@ -774,7 +776,7 @@ static float cloth_shrink_factor(ClothModifierData *clmd, ClothVertex *verts, in
return 1.0f;
}
static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float UNUSED(framenr), int first)
static int cloth_from_object(Object *ob, ClothModifierData *clmd, Mesh *mesh, float UNUSED(framenr), int first)
{
int i = 0;
MVert *mvert = NULL;
@ -804,25 +806,25 @@ static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *d
return 0;
}
// mesh input objects need DerivedMesh
if ( !dm )
// mesh input objects need Mesh
if ( !mesh )
return 0;
cloth_from_mesh ( clmd, dm );
cloth_from_mesh ( clmd, mesh );
// create springs
clmd->clothObject->springs = NULL;
clmd->clothObject->numsprings = -1;
if ( clmd->sim_parms->shapekey_rest && !(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH ) )
shapekey_rest = dm->getVertDataArray ( dm, CD_CLOTH_ORCO );
shapekey_rest = CustomData_get_layer(&mesh->vdata, CD_CLOTH_ORCO);
mvert = dm->getVertArray (dm);
mvert = mesh->mvert;
verts = clmd->clothObject->verts;
// set initial values
for ( i = 0; i < dm->getNumVerts(dm); i++, verts++ ) {
for ( i = 0; i < mesh->totvert; i++, verts++ ) {
if (first) {
copy_v3_v3(verts->x, mvert[i].co);
@ -860,9 +862,9 @@ static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *d
// apply / set vertex groups
// has to be happen before springs are build!
cloth_apply_vgroup (clmd, dm);
cloth_apply_vgroup (clmd, mesh);
if ( !cloth_build_springs ( clmd, dm ) ) {
if ( !cloth_build_springs ( clmd, mesh ) ) {
cloth_free_modifier ( clmd );
modifier_setError(&(clmd->modifier), "Cannot build springs");
printf("cloth_free_modifier cloth_build_springs\n");
@ -877,7 +879,7 @@ static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *d
clmd->clothObject->bvhtree = bvhtree_build_from_cloth ( clmd, MAX2(clmd->coll_parms->epsilon, clmd->coll_parms->distance_repel) );
for (i = 0; i < dm->getNumVerts(dm); i++) {
for (i = 0; i < mesh->totvert; i++) {
maxdist = MAX2(maxdist, clmd->coll_parms->selfepsilon* ( cloth->verts[i].avg_spring_len*2.0f));
}
@ -886,12 +888,12 @@ static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *d
return 1;
}
static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm )
static void cloth_from_mesh ( ClothModifierData *clmd, Mesh *mesh )
{
const MLoop *mloop = dm->getLoopArray(dm);
const MLoopTri *looptri = dm->getLoopTriArray(dm);
const unsigned int mvert_num = dm->getNumVerts(dm);
const unsigned int looptri_num = dm->getNumLoopTri(dm);
const MLoop *mloop = mesh->mloop;
const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh);
const unsigned int mvert_num = mesh->totvert;
const unsigned int looptri_num = mesh->runtime.looptris.len;
/* Allocate our vertices. */
clmd->clothObject->mvert_num = mvert_num;
@ -912,7 +914,7 @@ static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm )
printf("cloth_free_modifier clmd->clothObject->looptri\n");
return;
}
DM_verttri_from_looptri(clmd->clothObject->tri, mloop, looptri, looptri_num);
BKE_mesh_runtime_verttri_from_looptri(clmd->clothObject->tri, mloop, looptri, looptri_num);
/* Free the springs since they can't be correct if the vertices
* changed.
@ -1154,27 +1156,27 @@ static void cloth_update_springs( ClothModifierData *clmd )
}
/* Update rest verts, for dynamically deformable cloth */
static void cloth_update_verts( Object *ob, ClothModifierData *clmd, DerivedMesh *dm )
static void cloth_update_verts( Object *ob, ClothModifierData *clmd, Mesh *mesh )
{
unsigned int i = 0;
MVert *mvert = dm->getVertArray (dm);
MVert *mvert = mesh->mvert;
ClothVertex *verts = clmd->clothObject->verts;
/* vertex count is already ensured to match */
for ( i = 0; i < dm->getNumVerts(dm); i++, verts++ ) {
for ( i = 0; i < mesh->totvert; i++, verts++ ) {
copy_v3_v3(verts->xrest, mvert[i].co);
mul_m4_v3(ob->obmat, verts->xrest);
}
}
/* Update spring rest lenght, for dynamically deformable cloth */
static void cloth_update_spring_lengths( ClothModifierData *clmd, DerivedMesh *dm )
static void cloth_update_spring_lengths( ClothModifierData *clmd, Mesh *mesh )
{
Cloth *cloth = clmd->clothObject;
LinkNode *search = cloth->springs;
unsigned int struct_springs = 0;
unsigned int i = 0;
unsigned int mvert_num = (unsigned int)dm->getNumVerts(dm);
unsigned int mvert_num = (unsigned int)mesh->totvert;
float shrink_factor;
clmd->sim_parms->avg_spring_len = 0.0f;
@ -1249,19 +1251,19 @@ void cloth_parallel_transport_hair_frame(float mat[3][3], const float dir_old[3]
mul_m3_m3m3(mat, rot, mat);
}
static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
static int cloth_build_springs ( ClothModifierData *clmd, Mesh *mesh )
{
Cloth *cloth = clmd->clothObject;
ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL;
unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0, struct_springs_real = 0;
unsigned int i = 0;
unsigned int mvert_num = (unsigned int)dm->getNumVerts(dm);
unsigned int numedges = (unsigned int)dm->getNumEdges (dm);
unsigned int numpolys = (unsigned int)dm->getNumPolys(dm);
unsigned int mvert_num = (unsigned int)mesh->totvert;
unsigned int numedges = (unsigned int)mesh->totedge;
unsigned int numpolys = (unsigned int)mesh->totpoly;
float shrink_factor;
const MEdge *medge = dm->getEdgeArray(dm);
const MPoly *mpoly = dm->getPolyArray(dm);
const MLoop *mloop = dm->getLoopArray(dm);
const MEdge *medge = mesh->medge;
const MPoly *mpoly = mesh->mpoly;
const MLoop *mloop = mesh->mloop;
int index2 = 0; // our second vertex index
LinkNodePair *edgelist;
EdgeSet *edgeset = NULL;

View File

@ -3214,13 +3214,7 @@ static void do_hair_dynamics(ParticleSimulationData *sim)
LIB_ID_COPY_NO_PREVIEW,
false);
deformedVerts = BKE_mesh_vertexCos_get(psys->hair_out_mesh, NULL);
/* TODO(Sybren): after porting Cloth modifier, remove this conversion */
DerivedMesh *hair_in_dm = CDDM_from_mesh(psys->hair_in_mesh);
clothModifier_do(psys->clmd, sim->depsgraph, sim->scene, sim->ob, hair_in_dm, deformedVerts);
hair_in_dm->needsFree = 1;
hair_in_dm->release(hair_in_dm);
clothModifier_do(psys->clmd, sim->depsgraph, sim->scene, sim->ob, psys->hair_in_mesh, deformedVerts);
BKE_mesh_apply_vert_coords(psys->hair_out_mesh, deformedVerts);
MEM_freeN(deformedVerts);

View File

@ -607,7 +607,7 @@ typedef struct SoftbodyModifierData {
typedef struct ClothModifierData {
ModifierData modifier;
struct Scene *scene; /* the context, time etc is here */
struct Scene *scene; /* the context is here */
struct Cloth *clothObject; /* The internal data structure for cloth. */
struct ClothSimSettings *sim_parms; /* definition is in DNA_cloth_types.h */
struct ClothCollSettings *coll_parms; /* definition is in DNA_cloth_types.h */

View File

@ -36,6 +36,7 @@
#include "DNA_cloth_types.h"
#include "DNA_key_types.h"
#include "DNA_mesh_types.h"
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
@ -44,11 +45,12 @@
#include "BLI_utildefines.h"
#include "BKE_cloth.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_effect.h"
#include "BKE_global.h"
#include "BKE_key.h"
#include "BKE_library.h"
#include "BKE_library_query.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_pointcache.h"
@ -71,10 +73,10 @@ static void initData(ModifierData *md)
static void deformVerts(
ModifierData *md, const ModifierEvalContext *ctx,
DerivedMesh *derivedData, float (*vertexCos)[3],
Mesh *mesh, float (*vertexCos)[3],
int numVerts)
{
DerivedMesh *dm;
Mesh *mesh_src;
ClothModifierData *clmd = (ClothModifierData *) md;
/* check for alloc failing */
@ -85,9 +87,20 @@ static void deformVerts(
return;
}
dm = get_dm(ctx->object, NULL, derivedData, NULL, false, false);
if (dm == derivedData)
dm = CDDM_copy(dm);
if (mesh == NULL) {
mesh_src = get_mesh(ctx->object, NULL, NULL, NULL, false, false);
}
else {
/* Not possible to use get_mesh() in this case as we'll modify its vertices
* and get_mesh() would return 'mesh' directly. */
BKE_id_copy_ex(
NULL, (ID *)mesh, (ID **)&mesh_src,
LIB_ID_CREATE_NO_MAIN |
LIB_ID_CREATE_NO_USER_REFCOUNT |
LIB_ID_CREATE_NO_DEG_TAG |
LIB_ID_COPY_NO_PREVIEW,
false);
}
/* TODO(sergey): For now it actually duplicates logic from DerivedMesh.c
* and needs some more generic solution. But starting experimenting with
@ -95,25 +108,24 @@ static void deformVerts(
*
* Also hopefully new cloth system will arrive soon..
*/
if (derivedData == NULL && clmd->sim_parms->shapekey_rest) {
if (mesh == NULL && clmd->sim_parms->shapekey_rest) {
KeyBlock *kb = BKE_keyblock_from_key(BKE_key_from_object(ctx->object),
clmd->sim_parms->shapekey_rest);
if (kb && kb->data != NULL) {
float (*layerorco)[3];
if (!(layerorco = DM_get_vert_data_layer(dm, CD_CLOTH_ORCO))) {
DM_add_vert_layer(dm, CD_CLOTH_ORCO, CD_CALLOC, NULL);
layerorco = DM_get_vert_data_layer(dm, CD_CLOTH_ORCO);
if (!(layerorco = CustomData_get_layer(&mesh_src->vdata, CD_CLOTH_ORCO))) {
layerorco = CustomData_add_layer(&mesh_src->vdata, CD_CLOTH_ORCO, CD_CALLOC, NULL, mesh_src->totvert);
}
memcpy(layerorco, kb->data, sizeof(float) * 3 * numVerts);
}
}
CDDM_apply_vert_coords(dm, vertexCos);
BKE_mesh_apply_vert_coords(mesh_src, vertexCos);
clothModifier_do(clmd, ctx->depsgraph, md->scene, ctx->object, dm, vertexCos);
clothModifier_do(clmd, ctx->depsgraph, md->scene, ctx->object, mesh_src, vertexCos);
dm->release(dm);
BKE_id_free(NULL, mesh_src);
}
static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
@ -229,14 +241,14 @@ ModifierTypeInfo modifierType_Cloth = {
/* copyData */ copyData,
/* deformVerts_DM */ deformVerts,
/* deformVerts_DM */ NULL,
/* deformMatrices_DM */ NULL,
/* deformVertsEM_DM */ NULL,
/* deformMatricesEM_DM*/NULL,
/* applyModifier_DM */ NULL,
/* applyModifierEM_DM */NULL,
/* deformVerts */ NULL,
/* deformVerts */ deformVerts,
/* deformMatrices */ NULL,
/* deformVertsEM */ NULL,
/* deformMatricesEM */ NULL,