Decouple highlighted tiles from RenderPart

Should be no visible change on user side.
Preparing for render parts removal as part of Cycles X project.

Differential Revision: https://developer.blender.org/D12317
This commit is contained in:
Sergey Sharybin 2021-08-25 17:59:41 +02:00
parent ec66b3ef9b
commit c52db4c4cf
3 changed files with 72 additions and 25 deletions

View File

@ -291,6 +291,15 @@ static RenderPart *get_part_from_result(Render *re, RenderResult *result)
return BLI_ghash_lookup(re->parts, &key);
}
static HighlightedTile highlighted_tile_from_result_get(Render *re, RenderResult *result)
{
HighlightedTile tile;
tile.rect = result->tilerect;
BLI_rcti_translate(&tile.rect, re->disprect.xmin, re->disprect.ymin);
return tile;
}
RenderResult *RE_engine_begin_result(
RenderEngine *engine, int x, int y, int w, int h, const char *layername, const char *viewname)
{
@ -426,6 +435,30 @@ void RE_engine_end_result(
}
}
if (re->engine && (re->engine->flag & RE_ENGINE_HIGHLIGHT_TILES)) {
BLI_mutex_lock(&re->highlighted_tiles_mutex);
if (re->highlighted_tiles == NULL) {
re->highlighted_tiles = BLI_gset_new(
BLI_ghashutil_inthash_v4_p, BLI_ghashutil_inthash_v4_cmp, "highlighted tiles");
}
HighlightedTile tile = highlighted_tile_from_result_get(re, result);
if (highlight) {
void **tile_in_set;
if (!BLI_gset_ensure_p_ex(re->highlighted_tiles, &tile, &tile_in_set)) {
*tile_in_set = MEM_mallocN(sizeof(HighlightedTile), __func__);
memcpy(*tile_in_set, &tile, sizeof(tile));
}
BLI_gset_add(re->highlighted_tiles, &tile);
}
else {
BLI_gset_remove(re->highlighted_tiles, &tile, MEM_freeN);
}
BLI_mutex_unlock(&re->highlighted_tiles_mutex);
}
if (!cancel || merge_results) {
if (re->result->do_exr_tile) {
if (!cancel && merge_results) {
@ -597,43 +630,43 @@ rcti *RE_engine_get_current_tiles(Render *re, int *r_total_tiles, bool *r_needs_
rcti *tiles = tiles_static;
int allocation_size = BLENDER_MAX_THREADS;
BLI_rw_mutex_lock(&re->partsmutex, THREAD_LOCK_READ);
BLI_mutex_lock(&re->highlighted_tiles_mutex);
*r_needs_free = false;
if (!re->parts || (re->engine && (re->engine->flag & RE_ENGINE_HIGHLIGHT_TILES) == 0)) {
if (re->highlighted_tiles == NULL) {
*r_total_tiles = 0;
BLI_rw_mutex_unlock(&re->partsmutex);
BLI_mutex_unlock(&re->highlighted_tiles_mutex);
return NULL;
}
GHashIterator pa_iter;
GHASH_ITER (pa_iter, re->parts) {
RenderPart *pa = BLI_ghashIterator_getValue(&pa_iter);
if (pa->status == PART_STATUS_IN_PROGRESS) {
if (total_tiles >= allocation_size) {
/* Just in case we're using crazy network rendering with more
* workers than BLENDER_MAX_THREADS.
GSET_FOREACH_BEGIN (HighlightedTile *, tile, re->highlighted_tiles) {
if (total_tiles >= allocation_size) {
/* Just in case we're using crazy network rendering with more
* workers than BLENDER_MAX_THREADS.
*/
allocation_size += allocation_step;
if (tiles == tiles_static) {
/* Can not realloc yet, tiles are pointing to a
* stack memory.
*/
allocation_size += allocation_step;
if (tiles == tiles_static) {
/* Can not realloc yet, tiles are pointing to a
* stack memory.
*/
tiles = MEM_mallocN(allocation_size * sizeof(rcti), "current engine tiles");
}
else {
tiles = MEM_reallocN(tiles, allocation_size * sizeof(rcti));
}
*r_needs_free = true;
tiles = MEM_mallocN(allocation_size * sizeof(rcti), "current engine tiles");
}
tiles[total_tiles] = pa->disprect;
total_tiles++;
else {
tiles = MEM_reallocN(tiles, allocation_size * sizeof(rcti));
}
*r_needs_free = true;
}
tiles[total_tiles] = tile->rect;
total_tiles++;
}
BLI_rw_mutex_unlock(&re->partsmutex);
GSET_FOREACH_END();
BLI_mutex_unlock(&re->highlighted_tiles_mutex);
*r_total_tiles = total_tiles;
return tiles;
}

View File

@ -569,6 +569,7 @@ Render *RE_NewRender(const char *name)
BLI_strncpy(re->name, name, RE_MAXNAME);
BLI_rw_mutex_init(&re->resultmutex);
BLI_rw_mutex_init(&re->partsmutex);
BLI_mutex_init(&re->highlighted_tiles_mutex);
}
RE_InitRenderCB(re);
@ -633,12 +634,17 @@ void RE_FreeRender(Render *re)
BLI_rw_mutex_end(&re->resultmutex);
BLI_rw_mutex_end(&re->partsmutex);
BLI_mutex_end(&re->highlighted_tiles_mutex);
BLI_freelistN(&re->view_layers);
BLI_freelistN(&re->r.views);
BKE_curvemapping_free_data(&re->r.mblur_shutter_curve);
if (re->highlighted_tiles != NULL) {
BLI_gset_free(re->highlighted_tiles, MEM_freeN);
}
/* main dbase can already be invalid now, some database-free code checks it */
re->main = NULL;
re->scene = NULL;

View File

@ -37,6 +37,7 @@
#include "RE_pipeline.h"
struct GHash;
struct GSet;
struct Main;
struct Object;
struct RenderEngine;
@ -59,6 +60,10 @@ typedef struct RenderPart {
short status;
} RenderPart;
typedef struct HighlightedTile {
rcti rect;
} HighlightedTile;
enum {
/* PART_STATUS_NONE = 0, */ /* UNUSED */
PART_STATUS_IN_PROGRESS = 1,
@ -118,6 +123,9 @@ struct Render {
ThreadRWMutex partsmutex;
struct GHash *parts;
ThreadMutex highlighted_tiles_mutex;
struct GSet *highlighted_tiles;
/* render engine */
struct RenderEngine *engine;