Cycles: Free caches used by the synchronized objects

Issue this commit is addressed to is that particle system and particle modifier
will contain caches once derived mesh was requested and this cached data will
never be freed.

This could easily lead to unwanted memory peaks during synchronization stage
of rendering.

The idea is to have RNA function in object which would free caches which can't
be freed otherwise. This function is not intended to deal with derived final
since it might be used by other objects (for example by object with boolean
modifier).

This cache freeing is only happening in the background rendering and locked
interface rendering.

From quick tests with victor file this change reduces peak memory usage by
command line rendering by around 6% (1780MB vs. 1883MB). For rendering from
the interface it's about 12% (1763MB vs. 1998MB).

Reviewers: campbellbarton, lukastoenne

Differential Revision: https://developer.blender.org/D1121
This commit is contained in:
Sergey Sharybin
2015-02-17 17:27:15 +05:00
parent 79393cb7a2
commit 0e18a56432
4 changed files with 56 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
#include "scene.h"
#include "blender_sync.h"
#include "blender_session.h"
#include "blender_util.h"
#include "subd_mesh.h"
@@ -588,6 +589,14 @@ static void create_subd_mesh(Scene *scene, Mesh *mesh, BL::Mesh b_mesh, PointerR
Mesh *BlenderSync::sync_mesh(BL::Object b_ob, bool object_updated, bool hide_tris)
{
/* When viewport display is not needed during render we can force some
* caches to be releases from blender side in order to reduce peak memory
* footprint during synchronization process.
*/
const bool is_interface_locked = b_engine.render() &&
b_engine.render().use_lock_interface();
const bool can_free_caches = BlenderSession::headless || is_interface_locked;
/* test if we can instance or if the object is modified */
BL::ID b_ob_data = b_ob.data();
BL::ID key = (BKE_object_is_modified(b_ob))? b_ob: b_ob_data;
@@ -681,6 +690,10 @@ Mesh *BlenderSync::sync_mesh(BL::Object b_ob, bool object_updated, bool hide_tri
if(render_layer.use_hair)
sync_curves(mesh, b_mesh, b_ob, false);
if(can_free_caches) {
b_ob.cache_release();
}
/* free derived mesh */
b_data.meshes.remove(b_mesh);
}

View File

@@ -69,6 +69,7 @@ void BKE_object_update_base_layer(struct Scene *scene, struct Object *ob);
void BKE_object_free(struct Object *ob);
void BKE_object_free_ex(struct Object *ob, bool do_id_user);
void BKE_object_free_derived_caches(struct Object *ob);
void BKE_object_free_caches(struct Object *object);
void BKE_object_modifier_hook_reset(struct Object *ob, struct HookModifierData *hmd);

View File

@@ -329,6 +329,45 @@ void BKE_object_free_derived_caches(Object *ob)
BKE_object_free_curve_cache(ob);
}
void BKE_object_free_caches(Object *object)
{
ModifierData *md;
short update_flag = 0;
/* Free particle system caches holding paths. */
if (object->particlesystem.first) {
ParticleSystem *psys;
for (psys = object->particlesystem.first;
psys != NULL;
psys = psys->next)
{
psys_free_path_cache(psys, psys->edit);
update_flag |= PSYS_RECALC;
}
}
/* Free memory used by cached derived meshes in the particle system modifiers. */
for (md = object->modifiers.first; md != NULL; md = md->next) {
if (md->type == eModifierType_ParticleSystem) {
ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
if (psmd->dm != NULL) {
psmd->dm->needsFree = 1;
psmd->dm->release(psmd->dm);
psmd->dm = NULL;
update_flag |= OB_RECALC_DATA;
}
}
}
/* Tag object for update, so once memory critical operation is over and
* scene update routines are back to it's business the object will be
* guaranteed to be in a known state.
*/
if (update_flag != 0) {
DAG_id_tag_update(&object->id, update_flag);
}
}
/* do not free object itself */
void BKE_object_free_ex(Object *ob, bool do_id_user)
{

View File

@@ -655,6 +655,9 @@ void RNA_api_object(StructRNA *srna)
RNA_def_function_ui_description(func, "Load the objects edit-mode data intp the object data");
parm = RNA_def_boolean(func, "result", 0, "", "Success");
RNA_def_function_return(func, parm);
func = RNA_def_function(srna, "cache_release", "BKE_object_free_caches");
RNA_def_function_ui_description(func, "Release memory used by caches associated with this object. Intended to be used by render engines only");
}