2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-02-17 19:59:41 +01:00
|
|
|
#pragma once
|
2002-10-12 13:37:38 +02:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
2011-02-18 14:05:18 +01: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 18:56:34 +02:00
|
|
|
#include "BLI_compiler_attrs.h"
|
|
|
|
#include "BLI_compiler_compat.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2022-08-31 16:09:01 +02:00
|
|
|
#include "DNA_mesh_types.h"
|
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 18:56:34 +02:00
|
|
|
#include "DNA_meshdata_types.h"
|
2022-08-31 16:09:01 +02:00
|
|
|
|
|
|
|
#include "BKE_customdata.h"
|
2018-05-15 13:26:40 +02:00
|
|
|
|
2019-01-28 11:08:24 +01:00
|
|
|
struct BMesh;
|
2016-07-01 11:07:11 +02:00
|
|
|
struct BMeshCreateParams;
|
2018-05-02 11:39:23 +02:00
|
|
|
struct BMeshFromMeshParams;
|
|
|
|
struct BMeshToMeshParams;
|
2005-07-14 20:04:27 +02:00
|
|
|
struct BoundBox;
|
2019-01-28 11:08:24 +01:00
|
|
|
struct CustomData;
|
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
|
|
|
struct CustomData_MeshMasks;
|
2018-04-06 12:07:27 +02:00
|
|
|
struct Depsgraph;
|
2018-06-01 17:05:21 +02:00
|
|
|
struct KeyBlock;
|
2019-01-28 11:08:24 +01:00
|
|
|
struct LinkNode;
|
|
|
|
struct ListBase;
|
|
|
|
struct MDeformVert;
|
|
|
|
struct MDisps;
|
|
|
|
struct MFace;
|
2015-07-16 19:36:03 +02:00
|
|
|
struct MLoopTri;
|
2019-01-28 11:08:24 +01:00
|
|
|
struct Main;
|
|
|
|
struct MemArena;
|
|
|
|
struct Mesh;
|
2002-10-12 13:37:38 +02:00
|
|
|
struct Object;
|
2019-01-28 11:08:24 +01:00
|
|
|
struct Scene;
|
2013-09-09 04:11:44 +02:00
|
|
|
|
2002-10-12 13:37:38 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-08-02 22:14:18 +02:00
|
|
|
/* TODO: Move to `BKE_mesh_types.hh` when possible. */
|
|
|
|
typedef enum eMeshBatchDirtyMode {
|
|
|
|
BKE_MESH_BATCH_DIRTY_ALL = 0,
|
|
|
|
BKE_MESH_BATCH_DIRTY_SELECT,
|
|
|
|
BKE_MESH_BATCH_DIRTY_SELECT_PAINT,
|
|
|
|
BKE_MESH_BATCH_DIRTY_SHADING,
|
|
|
|
BKE_MESH_BATCH_DIRTY_UVEDIT_ALL,
|
|
|
|
BKE_MESH_BATCH_DIRTY_UVEDIT_SELECT,
|
|
|
|
} eMeshBatchDirtyMode;
|
|
|
|
|
2022-06-23 19:00:25 +02:00
|
|
|
/* mesh_runtime.cc */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call after changing vertex positions to tag lazily calculated caches for recomputation.
|
|
|
|
*/
|
2023-02-27 22:08:48 +01:00
|
|
|
void BKE_mesh_tag_positions_changed(struct Mesh *mesh);
|
2022-06-23 19:00:25 +02:00
|
|
|
|
2023-08-29 17:07:42 +02:00
|
|
|
/**
|
|
|
|
* The same as #BKE_mesh_tag_positions_changed but doesn't tag normals dirty, instead expecting
|
|
|
|
* them to be updated separately.
|
|
|
|
*/
|
|
|
|
void BKE_mesh_tag_positions_changed_no_normals(struct Mesh *mesh);
|
|
|
|
|
2022-06-23 19:00:25 +02:00
|
|
|
/**
|
|
|
|
* Call after moving every mesh vertex by the same translation.
|
|
|
|
*/
|
2023-02-27 22:08:48 +01:00
|
|
|
void BKE_mesh_tag_positions_changed_uniformly(struct Mesh *mesh);
|
2022-06-23 19:00:25 +02:00
|
|
|
|
2022-11-18 23:05:06 +01:00
|
|
|
void BKE_mesh_tag_topology_changed(struct Mesh *mesh);
|
|
|
|
|
2022-11-27 00:54:05 +01:00
|
|
|
/**
|
|
|
|
* Call when new edges and vertices have been created but positions and faces haven't changed.
|
|
|
|
*/
|
|
|
|
void BKE_mesh_tag_edges_split(struct Mesh *mesh);
|
|
|
|
|
2023-04-03 18:01:16 +02:00
|
|
|
/**
|
|
|
|
* Call when face vertex order has changed but positions and faces haven't changed
|
|
|
|
*/
|
|
|
|
void BKE_mesh_tag_face_winding_changed(struct Mesh *mesh);
|
|
|
|
|
2023-07-31 03:50:54 +02:00
|
|
|
/* `mesh.cc` */
|
2013-06-14 10:28:27 +02:00
|
|
|
|
2018-05-02 11:39:23 +02:00
|
|
|
struct BMesh *BKE_mesh_to_bmesh_ex(const struct Mesh *me,
|
|
|
|
const struct BMeshCreateParams *create_params,
|
|
|
|
const struct BMeshFromMeshParams *convert_params);
|
2016-07-01 11:07:11 +02:00
|
|
|
struct BMesh *BKE_mesh_to_bmesh(struct Mesh *me,
|
|
|
|
struct Object *ob,
|
2022-01-07 01:38:08 +01:00
|
|
|
bool add_key_index,
|
2016-07-01 11:07:11 +02:00
|
|
|
const struct BMeshCreateParams *params);
|
2013-01-24 05:02:30 +01:00
|
|
|
|
2019-09-16 10:45:49 +02:00
|
|
|
struct Mesh *BKE_mesh_from_bmesh_nomain(struct BMesh *bm,
|
|
|
|
const struct BMeshToMeshParams *params,
|
|
|
|
const struct Mesh *me_settings);
|
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
|
|
|
struct Mesh *BKE_mesh_from_bmesh_for_eval_nomain(struct BMesh *bm,
|
2019-09-16 10:45:49 +02:00
|
|
|
const struct CustomData_MeshMasks *cd_mask_extra,
|
|
|
|
const struct Mesh *me_settings);
|
2018-05-02 11:39:23 +02:00
|
|
|
|
Mesh: Avoid creating incorrect original index layers
Currently, whenever any BMesh is converted to a Mesh (except for edit
mode switching), original index (`CD_ORIGINDEX`) layers are added.
This is incorrect, because many operations just convert some Mesh into
a BMesh and then back, but they shouldn't make any assumption about
where their input mesh came from. It might even come from a primitive
in geometry nodes, where there are no original indices at all.
Conceptually, mesh original indices should be filled by the modifier
stack when first creating the evaluated mesh. So that's where they're
moved in this patch. A separate function now fills the indices with their
default (0,1,2,3...) values. The way the mesh wrapper system defers
the BMesh to Mesh conversion makes this a bit less obvious though.
The old behavior is incorrect, but it's also slower, because three
arrays the size of the mesh's vertices, edges, and faces had to be
allocated and filled during the BMesh to Mesh conversion, which just
ends up putting more pressure on the cache. In the many cases where
original indices aren't used, I measured an **8% speedup** for the
conversion (from 76.5ms to 70.7ms).
Generally there is an assumption that BMesh is "original" and Mesh is
"evaluated". After this patch, that assumption isn't quite as strong,
but it still exists for two reasons. First, original indices are added
whenever converting a BMesh "wrapper" to a Mesh. Second, original
indices are not added to the BMesh at the beginning of evaluation,
which assumes that every BMesh in the viewport is original and doesn't
need the mapping.
Differential Revision: https://developer.blender.org/D14018
2022-02-18 17:51:00 +01:00
|
|
|
/**
|
|
|
|
* Add original index (#CD_ORIGINDEX) layers if they don't already exist. This is meant to be used
|
|
|
|
* when creating an evaluated mesh from an original edit mode mesh, to allow mapping from the
|
|
|
|
* evaluated vertices to the originals.
|
2022-07-11 17:25:13 +02:00
|
|
|
*
|
|
|
|
* The mesh is expected to of a `ME_WRAPPER_TYPE_MDATA` wrapper type. This is asserted.
|
Mesh: Avoid creating incorrect original index layers
Currently, whenever any BMesh is converted to a Mesh (except for edit
mode switching), original index (`CD_ORIGINDEX`) layers are added.
This is incorrect, because many operations just convert some Mesh into
a BMesh and then back, but they shouldn't make any assumption about
where their input mesh came from. It might even come from a primitive
in geometry nodes, where there are no original indices at all.
Conceptually, mesh original indices should be filled by the modifier
stack when first creating the evaluated mesh. So that's where they're
moved in this patch. A separate function now fills the indices with their
default (0,1,2,3...) values. The way the mesh wrapper system defers
the BMesh to Mesh conversion makes this a bit less obvious though.
The old behavior is incorrect, but it's also slower, because three
arrays the size of the mesh's vertices, edges, and faces had to be
allocated and filled during the BMesh to Mesh conversion, which just
ends up putting more pressure on the cache. In the many cases where
original indices aren't used, I measured an **8% speedup** for the
conversion (from 76.5ms to 70.7ms).
Generally there is an assumption that BMesh is "original" and Mesh is
"evaluated". After this patch, that assumption isn't quite as strong,
but it still exists for two reasons. First, original indices are added
whenever converting a BMesh "wrapper" to a Mesh. Second, original
indices are not added to the BMesh at the beginning of evaluation,
which assumes that every BMesh in the viewport is original and doesn't
need the mapping.
Differential Revision: https://developer.blender.org/D14018
2022-02-18 17:51:00 +01:00
|
|
|
*/
|
|
|
|
void BKE_mesh_ensure_default_orig_index_customdata(struct Mesh *mesh);
|
|
|
|
|
2022-07-11 17:25:13 +02:00
|
|
|
/**
|
|
|
|
* Same as #BKE_mesh_ensure_default_orig_index_customdata but does not perform any checks: they
|
|
|
|
* must be done by the caller.
|
|
|
|
*/
|
|
|
|
void BKE_mesh_ensure_default_orig_index_customdata_no_check(struct Mesh *mesh);
|
|
|
|
|
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
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Sets each output array element to the edge index if it is a real edge, or -1.
|
|
|
|
*/
|
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
|
|
|
void BKE_mesh_looptri_get_real_edges(const blender::int2 *edges,
|
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_verts,
|
|
|
|
const int *corner_edges,
|
|
|
|
const struct MLoopTri *tri,
|
2018-10-20 20:02:52 +02:00
|
|
|
int r_edges[3]);
|
2012-05-22 17:28:44 +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
|
|
|
#endif
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Free (or release) any data used by this mesh (does not free the mesh itself).
|
|
|
|
* Only use for undo, in most cases `BKE_id_free(nullptr, me)` should be used.
|
|
|
|
*/
|
2021-08-20 08:21:29 +02:00
|
|
|
void BKE_mesh_free_data_for_undo(struct Mesh *me);
|
2023-04-12 17:27:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all geometry and derived data like caches from the mesh.
|
|
|
|
*/
|
2019-07-30 18:38:31 +02:00
|
|
|
void BKE_mesh_clear_geometry(struct Mesh *me);
|
2023-04-12 17:27:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as #BKE_mesh_clear_geometry, but also clears attribute meta-data like active attribute
|
|
|
|
* names and vertex group names. Used when the geometry is *entirely* replaced.
|
|
|
|
*/
|
|
|
|
void BKE_mesh_clear_geometry_and_metadata(struct Mesh *me);
|
|
|
|
|
2013-02-05 13:46:15 +01:00
|
|
|
struct Mesh *BKE_mesh_add(struct Main *bmain, const char *name);
|
2022-01-11 15:42:07 +01:00
|
|
|
|
|
|
|
void BKE_mesh_free_editmesh(struct Mesh *mesh);
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* A version of #BKE_mesh_copy_parameters that is intended for evaluated output
|
|
|
|
* (the modifier stack for example).
|
|
|
|
*
|
|
|
|
* \warning User counts are not handled for ID's.
|
|
|
|
*/
|
2021-06-17 06:56:39 +02:00
|
|
|
void BKE_mesh_copy_parameters_for_eval(struct Mesh *me_dst, const struct Mesh *me_src);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Copy user editable settings that we want to preserve
|
|
|
|
* when a new mesh is based on an existing mesh.
|
|
|
|
*/
|
2021-06-17 06:56:39 +02:00
|
|
|
void BKE_mesh_copy_parameters(struct Mesh *me_dst, const struct Mesh *me_src);
|
2014-03-16 11:55:30 +01:00
|
|
|
void BKE_mesh_ensure_skin_customdata(struct Mesh *me);
|
2009-08-30 23:30:07 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/** Add face offsets to describe faces to a new mesh. */
|
|
|
|
void BKE_mesh_face_offsets_ensure_alloc(struct Mesh *mesh);
|
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
|
|
|
struct Mesh *BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int loops_num);
|
2023-02-27 17:09:26 +01:00
|
|
|
struct Mesh *BKE_mesh_new_nomain_from_template(
|
2023-07-24 22:06:55 +02:00
|
|
|
const struct Mesh *me_src, int verts_num, int edges_num, int faces_num, int loops_num);
|
2019-08-27 18:17:27 +02:00
|
|
|
struct Mesh *BKE_mesh_new_nomain_from_template_ex(const struct Mesh *me_src,
|
2023-04-19 21:28:53 +02:00
|
|
|
int verts_num,
|
|
|
|
int edges_num,
|
|
|
|
int tessface_num,
|
2023-07-24 22:06:55 +02:00
|
|
|
int faces_num,
|
2023-04-19 21:28:53 +02:00
|
|
|
int loops_num,
|
2020-02-26 04:24:57 +01:00
|
|
|
struct CustomData_MeshMasks mask);
|
2018-04-19 11:03:58 +02:00
|
|
|
|
2020-09-04 20:59:13 +02:00
|
|
|
void BKE_mesh_eval_delete(struct Mesh *mesh_eval);
|
2019-07-28 15:12:38 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Performs copy for use during evaluation,
|
|
|
|
* optional referencing original arrays to reduce memory.
|
|
|
|
*/
|
2023-04-19 21:49:13 +02:00
|
|
|
struct Mesh *BKE_mesh_copy_for_eval(const struct Mesh *source);
|
2018-08-16 14:20:36 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* These functions construct a new Mesh,
|
|
|
|
* contrary to #BKE_mesh_to_curve_nurblist which modifies ob itself.
|
|
|
|
*/
|
2021-07-20 03:19:59 +02:00
|
|
|
struct Mesh *BKE_mesh_new_nomain_from_curve(const struct Object *ob);
|
2021-06-28 20:47:48 +02:00
|
|
|
struct Mesh *BKE_mesh_new_nomain_from_curve_displist(const struct Object *ob,
|
|
|
|
const struct ListBase *dispbase);
|
2018-05-08 11:33:31 +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 06:10:43 +01:00
|
|
|
bool BKE_mesh_attribute_required(const char *name);
|
|
|
|
|
2012-12-21 08:28:14 +01:00
|
|
|
float (*BKE_mesh_orco_verts_get(struct Object *ob))[3];
|
2023-08-24 02:45:34 +02:00
|
|
|
void BKE_mesh_orco_verts_transform(struct Mesh *me, float (*orco)[3], int totvert, bool invert);
|
2021-11-22 12:04:37 +01:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Add a #CD_ORCO layer to the Mesh if there is none already.
|
|
|
|
*/
|
2021-11-22 12:04:37 +01:00
|
|
|
void BKE_mesh_orco_ensure(struct Object *ob, struct Mesh *mesh);
|
|
|
|
|
2012-05-05 18:03:57 +02:00
|
|
|
struct Mesh *BKE_mesh_from_object(struct Object *ob);
|
2018-05-29 15:49:21 +02:00
|
|
|
void BKE_mesh_assign_object(struct Main *bmain, struct Object *ob, struct Mesh *me);
|
2018-06-01 13:48:39 +02:00
|
|
|
void BKE_mesh_to_curve_nurblist(const struct Mesh *me,
|
|
|
|
struct ListBase *nurblist,
|
2022-01-07 01:38:08 +01:00
|
|
|
int edge_users_test);
|
2018-06-11 16:02:46 +02:00
|
|
|
void BKE_mesh_to_curve(struct Main *bmain,
|
|
|
|
struct Depsgraph *depsgraph,
|
|
|
|
struct Scene *scene,
|
|
|
|
struct Object *ob);
|
2020-04-09 18:49:40 +02:00
|
|
|
void BKE_mesh_to_pointcloud(struct Main *bmain,
|
|
|
|
struct Depsgraph *depsgraph,
|
|
|
|
struct Scene *scene,
|
|
|
|
struct Object *ob);
|
|
|
|
void BKE_pointcloud_to_mesh(struct Main *bmain,
|
|
|
|
struct Depsgraph *depsgraph,
|
|
|
|
struct Scene *scene,
|
|
|
|
struct Object *ob);
|
2013-08-14 13:29:58 +02:00
|
|
|
void BKE_mesh_material_index_remove(struct Mesh *me, short index);
|
2019-07-31 21:04:52 +02:00
|
|
|
bool BKE_mesh_material_index_used(struct Mesh *me, short index);
|
2013-08-14 13:29:58 +02:00
|
|
|
void BKE_mesh_material_index_clear(struct Mesh *me);
|
2015-04-27 23:24:56 +02:00
|
|
|
void BKE_mesh_material_remap(struct Mesh *me, const unsigned int *remap, unsigned int remap_len);
|
2022-01-07 01:38:08 +01:00
|
|
|
void BKE_mesh_smooth_flag_set(struct Mesh *me, bool use_smooth);
|
2022-05-11 11:54:14 +02:00
|
|
|
void BKE_mesh_auto_smooth_flag_set(struct Mesh *me, bool use_auto_smooth, float auto_smooth_angle);
|
2013-09-09 04:11:44 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Used for unit testing; compares two meshes, checking only
|
|
|
|
* differences we care about. should be usable with leaf's
|
|
|
|
* testing framework I get RNA work done, will use hackish
|
|
|
|
* testing code for now.
|
|
|
|
*/
|
2012-05-05 23:28:12 +02:00
|
|
|
const char *BKE_mesh_cmp(struct Mesh *me1, struct Mesh *me2, float thresh);
|
2010-10-27 04:22:55 +02:00
|
|
|
|
2012-05-05 23:28:12 +02:00
|
|
|
struct BoundBox *BKE_mesh_boundbox_get(struct Object *ob);
|
2019-09-23 15:31:11 +02:00
|
|
|
|
|
|
|
void BKE_mesh_texspace_calc(struct Mesh *me);
|
|
|
|
void BKE_mesh_texspace_ensure(struct Mesh *me);
|
2023-01-18 07:17:32 +01:00
|
|
|
void BKE_mesh_texspace_get(struct Mesh *me,
|
|
|
|
float r_texspace_location[3],
|
|
|
|
float r_texspace_size[3]);
|
2019-09-23 15:54:21 +02:00
|
|
|
void BKE_mesh_texspace_get_reference(struct Mesh *me,
|
2023-01-18 07:17:32 +01:00
|
|
|
char **r_texspace_flag,
|
|
|
|
float **r_texspace_location,
|
|
|
|
float **r_texspace_size);
|
2005-07-14 20:04:27 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Create new mesh from the given object at its current state.
|
2023-06-14 15:43:20 +02:00
|
|
|
* The caller owns the result mesh.
|
2019-05-24 14:37:47 +02:00
|
|
|
*
|
2023-06-14 15:43:20 +02:00
|
|
|
* If \a preserve_all_data_layers is true then the modifier stack is re-evaluated to ensure it
|
2019-05-24 14:37:47 +02:00
|
|
|
* preserves all possible custom data layers.
|
|
|
|
*
|
2023-06-14 15:43:20 +02:00
|
|
|
* \note Dependency graph argument is required when preserve_all_data_layers is true, and is
|
2021-12-07 07:19:15 +01:00
|
|
|
* ignored otherwise.
|
|
|
|
*/
|
2019-05-24 14:37:47 +02:00
|
|
|
struct Mesh *BKE_mesh_new_from_object(struct Depsgraph *depsgraph,
|
|
|
|
struct Object *object,
|
2022-01-07 01:38:08 +01:00
|
|
|
bool preserve_all_data_layers,
|
|
|
|
bool preserve_origindex);
|
2019-05-16 13:49:21 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* This is a version of BKE_mesh_new_from_object() which stores mesh in the given main database.
|
2019-08-02 17:45:32 +02:00
|
|
|
* However, that function enforces object type to be a geometry one, and ensures a mesh is always
|
2021-12-07 07:19:15 +01:00
|
|
|
* generated, be it empty.
|
|
|
|
*/
|
2019-05-24 14:37:47 +02:00
|
|
|
struct Mesh *BKE_mesh_new_from_object_to_bmain(struct Main *bmain,
|
|
|
|
struct Depsgraph *depsgraph,
|
|
|
|
struct Object *object,
|
|
|
|
bool preserve_all_data_layers);
|
2019-05-16 13:49:21 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
2022-09-09 15:24:31 +02:00
|
|
|
* Move data from a mesh outside of the main data-base into a mesh in the data-base.
|
|
|
|
* Takes ownership of the source mesh.
|
2021-12-07 07:19:15 +01:00
|
|
|
*/
|
2022-09-09 15:24:31 +02:00
|
|
|
void BKE_mesh_nomain_to_mesh(struct Mesh *mesh_src, struct Mesh *mesh_dst, struct Object *ob);
|
2018-06-06 10:30:24 +02:00
|
|
|
void BKE_mesh_nomain_to_meshkey(struct Mesh *mesh_src, struct Mesh *mesh_dst, struct KeyBlock *kb);
|
2018-06-01 17:05:21 +02:00
|
|
|
|
2013-09-09 04:11:44 +02:00
|
|
|
/* vertex level transformations & checks (no derived mesh) */
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/* basic vertex data functions */
|
2020-08-02 09:17:31 +02:00
|
|
|
void BKE_mesh_transform(struct Mesh *me, const float mat[4][4], bool do_keys);
|
2022-01-07 01:38:08 +01:00
|
|
|
void BKE_mesh_translate(struct Mesh *me, const float offset[3], bool do_keys);
|
2013-09-09 04:11:44 +02:00
|
|
|
|
|
|
|
void BKE_mesh_tessface_clear(struct Mesh *mesh);
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_clear(struct Mesh *me);
|
|
|
|
void BKE_mesh_mselect_validate(struct Mesh *me);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* \return the index within `me->mselect`, or -1
|
|
|
|
*/
|
2013-09-09 04:11:44 +02:00
|
|
|
int BKE_mesh_mselect_find(struct Mesh *me, int index, int type);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* \return The index of the active element.
|
|
|
|
*/
|
2013-09-09 04:11:44 +02:00
|
|
|
int BKE_mesh_mselect_active_get(struct Mesh *me, int type);
|
|
|
|
void BKE_mesh_mselect_active_set(struct Mesh *me, int index, int type);
|
|
|
|
|
2018-12-17 08:15:41 +01:00
|
|
|
void BKE_mesh_count_selected_items(const struct Mesh *mesh, int r_count[3]);
|
|
|
|
|
2019-08-21 22:28:35 +02:00
|
|
|
float (*BKE_mesh_vert_coords_alloc(const struct Mesh *mesh, int *r_vert_len))[3];
|
|
|
|
|
2019-08-22 05:45:31 +02:00
|
|
|
void BKE_mesh_vert_coords_apply_with_mat4(struct Mesh *mesh,
|
|
|
|
const float (*vert_coords)[3],
|
|
|
|
const float mat[4][4]);
|
2019-08-21 22:28:35 +02:00
|
|
|
void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float (*vert_coords)[3]);
|
2018-05-08 11:33:31 +02:00
|
|
|
|
2022-07-26 21:40:59 +02:00
|
|
|
/* *** mesh_tessellate.cc *** */
|
2021-06-18 06:38:25 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
2023-07-26 03:59:47 +02:00
|
|
|
* See #bke::mesh::looptris_calc
|
2021-12-07 07:19:15 +01: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
|
|
|
void BKE_mesh_recalc_looptri(const int *corner_verts,
|
2023-07-24 22:06:55 +02:00
|
|
|
const int *face_offsets,
|
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 06:10:43 +01:00
|
|
|
const float (*vert_positions)[3],
|
2023-03-12 22:29:15 +01:00
|
|
|
int totvert,
|
2021-06-18 06:38:25 +02:00
|
|
|
int totloop,
|
2023-07-24 22:06:55 +02:00
|
|
|
int faces_num,
|
2021-06-18 06:38:25 +02:00
|
|
|
struct MLoopTri *mlooptri);
|
|
|
|
|
2021-07-07 06:01:58 +02:00
|
|
|
/* *** mesh_normals.cc *** */
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 21:37:58 +01:00
|
|
|
|
2023-08-31 20:22:22 +02:00
|
|
|
/** Return true if the mesh vertex normals either are not stored or are dirty. */
|
2023-02-27 21:52:29 +01:00
|
|
|
bool BKE_mesh_vert_normals_are_dirty(const struct Mesh *mesh);
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 21:37:58 +01:00
|
|
|
|
2023-08-31 20:22:22 +02:00
|
|
|
/** Return true if the mesh face normals either are not stored or are dirty. */
|
2023-07-24 22:06:55 +02:00
|
|
|
bool BKE_mesh_face_normals_are_dirty(const struct Mesh *mesh);
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 21:37:58 +01:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Called after calculating all modifiers.
|
|
|
|
*/
|
2018-10-11 07:32:49 +02:00
|
|
|
void BKE_mesh_ensure_normals_for_display(struct Mesh *mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
/**
|
|
|
|
* References a contiguous loop-fan with normal offset vars.
|
|
|
|
*/
|
|
|
|
typedef struct MLoopNorSpace {
|
2019-04-21 05:44:06 +02:00
|
|
|
/** Automatically computed loop normal. */
|
|
|
|
float vec_lnor[3];
|
|
|
|
/** Reference vector, orthogonal to vec_lnor. */
|
|
|
|
float vec_ref[3];
|
|
|
|
/** Third vector, orthogonal to vec_lnor and vec_ref. */
|
|
|
|
float vec_ortho[3];
|
|
|
|
/** Reference angle, around vec_ortho, in ]0, pi] range (0.0 marks that space as invalid). */
|
|
|
|
float ref_alpha;
|
|
|
|
/** Reference angle, around vec_lnor, in ]0, 2pi] range (0.0 marks that space as invalid). */
|
|
|
|
float ref_beta;
|
|
|
|
/** All loops using this lnor space (i.e. smooth fan of loops),
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
* as (depending on owning MLoopNorSpaceArrary.data_type):
|
|
|
|
* - Indices (uint_in_ptr), or
|
|
|
|
* - BMLoop pointers. */
|
|
|
|
struct LinkNode *loops;
|
|
|
|
char flags;
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
} MLoopNorSpace;
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
/**
|
|
|
|
* MLoopNorSpace.flags
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
MLNOR_SPACE_IS_SINGLE = 1 << 0,
|
|
|
|
};
|
|
|
|
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
/**
|
|
|
|
* Collection of #MLoopNorSpace basic storage & pre-allocation.
|
|
|
|
*/
|
|
|
|
typedef struct MLoopNorSpaceArray {
|
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
|
|
|
MLoopNorSpace **lspacearr; /* Face corner aligned array */
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
struct LinkNode
|
|
|
|
*loops_pool; /* Allocated once, avoids to call BLI_linklist_prepend_arena() for each loop! */
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
char data_type; /* Whether we store loop indices, or pointers to BMLoop. */
|
2022-03-28 03:29:47 +02:00
|
|
|
int spaces_num; /* Number of clnors spaces defined in this array. */
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
struct MemArena *mem;
|
|
|
|
} MLoopNorSpaceArray;
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
/**
|
|
|
|
* MLoopNorSpaceArray.data_type
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
MLNOR_SPACEARR_LOOP_INDEX = 0,
|
|
|
|
MLNOR_SPACEARR_BMLOOP_PTR = 1,
|
|
|
|
};
|
|
|
|
|
2019-02-28 20:40:21 +01:00
|
|
|
/* Low-level custom normals functions. */
|
2022-01-07 01:38:08 +01:00
|
|
|
void BKE_lnor_spacearr_init(MLoopNorSpaceArray *lnors_spacearr, int numLoops, char data_type);
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr);
|
|
|
|
void BKE_lnor_spacearr_free(MLoopNorSpaceArray *lnors_spacearr);
|
2021-07-14 05:22:58 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Utility for multi-threaded calculation that ensures
|
|
|
|
* `lnors_spacearr_tls` doesn't share memory with `lnors_spacearr`
|
|
|
|
* that would cause it not to be thread safe.
|
|
|
|
*
|
|
|
|
* \note This works as long as threads never operate on the same loops at once.
|
|
|
|
*/
|
2021-07-14 05:22:58 +02:00
|
|
|
void BKE_lnor_spacearr_tls_init(MLoopNorSpaceArray *lnors_spacearr,
|
|
|
|
MLoopNorSpaceArray *lnors_spacearr_tls);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Utility for multi-threaded calculation
|
|
|
|
* that merges `lnors_spacearr_tls` into `lnors_spacearr`.
|
|
|
|
*/
|
2021-07-14 05:22:58 +02:00
|
|
|
void BKE_lnor_spacearr_tls_join(MLoopNorSpaceArray *lnors_spacearr,
|
|
|
|
MLoopNorSpaceArray *lnors_spacearr_tls);
|
|
|
|
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
MLoopNorSpace *BKE_lnor_space_create(MLoopNorSpaceArray *lnors_spacearr);
|
2023-04-27 05:56:45 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Should only be called once.
|
|
|
|
* Beware, this modifies ref_vec and other_vec in place!
|
|
|
|
* In case no valid space can be generated, ref_alpha and ref_beta are set to zero
|
|
|
|
* (which means 'use auto lnors').
|
|
|
|
*/
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
void BKE_lnor_space_define(MLoopNorSpace *lnor_space,
|
|
|
|
const float lnor[3],
|
|
|
|
float vec_ref[3],
|
|
|
|
float vec_other[3],
|
2023-04-27 05:56:45 +02:00
|
|
|
blender::Span<blender::float3> edge_vectors);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Add a new given loop to given lnor_space.
|
|
|
|
* Depending on \a lnor_space->data_type, we expect \a bm_loop to be a pointer to BMLoop struct
|
|
|
|
* (in case of BMLOOP_PTR), or nullptr (in case of LOOP_INDEX), loop index is then stored in
|
|
|
|
* pointer. If \a is_single is set, the BMLoop or loop index is directly stored in \a
|
|
|
|
* lnor_space->loops pointer (since there is only one loop in this fan), else it is added to the
|
|
|
|
* linked list of loops in the fan.
|
|
|
|
*/
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
void BKE_lnor_space_add_loop(MLoopNorSpaceArray *lnors_spacearr,
|
|
|
|
MLoopNorSpace *lnor_space,
|
2022-01-07 01:38:08 +01:00
|
|
|
int ml_index,
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
void *bm_loop,
|
2022-01-07 01:38:08 +01:00
|
|
|
bool is_single);
|
2022-11-22 18:48:16 +01:00
|
|
|
void BKE_lnor_space_custom_data_to_normal(const MLoopNorSpace *lnor_space,
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
const short clnor_data[2],
|
|
|
|
float r_custom_lnor[3]);
|
2022-11-22 18:48:16 +01:00
|
|
|
void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space,
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
const float custom_lnor[3],
|
|
|
|
short r_clnor_data[2]);
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Computes average per-vertex normals from given custom loop normals.
|
|
|
|
*
|
|
|
|
* \param clnors: The computed custom loop normals.
|
|
|
|
* \param r_vert_clnors: The (already allocated) array where to store averaged per-vertex normals.
|
|
|
|
*/
|
2022-01-07 01:38:08 +01:00
|
|
|
void BKE_mesh_normals_loop_to_vertex(int numVerts,
|
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_verts,
|
2022-01-07 01:38:08 +01:00
|
|
|
int numLoops,
|
2015-08-12 18:21:41 +02:00
|
|
|
const float (*clnors)[3],
|
|
|
|
float (*r_vert_clnors)[3]);
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* High-level custom normals functions.
|
|
|
|
*/
|
2019-02-28 20:40:21 +01:00
|
|
|
bool BKE_mesh_has_custom_loop_normals(struct Mesh *me);
|
|
|
|
|
|
|
|
void BKE_mesh_calc_normals_split(struct Mesh *mesh);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Compute 'split' (aka loop, or per face corner's) normals.
|
|
|
|
*
|
|
|
|
* \param r_lnors_spacearr: Allows to get computed loop normal space array.
|
|
|
|
* That data, among other things, contains 'smooth fan' info, useful e.g.
|
|
|
|
* to split geometry along sharp edges.
|
|
|
|
*/
|
2023-08-04 19:05:08 +02:00
|
|
|
void BKE_mesh_calc_normals_split_ex(const struct Mesh *mesh,
|
2022-07-08 14:45:48 +02:00
|
|
|
struct MLoopNorSpaceArray *r_lnors_spacearr,
|
|
|
|
float (*r_corner_normals)[3]);
|
2019-02-28 20:40:21 +01:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Higher level functions hiding most of the code needed around call to
|
2023-03-12 22:29:15 +01:00
|
|
|
* #normals_loop_custom_set().
|
2021-12-07 07:19:15 +01:00
|
|
|
*
|
2022-12-17 02:47:43 +01:00
|
|
|
* \param r_custom_loop_normals: is not const, since code will replace zero_v3 normals there
|
2021-12-07 07:19:15 +01:00
|
|
|
* with automatically computed vectors.
|
|
|
|
*/
|
2022-12-17 02:47:43 +01:00
|
|
|
void BKE_mesh_set_custom_normals(struct Mesh *mesh, float (*r_custom_loop_normals)[3]);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Higher level functions hiding most of the code needed around call to
|
2023-03-12 22:29:15 +01:00
|
|
|
* #normals_loop_custom_set_from_verts().
|
2021-12-07 07:19:15 +01:00
|
|
|
*
|
2022-12-17 02:47:43 +01:00
|
|
|
* \param r_custom_vert_normals: is not const, since code will replace zero_v3 normals there
|
2021-12-07 07:19:15 +01:00
|
|
|
* with automatically computed vectors.
|
|
|
|
*/
|
2022-12-17 02:47:43 +01:00
|
|
|
void BKE_mesh_set_custom_normals_from_verts(struct Mesh *mesh, float (*r_custom_vert_normals)[3]);
|
2019-02-28 20:40:21 +01:00
|
|
|
|
2021-07-07 06:01:58 +02:00
|
|
|
/* *** mesh_evaluate.cc *** */
|
2021-07-01 02:08:47 +02:00
|
|
|
|
2019-09-09 19:41:12 +02:00
|
|
|
float BKE_mesh_calc_area(const struct Mesh *me);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-10 20:39:27 +02:00
|
|
|
bool BKE_mesh_center_median(const struct Mesh *me, float r_cent[3]);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* Calculate the center from faces,
|
2021-12-07 07:19:15 +01:00
|
|
|
* use when we want to ignore vertex locations that don't have connected faces.
|
|
|
|
*/
|
2023-07-24 22:06:55 +02:00
|
|
|
bool BKE_mesh_center_median_from_faces(const struct Mesh *me, float r_cent[3]);
|
2017-08-21 07:06:07 +02:00
|
|
|
bool BKE_mesh_center_of_surface(const struct Mesh *me, float r_cent[3]);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* \note Mesh must be manifold with consistent face-winding,
|
2023-07-24 22:06:55 +02:00
|
|
|
* see #mesh_calc_face_volume_centroid for details.
|
2021-12-07 07:19:15 +01:00
|
|
|
*/
|
2017-08-21 07:06:07 +02:00
|
|
|
bool BKE_mesh_center_of_volume(const struct Mesh *me, float r_cent[3]);
|
2013-05-28 16:23:07 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Calculate the volume and center.
|
|
|
|
*
|
|
|
|
* \param r_volume: Volume (unsigned).
|
|
|
|
* \param r_center: Center of mass.
|
|
|
|
*/
|
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 06:10:43 +01:00
|
|
|
void BKE_mesh_calc_volume(const float (*vert_positions)[3],
|
2022-01-07 01:38:08 +01:00
|
|
|
int mverts_num,
|
2015-07-19 21:19:47 +02:00
|
|
|
const struct MLoopTri *mlooptri,
|
2022-01-07 01:38:08 +01:00
|
|
|
int looptri_num,
|
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_verts,
|
2015-07-19 21:19:47 +02:00
|
|
|
float *r_volume,
|
|
|
|
float r_center[3]);
|
2014-07-11 12:06:13 +02:00
|
|
|
|
2021-12-07 07:19:15 +01: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
|
|
|
* Flip a single corner's #MDisps structure,
|
2021-12-07 07:19:15 +01:00
|
|
|
* low level function to be called from face-flipping code which re-arranged the mdisps themselves.
|
|
|
|
*/
|
2022-01-07 01:38:08 +01:00
|
|
|
void BKE_mesh_mdisp_flip(struct MDisps *md, bool use_loop_mdisp_flip);
|
2016-03-14 05:17:29 +01:00
|
|
|
|
2022-05-05 12:36:15 +02:00
|
|
|
/**
|
2023-01-10 20:49:51 +01:00
|
|
|
* Account for custom-data such as UVs becoming detached because of imprecision
|
2022-05-05 12:36:15 +02:00
|
|
|
* in custom-data interpolation.
|
2023-01-10 20:49:51 +01:00
|
|
|
* Without running this operation subdivision surface can cause UVs to be disconnected,
|
2023-02-12 04:37:16 +01:00
|
|
|
* see: #81065.
|
2022-05-05 12:36:15 +02:00
|
|
|
*/
|
|
|
|
void BKE_mesh_merge_customdata_for_apply_modifier(struct Mesh *me);
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/* Flush flags. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the hide flag for edges and faces from the corresponding flag in verts.
|
|
|
|
*/
|
2013-09-09 04:11:44 +02:00
|
|
|
void BKE_mesh_flush_hidden_from_verts(struct Mesh *me);
|
2023-07-24 22:06:55 +02:00
|
|
|
void BKE_mesh_flush_hidden_from_faces(struct Mesh *me);
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 16:38:37 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
void BKE_mesh_flush_select_from_faces(struct Mesh *me);
|
2013-09-09 04:11:44 +02:00
|
|
|
void BKE_mesh_flush_select_from_verts(struct Mesh *me);
|
2013-06-14 09:15:38 +02:00
|
|
|
|
2013-09-09 04:11:44 +02:00
|
|
|
/* spatial evaluation */
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* This function takes the difference between 2 vertex-coord-arrays
|
|
|
|
* (\a vert_cos_src, \a vert_cos_dst),
|
|
|
|
* and applies the difference to \a vert_cos_new relative to \a vert_cos_org.
|
|
|
|
*
|
|
|
|
* \param vert_cos_src: reference deform source.
|
|
|
|
* \param vert_cos_dst: reference deform destination.
|
|
|
|
*
|
|
|
|
* \param vert_cos_org: reference for the output location.
|
|
|
|
* \param vert_cos_new: resulting coords.
|
|
|
|
*/
|
2023-07-24 22:06:55 +02:00
|
|
|
void BKE_mesh_calc_relative_deform(const int *face_offsets,
|
|
|
|
int faces_num,
|
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_verts,
|
2022-01-07 01:38:08 +01:00
|
|
|
int totvert,
|
2009-01-21 23:40:28 +01:00
|
|
|
|
2013-09-09 04:11:44 +02:00
|
|
|
const float (*vert_cos_src)[3],
|
|
|
|
const float (*vert_cos_dst)[3],
|
2007-09-18 21:39:25 +02:00
|
|
|
|
2013-09-09 04:11:44 +02:00
|
|
|
const float (*vert_cos_org)[3],
|
|
|
|
float (*vert_cos_new)[3]);
|
2007-09-18 21:39:25 +02:00
|
|
|
|
2022-02-28 16:08:26 +01:00
|
|
|
/* *** mesh_validate.cc *** */
|
2013-09-09 04:11:44 +02:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Validates and corrects a Mesh.
|
|
|
|
*
|
|
|
|
* \returns true if a change is made.
|
|
|
|
*/
|
2022-01-07 01:38:08 +01:00
|
|
|
bool BKE_mesh_validate(struct Mesh *me, bool do_verbose, bool cddata_check_mask);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Checks if a Mesh is valid without any modification. This is always verbose.
|
2021-12-29 19:37:01 +01:00
|
|
|
* \returns True if the mesh is valid.
|
2021-12-07 07:19:15 +01:00
|
|
|
*/
|
2018-04-27 06:39:53 +02:00
|
|
|
bool BKE_mesh_is_valid(struct Mesh *me);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* Check all material indices of faces are valid, invalid ones are set to 0.
|
2021-12-29 19:37:01 +01:00
|
|
|
* \returns True if the material indices are valid.
|
2021-12-07 07:19:15 +01:00
|
|
|
*/
|
2018-07-01 15:47:09 +02:00
|
|
|
bool BKE_mesh_validate_material_indices(struct Mesh *me);
|
2013-09-09 04:11:44 +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
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Validate the mesh, \a do_fixes requires \a mesh to be non-null.
|
|
|
|
*
|
|
|
|
* \return false if no changes needed to be made.
|
|
|
|
*
|
|
|
|
* Vertex Normals
|
|
|
|
* ==============
|
|
|
|
*
|
|
|
|
* While zeroed normals are checked, these checks aren't comprehensive.
|
|
|
|
* Technically, to detect errors here a normal recalculation and comparison is necessary.
|
|
|
|
* However this function is mainly to prevent severe errors in geometry
|
|
|
|
* (invalid data that will crash Blender, or cause some features to behave incorrectly),
|
|
|
|
* not to detect subtle differences in the resulting normals which could be caused
|
|
|
|
* by importers that load normals (for example).
|
|
|
|
*/
|
2013-09-04 03:29:34 +02:00
|
|
|
bool BKE_mesh_validate_arrays(struct Mesh *me,
|
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 06:10:43 +01:00
|
|
|
float (*vert_positions)[3],
|
2011-12-08 05:51:03 +01:00
|
|
|
unsigned int 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,
|
2011-12-08 05:51:03 +01:00
|
|
|
unsigned int totedge,
|
2012-06-28 11:08:11 +02:00
|
|
|
struct MFace *mfaces,
|
|
|
|
unsigned int 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,
|
2012-03-15 21:10:07 +01:00
|
|
|
unsigned int totloop,
|
2023-07-24 22:06:55 +02:00
|
|
|
int *face_offsets,
|
|
|
|
unsigned int faces_num,
|
2011-12-08 05:51:03 +01:00
|
|
|
struct MDeformVert *dverts, /* assume totvert length */
|
2022-01-07 01:38:08 +01:00
|
|
|
bool do_verbose,
|
|
|
|
bool do_fixes,
|
2013-09-04 03:29:34 +02:00
|
|
|
bool *r_change);
|
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
|
|
|
#endif
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* \returns is_valid.
|
|
|
|
*/
|
2023-07-25 21:15:52 +02:00
|
|
|
bool BKE_mesh_validate_all_customdata(struct CustomData *vert_data,
|
2022-01-07 01:38:08 +01:00
|
|
|
uint totvert,
|
2023-07-25 21:15:52 +02:00
|
|
|
struct CustomData *edge_data,
|
2022-01-07 01:38:08 +01:00
|
|
|
uint totedge,
|
2023-07-25 21:15:52 +02:00
|
|
|
struct CustomData *loop_data,
|
2022-01-07 01:38:08 +01:00
|
|
|
uint totloop,
|
2018-12-03 16:19:08 +01:00
|
|
|
struct CustomData *pdata,
|
2023-07-24 22:06:55 +02:00
|
|
|
uint faces_num,
|
2022-01-07 01:38:08 +01:00
|
|
|
bool check_meshmask,
|
|
|
|
bool do_verbose,
|
|
|
|
bool do_fixes,
|
2013-09-04 03:29:34 +02:00
|
|
|
bool *r_change);
|
|
|
|
|
2013-09-09 04:11:44 +02:00
|
|
|
void BKE_mesh_strip_loose_faces(struct Mesh *me);
|
2011-02-09 03:28:11 +01:00
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* Calculate edges from faces.
|
2021-12-07 07:19:15 +01:00
|
|
|
*/
|
2022-01-07 01:38:08 +01:00
|
|
|
void BKE_mesh_calc_edges(struct Mesh *mesh, bool keep_existing_edges, bool select_new_edges);
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Calculate/create edges from tessface data
|
|
|
|
*
|
|
|
|
* \param mesh: The mesh to add edges into
|
|
|
|
*/
|
2018-09-25 12:35:43 +02:00
|
|
|
void BKE_mesh_calc_edges_tessface(struct Mesh *mesh);
|
2011-02-09 16:13:20 +01:00
|
|
|
|
2020-12-14 13:00:28 +01:00
|
|
|
/* In DerivedMesh.cc */
|
2022-07-11 17:25:13 +02:00
|
|
|
void BKE_mesh_wrapper_deferred_finalize_mdata(struct Mesh *me_eval,
|
|
|
|
const struct CustomData_MeshMasks *cd_mask_finalize);
|
2020-05-25 12:16:42 +02:00
|
|
|
|
2015-05-12 10:57:11 +02:00
|
|
|
/* **** Depsgraph evaluation **** */
|
|
|
|
|
2018-04-06 12:07:27 +02:00
|
|
|
void BKE_mesh_eval_geometry(struct Depsgraph *depsgraph, struct Mesh *mesh);
|
2015-05-12 10:57:11 +02:00
|
|
|
|
2017-04-21 13:14:11 +02:00
|
|
|
/* Draw Cache */
|
2020-10-09 07:27:18 +02:00
|
|
|
void BKE_mesh_batch_cache_dirty_tag(struct Mesh *me, eMeshBatchDirtyMode mode);
|
2022-11-16 02:15:35 +01:00
|
|
|
void BKE_mesh_batch_cache_free(void *batch_cache);
|
2017-04-21 13:14:11 +02:00
|
|
|
|
2020-10-09 07:27:18 +02:00
|
|
|
extern void (*BKE_mesh_batch_cache_dirty_tag_cb)(struct Mesh *me, eMeshBatchDirtyMode mode);
|
2022-11-16 02:15:35 +01:00
|
|
|
extern void (*BKE_mesh_batch_cache_free_cb)(void *batch_cache);
|
2019-02-23 02:58:39 +01:00
|
|
|
|
2023-07-31 03:50:54 +02:00
|
|
|
/* `mesh_debug.cc` */
|
2022-04-04 05:17:03 +02:00
|
|
|
|
2022-01-19 05:09:48 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
char *BKE_mesh_debug_info(const struct Mesh *me)
|
|
|
|
ATTR_NONNULL(1) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
|
|
|
|
void BKE_mesh_debug_print(const struct Mesh *me) ATTR_NONNULL(1);
|
|
|
|
#endif
|
|
|
|
|
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 18:56:34 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Inline Mesh Data Access
|
|
|
|
* \{ */
|
|
|
|
|
2022-08-31 16:09:01 +02:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* \return The material index for each face. May be null.
|
2023-03-02 04:30:38 +01:00
|
|
|
* \note In C++ code, prefer using the attribute API (#AttributeAccessor).
|
2022-08-31 16:09:01 +02:00
|
|
|
*/
|
|
|
|
BLI_INLINE const int *BKE_mesh_material_indices(const Mesh *mesh)
|
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
return (const int *)CustomData_get_layer_named(
|
|
|
|
&mesh->face_data, CD_PROP_INT32, "material_index");
|
2022-08-31 16:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* \return The material index for each face. Create the layer if it doesn't exist.
|
2023-03-02 04:30:38 +01:00
|
|
|
* \note In C++ code, prefer using the attribute API (#MutableAttributeAccessor).
|
2022-08-31 16:09:01 +02:00
|
|
|
*/
|
|
|
|
BLI_INLINE int *BKE_mesh_material_indices_for_write(Mesh *mesh)
|
|
|
|
{
|
2023-01-14 00:21:20 +01:00
|
|
|
int *indices = (int *)CustomData_get_layer_named_for_write(
|
2023-07-25 21:15:52 +02:00
|
|
|
&mesh->face_data, CD_PROP_INT32, "material_index", mesh->faces_num);
|
2022-08-31 16:09:01 +02:00
|
|
|
if (indices) {
|
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
return (int *)CustomData_add_layer_named(
|
2023-07-25 21:15:52 +02:00
|
|
|
&mesh->face_data, CD_PROP_INT32, CD_SET_DEFAULT, mesh->faces_num, "material_index");
|
2022-08-31 16:09:01 +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 06:10:43 +01:00
|
|
|
BLI_INLINE const float (*BKE_mesh_vert_positions(const Mesh *mesh))[3]
|
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 18:56:34 +02:00
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
return (const float(*)[3])CustomData_get_layer_named(
|
|
|
|
&mesh->vert_data, CD_PROP_FLOAT3, "position");
|
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 18:56:34 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
BLI_INLINE const int *BKE_mesh_face_offsets(const Mesh *mesh)
|
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 18:56:34 +02:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
return mesh->face_offset_indices;
|
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 18:56:34 +02: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
|
|
|
BLI_INLINE const int *BKE_mesh_corner_verts(const Mesh *mesh)
|
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 18:56:34 +02:00
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
return (const int *)CustomData_get_layer_named(&mesh->loop_data, CD_PROP_INT32, ".corner_vert");
|
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 18:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BLI_INLINE const MDeformVert *BKE_mesh_deform_verts(const Mesh *mesh)
|
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
return (const MDeformVert *)CustomData_get_layer(&mesh->vert_data, CD_MDEFORMVERT);
|
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 18:56:34 +02:00
|
|
|
}
|
|
|
|
BLI_INLINE MDeformVert *BKE_mesh_deform_verts_for_write(Mesh *mesh)
|
|
|
|
{
|
2023-01-14 00:21:20 +01:00
|
|
|
MDeformVert *dvert = (MDeformVert *)CustomData_get_layer_for_write(
|
2023-07-25 21:15:52 +02:00
|
|
|
&mesh->vert_data, CD_MDEFORMVERT, mesh->totvert);
|
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 18:56:34 +02:00
|
|
|
if (dvert) {
|
|
|
|
return dvert;
|
|
|
|
}
|
|
|
|
return (MDeformVert *)CustomData_add_layer(
|
2023-07-25 21:15:52 +02:00
|
|
|
&mesh->vert_data, CD_MDEFORMVERT, CD_SET_DEFAULT, mesh->totvert);
|
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 18:56:34 +02:00
|
|
|
}
|
|
|
|
|
2002-10-12 13:37:38 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
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 18:56:34 +02:00
|
|
|
|
|
|
|
/** \} */
|