Sculpt: Use C++ Set to store PBVH Node BMesh elements #113907

Merged
Hans Goudey merged 9 commits from HooglyBoogly/blender:cleanup-pbvh-bmesh-set into main 2023-10-19 14:18:24 +02:00
Member

Mainly to simplify code and also add some add type safety, replace
GSet with blender::Set for the storage of BMesh triangles and
vertices on each PBVH node. Some initial tests point to better
performance too, but the numbers are hard to verify so far.

Because of the larger PBVHNode, memory usage slightly increases
(observed a 2% increase with a 1M face grid) for regular Mesh sculpting,
but it seems Set is more memory efficient than GSet, because I also
observed a 10% decrease in memory usage for dynamic topology.
In the future nodes can be split in a more data-oriented fashion to
reduce memory usage overall.

This also makes it simpler to switch to another type in the future.

Mainly to simplify code and also add some add type safety, replace `GSet` with `blender::Set` for the storage of BMesh triangles and vertices on each PBVH node. Some initial tests point to better performance too, but the numbers are hard to verify so far. Because of the larger `PBVHNode`, memory usage slightly increases (observed a 2% increase with a 1M face grid) for regular Mesh sculpting, but it seems `Set` is more memory efficient than `GSet`, because I also observed a 10% decrease in memory usage for dynamic topology. In the future nodes can be split in a more data-oriented fashion to reduce memory usage overall. This also makes it simpler to switch to another type in the future.
Hans Goudey added 1 commit 2023-10-18 20:12:42 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
2bdb3b1ce5
Cleanup: Use C++ Set to store BMesh faces
Mainly to simplify code and also add some add type safety, replace
`GSet` with `blender::Set` for the storage of BMesh triangles on
each PBVH node. Faces aren't used that much in sculpt code so the code
change is relatively simple.
Hans Goudey changed title from Cleanup: Use C++ Set to store BMesh faces to Cleanup: Use C++ Set to store PBVH Node BMesh faces 2023-10-18 20:14:26 +02:00
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey changed title from Cleanup: Use C++ Set to store PBVH Node BMesh faces to Cleanup: Use C++ Set to store PBVH Node BMesh elements 2023-10-18 23:23:00 +02:00
Hans Goudey added 2 commits 2023-10-18 23:32:44 +02:00
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey requested review from Sergey Sharybin 2023-10-18 23:33:40 +02:00
Hans Goudey added this to the Sculpt, Paint & Texture project 2023-10-18 23:33:53 +02:00
Hans Goudey added the
Module
Sculpt, Paint & Texture
label 2023-10-18 23:34:00 +02:00
Hans Goudey changed title from Cleanup: Use C++ Set to store PBVH Node BMesh elements to Sculpt: Use C++ Set to store PBVH Node BMesh elements 2023-10-18 23:34:13 +02:00
Jesse Yurkovich reviewed 2023-10-19 00:25:07 +02:00
@ -114,3 +117,1 @@
GSet *bm_faces = nullptr;
GSet *bm_unique_verts = nullptr;
GSet *bm_other_verts = nullptr;
blender::Set<BMFace *> bm_faces;

Storing the blender::Set directly like this increases the size of PBVHNode from 272 to 608 bytes (each set is 120). Is that alright?

Storing the blender::Set directly like this increases the size of `PBVHNode` from 272 to 608 bytes (each set is 120). Is that alright?

Would be nice to double-check on some production high-poky mesh.

AFAIK, the nodes are quite "big" in the terms of amount of geometry they hold, so intuitively I would not consider it a worrysome increase for the files.

In the future we'd need to look into making nodes much smaller, in both memory foorptintm, but also in terms of amount of geometry per leaf node.

That being said, maybe it would be better to do std::unique_ptr<blender::Set> so that the increase of memory footprint only happen for BMesh case (where a lot more of memory will be allocated on heap for GSet anyway) ?

Would be nice to double-check on some production high-poky mesh. AFAIK, the nodes are quite "big" in the terms of amount of geometry they hold, so intuitively I would not consider it a worrysome increase for the files. In the future we'd need to look into making nodes much smaller, in both memory foorptintm, but also in terms of amount of geometry per leaf node. That being said, maybe it would be better to do `std::unique_ptr<blender::Set>` so that the increase of memory footprint only happen for BMesh case (where a lot more of memory will be allocated on heap for GSet anyway) ?
Author
Member

Using a zero inline buffer size brought the size per set down to 64 bytes. With the total PBVHNode size now at 416. That's still not a great increase, but at leas the size isn't double anymore.

My hope for PBVH leaf nodes it to separate them into separate structs for each different PBVH type. That way the size of these sets wouldn't influence the size of nodes for multires or Mesh sculpting. I'd also separate things like PBVHColorBufferNode and PBVHPixelsNode so we don't always have to store them. Non-leaf nodes wouldn't store any of this data at all.

If it helps I can make a design/todo task about that.

For dynamic topology, I think storing the sets directly is nice and will help reduce indirection.

Using a zero inline buffer size brought the size per set down to 64 bytes. With the total `PBVHNode` size now at 416. That's still not a great increase, but at leas the size isn't double anymore. My hope for PBVH leaf nodes it to separate them into separate structs for each different PBVH type. That way the size of these sets wouldn't influence the size of nodes for multires or Mesh sculpting. I'd also separate things like `PBVHColorBufferNode` and `PBVHPixelsNode` so we don't always have to store them. Non-leaf nodes wouldn't store any of this data at all. If it helps I can make a design/todo task about that. For dynamic topology, I think storing the sets directly is nice and will help reduce indirection.

That's cool improvement!

Still think it would be nice to get some numbers for memory consumption before/after.

To give you some background. This is 50% increase of node size. Increasing BVH2 node size in used in Cycles will lead to around 30% (give of take) overall memory usage for scenes with complex geometry and procedural materials. On a typical production file it will be 10-15% hit. Is not something we should be accepting that easily.

As I've said before, I don't really expect it to be that big of a problem for the PBVH, but it would be very nice to see some number to confirm our expectations. Should be an easy test to do: subdivide cube to 1M faces, enabled dyntopo, compare total memory usage before/after patch.

That's cool improvement! Still think it would be nice to get some numbers for memory consumption before/after. To give you some background. This is 50% increase of node size. Increasing BVH2 node size in used in Cycles will lead to around 30% (give of take) overall memory usage for scenes with complex geometry and procedural materials. On a typical production file it will be 10-15% hit. Is not something we should be accepting that easily. As I've said before, I don't really expect it to be that big of a problem for the PBVH, but it would be very nice to see some number to confirm our expectations. Should be an easy test to do: subdivide cube to 1M faces, enabled dyntopo, compare total memory usage before/after patch.
Author
Member

I did some basic testing with a 1 million face grid.

Before: Dyntopo: 1.84 GB  Mesh: 367 MB
After:  Dyntopo: 1.67 GB  Mesh: 374 MB

Conclusion: 2% memory usage increase for mesh sculpting, 10% less memory usage in dynamic topology. The idea of splitting the PBVHNode struct in the future is enough to make this seem worthwhile currently IMO.

I did some basic testing with a 1 million face grid. ``` Before: Dyntopo: 1.84 GB Mesh: 367 MB After: Dyntopo: 1.67 GB Mesh: 374 MB ``` Conclusion: 2% memory usage increase for mesh sculpting, 10% less memory usage in dynamic topology. The idea of splitting the `PBVHNode` struct in the future is enough to make this seem worthwhile currently IMO.

Think 2% is a fine trade-off.

Think 2% is a fine trade-off.
Jesse Yurkovich reviewed 2023-10-19 00:25:24 +02:00
@ -230,3 +227,3 @@
}
static void partialvis_update_bmesh_faces(GSet *faces)
static void partialvis_update_bmesh_faces(const blender::Set<BMFace *> faces)

Pass by reference here?

Pass by reference here?
HooglyBoogly marked this conversation as resolved
Sergey Sharybin reviewed 2023-10-19 10:01:53 +02:00
@ -522,0 +520,4 @@
std::optional<blender::Set<BMVert *>::Iterator> bm_unique_verts;
std::optional<blender::Set<BMVert *>::Iterator> bm_unique_verts_end;
std::optional<blender::Set<BMVert *>::Iterator> bm_other_verts;
std::optional<blender::Set<BMVert *>::Iterator> bm_other_verts_end;

Think we need to add #include <optional> at the top of the file. We shouldn't rely on indirectly included headers.

Think we need to add `#include <optional>` at the top of the file. We shouldn't rely on indirectly included headers.
HooglyBoogly marked this conversation as resolved
Hans Goudey added 4 commits 2023-10-19 10:18:29 +02:00
Hans Goudey added 2 commits 2023-10-19 10:42:24 +02:00
Sergey Sharybin approved these changes 2023-10-19 10:59:01 +02:00
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey merged commit 40080f618c into main 2023-10-19 14:18:24 +02:00
Hans Goudey deleted branch cleanup-pbvh-bmesh-set 2023-10-19 14:18:26 +02:00
Sign in to join this conversation.
No reviewers
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 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#113907
No description provided.