From 4e7e539b3393b65b3dbaff39ba367a79b83e898e Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 6 Dec 2023 18:30:51 -0500 Subject: [PATCH] Sculpt: Make hide poly pointer in sculpt session const This avoids having to make the original data layer mutable when we aren't going to modify it, meaning the memory can still be shared with the evaluated mesh-- saving 1 byte per face in some situations. This was made possible by previous commits that moved to using the Mesh attribute API instead of the SculptSession pointer to edit this data. Eventually the `hide_poly` pointer should be completely removed. --- source/blender/blenkernel/BKE_paint.hh | 9 ++++----- source/blender/blenkernel/intern/paint.cc | 15 +++++---------- .../editors/sculpt_paint/sculpt_face_set.cc | 2 +- .../blender/editors/sculpt_paint/sculpt_undo.cc | 3 +-- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/source/blender/blenkernel/BKE_paint.hh b/source/blender/blenkernel/BKE_paint.hh index 38d3f34daea..59226ec09a3 100644 --- a/source/blender/blenkernel/BKE_paint.hh +++ b/source/blender/blenkernel/BKE_paint.hh @@ -611,7 +611,7 @@ struct SculptSession { * A reference to the ".hide_poly" attribute, to store whether (base) faces are hidden. * May be null. */ - bool *hide_poly; + const bool *hide_poly; /* BMesh for dynamic topology sculpting */ BMesh *bm; @@ -844,11 +844,10 @@ void BKE_sculpt_update_object_after_eval(Depsgraph *depsgraph, Object *ob_eval); MultiresModifierData *BKE_sculpt_multires_active(const Scene *scene, Object *ob); int *BKE_sculpt_face_sets_ensure(Object *ob); /** - * Create the attribute used to store face visibility and retrieve its data. - * Note that changes to the face visibility have to be propagated to other domains - * (see #ed::sculpt_paint::hide::sync_all_from_faces). + * Update the pointer to the ".hide_poly" attribute. This is necessary because it is dynamically + * created, removed, and made mutable. */ -bool *BKE_sculpt_hide_poly_ensure(Mesh *mesh); +void BKE_sculpt_hide_poly_pointer_update(Object &object); /** * Ensures a mask layer exists. If depsgraph and bmain are non-null, diff --git a/source/blender/blenkernel/intern/paint.cc b/source/blender/blenkernel/intern/paint.cc index 2bdedbb73fb..eb2af468b31 100644 --- a/source/blender/blenkernel/intern/paint.cc +++ b/source/blender/blenkernel/intern/paint.cc @@ -1734,8 +1734,7 @@ static void sculpt_update_object(Depsgraph *depsgraph, ss->face_sets = nullptr; } - ss->hide_poly = (bool *)CustomData_get_layer_named_for_write( - &me->face_data, CD_PROP_BOOL, ".hide_poly", me->faces_num); + ss->hide_poly = (bool *)CustomData_get_layer_named(&me->face_data, CD_PROP_BOOL, ".hide_poly"); ss->subdiv_ccg = me_eval->runtime->subdiv_ccg.get(); @@ -1979,15 +1978,11 @@ int *BKE_sculpt_face_sets_ensure(Object *ob) return nullptr; } -bool *BKE_sculpt_hide_poly_ensure(Mesh *mesh) +void BKE_sculpt_hide_poly_pointer_update(Object &object) { - bool *hide_poly = static_cast(CustomData_get_layer_named_for_write( - &mesh->face_data, CD_PROP_BOOL, ".hide_poly", mesh->faces_num)); - if (hide_poly != nullptr) { - return hide_poly; - } - return static_cast(CustomData_add_layer_named( - &mesh->face_data, CD_PROP_BOOL, CD_SET_DEFAULT, mesh->faces_num, ".hide_poly")); + const Mesh &mesh = *static_cast(object.data); + object.sculpt->hide_poly = static_cast( + CustomData_get_layer_named(&mesh.face_data, CD_PROP_BOOL, ".hide_poly")); } void BKE_sculpt_mask_layers_ensure(Depsgraph *depsgraph, diff --git a/source/blender/editors/sculpt_paint/sculpt_face_set.cc b/source/blender/editors/sculpt_paint/sculpt_face_set.cc index 3181806da5b..1d732008311 100644 --- a/source/blender/editors/sculpt_paint/sculpt_face_set.cc +++ b/source/blender/editors/sculpt_paint/sculpt_face_set.cc @@ -980,7 +980,7 @@ static int sculpt_face_set_change_visibility_exec(bContext *C, wmOperator *op) SCULPT_undo_push_end(&object); BKE_pbvh_update_visibility(ss->pbvh); - ss->hide_poly = BKE_sculpt_hide_poly_ensure(mesh); + BKE_sculpt_hide_poly_pointer_update(object); SCULPT_topology_islands_invalidate(object.sculpt); hide::tag_update_visibility(*C); diff --git a/source/blender/editors/sculpt_paint/sculpt_undo.cc b/source/blender/editors/sculpt_paint/sculpt_undo.cc index bc683ba3363..3b25e04fc1b 100644 --- a/source/blender/editors/sculpt_paint/sculpt_undo.cc +++ b/source/blender/editors/sculpt_paint/sculpt_undo.cc @@ -573,7 +573,6 @@ static bool sculpt_undo_restore_hidden_face(Object &object, MutableSpan modified_faces) { using namespace blender; - SculptSession *ss = object.sculpt; Mesh &mesh = *static_cast(object.data); bke::MutableAttributeAccessor attributes = mesh.attributes_for_write(); bke::SpanAttributeWriter hide_poly = attributes.lookup_or_add_for_write_span( @@ -592,7 +591,7 @@ static bool sculpt_undo_restore_hidden_face(Object &object, } } hide_poly.finish(); - ss->hide_poly = BKE_sculpt_hide_poly_ensure(&mesh); + BKE_sculpt_hide_poly_pointer_update(object); return modified; }