Files
test2/source/blender/blenkernel/intern/multires_reshape_subdivide.cc
Hans Goudey 7966cd16d6 Mesh: Replace MPoly struct with offset indices
Implements #95967.

Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.

The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.

Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.

Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.

Some:
- The offset integer array has to be one longer than the face count to
  avoid a branch for every face, which means the data is no longer part
  of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
  until more reusable CoW from #104478 is committed. That will be added
  in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
  copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
  structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
  corners, but just in case, meshes with mismatched order are fixed by
  versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
  necessary here unfortunately. It should be worth it in 3.6, since
  that's the best way to allow loading meshes from 4.0, which is
  important for an LTS version.

Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00

96 lines
3.1 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2020 Blender Foundation */
/** \file
* \ingroup bke
*/
#include "MEM_guardedalloc.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_scene_types.h"
#include "BKE_customdata.h"
#include "BKE_lib_id.h"
#include "BKE_mesh.hh"
#include "BKE_mesh_runtime.h"
#include "BKE_modifier.h"
#include "BKE_multires.h"
#include "BKE_subdiv.h"
#include "BKE_subsurf.h"
#include "BLI_math_vector.h"
#include "DEG_depsgraph_query.h"
#include "multires_reshape.hh"
static void multires_subdivide_create_object_space_linear_grids(Mesh *mesh)
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = mesh->vert_positions();
const blender::OffsetIndices polys = mesh->polys();
const blender::Span<int> corner_verts = mesh->corner_verts();
MDisps *mdisps = static_cast<MDisps *>(
CustomData_get_layer_for_write(&mesh->ldata, CD_MDISPS, mesh->totloop));
for (const int p : polys.index_range()) {
const blender::IndexRange poly = polys[p];
const float3 poly_center = mesh::poly_center_calc(positions, corner_verts.slice(poly));
for (int l = 0; l < poly.size(); l++) {
const int loop_index = poly[l];
float(*disps)[3] = mdisps[loop_index].disps;
mdisps[loop_index].totdisp = 4;
mdisps[loop_index].level = 1;
int prev_loop_index = l - 1 >= 0 ? loop_index - 1 : loop_index + poly.size() - 1;
int next_loop_index = l + 1 < poly.size() ? loop_index + 1 : poly.start();
const int vert = corner_verts[loop_index];
const int vert_next = corner_verts[next_loop_index];
const int vert_prev = corner_verts[prev_loop_index];
copy_v3_v3(disps[0], poly_center);
mid_v3_v3v3(disps[1], positions[vert], positions[vert_next]);
mid_v3_v3v3(disps[2], positions[vert], positions[vert_prev]);
copy_v3_v3(disps[3], positions[vert]);
}
}
}
void multires_subdivide_create_tangent_displacement_linear_grids(Object *object,
MultiresModifierData *mmd)
{
Mesh *coarse_mesh = static_cast<Mesh *>(object->data);
multires_force_sculpt_rebuild(object);
MultiresReshapeContext reshape_context;
const int new_top_level = mmd->totlvl + 1;
const bool has_mdisps = CustomData_has_layer(&coarse_mesh->ldata, CD_MDISPS);
if (!has_mdisps) {
CustomData_add_layer(&coarse_mesh->ldata, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->totloop);
}
if (new_top_level == 1) {
/* No MDISPS. Create new grids for level 1 using the edges mid point and poly centers. */
multires_reshape_ensure_grids(coarse_mesh, 1);
multires_subdivide_create_object_space_linear_grids(coarse_mesh);
}
/* Convert the new grids to tangent displacement. */
multires_set_tot_level(object, mmd, new_top_level);
if (!multires_reshape_context_create_from_modifier(
&reshape_context, object, mmd, new_top_level)) {
return;
}
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
multires_reshape_context_free(&reshape_context);
}