Sculpt: Fix #107123: New normals impl. for PBVH_FACES #107456

Closed
Joseph Eagar wants to merge 5 commits from temp-pbvh-normals into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This patch rewrites PBVH_FACE's normal update code. The old code worked by calculating normals in loop tris and then adding them to vertices, which were later normalized; unfortunately this is incorrect, the first topological ring surrounding vertices needs to be updated too. This is not possible with the existing code which operates on a list of PBVHNodes. We can't add more vertices to the update set without knowing which nodes they belong to.

The new code works a bit differently:

  • Updates poly normals as well as vertices.
  • Normals are accumulated at each vertex from surrounding polys via pbvh->pmap, instead of directly propagating normals from loop tris to verts.
  • pbvh->pmap is now always created, the need_pmap (along with need_mask) arguments to BKE_sculpt_update_object_for_edit will be removed in a separate cleanup commit.
  • The C++ task API is used for (much greater) code clarity.
  • There is some use of atomics which I'm not sure is necessary (at least for x86).
This patch rewrites PBVH_FACE's normal update code. The old code worked by calculating normals in loop tris and then adding them to vertices, which were later normalized; unfortunately this is incorrect, the first topological ring surrounding vertices needs to be updated too. This is not possible with the existing code which operates on a list of `PBVHNode`s. We can't add more vertices to the update set without knowing which nodes they belong to. The new code works a bit differently: * Updates poly normals as well as vertices. * Normals are accumulated at each vertex from surrounding polys via `pbvh->pmap`, instead of directly propagating normals from loop tris to verts. * `pbvh->pmap` is now always created, the `need_pmap` (along with `need_mask`) arguments to `BKE_sculpt_update_object_for_edit` will be removed in a separate cleanup commit. * The C++ task API is used for (much greater) code clarity. * There is some use of atomics which I'm not sure is necessary (at least for x86).
Joseph Eagar added 1 commit 2023-04-29 02:11:57 +02:00
Joseph Eagar requested review from Hans Goudey 2023-04-29 02:12:34 +02:00
Joseph Eagar added 1 commit 2023-04-29 02:17:32 +02:00
Iliya Katushenock reviewed 2023-04-29 10:25:38 +02:00
@ -1446,0 +1417,4 @@
float(*vert_positions)[3] = pbvh->vert_positions;
Vector<int> verts, tris; /* Note: these two vectors can contain duplicates. */
for (int i = 0; i < task_update_verts.size(); i++) {
for (const Span<int> verts : task_update_verts)

Same below.

```Cpp for (const Span<int> verts : task_update_verts) ``` Same below.
Iliya Katushenock reviewed 2023-04-29 10:25:40 +02:00
@ -1446,0 +1415,4 @@
/* Don't bother de-duplicating. */
float(*vert_positions)[3] = pbvh->vert_positions;
Vector<int> verts, tris; /* Note: these two vectors can contain duplicates. */

If each vector in task_update_verts contain at least one element.

verts.ensure(task_update_verts.size());

Same for tris

If each vector in `task_update_verts` contain at least one element. ```Cpp verts.ensure(task_update_verts.size()); ``` Same for `tris`
Author
Member

That assumption doesn't really hold, and I don't think using the nodes size as a heuristic really works. You'd have to multiply it by some factor (a percentage of the leaf limit most likely) which would have to be tested across a range of conditions.

That assumption doesn't really hold, and I don't think using the nodes size as a heuristic really works. You'd have to multiply it by some factor (a percentage of the leaf limit most likely) which would have to be tested across a range of conditions.

This heuristic provides some improvement, but yes, without a real calculation, you can't allocate all the memory. But if it's 100 tasks with 3 verts, you'll be allocating 201, not 300 times.
But yes, this is not some real implementation requirement, but just a weak suggestion, you can close it if you don't think it's really useful

This heuristic provides some improvement, but yes, without a real calculation, you can't allocate all the memory. But if it's 100 tasks with 3 verts, you'll be allocating 201, not 300 times. But yes, this is not some real implementation requirement, but just a weak suggestion, you can close it if you don't think it's really useful
Iliya Katushenock reviewed 2023-04-29 10:25:42 +02:00
@ -1400,2 +1389,2 @@
{
PBVHUpdateData *data = static_cast<PBVHUpdateData *>(userdata);
task_update_verts.resize(nodes.size());
task_update_tris.resize(nodes.size());
Vector<Vector<int>> task_update_verts;
Vector<Vector<int>> task_update_tris;

task_update_verts.resize(nodes.size());
task_update_tris.resize(nodes.size());

->

Array<Vector<int>> task_update_verts(nodes.size());
Array<Vector<int>> task_update_tris(nodes.size());

```Cpp Vector<Vector<int>> task_update_verts; Vector<Vector<int>> task_update_tris; task_update_verts.resize(nodes.size()); task_update_tris.resize(nodes.size()); ``` -> ```Cpp Array<Vector<int>> task_update_verts(nodes.size()); Array<Vector<int>> task_update_tris(nodes.size()); ```
Iliya Katushenock reviewed 2023-04-29 10:26:01 +02:00
@ -1376,3 +1381,1 @@
static void pbvh_update_normals_clear_task_cb(void *__restrict userdata,
const int n,
const TaskParallelTLS *__restrict /*tls*/)
static void pbvh_faces_update_normals(PBVH *pbvh, Span<PBVHNode *> nodes)

const Span<PBVHNode *> nodes

`const Span<PBVHNode *> nodes`
Author
Member

Shouldn't that be Span<const PBVHNode*>?

Shouldn't that be `Span<const PBVHNode*>`?

The container itself is constant, since you don't change it by value (for example, swap between 2 spans). Its nodes are not contants, so how can you get a Span<const Node> from a Vector<Node>? (and you change nodes in code as i can see, not sure why isn't mutable span now)

The container itself is constant, since you don't change it by value (for example, swap between 2 spans). Its nodes are not contants, so how can you get a `Span<const Node>` from a `Vector<Node>`? (and you change nodes in code as i can see, not sure why isn't mutable span now)
Joseph Eagar added 2 commits 2023-04-29 11:35:31 +02:00
Joseph Eagar added 1 commit 2023-04-29 11:35:52 +02:00
Member

I submitted an alternative fix in #107458, which is more similar to mesh normal calculation and makes more use of the vert -> poly map, and recalculates the entire poly normal at once.

I submitted an alternative fix in #107458, which is more similar to mesh normal calculation and makes more use of the vert -> poly map, and recalculates the entire poly normal at once.
Julien Kaspar added the
Module
Sculpt, Paint & Texture
label 2023-05-01 12:32:57 +02:00
Member

@JosephEagar Since #107458 is merged I think this can be closed right?

@JosephEagar Since #107458 is merged I think this can be closed right?
Hans Goudey closed this pull request 2023-05-15 17:25:43 +02:00
Hans Goudey deleted branch temp-pbvh-normals 2023-05-16 17:19:02 +02:00

Pull request closed

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 project
No Assignees
4 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#107456
No description provided.