2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-09 01:27:46 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2011-02-27 20:40:57 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-02-28 10:08:26 -05:00
|
|
|
#include <climits>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
2011-02-09 01:27:46 +00:00
|
|
|
|
2019-02-01 12:44:19 +11:00
|
|
|
#include "CLG_log.h"
|
|
|
|
|
|
2011-02-09 01:27:46 +00:00
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
2012-03-15 20:10:07 +00:00
|
|
|
#include "DNA_object_types.h"
|
2011-02-09 01:27:46 +00:00
|
|
|
|
2023-08-29 17:00:33 +02:00
|
|
|
#include "BLI_bitmap.h"
|
|
|
|
|
#include "BLI_map.hh"
|
2011-12-01 19:21:58 +00:00
|
|
|
#include "BLI_math_base.h"
|
2013-04-27 12:51:23 +00:00
|
|
|
#include "BLI_math_vector.h"
|
2023-08-29 17:00:33 +02:00
|
|
|
#include "BLI_ordered_edge.hh"
|
|
|
|
|
#include "BLI_sys_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2023-08-29 17:00:33 +02:00
|
|
|
#include "BLI_vector_set.hh"
|
2011-02-09 01:27:46 +00:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
#include "BKE_attribute.hh"
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
#include "BKE_customdata.h"
|
2012-03-15 20:10:07 +00:00
|
|
|
#include "BKE_deform.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2011-02-09 02:28:11 +00:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph.hh"
|
2017-04-06 16:11:50 +02:00
|
|
|
|
2011-02-09 01:27:46 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
using blender::float3;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
using blender::MutableSpan;
|
|
|
|
|
using blender::Span;
|
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* loop v/e are unsigned, so using max uint_32 value as invalid marker... */
|
|
|
|
|
#define INVALID_LOOP_EDGE_MARKER 4294967295u
|
|
|
|
|
|
2019-02-01 12:44:19 +11:00
|
|
|
static CLG_LogRef LOG = {"bke.mesh"};
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
void strip_loose_facesloops(Mesh *me, blender::BitSpan faces_to_remove);
|
2023-03-01 22:30:38 -05:00
|
|
|
void mesh_strip_edges(Mesh *me);
|
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Internal functions
|
|
|
|
|
* \{ */
|
2011-02-09 01:27:46 +00:00
|
|
|
|
2022-02-28 10:08:26 -05:00
|
|
|
union EdgeUUID {
|
2012-06-28 09:08:11 +00:00
|
|
|
uint32_t verts[2];
|
|
|
|
|
int64_t edval;
|
2022-02-28 10:08:26 -05:00
|
|
|
};
|
2012-06-28 09:08:11 +00:00
|
|
|
|
2022-02-28 10:08:26 -05:00
|
|
|
struct SortFace {
|
2018-03-23 11:51:19 +01:00
|
|
|
EdgeUUID es[4];
|
2021-06-18 14:27:41 +10:00
|
|
|
uint index;
|
2022-02-28 10:08:26 -05:00
|
|
|
};
|
2012-06-28 09:08:11 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Used to detect faces using exactly the same vertices. */
|
|
|
|
|
/* Used to detect loops used by no (disjoint) or more than one (intersect) faces. */
|
2022-02-28 10:08:26 -05:00
|
|
|
struct SortPoly {
|
2012-03-15 20:10:07 +00:00
|
|
|
int *verts;
|
|
|
|
|
int numverts;
|
|
|
|
|
int loopstart;
|
2021-06-18 14:27:41 +10:00
|
|
|
uint index;
|
2014-04-01 11:34:00 +11:00
|
|
|
bool invalid; /* Poly index. */
|
2022-02-28 10:08:26 -05:00
|
|
|
};
|
2012-03-15 20:10:07 +00:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
static void edge_store_assign(uint32_t verts[2], const uint32_t v1, const uint32_t v2)
|
|
|
|
|
{
|
|
|
|
|
if (v1 < v2) {
|
|
|
|
|
verts[0] = v1;
|
|
|
|
|
verts[1] = v2;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
verts[0] = v2;
|
|
|
|
|
verts[1] = v1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void edge_store_from_mface_quad(EdgeUUID es[4], MFace *mf)
|
|
|
|
|
{
|
|
|
|
|
edge_store_assign(es[0].verts, mf->v1, mf->v2);
|
|
|
|
|
edge_store_assign(es[1].verts, mf->v2, mf->v3);
|
|
|
|
|
edge_store_assign(es[2].verts, mf->v3, mf->v4);
|
|
|
|
|
edge_store_assign(es[3].verts, mf->v4, mf->v1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void edge_store_from_mface_tri(EdgeUUID es[4], MFace *mf)
|
|
|
|
|
{
|
|
|
|
|
edge_store_assign(es[0].verts, mf->v1, mf->v2);
|
|
|
|
|
edge_store_assign(es[1].verts, mf->v2, mf->v3);
|
|
|
|
|
edge_store_assign(es[2].verts, mf->v3, mf->v1);
|
|
|
|
|
es[3].verts[0] = es[3].verts[1] = UINT_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int int64_cmp(const void *v1, const void *v2)
|
|
|
|
|
{
|
|
|
|
|
const int64_t x1 = *(const int64_t *)v1;
|
|
|
|
|
const int64_t x2 = *(const int64_t *)v2;
|
|
|
|
|
|
|
|
|
|
if (x1 > x2) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
if (x1 < x2) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int search_face_cmp(const void *v1, const void *v2)
|
|
|
|
|
{
|
2022-02-28 10:08:26 -05:00
|
|
|
const SortFace *sfa = static_cast<const SortFace *>(v1);
|
|
|
|
|
const SortFace *sfb = static_cast<const SortFace *>(v2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
if (sfa->es[0].edval > sfb->es[0].edval) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[0].edval < sfb->es[0].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[1].edval > sfb->es[1].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[1].edval < sfb->es[1].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[2].edval > sfb->es[2].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[2].edval < sfb->es[2].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[3].edval > sfb->es[3].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
if (sfa->es[3].edval < sfb->es[3].edval) {
|
2012-06-28 09:08:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 23:08:40 +10:00
|
|
|
/* TODO: check there is not some standard define of this somewhere! */
|
2012-03-15 20:10:07 +00:00
|
|
|
static int int_cmp(const void *v1, const void *v2)
|
2011-02-09 15:13:20 +00:00
|
|
|
{
|
2012-04-18 09:16:30 +00:00
|
|
|
return *(int *)v1 > *(int *)v2 ? 1 : *(int *)v1 < *(int *)v2 ? -1 : 0;
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 20:10:07 +00:00
|
|
|
static int search_poly_cmp(const void *v1, const void *v2)
|
2011-02-09 01:27:46 +00:00
|
|
|
{
|
2022-02-28 10:08:26 -05:00
|
|
|
const SortPoly *sp1 = static_cast<const SortPoly *>(v1);
|
|
|
|
|
const SortPoly *sp2 = static_cast<const SortPoly *>(v2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Reject all invalid faces at end of list! */
|
2019-04-22 09:39:35 +10:00
|
|
|
if (sp1->invalid || sp2->invalid) {
|
2014-07-25 16:45:26 +02:00
|
|
|
return sp1->invalid ? (sp2->invalid ? 0 : 1) : -1;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Else, sort on first non-equal verts (remember verts of valid faces are sorted). */
|
2020-09-22 10:08:02 +02:00
|
|
|
const int max_idx = sp1->numverts > sp2->numverts ? sp2->numverts : sp1->numverts;
|
|
|
|
|
for (int idx = 0; idx < max_idx; idx++) {
|
2015-11-23 11:27:02 +11:00
|
|
|
const int v1_i = sp1->verts[idx];
|
|
|
|
|
const int v2_i = sp2->verts[idx];
|
|
|
|
|
if (v1_i != v2_i) {
|
|
|
|
|
return (v1_i > v2_i) ? 1 : -1;
|
2014-07-25 16:45:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sp1->numverts > sp2->numverts ? 1 : sp1->numverts < sp2->numverts ? -1 : 0;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 20:10:07 +00:00
|
|
|
static int search_polyloop_cmp(const void *v1, const void *v2)
|
2011-02-09 01:27:46 +00:00
|
|
|
{
|
2022-02-28 10:08:26 -05:00
|
|
|
const SortPoly *sp1 = static_cast<const SortPoly *>(v1);
|
|
|
|
|
const SortPoly *sp2 = static_cast<const SortPoly *>(v2);
|
2011-02-10 12:34:52 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Reject all invalid faces at end of list! */
|
2019-04-22 09:39:35 +10:00
|
|
|
if (sp1->invalid || sp2->invalid) {
|
2012-03-15 20:10:07 +00:00
|
|
|
return sp1->invalid && sp2->invalid ? 0 : sp1->invalid ? 1 : -1;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Else, sort on loopstart. */
|
|
|
|
|
return sp1->loopstart > sp2->loopstart ? 1 : sp1->loopstart < sp2->loopstart ? -1 : 0;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Validation
|
|
|
|
|
* \{ */
|
2011-02-09 01:27:46 +00:00
|
|
|
|
2019-02-01 12:44:19 +11:00
|
|
|
#define PRINT_MSG(...) \
|
2020-09-19 16:01:32 +10:00
|
|
|
if (do_verbose) { \
|
|
|
|
|
CLOG_INFO(&LOG, 1, __VA_ARGS__); \
|
|
|
|
|
} \
|
|
|
|
|
((void)0)
|
2013-09-04 01:29:34 +00:00
|
|
|
|
2019-02-01 12:44:19 +11:00
|
|
|
#define PRINT_ERR(...) \
|
|
|
|
|
do { \
|
|
|
|
|
is_valid = false; \
|
|
|
|
|
if (do_verbose) { \
|
|
|
|
|
CLOG_ERROR(&LOG, __VA_ARGS__); \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
2013-09-04 01:29:34 +00:00
|
|
|
|
2020-08-07 16:22:50 +02:00
|
|
|
/* NOLINTNEXTLINE: readability-function-size */
|
2018-07-13 08:36:10 +02:00
|
|
|
bool BKE_mesh_validate_arrays(Mesh *mesh,
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
float (*vert_positions)[3],
|
2021-06-18 14:27:41 +10:00
|
|
|
uint totvert,
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
blender::int2 *edges,
|
2021-06-18 14:27:41 +10:00
|
|
|
uint totedge,
|
2018-07-13 08:36:10 +02:00
|
|
|
MFace *mfaces,
|
2021-06-18 14:27:41 +10:00
|
|
|
uint totface,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
int *corner_verts,
|
|
|
|
|
int *corner_edges,
|
2021-06-18 14:27:41 +10:00
|
|
|
uint totloop,
|
2023-07-24 22:06:55 +02:00
|
|
|
int *face_offsets,
|
|
|
|
|
uint faces_num,
|
2018-07-13 08:36:10 +02:00
|
|
|
MDeformVert *dverts, /* assume totvert length */
|
|
|
|
|
const bool do_verbose,
|
|
|
|
|
const bool do_fixes,
|
|
|
|
|
bool *r_changed)
|
2011-02-09 01:27:46 +00:00
|
|
|
{
|
2023-08-29 17:00:33 +02:00
|
|
|
using namespace blender;
|
2019-01-03 15:52:07 +11:00
|
|
|
#define REMOVE_EDGE_TAG(_me) \
|
|
|
|
|
{ \
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
_me[0] = _me[1]; \
|
2019-01-03 15:52:07 +11:00
|
|
|
free_flag.edges = do_fixes; \
|
|
|
|
|
} \
|
|
|
|
|
(void)0
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
#define IS_REMOVED_EDGE(_me) (_me[0] == _me[1])
|
2012-03-15 20:10:07 +00:00
|
|
|
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
#define REMOVE_LOOP_TAG(corner) \
|
2019-01-03 15:52:07 +11:00
|
|
|
{ \
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
corner_edges[corner] = INVALID_LOOP_EDGE_MARKER; \
|
2019-01-03 15:52:07 +11:00
|
|
|
free_flag.polyloops = do_fixes; \
|
|
|
|
|
} \
|
|
|
|
|
(void)0
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::BitVector<> faces_to_remove(faces_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
blender::bke::AttributeWriter<int> material_indices =
|
2022-09-07 21:41:39 -05:00
|
|
|
mesh->attributes_for_write().lookup_for_write<int>("material_index");
|
2022-08-31 09:09:01 -05:00
|
|
|
blender::MutableVArraySpan<int> material_indices_span(material_indices.varray);
|
|
|
|
|
|
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
|
|
|
#if 0
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices<int> faces({face_offsets, faces_num + 1});
|
|
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
BLI_assert(faces[i].size() > 2);
|
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
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-06-18 14:27:41 +10:00
|
|
|
uint i, j;
|
2012-03-15 20:10:07 +00:00
|
|
|
int *v;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
bool is_valid = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
int verts : 1;
|
|
|
|
|
int verts_weight : 1;
|
2015-10-07 14:38:36 +11:00
|
|
|
int loops_edge : 1;
|
2015-02-24 13:08:07 +11:00
|
|
|
};
|
|
|
|
|
int as_flag;
|
|
|
|
|
} fix_flag;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
int edges : 1;
|
|
|
|
|
int faces : 1;
|
2023-07-24 22:06:55 +02:00
|
|
|
/* This regroups loops and faces! */
|
2015-02-24 13:08:07 +11:00
|
|
|
int polyloops : 1;
|
|
|
|
|
int mselect : 1;
|
|
|
|
|
};
|
|
|
|
|
int as_flag;
|
|
|
|
|
} free_flag;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
int edges : 1;
|
|
|
|
|
};
|
|
|
|
|
int as_flag;
|
|
|
|
|
} recalc_flag;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-08-29 17:00:33 +02:00
|
|
|
Map<OrderedEdge, int> edge_hash;
|
|
|
|
|
edge_hash.reserve(totedge);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-02-28 10:08:26 -05:00
|
|
|
BLI_assert(!(do_fixes && mesh == nullptr));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
fix_flag.as_flag = 0;
|
|
|
|
|
free_flag.as_flag = 0;
|
|
|
|
|
recalc_flag.as_flag = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
PRINT_MSG("verts(%u), edges(%u), loops(%u), polygons(%u)", totvert, totedge, totloop, faces_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (totedge == 0 && faces_num != 0) {
|
|
|
|
|
PRINT_ERR("\tLogical error, %u polygons and 0 edges", faces_num);
|
2015-02-24 13:08:07 +11:00
|
|
|
recalc_flag.edges = do_fixes;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
for (i = 0; i < totvert; i++) {
|
2012-04-18 09:16:30 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
if (!isfinite(vert_positions[i][j])) {
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tVertex %u: has invalid coordinate", i);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-09 07:23:17 +00:00
|
|
|
if (do_fixes) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
zero_v3(vert_positions[i]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
fix_flag.verts = true;
|
2011-12-01 19:21:58 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-12-01 19:21:58 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
for (i = 0; i < totedge; i++) {
|
|
|
|
|
blender::int2 &edge = edges[i];
|
2014-04-11 11:25:41 +10:00
|
|
|
bool remove = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
if (edge[0] == edge[1]) {
|
|
|
|
|
PRINT_ERR("\tEdge %u: has matching verts, both %d", i, edge[0]);
|
2012-04-18 09:16:30 +00:00
|
|
|
remove = do_fixes;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
if (edge[0] >= totvert) {
|
|
|
|
|
PRINT_ERR("\tEdge %u: v1 index out of range, %d", i, edge[0]);
|
2012-04-18 09:16:30 +00:00
|
|
|
remove = do_fixes;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
if (edge[1] >= totvert) {
|
|
|
|
|
PRINT_ERR("\tEdge %u: v2 index out of range, %d", i, edge[1]);
|
2012-04-18 09:16:30 +00:00
|
|
|
remove = do_fixes;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-08-29 17:00:33 +02:00
|
|
|
if ((edge[0] != edge[1]) && edge_hash.contains(edge)) {
|
|
|
|
|
PRINT_ERR("\tEdge %u: is a duplicate of %d", i, edge_hash.lookup(edge));
|
2012-04-18 09:16:30 +00:00
|
|
|
remove = do_fixes;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
if (remove == false) {
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
if (edge[0] != edge[1]) {
|
2023-08-29 17:00:33 +02:00
|
|
|
edge_hash.add(edge, i);
|
2015-02-26 17:47:41 +11:00
|
|
|
}
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-03-01 15:57:50 -05:00
|
|
|
REMOVE_EDGE_TAG(edge);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (mfaces && !face_offsets) {
|
2015-02-24 13:08:07 +11:00
|
|
|
#define REMOVE_FACE_TAG(_mf) \
|
|
|
|
|
{ \
|
|
|
|
|
_mf->v3 = 0; \
|
|
|
|
|
free_flag.faces = do_fixes; \
|
|
|
|
|
} \
|
|
|
|
|
(void)0
|
2012-06-28 09:08:11 +00:00
|
|
|
#define CHECK_FACE_VERT_INDEX(a, b) \
|
|
|
|
|
if (mf->a == mf->b) { \
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR(" face %u: verts invalid, " STRINGIFY(a) "/" STRINGIFY(b) " both %u", i, mf->a); \
|
2012-06-28 09:08:11 +00:00
|
|
|
remove = do_fixes; \
|
|
|
|
|
} \
|
|
|
|
|
(void)0
|
|
|
|
|
#define CHECK_FACE_EDGE(a, b) \
|
2023-08-29 17:00:33 +02:00
|
|
|
if (!edge_hash.contains({mf->a, mf->b})) { \
|
2013-09-04 01:29:34 +00:00
|
|
|
PRINT_ERR(" face %u: edge " STRINGIFY(a) "/" STRINGIFY(b) " (%u,%u) is missing edge data", \
|
2019-02-01 12:44:19 +11:00
|
|
|
i, \
|
|
|
|
|
mf->a, \
|
|
|
|
|
mf->b); \
|
2015-02-24 13:08:07 +11:00
|
|
|
recalc_flag.edges = do_fixes; \
|
2012-07-05 13:02:42 +00:00
|
|
|
} \
|
|
|
|
|
(void)0
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
MFace *mf;
|
|
|
|
|
MFace *mf_prev;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-02-28 10:08:26 -05:00
|
|
|
SortFace *sort_faces = (SortFace *)MEM_callocN(sizeof(SortFace) * totface, "search faces");
|
2012-06-28 09:08:11 +00:00
|
|
|
SortFace *sf;
|
|
|
|
|
SortFace *sf_prev;
|
2021-06-18 14:27:41 +10:00
|
|
|
uint totsortface = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("No Polys, only tessellated Faces");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
for (i = 0, mf = mfaces, sf = sort_faces; i < totface; i++, mf++) {
|
2014-04-11 11:25:41 +10:00
|
|
|
bool remove = false;
|
2012-06-28 09:08:11 +00:00
|
|
|
int fidx;
|
2021-06-18 14:27:41 +10:00
|
|
|
uint fv[4];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
fidx = mf->v4 ? 3 : 2;
|
|
|
|
|
do {
|
|
|
|
|
fv[fidx] = *(&(mf->v1) + fidx);
|
|
|
|
|
if (fv[fidx] >= totvert) {
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tFace %u: 'v%d' index out of range, %u", i, fidx + 1, fv[fidx]);
|
2012-06-28 09:08:11 +00:00
|
|
|
remove = do_fixes;
|
|
|
|
|
}
|
|
|
|
|
} while (fidx--);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
if (remove == false) {
|
2012-06-28 09:08:11 +00:00
|
|
|
if (mf->v4) {
|
|
|
|
|
CHECK_FACE_VERT_INDEX(v1, v2);
|
|
|
|
|
CHECK_FACE_VERT_INDEX(v1, v3);
|
|
|
|
|
CHECK_FACE_VERT_INDEX(v1, v4);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
CHECK_FACE_VERT_INDEX(v2, v3);
|
|
|
|
|
CHECK_FACE_VERT_INDEX(v2, v4);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
CHECK_FACE_VERT_INDEX(v3, v4);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CHECK_FACE_VERT_INDEX(v1, v2);
|
|
|
|
|
CHECK_FACE_VERT_INDEX(v1, v3);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
CHECK_FACE_VERT_INDEX(v2, v3);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-01 11:34:00 +11:00
|
|
|
if (remove == false) {
|
2012-06-28 09:08:11 +00:00
|
|
|
if (totedge) {
|
|
|
|
|
if (mf->v4) {
|
|
|
|
|
CHECK_FACE_EDGE(v1, v2);
|
|
|
|
|
CHECK_FACE_EDGE(v2, v3);
|
|
|
|
|
CHECK_FACE_EDGE(v3, v4);
|
|
|
|
|
CHECK_FACE_EDGE(v4, v1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CHECK_FACE_EDGE(v1, v2);
|
|
|
|
|
CHECK_FACE_EDGE(v2, v3);
|
|
|
|
|
CHECK_FACE_EDGE(v3, v1);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
sf->index = i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
if (mf->v4) {
|
|
|
|
|
edge_store_from_mface_quad(sf->es, mf);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
qsort(sf->es, 4, sizeof(int64_t), int64_cmp);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
edge_store_from_mface_tri(sf->es, mf);
|
|
|
|
|
qsort(sf->es, 3, sizeof(int64_t), int64_cmp);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
totsortface++;
|
|
|
|
|
sf++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
if (remove) {
|
|
|
|
|
REMOVE_FACE_TAG(mf);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-06-28 09:08:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
qsort(sort_faces, totsortface, sizeof(SortFace), search_face_cmp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
sf = sort_faces;
|
|
|
|
|
sf_prev = sf;
|
|
|
|
|
sf++;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
for (i = 1; i < totsortface; i++, sf++) {
|
2014-04-11 11:25:41 +10:00
|
|
|
bool remove = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
/* on a valid mesh, code below will never run */
|
|
|
|
|
if (memcmp(sf->es, sf_prev->es, sizeof(sf_prev->es)) == 0) {
|
|
|
|
|
mf = mfaces + sf->index;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
if (do_verbose) {
|
|
|
|
|
mf_prev = mfaces + sf_prev->index;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
if (mf->v4) {
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tFace %u & %u: are duplicates (%u,%u,%u,%u) (%u,%u,%u,%u)",
|
2013-09-04 01:29:34 +00:00
|
|
|
sf->index,
|
|
|
|
|
sf_prev->index,
|
|
|
|
|
mf->v1,
|
|
|
|
|
mf->v2,
|
|
|
|
|
mf->v3,
|
|
|
|
|
mf->v4,
|
|
|
|
|
mf_prev->v1,
|
|
|
|
|
mf_prev->v2,
|
|
|
|
|
mf_prev->v3,
|
|
|
|
|
mf_prev->v4);
|
2012-06-28 09:08:11 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tFace %u & %u: are duplicates (%u,%u,%u) (%u,%u,%u)",
|
2013-09-04 01:29:34 +00:00
|
|
|
sf->index,
|
|
|
|
|
sf_prev->index,
|
|
|
|
|
mf->v1,
|
|
|
|
|
mf->v2,
|
|
|
|
|
mf->v3,
|
|
|
|
|
mf_prev->v1,
|
|
|
|
|
mf_prev->v2,
|
|
|
|
|
mf_prev->v3);
|
2012-06-28 09:08:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
remove = do_fixes;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
sf_prev = sf;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
if (remove) {
|
|
|
|
|
REMOVE_FACE_TAG(mf);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
MEM_freeN(sort_faces);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-28 09:08:11 +00:00
|
|
|
#undef REMOVE_FACE_TAG
|
|
|
|
|
#undef CHECK_FACE_VERT_INDEX
|
|
|
|
|
#undef CHECK_FACE_EDGE
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Checking loops and faces is a bit tricky, as they are quite intricate...
|
2012-03-15 20:10:07 +00:00
|
|
|
*
|
|
|
|
|
* Polys must have:
|
|
|
|
|
* - a valid loopstart value.
|
|
|
|
|
* - a valid totloop value (>= 3 and loopstart+totloop < me.totloop).
|
|
|
|
|
*
|
|
|
|
|
* Loops must have:
|
|
|
|
|
* - a valid v value.
|
2023-07-24 22:06:55 +02:00
|
|
|
* - a valid e value (corresponding to the edge it defines with the next loop in face).
|
2012-03-15 20:10:07 +00:00
|
|
|
*
|
2023-07-24 22:06:55 +02:00
|
|
|
* Also, loops not used by faces can be discarded.
|
|
|
|
|
* And "intersecting" loops (i.e. loops used by more than one face) are invalid,
|
|
|
|
|
* so be sure to leave at most one face per loop!
|
2012-03-15 20:10:07 +00:00
|
|
|
*/
|
|
|
|
|
{
|
2022-01-28 22:40:13 -06:00
|
|
|
BLI_bitmap *vert_tag = BLI_BITMAP_NEW(mesh->totvert, __func__);
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
SortPoly *sort_polys = (SortPoly *)MEM_callocN(sizeof(SortPoly) * faces_num,
|
2022-02-28 10:08:26 -05:00
|
|
|
"mesh validate's sort_polys");
|
2012-03-15 20:10:07 +00:00
|
|
|
SortPoly *prev_sp, *sp = sort_polys;
|
|
|
|
|
int prev_end;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t i : blender::IndexRange(faces_num)) {
|
|
|
|
|
const int poly_start = face_offsets[i];
|
|
|
|
|
const int poly_size = face_offsets[i + 1] - poly_start;
|
2012-03-15 20:10:07 +00:00
|
|
|
sp->index = i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-03 14:44:02 +10:00
|
|
|
/* Material index, isolated from other tests here. While large indices are clamped,
|
|
|
|
|
* negative indices aren't supported by drawing, exporters etc.
|
|
|
|
|
* To check the indices are in range, use #BKE_mesh_validate_material_indices */
|
2022-08-31 09:09:01 -05:00
|
|
|
if (material_indices && material_indices_span[i] < 0) {
|
|
|
|
|
PRINT_ERR("\tPoly %u has invalid material (%d)", sp->index, material_indices_span[i]);
|
2020-08-03 14:44:02 +10:00
|
|
|
if (do_fixes) {
|
2022-08-31 09:09:01 -05:00
|
|
|
material_indices_span[i] = 0;
|
2020-08-03 14:44:02 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
if (poly_start < 0 || poly_size < 3) {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Invalid loop data. */
|
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
|
|
|
PRINT_ERR(
|
|
|
|
|
"\tPoly %u is invalid (loopstart: %d, totloop: %d)", sp->index, poly_start, poly_size);
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = true;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
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
|
|
|
else if (poly_start + poly_size > totloop) {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Invalid loop data. */
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR(
|
2022-03-25 12:04:20 +11:00
|
|
|
"\tPoly %u uses loops out of range "
|
|
|
|
|
"(loopstart: %d, loopend: %d, max number of loops: %u)",
|
2013-09-04 01:29:34 +00:00
|
|
|
sp->index,
|
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
|
|
|
poly_start,
|
|
|
|
|
poly_start + poly_size - 1,
|
2014-04-01 11:34:00 +11:00
|
|
|
totloop - 1);
|
|
|
|
|
sp->invalid = true;
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Poly itself is valid, for now. */
|
|
|
|
|
int v1, v2; /* v1 is prev loop vert idx, v2 is current loop one. */
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = false;
|
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
|
|
|
sp->verts = v = (int *)MEM_mallocN(sizeof(int) * poly_size, "Vert idx of SortPoly");
|
|
|
|
|
sp->numverts = poly_size;
|
|
|
|
|
sp->loopstart = poly_start;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-27 12:07:07 +10:00
|
|
|
/* Ideally we would only have to do that once on all vertices
|
2023-07-24 22:06:55 +02:00
|
|
|
* before we start checking each face, but several faces can use same vert,
|
|
|
|
|
* so we have to ensure here all verts of current face are cleared. */
|
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
|
|
|
for (j = 0; j < poly_size; j++) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int vert = corner_verts[sp->loopstart + j];
|
|
|
|
|
if (vert < totvert) {
|
|
|
|
|
BLI_BITMAP_DISABLE(vert_tag, vert);
|
2015-11-15 17:11:19 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Test all face's loops' vert idx. */
|
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
|
|
|
for (j = 0; j < poly_size; j++, v++) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int vert = corner_verts[sp->loopstart + j];
|
|
|
|
|
if (vert >= totvert) {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Invalid vert idx. */
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
PRINT_ERR("\tLoop %u has invalid vert reference (%d)", sp->loopstart + j, vert);
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = true;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
else if (BLI_BITMAP_TEST(vert_tag, vert)) {
|
2023-03-03 10:50:54 -05:00
|
|
|
PRINT_ERR("\tPoly %u has duplicated vert reference at corner (%u)", uint(i), j);
|
2015-11-15 17:11:19 +01:00
|
|
|
sp->invalid = true;
|
|
|
|
|
}
|
2014-08-19 21:09:10 +10:00
|
|
|
else {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
BLI_BITMAP_ENABLE(vert_tag, vert);
|
2014-08-19 21:09:10 +10:00
|
|
|
}
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
*v = vert;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (sp->invalid) {
|
2023-03-03 10:50:54 -05:00
|
|
|
sp++;
|
2012-03-15 20:10:07 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Test all face's loops. */
|
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
|
|
|
for (j = 0; j < poly_size; j++) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int corner = sp->loopstart + j;
|
|
|
|
|
const int vert = corner_verts[corner];
|
|
|
|
|
const int edge_i = corner_edges[corner];
|
|
|
|
|
v1 = vert;
|
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
|
|
|
v2 = corner_verts[sp->loopstart + (j + 1) % poly_size];
|
2023-08-29 17:00:33 +02:00
|
|
|
if (!edge_hash.contains({v1, v2})) {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Edge not existing. */
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tPoly %u needs missing edge (%d, %d)", sp->index, v1, v2);
|
2019-04-22 09:39:35 +10:00
|
|
|
if (do_fixes) {
|
2015-02-24 13:08:07 +11:00
|
|
|
recalc_flag.edges = true;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = true;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
else if (edge_i >= totedge) {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Invalid edge idx.
|
|
|
|
|
* We already know from previous text that a valid edge exists, use it (if allowed)! */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (do_fixes) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
int prev_e = edge_i;
|
2023-08-29 17:00:33 +02:00
|
|
|
corner_edges[corner] = edge_hash.lookup({v1, v2});
|
2015-10-07 14:38:36 +11:00
|
|
|
fix_flag.loops_edge = true;
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
PRINT_ERR("\tLoop %d has invalid edge reference (%d), fixed using edge %d",
|
|
|
|
|
corner,
|
2013-09-04 01:29:34 +00:00
|
|
|
prev_e,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
corner_edges[corner]);
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
PRINT_ERR("\tLoop %d has invalid edge reference (%d)", corner, edge_i);
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = true;
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-02-09 15:13:20 +00:00
|
|
|
else {
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
const blender::int2 &edge = edges[edge_i];
|
2023-03-01 15:57:50 -05:00
|
|
|
if (IS_REMOVED_EDGE(edge) ||
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
!((edge[0] == v1 && edge[1] == v2) || (edge[0] == v2 && edge[1] == v1)))
|
|
|
|
|
{
|
2012-03-15 20:10:07 +00:00
|
|
|
/* The pointed edge is invalid (tagged as removed, or vert idx mismatch),
|
2019-04-27 12:07:07 +10:00
|
|
|
* and we already know from previous test that a valid one exists,
|
|
|
|
|
* use it (if allowed)! */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (do_fixes) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
int prev_e = edge_i;
|
2023-08-29 17:00:33 +02:00
|
|
|
corner_edges[corner] = edge_hash.lookup({v1, v2});
|
2015-10-07 14:38:36 +11:00
|
|
|
fix_flag.loops_edge = true;
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR(
|
|
|
|
|
"\tPoly %u has invalid edge reference (%d, is_removed: %d), fixed using edge "
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
"%d",
|
2017-05-26 21:48:18 +02:00
|
|
|
sp->index,
|
|
|
|
|
prev_e,
|
2023-03-01 15:57:50 -05:00
|
|
|
IS_REMOVED_EDGE(edge),
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
corner_edges[corner]);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-02-01 12:44:19 +11:00
|
|
|
else {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
PRINT_ERR("\tPoly %u has invalid edge reference (%d)", sp->index, edge_i);
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = true;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 20:10:07 +00:00
|
|
|
if (!sp->invalid) {
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Needed for checking faces using same verts below. */
|
2015-11-15 17:11:19 +01:00
|
|
|
qsort(sp->verts, sp->numverts, sizeof(int), int_cmp);
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-03 10:50:54 -05:00
|
|
|
sp++;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-28 22:40:13 -06:00
|
|
|
MEM_freeN(vert_tag);
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Second check pass, testing faces using the same verts. */
|
|
|
|
|
qsort(sort_polys, faces_num, sizeof(SortPoly), search_poly_cmp);
|
2012-03-15 20:10:07 +00:00
|
|
|
sp = prev_sp = sort_polys;
|
|
|
|
|
sp++;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (i = 1; i < faces_num; i++, sp++) {
|
2012-03-15 20:10:07 +00:00
|
|
|
int p1_nv = sp->numverts, p2_nv = prev_sp->numverts;
|
2014-04-27 00:20:13 +10:00
|
|
|
const int *p1_v = sp->verts, *p2_v = prev_sp->verts;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-11-15 17:11:19 +01:00
|
|
|
if (sp->invalid) {
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Break, because all known invalid faces have been put at the end
|
2019-04-27 12:07:07 +10:00
|
|
|
* by qsort with search_poly_cmp. */
|
2012-03-15 20:10:07 +00:00
|
|
|
break;
|
2015-11-15 17:11:19 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Test same faces. */
|
2012-03-24 06:18:31 +00:00
|
|
|
if ((p1_nv == p2_nv) && (memcmp(p1_v, p2_v, p1_nv * sizeof(*p1_v)) == 0)) {
|
2012-03-15 20:10:07 +00:00
|
|
|
if (do_verbose) {
|
2019-02-01 12:44:19 +11:00
|
|
|
/* TODO: convert list to string */
|
2015-06-19 12:29:06 +02:00
|
|
|
PRINT_ERR("\tPolys %u and %u use same vertices (%d", prev_sp->index, sp->index, *p1_v);
|
2019-04-22 09:39:35 +10:00
|
|
|
for (j = 1; j < p1_nv; j++) {
|
2015-06-19 12:29:06 +02:00
|
|
|
PRINT_ERR(", %d", p1_v[j]);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
PRINT_ERR("), considering face %u as invalid.", sp->index);
|
2013-09-04 01:29:34 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
is_valid = false;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
2014-04-01 11:34:00 +11:00
|
|
|
sp->invalid = true;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
prev_sp = sp;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Third check pass, testing loops used by none or more than one face. */
|
|
|
|
|
qsort(sort_polys, faces_num, sizeof(SortPoly), search_polyloop_cmp);
|
2012-03-15 20:10:07 +00:00
|
|
|
sp = sort_polys;
|
2022-02-28 10:08:26 -05:00
|
|
|
prev_sp = nullptr;
|
2012-03-15 20:10:07 +00:00
|
|
|
prev_end = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (i = 0; i < faces_num; i++, sp++) {
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Free this now, we don't need it anymore, and avoid us another loop! */
|
2019-04-22 09:39:35 +10:00
|
|
|
if (sp->verts) {
|
2012-03-15 20:10:07 +00:00
|
|
|
MEM_freeN(sp->verts);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Note above prev_sp: in following code, we make sure it is always valid face (or nullptr).
|
2022-02-28 10:08:26 -05:00
|
|
|
*/
|
2012-03-15 20:10:07 +00:00
|
|
|
if (sp->invalid) {
|
|
|
|
|
if (do_fixes) {
|
2023-07-24 22:06:55 +02:00
|
|
|
faces_to_remove[sp->index].set();
|
2023-04-14 14:24:42 -04:00
|
|
|
free_flag.polyloops = do_fixes;
|
2012-03-15 20:10:07 +00:00
|
|
|
/* DO NOT REMOVE ITS LOOPS!!!
|
2023-07-24 22:06:55 +02:00
|
|
|
* As already invalid faces are at the end of the SortPoly list, the loops they
|
2012-03-15 20:10:07 +00:00
|
|
|
* were the only users have already been tagged as "to remove" during previous
|
2012-04-21 14:14:58 +00:00
|
|
|
* iterations, and we don't want to remove some loops that may be used by
|
2023-07-24 22:06:55 +02:00
|
|
|
* another valid face! */
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-03-15 20:10:07 +00:00
|
|
|
/* Test loops users. */
|
|
|
|
|
else {
|
|
|
|
|
/* Unused loops. */
|
|
|
|
|
if (prev_end < sp->loopstart) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
int corner;
|
|
|
|
|
for (j = prev_end, corner = prev_end; j < sp->loopstart; j++, corner++) {
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tLoop %u is unused.", j);
|
2019-04-22 09:39:35 +10:00
|
|
|
if (do_fixes) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
REMOVE_LOOP_TAG(corner);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
prev_end = sp->loopstart + sp->numverts;
|
|
|
|
|
prev_sp = sp;
|
|
|
|
|
}
|
|
|
|
|
/* Multi-used loops. */
|
|
|
|
|
else if (prev_end > sp->loopstart) {
|
2023-07-24 22:06:55 +02:00
|
|
|
PRINT_ERR("\tPolys %u and %u share loops from %d to %d, considering face %u as invalid.",
|
2013-09-04 01:29:34 +00:00
|
|
|
prev_sp->index,
|
|
|
|
|
sp->index,
|
|
|
|
|
sp->loopstart,
|
|
|
|
|
prev_end,
|
|
|
|
|
sp->index);
|
2012-03-15 20:10:07 +00:00
|
|
|
if (do_fixes) {
|
2023-07-24 22:06:55 +02:00
|
|
|
faces_to_remove[sp->index].set();
|
2023-04-14 14:24:42 -04:00
|
|
|
free_flag.polyloops = do_fixes;
|
2012-03-15 20:10:07 +00:00
|
|
|
/* DO NOT REMOVE ITS LOOPS!!!
|
2023-07-24 22:06:55 +02:00
|
|
|
* They might be used by some next, valid face!
|
2012-03-15 20:10:07 +00:00
|
|
|
* Just not updating prev_end/prev_sp vars is enough to ensure the loops
|
|
|
|
|
* effectively no more needed will be marked as "to be removed"! */
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-02-10 14:13:13 +00:00
|
|
|
else {
|
2012-03-15 20:10:07 +00:00
|
|
|
prev_end = sp->loopstart + sp->numverts;
|
|
|
|
|
prev_sp = sp;
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-03-15 20:10:07 +00:00
|
|
|
/* We may have some remaining unused loops to get rid of! */
|
|
|
|
|
if (prev_end < totloop) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
int corner;
|
|
|
|
|
for (j = prev_end, corner = prev_end; j < totloop; j++, corner++) {
|
2019-02-01 12:44:19 +11:00
|
|
|
PRINT_ERR("\tLoop %u is unused.", j);
|
2019-04-22 09:39:35 +10:00
|
|
|
if (do_fixes) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
REMOVE_LOOP_TAG(corner);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 20:10:07 +00:00
|
|
|
MEM_freeN(sort_polys);
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-08 04:51:03 +00:00
|
|
|
/* fix deform verts */
|
|
|
|
|
if (dverts) {
|
|
|
|
|
MDeformVert *dv;
|
2012-04-18 09:16:30 +00:00
|
|
|
for (i = 0, dv = dverts; i < totvert; i++, dv++) {
|
2011-12-09 20:29:21 +00:00
|
|
|
MDeformWeight *dw;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-18 09:16:30 +00:00
|
|
|
for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: greater than max defgroups is accounted for in our code, but not < 0. */
|
2016-05-16 00:48:02 +02:00
|
|
|
if (!isfinite(dw->weight)) {
|
2020-02-11 12:35:10 +11:00
|
|
|
PRINT_ERR("\tVertex deform %u, group %u has weight: %f", i, dw->def_nr, dw->weight);
|
2011-12-08 04:51:03 +00:00
|
|
|
if (do_fixes) {
|
2012-04-18 09:16:30 +00:00
|
|
|
dw->weight = 0.0f;
|
2015-02-24 13:08:07 +11:00
|
|
|
fix_flag.verts_weight = true;
|
2011-12-08 04:51:03 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-12-09 20:29:21 +00:00
|
|
|
else if (dw->weight < 0.0f || dw->weight > 1.0f) {
|
2020-02-11 12:35:10 +11:00
|
|
|
PRINT_ERR("\tVertex deform %u, group %u has weight: %f", i, dw->def_nr, dw->weight);
|
2011-12-09 20:29:21 +00:00
|
|
|
if (do_fixes) {
|
|
|
|
|
CLAMP(dw->weight, 0.0f, 1.0f);
|
2015-02-24 13:08:07 +11:00
|
|
|
fix_flag.verts_weight = true;
|
2011-12-08 04:51:03 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-11 12:35:10 +11:00
|
|
|
/* Not technically incorrect since this is unsigned, however,
|
2021-06-18 14:27:41 +10:00
|
|
|
* a value over INT_MAX is almost certainly caused by wrapping an uint. */
|
2020-02-11 12:35:10 +11:00
|
|
|
if (dw->def_nr >= INT_MAX) {
|
|
|
|
|
PRINT_ERR("\tVertex deform %u, has invalid group %u", i, dw->def_nr);
|
2011-12-08 04:51:03 +00:00
|
|
|
if (do_fixes) {
|
2020-03-06 12:50:56 +11:00
|
|
|
BKE_defvert_remove_group(dv, dw);
|
2015-02-24 13:08:07 +11:00
|
|
|
fix_flag.verts_weight = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-08 04:51:03 +00:00
|
|
|
if (dv->dw) {
|
|
|
|
|
/* re-allocated, the new values compensate for stepping
|
|
|
|
|
* within the for loop and may not be valid */
|
|
|
|
|
j--;
|
2012-04-18 09:16:30 +00:00
|
|
|
dw = dv->dw + j;
|
2011-12-08 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
else { /* all freed */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-12-08 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-18 09:16:30 +00:00
|
|
|
#undef REMOVE_EDGE_TAG
|
|
|
|
|
#undef IS_REMOVED_EDGE
|
|
|
|
|
#undef REMOVE_LOOP_TAG
|
|
|
|
|
#undef REMOVE_POLY_TAG
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (mesh) {
|
2015-02-24 13:08:07 +11:00
|
|
|
if (free_flag.faces) {
|
2012-06-28 09:08:11 +00:00
|
|
|
BKE_mesh_strip_loose_faces(mesh);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
if (free_flag.polyloops) {
|
2023-07-24 22:06:55 +02:00
|
|
|
strip_loose_facesloops(mesh, faces_to_remove);
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
if (free_flag.edges) {
|
2023-03-01 22:30:38 -05:00
|
|
|
mesh_strip_edges(mesh);
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
if (recalc_flag.edges) {
|
2013-03-16 01:19:03 +00:00
|
|
|
BKE_mesh_calc_edges(mesh, true, false);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-02 09:57:31 +00:00
|
|
|
if (mesh && mesh->mselect) {
|
|
|
|
|
MSelect *msel;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-02 09:57:31 +00:00
|
|
|
for (i = 0, msel = mesh->mselect; i < mesh->totselect; i++, msel++) {
|
2012-07-05 13:02:42 +00:00
|
|
|
int tot_elem = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-02 09:57:31 +00:00
|
|
|
if (msel->index < 0) {
|
2015-06-19 12:29:06 +02:00
|
|
|
PRINT_ERR(
|
|
|
|
|
"\tMesh select element %u type %d index is negative, "
|
2013-09-04 01:29:34 +00:00
|
|
|
"resetting selection stack.\n",
|
|
|
|
|
i,
|
|
|
|
|
msel->type);
|
2015-02-24 13:08:07 +11:00
|
|
|
free_flag.mselect = do_fixes;
|
2012-07-02 09:57:31 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-02 09:57:31 +00:00
|
|
|
switch (msel->type) {
|
|
|
|
|
case ME_VSEL:
|
|
|
|
|
tot_elem = mesh->totvert;
|
|
|
|
|
break;
|
|
|
|
|
case ME_ESEL:
|
|
|
|
|
tot_elem = mesh->totedge;
|
|
|
|
|
break;
|
|
|
|
|
case ME_FSEL:
|
2023-07-24 22:06:55 +02:00
|
|
|
tot_elem = mesh->faces_num;
|
2012-07-02 09:57:31 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-02 09:57:31 +00:00
|
|
|
if (msel->index > tot_elem) {
|
2015-06-19 12:29:06 +02:00
|
|
|
PRINT_ERR(
|
|
|
|
|
"\tMesh select element %u type %d index %d is larger than data array size %d, "
|
2013-09-04 01:29:34 +00:00
|
|
|
"resetting selection stack.\n",
|
|
|
|
|
i,
|
|
|
|
|
msel->type,
|
|
|
|
|
msel->index,
|
|
|
|
|
tot_elem);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
free_flag.mselect = do_fixes;
|
2012-07-02 09:57:31 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
if (free_flag.mselect) {
|
2012-07-02 09:57:31 +00:00
|
|
|
MEM_freeN(mesh->mselect);
|
2022-02-28 10:08:26 -05:00
|
|
|
mesh->mselect = nullptr;
|
2012-07-02 09:57:31 +00:00
|
|
|
mesh->totselect = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
material_indices_span.save();
|
|
|
|
|
material_indices.finish();
|
|
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
PRINT_MSG("%s: finished\n\n", __func__);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-24 13:08:07 +11:00
|
|
|
*r_changed = (fix_flag.as_flag || free_flag.as_flag || recalc_flag.as_flag);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-11-15 17:11:19 +01:00
|
|
|
BLI_assert((*r_changed == false) || (do_fixes == true));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
return is_valid;
|
2011-02-09 01:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-13 08:36:10 +02:00
|
|
|
static bool mesh_validate_customdata(CustomData *data,
|
2022-06-01 14:38:06 +10:00
|
|
|
eCustomDataMask mask,
|
2018-12-03 16:19:08 +01:00
|
|
|
const uint totitems,
|
2018-07-13 08:36:10 +02:00
|
|
|
const bool do_verbose,
|
|
|
|
|
const bool do_fixes,
|
|
|
|
|
bool *r_change)
|
2011-10-23 17:52:20 +00:00
|
|
|
{
|
2013-09-04 01:29:34 +00:00
|
|
|
bool is_valid = true;
|
|
|
|
|
bool has_fixes = false;
|
|
|
|
|
int i = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
PRINT_MSG("%s: Checking %d CD layers...\n", __func__, data->totlayer);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-23 13:55:51 +11:00
|
|
|
/* Set dummy values so the layer-type is always initialized on first access. */
|
|
|
|
|
int layer_num = -1;
|
|
|
|
|
int layer_num_type = -1;
|
|
|
|
|
|
2012-04-18 09:16:30 +00:00
|
|
|
while (i < data->totlayer) {
|
|
|
|
|
CustomDataLayer *layer = &data->layers[i];
|
2023-03-29 17:10:49 +02:00
|
|
|
const eCustomDataType type = eCustomDataType(layer->type);
|
2013-09-04 01:29:34 +00:00
|
|
|
bool ok = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-23 13:55:51 +11:00
|
|
|
/* Count layers when the type changes. */
|
2023-03-29 17:10:49 +02:00
|
|
|
if (layer_num_type != type) {
|
|
|
|
|
layer_num = CustomData_number_of_layers(data, type);
|
|
|
|
|
layer_num_type = type;
|
2023-03-23 13:55:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Validate active index, for a time this could be set to a negative value, see: #105860. */
|
|
|
|
|
int *active_index_array[] = {
|
|
|
|
|
&layer->active,
|
|
|
|
|
&layer->active_rnd,
|
|
|
|
|
&layer->active_clone,
|
|
|
|
|
&layer->active_mask,
|
|
|
|
|
};
|
|
|
|
|
for (int *active_index : Span(active_index_array, ARRAY_SIZE(active_index_array))) {
|
|
|
|
|
if (*active_index < 0) {
|
|
|
|
|
PRINT_ERR("\tCustomDataLayer type %d has a negative active index (%d)\n",
|
|
|
|
|
layer->type,
|
|
|
|
|
*active_index);
|
|
|
|
|
if (do_fixes) {
|
|
|
|
|
*active_index = 0;
|
|
|
|
|
has_fixes = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (*active_index >= layer_num) {
|
|
|
|
|
PRINT_ERR("\tCustomDataLayer type %d has an out of bounds active index (%d >= %d)\n",
|
|
|
|
|
layer->type,
|
|
|
|
|
*active_index,
|
|
|
|
|
layer_num);
|
|
|
|
|
if (do_fixes) {
|
|
|
|
|
BLI_assert(layer_num > 0);
|
|
|
|
|
*active_index = layer_num - 1;
|
|
|
|
|
has_fixes = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 17:10:49 +02:00
|
|
|
if (CustomData_layertype_is_singleton(type)) {
|
2023-03-23 13:55:51 +11:00
|
|
|
if (layer_num > 1) {
|
2013-09-04 01:29:34 +00:00
|
|
|
PRINT_ERR("\tCustomDataLayer type %d is a singleton, found %d in Mesh structure\n",
|
2023-03-29 17:10:49 +02:00
|
|
|
type,
|
2023-03-23 13:55:51 +11:00
|
|
|
layer_num);
|
2013-09-04 01:29:34 +00:00
|
|
|
ok = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
if (mask != 0) {
|
2023-03-29 17:10:49 +02:00
|
|
|
eCustomDataMask layer_typemask = CD_TYPE_AS_MASK(type);
|
2013-09-04 01:29:34 +00:00
|
|
|
if ((layer_typemask & mask) == 0) {
|
2023-03-29 17:10:49 +02:00
|
|
|
PRINT_ERR("\tCustomDataLayer type %d which isn't in the mask\n", type);
|
2013-09-04 01:29:34 +00:00
|
|
|
ok = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
if (ok == false) {
|
2012-03-24 06:18:31 +00:00
|
|
|
if (do_fixes) {
|
2023-03-29 17:10:49 +02:00
|
|
|
CustomData_free_layer(data, type, 0, i);
|
2013-09-04 01:29:34 +00:00
|
|
|
has_fixes = true;
|
2011-10-23 17:52:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-12-03 16:19:08 +01:00
|
|
|
if (ok) {
|
|
|
|
|
if (CustomData_layer_validate(layer, totitems, do_fixes)) {
|
2023-03-29 17:10:49 +02:00
|
|
|
PRINT_ERR("\tCustomDataLayer type %d has some invalid data\n", type);
|
2018-12-03 16:19:08 +01:00
|
|
|
has_fixes = do_fixes;
|
|
|
|
|
}
|
2011-10-23 17:52:20 +00:00
|
|
|
i++;
|
2018-12-03 16:19:08 +01:00
|
|
|
}
|
2011-10-23 17:52:20 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
PRINT_MSG("%s: Finished (is_valid=%d)\n\n", __func__, int(!has_fixes));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
*r_change = has_fixes;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
return is_valid;
|
2011-10-23 17:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
bool BKE_mesh_validate_all_customdata(CustomData *vert_data,
|
2018-12-03 16:19:08 +01:00
|
|
|
const uint totvert,
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData *edge_data,
|
2018-12-03 16:19:08 +01:00
|
|
|
const uint totedge,
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData *loop_data,
|
2018-12-03 16:19:08 +01:00
|
|
|
const uint totloop,
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData *face_data,
|
2023-07-24 22:06:55 +02:00
|
|
|
const uint faces_num,
|
2018-07-13 08:36:10 +02:00
|
|
|
const bool check_meshmask,
|
|
|
|
|
const bool do_verbose,
|
|
|
|
|
const bool do_fixes,
|
|
|
|
|
bool *r_change)
|
2011-10-23 17:52:20 +00:00
|
|
|
{
|
2013-09-04 01:29:34 +00:00
|
|
|
bool is_valid = true;
|
|
|
|
|
bool is_change_v, is_change_e, is_change_l, is_change_p;
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
CustomData_MeshMasks mask = {0};
|
|
|
|
|
if (check_meshmask) {
|
|
|
|
|
mask = CD_MASK_MESH;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
is_valid &= mesh_validate_customdata(
|
2023-07-25 21:15:52 +02:00
|
|
|
vert_data, mask.vmask, totvert, do_verbose, do_fixes, &is_change_v);
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
is_valid &= mesh_validate_customdata(
|
2023-07-25 21:15:52 +02:00
|
|
|
edge_data, mask.emask, totedge, do_verbose, do_fixes, &is_change_e);
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
is_valid &= mesh_validate_customdata(
|
2023-07-25 21:15:52 +02:00
|
|
|
loop_data, mask.lmask, totloop, do_verbose, do_fixes, &is_change_l);
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
is_valid &= mesh_validate_customdata(
|
2023-07-25 21:15:52 +02:00
|
|
|
face_data, mask.pmask, faces_num, do_verbose, do_fixes, &is_change_p);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
const int tot_uvloop = CustomData_number_of_layers(loop_data, CD_PROP_FLOAT2);
|
2013-11-11 20:37:19 +00:00
|
|
|
if (tot_uvloop > MAX_MTFACE) {
|
|
|
|
|
PRINT_ERR(
|
|
|
|
|
"\tMore UV layers than %d allowed, %d last ones won't be available for render, shaders, "
|
|
|
|
|
"etc.\n",
|
|
|
|
|
MAX_MTFACE,
|
|
|
|
|
tot_uvloop - MAX_MTFACE);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-08-20 10:35:14 +03:00
|
|
|
/* check indices of clone/stencil */
|
2023-07-25 21:15:52 +02:00
|
|
|
if (do_fixes && CustomData_get_clone_layer(loop_data, CD_PROP_FLOAT2) >= tot_uvloop) {
|
|
|
|
|
CustomData_set_layer_clone(loop_data, CD_PROP_FLOAT2, 0);
|
2015-08-20 10:35:14 +03:00
|
|
|
is_change_l = true;
|
|
|
|
|
}
|
2023-07-25 21:15:52 +02:00
|
|
|
if (do_fixes && CustomData_get_stencil_layer(loop_data, CD_PROP_FLOAT2) >= tot_uvloop) {
|
|
|
|
|
CustomData_set_layer_stencil(loop_data, CD_PROP_FLOAT2, 0);
|
2015-08-20 10:35:14 +03:00
|
|
|
is_change_l = true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
*r_change = (is_change_v || is_change_e || is_change_l || is_change_p);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-04 01:29:34 +00:00
|
|
|
return is_valid;
|
2011-10-23 17:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-01 15:47:09 +02:00
|
|
|
bool BKE_mesh_validate(Mesh *me, const bool do_verbose, const bool cddata_check_mask)
|
2011-02-09 01:27:46 +00:00
|
|
|
{
|
2013-11-26 06:39:14 +11:00
|
|
|
bool changed;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (do_verbose) {
|
2019-02-01 12:44:19 +11:00
|
|
|
CLOG_INFO(&LOG, 0, "MESH: %s", me->id.name + 2);
|
2011-04-25 06:44:43 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
BKE_mesh_validate_all_customdata(&me->vert_data,
|
2022-03-16 10:57:19 +01:00
|
|
|
me->totvert,
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->edge_data,
|
2022-03-16 10:57:19 +01:00
|
|
|
me->totedge,
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->loop_data,
|
2022-03-16 10:57:19 +01:00
|
|
|
me->totloop,
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->face_data,
|
2023-07-24 22:06:55 +02:00
|
|
|
me->faces_num,
|
2022-03-16 10:57:19 +01:00
|
|
|
cddata_check_mask,
|
|
|
|
|
do_verbose,
|
|
|
|
|
true,
|
|
|
|
|
&changed);
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
MutableSpan<float3> positions = me->vert_positions_for_write();
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
MutableSpan<blender::int2> edges = me->edges_for_write();
|
2023-07-24 22:06:55 +02:00
|
|
|
MutableSpan<int> face_offsets = me->face_offsets_for_write();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
MutableSpan<int> corner_verts = me->corner_verts_for_write();
|
|
|
|
|
MutableSpan<int> corner_edges = me->corner_edges_for_write();
|
2022-03-16 10:57:19 +01:00
|
|
|
|
2023-01-13 17:21:20 -06:00
|
|
|
BKE_mesh_validate_arrays(
|
|
|
|
|
me,
|
|
|
|
|
reinterpret_cast<float(*)[3]>(positions.data()),
|
|
|
|
|
positions.size(),
|
|
|
|
|
edges.data(),
|
|
|
|
|
edges.size(),
|
2023-07-24 22:06:55 +02:00
|
|
|
(MFace *)CustomData_get_layer_for_write(&me->fdata_legacy, CD_MFACE, me->totface_legacy),
|
|
|
|
|
me->totface_legacy,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
corner_verts.data(),
|
|
|
|
|
corner_edges.data(),
|
|
|
|
|
corner_verts.size(),
|
2023-07-24 22:06:55 +02:00
|
|
|
face_offsets.data(),
|
|
|
|
|
me->faces_num,
|
2023-01-13 17:21:20 -06:00
|
|
|
me->deform_verts_for_write().data(),
|
|
|
|
|
do_verbose,
|
|
|
|
|
true,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-11-26 06:39:14 +11:00
|
|
|
if (changed) {
|
2021-06-24 19:13:52 +10:00
|
|
|
DEG_id_tag_update(&me->id, ID_RECALC_GEOMETRY_ALL_MODES);
|
2013-03-17 19:55:10 +00:00
|
|
|
return true;
|
2012-03-15 20:10:07 +00:00
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
|
|
|
|
|
return false;
|
2011-02-09 15:13:20 +00:00
|
|
|
}
|
2013-09-16 06:00:25 +00:00
|
|
|
|
2018-04-27 00:39:53 -04:00
|
|
|
bool BKE_mesh_is_valid(Mesh *me)
|
|
|
|
|
{
|
|
|
|
|
const bool do_verbose = true;
|
|
|
|
|
const bool do_fixes = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-27 00:39:53 -04:00
|
|
|
bool is_valid = true;
|
|
|
|
|
bool changed = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-27 00:39:53 -04:00
|
|
|
is_valid &= BKE_mesh_validate_all_customdata(
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->vert_data,
|
2018-12-03 16:19:08 +01:00
|
|
|
me->totvert,
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->edge_data,
|
2018-12-03 16:19:08 +01:00
|
|
|
me->totedge,
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->loop_data,
|
2018-12-03 16:19:08 +01:00
|
|
|
me->totloop,
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->face_data,
|
2023-07-24 22:06:55 +02:00
|
|
|
me->faces_num,
|
2018-04-27 00:39:53 -04:00
|
|
|
false, /* setting mask here isn't useful, gives false positives */
|
|
|
|
|
do_verbose,
|
|
|
|
|
do_fixes,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
MutableSpan<float3> positions = me->vert_positions_for_write();
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
MutableSpan<blender::int2> edges = me->edges_for_write();
|
2023-07-24 22:06:55 +02:00
|
|
|
MutableSpan<int> face_offsets = me->face_offsets_for_write();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
MutableSpan<int> corner_verts = me->corner_verts_for_write();
|
|
|
|
|
MutableSpan<int> corner_edges = me->corner_edges_for_write();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
|
2023-01-13 17:21:20 -06:00
|
|
|
is_valid &= BKE_mesh_validate_arrays(
|
|
|
|
|
me,
|
|
|
|
|
reinterpret_cast<float(*)[3]>(positions.data()),
|
|
|
|
|
positions.size(),
|
|
|
|
|
edges.data(),
|
|
|
|
|
edges.size(),
|
2023-07-24 22:06:55 +02:00
|
|
|
(MFace *)CustomData_get_layer_for_write(&me->fdata_legacy, CD_MFACE, me->totface_legacy),
|
|
|
|
|
me->totface_legacy,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
corner_verts.data(),
|
|
|
|
|
corner_edges.data(),
|
|
|
|
|
corner_verts.size(),
|
2023-07-24 22:06:55 +02:00
|
|
|
face_offsets.data(),
|
|
|
|
|
me->faces_num,
|
2023-01-13 17:21:20 -06:00
|
|
|
me->deform_verts_for_write().data(),
|
|
|
|
|
do_verbose,
|
|
|
|
|
do_fixes,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-27 00:39:53 -04:00
|
|
|
BLI_assert(changed == false);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-27 00:39:53 -04:00
|
|
|
return is_valid;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-01 15:47:09 +02:00
|
|
|
bool BKE_mesh_validate_material_indices(Mesh *me)
|
2014-07-17 17:12:12 +02:00
|
|
|
{
|
2022-08-31 09:09:01 -05:00
|
|
|
const int mat_nr_max = max_ii(0, me->totcol - 1);
|
2014-07-17 17:12:12 +02:00
|
|
|
bool is_valid = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
blender::bke::AttributeWriter<int> material_indices =
|
2022-09-07 21:41:39 -05:00
|
|
|
me->attributes_for_write().lookup_for_write<int>("material_index");
|
2022-08-31 09:09:01 -05:00
|
|
|
blender::MutableVArraySpan<int> material_indices_span(material_indices.varray);
|
|
|
|
|
for (const int i : material_indices_span.index_range()) {
|
|
|
|
|
if (material_indices_span[i] < 0 || material_indices_span[i] > mat_nr_max) {
|
|
|
|
|
material_indices_span[i] = 0;
|
2014-07-17 17:12:12 +02:00
|
|
|
is_valid = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-31 09:09:01 -05:00
|
|
|
material_indices_span.save();
|
|
|
|
|
material_indices.finish();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-07-17 17:12:12 +02:00
|
|
|
if (!is_valid) {
|
2021-06-24 19:13:52 +10:00
|
|
|
DEG_id_tag_update(&me->id, ID_RECALC_GEOMETRY_ALL_MODES);
|
2014-07-17 17:12:12 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
|
|
|
|
|
return false;
|
2014-07-17 17:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
2013-09-09 02:11:44 +00:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Stripping (removing invalid data)
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
void strip_loose_facesloops(Mesh *me, blender::BitSpan faces_to_remove)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
MutableSpan<int> face_offsets = me->face_offsets_for_write();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
MutableSpan<int> corner_edges = me->corner_edges_for_write();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
int a, b;
|
|
|
|
|
/* New loops idx! */
|
2022-02-28 10:08:26 -05:00
|
|
|
int *new_idx = (int *)MEM_mallocN(sizeof(int) * me->totloop, __func__);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (a = b = 0; a < me->faces_num; a++) {
|
2014-04-01 11:34:00 +11:00
|
|
|
bool invalid = false;
|
2023-07-24 22:06:55 +02:00
|
|
|
int start = face_offsets[a];
|
|
|
|
|
int size = face_offsets[a + 1] - start;
|
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
|
|
|
int stop = start + size;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (faces_to_remove[a]) {
|
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
|
|
|
invalid = true;
|
|
|
|
|
}
|
|
|
|
|
else if (stop > me->totloop || stop < start || size < 0) {
|
2014-04-01 11:34:00 +11:00
|
|
|
invalid = true;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-24 22:06:55 +02:00
|
|
|
/* If one of the face's loops is invalid, the whole face is invalid! */
|
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
|
|
|
if (corner_edges.slice(start, size).as_span().contains(INVALID_LOOP_EDGE_MARKER)) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
invalid = true;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
if (size >= 3 && !invalid) {
|
2013-09-09 02:11:44 +00:00
|
|
|
if (a != b) {
|
2023-07-24 22:06:55 +02:00
|
|
|
face_offsets[b] = face_offsets[a];
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_copy_data(&me->face_data, &me->face_data, a, b, 1);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
b++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (a != b) {
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free_elem(&me->face_data, b, a - b);
|
2023-07-24 22:06:55 +02:00
|
|
|
me->faces_num = b;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* And now, get rid of invalid loops. */
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
int corner = 0;
|
|
|
|
|
for (a = b = 0; a < me->totloop; a++, corner++) {
|
|
|
|
|
if (corner_edges[corner] != INVALID_LOOP_EDGE_MARKER) {
|
2013-09-09 02:11:44 +00:00
|
|
|
if (a != b) {
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_copy_data(&me->loop_data, &me->loop_data, a, b, 1);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
new_idx[a] = b;
|
|
|
|
|
b++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-07-24 22:06:55 +02:00
|
|
|
/* XXX Theoretically, we should be able to not do this, as no remaining face
|
2013-09-09 02:11:44 +00:00
|
|
|
* should use any stripped loop. But for security's sake... */
|
|
|
|
|
new_idx[a] = -a;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (a != b) {
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free_elem(&me->loop_data, b, a - b);
|
2013-09-09 02:11:44 +00:00
|
|
|
me->totloop = b;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
face_offsets[me->faces_num] = me->totloop;
|
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
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* And now, update faces' start loop index. */
|
|
|
|
|
/* NOTE: At this point, there should never be any face using a striped loop! */
|
|
|
|
|
for (const int i : blender::IndexRange(me->faces_num)) {
|
|
|
|
|
face_offsets[i] = new_idx[face_offsets[i]];
|
|
|
|
|
BLI_assert(face_offsets[i] >= 0);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
MEM_freeN(new_idx);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 22:30:38 -05:00
|
|
|
void mesh_strip_edges(Mesh *me)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
blender::int2 *e;
|
2013-09-09 02:11:44 +00:00
|
|
|
int a, b;
|
2022-02-28 10:08:26 -05:00
|
|
|
uint *new_idx = (uint *)MEM_mallocN(sizeof(int) * me->totedge, __func__);
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
MutableSpan<blender::int2> edges = me->edges_for_write();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (a = b = 0, e = edges.data(); a < me->totedge; a++, e++) {
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
if ((*e)[0] != (*e)[1]) {
|
2013-09-09 02:11:44 +00:00
|
|
|
if (a != b) {
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
memcpy(&edges[b], e, sizeof(edges[b]));
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_copy_data(&me->edge_data, &me->edge_data, a, b, 1);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
new_idx[a] = b;
|
|
|
|
|
b++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
new_idx[a] = INVALID_LOOP_EDGE_MARKER;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (a != b) {
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free_elem(&me->edge_data, b, a - b);
|
2013-09-09 02:11:44 +00:00
|
|
|
me->totedge = b;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* And now, update loops' edge indices. */
|
|
|
|
|
/* XXX We hope no loop was pointing to a striped edge!
|
|
|
|
|
* Else, its e will be set to INVALID_LOOP_EDGE_MARKER :/ */
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
MutableSpan<int> corner_edges = me->corner_edges_for_write();
|
|
|
|
|
for (const int i : corner_edges.index_range()) {
|
|
|
|
|
corner_edges[i] = new_idx[corner_edges[i]];
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
MEM_freeN(new_idx);
|
|
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Edge Calculation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2018-09-25 12:35:43 +02:00
|
|
|
void BKE_mesh_calc_edges_tessface(Mesh *mesh)
|
|
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
const int numFaces = mesh->totface_legacy;
|
2023-08-29 17:00:33 +02:00
|
|
|
blender::VectorSet<blender::OrderedEdge> eh;
|
|
|
|
|
eh.reserve(numFaces);
|
2023-07-24 22:06:55 +02:00
|
|
|
MFace *mfaces = (MFace *)CustomData_get_layer_for_write(
|
|
|
|
|
&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
MFace *mf = mfaces;
|
2020-09-22 10:08:02 +02:00
|
|
|
for (int i = 0; i < numFaces; i++, mf++) {
|
2023-08-29 17:00:33 +02:00
|
|
|
eh.add({mf->v1, mf->v2});
|
|
|
|
|
eh.add({mf->v2, mf->v3});
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-25 12:35:43 +02:00
|
|
|
if (mf->v4) {
|
2023-08-29 17:00:33 +02:00
|
|
|
eh.add({mf->v3, mf->v4});
|
|
|
|
|
eh.add({mf->v4, mf->v1});
|
2018-09-25 12:35:43 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2023-08-29 17:00:33 +02:00
|
|
|
eh.add({mf->v3, mf->v1});
|
2018-09-25 12:35:43 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 17:00:33 +02:00
|
|
|
const int numEdges = eh.size();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-25 12:35:43 +02:00
|
|
|
/* write new edges into a temporary CustomData */
|
2020-09-22 10:08:02 +02:00
|
|
|
CustomData edgeData;
|
2018-09-25 12:35:43 +02:00
|
|
|
CustomData_reset(&edgeData);
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
CustomData_add_layer_named(&edgeData, CD_PROP_INT32_2D, CD_CONSTRUCT, numEdges, ".edge_verts");
|
2023-03-14 15:30:26 +01:00
|
|
|
CustomData_add_layer(&edgeData, CD_ORIGINDEX, CD_SET_DEFAULT, numEdges);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
blender::int2 *ege = (blender::int2 *)CustomData_get_layer_named_for_write(
|
|
|
|
|
&edgeData, CD_PROP_INT32_2D, ".edge_verts", mesh->totedge);
|
2023-01-13 17:21:20 -06:00
|
|
|
int *index = (int *)CustomData_get_layer_for_write(&edgeData, CD_ORIGINDEX, mesh->totedge);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-08-29 17:00:33 +02:00
|
|
|
memset(index, ORIGINDEX_NONE, sizeof(int) * numEdges);
|
|
|
|
|
MutableSpan(ege, numEdges).copy_from(eh.as_span().cast<blender::int2>());
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-25 12:35:43 +02:00
|
|
|
/* free old CustomData and assign new one */
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free(&mesh->edge_data, mesh->totedge);
|
|
|
|
|
mesh->edge_data = edgeData;
|
2018-09-25 12:35:43 +02:00
|
|
|
mesh->totedge = numEdges;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|