Cleanup: Sculpt: Avoid unnecessarily changing vertex normals #111470

Merged
Hans Goudey merged 3 commits from HooglyBoogly/blender:remove-sculpt-normal-reference into main 2023-08-24 16:48:02 +02:00
Member

Keeping a mutable reference to vertex normals for the entire lifetime
of the PBVH structure makes caching the normals and sharing the cache
harder than it should be. Generally code is safer when we reduce the
number of mutable references to an object.

Currently the normals are modified in two places. First is the sculpt
mesh normal recalculation. There we can just retrieve the normals from
the mesh each time. Second is the restore from an undo step. That is
unnecessary because the normals are marked for recalculation anyway.
It doesn't even make much sense to store the normals in an undo step
when we can easily recalculate them based on new positions.

This change helps with #110479. These were also the last place that
kept a mutable reference to normals. I tested undo and redo after
sculpting, and it works well for each PBVH type.

Keeping a mutable reference to vertex normals for the entire lifetime of the PBVH structure makes caching the normals and sharing the cache harder than it should be. Generally code is safer when we reduce the number of mutable references to an object. Currently the normals are modified in two places. First is the sculpt mesh normal recalculation. There we can just retrieve the normals from the mesh each time. Second is the restore from an undo step. That is unnecessary because the normals are marked for recalculation anyway. It doesn't even make much sense to store the normals in an undo step when we can easily recalculate them based on new positions. This change helps with #110479. These were also the last place that kept a mutable reference to normals. I tested undo and redo after sculpting, and it works well for each PBVH type.
Hans Goudey added 1 commit 2023-08-24 14:38:37 +02:00
9f25542080 Cleanup: Sculpt: Avoid unnecessarily changing vertex normals
Keeping a mutable reference to vertex normals for the entire lifetime
of the PBVH structure makes caching the normals and sharing the cache
harder than it should be. Generally code is safer when we reduce the
number of mutable references to an object.

Currently the normals are modified in two places. First is the sculpt
mesh normal recalculation. There we can just retrieve the normals from
the mesh each time. Second is the restore from an undo step. That is
unnecessary because the normals are marked for recalculation anyway.
It doesn't even make much sense to store the normals in an undo step
when we can easily recalculate them based on new positions anyway.

This change helps with #110479. These were also the last place that
kept a mutable reference to normals. I tested undo and redo after
sculpting, and it works well for each PBVH type.
Hans Goudey requested review from Sergey Sharybin 2023-08-24 14:38:51 +02:00
Hans Goudey requested review from Jacques Lucke 2023-08-24 14:38:51 +02:00

That is unnecessary because the normals are marked for recalculation anyway.

I can only clearly see this done when sculpting operates on a base mesh.
It is not really clear to me what ensures validity of normals after undo for multires and dynamic topology sculpting.

This part needs to be clarified, as locally the proposed state of the undo code seems wrong.

> That is unnecessary because the normals are marked for recalculation anyway. I can only clearly see this done when sculpting operates on a base mesh. It is not really clear to me what ensures validity of normals after undo for multires and dynamic topology sculpting. This part needs to be clarified, as locally the proposed state of the undo code seems wrong.
Author
Member

Dynamic topology and multires sculpting just update the normals of entire PBVH nodes (see BKE_pbvh_update_normals), so in this case the call to BKE_pbvh_node_mark_update is enough to recalculate all the necessary normals.

Dynamic topology and multires sculpting just update the normals of entire PBVH nodes (see `BKE_pbvh_update_normals`), so in this case the call to `BKE_pbvh_node_mark_update` is enough to recalculate all the necessary normals.

Ah, indeed, I see it now. Thanks for clarifying.

The remaining confusion from my side is the assignment of pbvh->vert_normals in the normals_update function. Why is it needed now?

From testing and rest of the code it seems all fine though.

Ah, indeed, I see it now. Thanks for clarifying. The remaining confusion from my side is the assignment of pbvh->vert_normals in the normals_update function. Why is it needed now? From testing and rest of the code it seems all fine though.
Author
Member

The remaining confusion from my side is the assignment of pbvh->vert_normals in the normals_update function. Why is it needed now?

Good question! I was thinking of reassigning the data from the runtime vector, but it shouldn't be reallocated here, so that's unnecessary, I'll remove it.

Thanks for the quick review!

>The remaining confusion from my side is the assignment of pbvh->vert_normals in the normals_update function. Why is it needed now? Good question! I was thinking of reassigning the data from the runtime vector, but it shouldn't be reallocated here, so that's unnecessary, I'll remove it. Thanks for the quick review!
Hans Goudey added 2 commits 2023-08-24 15:57:24 +02:00
Sergey Sharybin approved these changes 2023-08-24 15:58:22 +02:00
Hans Goudey merged commit 111e586424 into main 2023-08-24 16:48:02 +02:00
Hans Goudey deleted branch remove-sculpt-normal-reference 2023-08-24 16:48:04 +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
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#111470
No description provided.