2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
2023-02-22 10:16:42 +01:00
|
|
|
* Copyright 2005 Blender Foundation */
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2018-05-08 10:07:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "atomic_ops.h"
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
2018-06-05 16:58:08 +02:00
|
|
|
#include "DNA_object_types.h"
|
2018-05-08 10:07:21 +02:00
|
|
|
|
|
|
|
|
#include "BLI_math_geom.h"
|
2022-04-13 17:51:05 -05:00
|
|
|
#include "BLI_task.hh"
|
2022-11-18 16:05:06 -06:00
|
|
|
#include "BLI_timeit.hh"
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
#include "BKE_bvhutils.h"
|
2022-10-12 20:55:26 -05:00
|
|
|
#include "BKE_editmesh_cache.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2018-06-05 15:59:53 +02:00
|
|
|
#include "BKE_mesh_runtime.h"
|
Shrinkwrap: new mode that projects along the target normal.
The Nearest Surface Point shrink method, while fast, is neither
smooth nor continuous: as the source point moves, the projected
point can both stop and jump. This causes distortions in the
deformation of the shrinkwrap modifier, and the motion of an
animated object with a shrinkwrap constraint.
This patch implements a new mode, which, instead of using the simple
nearest point search, iteratively solves an equation for each triangle
to find a point which has its interpolated normal point to or from the
original vertex. Non-manifold boundary edges are treated as infinitely
thin cylinders that cast normals in all perpendicular directions.
Since this is useful for the constraint, and having multiple
objects with constraints targeting the same guide mesh is a quite
reasonable use case, rather than calculating the mesh boundary edge
data over and over again, it is precomputed and cached in the mesh.
Reviewers: mont29
Differential Revision: https://developer.blender.org/D3836
2018-11-06 21:04:53 +03:00
|
|
|
#include "BKE_shrinkwrap.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_subdiv_ccg.h"
|
2018-05-08 10:07:21 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
using blender::float3;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
using blender::MutableSpan;
|
|
|
|
|
using blender::Span;
|
|
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Runtime Struct Utils
|
|
|
|
|
* \{ */
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2022-11-15 19:15:35 -06:00
|
|
|
namespace blender::bke {
|
|
|
|
|
|
|
|
|
|
static void edit_data_reset(EditMeshData &edit_data)
|
2018-05-09 15:38:58 +02:00
|
|
|
{
|
2022-11-15 19:15:35 -06:00
|
|
|
MEM_SAFE_FREE(edit_data.polyCos);
|
|
|
|
|
MEM_SAFE_FREE(edit_data.polyNos);
|
|
|
|
|
MEM_SAFE_FREE(edit_data.vertexCos);
|
|
|
|
|
MEM_SAFE_FREE(edit_data.vertexNos);
|
2018-05-09 15:38:58 +02:00
|
|
|
}
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2022-11-15 19:15:35 -06:00
|
|
|
static void free_edit_data(MeshRuntime &mesh_runtime)
|
2018-05-11 15:48:14 -03:00
|
|
|
{
|
2022-11-15 19:15:35 -06:00
|
|
|
if (mesh_runtime.edit_data) {
|
|
|
|
|
edit_data_reset(*mesh_runtime.edit_data);
|
|
|
|
|
MEM_freeN(mesh_runtime.edit_data);
|
|
|
|
|
mesh_runtime.edit_data = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_mesh_eval(MeshRuntime &mesh_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (mesh_runtime.mesh_eval != nullptr) {
|
|
|
|
|
mesh_runtime.mesh_eval->edit_mesh = nullptr;
|
|
|
|
|
BKE_id_free(nullptr, mesh_runtime.mesh_eval);
|
|
|
|
|
mesh_runtime.mesh_eval = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_subdiv_ccg(MeshRuntime &mesh_runtime)
|
|
|
|
|
{
|
|
|
|
|
/* TODO(sergey): Does this really belong here? */
|
|
|
|
|
if (mesh_runtime.subdiv_ccg != nullptr) {
|
|
|
|
|
BKE_subdiv_ccg_destroy(mesh_runtime.subdiv_ccg);
|
|
|
|
|
mesh_runtime.subdiv_ccg = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_bvh_cache(MeshRuntime &mesh_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (mesh_runtime.bvh_cache) {
|
|
|
|
|
bvhcache_free(mesh_runtime.bvh_cache);
|
|
|
|
|
mesh_runtime.bvh_cache = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 15:07:31 -04:00
|
|
|
static void reset_normals(MeshRuntime &mesh_runtime)
|
2022-11-15 19:15:35 -06:00
|
|
|
{
|
2023-03-15 15:07:31 -04:00
|
|
|
mesh_runtime.vert_normals.clear_and_shrink();
|
|
|
|
|
mesh_runtime.poly_normals.clear_and_shrink();
|
2022-11-15 19:15:35 -06:00
|
|
|
mesh_runtime.vert_normals_dirty = true;
|
|
|
|
|
mesh_runtime.poly_normals_dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_batch_cache(MeshRuntime &mesh_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (mesh_runtime.batch_cache) {
|
|
|
|
|
BKE_mesh_batch_cache_free(mesh_runtime.batch_cache);
|
|
|
|
|
mesh_runtime.batch_cache = nullptr;
|
2019-05-27 11:45:33 +02:00
|
|
|
}
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 19:15:35 -06:00
|
|
|
MeshRuntime::~MeshRuntime()
|
|
|
|
|
{
|
|
|
|
|
free_mesh_eval(*this);
|
|
|
|
|
free_subdiv_ccg(*this);
|
|
|
|
|
free_bvh_cache(*this);
|
|
|
|
|
free_edit_data(*this);
|
|
|
|
|
free_batch_cache(*this);
|
|
|
|
|
if (this->shrinkwrap_data) {
|
|
|
|
|
BKE_shrinkwrap_boundary_data_free(this->shrinkwrap_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::bke
|
|
|
|
|
|
2022-11-18 16:05:06 -06:00
|
|
|
const blender::bke::LooseEdgeCache &Mesh::loose_edges() const
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
this->runtime->loose_edges_cache.ensure([&](LooseEdgeCache &r_data) {
|
|
|
|
|
blender::BitVector<> &loose_edges = r_data.is_loose_bits;
|
|
|
|
|
loose_edges.resize(0);
|
|
|
|
|
loose_edges.resize(this->totedge, true);
|
|
|
|
|
|
|
|
|
|
int count = this->totedge;
|
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
|
|
|
for (const int edge : this->corner_edges()) {
|
|
|
|
|
if (loose_edges[edge]) {
|
|
|
|
|
loose_edges[edge].reset();
|
2022-11-18 16:05:06 -06:00
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-27 13:09:45 -05:00
|
|
|
if (count == 0) {
|
|
|
|
|
loose_edges.clear_and_shrink();
|
|
|
|
|
}
|
2022-11-18 16:05:06 -06:00
|
|
|
r_data.count = count;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this->runtime->loose_edges_cache.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Mesh::loose_edges_tag_none() const
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
this->runtime->loose_edges_cache.ensure([&](LooseEdgeCache &r_data) {
|
2023-02-27 13:09:45 -05:00
|
|
|
r_data.is_loose_bits.clear_and_shrink();
|
2022-11-18 16:05:06 -06:00
|
|
|
r_data.count = 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 17:29:24 -06:00
|
|
|
blender::Span<MLoopTri> Mesh::looptris() const
|
2018-05-08 10:07:21 +02:00
|
|
|
{
|
2022-11-18 17:29:24 -06:00
|
|
|
this->runtime->looptris_cache.ensure([&](blender::Array<MLoopTri> &r_data) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = this->vert_positions();
|
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
|
|
|
const blender::OffsetIndices polys = this->polys();
|
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 Span<int> corner_verts = this->corner_verts();
|
2019-04-17 06:17:24 +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
|
|
|
r_data.reinitialize(poly_to_tri_count(polys.size(), corner_verts.size()));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-11-18 17:29:24 -06:00
|
|
|
if (BKE_mesh_poly_normals_are_dirty(this)) {
|
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
|
|
|
blender::bke::mesh::looptris_calc(positions, polys, corner_verts, r_data);
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
2022-11-18 17:29:24 -06:00
|
|
|
else {
|
2023-03-12 22:29:15 +01:00
|
|
|
blender::bke::mesh::looptris_calc_with_normals(
|
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
|
|
|
positions, polys, corner_verts, this->poly_normals(), r_data);
|
2022-11-18 17:29:24 -06:00
|
|
|
}
|
|
|
|
|
});
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-11-18 17:29:24 -06:00
|
|
|
return this->runtime->looptris_cache.data();
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-08 19:26:36 +02:00
|
|
|
int BKE_mesh_runtime_looptri_len(const Mesh *mesh)
|
2018-05-08 10:07:21 +02:00
|
|
|
{
|
2022-11-18 17:29:24 -06:00
|
|
|
/* Allow returning the size without calculating the cache. */
|
|
|
|
|
return poly_to_tri_count(mesh->totpoly, mesh->totloop);
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 11:37:01 -05:00
|
|
|
const MLoopTri *BKE_mesh_runtime_looptri_ensure(const Mesh *mesh)
|
2018-05-08 10:07:21 +02:00
|
|
|
{
|
2022-11-18 17:29:24 -06:00
|
|
|
return mesh->looptris().data();
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
2018-05-11 15:48:14 -03:00
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
void BKE_mesh_runtime_verttri_from_looptri(MVertTri *r_verttri,
|
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,
|
2018-06-05 15:54:12 +02:00
|
|
|
const MLoopTri *looptri,
|
|
|
|
|
int looptri_num)
|
2018-05-17 15:26:59 +02:00
|
|
|
{
|
2020-12-27 21:46:57 -06:00
|
|
|
for (int i = 0; i < looptri_num; i++) {
|
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
|
|
|
r_verttri[i].tri[0] = corner_verts[looptri[i].tri[0]];
|
|
|
|
|
r_verttri[i].tri[1] = corner_verts[looptri[i].tri[1]];
|
|
|
|
|
r_verttri[i].tri[2] = corner_verts[looptri[i].tri[2]];
|
2018-05-17 15:26:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh)
|
|
|
|
|
{
|
2022-10-12 20:55:26 -05:00
|
|
|
if (mesh->runtime->edit_data != nullptr) {
|
2018-05-11 15:48:14 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-10-12 20:55:26 -05:00
|
|
|
mesh->runtime->edit_data = MEM_cnew<EditMeshData>(__func__);
|
2018-05-11 15:48:14 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 19:15:35 -06:00
|
|
|
void BKE_mesh_runtime_reset_edit_data(Mesh *mesh)
|
2020-06-24 22:09:40 +10:00
|
|
|
{
|
2022-11-15 19:15:35 -06:00
|
|
|
using namespace blender::bke;
|
|
|
|
|
if (EditMeshData *edit_data = mesh->runtime->edit_data) {
|
|
|
|
|
edit_data_reset(*edit_data);
|
2020-06-24 22:09:40 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 19:15:35 -06:00
|
|
|
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
|
2018-05-11 15:48:14 -03:00
|
|
|
{
|
2022-11-15 19:15:35 -06:00
|
|
|
using namespace blender::bke;
|
|
|
|
|
free_mesh_eval(*mesh->runtime);
|
|
|
|
|
free_batch_cache(*mesh->runtime);
|
|
|
|
|
free_edit_data(*mesh->runtime);
|
|
|
|
|
BKE_mesh_runtime_clear_geometry(mesh);
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_runtime_clear_geometry(Mesh *mesh)
|
|
|
|
|
{
|
2022-11-16 12:45:59 -06:00
|
|
|
/* Tagging shared caches dirty will free the allocated data if there is only one user. */
|
2022-11-15 19:15:35 -06:00
|
|
|
free_bvh_cache(*mesh->runtime);
|
2023-03-15 15:07:31 -04:00
|
|
|
reset_normals(*mesh->runtime);
|
2022-11-15 19:15:35 -06:00
|
|
|
free_subdiv_ccg(*mesh->runtime);
|
2022-11-16 18:30:04 -06:00
|
|
|
mesh->runtime->bounds_cache.tag_dirty();
|
2022-11-18 16:05:06 -06:00
|
|
|
mesh->runtime->loose_edges_cache.tag_dirty();
|
2022-11-18 17:29:24 -06:00
|
|
|
mesh->runtime->looptris_cache.tag_dirty();
|
2023-02-06 13:24:40 -05:00
|
|
|
mesh->runtime->subsurf_face_dot_tags.clear_and_shrink();
|
2023-02-09 15:56:05 +01:00
|
|
|
mesh->runtime->subsurf_optimal_display_edges.clear_and_shrink();
|
2022-11-15 19:15:35 -06:00
|
|
|
if (mesh->runtime->shrinkwrap_data) {
|
|
|
|
|
BKE_shrinkwrap_boundary_data_free(mesh->runtime->shrinkwrap_data);
|
2023-04-13 09:21:10 -04:00
|
|
|
mesh->runtime->shrinkwrap_data = nullptr;
|
2018-09-06 17:06:17 +02:00
|
|
|
}
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
2022-11-26 17:54:05 -06:00
|
|
|
void BKE_mesh_tag_edges_split(struct Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
/* Triangulation didn't change because vertex positions and loop vertex indices didn't change.
|
|
|
|
|
* Face normals didn't change either, but tag those anyway, since there is no API function to
|
|
|
|
|
* only tag vertex normals dirty. */
|
|
|
|
|
free_bvh_cache(*mesh->runtime);
|
2023-03-15 15:07:31 -04:00
|
|
|
reset_normals(*mesh->runtime);
|
2022-11-26 17:54:05 -06:00
|
|
|
free_subdiv_ccg(*mesh->runtime);
|
|
|
|
|
mesh->runtime->loose_edges_cache.tag_dirty();
|
2023-02-06 13:24:40 -05:00
|
|
|
mesh->runtime->subsurf_face_dot_tags.clear_and_shrink();
|
2023-02-09 15:56:05 +01:00
|
|
|
mesh->runtime->subsurf_optimal_display_edges.clear_and_shrink();
|
2022-11-26 17:54:05 -06:00
|
|
|
if (mesh->runtime->shrinkwrap_data) {
|
|
|
|
|
BKE_shrinkwrap_boundary_data_free(mesh->runtime->shrinkwrap_data);
|
2023-04-14 07:00:28 -04:00
|
|
|
mesh->runtime->shrinkwrap_data = nullptr;
|
2022-11-26 17:54:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-03 18:01:16 +02:00
|
|
|
void BKE_mesh_tag_face_winding_changed(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
mesh->runtime->vert_normals_dirty = true;
|
|
|
|
|
mesh->runtime->poly_normals_dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 16:08:48 -05:00
|
|
|
void BKE_mesh_tag_positions_changed(Mesh *mesh)
|
2022-06-23 12:00:25 -05:00
|
|
|
{
|
2023-03-15 14:33:51 -04:00
|
|
|
mesh->runtime->vert_normals_dirty = true;
|
|
|
|
|
mesh->runtime->poly_normals_dirty = true;
|
2022-11-15 19:15:35 -06:00
|
|
|
free_bvh_cache(*mesh->runtime);
|
2022-11-18 17:29:24 -06:00
|
|
|
mesh->runtime->looptris_cache.tag_dirty();
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 13:46:55 -06:00
|
|
|
mesh->runtime->bounds_cache.tag_dirty();
|
2022-06-23 12:00:25 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-27 16:08:48 -05:00
|
|
|
void BKE_mesh_tag_positions_changed_uniformly(Mesh *mesh)
|
2022-06-23 12:00:25 -05:00
|
|
|
{
|
2022-11-15 23:58:34 -06:00
|
|
|
/* The normals and triangulation didn't change, since all verts moved by the same amount. */
|
|
|
|
|
free_bvh_cache(*mesh->runtime);
|
|
|
|
|
mesh->runtime->bounds_cache.tag_dirty();
|
2022-06-23 12:00:25 -05:00
|
|
|
}
|
|
|
|
|
|
2022-11-18 16:05:06 -06:00
|
|
|
void BKE_mesh_tag_topology_changed(struct Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
BKE_mesh_runtime_clear_geometry(mesh);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 20:55:26 -05:00
|
|
|
bool BKE_mesh_is_deformed_only(const Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
return mesh->runtime->deformed_only;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eMeshWrapperType BKE_mesh_wrapper_type(const struct Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
return mesh->runtime->wrapper_type;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Batch Cache Callbacks
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
/* Draw Engine */
|
2023-03-29 14:16:31 +11:00
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
void (*BKE_mesh_batch_cache_dirty_tag_cb)(Mesh *me, eMeshBatchDirtyMode mode) = nullptr;
|
2022-11-15 19:15:35 -06:00
|
|
|
void (*BKE_mesh_batch_cache_free_cb)(void *batch_cache) = nullptr;
|
2018-05-11 15:48:14 -03:00
|
|
|
|
2020-10-09 07:27:18 +02:00
|
|
|
void BKE_mesh_batch_cache_dirty_tag(Mesh *me, eMeshBatchDirtyMode mode)
|
2018-05-11 15:48:14 -03:00
|
|
|
{
|
2022-10-12 20:55:26 -05:00
|
|
|
if (me->runtime->batch_cache) {
|
2018-08-23 10:14:29 -03:00
|
|
|
BKE_mesh_batch_cache_dirty_tag_cb(me, mode);
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-15 19:15:35 -06:00
|
|
|
void BKE_mesh_batch_cache_free(void *batch_cache)
|
2018-05-11 15:48:14 -03:00
|
|
|
{
|
2022-11-15 19:15:35 -06:00
|
|
|
BKE_mesh_batch_cache_free_cb(batch_cache);
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
2018-06-05 15:54:12 +02:00
|
|
|
|
|
|
|
|
/** \} */
|
2018-06-22 17:54:18 +02:00
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
2022-01-19 15:09:48 +11:00
|
|
|
/** \name Mesh Runtime Validation
|
2018-06-22 17:54:18 +02:00
|
|
|
* \{ */
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
|
|
|
|
|
bool BKE_mesh_runtime_is_valid(Mesh *me_eval)
|
|
|
|
|
{
|
|
|
|
|
const bool do_verbose = true;
|
|
|
|
|
const bool do_fixes = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
bool is_valid = true;
|
|
|
|
|
bool changed = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
if (do_verbose) {
|
|
|
|
|
printf("MESH: %s\n", me_eval->id.name + 2);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
MutableSpan<float3> positions = me_eval->vert_positions_for_write();
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
MutableSpan<blender::int2> edges = me_eval->edges_for_write();
|
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
|
|
|
MutableSpan<int> poly_offsets = me_eval->poly_offsets_for_write();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
MutableSpan<int> corner_verts = me_eval->corner_verts_for_write();
|
|
|
|
|
MutableSpan<int> corner_edges = me_eval->corner_edges_for_write();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
is_valid &= BKE_mesh_validate_all_customdata(
|
2018-12-03 16:19:08 +01:00
|
|
|
&me_eval->vdata,
|
|
|
|
|
me_eval->totvert,
|
|
|
|
|
&me_eval->edata,
|
|
|
|
|
me_eval->totedge,
|
|
|
|
|
&me_eval->ldata,
|
|
|
|
|
me_eval->totloop,
|
|
|
|
|
&me_eval->pdata,
|
|
|
|
|
me_eval->totpoly,
|
2018-07-13 08:37:20 +02:00
|
|
|
false, /* setting mask here isn't useful, gives false positives */
|
|
|
|
|
do_verbose,
|
|
|
|
|
do_fixes,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-14 14:15:21 +01:00
|
|
|
is_valid &= BKE_mesh_validate_arrays(me_eval,
|
|
|
|
|
reinterpret_cast<float(*)[3]>(positions.data()),
|
|
|
|
|
positions.size(),
|
|
|
|
|
edges.data(),
|
|
|
|
|
edges.size(),
|
|
|
|
|
static_cast<MFace *>(CustomData_get_layer_for_write(
|
|
|
|
|
&me_eval->fdata, CD_MFACE, me_eval->totface)),
|
|
|
|
|
me_eval->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
|
|
|
corner_verts.data(),
|
|
|
|
|
corner_edges.data(),
|
|
|
|
|
corner_verts.size(),
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
poly_offsets.data(),
|
|
|
|
|
me_eval->totpoly,
|
2023-01-14 14:15:21 +01:00
|
|
|
me_eval->deform_verts_for_write().data(),
|
|
|
|
|
do_verbose,
|
|
|
|
|
do_fixes,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
BLI_assert(changed == false);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
return is_valid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* NDEBUG */
|
|
|
|
|
|
|
|
|
|
/** \} */
|