From fcc844f8fbd0d10aeb5012c0b25babe76c278e9e Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Mon, 14 Jun 2021 23:50:24 +1000 Subject: BLI: use explicit task isolation, no longer part of parallel operations After looking into task isolation issues with Sergey, we couldn't find the reason behind the deadlocks that we are getting in T87938 and a Sprite Fright file involving motion blur renders. There is no apparent place where we adding or waiting on tasks in a task group from different isolation regions, which is what is known to cause problems. Yet it still hangs. Either we do not understand some limitation of TBB isolation, or there is a bug in TBB, but we could not figure it out. Instead the idea is to use isolation only where we know we need it: when holding a mutex lock and then doing some multithreaded operation within that locked region. Three places where we do this now: * Generated images * Cached BVH tree building * OpenVDB lazy grid loading Compared to the more automatic approach previously used, there is the downside that it is easy to miss places where we need isolation. Yet doing it more automatically is also causing unexpected issue and bugs that we found no solution for, so this seems better. Patch implemented by Sergey and me. Differential Revision: https://developer.blender.org/D11603 --- source/blender/blenkernel/intern/image.c | 55 +++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 11 deletions(-) (limited to 'source/blender/blenkernel/intern/image.c') diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index 2f7e2b41a73..dad518ec696 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -68,6 +68,7 @@ #include "BLI_math_vector.h" #include "BLI_mempool.h" #include "BLI_system.h" +#include "BLI_task.h" #include "BLI_threads.h" #include "BLI_timecode.h" /* For stamp time-code format. */ #include "BLI_utildefines.h" @@ -882,6 +883,39 @@ Image *BKE_image_load_exists(Main *bmain, const char *filepath) return BKE_image_load_exists_ex(bmain, filepath, NULL); } +typedef struct ImageFillData { + short gen_type; + uint width; + uint height; + unsigned char *rect; + float *rect_float; + float fill_color[4]; +} ImageFillData; + +static void image_buf_fill_isolated(void *usersata_v) +{ + ImageFillData *usersata = usersata_v; + + const short gen_type = usersata->gen_type; + const uint width = usersata->width; + const uint height = usersata->height; + + unsigned char *rect = usersata->rect; + float *rect_float = usersata->rect_float; + + switch (gen_type) { + case IMA_GENTYPE_GRID: + BKE_image_buf_fill_checker(rect, rect_float, width, height); + break; + case IMA_GENTYPE_GRID_COLOR: + BKE_image_buf_fill_checker_color(rect, rect_float, width, height); + break; + default: + BKE_image_buf_fill_color(rect, rect_float, width, height, usersata->fill_color); + break; + } +} + static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, @@ -944,17 +978,16 @@ static ImBuf *add_ibuf_size(unsigned int width, STRNCPY(ibuf->name, name); - switch (gen_type) { - case IMA_GENTYPE_GRID: - BKE_image_buf_fill_checker(rect, rect_float, width, height); - break; - case IMA_GENTYPE_GRID_COLOR: - BKE_image_buf_fill_checker_color(rect, rect_float, width, height); - break; - default: - BKE_image_buf_fill_color(rect, rect_float, width, height, fill_color); - break; - } + ImageFillData data; + + data.gen_type = gen_type; + data.width = width; + data.height = height; + data.rect = rect; + data.rect_float = rect_float; + copy_v4_v4(data.fill_color, fill_color); + + BLI_task_isolate(image_buf_fill_isolated, &data); return ibuf; } -- cgit v1.2.3