Sculpt: Improve mesh normals update performance #116209

Merged
Hans Goudey merged 14 commits from HooglyBoogly/blender:pbvh-mesh-normals-full-node into main 2024-01-08 18:49:36 +01:00
Member

Instead of storing a boolean "update tag" for every vertex, just
recalculate the normals of all the faces and vertices in affected PBVH
nodes. This avoids the overhead of tracking position updates at the
vertex level, and simplifies the normal calculation, since we can
just calculate values for all the normals in a node.

The main way we gain performance is avoiding building a VectorSet
for all vertices and faces that need updates in the entire mesh. That
process had to be single threaded, and was a large bottleneck when many
vertices were affected at a time.

That VectorSet was necessary for thread safety deduplication of work
though, because neighboring nodes can't calculate the normals of the
same faces or vertices at the same time. (Normals need to be calculated
for all faces connected to moved vertices and all vertices connected to
those faces). In this PR, we only build a set of the boundary faces
and vertices. We calculate those in a separate step, which duplicates
work from the non-boundary calculations, but at least it's threadsafe.

I measured normal recalculation timing when sculpting on a 16 million
vertex mesh. The removal of the vert_bitmap array also saves 16 MB
of memory.

Nodes Affected Vertices Before (ms) After (ms)
4 15625 0.208 0.304
35 136281 2.98 0.988
117 457156 15.0 3.21
2455 9583782 566 84.0
Instead of storing a boolean "update tag" for every vertex, just recalculate the normals of all the faces and vertices in affected PBVH nodes. This avoids the overhead of tracking position updates at the vertex level, and simplifies the normal calculation, since we can just calculate values for all the normals in a node. The main way we gain performance is avoiding building a `VectorSet` for all vertices and faces that need updates in the entire mesh. That process had to be single threaded, and was a large bottleneck when many vertices were affected at a time. That `VectorSet` was necessary for thread safety deduplication of work though, because neighboring nodes can't calculate the normals of the same faces or vertices at the same time. (Normals need to be calculated for all faces connected to moved vertices and all vertices connected to those faces). In this PR, we only build a set of the *boundary* faces and vertices. We calculate those in a separate step, which duplicates work from the non-boundary calculations, but at least it's threadsafe. I measured normal recalculation timing when sculpting on a 16 million vertex mesh. The removal of the `vert_bitmap` array also saves 16 MB of memory. | Nodes | Affected Vertices | Before (ms) | After (ms) | | ----- | ------------ | ----------- | ---------- | | 4 | 15625 | 0.208 | 0.304 | | 35 | 136281 | 2.98 | 0.988 | | 117 | 457156 | 15.0 | 3.21 | | 2455 | 9583782 | 566 | 84.0 |
Hans Goudey added 5 commits 2023-12-14 23:29:51 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
4f97ccd6c9
Fixes
Hans Goudey added this to the Sculpt, Paint & Texture project 2023-12-14 23:30:16 +01:00
Hans Goudey added the
Module
Sculpt, Paint & Texture
Interest
Performance
labels 2023-12-14 23:30:54 +01:00
Hans Goudey requested review from Sergey Sharybin 2023-12-14 23:32:12 +01:00
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey added 1 commit 2023-12-14 23:35:05 +01:00
Hans Goudey added 1 commit 2023-12-16 03:24:40 +01:00
Hans Goudey added 1 commit 2023-12-18 17:46:04 +01:00
Hans Goudey added 1 commit 2023-12-29 18:04:51 +01:00

That is quite a speedup shown in the table!

I have few questions:

  • You mention you've measured performance on 16M vertex mesh, but the table contains rows with the different number of nodes. and the different number of unique verts. So what is the variable there? Are those different meshes, or did you vary the node size, or something else?
  • What is the time shown in the table? Is it overall stroke time? Brush callback on a node time? PBVH normals update time?

Thinking a bit into the future, when we'll hopefully have smaller nodes: would it be benefitial to limit updates to modified nodes only? Surely doing per-vertex tracking is overkill which leads to slowdown, but maybe tagging modified nodes and limiting normals update to them is something we'd want to have in the long term? If an artist is zoomed into into some detail of a mesh it seems a bit wasteful to re-calculate all normals while they are only modifying vertices in a handful of nodes. What are your thoughts about it?

That is quite a speedup shown in the table! I have few questions: - You mention you've measured performance on 16M vertex mesh, but the table contains rows with the different number of nodes. and the different number of unique verts. So what is the variable there? Are those different meshes, or did you vary the node size, or something else? - What is the time shown in the table? Is it overall stroke time? Brush callback on a node time? PBVH normals update time? Thinking a bit into the future, when we'll hopefully have smaller nodes: would it be benefitial to limit updates to modified nodes only? Surely doing per-vertex tracking is overkill which leads to slowdown, but maybe tagging modified nodes and limiting normals update to them is something we'd want to have in the long term? If an artist is zoomed into into some detail of a mesh it seems a bit wasteful to re-calculate all normals while they are only modifying vertices in a handful of nodes. What are your thoughts about it?
Hans Goudey added 1 commit 2024-01-04 21:59:26 +01:00
Author
Member
  • You mention you've measured performance on 16M vertex mesh, but the table contains rows with the different number of nodes. and the different number of unique verts. So what is the variable there?

Right, my bad, the description is confusing. It's the number of vertices affected by the brush stroke (depending on the brush size) in that 16 million vertex mesh.

  • What is the time shown in the table? Is it overall stroke time? Brush callback on a node time? PBVH normals update time?

It's only the normals update time.

Thinking a bit into the future, when we'll hopefully have smaller nodes: would it be benefitial to limit updates to modified nodes only?

Maybe I was misleading-- only normals of affected nodes are recalculated. That's handled by the existing BKE_pbvh_node_mark_update calls.

More generally though, this is still the main trouble with this code. Changing vertex normals also changes surrounding face normals, which changes the normals of all vertices connected to those faces. So the set of vertices that need recalculated normals can propagate into unchanged/untagged PBVH nodes. It's the best solution I have now, but I don't really like the vert_to_face_map and VectorSet combination. I think it will continue to give us performance problems in the future, and it won't scale as well with smaller nodes, since the proportion of boundary vertices will increase.

> - You mention you've measured performance on 16M vertex mesh, but the table contains rows with the different number of nodes. and the different number of unique verts. So what is the variable there? Right, my bad, the description is confusing. It's the number of vertices affected by the brush stroke (depending on the brush size) in that 16 million vertex mesh. > - What is the time shown in the table? Is it overall stroke time? Brush callback on a node time? PBVH normals update time? It's only the normals update time. > Thinking a bit into the future, when we'll hopefully have smaller nodes: would it be benefitial to limit updates to modified nodes only? Maybe I was misleading-- only normals of affected nodes are recalculated. That's handled by the existing `BKE_pbvh_node_mark_update` calls. More generally though, this is still the main trouble with this code. Changing vertex normals also changes surrounding face normals, which changes the normals of all vertices connected to those faces. So the set of vertices that need recalculated normals can propagate into unchanged/untagged PBVH nodes. It's the best solution I have now, but I don't really like the `vert_to_face_map` and `VectorSet` combination. I think it will continue to give us performance problems in the future, and it won't scale as well with smaller nodes, since the proportion of boundary vertices will increase.
Hans Goudey added 1 commit 2024-01-05 16:40:12 +01:00
Hans Goudey changed title from PBVH: Improve mesh normals update performance to Sculpt: Improve mesh normals update performance 2024-01-07 20:20:39 +01:00

Ah, the comment makes it more clear and aligns better with what I see in the code. Maybe tweak the description "in entire PBVH nodes" -> "in entire affected PBVH nodes" ?

Didn't have time to apply the patch and verify it, would trust your testing on this. Unless you explicitly want extra eyes for testing..

Ah, the comment makes it more clear and aligns better with what I see in the code. Maybe tweak the description "in entire PBVH nodes" -> "in entire affected PBVH nodes" ? Didn't have time to apply the patch and verify it, would trust your testing on this. Unless you explicitly want extra eyes for testing..
Author
Member

Ah, the comment makes it more clear and aligns better with what I see in the code. Maybe tweak the description "in entire PBVH nodes" -> "in entire affected PBVH nodes" ?

Good point, I went with "all the faces and vertices in affected PBVH nodes".

Didn't have time to apply the patch and verify it, would trust your testing on this. Unless you explicitly want extra eyes for testing..

I'll double check again before committing. But yeah, I've tested this a few times by now.

> Ah, the comment makes it more clear and aligns better with what I see in the code. Maybe tweak the description "in entire PBVH nodes" -> "in entire affected PBVH nodes" ? Good point, I went with "all the faces and vertices in affected PBVH nodes". > Didn't have time to apply the patch and verify it, would trust your testing on this. Unless you explicitly want extra eyes for testing.. I'll double check again before committing. But yeah, I've tested this a few times by now.
Sergey Sharybin approved these changes 2024-01-08 18:26:58 +01:00
Hans Goudey added 1 commit 2024-01-08 18:27:56 +01:00
Hans Goudey added 1 commit 2024-01-08 18:44:05 +01:00
Hans Goudey added 1 commit 2024-01-08 18:46:29 +01:00
Hans Goudey merged commit aaa25bc882 into main 2024-01-08 18:49:36 +01:00
Hans Goudey deleted branch pbvh-mesh-normals-full-node 2024-01-08 18:49:39 +01: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
2 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#116209
No description provided.