2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2005 Blender Foundation. All rights reserved. */
|
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;
|
|
|
|
|
for (const MLoop &loop : this->loops()) {
|
|
|
|
|
if (loose_edges[loop.e]) {
|
|
|
|
|
loose_edges[loop.e].reset();
|
|
|
|
|
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();
|
2022-11-18 17:29:24 -06:00
|
|
|
const Span<MPoly> polys = this->polys();
|
|
|
|
|
const Span<MLoop> loops = this->loops();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-11-18 17:29:24 -06:00
|
|
|
r_data.reinitialize(poly_to_tri_count(polys.size(), loops.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)) {
|
2023-03-12 22:29:15 +01:00
|
|
|
blender::bke::mesh::looptris_calc(positions, polys, loops, 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(
|
|
|
|
|
positions, polys, loops, 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,
|
|
|
|
|
const MLoop *mloop,
|
|
|
|
|
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++) {
|
2018-05-17 15:26:59 +02:00
|
|
|
r_verttri[i].tri[0] = mloop[looptri[i].tri[0]].v;
|
|
|
|
|
r_verttri[i].tri[1] = mloop[looptri[i].tri[1]].v;
|
|
|
|
|
r_verttri[i].tri[2] = mloop[looptri[i].tri[2]].v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
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-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 */
|
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: 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
|
|
|
MutableSpan<MEdge> edges = me_eval->edges_for_write();
|
2022-09-07 00:06:31 -05:00
|
|
|
MutableSpan<MPoly> polys = me_eval->polys_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
|
|
|
MutableSpan<MLoop> loops = me_eval->loops_for_write();
|
|
|
|
|
|
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,
|
|
|
|
|
loops.data(),
|
|
|
|
|
loops.size(),
|
|
|
|
|
polys.data(),
|
|
|
|
|
polys.size(),
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
/** \} */
|