Physics: update softbody and dynamic paint to get colliders from depsgraph.

Because looping over the scene is unsafe and slow.
This commit is contained in:
Brecht Van Lommel 2018-06-25 14:21:15 +02:00
parent 2c9b32949b
commit c2110213ca
7 changed files with 53 additions and 106 deletions

View File

@ -96,10 +96,7 @@ bool BKE_collection_object_cyclic_check(struct Main *bmain, struct Object *objec
struct ListBase BKE_collection_object_cache_get(struct Collection *collection);
void BKE_collection_object_cache_free(struct Collection *collection);
struct Base *BKE_collection_or_layer_objects(const struct Depsgraph *depsgraph,
const struct Scene *scene,
const struct ViewLayer *view_layer,
struct Collection *collection);
struct Base *BKE_collection_or_layer_objects(const struct ViewLayer *view_layer, struct Collection *collection);
/* Editing. */

View File

@ -364,36 +364,13 @@ void BKE_collection_object_cache_free(Collection *collection)
collection_object_cache_free(collection);
}
Base *BKE_collection_or_layer_objects(const Depsgraph *depsgraph,
const Scene *scene,
const ViewLayer *view_layer,
Collection *collection)
Base *BKE_collection_or_layer_objects(const ViewLayer *view_layer, Collection *collection)
{
// TODO: this is used by physics to get objects from a collection, but the
// the physics systems are not all using the depsgraph correctly which means
// we try different things. Instead we should explicitly get evaluated or
// non-evaluated data and always have the depsgraph available when needed
if (collection) {
return BKE_collection_object_cache_get(collection).first;
}
else if (depsgraph) {
view_layer = DEG_get_evaluated_view_layer(depsgraph);
if (view_layer) {
return FIRSTBASE(view_layer);
}
else {
view_layer = DEG_get_input_view_layer(depsgraph);
return FIRSTBASE(view_layer);
}
}
else if (view_layer) {
return FIRSTBASE(view_layer);
}
else {
/* depsgraph is NULL during deg build */
return FIRSTBASE(BKE_view_layer_context_active_PLACEHOLDER(scene));
return FIRSTBASE(view_layer);
}
}

View File

@ -518,7 +518,7 @@ static void add_collision_object(ListBase *relations, Object *ob, int level, uns
ListBase *BKE_collision_relations_create(Depsgraph *depsgraph, Collection *collection, unsigned int modifier_type)
{
ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph);
Base *base = BKE_collection_or_layer_objects(NULL, NULL, view_layer, collection);
Base *base = BKE_collection_or_layer_objects(view_layer, collection);
const bool for_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
const int base_flag = (for_render) ? BASE_ENABLED_RENDER : BASE_ENABLED_VIEWPORT;
@ -548,6 +548,7 @@ Object **BKE_collision_objects_create(Depsgraph *depsgraph, Object *self, Collec
ListBase *relations = DEG_get_collision_relations(depsgraph, collection, modifier_type);
if (!relations) {
*numcollobj = 0;
return NULL;
}

View File

@ -56,6 +56,7 @@
#include "BKE_armature.h"
#include "BKE_bvhutils.h" /* bvh tree */
#include "BKE_collection.h"
#include "BKE_collision.h"
#include "BKE_colorband.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_constraint.h"
@ -490,28 +491,17 @@ static void scene_setSubframe(Scene *scene, float subframe)
scene->r.subframe = subframe;
}
static int surface_getBrushFlags(DynamicPaintSurface *surface, const Depsgraph *depsgraph)
static int surface_getBrushFlags(DynamicPaintSurface *surface, Depsgraph *depsgraph)
{
Base *base = BKE_collection_or_layer_objects(depsgraph, NULL, NULL, surface->brush_group);
Object *brushObj = NULL;
ModifierData *md = NULL;
unsigned int numobjects;
Object **objects = BKE_collision_objects_create(depsgraph, NULL, surface->brush_group, &numobjects, eModifierType_DynamicPaint);
int flags = 0;
while (base) {
brushObj = NULL;
for (int i = 0; i < numobjects; i++) {
Object *brushObj = objects[i];
/* select object */
brushObj = base->object;
/* next item */
base = base->next;
if (!brushObj) {
continue;
}
md = modifiers_findByType(brushObj, eModifierType_DynamicPaint);
ModifierData *md = modifiers_findByType(brushObj, eModifierType_DynamicPaint);
if (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render)) {
DynamicPaintModifierData *pmd2 = (DynamicPaintModifierData *)md;
@ -524,6 +514,8 @@ static int surface_getBrushFlags(DynamicPaintSurface *surface, const Depsgraph *
}
}
BKE_collision_objects_free(objects);
return flags;
}
@ -5758,7 +5750,7 @@ static void dynamic_paint_generate_bake_data_cb(
}
}
static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const Depsgraph *depsgraph, Object *ob)
static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, Depsgraph *depsgraph, Object *ob)
{
PaintSurfaceData *sData = surface->data;
PaintBakeData *bData = sData->bData;
@ -5911,29 +5903,18 @@ static int dynamicPaint_doStep(
* Loop through surface's target paint objects and do painting
*/
{
Object *brushObj = NULL;
ModifierData *md = NULL;
Base *base = BKE_collection_or_layer_objects(depsgraph, NULL, NULL, surface->brush_group);
unsigned int numobjects;
Object **objects = BKE_collision_objects_create(depsgraph, NULL, surface->brush_group, &numobjects, eModifierType_DynamicPaint);
/* backup current scene frame */
int scene_frame = scene->r.cfra;
float scene_subframe = scene->r.subframe;
while (base) {
brushObj = NULL;
/* select object */
brushObj = base->object;
/* next item */
base = base->next;
if (!brushObj) {
/* skip item */
continue;
}
for (int i = 0; i < numobjects; i++) {
Object *brushObj = objects[i];
/* check if target has an active dp modifier */
md = modifiers_findByType(brushObj, eModifierType_DynamicPaint);
ModifierData *md = modifiers_findByType(brushObj, eModifierType_DynamicPaint);
if (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render)) {
DynamicPaintModifierData *pmd2 = (DynamicPaintModifierData *)md;
/* make sure we're dealing with a brush */
@ -5995,6 +5976,8 @@ static int dynamicPaint_doStep(
}
}
}
BKE_collision_objects_free(objects);
}
/* surfaces operations that use adjacency data */

View File

@ -228,7 +228,7 @@ ListBase *BKE_effector_relations_create(
ViewLayer *view_layer,
Collection *collection)
{
Base *base = BKE_collection_or_layer_objects(NULL, NULL, view_layer, collection);
Base *base = BKE_collection_or_layer_objects(view_layer, collection);
const bool for_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
const int base_flag = (for_render) ? BASE_ENABLED_RENDER : BASE_ENABLED_VIEWPORT;

View File

@ -72,6 +72,7 @@ variables on the UI for now
#include "BLI_threads.h"
#include "BKE_collection.h"
#include "BKE_collision.h"
#include "BKE_curve.h"
#include "BKE_effect.h"
#include "BKE_global.h"
@ -521,20 +522,18 @@ static void ccd_build_deflector_hash(Depsgraph *depsgraph, Collection *collectio
{
if (!hash) return;
/* Explicit collision collection. */
Base *base = BKE_collection_or_layer_objects(depsgraph, NULL, NULL, collection);
unsigned int numobjects;
Object **objects = BKE_collision_objects_create(depsgraph, vertexowner, collection, &numobjects, eModifierType_Collision);
for (; base; base = base->next) {
/* Only proceed for mesh object in same layer. */
if (base->object->type == OB_MESH) {
Object *ob = base->object;
if (ob == vertexowner) {
/* If vertexowner is given we don't want to check collision with owner object. */
continue;
}
for (int i = 0; i < numobjects; i++) {
Object *ob = objects[i];
if (ob->type == OB_MESH) {
ccd_build_deflector_hash_single(hash, ob);
}
}
BKE_collision_objects_free(objects);
}
static void ccd_update_deflector_hash_single(GHash *hash, Object *ob)
@ -554,23 +553,19 @@ static void ccd_update_deflector_hash(Depsgraph *depsgraph, Collection *collecti
{
if ((!hash) || (!vertexowner)) return;
/* Explicit collision collection. */
Base *base = BKE_collection_or_layer_objects(depsgraph, NULL, NULL, collection);
unsigned int numobjects;
Object **objects = BKE_collision_objects_create(depsgraph, vertexowner, collection, &numobjects, eModifierType_Collision);
for (; base; base = base->next) {
/* Only proceed for mesh object in same layer. */
if (base->object->type == OB_MESH) {
Object *ob = base->object;
if (ob == vertexowner) {
/* If vertexowner is given we don't want to check collision with owner object. */
continue;
}
for (int i = 0; i < numobjects; i++) {
Object *ob = objects[i];
if (ob->type == OB_MESH) {
ccd_update_deflector_hash_single(hash, ob);
}
}
}
BKE_collision_objects_free(objects);
}
/*--- collider caching and dicing ---*/
@ -959,21 +954,13 @@ static void free_softbody_intern(SoftBody *sb)
/**
* \note collection overrides scene when not NULL.
*/
static bool are_there_deflectors(Base *first_base)
{
for (Base *base = first_base; base; base = base->next) {
if (base->object->pd) {
if (base->object->pd->deflect)
return 1;
}
}
return 0;
}
static int query_external_colliders(Depsgraph *depsgraph, Collection *collection)
{
return(are_there_deflectors(BKE_collection_or_layer_objects(depsgraph, NULL, NULL, collection)));
unsigned int numobjects;
Object **objects = BKE_collision_objects_create(depsgraph, NULL, collection, &numobjects, eModifierType_Collision);
BKE_collision_objects_free(objects);
return (numobjects != 0);
}
/* --- dependency information functions*/
@ -3491,9 +3478,7 @@ static void softbody_step(struct Depsgraph *depsgraph, Scene *scene, Object *ob,
ccd_update_deflector_hash(depsgraph, sb->collision_group, ob, sb->scratch->colliderhash);
if (sb->scratch->needstobuildcollider) {
if (query_external_colliders(depsgraph, sb->collision_group)) {
ccd_build_deflector_hash(depsgraph, sb->collision_group, ob, sb->scratch->colliderhash);
}
ccd_build_deflector_hash(depsgraph, sb->collision_group, ob, sb->scratch->colliderhash);
sb->scratch->needstobuildcollider=0;
}

View File

@ -40,10 +40,12 @@ extern "C" {
} /* extern "C" */
#include "DNA_group_types.h"
#include "DNA_object_types.h"
#include "DNA_object_force_types.h"
#include "DEG_depsgraph_build.h"
#include "DEG_depsgraph_physics.h"
#include "DEG_depsgraph_query.h"
#include "depsgraph.h"
#include "depsgraph_intern.h"
@ -73,7 +75,8 @@ ListBase *DEG_get_effector_relations(const Depsgraph *graph,
return NULL;
}
return (ListBase *)BLI_ghash_lookup(deg_graph->physics_relations[DEG_PHYSICS_EFFECTOR], collection);
ID *collection_orig = DEG_get_original_id(&collection->id);
return (ListBase *)BLI_ghash_lookup(deg_graph->physics_relations[DEG_PHYSICS_EFFECTOR], collection_orig);
}
ListBase *DEG_get_collision_relations(const Depsgraph *graph,
@ -86,7 +89,8 @@ ListBase *DEG_get_collision_relations(const Depsgraph *graph,
return NULL;
}
return (ListBase *)BLI_ghash_lookup(deg_graph->physics_relations[type], collection);
ID *collection_orig = DEG_get_original_id(&collection->id);
return (ListBase *)BLI_ghash_lookup(deg_graph->physics_relations[type], collection_orig);
}
/********************** Depsgraph Building API ************************/
@ -174,7 +178,7 @@ ListBase *deg_build_effector_relations(Depsgraph *graph,
if (relations == NULL) {
::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph*>(graph);
relations = BKE_effector_relations_create(depsgraph, graph->view_layer, collection);
BLI_ghash_insert(hash, collection, relations);
BLI_ghash_insert(hash, &collection->id, relations);
}
return relations;
@ -195,7 +199,7 @@ ListBase *deg_build_collision_relations(Depsgraph *graph,
if (relations == NULL) {
::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph*>(graph);
relations = BKE_collision_relations_create(depsgraph, collection, modifier_type);
BLI_ghash_insert(hash, collection, relations);
BLI_ghash_insert(hash, &collection->id, relations);
}
return relations;