Mesh: Share normals caches by splitting vertex and face calculation #110479

Merged
Hans Goudey merged 14 commits from HooglyBoogly/blender:mesh-vert-face-normals-split into main 2023-08-25 23:06:12 +02:00
Member

Since vertex and face normals can be calculated separately, it simplifies
things to further separate the two caches. This makes it easier to use
SharedCache to avoid recalculating normals when copying meshes.

Sharing vertex normal caches with meshes with the same positions and
topology allows completely skipping recomputation as meshes are
copied. The effects are similar to e8f4010611, but normals are much
more expensive, so the benefit is larger.

In a simple test changing a large grid's generic attribute with geometry
nodes, I observed a performance improvement from 12 to 17 FPS.
Most real world situations will have smaller changes though.

Completely splitting face and vertex calculation is slightly slower
when face normals aren't already calculated, so I kept the option
to recalculate them together as well.

This simplifies investigating the changes in #105920 which resolve
non-determinism in the vertex normal calculation. If we can make the
topology map creation fast enough, that might allow simplifying this
code more in the future.

Since vertex and face normals can be calculated separately, it simplifies things to further separate the two caches. This makes it easier to use `SharedCache` to avoid recalculating normals when copying meshes. Sharing vertex normal caches with meshes with the same positions and topology allows completely skipping recomputation as meshes are copied. The effects are similar to e8f4010611e7, but normals are much more expensive, so the benefit is larger. In a simple test changing a large grid's generic attribute with geometry nodes, I observed a performance improvement from 12 to 17 FPS. Most real world situations will have smaller changes though. Completely splitting face and vertex calculation is slightly slower when face normals aren't already calculated, so I kept the option to recalculate them together as well. This simplifies investigating the changes in #105920 which resolve non-determinism in the vertex normal calculation. If we can make the topology map creation fast enough, that might allow simplifying this code more in the future.
Hans Goudey force-pushed mesh-vert-face-normals-split from 5820eb501c to 1b5f7de7c2 2023-07-27 14:14:10 +02:00 Compare
Hans Goudey added 2 commits 2023-08-12 07:50:08 +02:00
Hans Goudey changed title from Mesh: Split vertex and face normal calculation to Mesh: Share normals caches by splitting vertex and face calculation 2023-08-12 07:50:19 +02:00
Member

Generally seems fine, but would of course be fine if we could avoid the slowdown if possible. Right now, it's not clear to me if it's at all possible to have no slowdown while still making normal calculation deterministic like you did in #105920. It would be good if you could describe a bit better why there is non-determinism in the first place and how it impacts other parts of Blender.

Ignoring the non-determinism issue, it seems like it might be possible to keep the same performance while still having two separate shared caches, at the cost of a bit more complexity. This could be achieved by calling face_normals_cache.ensure directly from Mesh::vert_normals(). Then one could have a specialized implementation that computes the vertex and face normals at the same time if the face normals are not available yet.

Generally seems fine, but would of course be fine if we could avoid the slowdown if possible. Right now, it's not clear to me if it's at all possible to have no slowdown while still making normal calculation deterministic like you did in #105920. It would be good if you could describe a bit better why there is non-determinism in the first place and how it impacts other parts of Blender. Ignoring the non-determinism issue, it seems like it might be possible to keep the same performance while still having two separate shared caches, at the cost of a bit more complexity. This could be achieved by calling `face_normals_cache.ensure` directly from `Mesh::vert_normals()`. Then one could have a specialized implementation that computes the vertex and face normals at the same time if the face normals are not available yet.

The changes seem fine but it would be good if the slowdown could be avoided for the common code path for animation playback.

Could there be a function that calculates vertex & face normals at once which is called from BKE_mesh_ensure_normals_for_display for e.g.


From my own tests this can slow down animation playback ~6%, tested with a single modifier an 2.5million quads.
While not a large amount, it's noticeable and would be good to avoid if possible.

The changes seem fine but it would be good if the slowdown could be avoided for the common code path for animation playback. Could there be a function that calculates vertex & face normals at once which is called from `BKE_mesh_ensure_normals_for_display` for e.g. --- From my own tests this can slow down animation playback ~6%, tested with a single modifier an 2.5million quads. While not a large amount, it's noticeable and would be good to avoid if possible.
Hans Goudey added 5 commits 2023-08-24 19:18:04 +02:00
Author
Member

Thanks for the comments! I added back the code calculating both vertex and face normals together, in addition to the option of separating them. I think this is now ready, though I'll run some more performance tests on my desktop when I'm back home, just to get some "X times faster" numbers.

Thanks for the comments! I added back the code calculating both vertex and face normals together, in addition to the option of separating them. I think this is now ready, though I'll run some more performance tests on my desktop when I'm back home, just to get some "X times faster" numbers.
Hans Goudey requested review from Campbell Barton 2023-08-24 19:24:01 +02:00
Hans Goudey requested review from Jacques Lucke 2023-08-24 19:24:02 +02:00
Hans Goudey added this to the 4.0 milestone 2023-08-24 19:24:08 +02:00
Campbell Barton approved these changes 2023-08-25 12:08:19 +02:00
Campbell Barton left a comment
Owner

Re-tested, if anything this is a little faster now (~1-3%).
Some notes inline but nothing blocking.

Re-tested, if anything this is a little faster now (~1-3%). Some notes inline but nothing blocking.
@ -83,3 +81,1 @@
Span<int> corner_verts,
MutableSpan<float3> face_normals,
MutableSpan<float3> vert_normals);
void normals_calc_verts(Span<float3> vert_positions,

Worth adding a note about normals_calc_faces_verts in the doc-string as it may be preferred.

Worth adding a note about `normals_calc_faces_verts` in the doc-string as it may be preferred.
HooglyBoogly marked this conversation as resolved
@ -289,3 +275,1 @@
}
});
}
static void normals_calc_faces_verts(const Span<float3> positions,

suggestion, but this reads a bit like face-verts which is sometimes used for faces-of-verts: I find this makes it clearer that the function is calculating both the values at once: normals_calc_faces_and_verts.
(this knit-pick applies to the old function too).

*suggestion*, but this reads a bit like face-verts which is sometimes used for faces-of-verts: I find this makes it clearer that the function is calculating both the values at once: `normals_calc_faces_and_verts`. (this knit-pick applies to the old function too).
HooglyBoogly marked this conversation as resolved
Jacques Lucke approved these changes 2023-08-25 12:48:37 +02:00
Hans Goudey added 3 commits 2023-08-25 16:25:57 +02:00
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey added 1 commit 2023-08-25 22:28:07 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
fd473c0ac7
Fix tests
`face_normal_calc` gives different results than `normal_calc_ngon` which
is used by the existing vertex normal calculation. That can be looked
into separately. Until then, switch to the latter function.
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey merged commit 383a145a19 into main 2023-08-25 23:06:12 +02:00
Hans Goudey deleted branch mesh-vert-face-normals-split 2023-08-25 23:06:14 +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#110479
No description provided.