Fix Memory Leak introduced by Draw Manager Threading

The memory leak is noticeable when using custom bone shapes. When using custom
bone shapes objects could be extracted twice. Where the second extraction can
overwrite data created by the first extraction what causes the memory leak.

Options that have been checked:
1. Use two task graphs phases. One for normal extraction (DST.task_graph) and
   the other one will handle extractions that require blocking threads.
2. Keep a list of all objects that needs extraction and only start extraction
   when all objects have been populated.

The second would slow performance as the extraction only happens when all
objects have been populated. In the future we might want to go for the second
option when we have the capability to render multiple viewports with a single
populate. As this design isn't clear this patch will implement the first
option.

Reviewed By: Clément Foucault

Differential Revision: https://developer.blender.org/D7969
This commit is contained in:
Jeroen Bakker
2020-06-15 15:22:40 +02:00
committed by Jeroen Bakker
parent ec25084f5a
commit 3c717a631b
7 changed files with 23 additions and 9 deletions

View File

@@ -510,7 +510,7 @@ static void drw_shgroup_bone_envelope(ArmatureDrawContext *ctx,
/* Custom (geometry) */
extern void drw_batch_cache_validate(Object *custom);
extern void drw_batch_cache_generate_requested(Object *custom);
extern void drw_batch_cache_generate_requested_delayed(Object *custom);
BLI_INLINE DRWCallBuffer *custom_bone_instance_shgroup(ArmatureDrawContext *ctx,
DRWShadingGroup *grp,
@@ -567,7 +567,7 @@ static void drw_shgroup_bone_custom_solid(ArmatureDrawContext *ctx,
}
/* TODO(fclem) needs to be moved elsewhere. */
drw_batch_cache_generate_requested(custom);
drw_batch_cache_generate_requested_delayed(custom);
}
static void drw_shgroup_bone_custom_wire(ArmatureDrawContext *ctx,
@@ -591,7 +591,7 @@ static void drw_shgroup_bone_custom_wire(ArmatureDrawContext *ctx,
}
/* TODO(fclem) needs to be moved elsewhere. */
drw_batch_cache_generate_requested(custom);
drw_batch_cache_generate_requested_delayed(custom);
}
static void drw_shgroup_bone_custom_empty(ArmatureDrawContext *ctx,

View File

@@ -3552,6 +3552,11 @@ void drw_batch_cache_generate_requested(Object *ob)
}
}
void drw_batch_cache_generate_requested_delayed(Object *ob)
{
BLI_gset_add(DST.delayed_extraction, ob);
}
void DRW_batch_cache_free_old(Object *ob, int ctime)
{
struct Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);

View File

@@ -132,11 +132,17 @@ static void drw_task_graph_init(void)
{
BLI_assert(DST.task_graph == NULL);
DST.task_graph = BLI_task_graph_create();
DST.delayed_extraction = BLI_gset_ptr_new(__func__);
}
static void drw_task_graph_deinit(void)
{
BLI_task_graph_work_and_wait(DST.task_graph);
BLI_gset_free(DST.delayed_extraction, (void (*)(void *key))drw_batch_cache_generate_requested);
DST.delayed_extraction = NULL;
BLI_task_graph_work_and_wait(DST.task_graph);
BLI_task_graph_free(DST.task_graph);
DST.task_graph = NULL;
}
@@ -1486,6 +1492,7 @@ void DRW_draw_render_loop_ex(struct Depsgraph *depsgraph,
drw_duplidata_free();
drw_engines_cache_finish();
drw_task_graph_deinit();
DRW_render_instance_buffer_finish();
#ifdef USE_PROFILE
@@ -1494,7 +1501,6 @@ void DRW_draw_render_loop_ex(struct Depsgraph *depsgraph,
#endif
}
drw_task_graph_deinit();
DRW_stats_begin();
GPU_framebuffer_bind(DST.default_framebuffer);
@@ -2292,9 +2298,9 @@ static void drw_draw_depth_loop_imp(struct Depsgraph *depsgraph,
drw_duplidata_free();
drw_engines_cache_finish();
drw_task_graph_deinit();
DRW_render_instance_buffer_finish();
}
drw_task_graph_deinit();
/* Start Drawing */
DRW_state_reset();
@@ -2411,6 +2417,7 @@ void DRW_draw_select_id(Depsgraph *depsgraph, ARegion *region, View3D *v3d, cons
drw_engines_cache_finish();
drw_task_graph_deinit();
#if 0 /* This is a workaround to a nasty bug that seems to be a nasty driver bug. (See T69377) */
DRW_render_instance_buffer_finish();
#else
@@ -2419,7 +2426,6 @@ void DRW_draw_select_id(Depsgraph *depsgraph, ARegion *region, View3D *v3d, cons
drw_resource_buffer_finish(DST.vmempool);
#endif
}
drw_task_graph_deinit();
/* Start Drawing */
DRW_state_reset();

View File

@@ -548,6 +548,8 @@ typedef struct DRWManager {
#endif
struct TaskGraph *task_graph;
/* Contains list of objects that needs to be extracted from other objects. */
struct GSet *delayed_extraction;
/* ---------- Nothing after this point is cleared after use ----------- */
@@ -585,6 +587,7 @@ eDRWCommandType command_type_get(uint64_t *command_type_bits, int index);
void drw_batch_cache_validate(Object *ob);
void drw_batch_cache_generate_requested(struct Object *ob);
void drw_batch_cache_generate_requested_delayed(Object *ob);
void drw_resource_buffer_finish(ViewportMemoryPool *vmempool);