Cleanup: Use Vector for passing lists of PBVHNodes around #106884

Merged
Joseph Eagar merged 13 commits from temp-sculpt-vector-nodes into main 2023-04-14 21:16:53 +02:00
Member

Cleaned up sculpt code to store lists of PBVHNodes with
blender::Vector instead of simple pointer arrays. This is much
simpler and eliminates memory leaks caused by forgetting to free
the result of BKE_pbvh_search_gather.

Notes:

  • BKE_pbvh_search_gather is now blender::pbvh::search_gather.
  • FilterCache and ExpandCache have ownership over their .nodes
    members; as a result they're no longer pure C structs and
    are allocated with MEM_new/MEM_delete.
  • The word 'totnode' no longer occurs anywhere in
    source/blender/editors/sculpt_paint

Todo (not for this PR): create a new properly C++ task API for sculpt
(with lambdas) and use it for brushes.

Cleaned up sculpt code to store lists of `PBVHNodes` with `blender::Vector` instead of simple pointer arrays. This is much simpler and eliminates memory leaks caused by forgetting to free the result of `BKE_pbvh_search_gather`. Notes: * `BKE_pbvh_search_gather` is now `blender::pbvh::search_gather`. * `FilterCache` and `ExpandCache` have ownership over their .nodes members; as a result they're no longer pure C structs and are allocated with `MEM_new`/`MEM_delete`. * The word 'totnode' no longer occurs anywhere in `source/blender/editors/sculpt_paint` Todo (not for this PR): create a new properly C++ task API for sculpt (with lambdas) and use it for brushes.
Joseph Eagar added 1 commit 2023-04-13 06:35:36 +02:00
58d0f27c70 Cleanup: Use Vector for passing lists of PBVHNodes around
Cleaned up sculpt code to store lists of PBVHNodes with
blender::Vector instead of simple pointer arrays.  This is much
simpler and elimates memory leaks caused by forgetting to free
the result of BKE_pbvh_search_gather.

Notes:

* BKE_pbvh_search_gather is now blender::pbvh::search_gather.
* FilterCache and ExpandCache have ownership over their .nodes
  members; as a result they're no longer pure C structs and
  are allocated with MEM_new/delete.
* The word 'totnode' no longer occurs anywhere in
  source/blender/editors/sculpt_paint

Todo: create a new properly C++ task API for sculpt
      (with lambdas) and use it for brushes.
Joseph Eagar requested review from Hans Goudey 2023-04-13 06:36:05 +02:00
Joseph Eagar requested review from Sergey Sharybin 2023-04-13 06:36:24 +02:00
Joseph Eagar added 1 commit 2023-04-13 06:36:41 +02:00
Sergey Sharybin reviewed 2023-04-13 09:46:12 +02:00
Sergey Sharybin left a comment
Owner

Generally I really like the change. It definitely moves the code to a much better state.

There are couple of suggestion points.

Generally I really like the change. It definitely moves the code to a much better state. There are couple of suggestion points.
@ -837,0 +830,4 @@
namespace blender::pbvh {
Vector<PBVHNode *> search_gather(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data);
Vector<PBVHNode *> search_gather_ex(PBVH *pbvh,

To me it would be more readable to either have polymorphic search_gather () or a single search_gather() with the PBVHNodeFlags leaf_flag = PBVH_Leaf .

To me it would be more readable to either have polymorphic `search_gather ()` or a single `search_gather()` with the `PBVHNodeFlags leaf_flag = PBVH_Leaf `.
JosephEagar marked this conversation as resolved
@ -1660,3 +1663,3 @@
}
void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode)
void pbvh_bmesh_normals_update(Vector<PBVHNode *> &nodes)

For such things I find it much more reliable and flexible to use span. I think in BLI terminology it will be MutableSpan<PBVHNode *> nodes.

It decouples from the specific of the actual storage used, solves possible issues if one forgets to add &, and allows easier use for things like callback data (re)assignment.

And for cases where vector is to be passes and is not modified, use const qualifier.

For such things I find it much more reliable and flexible to use span. I think in BLI terminology it will be `MutableSpan<PBVHNode *> nodes`. It decouples from the specific of the actual storage used, solves possible issues if one forgets to add `&`, and allows easier use for things like callback data (re)assignment. And for cases where vector is to be passes and is not modified, use `const` qualifier.
Author
Member

Me and Hans settled on using Span.

Me and Hans settled on using Span.
Joseph Eagar added 2 commits 2023-04-13 12:12:05 +02:00

@blender-bot build

@blender-bot build
Joseph Eagar added 1 commit 2023-04-13 14:19:42 +02:00

Do you plan more changes w.r.t Span?
Seems that PBVHUpdateData, pbvh_faces_update_normals, pbvh_update_draw_buffers and others can use it. Is just quickly checking for Vector<PBVHNode *> & to see where it is still used.

Do you plan more changes w.r.t Span? Seems that `PBVHUpdateData`, `pbvh_faces_update_normals`, `pbvh_update_draw_buffers` and others can use it. Is just quickly checking for `Vector<PBVHNode *> &` to see where it is still used.
Hans Goudey requested changes 2023-04-13 14:34:54 +02:00
Hans Goudey left a comment
Member

A great improvement! I found some smaller things inline though.

A great improvement! I found some smaller things inline though.
@ -835,2 +828,4 @@
#ifdef __cplusplus
}
namespace blender::pbvh {
Member

We're in blenkernel here, so the full namespace should be blender::bke::pbvh

We're in blenkernel here, so the full namespace should be `blender::bke::pbvh`
JosephEagar marked this conversation as resolved
@ -1411,3 +1369,2 @@
PBVH *pbvh;
PBVHNode **nodes;
int totnode;
Vector<PBVHNode *> &nodes;
Member

Span<PBVHNode *> nodes here too I think. Not using a reference avoids the need to add the constructor there too

`Span<PBVHNode *> nodes` here too I think. Not using a reference avoids the need to add the constructor there too
@ -1521,3 +1480,3 @@
}
static void pbvh_faces_update_normals(PBVH *pbvh, PBVHNode **nodes, int totnode)
static void pbvh_faces_update_normals(PBVH *pbvh, Vector<PBVHNode *> &nodes)
Member

Vector<PBVHNode *> & -> Span<PBVHNode *>

`Vector<PBVHNode *> &` -> `Span<PBVHNode *>`
JosephEagar marked this conversation as resolved
@ -1583,3 +1542,3 @@
}
static void pbvh_update_mask_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
static void pbvh_update_mask_redraw(PBVH *pbvh, Vector<PBVHNode *> &nodes, int flag)
Member

Vector<PBVHNode *> & -> Span<PBVHNode *>

`Vector<PBVHNode *> &` -> `Span<PBVHNode *>`
@ -1619,3 +1578,3 @@
}
static void pbvh_update_visibility_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
static void pbvh_update_visibility_redraw(PBVH *pbvh, Vector<PBVHNode *> &nodes, int flag)
Member

Vector<PBVHNode *> & -> Span<PBVHNode *>

I won't bother commenting that anymore, it's the same elsewhere.

`Vector<PBVHNode *> &` -> `Span<PBVHNode *>` I won't bother commenting that anymore, it's the same elsewhere.
JosephEagar marked this conversation as resolved
@ -3193,3 +3122,2 @@
/* Update draw buffers. */
if (totnode != 0 && (update_flag & (PBVH_RebuildDrawBuffers | PBVH_UpdateDrawBuffers))) {
pbvh_update_draw_buffers(pbvh, nodes, totnode, update_flag);
if (nodes.size() != 0 && (update_flag & (PBVH_RebuildDrawBuffers | PBVH_UpdateDrawBuffers))) {
Member

nodes.size() != 0 -> !nodes.is_empty()

Might as well do this here

`nodes.size() != 0` -> `!nodes.is_empty()` Might as well do this here
@ -544,3 +543,2 @@
MEM_SAFE_FREE(sgcontext->operation);
MEM_SAFE_FREE(sgcontext->nodes);
MEM_SAFE_FREE(sgcontext);
MEM_delete<SculptGestureContext>(sgcontext);
Member

Writing <SculptGestureContext> should be unnecessary for MEM_delete, since the pointer already has that type.

Writing `<SculptGestureContext>` should be unnecessary for `MEM_delete`, since the pointer already has that type.
Author
Member

Thanks, I tend to have an irrational fear of template argument deduction.

Thanks, I tend to have an irrational fear of template argument deduction.
@ -624,1 +617,3 @@
&sgcontext->totnode);
sgcontext->nodes = blender::pbvh::search_gather(
ss->pbvh, BKE_pbvh_node_frustum_contain_AABB, &frustum)
.as_span();
Member

.as_span() is forcing a copy here. Without it, SculptGestureContext would be able to take ownership of the vector returned by search_gather. Same above.

`.as_span()` is forcing a copy here. Without it, `SculptGestureContext` would be able to take ownership of the vector returned by `search_gather`. Same above.
@ -3429,2 +3422,2 @@
sizeof(*accum) * totnode, __func__);
blender::threading::parallel_for(IndexRange(totnode), 1LL, [&](IndexRange range) {
sizeof(*accum) * nodes.size(), __func__);
blender::threading::parallel_for(IndexRange(nodes.size()), 1LL, [&](IndexRange range) {
Member

IndexRange(nodes.size()) -> nodes.index_range()

`IndexRange(nodes.size())` -> `nodes.index_range()`
JosephEagar marked this conversation as resolved
@ -6151,2 +6095,3 @@
nodes = blender::pbvh::search_gather(ss->pbvh, SCULPT_search_sphere_cb, &data);
if (totnode == 0) {
if (nodes.size() == 0) {
Member

nodes.size() == 0 -> nodes.is_empty()

`nodes.size() == 0` -> `nodes.is_empty()`
JosephEagar marked this conversation as resolved
@ -86,3 +87,3 @@
data.ignore_fully_ineffective = false;
data.center = ss->cache->initial_location;
BKE_pbvh_search_gather(ss->pbvh, SCULPT_search_sphere_cb, &data, &nodes, r_totnode);
nodes = blender::pbvh::search_gather(ss->pbvh, SCULPT_search_sphere_cb, &data);
Member

Maybe just return the nodes directly here, rather than assigning them to a variable and returning the variable after the switch

Maybe just return the nodes directly here, rather than assigning them to a variable and returning the variable after the switch
JosephEagar marked this conversation as resolved
@ -89,2 +86,3 @@
Vector<PBVHNode *> nodes = blender::pbvh::search_gather(ss->pbvh, nullptr, nullptr);
if (!totnodes) {
if (!nodes.size()) {
Member

!nodes.size() -> nodes.is_empty()

Same elsewhere in this diff

`!nodes.size()` -> `nodes.is_empty()` Same elsewhere in this diff
JosephEagar marked this conversation as resolved
Joseph Eagar added 1 commit 2023-04-13 20:37:06 +02:00
906ad96a27 temp-sculpt-vector-nodes: Make requested changes
* Rename blender::pbvh => blender::bke::pbvh
* !nodes.size() bool tests => nodes.is_empty().
* Moved a few more places to Span.
* Remove a few improper calls to Vector.as_span().
Joseph Eagar added 1 commit 2023-04-13 20:38:03 +02:00
Joseph Eagar added 1 commit 2023-04-13 21:37:33 +02:00
Joseph Eagar added 1 commit 2023-04-13 21:40:10 +02:00
Joseph Eagar added 1 commit 2023-04-13 21:42:53 +02:00
Hans Goudey approved these changes 2023-04-14 00:01:00 +02:00
Hans Goudey left a comment
Member

Accepting now, there are a few tiny things to clean up inline though.

Accepting now, there are a few tiny things to clean up inline though.
@ -1655,3 +1615,3 @@
}
void pbvh_update_BB_redraw(PBVH *pbvh, PBVHNode **nodes, int totnode, int flag)
void pbvh_update_BB_redraw(PBVH *pbvh, Span<PBVHNode *> nodes, int flag)
Member
/home/hans/blender-git/blender/source/blender/blenkernel/intern/pbvh.cc:1617:6: warning: no previous declaration for ‘void pbvh_update_BB_redraw(PBVH*, blender::Span<PBVHNode*>, int)’ [-Wmissing-declarations]
 1617 | void pbvh_update_BB_redraw(PBVH *pbvh, Span<PBVHNode *> nodes, int flag)
      |      ^~~~~~~~~~~~~~~~~~~~~
``` /home/hans/blender-git/blender/source/blender/blenkernel/intern/pbvh.cc:1617:6: warning: no previous declaration for ‘void pbvh_update_BB_redraw(PBVH*, blender::Span<PBVHNode*>, int)’ [-Wmissing-declarations] 1617 | void pbvh_update_BB_redraw(PBVH *pbvh, Span<PBVHNode *> nodes, int flag) | ^~~~~~~~~~~~~~~~~~~~~ ```
@ -3957,2 +3913,3 @@
Vector<PBVHNode *> nodes = blender::bke::pbvh::gather_proxies(ss->pbvh);
threading::parallel_for(IndexRange(totnode), 1, [&](IndexRange range) {
threading::parallel_for(IndexRange(nodes.size()), 1, [&](IndexRange range) {
Member

IndexRange(nodes.size()) -> nodes.index_range()

Three of these in this file.

`IndexRange(nodes.size())` -> `nodes.index_range()` Three of these in this file.
@ -13,6 +13,7 @@
#include "BLI_math.h"
#include "BLI_task.h"
#include "BLI_utildefines.h"
#include "BLI_span.hh"
Member

make format

`make format`
@ -273,2 +272,2 @@
BKE_pbvh_parallel_range_settings(&settings, true, ss->filter_cache->totnode);
BLI_task_parallel_range(0, ss->filter_cache->totnode, &data, sculpt_expand_task_cb, &settings);
BKE_pbvh_parallel_range_settings(&settings, true, ss->filter_cache->nodes.size());
BLI_task_parallel_range(0, ss->filter_cache->nodes.size(), &data, sculpt_expand_task_cb, &settings);
Member

make format

`make format`
Joseph Eagar added 2 commits 2023-04-14 20:05:29 +02:00
Joseph Eagar added 1 commit 2023-04-14 20:59:59 +02:00
Joseph Eagar merged commit b86fc55d30 into main 2023-04-14 21:16:53 +02:00
Joseph Eagar deleted branch temp-sculpt-vector-nodes 2023-04-14 21:16:53 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#106884
No description provided.