This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/depsgraph/intern/node/deg_node_component.cc
Sergey Sharybin 0dcee6a386 Fix T99733: Objects with driven visibility are evaluated when not needed
The issue was caused by the fact that objects with driven or animated
visibility were considered visible by the dependency graph evaluation.

This change makes it so the dependency graph evaluation is aware of
visibility which might be changing. This is achieved by evaluating the
path of the graph which affects objects visibility and adjusts to it
before evaluating the rest of the graph.

There is some time penalty to this, but there does not seem to be a
way to fully avoid this penalty.

With the production shot from the heist project the FPS drops by a
tenth of a frame (~9.4 vs ~9.3 fps) when adding a driver to an object
which keeps it visible. Note that this is a bit hard to measure since
the FPS fluctuates quite a bit throughout the playback. On the other
hand, having a driver on a visibility of a heavy object from character
and setting visibility to false gives big speedup.

Also worth noting that there is no penalty at all when there are no
animated visibilities in the scene.

Differential Revision: https://developer.blender.org/D15498
2022-07-21 09:49:16 +02:00

380 lines
11 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2013 Blender Foundation. All rights reserved. */
/** \file
* \ingroup depsgraph
*/
#include "intern/node/deg_node_component.h"
#include <cstdio>
#include <cstring> /* required for STREQ later on. */
#include "BLI_ghash.h"
#include "BLI_hash.hh"
#include "BLI_utildefines.h"
#include "DNA_object_types.h"
#include "BKE_action.h"
#include "intern/node/deg_node_factory.h"
#include "intern/node/deg_node_id.h"
#include "intern/node/deg_node_operation.h"
namespace blender::deg {
/* *********** */
/* Outer Nodes */
/* -------------------------------------------------------------------- */
/** \name Standard Component Methods
* \{ */
ComponentNode::OperationIDKey::OperationIDKey()
: opcode(OperationCode::OPERATION), name(""), name_tag(-1)
{
}
ComponentNode::OperationIDKey::OperationIDKey(OperationCode opcode)
: opcode(opcode), name(""), name_tag(-1)
{
}
ComponentNode::OperationIDKey::OperationIDKey(OperationCode opcode, const char *name, int name_tag)
: opcode(opcode), name(name), name_tag(name_tag)
{
}
string ComponentNode::OperationIDKey::identifier() const
{
const string codebuf = to_string(static_cast<int>(opcode));
return "OperationIDKey(" + codebuf + ", " + name + ")";
}
bool ComponentNode::OperationIDKey::operator==(const OperationIDKey &other) const
{
return (opcode == other.opcode) && (STREQ(name, other.name)) && (name_tag == other.name_tag);
}
uint64_t ComponentNode::OperationIDKey::hash() const
{
const int opcode_as_int = static_cast<int>(opcode);
return BLI_ghashutil_combine_hash(
name_tag,
BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(opcode_as_int),
BLI_ghashutil_strhash_p(name)));
}
ComponentNode::ComponentNode()
: entry_operation(nullptr),
exit_operation(nullptr),
possibly_affects_visible_id(false),
affects_visible_id(false)
{
operations_map = new Map<ComponentNode::OperationIDKey, OperationNode *>();
}
void ComponentNode::init(const ID * /*id*/, const char * /*subdata*/)
{
/* hook up eval context? */
/* XXX: maybe this needs a special API? */
}
/* Free 'component' node */
ComponentNode::~ComponentNode()
{
clear_operations();
delete operations_map;
}
string ComponentNode::identifier() const
{
const string idname = this->owner->name;
const string typebuf = "" + to_string(static_cast<int>(type)) + ")";
return typebuf + name + " : " + idname +
"( affects_visible_id: " + (affects_visible_id ? "true" : "false") + ")";
}
OperationNode *ComponentNode::find_operation(OperationIDKey key) const
{
OperationNode *node = nullptr;
if (operations_map != nullptr) {
node = operations_map->lookup_default(key, nullptr);
}
else {
for (OperationNode *op_node : operations) {
if (op_node->opcode == key.opcode && op_node->name_tag == key.name_tag &&
STREQ(op_node->name.c_str(), key.name)) {
node = op_node;
break;
}
}
}
return node;
}
OperationNode *ComponentNode::find_operation(OperationCode opcode,
const char *name,
int name_tag) const
{
OperationIDKey key(opcode, name, name_tag);
return find_operation(key);
}
OperationNode *ComponentNode::get_operation(OperationIDKey key) const
{
OperationNode *node = find_operation(key);
if (node == nullptr) {
fprintf(stderr,
"%s: find_operation(%s) failed\n",
this->identifier().c_str(),
key.identifier().c_str());
BLI_assert_msg(0, "Request for non-existing operation, should not happen");
return nullptr;
}
return node;
}
OperationNode *ComponentNode::get_operation(OperationCode opcode,
const char *name,
int name_tag) const
{
OperationIDKey key(opcode, name, name_tag);
return get_operation(key);
}
bool ComponentNode::has_operation(OperationIDKey key) const
{
return find_operation(key) != nullptr;
}
bool ComponentNode::has_operation(OperationCode opcode, const char *name, int name_tag) const
{
OperationIDKey key(opcode, name, name_tag);
return has_operation(key);
}
OperationNode *ComponentNode::add_operation(const DepsEvalOperationCb &op,
OperationCode opcode,
const char *name,
int name_tag)
{
OperationNode *op_node = find_operation(opcode, name, name_tag);
if (!op_node) {
DepsNodeFactory *factory = type_get_factory(NodeType::OPERATION);
op_node = (OperationNode *)factory->create_node(this->owner->id_orig, "", name);
/* register opnode in this component's operation set */
OperationIDKey key(opcode, name, name_tag);
operations_map->add(key, op_node);
/* Set back-link. */
op_node->owner = this;
}
else {
fprintf(stderr,
"add_operation: Operation already exists - %s has %s at %p\n",
this->identifier().c_str(),
op_node->identifier().c_str(),
op_node);
BLI_assert_msg(0, "Should not happen!");
}
/* attach extra data */
op_node->evaluate = op;
op_node->opcode = opcode;
op_node->name = name;
op_node->name_tag = name_tag;
return op_node;
}
void ComponentNode::set_entry_operation(OperationNode *op_node)
{
BLI_assert(entry_operation == nullptr);
entry_operation = op_node;
}
void ComponentNode::set_exit_operation(OperationNode *op_node)
{
BLI_assert(exit_operation == nullptr);
exit_operation = op_node;
}
void ComponentNode::clear_operations()
{
if (operations_map != nullptr) {
for (OperationNode *op_node : operations_map->values()) {
delete op_node;
}
operations_map->clear();
}
for (OperationNode *op_node : operations) {
delete op_node;
}
operations.clear();
}
void ComponentNode::tag_update(Depsgraph *graph, eUpdateSource source)
{
OperationNode *entry_op = get_entry_operation();
if (entry_op != nullptr && entry_op->flag & DEPSOP_FLAG_NEEDS_UPDATE) {
return;
}
for (OperationNode *op_node : operations) {
op_node->tag_update(graph, source);
}
/* It is possible that tag happens before finalization. */
if (operations_map != nullptr) {
for (OperationNode *op_node : operations_map->values()) {
op_node->tag_update(graph, source);
}
}
}
OperationNode *ComponentNode::get_entry_operation()
{
if (entry_operation) {
return entry_operation;
}
if (operations_map != nullptr && operations_map->size() == 1) {
OperationNode *op_node = nullptr;
/* TODO(sergey): This is somewhat slow. */
for (OperationNode *tmp : operations_map->values()) {
op_node = tmp;
}
/* Cache for the subsequent usage. */
entry_operation = op_node;
return op_node;
}
if (operations.size() == 1) {
return operations[0];
}
return nullptr;
}
OperationNode *ComponentNode::get_exit_operation()
{
if (exit_operation) {
return exit_operation;
}
if (operations_map != nullptr && operations_map->size() == 1) {
OperationNode *op_node = nullptr;
/* TODO(sergey): This is somewhat slow. */
for (OperationNode *tmp : operations_map->values()) {
op_node = tmp;
}
/* Cache for the subsequent usage. */
exit_operation = op_node;
return op_node;
}
if (operations.size() == 1) {
return operations[0];
}
return nullptr;
}
void ComponentNode::finalize_build(Depsgraph * /*graph*/)
{
operations.reserve(operations_map->size());
for (OperationNode *op_node : operations_map->values()) {
operations.append(op_node);
}
delete operations_map;
operations_map = nullptr;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Bone Component
* \{ */
void BoneComponentNode::init(const ID *id, const char *subdata)
{
/* generic component-node... */
ComponentNode::init(id, subdata);
/* name of component comes is bone name */
/* TODO(sergey): This sets name to an empty string because subdata is
* empty. Is it a bug? */
// this->name = subdata;
/* bone-specific node data */
Object *object = (Object *)id;
this->pchan = BKE_pose_channel_find_name(object->pose, subdata);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Register All Components
* \{ */
DEG_COMPONENT_NODE_DEFINE(Animation, ANIMATION, ID_RECALC_ANIMATION);
/* TODO(sergey): Is this a correct tag? */
DEG_COMPONENT_NODE_DEFINE(BatchCache, BATCH_CACHE, ID_RECALC_SHADING);
DEG_COMPONENT_NODE_DEFINE(Bone, BONE, ID_RECALC_GEOMETRY);
DEG_COMPONENT_NODE_DEFINE(Cache, CACHE, 0);
DEG_COMPONENT_NODE_DEFINE(CopyOnWrite, COPY_ON_WRITE, ID_RECALC_COPY_ON_WRITE);
DEG_COMPONENT_NODE_DEFINE(ImageAnimation, IMAGE_ANIMATION, 0);
DEG_COMPONENT_NODE_DEFINE(Geometry, GEOMETRY, ID_RECALC_GEOMETRY);
DEG_COMPONENT_NODE_DEFINE(LayerCollections, LAYER_COLLECTIONS, 0);
DEG_COMPONENT_NODE_DEFINE(Parameters, PARAMETERS, 0);
DEG_COMPONENT_NODE_DEFINE(Particles, PARTICLE_SYSTEM, ID_RECALC_GEOMETRY);
DEG_COMPONENT_NODE_DEFINE(ParticleSettings, PARTICLE_SETTINGS, 0);
DEG_COMPONENT_NODE_DEFINE(PointCache, POINT_CACHE, 0);
DEG_COMPONENT_NODE_DEFINE(Pose, EVAL_POSE, ID_RECALC_GEOMETRY);
DEG_COMPONENT_NODE_DEFINE(Sequencer, SEQUENCER, 0);
DEG_COMPONENT_NODE_DEFINE(Shading, SHADING, ID_RECALC_SHADING);
DEG_COMPONENT_NODE_DEFINE(Transform, TRANSFORM, ID_RECALC_TRANSFORM);
DEG_COMPONENT_NODE_DEFINE(ObjectFromLayer, OBJECT_FROM_LAYER, 0);
DEG_COMPONENT_NODE_DEFINE(Dupli, DUPLI, 0);
DEG_COMPONENT_NODE_DEFINE(Synchronization, SYNCHRONIZATION, 0);
DEG_COMPONENT_NODE_DEFINE(Audio, AUDIO, 0);
DEG_COMPONENT_NODE_DEFINE(Armature, ARMATURE, 0);
DEG_COMPONENT_NODE_DEFINE(GenericDatablock, GENERIC_DATABLOCK, 0);
DEG_COMPONENT_NODE_DEFINE(Visibility, VISIBILITY, 0);
DEG_COMPONENT_NODE_DEFINE(Simulation, SIMULATION, 0);
DEG_COMPONENT_NODE_DEFINE(NTreeOutput, NTREE_OUTPUT, ID_RECALC_NTREE_OUTPUT);
/** \} */
/* -------------------------------------------------------------------- */
/** \name Node Types Register
* \{ */
void deg_register_component_depsnodes()
{
register_node_typeinfo(&DNTI_ANIMATION);
register_node_typeinfo(&DNTI_BONE);
register_node_typeinfo(&DNTI_CACHE);
register_node_typeinfo(&DNTI_BATCH_CACHE);
register_node_typeinfo(&DNTI_COPY_ON_WRITE);
register_node_typeinfo(&DNTI_GEOMETRY);
register_node_typeinfo(&DNTI_LAYER_COLLECTIONS);
register_node_typeinfo(&DNTI_PARAMETERS);
register_node_typeinfo(&DNTI_PARTICLE_SYSTEM);
register_node_typeinfo(&DNTI_PARTICLE_SETTINGS);
register_node_typeinfo(&DNTI_POINT_CACHE);
register_node_typeinfo(&DNTI_IMAGE_ANIMATION);
register_node_typeinfo(&DNTI_EVAL_POSE);
register_node_typeinfo(&DNTI_SEQUENCER);
register_node_typeinfo(&DNTI_SHADING);
register_node_typeinfo(&DNTI_TRANSFORM);
register_node_typeinfo(&DNTI_OBJECT_FROM_LAYER);
register_node_typeinfo(&DNTI_DUPLI);
register_node_typeinfo(&DNTI_SYNCHRONIZATION);
register_node_typeinfo(&DNTI_AUDIO);
register_node_typeinfo(&DNTI_ARMATURE);
register_node_typeinfo(&DNTI_GENERIC_DATABLOCK);
register_node_typeinfo(&DNTI_VISIBILITY);
register_node_typeinfo(&DNTI_SIMULATION);
register_node_typeinfo(&DNTI_NTREE_OUTPUT);
}
/** \} */
} // namespace blender::deg