Mesh: Split MLoopTri poly indices into a separate array #106774

Merged
Hans Goudey merged 25 commits from HooglyBoogly/blender:refactor-mesh-looptri-generic into main 2023-05-04 15:39:16 +02:00
Member

For derived mesh triangulation information, currently the three face
corner indices are stored in the same struct as index of the mesh
polygon the triangle is part of. While those bits of information are
often used together, they often aren't, and combining them prevents
the indices from being used with generic utilities. It also means that
1/3 more memory has to be written when recalculating the triangulation
after deforming the mesh, and that the entire triangle data has to be
read when only the polygon indices are needed.

This commit splits the polygon index into a separate cache on Mesh.
The triangulation data isn't saved to files, so this doesn't affect
.blend files at all.

In a simple test deforming a mesh with geometry nodes, the time used
to recalculate the triangulation reduced from 2.0 ms to 1.6 ms,
increasing overall FPS from 14.6 to 15.

For derived mesh triangulation information, currently the three face corner indices are stored in the same struct as index of the mesh polygon the triangle is part of. While those bits of information are often used together, they often aren't, and combining them prevents the indices from being used with generic utilities. It also means that 1/3 more memory has to be written when recalculating the triangulation after deforming the mesh, and that the entire triangle data has to be read when only the polygon indices are needed. This commit splits the polygon index into a separate cache on `Mesh`. The triangulation data isn't saved to files, so this doesn't affect .blend files at all. In a simple test deforming a mesh with geometry nodes, the time used to recalculate the triangulation reduced from 2.0 ms to 1.6 ms, increasing overall FPS from 14.6 to 15.
Hans Goudey added 9 commits 2023-04-10 20:40:00 +02:00
Hans Goudey added 1 commit 2023-04-13 03:43:22 +02:00
Hans Goudey added 1 commit 2023-04-13 04:02:49 +02:00
Hans Goudey added 1 commit 2023-04-13 04:11:06 +02:00
Hans Goudey added 1 commit 2023-04-14 04:26:41 +02:00
Hans Goudey added 1 commit 2023-04-14 05:00:12 +02:00
Hans Goudey added this to the Modeling project 2023-04-14 14:18:39 +02:00
Hans Goudey added 1 commit 2023-04-14 14:21:16 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
39e4b2cc68
Merge branch 'main' into refactor-mesh-looptri-generic
Hans Goudey added 1 commit 2023-04-16 22:04:14 +02:00
Hans Goudey added 1 commit 2023-04-26 01:21:27 +02:00
Hans Goudey added 1 commit 2023-04-28 17:14:21 +02:00
Hans Goudey added 1 commit 2023-04-30 23:55:20 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
760a006f0a
Merge branch 'main' into refactor-mesh-looptri-generic
Hans Goudey added 1 commit 2023-05-01 00:03:29 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
6b8cbe3f77
Fix compile error after merge
Hans Goudey added 1 commit 2023-05-01 00:18:00 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
e36ab759af
Fix second compile error after merge
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey requested review from Campbell Barton 2023-05-01 02:36:35 +02:00
Hans Goudey requested review from Jacques Lucke 2023-05-01 02:36:36 +02:00
Author
Member

Not sure it's worth anyone else's time to read through the whole diff. The interesting parts are:

  • mesh.cc: Mesh::looptri_polys()
  • DNA_mesh_types.h: looptri_polys()
  • rna_mesh.c: "loop_triangle_faces"
  • mesh_tessellate.cc: looptris_calc_poly_indices

And for nice examples about how things are generally improved:

  • mesh_sample.cc: sample_face_attribute (3x less memory accessed for face indices)
  • mesh_runtime.cc: BKE_mesh_tag_positions_changed (Deforming mesh doesn't clear this cache)
  • mesh_tessellate.cc: mesh_calc_tessellation_for_face_impl (Writes 25% less memory when computing triangulation)
Not sure it's worth anyone else's time to read through the whole diff. The interesting parts are: - `mesh.cc`: `Mesh::looptri_polys()` - `DNA_mesh_types.h`: `looptri_polys()` - `rna_mesh.c`: `"loop_triangle_faces"` - `mesh_tessellate.cc`: `looptris_calc_poly_indices` And for nice examples about how things are generally improved: - `mesh_sample.cc`: `sample_face_attribute` (3x less memory accessed for face indices) - `mesh_runtime.cc`: `BKE_mesh_tag_positions_changed` (Deforming mesh doesn't clear this cache) - `mesh_tessellate.cc`: `mesh_calc_tessellation_for_face_impl` (Writes 25% less memory when computing triangulation)
Jacques Lucke reviewed 2023-05-01 11:57:00 +02:00
@ -4330,1 +4374,4 @@
rna_def_looptri_poly_value(brna);
prop = RNA_def_property(srna, "loop_triangle_faces", PROP_COLLECTION, PROP_NONE);
Member

Why is it called "faces" here instead of e.g. "polygons"?

Why is it called "faces" here instead of e.g. "polygons"?
Author
Member

It's "faces" because the plan (#101689) is to replace the "polys" term with "faces"-- the same reason we have the "sharp_face" attribute instead of "sharp_poly". I guess the way that applies to RNA isn't as clear (worth breaking compatibility for that rename? We're doing a large compatibility break in 4.0 anyway (#100153)).

It's "faces" because the plan (#101689) is to replace the "polys" term with "faces"-- the same reason we have the "sharp_face" attribute instead of "sharp_poly". I guess the way that applies to RNA isn't as clear (worth breaking compatibility for that rename? We're doing a large compatibility break in 4.0 anyway (#100153)).
Member

Currently I don't see much of a benefig of breaking rna here, but using faces here is fine with me.

Currently I don't see much of a benefig of breaking rna here, but using faces here is fine with me.

Rather not use term faces as it leaves the API in a confusing/inconsistent state.

Smooth faces is a characteristic so I'm not so fussed using the naming there... where as a face index and a polygon index are not obviously the same thing (especially when we have ).

If you write:

for poly_index in mesh.loop_triangle_faces:
    mesh.polygons[poly_index]

Or

for face_index in mesh.loop_triangle_faces:
    mesh.polygons[face_index]

Both read like an error, even: "Triangle Faces", "The polygon index ... reads as if one of them should be changed.

Rather not use term faces as it leaves the API in a confusing/inconsistent state. Smooth faces is a characteristic so I'm not so fussed using the naming there... where as a face index and a polygon index are not obviously the same thing (especially when we have ). If you write: ``` for poly_index in mesh.loop_triangle_faces: mesh.polygons[poly_index] ``` Or ``` for face_index in mesh.loop_triangle_faces: mesh.polygons[face_index] ``` Both read like an error, even: `"Triangle Faces", "The polygon index ...` reads as if one of them should be changed.
Jacques Lucke approved these changes 2023-05-01 17:16:32 +02:00
Hans Goudey added 1 commit 2023-05-03 15:04:59 +02:00
Hans Goudey added 1 commit 2023-05-03 16:27:32 +02:00
Campbell Barton approved these changes 2023-05-04 07:13:46 +02:00
Campbell Barton left a comment
Owner

Minor request & suggestion, otherwise LGTM.

Minor request & suggestion, otherwise LGTM.
@ -1260,3 +1260,3 @@
}
bool paint_is_face_hidden(const MLoopTri *lt, const bool *hide_poly)
bool paint_is_face_hidden(const int *looptri_polys, const bool *hide_poly, const int tri_index)

It looks like this could take the PBVH instead of looptri_polys & hide_poly. No strong opinion though.

It looks like this could take the `PBVH` instead of `looptri_polys` & `hide_poly`. No strong opinion though.
Author
Member

I think this isn't worth putting in a function now, I'll probably remove it in the near future.

I think this isn't worth putting in a function now, I'll probably remove it in the near future.
Campbell Barton reviewed 2023-05-04 07:19:07 +02:00
@ -301,0 +303,4 @@
threading::parallel_for(polys.index_range(), 1024, [&](const IndexRange range) {
for (const int64_t i : range) {
const IndexRange poly = polys[i];
const int start = poly_to_tri_count(int(i), int(poly.start()));

poly_to_tri_count could run once at the start, then increment it by num in the loop (in practice the performance difference might be insignificant though).

`poly_to_tri_count` could run once at the start, then increment it by `num` in the loop (in practice the performance difference might be insignificant though).
Author
Member

I tried this and found if anything it's a bit slower (6.5-6.6 ms rather than 6.4-6.5 ms before) with 16 million faces.

I tried this and found if anything it's a bit slower (6.5-6.6 ms rather than 6.4-6.5 ms before) with 16 million faces.
Hans Goudey added 2 commits 2023-05-04 14:56:13 +02:00
Hans Goudey merged commit d0705bd697 into main 2023-05-04 15:39:16 +02:00
Hans Goudey deleted branch refactor-mesh-looptri-generic 2023-05-04 15:39:17 +02:00
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:42 +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#106774
No description provided.