Option to request a CustomData layer for write without preserving existing data #117179
No reviewers
Labels
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
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#117179
Loading…
Reference in New Issue
No description provided.
Delete Branch "Eugene-Kuznetsov/blender:customdata_preserve_flag"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This adds an optional flag to CustomData_get_layer_named_for_write to signal that the caller is not interested in the layer's current content.
(Separated from #116545)
Thanks for the PR! We have
for_write_only
in the attribute API for the same reason, it does make a difference. And it makes sense that we should be able to get that benefit without using the attribute API as well. To me the(false)
is enough to show that there's something "special" going on.That said, this would be much more convincing with some performance numbers included. There are also plenty of other places to add this option-- mesh and point cloud accessors for example. I'm happy to add support in other places after this though.
@ -193,3 +193,3 @@
Span<float3> positions() const;
MutableSpan<float3> positions_for_write();
MutableSpan<float3> positions_for_write(bool preserve=true);
I'd suggest configuring your editor to run clang format whenever you save the file, that would save a lot of time!
@ -248,4 +249,4 @@
if (data != nullptr) {
return {data, num};
}
data = (T *)CustomData_add_layer_named(&custom_data, type, CD_SET_DEFAULT, num, name.c_str());
If
preserve
is false, you can useCD_CONSTRUCT
instead ofCD_SET_DEFAULT
. That will allow usingmalloc
instead ofcalloc
in most cases.It might be worth using the same semantics as the attribute API actually: flipping the value and calling it
for_write_only
instead.It's also worth documenting (somewhere?) that all values have to be written to in this case.
Requesting changes based on my previous comment.
46c1c6ecf1
to331c1d8f52
Here's what I see from this specific change in #116545, using the test file attached there, and timing either the entire function
4f91c770ce/source/blender/nodes/geometry/nodes/node_geo_deform_curves_on_surface.cc (L328)
or specifically the call to positions_for_write:with 'false':
Timer '<positions_for_write>DeformCurvesOnSurface(74282, 74282, 1199404)': (Average: 956 ns, Min: 660 ns, Last: 830 ns 80 calls)
Timer 'DeformCurvesOnSurface(74282, 74282, 1199404)': (Average: 19.68 ms, Min: 18.04 ms, Last: 19.44 ms 70 calls)
with 'true':
Timer '<positions_for_write>DeformCurvesOnSurface(74282, 74282, 1199404)': (Average: 4.04 ms, Min: 3.95 ms, Last: 4.09 ms 60 calls)
Timer 'DeformCurvesOnSurface(74282, 74282, 1199404)': (Average: 23.94 ms, Min: 22.66 ms, Last: 23.76 ms 60 calls)
that's about 15% shaved off the node execution time.
331c1d8f52
to0ccb374d64
Just to be clear, from my perspective this would need some changes to be merged in main.
preserve
should change tofor_write_only
for consistency with the attribute API, defaulted to falsebke::CurvesGeometry::positions_for_write()
affected, there are lots of similar functions across the geometry types0ccb374d64
tof7a2ce8079
This change grew quite a bit more than I hoped TBH. I'd rather not include
create_if_missing
in this PR at all-- it's an orthogonal change. Templated CustomData API functions aren't related either, I would also prefer not to combine that with the goal of this PR. And I don't think this should need to include theCPPType
forMDeformVert
. That's not added yet so far on purpose.f7a2ce8079
to0ccb374d64
Without the templates, it will not be all that shorter, because, in situations where for_write_only may be set, it is often as part of a pattern
so I have to touch at least two lines in every such place anyway. Templates make these patterns shorter and less error-prone.
I've reverted the changes. I'm going to change preserve -> for_write_only next time I have time to work on this.
Option to request a CustomData layer for write without preserving existing datato WIP: Option to request a CustomData layer for write without preserving existing data0ccb374d64
to96947ed5ff
WIP: Option to request a CustomData layer for write without preserving existing datato Option to request a CustomData layer for write without preserving existing dataI'm actually having trouble finding places where it's correct to use this. There is a problem with your change in #116545. Calling
positions_for_write()
may remove a user from the existing array if it is non-mutable. If another thread removes the last user after that, while it's still being used by the surface deform node, that results in a use-after free. Generally we have to be careful keeping around a pointer to the old read-only copy of an attribute after calling "for_write".Besides that case, most places seem to be building geometry from scratch where this isn't helpful. It would be nice to have a few uses of this option in this PR just to show its utility.
These are the locations where for_write seems to be called for (as far as I understand the code):
source/blender/blenkernel/intern/DerivedMesh.cc:406
source/blender/blenkernel/intern/data_transfer.cc:1057 if (mix_factor != 1.0f || mix_weights)
source/blender/blenkernel/intern/mesh_normals.cc:1532
source/blender/modifiers/intern/MOD_cloth.cc:101
source/blender/render/intern/multires_bake.cc:500..508
possibly some places in grease pencil code.
This is helpful even when building geometry from scratch, since it allows to bypass default-initialization. Any time the user expects to overwrite the entire buffer returned by CustomData_layer_named_for_write() or get_mutable_attribute(), there's a performance benefit from letting the API know about it.
234b20dbd3
to1e4f708c4b
Checkout
From your project repository, check out a new branch and test the changes.