Fix threading issues of viewport rendering when using movies/generated images
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 24 Jan 2013 08:14:20 +0000 (08:14 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 24 Jan 2013 08:14:20 +0000 (08:14 +0000)
Issue was caused by the fact that guarded allocator is not thread-safe and
generated images/movies could allocate memory when loading pixels to Cycles.

Currently solved by switching memory allocator to using mutex lock (the same
as sued for jobs) when viewport rendering is used.

Nicer solution would be to make guarded allocator thread-safe by using atomic
operations and lock-free lists, but that's more serious change.

source/blender/editors/space_view3d/view3d_draw.c
source/blender/render/extern/include/RE_engine.h
source/blender/render/intern/source/external_engine.c

index 39581adb62247ce85be21aecfd8512b84d6d411c..ff63f16d1252a868b5e0dc4fdc0515a202998f6e 100644 (file)
@@ -2883,7 +2883,7 @@ static int view3d_main_area_draw_engine(const bContext *C, ARegion *ar, int draw
                if (!(type->view_update && type->view_draw))
                        return 0;
 
-               engine = RE_engine_create(type);
+               engine = RE_engine_create_ex(type, TRUE);
 
                engine->tile_x = scene->r.tilex;
                engine->tile_y = scene->r.tiley;
index 64135a16f5d3a5702c91a9fd8a3b18f8ec1bc6c8..b687acae1f7a8e5923cf95de4190bad0b092de83 100644 (file)
@@ -62,6 +62,7 @@ struct Scene;
 #define RE_ENGINE_DO_UPDATE            8
 #define RE_ENGINE_RENDERING            16
 #define RE_ENGINE_HIGHLIGHT_TILES      32
+#define RE_ENGINE_USED_FOR_VIEWPORT    64
 
 extern ListBase R_engines;
 
@@ -105,6 +106,7 @@ typedef struct RenderEngine {
 } RenderEngine;
 
 RenderEngine *RE_engine_create(RenderEngineType *type);
+RenderEngine *RE_engine_create_ex(RenderEngineType *type, int use_for_viewport);
 void RE_engine_free(RenderEngine *engine);
 
 void RE_layer_load_from_file(struct RenderLayer *layer, struct ReportList *reports, const char *filename, int x, int y);
index 296c8b6eba8dd9e6f50e524ed6ae5660ec835eb7..22a49bcbbc388c725994ea174a0928faae98bc60 100644 (file)
@@ -1,4 +1,5 @@
 /*
+
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -127,10 +128,21 @@ int RE_engine_is_external(Render *re)
 /* Create, Free */
 
 RenderEngine *RE_engine_create(RenderEngineType *type)
+{
+       return RE_engine_create_ex(type, FALSE);
+}
+
+RenderEngine *RE_engine_create_ex(RenderEngineType *type, int use_for_viewport)
 {
        RenderEngine *engine = MEM_callocN(sizeof(RenderEngine), "RenderEngine");
        engine->type = type;
 
+       if (use_for_viewport) {
+               engine->flag |= RE_ENGINE_USED_FOR_VIEWPORT;
+
+               BLI_begin_threaded_malloc();
+       }
+
        return engine;
 }
 
@@ -142,6 +154,10 @@ void RE_engine_free(RenderEngine *engine)
        }
 #endif
 
+       if (engine->flag & RE_ENGINE_USED_FOR_VIEWPORT) {
+               BLI_end_threaded_malloc();
+       }
+
        if (engine->text)
                MEM_freeN(engine->text);