Refactor: Sculpt: Remove SculptSession mesh size variables

Part of #118145.
This commit is contained in:
Hans Goudey
2024-09-13 10:14:47 -04:00
parent 1f7d594aca
commit 27e2062eac
10 changed files with 51 additions and 79 deletions

View File

@@ -23,6 +23,7 @@
#include "DNA_brush_enums.h"
#include "DNA_customdata_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_enums.h"
#include "BKE_pbvh.hh"
@@ -341,10 +342,6 @@ struct SculptSession : blender::NonCopyable, blender::NonMovable {
int level = 0;
} multires = {};
/* These contain the vertex and poly counts of the final mesh. */
int totvert = 0;
int faces_num = 0;
KeyBlock *shapekey_active = nullptr;
/* Edges to adjacent faces. */
@@ -357,10 +354,6 @@ struct SculptSession : blender::NonCopyable, blender::NonMovable {
blender::Array<int> vert_to_edge_indices;
blender::GroupedSpan<int> vert_to_edge_map;
/* Mesh Face Sets */
/* Total number of faces of the base mesh. */
int totfaces = 0;
/**
* A reference to the ".hide_poly" attribute, to store whether (base) faces are hidden.
* May be null.
@@ -450,7 +443,7 @@ struct SculptSession : blender::NonCopyable, blender::NonMovable {
/* Needed to continuously re-apply over the same weights (BRUSH_ACCUMULATE disabled).
* Lazy initialize as needed (flag is set to 1 to tag it as uninitialized). */
MDeformVert *dvert_prev;
blender::Array<MDeformVert> dvert_prev;
} wpaint;
/* TODO: identify sculpt-only fields */
@@ -538,7 +531,6 @@ void BKE_sculptsession_free_vwpaint_data(SculptSession *ss);
void BKE_sculptsession_free_pbvh(Object &object);
void BKE_sculptsession_bm_to_me(Object *ob, bool reorder);
void BKE_sculptsession_bm_to_me_for_render(Object *object);
int BKE_sculptsession_vertex_count(const SculptSession *ss);
/**
* Create new color layer on object if it doesn't have one and if experimental feature set has

View File

@@ -1629,10 +1629,10 @@ void BKE_sculptsession_free_vwpaint_data(SculptSession *ss)
{
if (ss->mode_type == OB_MODE_WEIGHT_PAINT) {
MEM_SAFE_FREE(ss->mode.wpaint.alpha_weight);
if (ss->mode.wpaint.dvert_prev) {
BKE_defvert_array_free_elems(ss->mode.wpaint.dvert_prev, ss->totvert);
MEM_freeN(ss->mode.wpaint.dvert_prev);
ss->mode.wpaint.dvert_prev = nullptr;
if (!ss->mode.wpaint.dvert_prev.is_empty()) {
BKE_defvert_array_free_elems(ss->mode.wpaint.dvert_prev.data(),
ss->mode.wpaint.dvert_prev.size());
ss->mode.wpaint.dvert_prev = {};
}
}
}
@@ -1987,14 +1987,8 @@ static void sculpt_update_object(Depsgraph *depsgraph,
ss.multires.active = true;
ss.multires.modifier = mmd;
ss.multires.level = mmd->sculptlvl;
ss.totvert = mesh_eval->verts_num;
ss.faces_num = mesh_eval->faces_num;
ss.totfaces = mesh_orig->faces_num;
}
else {
ss.totvert = mesh_orig->verts_num;
ss.faces_num = mesh_orig->faces_num;
ss.totfaces = mesh_orig->faces_num;
ss.multires.active = false;
ss.multires.modifier = nullptr;
ss.multires.level = 0;
@@ -2499,14 +2493,3 @@ void BKE_paint_face_set_overlay_color_get(const int face_set, const int seed, uc
&rgba[2]);
rgba_float_to_uchar(r_color, rgba);
}
int BKE_sculptsession_vertex_count(const SculptSession *ss)
{
if (ss->bm) {
return ss->bm->totvert;
}
if (ss->subdiv_ccg) {
return ss->subdiv_ccg->positions.size();
}
return ss->totvert;
}

View File

@@ -256,27 +256,25 @@ void init_session_data(const ToolSettings &ts, Object &ob)
/* Create average brush arrays */
if (ob.mode == OB_MODE_WEIGHT_PAINT) {
SculptSession &ss = *ob.sculpt;
if (!vwpaint::brush_use_accumulate(*ts.wpaint)) {
if (ob.sculpt->mode.wpaint.alpha_weight == nullptr) {
ob.sculpt->mode.wpaint.alpha_weight = (float *)MEM_callocN(mesh->verts_num * sizeof(float),
__func__);
if (ss.mode.wpaint.alpha_weight == nullptr) {
ss.mode.wpaint.alpha_weight = (float *)MEM_callocN(mesh->verts_num * sizeof(float),
__func__);
}
if (ob.sculpt->mode.wpaint.dvert_prev == nullptr) {
ob.sculpt->mode.wpaint.dvert_prev = (MDeformVert *)MEM_callocN(
mesh->verts_num * sizeof(MDeformVert), __func__);
MDeformVert *dv = ob.sculpt->mode.wpaint.dvert_prev;
for (int i = 0; i < mesh->verts_num; i++, dv++) {
/* Use to show this isn't initialized, never apply to the mesh data. */
dv->flag = 1;
}
if (ss.mode.wpaint.dvert_prev.is_empty()) {
MDeformVert initial_value{};
/* Use to show this isn't initialized, never apply to the mesh data. */
initial_value.flag = 1;
ss.mode.wpaint.dvert_prev = Array<MDeformVert>(mesh->verts_num, initial_value);
}
}
else {
MEM_SAFE_FREE(ob.sculpt->mode.wpaint.alpha_weight);
if (ob.sculpt->mode.wpaint.dvert_prev != nullptr) {
BKE_defvert_array_free_elems(ob.sculpt->mode.wpaint.dvert_prev, mesh->verts_num);
MEM_freeN(ob.sculpt->mode.wpaint.dvert_prev);
ob.sculpt->mode.wpaint.dvert_prev = nullptr;
MEM_SAFE_FREE(ss.mode.wpaint.alpha_weight);
if (!ss.mode.wpaint.dvert_prev.is_empty()) {
BKE_defvert_array_free_elems(ss.mode.wpaint.dvert_prev.data(),
ss.mode.wpaint.dvert_prev.size());
ss.mode.wpaint.dvert_prev = {};
}
}
}

View File

@@ -622,7 +622,7 @@ static void do_weight_paint_vertex_single(const VPaint &wp,
}
if (!vwpaint::brush_use_accumulate(wp)) {
MDeformVert *dvert_prev = ob.sculpt->mode.wpaint.dvert_prev;
MDeformVert *dvert_prev = ob.sculpt->mode.wpaint.dvert_prev.data();
MDeformVert *dv_prev = defweight_prev_init(dvert_prev, wpi.dvert.data(), index);
if (index_mirr != -1) {
defweight_prev_init(dvert_prev, wpi.dvert.data(), index_mirr);
@@ -770,7 +770,7 @@ static void do_weight_paint_vertex_multi(const VPaint &wp,
}
if (!vwpaint::brush_use_accumulate(wp)) {
MDeformVert *dvert_prev = ob.sculpt->mode.wpaint.dvert_prev;
MDeformVert *dvert_prev = ob.sculpt->mode.wpaint.dvert_prev.data();
MDeformVert *dv_prev = defweight_prev_init(dvert_prev, wpi.dvert.data(), index);
if (index_mirr != -1) {
defweight_prev_init(dvert_prev, wpi.dvert.data(), index_mirr);
@@ -1005,8 +1005,8 @@ static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float mo
wpd->precomputed_weight = (float *)MEM_mallocN(sizeof(float) * mesh.verts_num, __func__);
}
if (ob.sculpt->mode.wpaint.dvert_prev != nullptr) {
MDeformVert *dv = ob.sculpt->mode.wpaint.dvert_prev;
if (!ob.sculpt->mode.wpaint.dvert_prev.is_empty()) {
MDeformVert *dv = ob.sculpt->mode.wpaint.dvert_prev.data();
for (int i = 0; i < mesh.verts_num; i++, dv++) {
/* Use to show this isn't initialized, never apply to the mesh data. */
dv->flag = 1;

View File

@@ -1363,7 +1363,6 @@ static void calc_falloff_from_vert_and_symmetry(const Depsgraph &depsgraph,
*/
static void snap_init_from_enabled(const Depsgraph &depsgraph,
const Object &object,
SculptSession &ss,
Cache &expand_cache)
{
const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
@@ -1382,8 +1381,7 @@ static void snap_init_from_enabled(const Depsgraph &depsgraph,
const BitVector<> enabled_verts = enabled_state_to_bitmap(depsgraph, object, expand_cache);
const int totface = ss.totfaces;
for (int i = 0; i < totface; i++) {
for (const int i : faces.index_range()) {
const int face_set = expand_cache.original_face_sets[i];
expand_cache.snap_enabled_face_sets->add(face_set);
}
@@ -1823,13 +1821,11 @@ static void original_state_store(Object &ob, Cache &expand_cache)
*/
static void face_sets_restore(Object &object, Cache &expand_cache)
{
SculptSession &ss = *object.sculpt;
Mesh &mesh = *static_cast<Mesh *>(object.data);
const OffsetIndices<int> faces = mesh.faces();
const Span<int> corner_verts = mesh.corner_verts();
bke::SpanAttributeWriter<int> face_sets = face_set::ensure_face_sets_mesh(mesh);
const int totfaces = ss.totfaces;
for (int i = 0; i < totfaces; i++) {
for (const int i : faces.index_range()) {
if (expand_cache.original_face_sets[i] <= 0) {
/* Do not modify hidden Face Sets, even when restoring the IDs state. */
continue;
@@ -2253,7 +2249,7 @@ static int sculpt_expand_modal(bContext *C, wmOperator *op, const wmEvent *event
else {
expand_cache.snap = true;
expand_cache.snap_enabled_face_sets = std::make_unique<Set<int>>();
snap_init_from_enabled(*depsgraph, ob, ss, expand_cache);
snap_init_from_enabled(*depsgraph, ob, expand_cache);
}
break;
}
@@ -2394,8 +2390,6 @@ static int sculpt_expand_modal(bContext *C, wmOperator *op, const wmEvent *event
static void delete_face_set_id(
int *r_face_sets, Object &object, Cache &expand_cache, Mesh *mesh, const int delete_id)
{
SculptSession &ss = *object.sculpt;
const int totface = ss.totfaces;
const GroupedSpan<int> vert_to_face_map = mesh->vert_to_face_map();
const OffsetIndices faces = mesh->faces();
const Span<int> corner_verts = mesh->corner_verts();
@@ -2403,7 +2397,7 @@ static void delete_face_set_id(
/* Check that all the face sets IDs in the mesh are not equal to `delete_id`
* before attempting to delete it. */
bool all_same_id = true;
for (int i = 0; i < totface; i++) {
for (const int i : faces.index_range()) {
if (!is_face_in_active_component(object, faces, corner_verts, expand_cache, i)) {
continue;
}
@@ -2422,7 +2416,7 @@ static void delete_face_set_id(
BLI_LINKSTACK_INIT(queue);
BLI_LINKSTACK_INIT(queue_next);
for (int i = 0; i < totface; i++) {
for (const int i : faces.index_range()) {
if (r_face_sets[i] == delete_id) {
BLI_LINKSTACK_PUSH(queue, POINTER_FROM_INT(i));
}

View File

@@ -1106,7 +1106,6 @@ void SCULPT_OT_face_set_change_visibility(wmOperatorType *ot)
static int randomize_colors_exec(bContext *C, wmOperator * /*op*/)
{
Object &ob = *CTX_data_active_object(C);
SculptSession &ss = *ob.sculpt;
const View3D *v3d = CTX_wm_view3d(C);
const Base *base = CTX_data_active_base(C);
@@ -1129,8 +1128,9 @@ static int randomize_colors_exec(bContext *C, wmOperator * /*op*/)
}
const VArray<int> face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
const int random_index = clamp_i(
ss.totfaces * BLI_hash_int_01(mesh->face_sets_color_seed), 0, max_ii(0, ss.totfaces - 1));
const int random_index = clamp_i(mesh->faces_num * BLI_hash_int_01(mesh->face_sets_color_seed),
0,
max_ii(0, mesh->faces_num - 1));
mesh->face_sets_color_default = face_sets[random_index];
mesh->face_sets_color_seed += 1;

View File

@@ -760,7 +760,6 @@ static void report_invalid_mode(const blender::bke::pbvh::Type pbvh_type, Report
static bool can_exec(const bContext &C, ReportList &reports)
{
const Object &object = *CTX_data_active_object(&C);
const SculptSession &ss = *object.sculpt;
const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
if (pbvh.type() != bke::pbvh::Type::Mesh) {
/* Not supported in Multires and Dyntopo. */
@@ -768,7 +767,7 @@ static bool can_exec(const bContext &C, ReportList &reports)
return false;
}
if (ss.totvert == 0) {
if (static_cast<const Mesh *>(object.data)->faces_num == 0) {
/* No geometry to trim or to detect a valid position for the trimming shape. */
return false;
}

View File

@@ -1016,7 +1016,8 @@ static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
if (!restore_active_shape_key(*C, *depsgraph, step_data, object)) {
return;
}
Array<bool> modified_verts(ss.totvert, false);
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
Array<bool> modified_verts(mesh.verts_num, false);
restore_position_mesh(object, step_data.nodes, modified_verts);
node_mask.foreach_index([&](const int i) {
if (indices_contain_true(modified_verts, nodes[i].all_verts())) {
@@ -1058,7 +1059,8 @@ static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
}
else {
MutableSpan<bke::pbvh::MeshNode> nodes = pbvh.nodes<bke::pbvh::MeshNode>();
Array<bool> modified_verts(ss.totvert, false);
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
Array<bool> modified_verts(mesh.verts_num, false);
for (std::unique_ptr<Node> &unode : step_data.nodes) {
restore_vert_visibility_mesh(object, *unode, modified_verts);
}
@@ -1085,7 +1087,8 @@ static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
return;
}
Array<bool> modified_faces(ss.totfaces, false);
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
Array<bool> modified_faces(mesh.faces_num, false);
for (std::unique_ptr<Node> &unode : step_data.nodes) {
restore_hidden_face(object, *unode, modified_faces);
}
@@ -1140,7 +1143,8 @@ static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
}
else {
MutableSpan<bke::pbvh::MeshNode> nodes = pbvh.nodes<bke::pbvh::MeshNode>();
Array<bool> modified_verts(ss.totvert, false);
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
Array<bool> modified_verts(mesh.verts_num, false);
for (std::unique_ptr<Node> &unode : step_data.nodes) {
restore_mask_mesh(object, *unode, modified_verts);
}
@@ -1163,7 +1167,8 @@ static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
return;
}
Array<bool> modified_faces(ss.totfaces, false);
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
Array<bool> modified_faces(mesh.faces_num, false);
for (std::unique_ptr<Node> &unode : step_data.nodes) {
restore_face_sets(object, *unode, modified_faces);
}
@@ -1201,7 +1206,8 @@ static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
}
MutableSpan<bke::pbvh::MeshNode> nodes = pbvh.nodes<bke::pbvh::MeshNode>();
Array<bool> modified_verts(ss.totvert, false);
const Mesh &mesh = *static_cast<const Mesh *>(object.data);
Array<bool> modified_verts(mesh.verts_num, false);
restore_color(object, step_data, modified_verts);
node_mask.foreach_index([&](const int i) {
if (indices_contain_true(modified_verts, nodes[i].all_verts())) {
@@ -1832,7 +1838,7 @@ void push_begin_ex(Object &ob, const char *name)
switch (pbvh.type()) {
case bke::pbvh::Type::Mesh: {
const Mesh &mesh = *static_cast<const Mesh *>(ob.data);
us->data.mesh_verts_num = ss.totvert;
us->data.mesh_verts_num = mesh.verts_num;
us->data.mesh_corners_num = mesh.corners_num;
break;
}

View File

@@ -369,10 +369,12 @@ static void stats_object_sculpt(const Object *ob, SceneStats *stats)
}
switch (pbvh->type()) {
case blender::bke::pbvh::Type::Mesh:
stats->totvertsculpt = ss->totvert;
stats->totfacesculpt = ss->totfaces;
case blender::bke::pbvh::Type::Mesh: {
const Mesh &mesh = *static_cast<const Mesh *>(ob->data);
stats->totvertsculpt = mesh.verts_num;
stats->totfacesculpt = mesh.faces_num;
break;
}
case blender::bke::pbvh::Type::BMesh:
stats->totvertsculpt = ob->sculpt->bm->totvert;
stats->tottri = ob->sculpt->bm->totface;

View File

@@ -231,8 +231,6 @@ static Mesh *modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh
sculpt_session->multires.active = true;
sculpt_session->multires.modifier = mmd;
sculpt_session->multires.level = mmd->sculptlvl;
sculpt_session->totvert = mesh->verts_num;
sculpt_session->faces_num = mesh->faces_num;
}
// blender::bke::subdiv::stats_print(&subdiv->stats);
}