Mesh: Store BMEditMesh in shared pointer #120276

Merged
Hans Goudey merged 5 commits from HooglyBoogly/blender:mesh-edit-mesh-shared-ptr into main 2024-04-18 13:52:32 +02:00
Member

The main motivation for this is that it's part of a fix for #113377,
where I want to propagate the edit mesh pointers through copied
meshes in modifiers and geometry nodes, instead of just setting the
edit mesh pointer at the end of the modifier stack. That would have
two main benefits:

  1. We avoid the need to write to the evaluated mesh, after evaluation
    which means it can be shared directly among evaluated objects.
  2. When an object's mesh is completely replaced by the mesh from another
    object during evaluation (with the object info node), the final edit
    mesh pointer will not be "wrong", allowing us to skip index-mapped
    GPU data extraction.

Beyond that, using a shared pointer just makes things more automatic.
Handling of edit mesh data is already complicated enough, this way some
of the worry and complexity can be handled by RAII.

One thing to keep in mind is that the edit mesh's BMesh is still freed
manually with EDBM_mesh_free_data when leaving edit mode. I figured
that was a more conservative approach for now. Maybe eventually that
could be handled automatically with RAII too.

The main motivation for this is that it's part of a fix for #113377, where I want to propagate the edit mesh pointers through copied meshes in modifiers and geometry nodes, instead of just setting the edit mesh pointer at the end of the modifier stack. That would have two main benefits: 1. We avoid the need to write to the evaluated mesh, after evaluation which means it can be shared directly among evaluated objects. 2. When an object's mesh is completely replaced by the mesh from another object during evaluation (with the object info node), the final edit mesh pointer will not be "wrong", allowing us to skip index-mapped GPU data extraction. Beyond that, using a shared pointer just makes things more automatic. Handling of edit mesh data is already complicated enough, this way some of the worry and complexity can be handled by RAII. One thing to keep in mind is that the edit mesh's BMesh is still freed manually with `EDBM_mesh_free_data` when leaving edit mode. I figured that was a more conservative approach for now. Maybe eventually that could be handled automatically with RAII too.
Hans Goudey added 1 commit 2024-04-04 23:51:12 +02:00
Mesh: Store BMEditMesh in shared pointer
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
a2b0628521
The main motivation for this is that it's part of a fix for #113377,
where I want to propagate the edit mesh pointers through copied mesh
in the modifier stack and geometry nodes, instead of just setting the
mesh's edit mesh pointer at the end of the modifier stack. That would
have two main benefits:
1. We avoid the need to write to the evaluated mesh, which means it
   can be shared directly among evaluated objects.
2. When an object's mesh is completely replaced by the mesh from another
   object during evaluation (with the object info node), the final edit
   mesh pointer will not be incorrect, allowing us to skip index-mapped
   GPU data extraction.

Beyond that, using a shared pointer just makes things more automatic.
Handling of edit mesh data is already complicated enough, this way some
of the worry and complexity can be handled by RAII.

One thing to keep in mind is that the edit mesh's BMesh is still freed
manually with `EDBM_mesh_free_data` when leaving edit mode. I figured
that was a more conservative approach for now. Maybe eventually that
could be handled automatically with RAII too.
Hans Goudey requested review from Campbell Barton 2024-04-04 23:51:30 +02:00
Hans Goudey requested review from Sergey Sharybin 2024-04-04 23:51:31 +02:00
Hans Goudey added this to the Modeling project 2024-04-04 23:52:18 +02:00
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey added 1 commit 2024-04-10 20:24:58 +02:00
Sergey Sharybin reviewed 2024-04-15 17:25:13 +02:00
Sergey Sharybin left a comment
Owner

We avoid the need to write to the evaluated mesh, after evaluation which means it can be shared directly among evaluated objects.

Evaluated state is kind of supposed to be read-only. All modifications are supposed to be done on the original ID, and the evaluated state is to be updated via a dependency graph tag/update.

Is this writing to evaluated mesh is something that already is done in the code? It would help pointing it out, because maybe goal of why it is done is to be reached somehow differently. Or, digging into it might demonstrate better what the actual intent of such writes is.

> We avoid the need to write to the evaluated mesh, after evaluation which means it can be shared directly among evaluated objects. Evaluated state is kind of supposed to be read-only. All modifications are supposed to be done on the original ID, and the evaluated state is to be updated via a dependency graph tag/update. Is this writing to evaluated mesh is something that already is done in the code? It would help pointing it out, because maybe goal of why it is done is to be reached somehow differently. Or, digging into it might demonstrate better what the actual intent of such writes is.
@ -851,7 +843,6 @@ Mesh *BKE_mesh_new_nomain_from_template(const Mesh *me_src,
void BKE_mesh_eval_delete(Mesh *mesh_eval)
{
/* Evaluated mesh may point to edit mesh, but never owns it. */

The comment is out-of-date?

The comment is out-of-date?
Author
Member

Good point. It reminded me that this function is unnecessary now. I've replaced it with BKE_id_free

Good point. It reminded me that this function is unnecessary now. I've replaced it with `BKE_id_free`
HooglyBoogly marked this conversation as resolved
@ -70,3 +70,2 @@
mesh->runtime->edit_mesh = static_cast<BMEditMesh *>(MEM_dupallocN(em));
mesh->runtime->edit_mesh->is_shallow_copy = true;
mesh->runtime->edit_mesh = std::make_shared<BMEditMesh>(*em);

This seems to be a wrong usage of usage of shared pointers. There are now code-paths which will get bare pointer from the mesh->runtime->edit_mesh.get(), pass it to the BKE_mesh_wrapper_from_editmesh, and here a new shared pointer is created.
From my understanding it defeats the original intent of an evaluated state sharing the same em pointer as the input.

This seems to be a wrong usage of usage of shared pointers. There are now code-paths which will get bare pointer from the `mesh->runtime->edit_mesh.get()`, pass it to the `BKE_mesh_wrapper_from_editmesh`, and here a new shared pointer is created. From my understanding it defeats the original intent of an evaluated state sharing the same `em` pointer as the input.
Author
Member

Indeed, thanks. It isn't harmful to correctness in this PR, but it might have messed with my future plans here (comparing edit mesh pointer equality to decide whether to use mapped GPU data extraction). I'll change this function to take a shared pointer argument. That should reduce memory usage too.

Indeed, thanks. It isn't harmful to correctness in this PR, but it might have messed with my future plans here (comparing edit mesh pointer equality to decide whether to use mapped GPU data extraction). I'll change this function to take a shared pointer argument. That should reduce memory usage too.
HooglyBoogly marked this conversation as resolved
Author
Member

Is this writing to evaluated mesh is something that already is done in the code? It would help pointing it out, because maybe goal of why it is done is to be reached somehow differently. Or, digging into it might demonstrate better what the actual intent of such writes is.

The writing I'm referring to in the PR description maybe isn't so obviously problematic:

me_final->runtime->edit_mesh = me_cage->runtime->edit_mesh = mesh->runtime->edit_mesh;

However, I'd like to remove this for two reasons:

  1. Tracking the edit mesh pointer through modifiers and geometry nodes rather than replacing it on any output mesh at the end of modifier evaluation would be a nice fix for #113377 IMO.
  2. Ideally the object's final mesh could be the same output mesh as any other object. This would allow for "free" instancing using the geometry nodes. Writing to the output mesh makes that impossible. Here I'm working towards the proper fix for #119938 rather than the workaround we committed in the meantime.
>Is this writing to evaluated mesh is something that already is done in the code? It would help pointing it out, because maybe goal of why it is done is to be reached somehow differently. Or, digging into it might demonstrate better what the actual intent of such writes is. The writing I'm referring to in the PR description maybe isn't so obviously problematic: ``` me_final->runtime->edit_mesh = me_cage->runtime->edit_mesh = mesh->runtime->edit_mesh; ``` However, I'd like to remove this for two reasons: 1. Tracking the edit mesh pointer through modifiers and geometry nodes rather than replacing it on any output mesh at the end of modifier evaluation would be a nice fix for #113377 IMO. 2. Ideally the object's final mesh could be the same output mesh as any other object. This would allow for "free" instancing using the geometry nodes. Writing to the output mesh makes that impossible. Here I'm working towards the proper fix for #119938 rather than the workaround we committed in the meantime.
Hans Goudey added 3 commits 2024-04-15 18:23:17 +02:00

Ah, ok. It is quite clear the issues with the code you're pointing to, just wasn't as clear what the description is referring to exactly. Sounded a bit more problematic than it actually is.

What I am not sure about is how the replacing mesh in the modifier stack will work with the idea you've mentioned implemented. You still need to be able to see the editmesh somehow, in order to edit it. And to my knowledge drawing code uses evaluated mesh's editmesh structure. Would we need to make object to point to editmesh?

Ideally the object's final mesh could be the same output mesh as any other object.

Can you explain it in a bit more details? I don't think I fully understand this sentence.

Ah, ok. It is quite clear the issues with the code you're pointing to, just wasn't as clear what the description is referring to exactly. Sounded a bit more problematic than it actually is. What I am not sure about is how the replacing mesh in the modifier stack will work with the idea you've mentioned implemented. You still need to be able to see the editmesh somehow, in order to edit it. And to my knowledge drawing code uses evaluated mesh's editmesh structure. Would we need to make object to point to editmesh? > Ideally the object's final mesh could be the same output mesh as any other object. Can you explain it in a bit more details? I don't think I fully understand this sentence.

Forgot to mention. I do see the discussion might be going a bit on a bigger scope than the PR, but with such core side changes I think it is important we are all fully understanding each other. We can move the discussion somewhere outside of the PR, if you think it'll help clarity with it.

For the patch itself, I'd need to look into it again. While it does seems all the aspects are addressed, I'd like to give a bit more careful look and test before giving the final green.

Forgot to mention. I do see the discussion might be going a bit on a bigger scope than the PR, but with such core side changes I think it is important we are all fully understanding each other. We can move the discussion somewhere outside of the PR, if you think it'll help clarity with it. For the patch itself, I'd need to look into it again. While it does seems all the aspects are addressed, I'd like to give a bit more careful look and test before giving the final green.
Author
Member

And to my knowledge drawing code uses evaluated mesh's editmesh structure. Would we need to make object to point to editmesh?

Yes, the drawing code will need to change a bit too. Using the depsgraph to get the original object and then retrieving that object's data should work.

Ideally the object's final mesh could be the same output mesh as any other object.

Can you explain it in a bit more details? I don't think I fully understand this sentence.

Sure! An object's final mesh should be able to be the same mesh as the output of any other object. In other words, if a geometry nodes setup does final_mesh = other_object_geometry_set_eval.get_mesh_for_read(), we shouldn't have to modify final_mesh, which would require copying it. If these final meshes are shared, all the GPU data will be shared between them automatically by the existing draw extraction.

But you're right that this is a longer term goal that's a bit out of scope here, and shouldn't be necessary to justify the PR. I just wanted to give some background about why I was working on this.

>And to my knowledge drawing code uses evaluated mesh's editmesh structure. Would we need to make object to point to editmesh? Yes, the drawing code will need to change a bit too. Using the depsgraph to get the original object and then retrieving that object's data should work. > Ideally the object's final mesh could be the same output mesh as any other object. > >> Can you explain it in a bit more details? I don't think I fully understand this sentence. Sure! An object's final mesh should be able to be the same mesh as the output of any other object. In other words, if a geometry nodes setup does `final_mesh = other_object_geometry_set_eval.get_mesh_for_read()`, we shouldn't have to modify `final_mesh`, which would require copying it. If these final meshes are shared, all the GPU data will be shared between them automatically by the existing draw extraction. But you're right that this is a longer term goal that's a bit out of scope here, and shouldn't be necessary to justify the PR. I just wanted to give some background about why I was working on this.

Using the depsgraph to get the original object and then retrieving that object's data should work.

Need to be careful with that. Render engine is not supposed to look into an original object, as it might not be existing anymore (like, artist deleted an object while render is going). But that is more of details, just wanted to mention it, to avoid possible regression in the thread safety, if someone is reading it out of context :)

There are definitely ways of achieving the goal, we can look into more details when we'll be ready to cross that bridge.

I just wanted to give some background about why I was working on this.

Thanks for explanation. I do think giving such background does help understanding where things are moving, and (more importantly) why.

> Using the depsgraph to get the original object and then retrieving that object's data should work. Need to be careful with that. Render engine is not supposed to look into an original object, as it might not be existing anymore (like, artist deleted an object while render is going). But that is more of details, just wanted to mention it, to avoid possible regression in the thread safety, if someone is reading it out of context :) There are definitely ways of achieving the goal, we can look into more details when we'll be ready to cross that bridge. > I just wanted to give some background about why I was working on this. Thanks for explanation. I do think giving such background does help understanding where things are moving, and (more importantly) why.
Author
Member

Render engine is not supposed to look into an original object, as it might not be existing anymore

Ah right, thanks! Still internalizing that fact here I guess. Anyway, yeah, stuff to look at later.

>Render engine is not supposed to look into an original object, as it might not be existing anymore Ah right, thanks! Still internalizing that fact here I guess. Anyway, yeah, stuff to look at later.
Sergey Sharybin approved these changes 2024-04-17 17:59:23 +02:00
Campbell Barton approved these changes 2024-04-18 06:53:38 +02:00
Hans Goudey merged commit d95b1f120b into main 2024-04-18 13:52:32 +02:00
Hans Goudey deleted branch mesh-edit-mesh-shared-ptr 2024-04-18 13:52:34 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
Asset Browser Project
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#120276
No description provided.