diff options
author | Germano Cavalcante <mano-wii> | 2021-06-21 22:25:53 +0300 |
---|---|---|
committer | Germano Cavalcante <germano.costa@ig.com.br> | 2021-06-21 22:41:50 +0300 |
commit | b11a463e4fcd98f2fff6e05a03e97e71b93b8274 (patch) | |
tree | 6d6e3a294e19c75f3dfe2f9a1d06ab96875be04f /source/blender/editors/space_view3d | |
parent | b665ad8621a0db265fd666542d26aed463025db1 (diff) |
Refactor: Do not keep a copy of depth buffer in RegionView3D
The depth cache (located in `RegionView3D::depths`) is used for quick
and simple occlusion testing in:
- particle selection,
- "Draw Curve" operator and
- "Interactive Light Track to Cursor" operator,
However, keeping a texture buffer in cache is not a recommended practice.
For displays with high resolution like 8k this represents something
around 132MB.
Also, currently, each call to `ED_view3d_depth_override` invalidates
the depth cache. So that depth is never reused in multiple calls from
an operator (this was not the case in blender 2.79).
This commit allows to create a depth cache and release it in the same
operator. Thus, the buffer is kept in cache for a short time, freeing
up space.
No functional changes.
Diffstat (limited to 'source/blender/editors/space_view3d')
6 files changed, 44 insertions, 99 deletions
diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c index 8c356bc1600..c9f345b3123 100644 --- a/source/blender/editors/space_view3d/space_view3d.c +++ b/source/blender/editors/space_view3d/space_view3d.c @@ -786,12 +786,6 @@ static void view3d_main_region_free(ARegion *region) RE_engine_free(rv3d->render_engine); } - if (rv3d->depths) { - if (rv3d->depths->depths) { - MEM_freeN(rv3d->depths->depths); - } - MEM_freeN(rv3d->depths); - } if (rv3d->sms) { MEM_freeN(rv3d->sms); } @@ -815,7 +809,6 @@ static void *view3d_main_region_duplicate(void *poin) new->clipbb = MEM_dupallocN(rv3d->clipbb); } - new->depths = NULL; new->render_engine = NULL; new->sms = NULL; new->smooth_timer = NULL; diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c index 0bc19887118..c024bab355d 100644 --- a/source/blender/editors/space_view3d/view3d_draw.c +++ b/source/blender/editors/space_view3d/view3d_draw.c @@ -2221,7 +2221,7 @@ int ED_view3d_backbuf_sample_size_clamp(ARegion *region, const float dist) /** \name Z-Depth Utilities * \{ */ -void view3d_update_depths_rect(ARegion *region, ViewDepths *d, rcti *rect) +void view3d_depths_rect_create(ARegion *region, rcti *rect, ViewDepths *r_d) { /* clamp rect by region */ rcti r = { @@ -2242,70 +2242,44 @@ void view3d_update_depths_rect(ARegion *region, ViewDepths *d, rcti *rect) int h = BLI_rcti_size_y(rect); if (w <= 0 || h <= 0) { - if (d->depths) { - MEM_freeN(d->depths); - } - d->depths = NULL; - - d->damaged = false; + r_d->depths = NULL; + return; } - else if (d->w != w || d->h != h || d->x != x || d->y != y || d->depths == NULL) { - d->x = x; - d->y = y; - d->w = w; - d->h = h; - if (d->depths) { - MEM_freeN(d->depths); - } - - d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths Subset"); + r_d->x = x; + r_d->y = y; + r_d->w = w; + r_d->h = h; - d->damaged = true; - } + r_d->depths = MEM_mallocN(sizeof(float) * w * h, "View depths Subset"); - if (d->damaged) { + { GPUViewport *viewport = WM_draw_region_get_viewport(region); - view3d_opengl_read_Z_pixels(viewport, rect, d->depths); + view3d_opengl_read_Z_pixels(viewport, rect, r_d->depths); /* Range is assumed to be this as they are never changed. */ - d->depth_range[0] = 0.0; - d->depth_range[1] = 1.0; - d->damaged = false; + r_d->depth_range[0] = 0.0; + r_d->depth_range[1] = 1.0; } } /* Note, with nouveau drivers the glReadPixels() is very slow. T24339. */ -static void view3d_depth_cache_update(ARegion *region) +static ViewDepths *view3d_depths_create(ARegion *region) { - RegionView3D *rv3d = region->regiondata; + ViewDepths *d = MEM_callocN(sizeof(ViewDepths), "ViewDepths"); + d->w = region->winx; + d->h = region->winy; + d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths"); - /* Create storage for, and, if necessary, copy depth buffer. */ - if (!rv3d->depths) { - rv3d->depths = MEM_callocN(sizeof(ViewDepths), "ViewDepths"); - } - if (rv3d->depths) { - ViewDepths *d = rv3d->depths; - if (d->w != region->winx || d->h != region->winy || !d->depths) { - d->w = region->winx; - d->h = region->winy; - if (d->depths) { - MEM_freeN(d->depths); - } - d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths"); - d->damaged = true; - } - - if (d->damaged) { - GPUViewport *viewport = WM_draw_region_get_viewport(region); - DefaultFramebufferList *fbl = GPU_viewport_framebuffer_list_get(viewport); - GPU_framebuffer_read_depth(fbl->depth_only_fb, 0, 0, d->w, d->h, GPU_DATA_FLOAT, d->depths); + { + GPUViewport *viewport = WM_draw_region_get_viewport(region); + DefaultFramebufferList *fbl = GPU_viewport_framebuffer_list_get(viewport); + GPU_framebuffer_read_depth(fbl->depth_only_fb, 0, 0, d->w, d->h, GPU_DATA_FLOAT, d->depths); - /* Assumed to be this as they are never changed. */ - d->depth_range[0] = 0.0; - d->depth_range[1] = 1.0; - d->damaged = false; - } + /* Assumed to be this as they are never changed. */ + d->depth_range[0] = 0.0; + d->depth_range[1] = 1.0; } + return d; } /* Utility function to find the closest Z value, use for auto-depth. */ @@ -2345,7 +2319,7 @@ void ED_view3d_depth_override(Depsgraph *depsgraph, View3D *v3d, Object *obact, eV3DDepthOverrideMode mode, - bool update_cache) + ViewDepths **r_depths) { if (v3d->runtime.flag & V3D_RUNTIME_DEPTHBUF_OVERRIDDEN) { return; @@ -2390,12 +2364,8 @@ void ED_view3d_depth_override(Depsgraph *depsgraph, break; } - if (rv3d->depths != NULL) { - rv3d->depths->damaged = true; - /* TODO: Clear cache? */ - } - if (update_cache) { - view3d_depth_cache_update(region); + if (r_depths) { + *r_depths = view3d_depths_create(region); } } @@ -2409,6 +2379,13 @@ void ED_view3d_depth_override(Depsgraph *depsgraph, UI_Theme_Restore(&theme_state); } +void ED_view3d_depths_free(ViewDepths *depths) +{ + if (depths->depths) { + MEM_freeN(depths->depths); + } +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/editors/space_view3d/view3d_edit.c b/source/blender/editors/space_view3d/view3d_edit.c index 8b6d0e9ee04..ab0683644c3 100644 --- a/source/blender/editors/space_view3d/view3d_edit.c +++ b/source/blender/editors/space_view3d/view3d_edit.c @@ -953,7 +953,6 @@ static int viewrotate_modal(bContext *C, wmOperator *op, const wmEvent *event) } } else if (event_code == VIEW_CONFIRM) { - ED_view3d_depth_tag_update(vod->rv3d); use_autokey = true; ret = OPERATOR_FINISHED; } @@ -1014,7 +1013,6 @@ static int viewrotate_invoke(bContext *C, wmOperator *op, const wmEvent *event) } viewrotate_apply(vod, event_xy); - ED_view3d_depth_tag_update(vod->rv3d); viewops_data_free(C, op); @@ -1799,7 +1797,6 @@ static int viewmove_modal(bContext *C, wmOperator *op, const wmEvent *event) } } else if (event_code == VIEW_CONFIRM) { - ED_view3d_depth_tag_update(vod->rv3d); use_autokey = true; ret = OPERATOR_FINISHED; } @@ -1840,7 +1837,6 @@ static int viewmove_invoke(bContext *C, wmOperator *op, const wmEvent *event) if (event->type == MOUSEPAN) { /* invert it, trackpad scroll follows same principle as 2d windows this way */ viewmove_apply(vod, 2 * event->x - event->prevx, 2 * event->y - event->prevy); - ED_view3d_depth_tag_update(vod->rv3d); viewops_data_free(C, op); @@ -2254,7 +2250,6 @@ static int viewzoom_modal(bContext *C, wmOperator *op, const wmEvent *event) } } else if (event_code == VIEW_CONFIRM) { - ED_view3d_depth_tag_update(vod->rv3d); use_autokey = true; ret = OPERATOR_FINISHED; } @@ -2341,8 +2336,6 @@ static int viewzoom_exec(bContext *C, wmOperator *op) view3d_boxview_sync(area, region); } - ED_view3d_depth_tag_update(rv3d); - ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d); ED_view3d_camera_lock_autokey(v3d, rv3d, C, false, true); @@ -2398,8 +2391,6 @@ static int viewzoom_invoke(bContext *C, wmOperator *op, const wmEvent *event) (use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS))); ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true); - ED_view3d_depth_tag_update(vod->rv3d); - viewops_data_free(C, op); return OPERATOR_FINISHED; } @@ -2579,7 +2570,6 @@ static int viewdolly_modal(bContext *C, wmOperator *op, const wmEvent *event) } } else if (event_code == VIEW_CONFIRM) { - ED_view3d_depth_tag_update(vod->rv3d); use_autokey = true; ret = OPERATOR_FINISHED; } @@ -2636,8 +2626,6 @@ static int viewdolly_exec(bContext *C, wmOperator *op) view3d_boxview_sync(area, region); } - ED_view3d_depth_tag_update(rv3d); - ED_view3d_camera_lock_sync(CTX_data_ensure_evaluated_depsgraph(C), v3d, rv3d); ED_region_tag_redraw(region); @@ -2718,7 +2706,6 @@ static int viewdolly_invoke(bContext *C, wmOperator *op, const wmEvent *event) event->prevx; } viewdolly_apply(vod, &event->prevx, (U.uiflag & USER_ZOOM_INVERT) == 0); - ED_view3d_depth_tag_update(vod->rv3d); viewops_data_free(C, op); return OPERATOR_FINISHED; @@ -3629,13 +3616,13 @@ static int view3d_zoom_border_exec(bContext *C, wmOperator *op) ED_view3d_dist_range_get(v3d, dist_range); ED_view3d_depth_override( - CTX_data_ensure_evaluated_depsgraph(C), region, v3d, NULL, V3D_DEPTH_NO_GPENCIL, false); + CTX_data_ensure_evaluated_depsgraph(C), region, v3d, NULL, V3D_DEPTH_NO_GPENCIL, NULL); { /* avoid allocating the whole depth buffer */ ViewDepths depth_temp = {0}; /* avoid view3d_update_depths() for speed. */ - view3d_update_depths_rect(region, &depth_temp, &rect); + view3d_depths_rect_create(region, &rect, &depth_temp); /* find the closest Z pixel */ depth_close = view3d_depth_near(&depth_temp); @@ -4433,7 +4420,6 @@ static int viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event) } } else if (event_code == VIEW_CONFIRM) { - ED_view3d_depth_tag_update(vod->rv3d); use_autokey = true; ret = OPERATOR_FINISHED; } @@ -4541,7 +4527,6 @@ static int viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event) if (event->type == MOUSEROTATE) { vod->init.event_xy[0] = vod->prev.event_xy[0] = event->x; viewroll_apply(vod, event->prevx, event->prevy); - ED_view3d_depth_tag_update(vod->rv3d); viewops_data_free(C, op); return OPERATOR_FINISHED; @@ -4638,7 +4623,6 @@ static int viewpan_invoke(bContext *C, wmOperator *op, const wmEvent *event) viewmove_apply(vod, vod->prev.event_xy[0] + x, vod->prev.event_xy[1] + y); - ED_view3d_depth_tag_update(vod->rv3d); viewops_data_free(C, op); return OPERATOR_FINISHED; diff --git a/source/blender/editors/space_view3d/view3d_intern.h b/source/blender/editors/space_view3d/view3d_intern.h index 6f07cb8b44d..0964c2dcbcc 100644 --- a/source/blender/editors/space_view3d/view3d_intern.h +++ b/source/blender/editors/space_view3d/view3d_intern.h @@ -137,7 +137,7 @@ void ED_view3d_draw_depth_loop(struct Depsgraph *depsgraph, struct ARegion *region, View3D *v3d); -void view3d_update_depths_rect(struct ARegion *region, struct ViewDepths *d, struct rcti *rect); +void view3d_depths_rect_create(struct ARegion *region, struct rcti *rect, struct ViewDepths *r_d); float view3d_depth_near(struct ViewDepths *d); /* view3d_select.c */ diff --git a/source/blender/editors/space_view3d/view3d_select.c b/source/blender/editors/space_view3d/view3d_select.c index 5bdeb2cb35f..f768d7faf63 100644 --- a/source/blender/editors/space_view3d/view3d_select.c +++ b/source/blender/editors/space_view3d/view3d_select.c @@ -4385,7 +4385,7 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op) FOREACH_OBJECT_IN_MODE_END; } else if (obact && (obact->mode & OB_MODE_PARTICLE_EDIT)) { - if (PE_circle_select(C, sel_op, mval, (float)radius)) { + if (PE_circle_select(C, wm_userdata, sel_op, mval, (float)radius)) { return OPERATOR_FINISHED; } return OPERATOR_CANCELLED; diff --git a/source/blender/editors/space_view3d/view3d_utils.c b/source/blender/editors/space_view3d/view3d_utils.c index 8ae5d4a29e9..c7da3378ae3 100644 --- a/source/blender/editors/space_view3d/view3d_utils.c +++ b/source/blender/editors/space_view3d/view3d_utils.c @@ -1017,9 +1017,9 @@ static float view_autodist_depth_margin(ARegion *region, const int mval[2], int } ViewDepths depth_temp = {0}; - view3d_update_depths_rect(region, &depth_temp, &rect); + view3d_depths_rect_create(region, &rect, &depth_temp); float depth_close = view3d_depth_near(&depth_temp); - MEM_SAFE_FREE(depth_temp.depths); + ED_view3d_depths_free(&depth_temp); return depth_close; } @@ -1044,7 +1044,7 @@ bool ED_view3d_autodist(Depsgraph *depsgraph, bool depth_ok = false; /* Get Z Depths, needed for perspective, nice for ortho */ - ED_view3d_depth_override(depsgraph, region, v3d, NULL, V3D_DEPTH_NO_GPENCIL, false); + ED_view3d_depth_override(depsgraph, region, v3d, NULL, V3D_DEPTH_NO_GPENCIL, NULL); /* Attempt with low margin's first */ int i = 0; @@ -1694,7 +1694,8 @@ bool ED_view3d_depth_read_cached(const ViewDepths *vd, return false; } -bool ED_view3d_depth_read_cached_normal(const ViewContext *vc, +bool ED_view3d_depth_read_cached_normal(const ARegion *region, + const ViewDepths *depths, const int mval[2], float r_normal[3]) { @@ -1705,9 +1706,6 @@ bool ED_view3d_depth_read_cached_normal(const ViewContext *vc, bool depths_valid[9] = {false}; float coords[9][3] = {{0}}; - ARegion *region = vc->region; - const ViewDepths *depths = vc->rv3d->depths; - for (int x = 0, i = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { const int mval_ofs[2] = {mval[0] + (x - 1), mval[1] + (y - 1)}; @@ -1761,11 +1759,4 @@ bool ED_view3d_depth_unproject_v3(const ARegion *region, return ED_view3d_unproject_v3(region, centx, centy, depth, r_location_world); } -void ED_view3d_depth_tag_update(RegionView3D *rv3d) -{ - if (rv3d->depths) { - rv3d->depths->damaged = true; - } -} - /** \} */ |