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 */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2011-02-27 20:40:57 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2020-08-28 16:10:17 +02:00
|
|
|
/* Allow using deprecated functionality for .blend file I/O. */
|
2020-08-28 15:45:11 +02:00
|
|
|
#define DNA_DEPRECATED_ALLOW
|
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_defaults.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "DNA_key_types.h"
|
2020-05-07 15:55:52 +02:00
|
|
|
#include "DNA_material_types.h"
|
2012-02-19 22:17:30 +00:00
|
|
|
#include "DNA_mesh_types.h"
|
2018-06-26 17:45:00 +02:00
|
|
|
#include "DNA_meshdata_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_object_types.h"
|
2002-10-12 11:37:38 +00: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
|
|
|
#include "BLI_bounds.hh"
|
2020-08-28 15:45:11 +02:00
|
|
|
#include "BLI_endian_switch.h"
|
2020-03-05 14:53:23 +01:00
|
|
|
#include "BLI_ghash.h"
|
|
|
|
|
#include "BLI_hash.h"
|
2023-04-14 17:58:13 +02:00
|
|
|
#include "BLI_implicit_sharing.hh"
|
2021-12-22 11:04:03 -06:00
|
|
|
#include "BLI_index_range.hh"
|
2017-02-22 09:40:46 +01:00
|
|
|
#include "BLI_linklist.h"
|
2021-07-13 12:10:34 -04:00
|
|
|
#include "BLI_listbase.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_matrix.h"
|
2022-02-15 10:27:03 -06:00
|
|
|
#include "BLI_math_vector.hh"
|
2017-02-22 09:40:46 +01:00
|
|
|
#include "BLI_memarena.h"
|
2023-08-29 17:00:33 +02:00
|
|
|
#include "BLI_ordered_edge.hh"
|
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
|
|
|
#include "BLI_resource_scope.hh"
|
2023-08-29 17:00:33 +02:00
|
|
|
#include "BLI_set.hh"
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. 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 by default,
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 the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
#include "BLI_span.hh"
|
2013-09-20 11:14:08 +00:00
|
|
|
#include "BLI_string.h"
|
2021-12-22 11:04:03 -06:00
|
|
|
#include "BLI_task.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2022-05-29 11:02:10 +02:00
|
|
|
#include "BLI_vector.hh"
|
2022-08-31 09:09:01 -05:00
|
|
|
#include "BLI_virtual_array.hh"
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2020-03-06 15:38:52 +01:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
2020-04-03 13:07:36 +02:00
|
|
|
#include "BKE_anim_data.h"
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. 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 by default,
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 the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
#include "BKE_attribute.hh"
|
Refactor BKE_bpath module.
The main goal of this refactor is to make BPath module use `IDTypeInfo`,
and move each ID-specific part of the `foreach_path` looper into their
own IDTypeInfo struct, using a new `foreach_path` callback.
Additionally, following improvements/cleanups are included:
* Attempt to get better, more consistent namings.
** In particular, move from `path_visitor` to more standard `foreach_path`.
* Update and extend documentation.
** API doc was moved to header, according to recent discussions on this
topic.
* Remove `BKE_bpath_relocate_visitor` from API, this is specific
callback that belongs in `lib_id.c` user code.
NOTE: This commit is expected to be 100% non-behavioral-change. This
implies that several potential further changes were only noted as
comments (like using a more generic solution for
`lib_id_library_local_paths`, addressing inconsistencies like path of
packed libraries always being skipped, regardless of the
`BKE_BPATH_FOREACH_PATH_SKIP_PACKED` `eBPathForeachFlag` flag value,
etc.).
NOTE: basic unittests were added to master already in
rBdcc500e5a265093bc9cc.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D13381
2021-11-29 14:20:58 +01:00
|
|
|
#include "BKE_bpath.h"
|
2020-08-28 15:45:11 +02:00
|
|
|
#include "BKE_deform.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_editmesh.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_global.h"
|
2020-03-06 15:38:52 +01:00
|
|
|
#include "BKE_idtype.h"
|
2019-07-30 18:38:31 +02:00
|
|
|
#include "BKE_key.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2020-05-07 15:55:52 +02:00
|
|
|
#include "BKE_lib_query.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_main.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_material.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_legacy_convert.hh"
|
|
|
|
|
#include "BKE_mesh_runtime.hh"
|
|
|
|
|
#include "BKE_mesh_wrapper.hh"
|
2010-09-09 00:14:51 +00:00
|
|
|
#include "BKE_modifier.h"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_multires.hh"
|
2023-10-09 23:41:53 +02:00
|
|
|
#include "BKE_object.hh"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2020-03-05 14:53:23 +01:00
|
|
|
#include "PIL_time.h"
|
|
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph.hh"
|
|
|
|
|
#include "DEG_depsgraph_query.hh"
|
2009-05-26 04:17:47 +00:00
|
|
|
|
2023-08-28 15:01:05 +02:00
|
|
|
#include "BLO_read_write.hh"
|
2020-08-28 15:45:11 +02:00
|
|
|
|
2022-02-22 12:43:53 -05:00
|
|
|
using blender::float3;
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. 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 by default,
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 the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
using blender::MutableSpan;
|
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::Span;
|
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 09:38:37 -05:00
|
|
|
using blender::StringRef;
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. 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 by default,
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 the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
using blender::VArray;
|
2022-05-29 11:02:10 +02:00
|
|
|
using blender::Vector;
|
2022-02-22 12:43:53 -05:00
|
|
|
|
2020-03-06 15:38:52 +01:00
|
|
|
static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata);
|
|
|
|
|
|
|
|
|
|
static void mesh_init_data(ID *id)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = reinterpret_cast<Mesh *>(id);
|
2020-03-06 15:38:52 +01:00
|
|
|
|
|
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(mesh, id));
|
|
|
|
|
|
|
|
|
|
MEMCPY_STRUCT_AFTER(mesh, DNA_struct_default_get(Mesh), id);
|
|
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_reset(&mesh->vert_data);
|
|
|
|
|
CustomData_reset(&mesh->edge_data);
|
2023-07-24 22:06:55 +02:00
|
|
|
CustomData_reset(&mesh->fdata_legacy);
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_reset(&mesh->face_data);
|
|
|
|
|
CustomData_reset(&mesh->loop_data);
|
2020-03-06 15:38:52 +01:00
|
|
|
|
2022-10-12 20:55:26 -05:00
|
|
|
mesh->runtime = new blender::bke::MeshRuntime();
|
2020-03-06 15:38:52 +01:00
|
|
|
|
|
|
|
|
mesh->face_sets_color_seed = BLI_hash_int(PIL_check_seconds_timer_i() & UINT_MAX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mesh_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh_dst = reinterpret_cast<Mesh *>(id_dst);
|
|
|
|
|
const Mesh *mesh_src = reinterpret_cast<const Mesh *>(id_src);
|
2020-03-06 15:38:52 +01:00
|
|
|
|
2022-10-12 20:55:26 -05:00
|
|
|
mesh_dst->runtime = new blender::bke::MeshRuntime();
|
|
|
|
|
mesh_dst->runtime->deformed_only = mesh_src->runtime->deformed_only;
|
|
|
|
|
mesh_dst->runtime->wrapper_type = mesh_src->runtime->wrapper_type;
|
|
|
|
|
mesh_dst->runtime->wrapper_type_finalize = mesh_src->runtime->wrapper_type_finalize;
|
|
|
|
|
mesh_dst->runtime->subsurf_runtime_data = mesh_src->runtime->subsurf_runtime_data;
|
|
|
|
|
mesh_dst->runtime->cd_mask_extra = mesh_src->runtime->cd_mask_extra;
|
2023-02-09 15:56:05 +01:00
|
|
|
/* Copy face dot tags and edge tags, since meshes may be duplicated after a subsurf modifier or
|
|
|
|
|
* node, but we still need to be able to draw face center vertices and "optimal edges"
|
|
|
|
|
* differently. The tags may be cleared explicitly when the topology is changed. */
|
2023-02-06 13:24:40 -05:00
|
|
|
mesh_dst->runtime->subsurf_face_dot_tags = mesh_src->runtime->subsurf_face_dot_tags;
|
2023-02-09 15:56:05 +01:00
|
|
|
mesh_dst->runtime->subsurf_optimal_display_edges =
|
|
|
|
|
mesh_src->runtime->subsurf_optimal_display_edges;
|
2020-03-06 15:38:52 +01:00
|
|
|
if ((mesh_src->id.tag & LIB_TAG_NO_MAIN) == 0) {
|
|
|
|
|
/* This is a direct copy of a main mesh, so for now it has the same topology. */
|
2022-10-12 20:55:26 -05:00
|
|
|
mesh_dst->runtime->deformed_only = true;
|
2020-03-06 15:38:52 +01:00
|
|
|
}
|
2023-04-23 15:16:30 -04:00
|
|
|
/* This option is set for run-time meshes that have been copied from the current object's mode.
|
2020-08-11 21:46:06 +10:00
|
|
|
* Currently this is used for edit-mesh although it could be used for sculpt or other
|
2023-04-23 15:16:30 -04:00
|
|
|
* kinds of data specific to an object's mode.
|
2020-08-11 21:46:06 +10:00
|
|
|
*
|
|
|
|
|
* The flag signals that the mesh hasn't been modified from the data that generated it,
|
|
|
|
|
* allowing us to use the object-mode data for drawing.
|
|
|
|
|
*
|
2023-04-23 15:16:30 -04:00
|
|
|
* While this could be the caller's responsibility, keep here since it's
|
2020-08-11 21:46:06 +10:00
|
|
|
* highly unlikely we want to create a duplicate and not use it for drawing. */
|
2022-10-12 20:55:26 -05:00
|
|
|
mesh_dst->runtime->is_original_bmesh = false;
|
2020-03-06 15:38:52 +01:00
|
|
|
|
2022-11-18 17:02:30 -06:00
|
|
|
/* Share various derived caches between the source and destination mesh for improved performance
|
|
|
|
|
* when the source is persistent and edits to the destination mesh don't affect the caches.
|
|
|
|
|
* Caches will be "un-shared" as necessary later on. */
|
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_dst->runtime->bounds_cache = mesh_src->runtime->bounds_cache;
|
2023-08-25 23:06:06 +02:00
|
|
|
mesh_dst->runtime->vert_normals_cache = mesh_src->runtime->vert_normals_cache;
|
|
|
|
|
mesh_dst->runtime->face_normals_cache = mesh_src->runtime->face_normals_cache;
|
2023-04-22 13:46:11 +02:00
|
|
|
mesh_dst->runtime->loose_verts_cache = mesh_src->runtime->loose_verts_cache;
|
|
|
|
|
mesh_dst->runtime->verts_no_face_cache = mesh_src->runtime->verts_no_face_cache;
|
2022-11-18 16:05:06 -06:00
|
|
|
mesh_dst->runtime->loose_edges_cache = mesh_src->runtime->loose_edges_cache;
|
2022-11-18 17:29:24 -06:00
|
|
|
mesh_dst->runtime->looptris_cache = mesh_src->runtime->looptris_cache;
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh_dst->runtime->looptri_faces_cache = mesh_src->runtime->looptri_faces_cache;
|
2023-08-30 23:41:59 +02:00
|
|
|
mesh_dst->runtime->vert_to_face_offset_cache = mesh_src->runtime->vert_to_face_offset_cache;
|
|
|
|
|
mesh_dst->runtime->vert_to_face_map_cache = mesh_src->runtime->vert_to_face_map_cache;
|
|
|
|
|
mesh_dst->runtime->vert_to_corner_map_cache = mesh_src->runtime->vert_to_corner_map_cache;
|
|
|
|
|
mesh_dst->runtime->corner_to_face_map_cache = mesh_src->runtime->corner_to_face_map_cache;
|
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
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Only do tessface if we have no faces. */
|
|
|
|
|
const bool do_tessface = ((mesh_src->totface_legacy != 0) && (mesh_src->faces_num == 0));
|
2020-03-06 15:38:52 +01:00
|
|
|
|
|
|
|
|
CustomData_MeshMasks mask = CD_MASK_MESH;
|
|
|
|
|
|
|
|
|
|
if (mesh_src->id.tag & LIB_TAG_NO_MAIN) {
|
2022-06-01 15:11:56 +10:00
|
|
|
/* For copies in depsgraph, keep data like #CD_ORIGINDEX and #CD_ORCO. */
|
2020-03-06 15:38:52 +01:00
|
|
|
CustomData_MeshMasks_update(&mask, &CD_MASK_DERIVEDMESH);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
mesh_dst->mat = (Material **)MEM_dupallocN(mesh_src->mat);
|
2020-03-06 15:38:52 +01:00
|
|
|
|
2021-07-13 12:10:34 -04:00
|
|
|
BKE_defgroup_copy_list(&mesh_dst->vertex_group_names, &mesh_src->vertex_group_names);
|
2022-12-15 14:18:57 -06:00
|
|
|
mesh_dst->active_color_attribute = static_cast<char *>(
|
|
|
|
|
MEM_dupallocN(mesh_src->active_color_attribute));
|
|
|
|
|
mesh_dst->default_color_attribute = static_cast<char *>(
|
|
|
|
|
MEM_dupallocN(mesh_src->default_color_attribute));
|
2021-07-13 12:10:34 -04:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_copy(&mesh_src->vert_data, &mesh_dst->vert_data, mask.vmask, mesh_dst->totvert);
|
|
|
|
|
CustomData_copy(&mesh_src->edge_data, &mesh_dst->edge_data, mask.emask, mesh_dst->totedge);
|
|
|
|
|
CustomData_copy(&mesh_src->loop_data, &mesh_dst->loop_data, mask.lmask, mesh_dst->totloop);
|
|
|
|
|
CustomData_copy(&mesh_src->face_data, &mesh_dst->face_data, mask.pmask, mesh_dst->faces_num);
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::implicit_sharing::copy_shared_pointer(mesh_src->face_offset_indices,
|
|
|
|
|
mesh_src->runtime->face_offsets_sharing_info,
|
|
|
|
|
&mesh_dst->face_offset_indices,
|
|
|
|
|
&mesh_dst->runtime->face_offsets_sharing_info);
|
2020-03-06 15:38:52 +01:00
|
|
|
if (do_tessface) {
|
2023-07-24 16:32:08 -04:00
|
|
|
CustomData_copy(
|
|
|
|
|
&mesh_src->fdata_legacy, &mesh_dst->fdata_legacy, mask.fmask, mesh_dst->totface_legacy);
|
2020-03-06 15:38:52 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
mesh_tessface_clear_intern(mesh_dst, false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
mesh_dst->edit_mesh = nullptr;
|
2020-03-06 15:38:52 +01:00
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
mesh_dst->mselect = (MSelect *)MEM_dupallocN(mesh_dst->mselect);
|
2020-03-06 15:38:52 +01:00
|
|
|
|
2021-07-03 23:08:40 +10:00
|
|
|
/* TODO: Do we want to add flag to prevent this? */
|
2020-03-06 15:38:52 +01:00
|
|
|
if (mesh_src->key && (flag & LIB_ID_COPY_SHAPEKEY)) {
|
|
|
|
|
BKE_id_copy_ex(bmain, &mesh_src->key->id, (ID **)&mesh_dst->key, flag);
|
|
|
|
|
/* XXX This is not nice, we need to make BKE_id_copy_ex fully re-entrant... */
|
|
|
|
|
mesh_dst->key->from = &mesh_dst->id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_mesh_free_editmesh(Mesh *mesh)
|
2022-01-11 15:42:07 +01:00
|
|
|
{
|
|
|
|
|
if (mesh->edit_mesh == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mesh->edit_mesh->is_shallow_copy == false) {
|
|
|
|
|
BKE_editmesh_free_data(mesh->edit_mesh);
|
|
|
|
|
}
|
|
|
|
|
MEM_freeN(mesh->edit_mesh);
|
|
|
|
|
mesh->edit_mesh = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 15:38:52 +01:00
|
|
|
static void mesh_free_data(ID *id)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = reinterpret_cast<Mesh *>(id);
|
2020-03-09 18:40:06 +01:00
|
|
|
|
2022-01-11 15:42:07 +01:00
|
|
|
BKE_mesh_free_editmesh(mesh);
|
2021-09-02 15:29:34 +10:00
|
|
|
|
2023-04-12 11:27:52 -04:00
|
|
|
BKE_mesh_clear_geometry_and_metadata(mesh);
|
2020-03-06 15:38:52 +01:00
|
|
|
MEM_SAFE_FREE(mesh->mat);
|
2022-10-12 20:55:26 -05:00
|
|
|
|
|
|
|
|
delete mesh->runtime;
|
2020-03-06 15:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-07 15:55:52 +02:00
|
|
|
static void mesh_foreach_id(ID *id, LibraryForeachIDData *data)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = reinterpret_cast<Mesh *>(id);
|
2023-08-22 15:42:19 +02:00
|
|
|
const int flag = BKE_lib_query_foreachid_process_flags_get(data);
|
|
|
|
|
|
2021-10-26 10:40:36 +02:00
|
|
|
BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, mesh->texcomesh, IDWALK_CB_NEVER_SELF);
|
|
|
|
|
BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, mesh->key, IDWALK_CB_USER);
|
2020-05-07 15:55:52 +02:00
|
|
|
for (int i = 0; i < mesh->totcol; i++) {
|
2021-10-26 10:40:36 +02:00
|
|
|
BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, mesh->mat[i], IDWALK_CB_USER);
|
2020-05-07 15:55:52 +02:00
|
|
|
}
|
2023-08-22 15:42:19 +02:00
|
|
|
|
|
|
|
|
if (flag & IDWALK_DO_DEPRECATED_POINTERS) {
|
|
|
|
|
BKE_LIB_FOREACHID_PROCESS_ID_NOCHECK(data, mesh->ipo, IDWALK_CB_USER);
|
|
|
|
|
}
|
2020-05-07 15:55:52 +02:00
|
|
|
}
|
|
|
|
|
|
Refactor BKE_bpath module.
The main goal of this refactor is to make BPath module use `IDTypeInfo`,
and move each ID-specific part of the `foreach_path` looper into their
own IDTypeInfo struct, using a new `foreach_path` callback.
Additionally, following improvements/cleanups are included:
* Attempt to get better, more consistent namings.
** In particular, move from `path_visitor` to more standard `foreach_path`.
* Update and extend documentation.
** API doc was moved to header, according to recent discussions on this
topic.
* Remove `BKE_bpath_relocate_visitor` from API, this is specific
callback that belongs in `lib_id.c` user code.
NOTE: This commit is expected to be 100% non-behavioral-change. This
implies that several potential further changes were only noted as
comments (like using a more generic solution for
`lib_id_library_local_paths`, addressing inconsistencies like path of
packed libraries always being skipped, regardless of the
`BKE_BPATH_FOREACH_PATH_SKIP_PACKED` `eBPathForeachFlag` flag value,
etc.).
NOTE: basic unittests were added to master already in
rBdcc500e5a265093bc9cc.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D13381
2021-11-29 14:20:58 +01:00
|
|
|
static void mesh_foreach_path(ID *id, BPathForeachPathData *bpath_data)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *me = reinterpret_cast<Mesh *>(id);
|
2023-07-25 21:15:52 +02:00
|
|
|
if (me->loop_data.external) {
|
2023-06-23 10:09:01 +10:00
|
|
|
BKE_bpath_foreach_path_fixed_process(
|
2023-07-25 21:15:52 +02:00
|
|
|
bpath_data, me->loop_data.external->filepath, sizeof(me->loop_data.external->filepath));
|
Refactor BKE_bpath module.
The main goal of this refactor is to make BPath module use `IDTypeInfo`,
and move each ID-specific part of the `foreach_path` looper into their
own IDTypeInfo struct, using a new `foreach_path` callback.
Additionally, following improvements/cleanups are included:
* Attempt to get better, more consistent namings.
** In particular, move from `path_visitor` to more standard `foreach_path`.
* Update and extend documentation.
** API doc was moved to header, according to recent discussions on this
topic.
* Remove `BKE_bpath_relocate_visitor` from API, this is specific
callback that belongs in `lib_id.c` user code.
NOTE: This commit is expected to be 100% non-behavioral-change. This
implies that several potential further changes were only noted as
comments (like using a more generic solution for
`lib_id_library_local_paths`, addressing inconsistencies like path of
packed libraries always being skipped, regardless of the
`BKE_BPATH_FOREACH_PATH_SKIP_PACKED` `eBPathForeachFlag` flag value,
etc.).
NOTE: basic unittests were added to master already in
rBdcc500e5a265093bc9cc.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D13381
2021-11-29 14:20:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 15:45:11 +02:00
|
|
|
static void mesh_blend_write(BlendWriter *writer, ID *id, const void *id_address)
|
|
|
|
|
{
|
2022-08-23 13:50:47 -04:00
|
|
|
using namespace blender;
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = reinterpret_cast<Mesh *>(id);
|
2020-12-14 11:28:08 +01:00
|
|
|
const bool is_undo = BLO_write_is_undo(writer);
|
2021-08-19 11:13:55 +02:00
|
|
|
|
2022-05-29 11:02:10 +02:00
|
|
|
Vector<CustomDataLayer, 16> vert_layers;
|
|
|
|
|
Vector<CustomDataLayer, 16> edge_layers;
|
|
|
|
|
Vector<CustomDataLayer, 16> loop_layers;
|
2023-07-24 22:06:55 +02:00
|
|
|
Vector<CustomDataLayer, 16> face_layers;
|
2021-08-19 11:13:55 +02:00
|
|
|
|
2023-07-07 07:35:56 -04:00
|
|
|
/* Cache only - don't write. */
|
2021-11-06 19:16:37 +01:00
|
|
|
mesh->mface = nullptr;
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->totface_legacy = 0;
|
|
|
|
|
memset(&mesh->fdata_legacy, 0, sizeof(mesh->fdata_legacy));
|
2021-08-19 11:13:55 +02:00
|
|
|
|
|
|
|
|
/* Do not store actual geometry data in case this is a library override ID. */
|
|
|
|
|
if (ID_IS_OVERRIDE_LIBRARY(mesh) && !is_undo) {
|
|
|
|
|
mesh->totvert = 0;
|
2023-07-25 21:15:52 +02:00
|
|
|
memset(&mesh->vert_data, 0, sizeof(mesh->vert_data));
|
2021-08-19 11:13:55 +02:00
|
|
|
|
|
|
|
|
mesh->totedge = 0;
|
2023-07-25 21:15:52 +02:00
|
|
|
memset(&mesh->edge_data, 0, sizeof(mesh->edge_data));
|
2021-08-19 11:13:55 +02:00
|
|
|
|
|
|
|
|
mesh->totloop = 0;
|
2023-07-25 21:15:52 +02:00
|
|
|
memset(&mesh->loop_data, 0, sizeof(mesh->loop_data));
|
2021-08-19 11:13:55 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->faces_num = 0;
|
2023-07-25 21:15:52 +02:00
|
|
|
memset(&mesh->face_data, 0, sizeof(mesh->face_data));
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->face_offset_indices = nullptr;
|
2021-08-19 11:13:55 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_blend_write_prepare(mesh->vert_data, vert_layers, {});
|
|
|
|
|
CustomData_blend_write_prepare(mesh->edge_data, edge_layers, {});
|
|
|
|
|
CustomData_blend_write_prepare(mesh->loop_data, loop_layers, {});
|
|
|
|
|
CustomData_blend_write_prepare(mesh->face_data, face_layers, {});
|
2021-08-19 11:13:55 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-18 16:24:15 -06:00
|
|
|
mesh->runtime = nullptr;
|
|
|
|
|
|
2021-08-19 11:13:55 +02:00
|
|
|
BLO_write_id_struct(writer, Mesh, id_address, &mesh->id);
|
|
|
|
|
BKE_id_blend_write(writer, &mesh->id);
|
|
|
|
|
|
|
|
|
|
BKE_defbase_blend_write(writer, &mesh->vertex_group_names);
|
2022-12-15 14:18:57 -06:00
|
|
|
BLO_write_string(writer, mesh->active_color_attribute);
|
|
|
|
|
BLO_write_string(writer, mesh->default_color_attribute);
|
2021-08-19 11:13:55 +02:00
|
|
|
|
|
|
|
|
BLO_write_pointer_array(writer, mesh->totcol, mesh->mat);
|
|
|
|
|
BLO_write_raw(writer, sizeof(MSelect) * mesh->totselect, mesh->mselect);
|
|
|
|
|
|
|
|
|
|
CustomData_blend_write(
|
2023-07-25 21:15:52 +02:00
|
|
|
writer, &mesh->vert_data, vert_layers, mesh->totvert, CD_MASK_MESH.vmask, &mesh->id);
|
2021-08-19 11:13:55 +02:00
|
|
|
CustomData_blend_write(
|
2023-07-25 21:15:52 +02:00
|
|
|
writer, &mesh->edge_data, edge_layers, mesh->totedge, CD_MASK_MESH.emask, &mesh->id);
|
2023-07-07 07:35:56 -04:00
|
|
|
/* `fdata` is cleared above but written so slots align. */
|
2023-07-24 22:06:55 +02:00
|
|
|
CustomData_blend_write(
|
|
|
|
|
writer, &mesh->fdata_legacy, {}, mesh->totface_legacy, CD_MASK_MESH.fmask, &mesh->id);
|
2021-08-19 11:13:55 +02:00
|
|
|
CustomData_blend_write(
|
2023-07-25 21:15:52 +02:00
|
|
|
writer, &mesh->loop_data, loop_layers, mesh->totloop, CD_MASK_MESH.lmask, &mesh->id);
|
2021-08-19 11:13:55 +02:00
|
|
|
CustomData_blend_write(
|
2023-07-25 21:15:52 +02:00
|
|
|
writer, &mesh->face_data, face_layers, mesh->faces_num, CD_MASK_MESH.pmask, &mesh->id);
|
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
|
|
|
if (mesh->face_offset_indices) {
|
|
|
|
|
BLO_write_int32_array(writer, mesh->faces_num + 1, mesh->face_offset_indices);
|
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
|
|
|
}
|
2020-08-28 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mesh_blend_read_data(BlendDataReader *reader, ID *id)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = reinterpret_cast<Mesh *>(id);
|
2020-08-28 15:45:11 +02:00
|
|
|
BLO_read_pointer_array(reader, (void **)&mesh->mat);
|
2023-08-17 19:45:09 +02:00
|
|
|
/* This check added for python created meshes. */
|
|
|
|
|
if (!mesh->mat) {
|
|
|
|
|
mesh->totcol = 0;
|
|
|
|
|
}
|
2020-08-28 15:45:11 +02:00
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
/* Deprecated pointers to custom data layers are read here for backward compatibility
|
|
|
|
|
* with files where these were owning pointers rather than a view into custom data. */
|
2020-08-28 15:45:11 +02:00
|
|
|
BLO_read_data_address(reader, &mesh->mvert);
|
|
|
|
|
BLO_read_data_address(reader, &mesh->medge);
|
|
|
|
|
BLO_read_data_address(reader, &mesh->mface);
|
|
|
|
|
BLO_read_data_address(reader, &mesh->mtface);
|
|
|
|
|
BLO_read_data_address(reader, &mesh->dvert);
|
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
|
|
|
BLO_read_data_address(reader, &mesh->tface);
|
|
|
|
|
BLO_read_data_address(reader, &mesh->mcol);
|
|
|
|
|
|
2020-08-28 15:45:11 +02:00
|
|
|
BLO_read_data_address(reader, &mesh->mselect);
|
|
|
|
|
|
2021-07-13 12:10:34 -04:00
|
|
|
BLO_read_list(reader, &mesh->vertex_group_names);
|
2020-08-28 15:45:11 +02:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_blend_read(reader, &mesh->vert_data, mesh->totvert);
|
|
|
|
|
CustomData_blend_read(reader, &mesh->edge_data, mesh->totedge);
|
2023-07-24 22:06:55 +02:00
|
|
|
CustomData_blend_read(reader, &mesh->fdata_legacy, mesh->totface_legacy);
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_blend_read(reader, &mesh->loop_data, mesh->totloop);
|
|
|
|
|
CustomData_blend_read(reader, &mesh->face_data, mesh->faces_num);
|
2022-09-07 14:33:29 -05:00
|
|
|
if (mesh->deform_verts().is_empty()) {
|
|
|
|
|
/* Vertex group data was also an owning pointer in old Blender versions.
|
|
|
|
|
* Don't read them again if they were read as part of #CustomData. */
|
|
|
|
|
BKE_defvert_blend_read(reader, mesh->totvert, mesh->dvert);
|
|
|
|
|
}
|
2022-12-15 14:18:57 -06:00
|
|
|
BLO_read_data_address(reader, &mesh->active_color_attribute);
|
|
|
|
|
BLO_read_data_address(reader, &mesh->default_color_attribute);
|
2020-08-28 15:45:11 +02:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
mesh->texspace_flag &= ~ME_TEXSPACE_FLAG_AUTO_EVALUATED;
|
2021-11-06 19:16:37 +01:00
|
|
|
mesh->edit_mesh = nullptr;
|
2021-11-10 13:50:00 +01:00
|
|
|
|
2022-10-12 20:55:26 -05:00
|
|
|
mesh->runtime = new blender::bke::MeshRuntime();
|
2020-08-28 15:45:11 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (mesh->face_offset_indices) {
|
|
|
|
|
BLO_read_int32_array(reader, mesh->faces_num + 1, &mesh->face_offset_indices);
|
|
|
|
|
mesh->runtime->face_offsets_sharing_info = blender::implicit_sharing::info_for_mem_free(
|
|
|
|
|
mesh->face_offset_indices);
|
2023-04-14 17:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
if (mesh->mselect == nullptr) {
|
2020-08-28 15:45:11 +02:00
|
|
|
mesh->totselect = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 11:45:52 +10:00
|
|
|
if (BLO_read_requires_endian_switch(reader) && mesh->tface) {
|
2020-08-28 15:45:11 +02:00
|
|
|
TFace *tf = mesh->tface;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (int i = 0; i < mesh->totface_legacy; i++, tf++) {
|
2020-08-28 15:45:11 +02:00
|
|
|
BLI_endian_switch_uint32_array(tf->col, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 15:38:52 +01:00
|
|
|
IDTypeInfo IDType_ID_ME = {
|
2023-01-16 12:41:11 +11:00
|
|
|
/*id_code*/ ID_ME,
|
|
|
|
|
/*id_filter*/ FILTER_ID_ME,
|
|
|
|
|
/*main_listbase_index*/ INDEX_ID_ME,
|
|
|
|
|
/*struct_size*/ sizeof(Mesh),
|
|
|
|
|
/*name*/ "Mesh",
|
2023-10-04 02:53:31 +02:00
|
|
|
/*name_plural*/ N_("meshes"),
|
2023-01-16 12:41:11 +11:00
|
|
|
/*translation_context*/ BLT_I18NCONTEXT_ID_MESH,
|
|
|
|
|
/*flags*/ IDTYPE_FLAGS_APPEND_IS_REUSABLE,
|
|
|
|
|
/*asset_type_info*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*init_data*/ mesh_init_data,
|
|
|
|
|
/*copy_data*/ mesh_copy_data,
|
|
|
|
|
/*free_data*/ mesh_free_data,
|
|
|
|
|
/*make_local*/ nullptr,
|
|
|
|
|
/*foreach_id*/ mesh_foreach_id,
|
|
|
|
|
/*foreach_cache*/ nullptr,
|
|
|
|
|
/*foreach_path*/ mesh_foreach_path,
|
|
|
|
|
/*owner_pointer_get*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*blend_write*/ mesh_blend_write,
|
|
|
|
|
/*blend_read_data*/ mesh_blend_read_data,
|
2023-03-11 18:07:59 +01:00
|
|
|
/*blend_read_after_liblink*/ nullptr,
|
2023-01-16 12:41:11 +11:00
|
|
|
|
|
|
|
|
/*blend_read_undo_preserve*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*lib_override_apply_post*/ nullptr,
|
2020-03-06 15:38:52 +01:00
|
|
|
};
|
|
|
|
|
|
2010-10-27 02:22:55 +00:00
|
|
|
enum {
|
|
|
|
|
MESHCMP_DVERT_WEIGHTMISMATCH = 1,
|
|
|
|
|
MESHCMP_DVERT_GROUPMISMATCH,
|
|
|
|
|
MESHCMP_DVERT_TOTGROUPMISMATCH,
|
|
|
|
|
MESHCMP_LOOPCOLMISMATCH,
|
|
|
|
|
MESHCMP_LOOPUVMISMATCH,
|
|
|
|
|
MESHCMP_LOOPMISMATCH,
|
|
|
|
|
MESHCMP_POLYVERTMISMATCH,
|
|
|
|
|
MESHCMP_POLYMISMATCH,
|
|
|
|
|
MESHCMP_EDGEUNKNOWN,
|
|
|
|
|
MESHCMP_VERTCOMISMATCH,
|
2019-02-03 14:01:45 +11:00
|
|
|
MESHCMP_CDLAYERS_MISMATCH,
|
2021-08-06 00:03:16 +05:30
|
|
|
MESHCMP_ATTRIBUTE_VALUE_MISMATCH,
|
2010-10-27 02:22:55 +00:00
|
|
|
};
|
|
|
|
|
|
2011-05-09 05:09:07 +00:00
|
|
|
static const char *cmpcode_to_str(int code)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case MESHCMP_DVERT_WEIGHTMISMATCH:
|
|
|
|
|
return "Vertex Weight Mismatch";
|
|
|
|
|
case MESHCMP_DVERT_GROUPMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Group Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_DVERT_TOTGROUPMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Doesn't Belong To Same Number Of Groups";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_LOOPCOLMISMATCH:
|
2022-04-05 11:42:55 -07:00
|
|
|
return "Color Attribute Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_LOOPUVMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "UV Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_LOOPMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Loop Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_POLYVERTMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Loop Vert Mismatch In Poly Test";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_POLYMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Loop Vert Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_EDGEUNKNOWN:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Edge Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_VERTCOMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Coordinate Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_CDLAYERS_MISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "CustomData Layer Count Mismatch";
|
2021-08-06 00:03:16 +05:30
|
|
|
case MESHCMP_ATTRIBUTE_VALUE_MISMATCH:
|
|
|
|
|
return "Attribute Value Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
default:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Mesh Comparison Code Unknown";
|
|
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
static bool is_sublayer_name(char const *sublayer_name, char const *name)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(strlen(sublayer_name) == 2);
|
|
|
|
|
if (name[1] != sublayer_name[0]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (name[2] != sublayer_name[1]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (name[3] != '.') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:49:30 -04:00
|
|
|
static bool is_uv_bool_sublayer(const CustomDataLayer &layer)
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
{
|
2023-04-12 11:49:30 -04:00
|
|
|
char const *name = layer.name;
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
|
|
|
|
|
if (name[0] != '.') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return is_sublayer_name(UV_VERTSEL_NAME, name) || is_sublayer_name(UV_EDGESEL_NAME, name) ||
|
|
|
|
|
is_sublayer_name(UV_PINNED_NAME, name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 14:49:51 -05:00
|
|
|
/** Thresh is threshold for comparing vertices, UVs, vertex colors, weights, etc. */
|
2013-05-08 12:55:36 +00:00
|
|
|
static int customdata_compare(
|
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
|
|
|
CustomData *c1, CustomData *c2, const int total_length, Mesh *m1, const float thresh)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
2023-08-29 17:00:33 +02:00
|
|
|
using namespace blender;
|
2010-10-27 02:22:55 +00:00
|
|
|
CustomDataLayer *l1, *l2;
|
2021-08-10 19:16:36 +05:30
|
|
|
int layer_count1 = 0, layer_count2 = 0, j;
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
const uint64_t cd_mask_non_generic = CD_MASK_MDEFORMVERT;
|
2021-08-10 19:16:36 +05:30
|
|
|
const uint64_t cd_mask_all_attr = CD_MASK_PROP_ALL | cd_mask_non_generic;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
/* The uv selection / pin layers are ignored in the comparisons because
|
|
|
|
|
* the original flags they replace were ignored as well. Because of the
|
|
|
|
|
* lazy creation of these layers it would need careful handling of the
|
|
|
|
|
* test files to compare these layers. For now it has been decided to
|
|
|
|
|
* skip them.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-08-06 00:03:16 +05:30
|
|
|
for (int i = 0; i < c1->totlayer; i++) {
|
2021-11-24 10:37:06 +01:00
|
|
|
l1 = &c1->layers[i];
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
if ((CD_TYPE_AS_MASK(l1->type) & cd_mask_all_attr) && l1->anonymous_id == nullptr &&
|
2023-04-12 11:49:30 -04:00
|
|
|
!is_uv_bool_sublayer(*l1))
|
|
|
|
|
{
|
2021-08-10 19:16:36 +05:30
|
|
|
layer_count1++;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 00:03:16 +05:30
|
|
|
for (int i = 0; i < c2->totlayer; i++) {
|
2021-11-24 10:37:06 +01:00
|
|
|
l2 = &c2->layers[i];
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
if ((CD_TYPE_AS_MASK(l2->type) & cd_mask_all_attr) && l2->anonymous_id == nullptr &&
|
2023-04-12 11:49:30 -04:00
|
|
|
!is_uv_bool_sublayer(*l2))
|
|
|
|
|
{
|
2021-08-10 19:16:36 +05:30
|
|
|
layer_count2++;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-10 19:16:36 +05:30
|
|
|
if (layer_count1 != layer_count2) {
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
/* TODO(@HooglyBoogly): Re-enable after tests are updated for material index refactor and UV as
|
|
|
|
|
* generic attribute refactor. */
|
2022-08-31 09:09:01 -05:00
|
|
|
// return MESHCMP_CDLAYERS_MISMATCH;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-08-06 00:03:16 +05:30
|
|
|
l1 = c1->layers;
|
|
|
|
|
l2 = c2->layers;
|
|
|
|
|
|
2021-08-10 19:16:36 +05:30
|
|
|
for (int i1 = 0; i1 < c1->totlayer; i1++) {
|
2021-08-06 00:03:16 +05:30
|
|
|
l1 = c1->layers + i1;
|
2023-04-12 11:49:30 -04:00
|
|
|
if (l1->anonymous_id != nullptr || is_uv_bool_sublayer(*l1)) {
|
2022-12-12 12:13:33 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
bool found_corresponding_layer = false;
|
2021-08-10 19:16:36 +05:30
|
|
|
for (int i2 = 0; i2 < c2->totlayer; i2++) {
|
2021-08-06 00:03:16 +05:30
|
|
|
l2 = c2->layers + i2;
|
2022-12-12 12:13:33 +01:00
|
|
|
if (l1->type != l2->type || !STREQ(l1->name, l2->name) || l2->anonymous_id != nullptr) {
|
2021-08-06 00:03:16 +05:30
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* At this point `l1` and `l2` have the same name and type, so they should be compared. */
|
|
|
|
|
|
2022-12-12 12:13:33 +01:00
|
|
|
found_corresponding_layer = true;
|
|
|
|
|
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
if (StringRef(l1->name) == ".corner_edge") {
|
|
|
|
|
/* TODO(Hans): This attribute wasn't tested before loops were refactored into separate
|
|
|
|
|
* corner edges and corner verts attributes. Remove after updating tests. */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 00:03:16 +05:30
|
|
|
switch (l1->type) {
|
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
|
|
|
case CD_PROP_INT32_2D: {
|
|
|
|
|
blender::int2 *e1 = (blender::int2 *)l1->data;
|
|
|
|
|
blender::int2 *e2 = (blender::int2 *)l2->data;
|
|
|
|
|
|
|
|
|
|
if (StringRef(l1->name) == ".edge_verts") {
|
|
|
|
|
int etot = m1->totedge;
|
2023-08-29 17:00:33 +02:00
|
|
|
Set<OrderedEdge> ordered_edges;
|
|
|
|
|
ordered_edges.reserve(etot);
|
|
|
|
|
for (const int2 value : Span(e1, etot)) {
|
|
|
|
|
ordered_edges.add(value);
|
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
|
|
|
}
|
2021-08-10 19:16:36 +05:30
|
|
|
|
2023-08-29 17:00:33 +02:00
|
|
|
for (j = 0; j < etot; j++) {
|
|
|
|
|
if (!ordered_edges.contains(e2[j])) {
|
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
|
|
|
return MESHCMP_EDGEUNKNOWN;
|
|
|
|
|
}
|
2021-08-10 19:16:36 +05:30
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
else {
|
|
|
|
|
for (j = 0; j < total_length; j++) {
|
|
|
|
|
if (e1[j] != e2[j]) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
2023-04-14 16:08:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-04-20 09:10:10 -05:00
|
|
|
case CD_PROP_BYTE_COLOR: {
|
2021-11-06 19:16:37 +01:00
|
|
|
MLoopCol *lp1 = (MLoopCol *)l1->data;
|
|
|
|
|
MLoopCol *lp2 = (MLoopCol *)l2->data;
|
2021-08-10 19:16:36 +05:30
|
|
|
int ltot = m1->totloop;
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < ltot; j++, lp1++, lp2++) {
|
2021-08-24 00:49:36 +05:30
|
|
|
if (lp1->r != lp2->r || lp1->g != lp2->g || lp1->b != lp2->b || lp1->a != lp2->a) {
|
2021-08-10 19:16:36 +05:30
|
|
|
return MESHCMP_LOOPCOLMISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CD_MDEFORMVERT: {
|
2021-11-06 19:16:37 +01:00
|
|
|
MDeformVert *dv1 = (MDeformVert *)l1->data;
|
|
|
|
|
MDeformVert *dv2 = (MDeformVert *)l2->data;
|
2021-08-10 19:16:36 +05:30
|
|
|
int dvtot = m1->totvert;
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < dvtot; j++, dv1++, dv2++) {
|
|
|
|
|
int k;
|
|
|
|
|
MDeformWeight *dw1 = dv1->dw, *dw2 = dv2->dw;
|
|
|
|
|
|
|
|
|
|
if (dv1->totweight != dv2->totweight) {
|
|
|
|
|
return MESHCMP_DVERT_TOTGROUPMISMATCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (k = 0; k < dv1->totweight; k++, dw1++, dw2++) {
|
|
|
|
|
if (dw1->def_nr != dw2->def_nr) {
|
|
|
|
|
return MESHCMP_DVERT_GROUPMISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (fabsf(dw1->weight - dw2->weight) > thresh) {
|
|
|
|
|
return MESHCMP_DVERT_WEIGHTMISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-08-06 00:03:16 +05:30
|
|
|
case CD_PROP_FLOAT: {
|
2021-11-06 19:16:37 +01:00
|
|
|
const float *l1_data = (float *)l1->data;
|
|
|
|
|
const float *l2_data = (float *)l2->data;
|
2021-08-06 00:03:16 +05:30
|
|
|
|
|
|
|
|
for (int i = 0; i < total_length; i++) {
|
2021-08-24 00:49:36 +05:30
|
|
|
if (compare_threshold_relative(l1_data[i], l2_data[i], thresh)) {
|
2021-08-06 00:03:16 +05:30
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CD_PROP_FLOAT2: {
|
2021-11-06 19:16:37 +01:00
|
|
|
const float(*l1_data)[2] = (float(*)[2])l1->data;
|
|
|
|
|
const float(*l2_data)[2] = (float(*)[2])l2->data;
|
2021-08-06 00:03:16 +05:30
|
|
|
|
|
|
|
|
for (int i = 0; i < total_length; i++) {
|
2021-08-24 00:49:36 +05:30
|
|
|
if (compare_threshold_relative(l1_data[i][0], l2_data[i][0], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][1], l2_data[i][1], thresh)) {
|
2021-08-06 00:03:16 +05:30
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CD_PROP_FLOAT3: {
|
2021-11-06 19:16:37 +01:00
|
|
|
const float(*l1_data)[3] = (float(*)[3])l1->data;
|
|
|
|
|
const float(*l2_data)[3] = (float(*)[3])l2->data;
|
2021-08-06 00:03:16 +05:30
|
|
|
|
|
|
|
|
for (int i = 0; i < total_length; i++) {
|
2021-08-24 00:49:36 +05:30
|
|
|
if (compare_threshold_relative(l1_data[i][0], l2_data[i][0], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][1], l2_data[i][1], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][2], l2_data[i][2], thresh)) {
|
2021-08-06 00:03:16 +05:30
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2023-06-12 15:49:50 +02:00
|
|
|
}
|
|
|
|
|
case CD_PROP_QUATERNION: {
|
|
|
|
|
const float(*l1_data)[4] = (float(*)[4])l1->data;
|
|
|
|
|
const float(*l2_data)[4] = (float(*)[4])l2->data;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < total_length; i++) {
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][0], l2_data[i][0], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][1], l2_data[i][1], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][2], l2_data[i][2], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
if (compare_threshold_relative(l1_data[i][3], l2_data[i][3], thresh)) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-08-06 00:03:16 +05:30
|
|
|
}
|
2021-08-10 19:16:36 +05:30
|
|
|
case CD_PROP_INT32: {
|
2021-11-06 19:16:37 +01:00
|
|
|
const int *l1_data = (int *)l1->data;
|
|
|
|
|
const int *l2_data = (int *)l2->data;
|
2021-08-10 19:16:36 +05:30
|
|
|
|
2021-08-06 00:03:16 +05:30
|
|
|
for (int i = 0; i < total_length; i++) {
|
2021-08-10 19:16:36 +05:30
|
|
|
if (l1_data[i] != l2_data[i]) {
|
2021-08-06 00:03:16 +05:30
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-02-04 10:29:11 -06:00
|
|
|
case CD_PROP_INT8: {
|
|
|
|
|
const int8_t *l1_data = (int8_t *)l1->data;
|
|
|
|
|
const int8_t *l2_data = (int8_t *)l2->data;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < total_length; i++) {
|
|
|
|
|
if (l1_data[i] != l2_data[i]) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-08-10 19:16:36 +05:30
|
|
|
case CD_PROP_BOOL: {
|
2021-11-06 19:16:37 +01:00
|
|
|
const bool *l1_data = (bool *)l1->data;
|
|
|
|
|
const bool *l2_data = (bool *)l2->data;
|
2021-08-10 19:16:36 +05:30
|
|
|
for (int i = 0; i < total_length; i++) {
|
|
|
|
|
if (l1_data[i] != l2_data[i]) {
|
|
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2021-08-10 19:16:36 +05:30
|
|
|
break;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
2021-08-16 12:17:30 +05:30
|
|
|
case CD_PROP_COLOR: {
|
2021-11-06 19:16:37 +01:00
|
|
|
const MPropCol *l1_data = (MPropCol *)l1->data;
|
|
|
|
|
const MPropCol *l2_data = (MPropCol *)l2->data;
|
2021-08-16 12:17:30 +05:30
|
|
|
|
|
|
|
|
for (int i = 0; i < total_length; i++) {
|
2021-08-16 18:06:10 +10:00
|
|
|
for (j = 0; j < 4; j++) {
|
2021-08-24 00:49:36 +05:30
|
|
|
if (compare_threshold_relative(l1_data[i].color[j], l2_data[i].color[j], thresh)) {
|
2021-08-16 12:17:30 +05:30
|
|
|
return MESHCMP_ATTRIBUTE_VALUE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-08-10 19:16:36 +05:30
|
|
|
default: {
|
|
|
|
|
break;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-12 12:13:33 +01:00
|
|
|
if (!found_corresponding_layer) {
|
2022-12-15 17:30:40 +01:00
|
|
|
if ((uint64_t(1) << l1->type) & CD_MASK_PROP_ALL) {
|
2022-12-12 12:13:33 +01:00
|
|
|
return MESHCMP_CDLAYERS_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-11-11 23:36:56 +00:00
|
|
|
return 0;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
const char *BKE_mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
|
|
|
|
int c;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (!me1 || !me2) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return "Requires two input meshes";
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (me1->totvert != me2->totvert) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return "Number of verts don't match";
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (me1->totedge != me2->totedge) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return "Number of edges don't match";
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (me1->faces_num != me2->faces_num) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return "Number of faces don't match";
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (me1->totloop != me2->totloop) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return "Number of loops don't match";
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
if (!std::equal(
|
2023-07-24 22:06:55 +02:00
|
|
|
me1->face_offsets().begin(), me1->face_offsets().end(), me2->face_offsets().begin()))
|
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
|
|
|
{
|
|
|
|
|
return "Face sizes don't match";
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if ((c = customdata_compare(&me1->vert_data, &me2->vert_data, me1->totvert, me1, thresh))) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return cmpcode_to_str(c);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if ((c = customdata_compare(&me1->edge_data, &me2->edge_data, me1->totedge, me1, thresh))) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return cmpcode_to_str(c);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if ((c = customdata_compare(&me1->loop_data, &me2->loop_data, me1->totloop, me1, thresh))) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return cmpcode_to_str(c);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if ((c = customdata_compare(&me1->face_data, &me2->face_data, me1->faces_num, me1, thresh))) {
|
2010-10-27 02:22:55 +00:00
|
|
|
return cmpcode_to_str(c);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
return nullptr;
|
2010-10-27 02:22:55 +00: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
|
|
|
bool BKE_mesh_attribute_required(const char *name)
|
|
|
|
|
{
|
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
|
|
|
return ELEM(StringRef(name), "position", ".corner_vert", ".corner_edge", ".edge_verts");
|
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
|
|
|
}
|
|
|
|
|
|
2014-03-16 21:55:30 +11:00
|
|
|
void BKE_mesh_ensure_skin_customdata(Mesh *me)
|
|
|
|
|
{
|
2021-11-06 19:16:37 +01:00
|
|
|
BMesh *bm = me->edit_mesh ? me->edit_mesh->bm : nullptr;
|
2014-03-16 21:55:30 +11:00
|
|
|
MVertSkin *vs;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-16 21:55:30 +11:00
|
|
|
if (bm) {
|
|
|
|
|
if (!CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN)) {
|
|
|
|
|
BMVert *v;
|
|
|
|
|
BMIter iter;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-16 21:55:30 +11:00
|
|
|
BM_data_layer_add(bm, &bm->vdata, CD_MVERT_SKIN);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-16 21:55:30 +11:00
|
|
|
/* Mark an arbitrary vertex as root */
|
|
|
|
|
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
|
2021-11-06 19:16:37 +01:00
|
|
|
vs = (MVertSkin *)CustomData_bmesh_get(&bm->vdata, v->head.data, CD_MVERT_SKIN);
|
2014-03-16 21:55:30 +11:00
|
|
|
vs->flag |= MVERT_SKIN_ROOT;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-03-16 21:55:30 +11:00
|
|
|
else {
|
2023-07-25 21:15:52 +02:00
|
|
|
if (!CustomData_has_layer(&me->vert_data, CD_MVERT_SKIN)) {
|
2021-11-06 19:16:37 +01:00
|
|
|
vs = (MVertSkin *)CustomData_add_layer(
|
2023-07-25 21:15:52 +02:00
|
|
|
&me->vert_data, CD_MVERT_SKIN, CD_SET_DEFAULT, me->totvert);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-16 21:55:30 +11:00
|
|
|
/* Mark an arbitrary vertex as root */
|
|
|
|
|
if (vs) {
|
|
|
|
|
vs->flag |= MVERT_SKIN_ROOT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
bool BKE_mesh_has_custom_loop_normals(Mesh *me)
|
|
|
|
|
{
|
2019-02-17 18:05:18 +11:00
|
|
|
if (me->edit_mesh) {
|
|
|
|
|
return CustomData_has_layer(&me->edit_mesh->bm->ldata, CD_CUSTOMLOOPNORMAL);
|
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
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
return CustomData_has_layer(&me->loop_data, CD_CUSTOMLOOPNORMAL);
|
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
|
|
|
}
|
|
|
|
|
|
2021-08-20 16:21:29 +10:00
|
|
|
void BKE_mesh_free_data_for_undo(Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2020-03-06 15:38:52 +01:00
|
|
|
mesh_free_data(&me->id);
|
2019-07-30 18:38:31 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-02 15:29:36 +10:00
|
|
|
/**
|
|
|
|
|
* \note on data that this function intentionally doesn't free:
|
|
|
|
|
*
|
|
|
|
|
* - Materials and shape keys are not freed here (#Mesh.mat & #Mesh.key).
|
|
|
|
|
* As freeing shape keys requires tagging the depsgraph for updated relations,
|
|
|
|
|
* which is expensive.
|
|
|
|
|
* Material slots should be kept in sync with the object.
|
|
|
|
|
*
|
|
|
|
|
* - Edit-Mesh (#Mesh.edit_mesh)
|
2023-04-12 11:49:30 -04:00
|
|
|
* Since edit-mesh is tied to the object's mode, which crashes when called in edit-mode.
|
|
|
|
|
* See: #90972.
|
2021-09-02 15:29:36 +10:00
|
|
|
*/
|
2023-04-12 11:49:30 -04:00
|
|
|
static void mesh_clear_geometry(Mesh &mesh)
|
2019-07-30 18:38:31 +02:00
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free(&mesh.vert_data, mesh.totvert);
|
|
|
|
|
CustomData_free(&mesh.edge_data, mesh.totedge);
|
2023-07-24 22:06:55 +02:00
|
|
|
CustomData_free(&mesh.fdata_legacy, mesh.totface_legacy);
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free(&mesh.loop_data, mesh.totloop);
|
|
|
|
|
CustomData_free(&mesh.face_data, mesh.faces_num);
|
2023-07-24 22:06:55 +02:00
|
|
|
if (mesh.face_offset_indices) {
|
|
|
|
|
blender::implicit_sharing::free_shared_data(&mesh.face_offset_indices,
|
|
|
|
|
&mesh.runtime->face_offsets_sharing_info);
|
2023-04-14 17:58:13 +02:00
|
|
|
}
|
2023-04-12 11:49:30 -04:00
|
|
|
MEM_SAFE_FREE(mesh.mselect);
|
|
|
|
|
|
|
|
|
|
mesh.totvert = 0;
|
|
|
|
|
mesh.totedge = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh.totface_legacy = 0;
|
2023-04-12 11:49:30 -04:00
|
|
|
mesh.totloop = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh.faces_num = 0;
|
2023-04-12 11:49:30 -04:00
|
|
|
mesh.act_face = -1;
|
|
|
|
|
mesh.totselect = 0;
|
2023-04-12 11:27:52 -04:00
|
|
|
}
|
2022-09-09 08:24:31 -05:00
|
|
|
|
2023-04-12 11:27:52 -04:00
|
|
|
static void clear_attribute_names(Mesh &mesh)
|
|
|
|
|
{
|
|
|
|
|
BLI_freelistN(&mesh.vertex_group_names);
|
|
|
|
|
MEM_SAFE_FREE(mesh.active_color_attribute);
|
|
|
|
|
MEM_SAFE_FREE(mesh.default_color_attribute);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 18:40:06 +01:00
|
|
|
void BKE_mesh_clear_geometry(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
2023-04-12 11:49:30 -04:00
|
|
|
mesh_clear_geometry(*mesh);
|
2020-03-09 18:40:06 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:27:52 -04:00
|
|
|
void BKE_mesh_clear_geometry_and_metadata(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
2023-04-12 11:49:30 -04:00
|
|
|
mesh_clear_geometry(*mesh);
|
2023-04-12 11:27:52 -04:00
|
|
|
clear_attribute_names(*mesh);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 16:22:41 +00:00
|
|
|
static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata)
|
|
|
|
|
{
|
2012-10-31 09:50:24 +00:00
|
|
|
if (free_customdata) {
|
2023-07-24 22:06:55 +02:00
|
|
|
CustomData_free(&mesh->fdata_legacy, mesh->totface_legacy);
|
2012-10-31 09:50:24 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-24 22:06:55 +02:00
|
|
|
CustomData_reset(&mesh->fdata_legacy);
|
2012-10-31 09:50:24 +00:00
|
|
|
}
|
2012-03-06 16:22:41 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->totface_legacy = 0;
|
2012-03-06 16:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
First step to handle missing libs/datablocks when reading a file.
Idea is, instead of ignoring completely missing linked datablocks, to
create void placeholders for them.
That way, you can work on your file, save it, and find again your missing data once
lib becomes available again. Or you can edit missing lib's path (in Outliner),
save and reload the file, and you are done.
Also, Outliner now shows broken libraries (and placeholders) with a 'broken lib' icon.
Future plans are also to be able to relocate missing libs and reload them at runtime.
Code notes:
- Placeholder ID is just a regular datablock of same type as expected linked one,
with 'default' data, and a LIB_MISSING bitflag set.
- To allow creation of such datablocks, creation of datablocks in BKE was split in two step:
+ Allocation of memory itself.
+ Setting of all internal data to default values.
See also the design task (T43351).
Reviewed by @campbellbarton, thanks a bunch!
Differential Revision: https://developer.blender.org/D1394
2015-10-20 14:44:57 +02:00
|
|
|
Mesh *BKE_mesh_add(Main *bmain, const char *name)
|
|
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
return static_cast<Mesh *>(BKE_id_new(bmain, ID_ME, name));
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
void BKE_mesh_face_offsets_ensure_alloc(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
|
|
|
BLI_assert(mesh->face_offset_indices == nullptr);
|
|
|
|
|
BLI_assert(mesh->runtime->face_offsets_sharing_info == nullptr);
|
|
|
|
|
if (mesh->faces_num == 0) {
|
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
|
|
|
return;
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->face_offset_indices = static_cast<int *>(
|
|
|
|
|
MEM_malloc_arrayN(mesh->faces_num + 1, sizeof(int), __func__));
|
|
|
|
|
mesh->runtime->face_offsets_sharing_info = blender::implicit_sharing::info_for_mem_free(
|
|
|
|
|
mesh->face_offset_indices);
|
2023-04-12 14:56:28 -04:00
|
|
|
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
/* Fill offsets with obviously bad values to simplify finding missing initialization. */
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->face_offsets_for_write().fill(-1);
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
#endif
|
2023-04-14 17:58:13 +02:00
|
|
|
/* Set common values for convenience. */
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->face_offset_indices[0] = 0;
|
|
|
|
|
mesh->face_offset_indices[mesh->faces_num] = mesh->totloop;
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
MutableSpan<int> Mesh::face_offsets_for_write()
|
2023-04-14 17:58:13 +02:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
if (this->faces_num == 0) {
|
2023-06-14 09:47:28 -04:00
|
|
|
return {};
|
|
|
|
|
}
|
2023-04-14 17:58:13 +02:00
|
|
|
blender::implicit_sharing::make_trivial_data_mutable(
|
2023-07-24 22:06:55 +02:00
|
|
|
&this->face_offset_indices, &this->runtime->face_offsets_sharing_info, this->faces_num + 1);
|
|
|
|
|
return {this->face_offset_indices, this->faces_num + 1};
|
2023-04-14 17:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:49:30 -04:00
|
|
|
static void mesh_ensure_cdlayers_primary(Mesh &mesh)
|
2018-06-06 12:20:21 +02:00
|
|
|
{
|
2023-05-10 17:40:37 +02:00
|
|
|
blender::bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
|
|
|
|
|
blender::bke::AttributeInitConstruct attribute_init;
|
|
|
|
|
|
|
|
|
|
/* Try to create attributes if they do not exist. */
|
|
|
|
|
attributes.add("position", ATTR_DOMAIN_POINT, CD_PROP_FLOAT3, attribute_init);
|
|
|
|
|
attributes.add(".edge_verts", ATTR_DOMAIN_EDGE, CD_PROP_INT32_2D, attribute_init);
|
|
|
|
|
attributes.add(".corner_vert", ATTR_DOMAIN_CORNER, CD_PROP_INT32, attribute_init);
|
|
|
|
|
attributes.add(".corner_edge", ATTR_DOMAIN_CORNER, CD_PROP_INT32, attribute_init);
|
2018-06-06 12:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-19 15:28:53 -04:00
|
|
|
Mesh *BKE_mesh_new_nomain(const int verts_num,
|
|
|
|
|
const int edges_num,
|
2023-07-24 22:06:55 +02:00
|
|
|
const int faces_num,
|
2023-04-19 15:28:53 -04:00
|
|
|
const int loops_num)
|
2018-05-04 11:42:55 +02:00
|
|
|
{
|
2023-04-19 22:10:53 -04:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(BKE_libblock_alloc(
|
|
|
|
|
nullptr, ID_ME, BKE_idtype_idcode_to_name(ID_ME), LIB_ID_CREATE_LOCALIZE));
|
2018-05-04 11:42:55 +02:00
|
|
|
BKE_libblock_init_empty(&mesh->id);
|
|
|
|
|
|
2023-04-19 15:28:53 -04:00
|
|
|
mesh->totvert = verts_num;
|
|
|
|
|
mesh->totedge = edges_num;
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->faces_num = faces_num;
|
2023-04-19 15:28:53 -04:00
|
|
|
mesh->totloop = loops_num;
|
2018-05-11 10:37:16 +02:00
|
|
|
|
2023-04-12 11:49:30 -04:00
|
|
|
mesh_ensure_cdlayers_primary(*mesh);
|
2023-07-24 22:06:55 +02:00
|
|
|
BKE_mesh_face_offsets_ensure_alloc(mesh);
|
2018-06-06 12:20:21 +02:00
|
|
|
|
2018-05-04 11:42:55 +02:00
|
|
|
return mesh;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 14:56:39 -06:00
|
|
|
static void copy_attribute_names(const Mesh &mesh_src, Mesh &mesh_dst)
|
|
|
|
|
{
|
|
|
|
|
if (mesh_src.active_color_attribute) {
|
|
|
|
|
MEM_SAFE_FREE(mesh_dst.active_color_attribute);
|
|
|
|
|
mesh_dst.active_color_attribute = BLI_strdup(mesh_src.active_color_attribute);
|
|
|
|
|
}
|
|
|
|
|
if (mesh_src.default_color_attribute) {
|
|
|
|
|
MEM_SAFE_FREE(mesh_dst.default_color_attribute);
|
|
|
|
|
mesh_dst.default_color_attribute = BLI_strdup(mesh_src.default_color_attribute);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 14:56:39 +10:00
|
|
|
void BKE_mesh_copy_parameters(Mesh *me_dst, const Mesh *me_src)
|
2019-09-16 10:45:49 +02:00
|
|
|
{
|
|
|
|
|
/* Copy general settings. */
|
|
|
|
|
me_dst->editflag = me_src->editflag;
|
|
|
|
|
me_dst->flag = me_src->flag;
|
|
|
|
|
me_dst->remesh_voxel_size = me_src->remesh_voxel_size;
|
2019-09-27 17:41:05 +02:00
|
|
|
me_dst->remesh_voxel_adaptivity = me_src->remesh_voxel_adaptivity;
|
2019-09-16 10:45:49 +02:00
|
|
|
me_dst->remesh_mode = me_src->remesh_mode;
|
2020-11-02 20:09:41 +01:00
|
|
|
me_dst->symmetry = me_src->symmetry;
|
2019-09-16 10:45:49 +02:00
|
|
|
|
2020-03-05 14:53:23 +01:00
|
|
|
me_dst->face_sets_color_seed = me_src->face_sets_color_seed;
|
2020-09-19 17:18:57 +02:00
|
|
|
me_dst->face_sets_color_default = me_src->face_sets_color_default;
|
2020-03-05 14:53:23 +01:00
|
|
|
|
2019-09-16 10:45:49 +02:00
|
|
|
/* Copy texture space. */
|
2023-01-18 17:17:32 +11:00
|
|
|
me_dst->texspace_flag = me_src->texspace_flag;
|
|
|
|
|
copy_v3_v3(me_dst->texspace_location, me_src->texspace_location);
|
|
|
|
|
copy_v3_v3(me_dst->texspace_size, me_src->texspace_size);
|
2021-07-13 12:10:34 -04:00
|
|
|
|
|
|
|
|
me_dst->vertex_group_active_index = me_src->vertex_group_active_index;
|
2022-09-09 08:24:31 -05:00
|
|
|
me_dst->attributes_active_index = me_src->attributes_active_index;
|
2021-06-17 14:56:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
|
|
|
|
|
{
|
|
|
|
|
/* User counts aren't handled, don't copy into a mesh from #G_MAIN. */
|
|
|
|
|
BLI_assert(me_dst->id.tag & (LIB_TAG_NO_MAIN | LIB_TAG_COPIED_ON_WRITE));
|
|
|
|
|
|
|
|
|
|
BKE_mesh_copy_parameters(me_dst, me_src);
|
2022-12-19 14:56:39 -06:00
|
|
|
copy_attribute_names(*me_src, *me_dst);
|
2019-09-16 10:45:49 +02:00
|
|
|
|
2021-07-15 12:25:36 -04:00
|
|
|
/* Copy vertex group names. */
|
|
|
|
|
BLI_assert(BLI_listbase_is_empty(&me_dst->vertex_group_names));
|
|
|
|
|
BKE_defgroup_copy_list(&me_dst->vertex_group_names, &me_src->vertex_group_names);
|
|
|
|
|
|
2019-09-16 10:45:49 +02:00
|
|
|
/* Copy materials. */
|
2021-11-06 19:16:37 +01:00
|
|
|
if (me_dst->mat != nullptr) {
|
2019-09-16 10:45:49 +02:00
|
|
|
MEM_freeN(me_dst->mat);
|
|
|
|
|
}
|
2021-11-06 19:16:37 +01:00
|
|
|
me_dst->mat = (Material **)MEM_dupallocN(me_src->mat);
|
2019-09-16 10:45:49 +02:00
|
|
|
me_dst->totcol = me_src->totcol;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 18:17:27 +02:00
|
|
|
Mesh *BKE_mesh_new_nomain_from_template_ex(const Mesh *me_src,
|
2023-04-19 15:28:53 -04:00
|
|
|
const int verts_num,
|
|
|
|
|
const int edges_num,
|
|
|
|
|
const int tessface_num,
|
2023-07-24 22:06:55 +02:00
|
|
|
const int faces_num,
|
2023-04-19 15:28:53 -04:00
|
|
|
const int loops_num,
|
|
|
|
|
const CustomData_MeshMasks mask)
|
2018-04-19 11:03:58 +02:00
|
|
|
{
|
2023-02-27 15:29:35 -05:00
|
|
|
/* Only do tessface if we are creating tessfaces or copying from mesh with only tessfaces. */
|
2023-07-24 22:06:55 +02:00
|
|
|
const bool do_tessface = (tessface_num ||
|
|
|
|
|
((me_src->totface_legacy != 0) && (me_src->faces_num == 0)));
|
2023-02-27 15:29:35 -05:00
|
|
|
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *me_dst = static_cast<Mesh *>(BKE_id_new_nomain(ID_ME, nullptr));
|
2018-04-19 11:03:58 +02:00
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
me_dst->mselect = (MSelect *)MEM_dupallocN(me_src->mselect);
|
2018-04-19 11:03:58 +02:00
|
|
|
|
2023-04-19 15:28:53 -04:00
|
|
|
me_dst->totvert = verts_num;
|
|
|
|
|
me_dst->totedge = edges_num;
|
2023-07-24 22:06:55 +02:00
|
|
|
me_dst->faces_num = faces_num;
|
2023-04-19 15:28:53 -04:00
|
|
|
me_dst->totloop = loops_num;
|
2023-07-24 22:06:55 +02:00
|
|
|
me_dst->totface_legacy = tessface_num;
|
2018-04-19 11:03:58 +02:00
|
|
|
|
2021-06-17 14:56:39 +10:00
|
|
|
BKE_mesh_copy_parameters_for_eval(me_dst, me_src);
|
2018-12-08 06:39:46 -06:00
|
|
|
|
2023-07-25 15:23:56 -04:00
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&me_src->vert_data, &me_dst->vert_data, mask.vmask, CD_SET_DEFAULT, verts_num);
|
|
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&me_src->edge_data, &me_dst->edge_data, mask.emask, CD_SET_DEFAULT, edges_num);
|
|
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&me_src->face_data, &me_dst->face_data, mask.pmask, CD_SET_DEFAULT, faces_num);
|
|
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&me_src->loop_data, &me_dst->loop_data, mask.lmask, CD_SET_DEFAULT, loops_num);
|
2023-02-27 15:29:35 -05:00
|
|
|
if (do_tessface) {
|
2023-04-13 14:57:57 +02:00
|
|
|
CustomData_copy_layout(
|
2023-07-24 22:06:55 +02:00
|
|
|
&me_src->fdata_legacy, &me_dst->fdata_legacy, mask.fmask, CD_SET_DEFAULT, tessface_num);
|
2023-02-27 15:29:35 -05:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
mesh_tessface_clear_intern(me_dst, false);
|
|
|
|
|
}
|
2018-04-19 11:03:58 +02:00
|
|
|
|
2018-06-06 12:21:36 +02:00
|
|
|
/* The destination mesh should at least have valid primary CD layers,
|
|
|
|
|
* even in cases where the source mesh does not. */
|
2023-04-12 11:49:30 -04:00
|
|
|
mesh_ensure_cdlayers_primary(*me_dst);
|
2023-07-24 22:06:55 +02:00
|
|
|
BKE_mesh_face_offsets_ensure_alloc(me_dst);
|
|
|
|
|
if (do_tessface && !CustomData_get_layer(&me_dst->fdata_legacy, CD_MFACE)) {
|
|
|
|
|
CustomData_add_layer(&me_dst->fdata_legacy, CD_MFACE, CD_SET_DEFAULT, me_dst->totface_legacy);
|
2023-02-27 15:29:35 -05:00
|
|
|
}
|
2018-04-19 11:03:58 +02:00
|
|
|
|
|
|
|
|
return me_dst;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 15:28:53 -04:00
|
|
|
Mesh *BKE_mesh_new_nomain_from_template(const Mesh *me_src,
|
|
|
|
|
const int verts_num,
|
|
|
|
|
const int edges_num,
|
2023-07-24 22:06:55 +02:00
|
|
|
const int faces_num,
|
2023-04-19 15:28:53 -04:00
|
|
|
const int loops_num)
|
2018-04-19 11:03:58 +02:00
|
|
|
{
|
2019-08-27 18:17:27 +02:00
|
|
|
return BKE_mesh_new_nomain_from_template_ex(
|
2023-07-24 22:06:55 +02:00
|
|
|
me_src, verts_num, edges_num, 0, faces_num, loops_num, CD_MASK_EVERYTHING);
|
2018-04-19 11:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_mesh_eval_delete(Mesh *mesh_eval)
|
2019-07-28 15:12:38 +02:00
|
|
|
{
|
|
|
|
|
/* Evaluated mesh may point to edit mesh, but never owns it. */
|
2021-11-06 19:16:37 +01:00
|
|
|
mesh_eval->edit_mesh = nullptr;
|
2021-08-20 16:21:29 +10:00
|
|
|
mesh_free_data(&mesh_eval->id);
|
2019-07-28 15:12:38 +02:00
|
|
|
BKE_libblock_free_data(&mesh_eval->id, false);
|
|
|
|
|
MEM_freeN(mesh_eval);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 15:49:13 -04:00
|
|
|
Mesh *BKE_mesh_copy_for_eval(const Mesh *source)
|
2018-08-16 14:20:36 +02:00
|
|
|
{
|
2023-04-19 15:49:13 -04:00
|
|
|
return reinterpret_cast<Mesh *>(
|
|
|
|
|
BKE_id_copy_ex(nullptr, &source->id, nullptr, LIB_ID_COPY_LOCALIZE));
|
2018-08-16 14:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-02 11:39:23 +02:00
|
|
|
BMesh *BKE_mesh_to_bmesh_ex(const Mesh *me,
|
2023-06-03 08:36:28 +10:00
|
|
|
const BMeshCreateParams *create_params,
|
|
|
|
|
const BMeshFromMeshParams *convert_params)
|
2009-05-26 04:17:47 +00:00
|
|
|
{
|
2013-09-24 03:31:00 +00:00
|
|
|
const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(me);
|
2009-05-26 04:17:47 +00:00
|
|
|
|
2020-12-27 21:46:57 -06:00
|
|
|
BMesh *bm = BM_mesh_create(&allocsize, create_params);
|
2018-05-02 11:39:23 +02:00
|
|
|
BM_mesh_bm_from_me(bm, me, convert_params);
|
2009-05-26 04:17:47 +00:00
|
|
|
|
|
|
|
|
return bm;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 11:39:23 +02:00
|
|
|
BMesh *BKE_mesh_to_bmesh(Mesh *me,
|
|
|
|
|
Object *ob,
|
|
|
|
|
const bool add_key_index,
|
2023-06-03 08:36:28 +10:00
|
|
|
const BMeshCreateParams *params)
|
2018-05-02 11:39:23 +02:00
|
|
|
{
|
2021-11-08 16:54:20 +11:00
|
|
|
BMeshFromMeshParams bmesh_from_mesh_params{};
|
|
|
|
|
bmesh_from_mesh_params.calc_face_normal = false;
|
2022-03-22 09:33:50 -05:00
|
|
|
bmesh_from_mesh_params.calc_vert_normal = false;
|
2021-11-08 16:54:20 +11:00
|
|
|
bmesh_from_mesh_params.add_key_index = add_key_index;
|
|
|
|
|
bmesh_from_mesh_params.use_shapekey = true;
|
|
|
|
|
bmesh_from_mesh_params.active_shapekey = ob->shapenr;
|
|
|
|
|
return BKE_mesh_to_bmesh_ex(me, params, &bmesh_from_mesh_params);
|
2018-05-02 11:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-16 10:45:49 +02:00
|
|
|
Mesh *BKE_mesh_from_bmesh_nomain(BMesh *bm,
|
2023-06-03 08:36:28 +10:00
|
|
|
const BMeshToMeshParams *params,
|
2019-09-16 10:45:49 +02:00
|
|
|
const Mesh *me_settings)
|
2018-05-02 11:39:23 +02:00
|
|
|
{
|
2018-06-13 16:42:56 +02:00
|
|
|
BLI_assert(params->calc_object_remap == false);
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(BKE_id_new_nomain(ID_ME, nullptr));
|
2021-11-06 19:16:37 +01:00
|
|
|
BM_mesh_bm_to_me(nullptr, bm, mesh, params);
|
2021-06-17 14:56:39 +10:00
|
|
|
BKE_mesh_copy_parameters_for_eval(mesh, me_settings);
|
2018-05-02 11:39:23 +02:00
|
|
|
return mesh;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 10:45:49 +02:00
|
|
|
Mesh *BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm,
|
|
|
|
|
const CustomData_MeshMasks *cd_mask_extra,
|
|
|
|
|
const Mesh *me_settings)
|
2018-10-10 12:51:14 +11:00
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(BKE_id_new_nomain(ID_ME, nullptr));
|
2018-10-10 12:51:14 +11:00
|
|
|
BM_mesh_bm_to_me_for_eval(bm, mesh, cd_mask_extra);
|
2021-06-17 14:56:39 +10:00
|
|
|
BKE_mesh_copy_parameters_for_eval(mesh, me_settings);
|
2018-10-10 12:51:14 +11:00
|
|
|
return mesh;
|
|
|
|
|
}
|
|
|
|
|
|
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 10:51:00 -06:00
|
|
|
static void ensure_orig_index_layer(CustomData &data, const int size)
|
|
|
|
|
{
|
|
|
|
|
if (CustomData_has_layer(&data, CD_ORIGINDEX)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-14 15:30:26 +01:00
|
|
|
int *indices = (int *)CustomData_add_layer(&data, CD_ORIGINDEX, CD_SET_DEFAULT, size);
|
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 10:51:00 -06:00
|
|
|
range_vn_i(indices, size, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_ensure_default_orig_index_customdata(Mesh *mesh)
|
|
|
|
|
{
|
2022-10-12 20:55:26 -05:00
|
|
|
BLI_assert(mesh->runtime->wrapper_type == ME_WRAPPER_TYPE_MDATA);
|
2022-07-11 17:25:13 +02:00
|
|
|
BKE_mesh_ensure_default_orig_index_customdata_no_check(mesh);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_ensure_default_orig_index_customdata_no_check(Mesh *mesh)
|
|
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
ensure_orig_index_layer(mesh->vert_data, mesh->totvert);
|
|
|
|
|
ensure_orig_index_layer(mesh->edge_data, mesh->totedge);
|
|
|
|
|
ensure_orig_index_layer(mesh->face_data, mesh->faces_num);
|
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 10:51:00 -06:00
|
|
|
}
|
|
|
|
|
|
Refactor: Improve access to object data bounds
Currently object bounds (`object.runtime.bb`) are lazily initialized
when accessed. This access happens from arbitrary threads, and
is unprotected by a mutex. This can cause access to stale data at
best, and crashes at worst. Eager calculation is meant to keep this
working, but it's fragile.
Since e8f4010611e7, geometry bounds are cached in the geometry
itself, which makes this object-level cache redundant. So, it's clearer
to build the `BoundBox` from those cached bounds and return it by
value, without interacting with the object's cached bounding box.
The code change is is mostly a move from `const BoundBox *` to
`std::optional<BoundBox>`. This is only one step of a larger change
described in #96968. Followup steps would include switching to
a simpler and smaller `Bounds` type, removing redundant object-
level access, and eventually removing `object.runtime.bb`.
Access of bounds from the object for mesh, curves, and point cloud
objects should now be thread-safe. Other object types still lazily
initialize the object `BoundBox` cache since they don't have
a data-level cache.
Pull Request: https://projects.blender.org/blender/blender/pulls/113465
2023-10-19 14:18:40 +02:00
|
|
|
BoundBox BKE_mesh_boundbox_get(Object *ob)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
Refactor: Improve access to object data bounds
Currently object bounds (`object.runtime.bb`) are lazily initialized
when accessed. This access happens from arbitrary threads, and
is unprotected by a mutex. This can cause access to stale data at
best, and crashes at worst. Eager calculation is meant to keep this
working, but it's fragile.
Since e8f4010611e7, geometry bounds are cached in the geometry
itself, which makes this object-level cache redundant. So, it's clearer
to build the `BoundBox` from those cached bounds and return it by
value, without interacting with the object's cached bounding box.
The code change is is mostly a move from `const BoundBox *` to
`std::optional<BoundBox>`. This is only one step of a larger change
described in #96968. Followup steps would include switching to
a simpler and smaller `Bounds` type, removing redundant object-
level access, and eventually removing `object.runtime.bb`.
Access of bounds from the object for mesh, curves, and point cloud
objects should now be thread-safe. Other object types still lazily
initialize the object `BoundBox` cache since they don't have
a data-level cache.
Pull Request: https://projects.blender.org/blender/blender/pulls/113465
2023-10-19 14:18:40 +02:00
|
|
|
Mesh *me = static_cast<Mesh *>(ob->data);
|
|
|
|
|
float min[3], max[3];
|
2018-11-27 14:17:54 +11:00
|
|
|
|
Refactor: Improve access to object data bounds
Currently object bounds (`object.runtime.bb`) are lazily initialized
when accessed. This access happens from arbitrary threads, and
is unprotected by a mutex. This can cause access to stale data at
best, and crashes at worst. Eager calculation is meant to keep this
working, but it's fragile.
Since e8f4010611e7, geometry bounds are cached in the geometry
itself, which makes this object-level cache redundant. So, it's clearer
to build the `BoundBox` from those cached bounds and return it by
value, without interacting with the object's cached bounding box.
The code change is is mostly a move from `const BoundBox *` to
`std::optional<BoundBox>`. This is only one step of a larger change
described in #96968. Followup steps would include switching to
a simpler and smaller `Bounds` type, removing redundant object-
level access, and eventually removing `object.runtime.bb`.
Access of bounds from the object for mesh, curves, and point cloud
objects should now be thread-safe. Other object types still lazily
initialize the object `BoundBox` cache since they don't have
a data-level cache.
Pull Request: https://projects.blender.org/blender/blender/pulls/113465
2023-10-19 14:18:40 +02:00
|
|
|
INIT_MINMAX(min, max);
|
|
|
|
|
if (!BKE_mesh_wrapper_minmax(me, min, max)) {
|
|
|
|
|
min[0] = min[1] = min[2] = -1.0f;
|
|
|
|
|
max[0] = max[1] = max[2] = 1.0f;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
Refactor: Improve access to object data bounds
Currently object bounds (`object.runtime.bb`) are lazily initialized
when accessed. This access happens from arbitrary threads, and
is unprotected by a mutex. This can cause access to stale data at
best, and crashes at worst. Eager calculation is meant to keep this
working, but it's fragile.
Since e8f4010611e7, geometry bounds are cached in the geometry
itself, which makes this object-level cache redundant. So, it's clearer
to build the `BoundBox` from those cached bounds and return it by
value, without interacting with the object's cached bounding box.
The code change is is mostly a move from `const BoundBox *` to
`std::optional<BoundBox>`. This is only one step of a larger change
described in #96968. Followup steps would include switching to
a simpler and smaller `Bounds` type, removing redundant object-
level access, and eventually removing `object.runtime.bb`.
Access of bounds from the object for mesh, curves, and point cloud
objects should now be thread-safe. Other object types still lazily
initialize the object `BoundBox` cache since they don't have
a data-level cache.
Pull Request: https://projects.blender.org/blender/blender/pulls/113465
2023-10-19 14:18:40 +02:00
|
|
|
BoundBox bb;
|
|
|
|
|
BKE_boundbox_init_from_minmax(&bb, min, max);
|
|
|
|
|
return bb;
|
2018-11-27 14:17:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_texspace_calc(Mesh *me)
|
|
|
|
|
{
|
2023-01-18 17:17:32 +11:00
|
|
|
if (me->texspace_flag & ME_TEXSPACE_FLAG_AUTO) {
|
2019-09-23 15:31:11 +02:00
|
|
|
float min[3], max[3];
|
|
|
|
|
|
|
|
|
|
INIT_MINMAX(min, max);
|
2020-05-25 20:16:42 +10:00
|
|
|
if (!BKE_mesh_wrapper_minmax(me, min, max)) {
|
2019-09-23 15:31:11 +02:00
|
|
|
min[0] = min[1] = min[2] = -1.0f;
|
|
|
|
|
max[0] = max[1] = max[2] = 1.0f;
|
|
|
|
|
}
|
2018-11-27 14:17:54 +11:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
float texspace_location[3], texspace_size[3];
|
|
|
|
|
mid_v3_v3v3(texspace_location, min, max);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
texspace_size[0] = (max[0] - min[0]) / 2.0f;
|
|
|
|
|
texspace_size[1] = (max[1] - min[1]) / 2.0f;
|
|
|
|
|
texspace_size[2] = (max[2] - min[2]) / 2.0f;
|
2019-09-23 15:31:11 +02:00
|
|
|
|
|
|
|
|
for (int a = 0; a < 3; a++) {
|
2023-01-18 17:17:32 +11:00
|
|
|
if (texspace_size[a] == 0.0f) {
|
|
|
|
|
texspace_size[a] = 1.0f;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-01-18 17:17:32 +11:00
|
|
|
else if (texspace_size[a] > 0.0f && texspace_size[a] < 0.00001f) {
|
|
|
|
|
texspace_size[a] = 0.00001f;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-01-18 17:17:32 +11:00
|
|
|
else if (texspace_size[a] < 0.0f && texspace_size[a] > -0.00001f) {
|
|
|
|
|
texspace_size[a] = -0.00001f;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2005-07-14 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
copy_v3_v3(me->texspace_location, texspace_location);
|
|
|
|
|
copy_v3_v3(me->texspace_size, texspace_size);
|
2019-09-23 15:31:11 +02:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
me->texspace_flag |= ME_TEXSPACE_FLAG_AUTO_EVALUATED;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 15:31:11 +02:00
|
|
|
void BKE_mesh_texspace_ensure(Mesh *me)
|
2005-07-14 18:04:27 +00:00
|
|
|
{
|
2023-01-18 17:17:32 +11:00
|
|
|
if ((me->texspace_flag & ME_TEXSPACE_FLAG_AUTO) &&
|
|
|
|
|
!(me->texspace_flag & ME_TEXSPACE_FLAG_AUTO_EVALUATED))
|
|
|
|
|
{
|
2019-09-23 15:31:11 +02:00
|
|
|
BKE_mesh_texspace_calc(me);
|
2013-08-19 09:58:28 +00:00
|
|
|
}
|
2005-07-14 18:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
void BKE_mesh_texspace_get(Mesh *me, float r_texspace_location[3], float r_texspace_size[3])
|
2005-07-14 21:57:18 +00:00
|
|
|
{
|
2019-09-23 15:31:11 +02:00
|
|
|
BKE_mesh_texspace_ensure(me);
|
2005-07-14 21:57:18 +00:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
if (r_texspace_location) {
|
|
|
|
|
copy_v3_v3(r_texspace_location, me->texspace_location);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-01-18 17:17:32 +11:00
|
|
|
if (r_texspace_size) {
|
|
|
|
|
copy_v3_v3(r_texspace_size, me->texspace_size);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2005-07-14 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
void BKE_mesh_texspace_get_reference(Mesh *me,
|
|
|
|
|
char **r_texspace_flag,
|
|
|
|
|
float **r_texspace_location,
|
|
|
|
|
float **r_texspace_size)
|
2017-06-09 15:19:02 +02:00
|
|
|
{
|
2019-09-23 15:31:11 +02:00
|
|
|
BKE_mesh_texspace_ensure(me);
|
2017-06-09 15:19:02 +02:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
if (r_texspace_flag != nullptr) {
|
|
|
|
|
*r_texspace_flag = &me->texspace_flag;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-01-18 17:17:32 +11:00
|
|
|
if (r_texspace_location != nullptr) {
|
|
|
|
|
*r_texspace_location = me->texspace_location;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-01-18 17:17:32 +11:00
|
|
|
if (r_texspace_size != nullptr) {
|
|
|
|
|
*r_texspace_size = me->texspace_size;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2017-06-09 15:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
2012-12-21 07:28:14 +00:00
|
|
|
float (*BKE_mesh_orco_verts_get(Object *ob))[3]
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *me = static_cast<Mesh *>(ob->data);
|
2012-05-06 15:15:33 +00:00
|
|
|
Mesh *tme = me->texcomesh ? me->texcomesh : me;
|
2005-07-18 18:28:16 +00:00
|
|
|
|
2007-12-05 12:40:54 +00:00
|
|
|
/* Get appropriate vertex coordinates */
|
2021-11-06 19:16:37 +01:00
|
|
|
float(*vcos)[3] = (float(*)[3])MEM_calloc_arrayN(me->totvert, sizeof(*vcos), "orco mesh");
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = tme->vert_positions();
|
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
|
|
|
|
2020-12-27 21:46:57 -06:00
|
|
|
int totvert = min_ii(tme->totvert, me->totvert);
|
2009-11-01 00:06:53 +00:00
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (int a = 0; a < totvert; a++) {
|
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
|
|
|
copy_v3_v3(vcos[a], positions[a]);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2005-07-18 18:28:16 +00:00
|
|
|
|
2012-12-21 07:28:14 +00:00
|
|
|
return vcos;
|
2007-12-05 12:40:54 +00:00
|
|
|
}
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2023-08-24 10:45:34 +10:00
|
|
|
void BKE_mesh_orco_verts_transform(Mesh *me, float (*orco)[3], int totvert, const bool invert)
|
2007-12-05 12:40:54 +00:00
|
|
|
{
|
2023-01-18 17:17:32 +11:00
|
|
|
float texspace_location[3], texspace_size[3];
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2023-01-18 17:17:32 +11:00
|
|
|
BKE_mesh_texspace_get(me->texcomesh ? me->texcomesh : me, texspace_location, texspace_size);
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (invert) {
|
2020-12-27 21:46:57 -06:00
|
|
|
for (int a = 0; a < totvert; a++) {
|
2007-12-11 20:02:21 +00:00
|
|
|
float *co = orco[a];
|
2023-01-18 17:17:32 +11:00
|
|
|
madd_v3_v3v3v3(co, texspace_location, co, texspace_size);
|
2007-12-11 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2020-12-27 21:46:57 -06:00
|
|
|
for (int a = 0; a < totvert; a++) {
|
2007-12-11 20:02:21 +00:00
|
|
|
float *co = orco[a];
|
2023-01-18 17:17:32 +11:00
|
|
|
co[0] = (co[0] - texspace_location[0]) / texspace_size[0];
|
|
|
|
|
co[1] = (co[1] - texspace_location[1]) / texspace_size[1];
|
|
|
|
|
co[2] = (co[2] - texspace_location[2]) / texspace_size[2];
|
2007-12-11 20:02:21 +00:00
|
|
|
}
|
2005-07-18 19:58:23 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-22 12:04:37 +01:00
|
|
|
void BKE_mesh_orco_ensure(Object *ob, Mesh *mesh)
|
|
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
if (CustomData_has_layer(&mesh->vert_data, CD_ORCO)) {
|
2021-11-22 12:04:37 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Orcos are stored in normalized 0..1 range by convention. */
|
|
|
|
|
float(*orcodata)[3] = BKE_mesh_orco_verts_get(ob);
|
|
|
|
|
BKE_mesh_orco_verts_transform(mesh, orcodata, mesh->totvert, false);
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_add_layer_with_data(&mesh->vert_data, CD_ORCO, orcodata, mesh->totvert, nullptr);
|
2021-11-22 12:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-05 16:03:57 +00:00
|
|
|
Mesh *BKE_mesh_from_object(Object *ob)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2021-11-06 19:16:37 +01:00
|
|
|
if (ob == nullptr) {
|
|
|
|
|
return nullptr;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
if (ob->type == OB_MESH) {
|
2023-07-07 07:35:56 -04:00
|
|
|
return static_cast<Mesh *>(ob->data);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
return nullptr;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-29 15:49:21 +02:00
|
|
|
void BKE_mesh_assign_object(Main *bmain, Object *ob, Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2021-11-06 19:16:37 +01:00
|
|
|
Mesh *old = nullptr;
|
2010-09-09 00:14:51 +00:00
|
|
|
|
2021-11-06 19:16:37 +01:00
|
|
|
if (ob == nullptr) {
|
2012-05-06 15:15:33 +00:00
|
|
|
return;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2019-09-17 17:24:44 +02:00
|
|
|
multires_force_sculpt_rebuild(ob);
|
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (ob->type == OB_MESH) {
|
2023-07-07 07:35:56 -04:00
|
|
|
old = static_cast<Mesh *>(ob->data);
|
2019-04-22 09:39:35 +10:00
|
|
|
if (old) {
|
2015-11-09 19:47:10 +01:00
|
|
|
id_us_min(&old->id);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2012-05-06 15:15:33 +00:00
|
|
|
ob->data = me;
|
2002-10-12 11:37:38 +00:00
|
|
|
id_us_plus((ID *)me);
|
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2020-02-05 11:23:58 +01:00
|
|
|
BKE_object_materials_test(bmain, ob, (ID *)me);
|
2010-09-09 00:14:51 +00:00
|
|
|
|
2020-05-08 10:14:02 +02:00
|
|
|
BKE_modifiers_test_object(ob);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-14 11:29:58 +00:00
|
|
|
void BKE_mesh_material_index_remove(Mesh *me, short index)
|
2009-01-04 14:14:06 +00:00
|
|
|
{
|
2022-08-31 09:09:01 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-09-07 21:41:39 -05:00
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
2022-08-31 09:09:01 -05:00
|
|
|
AttributeWriter<int> material_indices = attributes.lookup_for_write<int>("material_index");
|
|
|
|
|
if (!material_indices) {
|
|
|
|
|
return;
|
2011-04-24 08:06:26 +00:00
|
|
|
}
|
2022-08-31 09:09:01 -05:00
|
|
|
if (material_indices.domain != ATTR_DOMAIN_FACE) {
|
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MutableVArraySpan<int> indices_span(material_indices.varray);
|
|
|
|
|
for (const int i : indices_span.index_range()) {
|
2022-12-13 13:36:48 -06:00
|
|
|
if (indices_span[i] > 0 && indices_span[i] >= index) {
|
2022-08-31 09:09:01 -05:00
|
|
|
indices_span[i]--;
|
2013-08-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-31 09:09:01 -05:00
|
|
|
indices_span.save();
|
|
|
|
|
material_indices.finish();
|
|
|
|
|
|
|
|
|
|
BKE_mesh_tessface_clear(me);
|
2013-08-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 12:04:52 -07:00
|
|
|
bool BKE_mesh_material_index_used(Mesh *me, short index)
|
|
|
|
|
{
|
2022-08-31 09:09:01 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-09-07 21:41:39 -05:00
|
|
|
const AttributeAccessor attributes = me->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<int> material_indices = *attributes.lookup_or_default<int>(
|
2022-08-31 09:09:01 -05:00
|
|
|
"material_index", ATTR_DOMAIN_FACE, 0);
|
|
|
|
|
if (material_indices.is_single()) {
|
|
|
|
|
return material_indices.get_internal_single() == index;
|
|
|
|
|
}
|
|
|
|
|
const VArraySpan<int> indices_span(material_indices);
|
|
|
|
|
return indices_span.contains(index);
|
2019-07-31 12:04:52 -07:00
|
|
|
}
|
|
|
|
|
|
2013-08-14 11:29:58 +00:00
|
|
|
void BKE_mesh_material_index_clear(Mesh *me)
|
|
|
|
|
{
|
2022-08-31 09:09:01 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-09-07 21:41:39 -05:00
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
2022-08-31 09:09:01 -05:00
|
|
|
attributes.remove("material_index");
|
2013-08-14 11:29:58 +00:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
BKE_mesh_tessface_clear(me);
|
2004-03-20 23:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-18 14:27:41 +10:00
|
|
|
void BKE_mesh_material_remap(Mesh *me, const uint *remap, uint remap_len)
|
2015-04-28 07:24:56 +10:00
|
|
|
{
|
2022-08-31 09:09:01 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-09-25 18:33:28 +10:00
|
|
|
const short remap_len_short = short(remap_len);
|
2015-04-28 07:24:56 +10:00
|
|
|
|
|
|
|
|
#define MAT_NR_REMAP(n) \
|
|
|
|
|
if (n < remap_len_short) { \
|
|
|
|
|
BLI_assert(n >= 0 && remap[n] < remap_len_short); \
|
|
|
|
|
n = remap[n]; \
|
|
|
|
|
} \
|
|
|
|
|
((void)0)
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-17 18:05:18 +11:00
|
|
|
if (me->edit_mesh) {
|
|
|
|
|
BMEditMesh *em = me->edit_mesh;
|
2015-04-28 07:24:56 +10:00
|
|
|
BMIter iter;
|
|
|
|
|
BMFace *efa;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-28 07:24:56 +10:00
|
|
|
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
|
|
|
|
|
MAT_NR_REMAP(efa->mat_nr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2022-09-07 21:41:39 -05:00
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
2022-09-12 17:17:11 -05:00
|
|
|
SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
|
|
|
|
|
"material_index", ATTR_DOMAIN_FACE);
|
2022-08-31 09:09:01 -05:00
|
|
|
if (!material_indices) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-12 17:17:11 -05:00
|
|
|
for (const int i : material_indices.span.index_range()) {
|
|
|
|
|
MAT_NR_REMAP(material_indices.span[i]);
|
2015-04-28 07:24:56 +10:00
|
|
|
}
|
2022-09-12 17:17:11 -05:00
|
|
|
material_indices.span.save();
|
2022-08-31 09:09:01 -05:00
|
|
|
material_indices.finish();
|
2015-04-28 07:24:56 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef MAT_NR_REMAP
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 12:00:03 +10:00
|
|
|
void BKE_mesh_smooth_flag_set(Mesh *me, const bool use_smooth)
|
2009-01-04 14:14:06 +00:00
|
|
|
{
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
2019-08-25 12:00:03 +10:00
|
|
|
if (use_smooth) {
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
attributes.remove("sharp_edge");
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
attributes.remove("sharp_face");
|
2011-04-24 08:06:26 +00:00
|
|
|
}
|
2019-08-25 12:00:03 +10:00
|
|
|
else {
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
attributes.remove("sharp_edge");
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
SpanAttributeWriter<bool> sharp_faces = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
"sharp_face", ATTR_DOMAIN_FACE);
|
|
|
|
|
sharp_faces.span.fill(true);
|
|
|
|
|
sharp_faces.finish();
|
2004-03-20 23:59:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-07-17 21:24:43 +00:00
|
|
|
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
void BKE_mesh_sharp_edges_set_from_angle(Mesh *me, const float angle)
|
2022-05-11 11:54:14 +02:00
|
|
|
{
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
bke::MutableAttributeAccessor attributes = me->attributes_for_write();
|
|
|
|
|
if (angle >= M_PI) {
|
|
|
|
|
attributes.remove("sharp_edge");
|
|
|
|
|
attributes.remove("sharp_face");
|
|
|
|
|
return;
|
2022-05-11 11:54:14 +02:00
|
|
|
}
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
if (angle == 0.0f) {
|
|
|
|
|
BKE_mesh_smooth_flag_set(me, false);
|
|
|
|
|
return;
|
2022-05-11 11:54:14 +02:00
|
|
|
}
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
bke::SpanAttributeWriter<bool> sharp_edges = attributes.lookup_or_add_for_write_span<bool>(
|
|
|
|
|
"sharp_edge", ATTR_DOMAIN_EDGE);
|
|
|
|
|
const bool *sharp_faces = static_cast<const bool *>(
|
|
|
|
|
CustomData_get_layer_named(&me->face_data, CD_PROP_BOOL, "sharp_face"));
|
|
|
|
|
bke::mesh::edges_sharp_from_angle_set(me->faces(),
|
|
|
|
|
me->corner_verts(),
|
|
|
|
|
me->corner_edges(),
|
|
|
|
|
me->face_normals(),
|
|
|
|
|
me->corner_to_face_map(),
|
|
|
|
|
sharp_faces,
|
|
|
|
|
angle,
|
|
|
|
|
sharp_edges.span);
|
|
|
|
|
sharp_edges.finish();
|
2022-05-11 11:54:14 +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
|
|
|
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,
|
2022-11-27 18:46:46 -06:00
|
|
|
const MLoopTri *tri,
|
|
|
|
|
int r_edges[3])
|
2018-10-20 21:02:52 +03:00
|
|
|
{
|
|
|
|
|
for (int i = 2, i_next = 0; i_next < 3; i = i_next++) {
|
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_1 = tri->tri[i];
|
|
|
|
|
const int corner_2 = tri->tri[i_next];
|
|
|
|
|
const int vert_1 = corner_verts[corner_1];
|
|
|
|
|
const int vert_2 = corner_verts[corner_2];
|
|
|
|
|
const int edge_i = corner_edges[corner_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
|
|
|
const blender::int2 &edge = edges[edge_i];
|
2018-10-20 21:02:52 +03: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
|
|
|
bool is_real = (vert_1 == edge[0] && vert_2 == edge[1]) ||
|
|
|
|
|
(vert_1 == edge[1] && vert_2 == edge[0]);
|
2018-10-20 21:02:52 +03: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_edges[i] = is_real ? edge_i : -1;
|
2018-10-20 21:02:52 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 08:08:18 -04:00
|
|
|
std::optional<blender::Bounds<blender::float3>> Mesh::bounds_min_max() const
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2021-12-22 11:04:03 -06:00
|
|
|
using namespace blender;
|
2023-06-16 08:08:18 -04:00
|
|
|
if (this->totvert == 0) {
|
|
|
|
|
return std::nullopt;
|
2021-12-22 11:04:03 -06:00
|
|
|
}
|
2023-06-16 08:08:18 -04:00
|
|
|
this->runtime->bounds_cache.ensure(
|
|
|
|
|
[&](Bounds<float3> &r_bounds) { r_bounds = *bounds::min_max(this->vert_positions()); });
|
|
|
|
|
return this->runtime->bounds_cache.data();
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2013-06-12 09:35:02 +00:00
|
|
|
|
2023-03-09 18:11:53 +01:00
|
|
|
void Mesh::bounds_set_eager(const blender::Bounds<float3> &bounds)
|
|
|
|
|
{
|
|
|
|
|
this->runtime->bounds_cache.ensure([&](blender::Bounds<float3> &r_data) { r_data = bounds; });
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 17:17:31 +10:00
|
|
|
void BKE_mesh_transform(Mesh *me, const float mat[4][4], bool do_keys)
|
2014-09-01 20:09:31 +10:00
|
|
|
{
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
MutableSpan<float3> positions = me->vert_positions_for_write();
|
2014-09-01 20:09:31 +10:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
for (float3 &position : positions) {
|
|
|
|
|
mul_m4_v3(mat, position);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2014-09-01 20:09:31 +10:00
|
|
|
|
|
|
|
|
if (do_keys && me->key) {
|
2021-11-06 19:16:37 +01:00
|
|
|
LISTBASE_FOREACH (KeyBlock *, kb, &me->key->block) {
|
|
|
|
|
float *fp = (float *)kb->data;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (int i = kb->totelem; i--; fp += 3) {
|
2014-09-01 20:09:31 +10:00
|
|
|
mul_m4_v3(mat, fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 16:08:48 -05:00
|
|
|
BKE_mesh_tag_positions_changed(me);
|
2014-09-01 20:09:31 +10:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 20:51:01 +02:00
|
|
|
static void translate_positions(MutableSpan<float3> positions, const float3 &translation)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2023-03-27 20:51:01 +02:00
|
|
|
using namespace blender;
|
|
|
|
|
threading::parallel_for(positions.index_range(), 2048, [&](const IndexRange range) {
|
|
|
|
|
for (float3 &position : positions.slice(range)) {
|
|
|
|
|
position += translation;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_translate(Mesh *mesh, const float offset[3], const bool do_keys)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender;
|
|
|
|
|
if (math::is_zero(float3(offset))) {
|
|
|
|
|
return;
|
2013-05-30 18:09:19 +00:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2023-03-27 20:51:01 +02:00
|
|
|
std::optional<Bounds<float3>> bounds;
|
|
|
|
|
if (mesh->runtime->bounds_cache.is_cached()) {
|
|
|
|
|
bounds = mesh->runtime->bounds_cache.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
translate_positions(mesh->vert_positions_for_write(), offset);
|
|
|
|
|
if (do_keys && mesh->key) {
|
|
|
|
|
LISTBASE_FOREACH (KeyBlock *, kb, &mesh->key->block) {
|
|
|
|
|
translate_positions({static_cast<float3 *>(kb->data), kb->totelem}, offset);
|
2013-05-30 18:09:19 +00:00
|
|
|
}
|
2013-05-10 10:07:01 +00:00
|
|
|
}
|
2023-03-27 20:51:01 +02:00
|
|
|
|
|
|
|
|
BKE_mesh_tag_positions_changed_uniformly(mesh);
|
|
|
|
|
|
|
|
|
|
if (bounds) {
|
|
|
|
|
bounds->min += offset;
|
|
|
|
|
bounds->max += offset;
|
|
|
|
|
mesh->bounds_set_eager(*bounds);
|
|
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2012-01-06 00:08:37 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_tessface_clear(Mesh *mesh)
|
2012-02-08 11:52:44 +00:00
|
|
|
{
|
2014-04-01 11:34:00 +11:00
|
|
|
mesh_tessface_clear_intern(mesh, true);
|
2012-02-08 11:52:44 +00:00
|
|
|
}
|
2012-05-08 14:58:38 +00:00
|
|
|
|
2013-06-15 09:42:58 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/* MSelect functions (currently used in weight paint mode) */
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_clear(Mesh *me)
|
|
|
|
|
{
|
2021-08-06 13:59:38 +10:00
|
|
|
MEM_SAFE_FREE(me->mselect);
|
2013-06-15 09:42:58 +00:00
|
|
|
me->totselect = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_validate(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 09:38:37 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2013-06-15 09:42:58 +00:00
|
|
|
MSelect *mselect_src, *mselect_dst;
|
|
|
|
|
int i_src, i_dst;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (me->totselect == 0) {
|
2013-06-15 09:42:58 +00:00
|
|
|
return;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-15 09:42:58 +00:00
|
|
|
mselect_src = me->mselect;
|
2021-11-06 19:16:37 +01:00
|
|
|
mselect_dst = (MSelect *)MEM_malloc_arrayN(
|
|
|
|
|
(me->totselect), sizeof(MSelect), "Mesh selection history");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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 09:38:37 -05:00
|
|
|
const AttributeAccessor attributes = me->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
|
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 09:38:37 -05:00
|
|
|
".select_vert", ATTR_DOMAIN_POINT, false);
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<bool> select_edge = *attributes.lookup_or_default<bool>(
|
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 09:38:37 -05:00
|
|
|
".select_edge", ATTR_DOMAIN_EDGE, false);
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<bool> select_poly = *attributes.lookup_or_default<bool>(
|
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 09:38:37 -05:00
|
|
|
".select_poly", ATTR_DOMAIN_FACE, false);
|
|
|
|
|
|
2013-06-15 09:42:58 +00:00
|
|
|
for (i_src = 0, i_dst = 0; i_src < me->totselect; i_src++) {
|
|
|
|
|
int index = mselect_src[i_src].index;
|
|
|
|
|
switch (mselect_src[i_src].type) {
|
|
|
|
|
case ME_VSEL: {
|
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 09:38:37 -05:00
|
|
|
if (select_vert[index]) {
|
2013-06-15 09:42:58 +00:00
|
|
|
mselect_dst[i_dst] = mselect_src[i_src];
|
|
|
|
|
i_dst++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ME_ESEL: {
|
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 09:38:37 -05:00
|
|
|
if (select_edge[index]) {
|
2013-06-15 09:42:58 +00:00
|
|
|
mselect_dst[i_dst] = mselect_src[i_src];
|
|
|
|
|
i_dst++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ME_FSEL: {
|
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 09:38:37 -05:00
|
|
|
if (select_poly[index]) {
|
2013-06-15 09:42:58 +00:00
|
|
|
mselect_dst[i_dst] = mselect_src[i_src];
|
|
|
|
|
i_dst++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2022-05-17 15:08:18 +02:00
|
|
|
BLI_assert_unreachable();
|
2013-06-15 09:42:58 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-15 09:42:58 +00:00
|
|
|
MEM_freeN(mselect_src);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-15 09:42:58 +00:00
|
|
|
if (i_dst == 0) {
|
|
|
|
|
MEM_freeN(mselect_dst);
|
2021-11-06 19:16:37 +01:00
|
|
|
mselect_dst = nullptr;
|
2013-06-15 09:42:58 +00:00
|
|
|
}
|
|
|
|
|
else if (i_dst != me->totselect) {
|
2021-11-06 19:16:37 +01:00
|
|
|
mselect_dst = (MSelect *)MEM_reallocN(mselect_dst, sizeof(MSelect) * i_dst);
|
2013-06-15 09:42:58 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-15 09:42:58 +00:00
|
|
|
me->totselect = i_dst;
|
|
|
|
|
me->mselect = mselect_dst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BKE_mesh_mselect_find(Mesh *me, int index, int type)
|
|
|
|
|
{
|
2014-07-20 01:30:29 +10:00
|
|
|
BLI_assert(ELEM(type, ME_VSEL, ME_ESEL, ME_FSEL));
|
2013-06-15 09:42:58 +00:00
|
|
|
|
2020-12-27 21:46:57 -06:00
|
|
|
for (int i = 0; i < me->totselect; i++) {
|
2013-06-15 09:42:58 +00:00
|
|
|
if ((me->mselect[i].index == index) && (me->mselect[i].type == type)) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BKE_mesh_mselect_active_get(Mesh *me, int type)
|
|
|
|
|
{
|
2014-07-20 01:30:29 +10:00
|
|
|
BLI_assert(ELEM(type, ME_VSEL, ME_ESEL, ME_FSEL));
|
2013-06-15 09:42:58 +00:00
|
|
|
|
2013-06-30 22:04:03 +00:00
|
|
|
if (me->totselect) {
|
2013-06-15 09:42:58 +00:00
|
|
|
if (me->mselect[me->totselect - 1].type == type) {
|
|
|
|
|
return me->mselect[me->totselect - 1].index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_active_set(Mesh *me, int index, int type)
|
|
|
|
|
{
|
|
|
|
|
const int msel_index = BKE_mesh_mselect_find(me, index, type);
|
|
|
|
|
|
|
|
|
|
if (msel_index == -1) {
|
|
|
|
|
/* add to the end */
|
2021-11-06 19:16:37 +01:00
|
|
|
me->mselect = (MSelect *)MEM_reallocN(me->mselect, sizeof(MSelect) * (me->totselect + 1));
|
2013-06-15 09:42:58 +00:00
|
|
|
me->mselect[me->totselect].index = index;
|
|
|
|
|
me->mselect[me->totselect].type = type;
|
|
|
|
|
me->totselect++;
|
|
|
|
|
}
|
|
|
|
|
else if (msel_index != me->totselect - 1) {
|
|
|
|
|
/* move to the end */
|
2023-01-09 11:12:03 -05:00
|
|
|
std::swap(me->mselect[msel_index], me->mselect[me->totselect - 1]);
|
2013-06-15 09:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_assert((me->mselect[me->totselect - 1].index == index) &&
|
|
|
|
|
(me->mselect[me->totselect - 1].type == type));
|
|
|
|
|
}
|
Bake API - bpy.ops.object.bake()
New operator that can calls a bake function to the current render engine when available. This commit provides no feature for the users, but allows external engines to be accessed by the operator and be integrated with the baking api.
The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result.
The Blender Internal (and multires) system is still running independent, but we eventually will pipe it through the API as well. Cycles baking will come next as a separated commit
Python Operator:
----------------
The operator can be called with some arguments, or a user interface can be created for it. In that case the arguments can be ommited and the interface can expose the settings from bpy.context.scene.render.bake
bpy.ops.object.bake(type='COMBINED', filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage="", normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', save_mode='INTERNAL', use_clear=False, use_split_materials=False, use_automatic_name=False)
Note: external save mode is currently disabled.
Supported Features:
------------------
* Margin - Baked result is extended this many pixels beyond the border of each UV "island," to soften seams in the texture.
* Selected to Active - bake shading on the surface of selected object to the active object. The rays are cast from the lowpoly object inwards towards the highpoly object. If the highpoly object is not entirely involved by the lowpoly object, you can tweak the rays start point with Cage Extrusion. For even more control of the cage you can use a Cage object.
* Cage Extrusion - distance to use for the inward ray cast when using selected to active
* Custom Cage - object to use as cage (instead of the lowpoly object).
* Normal swizzle - change the axis that gets mapped to RGB
* Normal space - save as tangent or object normal spaces
Supported Passes:
-----------------
Any pass that is supported by Blender renderlayer system. Though it's up to the external engine to provide a valid enum with its supported passes. Normal passes get a special treatment since we post-process them to converted and "swizzled"
Development Notes for External Engines:
---------------------------------------
(read them in bake_api.c)
* For a complete implementation example look at the Cycles Bake commit (next).
Review: D421
Reviewed by: Campbell Barton, Brecht van Lommel, Sergey Sharybin, Thomas Dinge
Normal map pipeline "consulting" by Andy Davies (metalliandy)
Original design by Brecht van Lommel.
The entire commit history can be found on the branch: bake-cycles
2014-01-02 19:05:07 -02:00
|
|
|
|
2018-12-17 18:15:41 +11:00
|
|
|
void BKE_mesh_count_selected_items(const Mesh *mesh, int r_count[3])
|
|
|
|
|
{
|
|
|
|
|
r_count[0] = r_count[1] = r_count[2] = 0;
|
2019-02-17 18:05:18 +11:00
|
|
|
if (mesh->edit_mesh) {
|
|
|
|
|
BMesh *bm = mesh->edit_mesh->bm;
|
2018-12-17 18:15:41 +11:00
|
|
|
r_count[0] = bm->totvertsel;
|
|
|
|
|
r_count[1] = bm->totedgesel;
|
|
|
|
|
r_count[2] = bm->totfacesel;
|
|
|
|
|
}
|
|
|
|
|
/* We could support faces in paint modes. */
|
|
|
|
|
}
|
2018-05-08 11:33:31 +02:00
|
|
|
|
2019-08-22 06:28:35 +10:00
|
|
|
float (*BKE_mesh_vert_coords_alloc(const Mesh *mesh, int *r_vert_len))[3]
|
|
|
|
|
{
|
2021-11-06 19:16:37 +01:00
|
|
|
float(*vert_coords)[3] = (float(*)[3])MEM_mallocN(sizeof(float[3]) * mesh->totvert, __func__);
|
2023-07-25 21:59:47 -04:00
|
|
|
MutableSpan(reinterpret_cast<float3 *>(vert_coords), mesh->totvert)
|
|
|
|
|
.copy_from(mesh->vert_positions());
|
2019-08-22 06:28:35 +10:00
|
|
|
if (r_vert_len) {
|
|
|
|
|
*r_vert_len = mesh->totvert;
|
|
|
|
|
}
|
|
|
|
|
return vert_coords;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_vert_coords_apply(Mesh *mesh, const float (*vert_coords)[3])
|
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 00:10:43 -05:00
|
|
|
MutableSpan<float3> positions = mesh->vert_positions_for_write();
|
|
|
|
|
for (const int i : positions.index_range()) {
|
|
|
|
|
copy_v3_v3(positions[i], vert_coords[i]);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2023-02-27 16:08:48 -05:00
|
|
|
BKE_mesh_tag_positions_changed(mesh);
|
2019-08-22 13:45:31 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_vert_coords_apply_with_mat4(Mesh *mesh,
|
|
|
|
|
const float (*vert_coords)[3],
|
|
|
|
|
const float mat[4][4])
|
|
|
|
|
{
|
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 = mesh->vert_positions_for_write();
|
|
|
|
|
for (const int i : positions.index_range()) {
|
|
|
|
|
mul_v3_m4v3(positions[i], mat, vert_coords[i]);
|
2019-08-22 13:45:31 +10:00
|
|
|
}
|
2023-02-27 16:08:48 -05:00
|
|
|
BKE_mesh_tag_positions_changed(mesh);
|
2018-05-08 11:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-12 13:57:11 +05:00
|
|
|
/* **** Depsgraph evaluation **** */
|
|
|
|
|
|
2018-07-13 08:36:10 +02:00
|
|
|
void BKE_mesh_eval_geometry(Depsgraph *depsgraph, Mesh *mesh)
|
2015-05-12 13:57:11 +05:00
|
|
|
{
|
2018-05-02 11:46:56 +02:00
|
|
|
DEG_debug_print_eval(depsgraph, __func__, mesh->id.name, mesh);
|
2019-07-02 16:48:28 +02:00
|
|
|
BKE_mesh_texspace_calc(mesh);
|
2019-06-06 12:26:40 +02:00
|
|
|
/* We are here because something did change in the mesh. This means we can not trust the existing
|
|
|
|
|
* evaluated mesh, and we don't know what parts of the mesh did change. So we simply delete the
|
|
|
|
|
* evaluated mesh and let objects to re-create it with updated settings. */
|
2022-10-12 20:55:26 -05:00
|
|
|
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;
|
2019-06-06 12:26:40 +02:00
|
|
|
}
|
2019-07-02 16:48:28 +02:00
|
|
|
if (DEG_is_active(depsgraph)) {
|
2023-07-07 07:35:56 -04:00
|
|
|
Mesh *mesh_orig = reinterpret_cast<Mesh *>(DEG_get_original_id(&mesh->id));
|
2023-01-18 17:17:32 +11:00
|
|
|
if (mesh->texspace_flag & ME_TEXSPACE_FLAG_AUTO_EVALUATED) {
|
|
|
|
|
mesh_orig->texspace_flag |= ME_TEXSPACE_FLAG_AUTO_EVALUATED;
|
|
|
|
|
copy_v3_v3(mesh_orig->texspace_location, mesh->texspace_location);
|
|
|
|
|
copy_v3_v3(mesh_orig->texspace_size, mesh->texspace_size);
|
2019-07-02 16:48:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-12 13:57:11 +05:00
|
|
|
}
|