2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2005 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup modifiers
|
2011-02-25 13:57:17 +00:00
|
|
|
*/
|
|
|
|
|
|
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"
|
|
|
|
|
#include "BLI_math_vector.h"
|
2019-02-25 11:39:14 +01:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2024-02-09 18:59:42 +01:00
|
|
|
#include "BLT_translation.hh"
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
#include "DNA_defaults.h"
|
2018-05-17 16:21:04 +02:00
|
|
|
#include "DNA_mesh_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_object_types.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "DNA_screen_types.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_bvhutils.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-06-14 11:59:32 -04:00
|
|
|
#include "BKE_mesh.hh"
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "UI_interface.hh"
|
|
|
|
|
#include "UI_resources.hh"
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2024-07-10 18:30:02 +02:00
|
|
|
#include "RNA_prototypes.hh"
|
2018-05-17 16:21:04 +02:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph.hh"
|
|
|
|
|
#include "DEG_depsgraph_query.hh"
|
2018-05-23 16:36:44 +02:00
|
|
|
|
2023-05-04 18:35:37 +02:00
|
|
|
#include "MOD_modifiertypes.hh"
|
|
|
|
|
#include "MOD_ui_common.hh"
|
2020-06-23 17:25:44 +02:00
|
|
|
|
2010-04-12 22:33:43 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void init_data(ModifierData *md)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
|
2018-06-17 17:04:27 +02:00
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(surmd, modifier));
|
|
|
|
|
|
|
|
|
|
MEMCPY_STRUCT_AFTER(surmd, DNA_struct_default_get(SurfaceModifierData), modifier);
|
2019-01-28 21:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void copy_data(const ModifierData *md_src, ModifierData *md_dst, const int flag)
|
2019-01-28 21:48:09 +01:00
|
|
|
{
|
|
|
|
|
SurfaceModifierData *surmd_dst = (SurfaceModifierData *)md_dst;
|
|
|
|
|
|
2020-05-08 10:14:02 +02:00
|
|
|
BKE_modifier_copydata_generic(md_src, md_dst, flag);
|
2019-01-28 21:48:09 +01:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
memset(&surmd_dst->runtime, 0, sizeof(surmd_dst->runtime));
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void free_data(ModifierData *md)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
|
2012-03-06 18:40:15 +00:00
|
|
|
|
|
|
|
|
if (surmd) {
|
2023-01-10 17:07:30 +11:00
|
|
|
if (surmd->runtime.bvhtree) {
|
|
|
|
|
free_bvhtree_from_mesh(surmd->runtime.bvhtree);
|
|
|
|
|
MEM_SAFE_FREE(surmd->runtime.bvhtree);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
if (surmd->runtime.mesh) {
|
2023-05-04 18:35:37 +02:00
|
|
|
BKE_id_free(nullptr, surmd->runtime.mesh);
|
|
|
|
|
surmd->runtime.mesh = nullptr;
|
2018-05-08 14:21:02 +02:00
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
MEM_SAFE_FREE(surmd->runtime.vert_positions_prev);
|
2018-06-17 17:04:27 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
MEM_SAFE_FREE(surmd->runtime.vert_velocities);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static bool depends_on_time(Scene * /*scene*/, ModifierData * /*md*/)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2013-06-02 03:59:19 +00:00
|
|
|
return true;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void deform_verts(ModifierData *md,
|
|
|
|
|
const ModifierEvalContext *ctx,
|
|
|
|
|
Mesh *mesh,
|
2023-11-14 10:54:57 +01:00
|
|
|
blender::MutableSpan<blender::float3> positions)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
|
2023-05-04 18:35:37 +02:00
|
|
|
const int cfra = int(DEG_get_ctime(ctx->depsgraph));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-01-22 12:51:14 +01:00
|
|
|
/* Free mesh and BVH cache. */
|
2023-01-10 17:07:30 +11:00
|
|
|
if (surmd->runtime.bvhtree) {
|
|
|
|
|
free_bvhtree_from_mesh(surmd->runtime.bvhtree);
|
|
|
|
|
MEM_SAFE_FREE(surmd->runtime.bvhtree);
|
2019-01-22 12:51:14 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
if (surmd->runtime.mesh) {
|
2023-05-04 18:35:37 +02:00
|
|
|
BKE_id_free(nullptr, surmd->runtime.mesh);
|
|
|
|
|
surmd->runtime.mesh = nullptr;
|
2018-05-17 16:21:04 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-17 16:21:04 +02:00
|
|
|
if (mesh) {
|
2024-05-20 13:18:24 -04:00
|
|
|
surmd->runtime.mesh = BKE_mesh_copy_for_eval(*mesh);
|
2018-11-27 17:21:16 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-01 17:33:04 +02:00
|
|
|
if (!ctx->object->pd) {
|
2023-07-27 12:04:18 +10:00
|
|
|
printf("SurfaceModifier deform_verts: Should not happen!\n");
|
2010-04-11 22:12:30 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
if (surmd->runtime.mesh) {
|
2022-03-28 12:29:47 +11:00
|
|
|
uint mesh_verts_num = 0, i = 0;
|
2010-04-11 22:12:30 +00:00
|
|
|
int init = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-28 12:14:09 -05:00
|
|
|
surmd->runtime.mesh->vert_positions_for_write().copy_from(positions);
|
2023-12-12 15:38:42 -05:00
|
|
|
surmd->runtime.mesh->tag_positions_changed();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh_verts_num = surmd->runtime.mesh->verts_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
if ((mesh_verts_num != surmd->runtime.verts_num) ||
|
2023-05-04 18:35:37 +02:00
|
|
|
(surmd->runtime.vert_positions_prev == nullptr) ||
|
|
|
|
|
(surmd->runtime.vert_velocities == nullptr) || (cfra != surmd->runtime.cfra_prev + 1))
|
2023-01-10 17:07:30 +11:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
MEM_SAFE_FREE(surmd->runtime.vert_positions_prev);
|
|
|
|
|
MEM_SAFE_FREE(surmd->runtime.vert_velocities);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-04 18:35:37 +02:00
|
|
|
surmd->runtime.vert_positions_prev = static_cast<float(*)[3]>(
|
|
|
|
|
MEM_calloc_arrayN(mesh_verts_num, sizeof(float[3]), __func__));
|
|
|
|
|
surmd->runtime.vert_velocities = static_cast<float(*)[3]>(
|
|
|
|
|
MEM_calloc_arrayN(mesh_verts_num, sizeof(float[3]), __func__));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
surmd->runtime.verts_num = mesh_verts_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
init = 1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* convert to global coordinates and calculate velocity */
|
2023-06-14 11:59:32 -04:00
|
|
|
blender::MutableSpan<blender::float3> positions =
|
|
|
|
|
surmd->runtime.mesh->vert_positions_for_write();
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
for (i = 0; i < mesh_verts_num; i++) {
|
|
|
|
|
float *vec = positions[i];
|
2024-02-14 16:14:49 +01:00
|
|
|
mul_m4_v3(ctx->object->object_to_world().ptr(), vec);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:15:10 +10:00
|
|
|
if (init) {
|
2023-01-10 17:07:30 +11:00
|
|
|
zero_v3(surmd->runtime.vert_velocities[i]);
|
2019-04-22 09:15:10 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2023-01-10 17:07:30 +11:00
|
|
|
sub_v3_v3v3(surmd->runtime.vert_velocities[i], vec, surmd->runtime.vert_positions_prev[i]);
|
2019-04-22 09:15:10 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
copy_v3_v3(surmd->runtime.vert_positions_prev[i], vec);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
surmd->runtime.cfra_prev = cfra;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
const bool has_face = surmd->runtime.mesh->faces_num > 0;
|
2023-12-20 02:21:48 +01:00
|
|
|
const bool has_edge = surmd->runtime.mesh->edges_num > 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
if (has_face || has_edge) {
|
2023-05-04 18:35:37 +02:00
|
|
|
surmd->runtime.bvhtree = static_cast<BVHTreeFromMesh *>(
|
|
|
|
|
MEM_callocN(sizeof(BVHTreeFromMesh), __func__));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (has_face) {
|
2023-01-10 17:07:30 +11:00
|
|
|
BKE_bvhtree_from_mesh_get(
|
2023-12-19 14:57:49 +01:00
|
|
|
surmd->runtime.bvhtree, surmd->runtime.mesh, BVHTREE_FROM_CORNER_TRIS, 2);
|
2021-05-30 22:26:31 +02:00
|
|
|
}
|
|
|
|
|
else if (has_edge) {
|
2023-01-10 17:07:30 +11:00
|
|
|
BKE_bvhtree_from_mesh_get(
|
|
|
|
|
surmd->runtime.bvhtree, surmd->runtime.mesh, BVHTREE_FROM_EDGES, 2);
|
2021-05-30 22:26:31 +02:00
|
|
|
}
|
2019-04-22 09:15:10 +10:00
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 18:35:37 +02:00
|
|
|
static void panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
2023-05-04 18:35:37 +02:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2024-01-11 19:49:03 +01:00
|
|
|
uiItemL(layout, RPT_("Settings are inside the Physics tab"), ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
modifier_panel_end(layout, ptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void panel_register(ARegionType *region_type)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
modifier_panel_register(region_type, eModifierType_Surface, panel_draw);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void blend_read(BlendDataReader * /*reader*/, ModifierData *md)
|
2020-06-23 17:25:44 +02:00
|
|
|
{
|
|
|
|
|
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
|
|
|
|
|
|
2023-01-10 17:07:30 +11:00
|
|
|
memset(&surmd->runtime, 0, sizeof(surmd->runtime));
|
2020-06-23 17:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
ModifierTypeInfo modifierType_Surface = {
|
2023-07-26 17:08:14 +02:00
|
|
|
/*idname*/ "Surface",
|
2023-01-16 12:41:11 +11:00
|
|
|
/*name*/ N_("Surface"),
|
2023-07-27 12:04:18 +10:00
|
|
|
/*struct_name*/ "SurfaceModifierData",
|
|
|
|
|
/*struct_size*/ sizeof(SurfaceModifierData),
|
2023-01-16 12:41:11 +11:00
|
|
|
/*srna*/ &RNA_SurfaceModifier,
|
2023-11-14 10:03:56 +01:00
|
|
|
/*type*/ ModifierTypeType::OnlyDeform,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs |
|
2012-05-06 13:38:33 +00:00
|
|
|
eModifierTypeFlag_NoUserAdd,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*icon*/ ICON_MOD_PHYSICS,
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
/*copy_data*/ copy_data,
|
|
|
|
|
|
|
|
|
|
/*deform_verts*/ deform_verts,
|
|
|
|
|
/*deform_matrices*/ nullptr,
|
|
|
|
|
/*deform_verts_EM*/ nullptr,
|
|
|
|
|
/*deform_matrices_EM*/ nullptr,
|
|
|
|
|
/*modify_mesh*/ nullptr,
|
|
|
|
|
/*modify_geometry_set*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*init_data*/ init_data,
|
|
|
|
|
/*required_data_mask*/ nullptr,
|
|
|
|
|
/*free_data*/ free_data,
|
|
|
|
|
/*is_disabled*/ nullptr,
|
|
|
|
|
/*update_depsgraph*/ nullptr,
|
|
|
|
|
/*depends_on_time*/ depends_on_time,
|
|
|
|
|
/*depends_on_normals*/ nullptr,
|
|
|
|
|
/*foreach_ID_link*/ nullptr,
|
|
|
|
|
/*foreach_tex_link*/ nullptr,
|
|
|
|
|
/*free_runtime_data*/ nullptr,
|
|
|
|
|
/*panel_register*/ panel_register,
|
|
|
|
|
/*blend_write*/ nullptr,
|
|
|
|
|
/*blend_read*/ blend_read,
|
2024-01-18 22:51:30 +01:00
|
|
|
/*foreach_cache*/ nullptr,
|
2010-04-11 22:12:30 +00:00
|
|
|
};
|