Sculpt: Delay Viewport Updates

In Blender 2.81 we update and draw all nodes inside the view planes.
When navigating with a pen tablet after an operation that tags the whole
mesh to update (like undo or inverting the mask), this introduces some
lag as nodes are updating when they enter the view. The viewport is not
fully responsive again until all nodes have entered the view after the
operation.

This commit delays nodes updates until the view navigation stops, so the
viewport navigation is always fully responsive. This introduces some
artifacts while navigating,  so it can be disabled if you don't want to
see them.

I'm storing the update planes in the PBVH. This way I can add support
for some tools to update in real-time only the nodes inside this plane
while running the operator, like the mesh filter.

Reviewed By: jbakker

Differential Revision: https://developer.blender.org/D6269
This commit is contained in:
Pablo Dobarro 2020-03-12 17:51:39 +01:00 committed by Pablo Dobarro
parent 34465a7fb0
commit b8d9b5e331
7 changed files with 89 additions and 13 deletions

View File

@ -903,6 +903,8 @@ class VIEW3D_PT_sculpt_options(Panel, View3DPaintPanel):
col = flow.column()
col.prop(sculpt, "show_low_resolution")
col = flow.column()
col.prop(sculpt, "use_sculpt_delay_updates")
col = flow.column()
col.prop(sculpt, "use_deform_only")
col = flow.column()

View File

@ -81,6 +81,9 @@ typedef struct PBVHFrustumPlanes {
int num_planes;
} PBVHFrustumPlanes;
void BKE_pbvh_set_frustum_planes(PBVH *bvh, PBVHFrustumPlanes *planes);
void BKE_pbvh_get_frustum_planes(PBVH *bvh, PBVHFrustumPlanes *planes);
/* Callbacks */
/* returns 1 if the search should continue from this node, 0 otherwise */
@ -190,7 +193,8 @@ bool BKE_pbvh_node_find_nearest_to_ray(PBVH *bvh,
void BKE_pbvh_draw_cb(PBVH *bvh,
bool show_vcol,
bool update_only_visible,
PBVHFrustumPlanes *frustum,
PBVHFrustumPlanes *update_frustum,
PBVHFrustumPlanes *draw_frustum,
void (*draw_fn)(void *user_data, struct GPU_PBVH_Buffers *buffers),
void *user_data);

View File

@ -2683,7 +2683,8 @@ static bool pbvh_draw_search_cb(PBVHNode *node, void *data_v)
void BKE_pbvh_draw_cb(PBVH *bvh,
bool show_vcol,
bool update_only_visible,
PBVHFrustumPlanes *frustum,
PBVHFrustumPlanes *update_frustum,
PBVHFrustumPlanes *draw_frustum,
void (*draw_fn)(void *user_data, GPU_PBVH_Buffers *buffers),
void *user_data)
{
@ -2704,7 +2705,7 @@ void BKE_pbvh_draw_cb(PBVH *bvh,
}
/* Gather visible nodes. */
PBVHDrawSearchData data = {.frustum = frustum, .accum_update_flag = 0};
PBVHDrawSearchData data = {.frustum = update_frustum, .accum_update_flag = 0};
BKE_pbvh_search_gather(bvh, pbvh_draw_search_cb, &data, &nodes, &totnode);
if (update_only_visible && (data.accum_update_flag & update_flag)) {
@ -2722,7 +2723,15 @@ void BKE_pbvh_draw_cb(PBVH *bvh,
}
node->flag &= ~(PBVH_RebuildDrawBuffers | PBVH_UpdateDrawBuffers);
}
MEM_SAFE_FREE(nodes);
PBVHDrawSearchData draw_data = {.frustum = draw_frustum, .accum_update_flag = 0};
BKE_pbvh_search_gather(bvh, pbvh_draw_search_cb, &draw_data, &nodes, &totnode);
for (int a = 0; a < totnode; a++) {
PBVHNode *node = nodes[a];
if (!(node->flag & PBVH_FullyHidden)) {
draw_fn(user_data, node->draw_buffers);
}
@ -2998,6 +3007,22 @@ void pbvh_show_face_sets_set(PBVH *bvh, bool show_face_sets)
bvh->show_face_sets = show_face_sets;
}
void BKE_pbvh_set_frustum_planes(PBVH *bvh, PBVHFrustumPlanes *planes)
{
bvh->num_planes = planes->num_planes;
for (int i = 0; i < bvh->num_planes; i++) {
copy_v4_v4(bvh->planes[i], planes->planes[i]);
}
}
void BKE_pbvh_get_frustum_planes(PBVH *bvh, PBVHFrustumPlanes *planes)
{
planes->num_planes = bvh->num_planes;
for (int i = 0; i < planes->num_planes; i++) {
copy_v4_v4(planes->planes[i], bvh->planes[i]);
}
}
void BKE_pbvh_parallel_range_settings(PBVHParallelSettings *settings,
bool use_threading,
int totnode)

View File

@ -168,6 +168,9 @@ struct PBVH {
int cd_vert_node_offset;
int cd_face_node_offset;
float planes[6][4];
int num_planes;
struct BMLog *bm_log;
struct SubdivCCG *subdiv_ccg;
};

View File

@ -884,7 +884,11 @@ static float sculpt_debug_colors[9][4] = {
static void sculpt_draw_cb(DRWSculptCallbackData *scd, GPU_PBVH_Buffers *buffers)
{
if (!buffers) {
return;
}
/* Meh... use_mask is a bit misleading here. */
if (scd->use_mask && !GPU_pbvh_buffers_has_overlays(buffers)) {
return;
}
@ -958,24 +962,52 @@ static void drw_sculpt_generate_calls(DRWSculptCallbackData *scd, bool use_vcol)
const DRWContextState *drwctx = DRW_context_state_get();
RegionView3D *rv3d = drwctx->rv3d;
const bool navigating = rv3d && (rv3d->rflag & RV3D_NAVIGATING);
Paint *p = NULL;
if (drwctx->evil_C != NULL) {
p = BKE_paint_get_active_from_context(drwctx->evil_C);
}
/* Frustum planes to show only visible PBVH nodes. */
float planes[6][4];
drw_sculpt_get_frustum_planes(scd->ob, planes);
PBVHFrustumPlanes frustum = {.planes = planes, .num_planes = 6};
float update_planes[6][4];
float draw_planes[6][4];
PBVHFrustumPlanes update_frustum;
PBVHFrustumPlanes draw_frustum;
if (p && (p->flags & PAINT_SCULPT_DELAY_UPDATES)) {
update_frustum.planes = update_planes;
update_frustum.num_planes = 6;
BKE_pbvh_get_frustum_planes(pbvh, &update_frustum);
if (!navigating) {
drw_sculpt_get_frustum_planes(scd->ob, update_planes);
update_frustum.planes = update_planes;
update_frustum.num_planes = 6;
BKE_pbvh_set_frustum_planes(pbvh, &update_frustum);
}
}
else {
drw_sculpt_get_frustum_planes(scd->ob, update_planes);
update_frustum.planes = update_planes;
update_frustum.num_planes = 6;
}
drw_sculpt_get_frustum_planes(scd->ob, draw_planes);
draw_frustum.planes = draw_planes;
draw_frustum.num_planes = 6;
/* Fast mode to show low poly multires while navigating. */
scd->fast_mode = false;
if (drwctx->evil_C != NULL) {
Paint *p = BKE_paint_get_active_from_context(drwctx->evil_C);
if (p && (p->flags & PAINT_FAST_NAVIGATE)) {
scd->fast_mode = rv3d && (rv3d->rflag & RV3D_NAVIGATING);
}
if (p && (p->flags & PAINT_FAST_NAVIGATE)) {
scd->fast_mode = rv3d && (rv3d->rflag & RV3D_NAVIGATING);
}
/* Update draw buffers only for visible nodes while painting.
* But do update them otherwise so navigating stays smooth. */
const bool update_only_visible = rv3d && (rv3d->rflag & RV3D_PAINTING);
bool update_only_visible = rv3d && !(rv3d->rflag & RV3D_PAINTING);
if (p && (p->flags & PAINT_SCULPT_DELAY_UPDATES)) {
update_only_visible = true;
}
Mesh *mesh = scd->ob->data;
BKE_pbvh_update_normals(pbvh, mesh->runtime.subdiv_ccg);
@ -983,7 +1015,8 @@ static void drw_sculpt_generate_calls(DRWSculptCallbackData *scd, bool use_vcol)
BKE_pbvh_draw_cb(pbvh,
use_vcol,
update_only_visible,
&frustum,
&update_frustum,
&draw_frustum,
(void (*)(void *, GPU_PBVH_Buffers *))sculpt_draw_cb,
scd);

View File

@ -2137,6 +2137,7 @@ typedef enum ePaintFlags {
PAINT_FAST_NAVIGATE = (1 << 1),
PAINT_SHOW_BRUSH_ON_SURFACE = (1 << 2),
PAINT_USE_CAVITY_MASK = (1 << 3),
PAINT_SCULPT_DELAY_UPDATES = (1 << 4),
} ePaintFlags;
/* Paint.symmetry_flags

View File

@ -617,6 +617,14 @@ static void rna_def_paint(BlenderRNA *brna)
prop, "Fast Navigate", "For multires, show low resolution while navigating the view");
RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL);
prop = RNA_def_property(srna, "use_sculpt_delay_updates", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "flags", PAINT_SCULPT_DELAY_UPDATES);
RNA_def_property_ui_text(
prop,
"Delay Viewport Updates",
"Update the geometry when it enters the view, providing faster view navigation");
RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL);
prop = RNA_def_property(srna, "input_samples", PROP_INT, PROP_UNSIGNED);
RNA_def_property_int_sdna(prop, NULL, "num_input_samples");
RNA_def_property_ui_range(prop, 1, PAINT_MAX_INPUT_SAMPLES, 1, -1);