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.
This commit is contained in:
Hans Goudey 2023-12-06 18:30:51 -05:00
parent 466dca07d5
commit 4e7e539b33
4 changed files with 11 additions and 18 deletions

View File

@ -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,

View File

@ -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<bool *>(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<bool *>(CustomData_add_layer_named(
&mesh->face_data, CD_PROP_BOOL, CD_SET_DEFAULT, mesh->faces_num, ".hide_poly"));
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
object.sculpt->hide_poly = static_cast<const bool *>(
CustomData_get_layer_named(&mesh.face_data, CD_PROP_BOOL, ".hide_poly"));
}
void BKE_sculpt_mask_layers_ensure(Depsgraph *depsgraph,

View File

@ -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);

View File

@ -573,7 +573,6 @@ static bool sculpt_undo_restore_hidden_face(Object &object,
MutableSpan<bool> modified_faces)
{
using namespace blender;
SculptSession *ss = object.sculpt;
Mesh &mesh = *static_cast<Mesh *>(object.data);
bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
bke::SpanAttributeWriter hide_poly = attributes.lookup_or_add_for_write_span<bool>(
@ -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;
}