Fix T63178 Eevee animation render crash
authorClément Foucault <foucault.clem@gmail.com>
Tue, 23 Apr 2019 18:34:26 +0000 (20:34 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Tue, 23 Apr 2019 18:35:02 +0000 (20:35 +0200)
If image buffer is not loaded and blender attempts to reload it (during
`BKE_image_acquire_ibuf`) over and over for each frame rendered.
When attempting this reload, image_load_image_file is calling
`BKE_image_free_buffers` and tag the Image to the (GPU) image_free_queue
(because this run on the rendering thread).

If the main thread decide to redraw the UI and go through `GPU_free_unused_buffers` they all get deleted and if that happens before the rendering thread use them ... segfault.

If I replace the environment textures with correct ones (the file does not seems to contain them), there is no crash when rendering.

I used a list of GPUTexture from blender Image to increase and decrease the
reference counter correctly.

This add very little memory and computation overhead.

source/blender/draw/intern/draw_manager.c
source/blender/draw/intern/draw_manager_data.c
source/blender/gpu/GPU_viewport.h
source/blender/gpu/intern/gpu_draw.c
source/blender/gpu/intern/gpu_viewport.c

index fc66d52..31dd582 100644 (file)
@@ -519,11 +519,20 @@ static void drw_viewport_cache_resize(void)
   GPU_viewport_cache_release(DST.viewport);
 
   if (DST.vmempool != NULL) {
+    /* Release Image textures. */
+    BLI_mempool_iter iter;
+    GPUTexture **tex;
+    BLI_mempool_iternew(DST.vmempool->images, &iter);
+    while ((tex = BLI_mempool_iterstep(&iter))) {
+      GPU_texture_free(*tex);
+    }
+
     BLI_mempool_clear_ex(DST.vmempool->calls, BLI_mempool_len(DST.vmempool->calls));
     BLI_mempool_clear_ex(DST.vmempool->states, BLI_mempool_len(DST.vmempool->states));
     BLI_mempool_clear_ex(DST.vmempool->shgroups, BLI_mempool_len(DST.vmempool->shgroups));
     BLI_mempool_clear_ex(DST.vmempool->uniforms, BLI_mempool_len(DST.vmempool->uniforms));
     BLI_mempool_clear_ex(DST.vmempool->passes, BLI_mempool_len(DST.vmempool->passes));
+    BLI_mempool_clear_ex(DST.vmempool->images, BLI_mempool_len(DST.vmempool->images));
   }
 
   DRW_instance_data_list_free_unused(DST.idatalist);
@@ -603,6 +612,10 @@ static void drw_viewport_var_init(void)
     if (DST.vmempool->passes == NULL) {
       DST.vmempool->passes = BLI_mempool_create(sizeof(DRWPass), 0, 64, 0);
     }
+    if (DST.vmempool->images == NULL) {
+      DST.vmempool->images = BLI_mempool_create(
+          sizeof(GPUTexture *), 0, 512, BLI_MEMPOOL_ALLOW_ITER);
+    }
 
     DST.idatalist = GPU_viewport_instance_data_list_get(DST.viewport);
     DRW_instance_data_list_reset(DST.idatalist);
index 77f8497..de3505b 100644 (file)
@@ -981,8 +981,12 @@ static DRWShadingGroup *drw_shgroup_material_inputs(DRWShadingGroup *grp,
       GPUTexture *tex = NULL;
 
       if (input->ima) {
-        tex = GPU_texture_from_blender(
+        GPUTexture **tex_ref = BLI_mempool_alloc(DST.vmempool->images);
+
+        *tex_ref = tex = GPU_texture_from_blender(
             input->ima, input->iuser, GL_TEXTURE_2D, input->image_isdata);
+
+        GPU_texture_ref(tex);
       }
       else {
         /* Color Ramps */
index 23e5ee2..198a9ec 100644 (file)
@@ -42,6 +42,7 @@ typedef struct ViewportMemoryPool {
   struct BLI_mempool *shgroups;
   struct BLI_mempool *uniforms;
   struct BLI_mempool *passes;
+  struct BLI_mempool *images;
 } ViewportMemoryPool;
 
 /* All FramebufferLists are just the same pointers with different names */
index e25c7d7..edc2f21 100644 (file)
@@ -1199,12 +1199,13 @@ void GPU_free_smoke_velocity(SmokeModifierData *smd)
 }
 
 static LinkNode *image_free_queue = NULL;
+static ThreadMutex img_queue_mutex = BLI_MUTEX_INITIALIZER;
 
 static void gpu_queue_image_for_free(Image *ima)
 {
-  BLI_thread_lock(LOCK_OPENGL);
+  BLI_mutex_lock(&img_queue_mutex);
   BLI_linklist_prepend(&image_free_queue, ima);
-  BLI_thread_unlock(LOCK_OPENGL);
+  BLI_mutex_unlock(&img_queue_mutex);
 }
 
 void GPU_free_unused_buffers(Main *bmain)
@@ -1213,7 +1214,7 @@ void GPU_free_unused_buffers(Main *bmain)
     return;
   }
 
-  BLI_thread_lock(LOCK_OPENGL);
+  BLI_mutex_lock(&img_queue_mutex);
 
   /* images */
   for (LinkNode *node = image_free_queue; node; node = node->next) {
@@ -1228,7 +1229,7 @@ void GPU_free_unused_buffers(Main *bmain)
   BLI_linklist_free(image_free_queue, NULL);
   image_free_queue = NULL;
 
-  BLI_thread_unlock(LOCK_OPENGL);
+  BLI_mutex_unlock(&img_queue_mutex);
 }
 
 static void gpu_free_image_immediate(Image *ima)
index 88b97ee..558b3f0 100644 (file)
@@ -632,6 +632,15 @@ void GPU_viewport_free(GPUViewport *viewport)
   if (viewport->vmempool.passes != NULL) {
     BLI_mempool_destroy(viewport->vmempool.passes);
   }
+  if (viewport->vmempool.images != NULL) {
+    BLI_mempool_iter iter;
+    GPUTexture **tex;
+    BLI_mempool_iternew(viewport->vmempool.images, &iter);
+    while ((tex = BLI_mempool_iterstep(&iter))) {
+      GPU_texture_free(*tex);
+    }
+    BLI_mempool_destroy(viewport->vmempool.images);
+  }
 
   DRW_instance_data_list_free(viewport->idatalist);
   MEM_freeN(viewport->idatalist);