diff options
Diffstat (limited to 'source/blender/blenkernel/intern')
39 files changed, 1130 insertions, 1049 deletions
diff --git a/source/blender/blenkernel/intern/asset.cc b/source/blender/blenkernel/intern/asset.cc index 67802b1d6b4..7103e017847 100644 --- a/source/blender/blenkernel/intern/asset.cc +++ b/source/blender/blenkernel/intern/asset.cc @@ -27,21 +27,31 @@ using namespace blender; AssetMetaData *BKE_asset_metadata_create() { - AssetMetaData *asset_data = (AssetMetaData *)MEM_callocN(sizeof(*asset_data), __func__); - memcpy(asset_data, DNA_struct_default_get(AssetMetaData), sizeof(*asset_data)); - return asset_data; + const AssetMetaData *default_metadata = DNA_struct_default_get(AssetMetaData); + return MEM_new<AssetMetaData>(__func__, *default_metadata); } void BKE_asset_metadata_free(AssetMetaData **asset_data) { - if ((*asset_data)->properties) { - IDP_FreeProperty((*asset_data)->properties); + (*asset_data)->~AssetMetaData(); + MEM_SAFE_FREE(*asset_data); +} + +AssetMetaData::~AssetMetaData() +{ + if (properties) { + IDP_FreeProperty(properties); } - MEM_SAFE_FREE((*asset_data)->author); - MEM_SAFE_FREE((*asset_data)->description); - BLI_freelistN(&(*asset_data)->tags); + MEM_SAFE_FREE(author); + MEM_SAFE_FREE(description); + BLI_freelistN(&tags); +} - MEM_SAFE_FREE(*asset_data); +std::unique_ptr<AssetMetaData> BKE_asset_metadata_move_to_unique_ptr(AssetMetaData *asset_data) +{ + std::unique_ptr unique_asset_data = std::make_unique<AssetMetaData>(*asset_data); + *asset_data = *DNA_struct_default_get(AssetMetaData); + return unique_asset_data; } static AssetTag *asset_metadata_tag_add(AssetMetaData *asset_data, const char *const name) diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc index 62d03b2d79b..a9fe59eba64 100644 --- a/source/blender/blenkernel/intern/asset_catalog.cc +++ b/source/blender/blenkernel/intern/asset_catalog.cc @@ -508,14 +508,13 @@ CatalogFilePath AssetCatalogService::find_suitable_cdf_path_for_writing( "catalog definition file should be put"); /* Ask the asset library API for an appropriate location. */ - char suitable_root_path[PATH_MAX]; - const bool asset_lib_root_found = BKE_asset_library_find_suitable_root_path_from_path( - blend_file_path.c_str(), suitable_root_path); - if (asset_lib_root_found) { + const std::string suitable_root_path = BKE_asset_library_find_suitable_root_path_from_path( + blend_file_path); + if (!suitable_root_path.empty()) { char asset_lib_cdf_path[PATH_MAX]; BLI_path_join(asset_lib_cdf_path, sizeof(asset_lib_cdf_path), - suitable_root_path, + suitable_root_path.c_str(), DEFAULT_CATALOG_FILENAME.c_str()); return asset_lib_cdf_path; } diff --git a/source/blender/blenkernel/intern/asset_library.cc b/source/blender/blenkernel/intern/asset_library.cc index b8420af1168..4dccee425c6 100644 --- a/source/blender/blenkernel/intern/asset_library.cc +++ b/source/blender/blenkernel/intern/asset_library.cc @@ -7,11 +7,14 @@ #include <memory> #include "BKE_asset_library.hh" +#include "BKE_asset_representation.hh" +#include "BKE_lib_remap.h" #include "BKE_main.h" #include "BKE_preferences.h" #include "BLI_fileops.h" #include "BLI_path_util.h" +#include "BLI_set.hh" #include "DNA_asset_types.h" #include "DNA_userdef_types.h" @@ -50,22 +53,22 @@ bool BKE_asset_library_has_any_unsaved_catalogs() return service->has_any_unsaved_catalogs(); } -bool BKE_asset_library_find_suitable_root_path_from_path(const char *input_path, - char *r_library_path) +std::string BKE_asset_library_find_suitable_root_path_from_path( + const blender::StringRefNull input_path) { if (bUserAssetLibrary *preferences_lib = BKE_preferences_asset_library_containing_path( - &U, input_path)) { - BLI_strncpy(r_library_path, preferences_lib->path, FILE_MAXDIR); - return true; + &U, input_path.c_str())) { + return preferences_lib->path; } - BLI_split_dir_part(input_path, r_library_path, FILE_MAXDIR); - return r_library_path[0] != '\0'; + char buffer[FILE_MAXDIR]; + BLI_split_dir_part(input_path.c_str(), buffer, FILE_MAXDIR); + return buffer; } -bool BKE_asset_library_find_suitable_root_path_from_main(const Main *bmain, char *r_library_path) +std::string BKE_asset_library_find_suitable_root_path_from_main(const Main *bmain) { - return BKE_asset_library_find_suitable_root_path_from_path(bmain->filepath, r_library_path); + return BKE_asset_library_find_suitable_root_path_from_path(bmain->filepath); } blender::bke::AssetCatalogService *BKE_asset_library_get_catalog_service( @@ -98,6 +101,13 @@ void BKE_asset_library_refresh_catalog_simplename(struct AssetLibrary *asset_lib lib->refresh_catalog_simplename(asset_data); } +void BKE_asset_library_remap_ids(IDRemapper *mappings) +{ + blender::bke::AssetLibraryService *service = blender::bke::AssetLibraryService::get(); + service->foreach_loaded_asset_library( + [mappings](blender::bke::AssetLibrary &library) { library.remap_ids(*mappings); }); +} + namespace blender::bke { AssetLibrary::AssetLibrary() : catalog_service(std::make_unique<AssetCatalogService>()) @@ -111,7 +121,7 @@ AssetLibrary::~AssetLibrary() } } -void AssetLibrary::load(StringRefNull library_root_directory) +void AssetLibrary::load_catalogs(StringRefNull library_root_directory) { auto catalog_service = std::make_unique<AssetCatalogService>(library_root_directory); catalog_service->load_from_disk(); @@ -123,6 +133,44 @@ void AssetLibrary::refresh() this->catalog_service->reload_catalogs(); } +AssetRepresentation &AssetLibrary::add_external_asset(StringRef name, + std::unique_ptr<AssetMetaData> metadata) +{ + asset_storage_.append(std::make_unique<AssetRepresentation>(name, std::move(metadata))); + return *asset_storage_.last(); +} + +AssetRepresentation &AssetLibrary::add_local_id_asset(ID &id) +{ + asset_storage_.append(std::make_unique<AssetRepresentation>(id)); + return *asset_storage_.last(); +} + +std::optional<int> AssetLibrary::find_asset_index(const AssetRepresentation &asset) +{ + int index = 0; + /* Find index of asset. */ + for (auto &asset_uptr : asset_storage_) { + if (&asset == asset_uptr.get()) { + return index; + } + index++; + } + + return {}; +} + +bool AssetLibrary::remove_asset(AssetRepresentation &asset) +{ + std::optional<int> asset_index = find_asset_index(asset); + if (!asset_index) { + return false; + } + + asset_storage_.remove_and_reorder(*asset_index); + return true; +} + namespace { void asset_library_on_save_post(struct Main *main, struct PointerRNA **pointers, @@ -166,6 +214,28 @@ void AssetLibrary::on_blend_save_post(struct Main *main, } } +void AssetLibrary::remap_ids(IDRemapper &mappings) +{ + Set<AssetRepresentation *> removed_id_assets; + + for (auto &asset_uptr : asset_storage_) { + if (!asset_uptr->is_local_id()) { + continue; + } + + IDRemapperApplyResult result = BKE_id_remapper_apply( + &mappings, &asset_uptr->local_asset_id_, ID_REMAP_APPLY_DEFAULT); + if (result == ID_REMAP_RESULT_SOURCE_UNASSIGNED) { + removed_id_assets.add(asset_uptr.get()); + } + } + + /* Remove the assets from storage. */ + for (AssetRepresentation *asset : removed_id_assets) { + remove_asset(*asset); + } +} + void AssetLibrary::refresh_catalog_simplename(struct AssetMetaData *asset_data) { if (BLI_uuid_is_nil(asset_data->catalog_id)) { diff --git a/source/blender/blenkernel/intern/asset_library_service.cc b/source/blender/blenkernel/intern/asset_library_service.cc index cd8de7908bf..35441b9b795 100644 --- a/source/blender/blenkernel/intern/asset_library_service.cc +++ b/source/blender/blenkernel/intern/asset_library_service.cc @@ -6,6 +6,7 @@ #include "asset_library_service.hh" +#include "BKE_asset_library.hh" #include "BKE_blender.h" #include "BKE_preferences.h" @@ -47,15 +48,10 @@ AssetLibrary *AssetLibraryService::get_asset_library( { if (library_reference.type == ASSET_LIBRARY_LOCAL) { /* For the "Current File" library we get the asset library root path based on main. */ - char root_path[FILE_MAX]; - if (bmain) { - BKE_asset_library_find_suitable_root_path_from_main(bmain, root_path); - } - else { - root_path[0] = '\0'; - } + std::string root_path = bmain ? BKE_asset_library_find_suitable_root_path_from_main(bmain) : + ""; - if (root_path[0] == '\0') { + if (root_path.empty()) { /* File wasn't saved yet. */ return get_asset_library_current_file(); } @@ -104,7 +100,7 @@ AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull top_l AssetLibrary *lib = lib_uptr.get(); lib->on_blend_save_handler_register(); - lib->load(top_dir_trailing_slash); + lib->load_catalogs(top_dir_trailing_slash); on_disk_libraries_.add_new(top_dir_trailing_slash, std::move(lib_uptr)); CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_dir_trailing_slash.c_str()); @@ -180,4 +176,15 @@ bool AssetLibraryService::has_any_unsaved_catalogs() const return false; } +void AssetLibraryService::foreach_loaded_asset_library(FunctionRef<void(AssetLibrary &)> fn) const +{ + if (current_file_library_) { + fn(*current_file_library_); + } + + for (const auto &asset_lib_uptr : on_disk_libraries_.values()) { + fn(*asset_lib_uptr); + } +} + } // namespace blender::bke diff --git a/source/blender/blenkernel/intern/asset_library_service.hh b/source/blender/blenkernel/intern/asset_library_service.hh index c22c6b182ce..6caaea72875 100644 --- a/source/blender/blenkernel/intern/asset_library_service.hh +++ b/source/blender/blenkernel/intern/asset_library_service.hh @@ -12,10 +12,13 @@ #include "BKE_asset_library.hh" +#include "BLI_function_ref.hh" #include "BLI_map.hh" #include <memory> +struct AssetLibraryReference; + namespace blender::bke { /** @@ -58,11 +61,16 @@ class AssetLibraryService { /** Returns whether there are any known asset libraries with unsaved catalog edits. */ bool has_any_unsaved_catalogs() const; + void foreach_loaded_asset_library(FunctionRef<void(AssetLibrary &)> fn) const; + protected: static std::unique_ptr<AssetLibraryService> instance_; /* Mapping absolute path of the library's top-level directory to the AssetLibrary instance. */ Map<std::string, AssetLibraryPtr> on_disk_libraries_; + /** Library without a known path, i.e. the "Current File" library if the file isn't saved yet. If + * the file was saved, a valid path for the library can be determined and #on_disk_libraries_ + * above should be used. */ AssetLibraryPtr current_file_library_; /* Handlers for managing the life cycle of the AssetLibraryService instance. */ diff --git a/source/blender/blenkernel/intern/asset_library_service_test.cc b/source/blender/blenkernel/intern/asset_library_service_test.cc index 7952e7ea3b0..18fdcb80155 100644 --- a/source/blender/blenkernel/intern/asset_library_service_test.cc +++ b/source/blender/blenkernel/intern/asset_library_service_test.cc @@ -8,6 +8,9 @@ #include "BKE_appdir.h" #include "BKE_callbacks.h" +#include "BKE_main.h" + +#include "DNA_asset_types.h" #include "CLG_log.h" @@ -102,6 +105,26 @@ TEST_F(AssetLibraryServiceTest, library_pointers) * cannot be reliably tested by just pointer comparison, though. */ } +TEST_F(AssetLibraryServiceTest, library_from_reference) +{ + AssetLibraryService *service = AssetLibraryService::get(); + AssetLibrary *const lib = service->get_asset_library_on_disk(asset_library_root_); + AssetLibrary *const curfile_lib = service->get_asset_library_current_file(); + + AssetLibraryReference ref{}; + ref.type = ASSET_LIBRARY_LOCAL; + EXPECT_EQ(curfile_lib, service->get_asset_library(nullptr, ref)) + << "Getting the local (current file) reference without a main saved on disk should return " + "the current file library"; + + Main dummy_main{}; + std::string dummy_filepath = asset_library_root_ + SEP + "dummy.blend"; + BLI_strncpy(dummy_main.filepath, dummy_filepath.c_str(), sizeof(dummy_main.filepath)); + EXPECT_EQ(lib, service->get_asset_library(&dummy_main, ref)) + << "Getting the local (current file) reference with a main saved on disk should return " + "the an asset library for this directory"; +} + TEST_F(AssetLibraryServiceTest, library_path_trailing_slashes) { AssetLibraryService *service = AssetLibraryService::get(); diff --git a/source/blender/blenkernel/intern/asset_representation.cc b/source/blender/blenkernel/intern/asset_representation.cc new file mode 100644 index 00000000000..bbaa634d5ad --- /dev/null +++ b/source/blender/blenkernel/intern/asset_representation.cc @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup bke + */ + +#include <stdexcept> + +#include "DNA_ID.h" +#include "DNA_asset_types.h" + +#include "BKE_asset.h" +#include "BKE_asset_representation.hh" + +namespace blender::bke { + +AssetRepresentation::AssetRepresentation(StringRef name, std::unique_ptr<AssetMetaData> metadata) + : is_local_id_(false), external_asset_() +{ + external_asset_.name = name; + external_asset_.metadata_ = std::move(metadata); +} + +AssetRepresentation::AssetRepresentation(ID &id) : is_local_id_(true), local_asset_id_(&id) +{ + if (!id.asset_data) { + throw std::invalid_argument("Passed ID is not an asset"); + } +} + +AssetRepresentation::AssetRepresentation(AssetRepresentation &&other) + : is_local_id_(other.is_local_id_) +{ + if (is_local_id_) { + local_asset_id_ = other.local_asset_id_; + other.local_asset_id_ = nullptr; + } + else { + external_asset_ = std::move(other.external_asset_); + } +} + +AssetRepresentation::~AssetRepresentation() +{ + if (!is_local_id_) { + external_asset_.~ExternalAsset(); + } +} + +StringRefNull AssetRepresentation::get_name() const +{ + if (is_local_id_) { + return local_asset_id_->name + 2; + } + + return external_asset_.name; +} + +AssetMetaData &AssetRepresentation::get_metadata() const +{ + return is_local_id_ ? *local_asset_id_->asset_data : *external_asset_.metadata_; +} + +bool AssetRepresentation::is_local_id() const +{ + return is_local_id_; +} + +} // namespace blender::bke + +/* ---------------------------------------------------------------------- */ +/** \name C-API + * \{ */ + +using namespace blender; + +const char *BKE_asset_representation_name_get(const AssetRepresentation *asset_handle) +{ + const bke::AssetRepresentation *asset = reinterpret_cast<const bke::AssetRepresentation *>( + asset_handle); + return asset->get_name().c_str(); +} + +AssetMetaData *BKE_asset_representation_metadata_get(const AssetRepresentation *asset_handle) +{ + const bke::AssetRepresentation *asset = reinterpret_cast<const bke::AssetRepresentation *>( + asset_handle); + return &asset->get_metadata(); +} + +bool BKE_asset_representation_is_local_id(const AssetRepresentation *asset_handle) +{ + const bke::AssetRepresentation *asset = reinterpret_cast<const bke::AssetRepresentation *>( + asset_handle); + return asset->is_local_id(); +} + +/** \} */ diff --git a/source/blender/blenkernel/intern/asset_test.cc b/source/blender/blenkernel/intern/asset_test.cc index fa8769862a8..5ff65054926 100644 --- a/source/blender/blenkernel/intern/asset_test.cc +++ b/source/blender/blenkernel/intern/asset_test.cc @@ -13,7 +13,7 @@ namespace blender::bke::tests { TEST(AssetMetadataTest, set_catalog_id) { - AssetMetaData meta; + AssetMetaData meta{}; const bUUID uuid = BLI_uuid_generate_random(); /* Test trivial values. */ diff --git a/source/blender/blenkernel/intern/attribute.cc b/source/blender/blenkernel/intern/attribute.cc index 1a54454bf9a..80647362826 100644 --- a/source/blender/blenkernel/intern/attribute.cc +++ b/source/blender/blenkernel/intern/attribute.cc @@ -495,29 +495,14 @@ CustomDataLayer *BKE_id_attributes_active_get(ID *id) return nullptr; } -void BKE_id_attributes_active_set(ID *id, CustomDataLayer *active_layer) +void BKE_id_attributes_active_set(ID *id, const char *name) { - DomainInfo info[ATTR_DOMAIN_NUM]; - get_domains(id, info); - - int index = 0; + const CustomDataLayer *layer = BKE_id_attribute_search( + id, name, CD_MASK_PROP_ALL, ATTR_DOMAIN_MASK_ALL); + BLI_assert(layer != nullptr); - for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) { - const CustomData *customdata = info[domain].customdata; - if (customdata == nullptr) { - continue; - } - for (int i = 0; i < customdata->totlayer; i++) { - const CustomDataLayer *layer = &customdata->layers[i]; - if (layer == active_layer) { - *BKE_id_attributes_active_index_p(id) = index; - return; - } - if (CD_MASK_PROP_ALL & CD_TYPE_AS_MASK(layer->type)) { - index++; - } - } - } + const int index = BKE_id_attribute_to_index(id, layer, ATTR_DOMAIN_MASK_ALL, CD_MASK_PROP_ALL); + *BKE_id_attributes_active_index_p(id) = index; } int *BKE_id_attributes_active_index_p(ID *id) diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.cc index 88ba50fe901..73b3d6fcba6 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.cc @@ -49,11 +49,11 @@ static void cloth_update_spring_lengths(ClothModifierData *clmd, Mesh *mesh); static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh); static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh); -typedef struct BendSpringRef { +struct BendSpringRef { int index; int polys; ClothSpring *spring; -} BendSpringRef; +}; /****************************************************************************** * @@ -64,13 +64,13 @@ typedef struct BendSpringRef { static BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon) { if (!clmd) { - return NULL; + return nullptr; } Cloth *cloth = clmd->clothObject; if (!cloth) { - return NULL; + return nullptr; } ClothVertex *verts = cloth->verts; @@ -78,14 +78,14 @@ static BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon) /* in the moment, return zero if no faces there */ if (!cloth->primitive_num) { - return NULL; + return nullptr; } /* Create quad-tree with k=26. */ BVHTree *bvhtree = BLI_bvhtree_new(cloth->primitive_num, epsilon, 4, 26); /* fill tree */ - if (clmd->hairdata == NULL) { + if (clmd->hairdata == nullptr) { for (int i = 0; i < cloth->primitive_num; i++, vt++) { float co[3][3]; @@ -123,7 +123,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self) ClothVertex *verts = cloth->verts; const MVertTri *vt; - BLI_assert(!(clmd->hairdata != NULL && self)); + BLI_assert(!(clmd->hairdata != nullptr && self)); if (self) { bvhtree = cloth->bvhselftree; @@ -139,7 +139,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self) vt = cloth->tri; /* update vertex position in bvh tree */ - if (clmd->hairdata == NULL) { + if (clmd->hairdata == nullptr) { if (verts && vt) { for (i = 0; i < cloth->primitive_num; i++, vt++) { float co[3][3], co_moving[3][3]; @@ -163,7 +163,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self) copy_v3_v3(co[1], verts[vt->tri[1]].tx); copy_v3_v3(co[2], verts[vt->tri[2]].tx); - ret = BLI_bvhtree_update_node(bvhtree, i, co[0], NULL, 3); + ret = BLI_bvhtree_update_node(bvhtree, i, co[0], nullptr, 3); } /* check if tree is already full */ @@ -185,7 +185,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self) copy_v3_v3(co[0], verts[edges[i].v1].tx); copy_v3_v3(co[1], verts[edges[i].v2].tx); - if (!BLI_bvhtree_update_node(bvhtree, i, co[0], NULL, 2)) { + if (!BLI_bvhtree_update_node(bvhtree, i, co[0], nullptr, 2)) { break; } } @@ -216,14 +216,14 @@ static bool do_init_cloth(Object *ob, ClothModifierData *clmd, Mesh *result, int cache = clmd->point_cache; /* initialize simulation data if it didn't exist already */ - if (clmd->clothObject == NULL) { + if (clmd->clothObject == nullptr) { if (!cloth_from_object(ob, clmd, result, framenr, 1)) { BKE_ptcache_invalidate(cache); BKE_modifier_set_error(ob, &(clmd->modifier), "Can't initialize cloth"); return false; } - if (clmd->clothObject == NULL) { + if (clmd->clothObject == nullptr) { BKE_ptcache_invalidate(cache); BKE_modifier_set_error(ob, &(clmd->modifier), "Null cloth object"); return false; @@ -248,9 +248,9 @@ static int do_step_cloth( Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, Mesh *result, int framenr) { /* simulate 1 frame forward */ - ClothVertex *verts = NULL; + ClothVertex *verts = nullptr; Cloth *cloth; - ListBase *effectors = NULL; + ListBase *effectors = nullptr; MVert *mvert; uint i = 0; int ret = 0; @@ -277,7 +277,8 @@ static int do_step_cloth( } } - effectors = BKE_effectors_create(depsgraph, ob, NULL, clmd->sim_parms->effector_weights, false); + effectors = BKE_effectors_create( + depsgraph, ob, nullptr, clmd->sim_parms->effector_weights, false); if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH) { cloth_update_verts(ob, clmd, result); @@ -368,7 +369,7 @@ void clothModifier_do(ClothModifierData *clmd, bool can_simulate = (framenr == clmd->clothObject->last_frame + 1) && !(cache->flag & PTCACHE_BAKED); - cache_result = BKE_ptcache_read(&pid, (float)framenr + scene->r.subframe, can_simulate); + cache_result = BKE_ptcache_read(&pid, float(framenr) + scene->r.subframe, can_simulate); if (cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED || (!can_simulate && cache_result == PTCACHE_READ_OLD)) { @@ -420,7 +421,7 @@ void clothModifier_do(ClothModifierData *clmd, void cloth_free_modifier(ClothModifierData *clmd) { - Cloth *cloth = NULL; + Cloth *cloth = nullptr; if (!clmd) { return; @@ -436,10 +437,10 @@ void cloth_free_modifier(ClothModifierData *clmd) cloth->mvert_num = 0; /* Free the springs. */ - if (cloth->springs != NULL) { + if (cloth->springs != nullptr) { LinkNode *search = cloth->springs; while (search) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); MEM_SAFE_FREE(spring->pa); MEM_SAFE_FREE(spring->pb); @@ -447,12 +448,12 @@ void cloth_free_modifier(ClothModifierData *clmd) MEM_freeN(spring); search = search->next; } - BLI_linklist_free(cloth->springs, NULL); + BLI_linklist_free(cloth->springs, nullptr); - cloth->springs = NULL; + cloth->springs = nullptr; } - cloth->springs = NULL; + cloth->springs = nullptr; cloth->numsprings = 0; /* free BVH collision tree */ @@ -475,7 +476,7 @@ void cloth_free_modifier(ClothModifierData *clmd) if (cloth->sew_edge_graph) { BLI_edgeset_free(cloth->sew_edge_graph); - cloth->sew_edge_graph = NULL; + cloth->sew_edge_graph = nullptr; } #if 0 @@ -484,13 +485,13 @@ void cloth_free_modifier(ClothModifierData *clmd) } #endif MEM_freeN(cloth); - clmd->clothObject = NULL; + clmd->clothObject = nullptr; } } void cloth_free_modifier_extern(ClothModifierData *clmd) { - Cloth *cloth = NULL; + Cloth *cloth = nullptr; if (G.debug & G_DEBUG_SIMDATA) { printf("cloth_free_modifier_extern\n"); } @@ -513,10 +514,10 @@ void cloth_free_modifier_extern(ClothModifierData *clmd) cloth->mvert_num = 0; /* Free the springs. */ - if (cloth->springs != NULL) { + if (cloth->springs != nullptr) { LinkNode *search = cloth->springs; while (search) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); MEM_SAFE_FREE(spring->pa); MEM_SAFE_FREE(spring->pb); @@ -524,12 +525,12 @@ void cloth_free_modifier_extern(ClothModifierData *clmd) MEM_freeN(spring); search = search->next; } - BLI_linklist_free(cloth->springs, NULL); + BLI_linklist_free(cloth->springs, nullptr); - cloth->springs = NULL; + cloth->springs = nullptr; } - cloth->springs = NULL; + cloth->springs = nullptr; cloth->numsprings = 0; /* free BVH collision tree */ @@ -552,7 +553,7 @@ void cloth_free_modifier_extern(ClothModifierData *clmd) if (cloth->sew_edge_graph) { BLI_edgeset_free(cloth->sew_edge_graph); - cloth->sew_edge_graph = NULL; + cloth->sew_edge_graph = nullptr; } #if 0 @@ -561,7 +562,7 @@ void cloth_free_modifier_extern(ClothModifierData *clmd) } #endif MEM_freeN(cloth); - clmd->clothObject = NULL; + clmd->clothObject = nullptr; } } @@ -614,6 +615,8 @@ static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh) ClothVertex *verts = clmd->clothObject->verts; + const blender::Span<MDeformVert> dverts = mesh->deform_verts(); + if (cloth_uses_vgroup(clmd)) { for (int i = 0; i < mvert_num; i++, verts++) { @@ -632,8 +635,8 @@ static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh) verts->flags &= ~(CLOTH_VERT_FLAG_PINNED | CLOTH_VERT_FLAG_NOSELFCOLL | CLOTH_VERT_FLAG_NOOBJCOLL); - const MDeformVert *dvert = CustomData_get(&mesh->vdata, i, CD_MDEFORMVERT); - if (dvert) { + if (!dverts.is_empty()) { + const MDeformVert *dvert = &dverts[i]; for (int j = 0; j < dvert->totweight; j++) { if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass - 1)) { verts->goal = dvert->dw[j].weight; @@ -713,12 +716,12 @@ static bool cloth_from_object( Object *ob, ClothModifierData *clmd, Mesh *mesh, float UNUSED(framenr), int first) { int i = 0; - ClothVertex *verts = NULL; - const float(*shapekey_rest)[3] = NULL; + ClothVertex *verts = nullptr; + const float(*shapekey_rest)[3] = nullptr; const float tnull[3] = {0, 0, 0}; /* If we have a clothObject, free it. */ - if (clmd->clothObject != NULL) { + if (clmd->clothObject != nullptr) { cloth_free_modifier(clmd); if (G.debug & G_DEBUG_SIMDATA) { printf("cloth_free_modifier cloth_from_object\n"); @@ -726,10 +729,10 @@ static bool cloth_from_object( } /* Allocate a new cloth object. */ - clmd->clothObject = MEM_callocN(sizeof(Cloth), "cloth"); + clmd->clothObject = MEM_cnew<Cloth>(__func__); if (clmd->clothObject) { clmd->clothObject->old_solver_type = 255; - clmd->clothObject->edgeset = NULL; + clmd->clothObject->edgeset = nullptr; } else { BKE_modifier_set_error(ob, &(clmd->modifier), "Out of memory on allocating clmd->clothObject"); @@ -744,14 +747,15 @@ static bool cloth_from_object( cloth_from_mesh(clmd, ob, mesh); /* create springs */ - clmd->clothObject->springs = NULL; + clmd->clothObject->springs = nullptr; clmd->clothObject->numsprings = -1; - clmd->clothObject->sew_edge_graph = NULL; + clmd->clothObject->sew_edge_graph = nullptr; if (clmd->sim_parms->shapekey_rest && !(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH)) { - shapekey_rest = CustomData_get_layer(&mesh->vdata, CD_CLOTH_ORCO); + shapekey_rest = static_cast<const float(*)[3]>( + CustomData_get_layer(&mesh->vdata, CD_CLOTH_ORCO)); } MVert *mvert = BKE_mesh_verts_for_write(mesh); @@ -830,9 +834,8 @@ static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, Mesh *mes /* Allocate our vertices. */ clmd->clothObject->mvert_num = mvert_num; - clmd->clothObject->verts = MEM_callocN(sizeof(ClothVertex) * clmd->clothObject->mvert_num, - "clothVertex"); - if (clmd->clothObject->verts == NULL) { + clmd->clothObject->verts = MEM_cnew_array<ClothVertex>(clmd->clothObject->mvert_num, __func__); + if (clmd->clothObject->verts == nullptr) { cloth_free_modifier(clmd); BKE_modifier_set_error( ob, &(clmd->modifier), "Out of memory on allocating clmd->clothObject->verts"); @@ -841,15 +844,16 @@ static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, Mesh *mes } /* save face information */ - if (clmd->hairdata == NULL) { + if (clmd->hairdata == nullptr) { clmd->clothObject->primitive_num = looptri_num; } else { clmd->clothObject->primitive_num = mesh->totedge; } - clmd->clothObject->tri = MEM_mallocN(sizeof(MVertTri) * looptri_num, "clothLoopTris"); - if (clmd->clothObject->tri == NULL) { + clmd->clothObject->tri = static_cast<MVertTri *>( + MEM_malloc_arrayN(looptri_num, sizeof(MVertTri), __func__)); + if (clmd->clothObject->tri == nullptr) { cloth_free_modifier(clmd); BKE_modifier_set_error( ob, &(clmd->modifier), "Out of memory on allocating clmd->clothObject->looptri"); @@ -863,7 +867,7 @@ static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, Mesh *mes /* Free the springs since they can't be correct if the vertices * changed. */ - if (clmd->clothObject->springs != NULL) { + if (clmd->clothObject->springs != nullptr) { MEM_freeN(clmd->clothObject->springs); } } @@ -888,7 +892,7 @@ static void cloth_free_edgelist(LinkNodePair *edgelist, uint mvert_num) { if (edgelist) { for (uint i = 0; i < mvert_num; i++) { - BLI_linklist_free(edgelist[i].list, NULL); + BLI_linklist_free(edgelist[i].list, nullptr); } MEM_freeN(edgelist); @@ -899,10 +903,10 @@ static void cloth_free_errorsprings(Cloth *cloth, LinkNodePair *edgelist, BendSpringRef *spring_ref) { - if (cloth->springs != NULL) { + if (cloth->springs != nullptr) { LinkNode *search = cloth->springs; while (search) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); MEM_SAFE_FREE(spring->pa); MEM_SAFE_FREE(spring->pb); @@ -910,9 +914,9 @@ static void cloth_free_errorsprings(Cloth *cloth, MEM_freeN(spring); search = search->next; } - BLI_linklist_free(cloth->springs, NULL); + BLI_linklist_free(cloth->springs, nullptr); - cloth->springs = NULL; + cloth->springs = nullptr; } cloth_free_edgelist(edgelist, cloth->mvert_num); @@ -921,7 +925,7 @@ static void cloth_free_errorsprings(Cloth *cloth, if (cloth->edgeset) { BLI_edgeset_free(cloth->edgeset); - cloth->edgeset = NULL; + cloth->edgeset = nullptr; } } @@ -965,7 +969,7 @@ static float cloth_spring_angle( static void cloth_hair_update_bending_targets(ClothModifierData *clmd) { Cloth *cloth = clmd->clothObject; - LinkNode *search = NULL; + LinkNode *search = nullptr; float hair_frame[3][3], dir_old[3], dir_new[3]; int prev_mn; /* to find hair chains */ @@ -983,7 +987,7 @@ static void cloth_hair_update_bending_targets(ClothModifierData *clmd) prev_mn = -1; for (search = cloth->springs; search; search = search->next) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); ClothHairData *hair_ij, *hair_kl; bool is_root = spring->kl != prev_mn; @@ -1023,7 +1027,7 @@ static void cloth_hair_update_bending_targets(ClothModifierData *clmd) static void cloth_hair_update_bending_rest_targets(ClothModifierData *clmd) { Cloth *cloth = clmd->clothObject; - LinkNode *search = NULL; + LinkNode *search = nullptr; float hair_frame[3][3], dir_old[3], dir_new[3]; int prev_mn; /* to find hair roots */ @@ -1041,7 +1045,7 @@ static void cloth_hair_update_bending_rest_targets(ClothModifierData *clmd) prev_mn = -1; for (search = cloth->springs; search; search = search->next) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); ClothHairData *hair_ij, *hair_kl; bool is_root = spring->kl != prev_mn; @@ -1079,11 +1083,11 @@ static void cloth_hair_update_bending_rest_targets(ClothModifierData *clmd) static void cloth_update_springs(ClothModifierData *clmd) { Cloth *cloth = clmd->clothObject; - LinkNode *search = NULL; + LinkNode *search = nullptr; search = cloth->springs; while (search) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); spring->lin_stiffness = 0.0f; @@ -1192,7 +1196,7 @@ static void cloth_update_spring_lengths(ClothModifierData *clmd, Mesh *mesh) } while (search) { - ClothSpring *spring = search->link; + ClothSpring *spring = static_cast<ClothSpring *>(search->link); if (spring->type != CLOTH_SPRING_TYPE_SEWING) { if (spring->type & (CLOTH_SPRING_TYPE_STRUCTURAL | CLOTH_SPRING_TYPE_SHEAR | @@ -1229,7 +1233,7 @@ static void cloth_update_spring_lengths(ClothModifierData *clmd, Mesh *mesh) for (i = 0; i < mvert_num; i++) { if (cloth->verts[i].spring_count > 0) { cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49f / - ((float)cloth->verts[i].spring_count); + float(cloth->verts[i].spring_count); } } } @@ -1316,12 +1320,12 @@ static bool cloth_add_shear_bend_spring(ClothModifierData *clmd, spring->la = k - j + 1; spring->lb = mpoly[i].totloop - k + j + 1; - spring->pa = MEM_mallocN(sizeof(*spring->pa) * spring->la, "spring poly"); + spring->pa = static_cast<int *>(MEM_mallocN(sizeof(*spring->pa) * spring->la, "spring poly")); if (!spring->pa) { return false; } - spring->pb = MEM_mallocN(sizeof(*spring->pb) * spring->lb, "spring poly"); + spring->pb = static_cast<int *>(MEM_mallocN(sizeof(*spring->pb) * spring->lb, "spring poly")); if (!spring->pb) { return false; } @@ -1357,7 +1361,7 @@ static bool cloth_add_shear_bend_spring(ClothModifierData *clmd, BLI_INLINE bool cloth_bend_set_poly_vert_array(int **poly, int len, const MLoop *mloop) { - int *p = MEM_mallocN(sizeof(int) * len, "spring poly"); + int *p = static_cast<int *>(MEM_mallocN(sizeof(int) * len, "spring poly")); if (!p) { return false; @@ -1417,7 +1421,7 @@ static bool find_internal_spring_target_vertex(BVHTreeFromMesh *treedata, uint vert_idx = -1; const MLoop *mloop = treedata->loop; - const MLoopTri *lt = NULL; + const MLoopTri *lt = nullptr; if (rayhit.index != -1 && rayhit.dist <= max_length) { if (check_normal && dot_v3v3(rayhit.no, no) < 0.0f) { @@ -1452,7 +1456,7 @@ static bool find_internal_spring_target_vertex(BVHTreeFromMesh *treedata, static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) { Cloth *cloth = clmd->clothObject; - ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL; + ClothSpring *spring = nullptr, *tspring = nullptr, *tspring2 = nullptr; uint struct_springs = 0, shear_springs = 0, bend_springs = 0, struct_springs_real = 0; uint mvert_num = (uint)mesh->totvert; uint numedges = (uint)mesh->totedge; @@ -1462,10 +1466,10 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) const MPoly *mpoly = BKE_mesh_polys(mesh); const MLoop *mloop = BKE_mesh_loops(mesh); int index2 = 0; /* our second vertex index */ - LinkNodePair *edgelist = NULL; - EdgeSet *edgeset = NULL; - LinkNode *search = NULL, *search2 = NULL; - BendSpringRef *spring_ref = NULL; + LinkNodePair *edgelist = nullptr; + EdgeSet *edgeset = nullptr; + LinkNode *search = nullptr, *search2 = nullptr; + BendSpringRef *spring_ref = nullptr; /* error handling */ if (numedges == 0) { @@ -1474,21 +1478,22 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) /* NOTE: handling ownership of springs and edgeset is quite sloppy * currently they are never initialized but assert just to be sure */ - BLI_assert(cloth->springs == NULL); - BLI_assert(cloth->edgeset == NULL); + BLI_assert(cloth->springs == nullptr); + BLI_assert(cloth->edgeset == nullptr); - cloth->springs = NULL; - cloth->edgeset = NULL; + cloth->springs = nullptr; + cloth->edgeset = nullptr; if (clmd->sim_parms->bending_model == CLOTH_BENDING_ANGULAR) { - spring_ref = MEM_callocN(sizeof(*spring_ref) * numedges, "temp bend spring reference"); + spring_ref = static_cast<BendSpringRef *>( + MEM_callocN(sizeof(*spring_ref) * numedges, __func__)); if (!spring_ref) { return false; } } else { - edgelist = MEM_callocN(sizeof(*edgelist) * mvert_num, "cloth_edgelist_alloc"); + edgelist = static_cast<LinkNodePair *>(MEM_callocN(sizeof(*edgelist) * mvert_num, __func__)); if (!edgelist) { return false; @@ -1498,9 +1503,9 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) bool use_internal_springs = (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_INTERNAL_SPRINGS); if (use_internal_springs && numpolys > 0) { - BVHTreeFromMesh treedata = {NULL}; + BVHTreeFromMesh treedata = {nullptr}; uint tar_v_idx; - Mesh *tmp_mesh = NULL; + Mesh *tmp_mesh = nullptr; RNG *rng; /* If using the rest shape key, it's necessary to make a copy of the mesh. */ @@ -1556,7 +1561,7 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) BLI_edgeset_free(existing_vert_pairs); free_bvhtree_from_mesh(&treedata); if (tmp_mesh) { - BKE_id_free(NULL, &tmp_mesh->id); + BKE_id_free(nullptr, &tmp_mesh->id); } return false; } @@ -1565,7 +1570,7 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) BLI_edgeset_free(existing_vert_pairs); free_bvhtree_from_mesh(&treedata); if (tmp_mesh) { - BKE_id_free(NULL, &tmp_mesh->id); + BKE_id_free(nullptr, &tmp_mesh->id); } BLI_rng_free(rng); } @@ -1577,7 +1582,7 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SEW) { /* cloth->sew_edge_graph should not exist before this */ - BLI_assert(cloth->sew_edge_graph == NULL); + BLI_assert(cloth->sew_edge_graph == nullptr); cloth->sew_edge_graph = BLI_edgeset_new("cloth_sewing_edges_graph"); } @@ -1730,8 +1735,8 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) spring->type &= ~CLOTH_SPRING_TYPE_BENDING; MEM_freeN(spring->pa); MEM_freeN(spring->pb); - spring->pa = NULL; - spring->pb = NULL; + spring->pa = nullptr; + spring->pb = nullptr; bend_springs--; } @@ -1748,11 +1753,11 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) break; } - tspring2 = search2->link; + tspring2 = static_cast<ClothSpring *>(search2->link); search = edgelist[tspring2->kl].list; while (search) { - tspring = search->link; + tspring = static_cast<ClothSpring *>(search->link); index2 = ((tspring->ij == tspring2->kl) ? (tspring->kl) : (tspring->ij)); /* Check for existing spring. */ @@ -1792,8 +1797,8 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) search = cloth->springs; search2 = search->next; while (search && search2) { - tspring = search->link; - tspring2 = search2->link; + tspring = static_cast<ClothSpring *>(search->link); + tspring2 = static_cast<ClothSpring *>(search2->link); if (tspring->ij == tspring2->kl) { spring = (ClothSpring *)MEM_callocN(sizeof(ClothSpring), "cloth spring"); @@ -1832,8 +1837,8 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) search = cloth->springs; search2 = search->next; while (search && search2) { - tspring = search->link; - tspring2 = search2->link; + tspring = static_cast<ClothSpring *>(search->link); + tspring2 = static_cast<ClothSpring *>(search2->link); if (tspring->ij == tspring2->kl) { spring = (ClothSpring *)MEM_callocN(sizeof(ClothSpring), "cloth spring"); diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c index 1d6092849cc..0ddd53ccb99 100644 --- a/source/blender/blenkernel/intern/context.c +++ b/source/blender/blenkernel/intern/context.c @@ -1495,7 +1495,7 @@ AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid) * require returning a non-owning pointer, which we don't have in the Asset Browser (yet). */ FileDirEntry *file = (FileDirEntry *)CTX_data_pointer_get_type(C, "active_file", &RNA_FileSelectEntry).data; - if (file && file->asset_data) { + if (file && file->asset) { *r_is_valid = true; return (AssetHandle){.file_data = file}; } diff --git a/source/blender/blenkernel/intern/cpp_types.cc b/source/blender/blenkernel/intern/cpp_types.cc new file mode 100644 index 00000000000..e6b33dd5b1a --- /dev/null +++ b/source/blender/blenkernel/intern/cpp_types.cc @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "BLI_cpp_type_make.hh" +#include "BLI_cpp_types_make.hh" + +#include "BKE_cpp_types.h" +#include "BKE_geometry_set.hh" +#include "BKE_instances.hh" + +#include "DNA_meshdata_types.h" + +#include "FN_init.h" + +struct Tex; +struct Image; +struct Material; + +BLI_CPP_TYPE_MAKE(GeometrySet, CPPTypeFlags::Printable); +BLI_CPP_TYPE_MAKE(blender::bke::InstanceReference, CPPTypeFlags::None) + +BLI_VECTOR_CPP_TYPE_MAKE(GeometrySet); + +BLI_CPP_TYPE_MAKE(Object *, CPPTypeFlags::BasicType) +BLI_CPP_TYPE_MAKE(Collection *, CPPTypeFlags::BasicType) +BLI_CPP_TYPE_MAKE(Tex *, CPPTypeFlags::BasicType) +BLI_CPP_TYPE_MAKE(Image *, CPPTypeFlags::BasicType) +BLI_CPP_TYPE_MAKE(Material *, CPPTypeFlags::BasicType) + +BLI_CPP_TYPE_MAKE(MStringProperty, CPPTypeFlags::None); + +void BKE_cpp_types_init() +{ + blender::register_cpp_types(); + FN_register_cpp_types(); + + BLI_CPP_TYPE_REGISTER(GeometrySet); + BLI_CPP_TYPE_REGISTER(blender::bke::InstanceReference); + + BLI_VECTOR_CPP_TYPE_REGISTER(GeometrySet); + + BLI_CPP_TYPE_REGISTER(Object *); + BLI_CPP_TYPE_REGISTER(Collection *); + BLI_CPP_TYPE_REGISTER(Tex *); + BLI_CPP_TYPE_REGISTER(Image *); + BLI_CPP_TYPE_REGISTER(Material *); + + BLI_CPP_TYPE_REGISTER(MStringProperty); +} diff --git a/source/blender/blenkernel/intern/curve_to_mesh_convert.cc b/source/blender/blenkernel/intern/curve_to_mesh_convert.cc index ecf3be9bd81..3a86068d8e8 100644 --- a/source/blender/blenkernel/intern/curve_to_mesh_convert.cc +++ b/source/blender/blenkernel/intern/curve_to_mesh_convert.cc @@ -71,7 +71,7 @@ static void fill_mesh_topology(const int vert_offset, MEdge &edge = edges[profile_edge_offset + i_ring]; edge.v1 = ring_vert_offset + i_profile; edge.v2 = next_ring_vert_offset + i_profile; - edge.flag = ME_EDGEDRAW | ME_EDGERENDER; + edge.flag = ME_EDGEDRAW; } } @@ -87,7 +87,7 @@ static void fill_mesh_topology(const int vert_offset, MEdge &edge = edges[ring_edge_offset + i_profile]; edge.v1 = ring_vert_offset + i_profile; edge.v2 = ring_vert_offset + i_next_profile; - edge.flag = ME_EDGEDRAW | ME_EDGERENDER; + edge.flag = ME_EDGEDRAW; } } diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc index 7c338480c71..43bdb8e7b8c 100644 --- a/source/blender/blenkernel/intern/curves_geometry.cc +++ b/source/blender/blenkernel/intern/curves_geometry.cc @@ -511,17 +511,7 @@ static void calculate_evaluated_offsets(const CurvesGeometry &curves, void CurvesGeometry::ensure_evaluated_offsets() const { - if (!this->runtime->offsets_cache_dirty) { - return; - } - - /* A double checked lock. */ - std::scoped_lock lock{this->runtime->offsets_cache_mutex}; - if (!this->runtime->offsets_cache_dirty) { - return; - } - - threading::isolate_task([&]() { + this->runtime->offsets_cache_mutex.ensure([&]() { this->runtime->evaluated_offsets_cache.resize(this->curves_num() + 1); if (this->has_curve_with_type(CURVE_TYPE_BEZIER)) { @@ -534,8 +524,6 @@ void CurvesGeometry::ensure_evaluated_offsets() const calculate_evaluated_offsets( *this, this->runtime->evaluated_offsets_cache, this->runtime->bezier_evaluated_offsets); }); - - this->runtime->offsets_cache_dirty = false; } Span<int> CurvesGeometry::evaluated_offsets() const @@ -569,17 +557,7 @@ Array<int> CurvesGeometry::point_to_curve_map() const void CurvesGeometry::ensure_nurbs_basis_cache() const { - if (!this->runtime->nurbs_basis_cache_dirty) { - return; - } - - /* A double checked lock. */ - std::scoped_lock lock{this->runtime->nurbs_basis_cache_mutex}; - if (!this->runtime->nurbs_basis_cache_dirty) { - return; - } - - threading::isolate_task([&]() { + this->runtime->nurbs_basis_cache_mutex.ensure([&]() { Vector<int64_t> nurbs_indices; const IndexMask nurbs_mask = this->indices_for_curve_type(CURVE_TYPE_NURBS, nurbs_indices); if (nurbs_mask.is_empty()) { @@ -619,23 +597,11 @@ void CurvesGeometry::ensure_nurbs_basis_cache() const } }); }); - - this->runtime->nurbs_basis_cache_dirty = false; } Span<float3> CurvesGeometry::evaluated_positions() const { - if (!this->runtime->position_cache_dirty) { - return this->runtime->evaluated_positions_span; - } - - /* A double checked lock. */ - std::scoped_lock lock{this->runtime->position_cache_mutex}; - if (!this->runtime->position_cache_dirty) { - return this->runtime->evaluated_positions_span; - } - - threading::isolate_task([&]() { + this->runtime->position_cache_mutex.ensure([&]() { if (this->is_single_type(CURVE_TYPE_POLY)) { this->runtime->evaluated_positions_span = this->positions(); this->runtime->evaluated_position_cache.clear_and_make_inline(); @@ -699,24 +665,12 @@ Span<float3> CurvesGeometry::evaluated_positions() const } }); }); - - this->runtime->position_cache_dirty = false; return this->runtime->evaluated_positions_span; } Span<float3> CurvesGeometry::evaluated_tangents() const { - if (!this->runtime->tangent_cache_dirty) { - return this->runtime->evaluated_tangent_cache; - } - - /* A double checked lock. */ - std::scoped_lock lock{this->runtime->tangent_cache_mutex}; - if (!this->runtime->tangent_cache_dirty) { - return this->runtime->evaluated_tangent_cache; - } - - threading::isolate_task([&]() { + this->runtime->tangent_cache_mutex.ensure([&]() { const Span<float3> evaluated_positions = this->evaluated_positions(); const VArray<bool> cyclic = this->cyclic(); @@ -732,9 +686,9 @@ Span<float3> CurvesGeometry::evaluated_tangents() const } }); - /* Correct the first and last tangents of non-cyclic Bezier curves so that they align with the - * inner handles. This is a separate loop to avoid the cost when Bezier type curves are not - * used. */ + /* Correct the first and last tangents of non-cyclic Bezier curves so that they align with + * the inner handles. This is a separate loop to avoid the cost when Bezier type curves are + * not used. */ Vector<int64_t> bezier_indices; const IndexMask bezier_mask = this->indices_for_curve_type(CURVE_TYPE_BEZIER, bezier_indices); if (!bezier_mask.is_empty()) { @@ -765,8 +719,6 @@ Span<float3> CurvesGeometry::evaluated_tangents() const }); } }); - - this->runtime->tangent_cache_dirty = false; return this->runtime->evaluated_tangent_cache; } @@ -781,17 +733,7 @@ static void rotate_directions_around_axes(MutableSpan<float3> directions, Span<float3> CurvesGeometry::evaluated_normals() const { - if (!this->runtime->normal_cache_dirty) { - return this->runtime->evaluated_normal_cache; - } - - /* A double checked lock. */ - std::scoped_lock lock{this->runtime->normal_cache_mutex}; - if (!this->runtime->normal_cache_dirty) { - return this->runtime->evaluated_normal_cache; - } - - threading::isolate_task([&]() { + this->runtime->normal_cache_mutex.ensure([&]() { const Span<float3> evaluated_tangents = this->evaluated_tangents(); const VArray<bool> cyclic = this->cyclic(); const VArray<int8_t> normal_mode = this->normal_mode(); @@ -842,8 +784,6 @@ Span<float3> CurvesGeometry::evaluated_normals() const } }); }); - - this->runtime->normal_cache_dirty = false; return this->runtime->evaluated_normal_cache; } @@ -851,8 +791,8 @@ void CurvesGeometry::interpolate_to_evaluated(const int curve_index, const GSpan src, GMutableSpan dst) const { - BLI_assert(!this->runtime->offsets_cache_dirty); - BLI_assert(!this->runtime->nurbs_basis_cache_dirty); + BLI_assert(this->runtime->offsets_cache_mutex.is_cached()); + BLI_assert(this->runtime->nurbs_basis_cache_mutex.is_cached()); const IndexRange points = this->points_for_curve(curve_index); BLI_assert(src.size() == points.size()); BLI_assert(dst.size() == this->evaluated_points_for_curve(curve_index).size()); @@ -881,8 +821,8 @@ void CurvesGeometry::interpolate_to_evaluated(const int curve_index, void CurvesGeometry::interpolate_to_evaluated(const GSpan src, GMutableSpan dst) const { - BLI_assert(!this->runtime->offsets_cache_dirty); - BLI_assert(!this->runtime->nurbs_basis_cache_dirty); + BLI_assert(this->runtime->offsets_cache_mutex.is_cached()); + BLI_assert(this->runtime->nurbs_basis_cache_mutex.is_cached()); const VArray<int8_t> types = this->curve_types(); const VArray<int> resolution = this->resolution(); const VArray<bool> cyclic = this->cyclic(); @@ -923,17 +863,7 @@ void CurvesGeometry::interpolate_to_evaluated(const GSpan src, GMutableSpan dst) void CurvesGeometry::ensure_evaluated_lengths() const { - if (!this->runtime->length_cache_dirty) { - return; - } - - /* A double checked lock. */ - std::scoped_lock lock{this->runtime->length_cache_mutex}; - if (!this->runtime->length_cache_dirty) { - return; - } - - threading::isolate_task([&]() { + this->runtime->length_cache_mutex.ensure([&]() { /* Use an extra length value for the final cyclic segment for a consistent size * (see comment on #evaluated_length_cache). */ const int total_num = this->evaluated_points_num() + this->curves_num(); @@ -954,8 +884,6 @@ void CurvesGeometry::ensure_evaluated_lengths() const } }); }); - - this->runtime->length_cache_dirty = false; } void CurvesGeometry::ensure_can_interpolate_to_evaluated() const @@ -986,23 +914,23 @@ void CurvesGeometry::resize(const int points_num, const int curves_num) void CurvesGeometry::tag_positions_changed() { - this->runtime->position_cache_dirty = true; - this->runtime->tangent_cache_dirty = true; - this->runtime->normal_cache_dirty = true; - this->runtime->length_cache_dirty = true; + this->runtime->position_cache_mutex.tag_dirty(); + this->runtime->tangent_cache_mutex.tag_dirty(); + this->runtime->normal_cache_mutex.tag_dirty(); + this->runtime->length_cache_mutex.tag_dirty(); } void CurvesGeometry::tag_topology_changed() { - this->runtime->position_cache_dirty = true; - this->runtime->tangent_cache_dirty = true; - this->runtime->normal_cache_dirty = true; - this->runtime->offsets_cache_dirty = true; - this->runtime->nurbs_basis_cache_dirty = true; - this->runtime->length_cache_dirty = true; + this->runtime->position_cache_mutex.tag_dirty(); + this->runtime->tangent_cache_mutex.tag_dirty(); + this->runtime->normal_cache_mutex.tag_dirty(); + this->runtime->offsets_cache_mutex.tag_dirty(); + this->runtime->nurbs_basis_cache_mutex.tag_dirty(); + this->runtime->length_cache_mutex.tag_dirty(); } void CurvesGeometry::tag_normals_changed() { - this->runtime->normal_cache_dirty = true; + this->runtime->normal_cache_mutex.tag_dirty(); } static void translate_positions(MutableSpan<float3> positions, const float3 &translation) diff --git a/source/blender/blenkernel/intern/curves_utils.cc b/source/blender/blenkernel/intern/curves_utils.cc index 2b13a7d3c8a..f5a69a995a3 100644 --- a/source/blender/blenkernel/intern/curves_utils.cc +++ b/source/blender/blenkernel/intern/curves_utils.cc @@ -128,18 +128,6 @@ IndexMask indices_for_type(const VArray<int8_t> &types, selection, 4096, r_indices, [&](const int index) { return types_span[index] == type; }); } -void indices_for_each_type(const VArray<int8_t> &types, - const std::array<int, CURVE_TYPES_NUM> &counts, - const IndexMask selection, - std::array<IndexMask, CURVE_TYPES_NUM> &r_type_masks, - std::array<Vector<int64_t>, CURVE_TYPES_NUM> &r_type_indices) -{ - for (const int64_t curve_type : IndexRange(CURVE_TYPES_NUM)) { - r_type_masks[curve_type] = indices_for_type( - types, counts, CurveType(curve_type), selection, r_type_indices[curve_type]); - } -} - void foreach_curve_by_type(const VArray<int8_t> &types, const std::array<int, CURVE_TYPES_NUM> &counts, const IndexMask selection, @@ -162,21 +150,4 @@ void foreach_curve_by_type(const VArray<int8_t> &types, call_if_not_empty(CURVE_TYPE_NURBS, nurbs_fn); } -void foreach_curve_by_type_mask(const std::array<IndexMask, CURVE_TYPES_NUM> &curve_type_mask, - FunctionRef<void(IndexMask)> catmull_rom_fn, - FunctionRef<void(IndexMask)> poly_fn, - FunctionRef<void(IndexMask)> bezier_fn, - FunctionRef<void(IndexMask)> nurbs_fn) -{ - auto call_if_not_empty = [&](const IndexMask curve_type_mask, FunctionRef<void(IndexMask)> fn) { - if (!curve_type_mask.is_empty()) { - fn(curve_type_mask); - } - }; - call_if_not_empty(curve_type_mask[0], catmull_rom_fn); - call_if_not_empty(curve_type_mask[1], poly_fn); - call_if_not_empty(curve_type_mask[2], bezier_fn); - call_if_not_empty(curve_type_mask[3], nurbs_fn); -} - } // namespace blender::bke::curves diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc index 15d92614c42..c8d7c4f2fdc 100644 --- a/source/blender/blenkernel/intern/customdata.cc +++ b/source/blender/blenkernel/intern/customdata.cc @@ -19,7 +19,6 @@ #include "BLI_bitmap.h" #include "BLI_color.hh" -#include "BLI_cpp_type_make.hh" #include "BLI_endian_switch.h" #include "BLI_index_range.hh" #include "BLI_math.h" @@ -3600,39 +3599,6 @@ const char *CustomData_get_layer_name(const CustomData *data, const int type, co /* BMesh functions */ -void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *ldata) -{ - int act; - - if (CustomData_has_layer(ldata, CD_MLOOPUV)) { - act = CustomData_get_active_layer(ldata, CD_MLOOPUV); - CustomData_set_layer_active(fdata, CD_MTFACE, act); - - act = CustomData_get_render_layer(ldata, CD_MLOOPUV); - CustomData_set_layer_render(fdata, CD_MTFACE, act); - - act = CustomData_get_clone_layer(ldata, CD_MLOOPUV); - CustomData_set_layer_clone(fdata, CD_MTFACE, act); - - act = CustomData_get_stencil_layer(ldata, CD_MLOOPUV); - CustomData_set_layer_stencil(fdata, CD_MTFACE, act); - } - - if (CustomData_has_layer(ldata, CD_PROP_BYTE_COLOR)) { - act = CustomData_get_active_layer(ldata, CD_PROP_BYTE_COLOR); - CustomData_set_layer_active(fdata, CD_MCOL, act); - - act = CustomData_get_render_layer(ldata, CD_PROP_BYTE_COLOR); - CustomData_set_layer_render(fdata, CD_MCOL, act); - - act = CustomData_get_clone_layer(ldata, CD_PROP_BYTE_COLOR); - CustomData_set_layer_clone(fdata, CD_MCOL, act); - - act = CustomData_get_stencil_layer(ldata, CD_PROP_BYTE_COLOR); - CustomData_set_layer_stencil(fdata, CD_MCOL, act); - } -} - void CustomData_bmesh_init_pool(CustomData *data, const int totelem, const char htype) { int chunksize; @@ -5440,5 +5406,3 @@ size_t CustomData_get_elem_size(CustomDataLayer *layer) { return LAYERTYPEINFO[layer->type].size; } - -BLI_CPP_TYPE_MAKE(MStringProperty, MStringProperty, CPPTypeFlags::None); diff --git a/source/blender/blenkernel/intern/data_transfer.c b/source/blender/blenkernel/intern/data_transfer.c index e6afca11b40..7b81f74206d 100644 --- a/source/blender/blenkernel/intern/data_transfer.c +++ b/source/blender/blenkernel/intern/data_transfer.c @@ -300,8 +300,8 @@ static void data_transfer_dtdata_type_preprocess(Mesh *me_src, use_split_nors_dst, split_angle_dst, NULL, - custom_nors_dst, - NULL); + NULL, + custom_nors_dst); } } } diff --git a/source/blender/blenkernel/intern/dynamicpaint.c b/source/blender/blenkernel/intern/dynamicpaint.c index b9cec17b6e0..e0ae3f42be6 100644 --- a/source/blender/blenkernel/intern/dynamicpaint.c +++ b/source/blender/blenkernel/intern/dynamicpaint.c @@ -2790,7 +2790,7 @@ static bool dynamicPaint_symmetrizeAdjData(PaintAdjData *ed, int active_points) int dynamicPaint_createUVSurface(Scene *scene, DynamicPaintSurface *surface, float *progress, - short *do_update) + bool *do_update) { /* Antialias jitter point relative coords */ const int aa_samples = (surface->flags & MOD_DPAINT_ANTIALIAS) ? 5 : 1; diff --git a/source/blender/blenkernel/intern/fcurve.c b/source/blender/blenkernel/intern/fcurve.c index beea3217126..3e772e37177 100644 --- a/source/blender/blenkernel/intern/fcurve.c +++ b/source/blender/blenkernel/intern/fcurve.c @@ -85,8 +85,6 @@ void BKE_fcurve_free(FCurve *fcu) void BKE_fcurves_free(ListBase *list) { - FCurve *fcu, *fcn; - /* Sanity check. */ if (list == NULL) { return; @@ -96,7 +94,8 @@ void BKE_fcurves_free(ListBase *list) * as we store reference to next, and freeing only touches the curve * it's given. */ - for (fcu = list->first; fcu; fcu = fcn) { + FCurve *fcn = NULL; + for (FCurve *fcu = list->first; fcu; fcu = fcn) { fcn = fcu->next; BKE_fcurve_free(fcu); } @@ -113,15 +112,13 @@ void BKE_fcurves_free(ListBase *list) FCurve *BKE_fcurve_copy(const FCurve *fcu) { - FCurve *fcu_d; - /* Sanity check. */ if (fcu == NULL) { return NULL; } /* Make a copy. */ - fcu_d = MEM_dupallocN(fcu); + FCurve *fcu_d = MEM_dupallocN(fcu); fcu_d->next = fcu_d->prev = NULL; fcu_d->grp = NULL; @@ -145,8 +142,6 @@ FCurve *BKE_fcurve_copy(const FCurve *fcu) void BKE_fcurves_copy(ListBase *dst, ListBase *src) { - FCurve *dfcu, *sfcu; - /* Sanity checks. */ if (ELEM(NULL, dst, src)) { return; @@ -156,8 +151,8 @@ void BKE_fcurves_copy(ListBase *dst, ListBase *src) BLI_listbase_clear(dst); /* Copy one-by-one. */ - for (sfcu = src->first; sfcu; sfcu = sfcu->next) { - dfcu = BKE_fcurve_copy(sfcu); + LISTBASE_FOREACH (FCurve *, sfcu, src) { + FCurve *dfcu = BKE_fcurve_copy(sfcu); BLI_addtail(dst, dfcu); } } @@ -203,12 +198,10 @@ FCurve *id_data_find_fcurve( { /* Anim vars */ AnimData *adt = BKE_animdata_from_id(id); - FCurve *fcu = NULL; /* Rna vars */ PointerRNA ptr; PropertyRNA *prop; - char *path; if (r_driven) { *r_driven = false; @@ -225,7 +218,7 @@ FCurve *id_data_find_fcurve( return NULL; } - path = RNA_path_from_ID_to_property(&ptr, prop); + char *path = RNA_path_from_ID_to_property(&ptr, prop); if (path == NULL) { return NULL; } @@ -233,7 +226,7 @@ FCurve *id_data_find_fcurve( /* FIXME: The way drivers are handled here (always NULL-ifying `fcu`) is very weird, this needs * to be re-checked I think?. */ bool is_driven = false; - fcu = BKE_animadata_fcurve_find_by_rna_path(adt, path, index, NULL, &is_driven); + FCurve *fcu = BKE_animadata_fcurve_find_by_rna_path(adt, path, index, NULL, &is_driven); if (is_driven) { if (r_driven != NULL) { *r_driven = is_driven; @@ -248,15 +241,13 @@ FCurve *id_data_find_fcurve( FCurve *BKE_fcurve_find(ListBase *list, const char rna_path[], const int array_index) { - FCurve *fcu; - /* Sanity checks. */ - if (ELEM(NULL, list, rna_path) || (array_index < 0)) { + if (ELEM(NULL, list, rna_path) || array_index < 0) { return NULL; } /* Check paths of curves, then array indices... */ - for (fcu = list->first; fcu; fcu = fcu->next) { + LISTBASE_FOREACH (FCurve *, fcu, list) { /* Check indices first, much cheaper than a string comparison. */ /* Simple string-compare (this assumes that they have the same root...) */ if (UNLIKELY(fcu->array_index == array_index && fcu->rna_path && @@ -276,15 +267,13 @@ FCurve *BKE_fcurve_find(ListBase *list, const char rna_path[], const int array_i FCurve *BKE_fcurve_iter_step(FCurve *fcu_iter, const char rna_path[]) { - FCurve *fcu; - /* Sanity checks. */ if (ELEM(NULL, fcu_iter, rna_path)) { return NULL; } /* Check paths of curves, then array indices... */ - for (fcu = fcu_iter; fcu; fcu = fcu->next) { + for (FCurve *fcu = fcu_iter; fcu; fcu = fcu->next) { /* Simple string-compare (this assumes that they have the same root...) */ if (fcu->rna_path && STREQ(fcu->rna_path, rna_path)) { return fcu; @@ -296,7 +285,6 @@ FCurve *BKE_fcurve_iter_step(FCurve *fcu_iter, const char rna_path[]) int BKE_fcurves_filter(ListBase *dst, ListBase *src, const char *dataPrefix, const char *dataName) { - FCurve *fcu; int matches = 0; /* Sanity checks. */ @@ -311,7 +299,7 @@ int BKE_fcurves_filter(ListBase *dst, ListBase *src, const char *dataPrefix, con char *quotedName = alloca(quotedName_size); /* Search each F-Curve one by one. */ - for (fcu = src->first; fcu; fcu = fcu->next) { + LISTBASE_FOREACH (FCurve *, fcu, src) { /* Check if quoted string matches the path. */ if (fcu->rna_path == NULL) { continue; @@ -487,16 +475,14 @@ static int BKE_fcurve_bezt_binarysearch_index_ex(const BezTriple array[], * - Keyframe to be added is to be added out of current bounds. * - Keyframe to be added would replace one of the existing ones on bounds. */ - if ((arraylen <= 0) || (array == NULL)) { + if (arraylen <= 0 || array == NULL) { CLOG_WARN(&LOG, "encountered invalid array"); return 0; } /* Check whether to add before/after/on. */ - float framenum; - /* 'First' Keyframe (when only one keyframe, this case is used) */ - framenum = array[0].vec[1][0]; + float framenum = array[0].vec[1][0]; if (IS_EQT(frame, framenum, threshold)) { *r_replace = true; return 0; @@ -522,9 +508,9 @@ static int BKE_fcurve_bezt_binarysearch_index_ex(const BezTriple array[], /* Compute and get midpoint. */ /* We calculate the midpoint this way to avoid int overflows... */ - int mid = start + ((end - start) / 2); + const int mid = start + ((end - start) / 2); - float midfra = array[mid].vec[1][0]; + const float midfra = array[mid].vec[1][0]; /* Check if exactly equal to midpoint. */ if (IS_EQT(frame, midfra, threshold)) { @@ -571,7 +557,7 @@ int BKE_fcurve_bezt_binarysearch_index(const BezTriple array[], /* ...................................... */ /* Helper for calc_fcurve_* functions -> find first and last BezTriple to be used. */ -static short get_fcurve_end_keyframes(FCurve *fcu, +static short get_fcurve_end_keyframes(const FCurve *fcu, BezTriple **first, BezTriple **last, const bool do_sel_only) @@ -589,10 +575,8 @@ static short get_fcurve_end_keyframes(FCurve *fcu, /* Only include selected items? */ if (do_sel_only) { - BezTriple *bezt; - /* Find first selected. */ - bezt = fcu->bezt; + BezTriple *bezt = fcu->bezt; for (int i = 0; i < fcu->totvert; bezt++, i++) { if (BEZT_ISSEL_ANY(bezt)) { *first = bezt; @@ -621,7 +605,7 @@ static short get_fcurve_end_keyframes(FCurve *fcu, return found; } -bool BKE_fcurve_calc_bounds(FCurve *fcu, +bool BKE_fcurve_calc_bounds(const FCurve *fcu, float *xmin, float *xmax, float *ymin, @@ -696,10 +680,9 @@ bool BKE_fcurve_calc_bounds(FCurve *fcu, /* Only loop over keyframes to find extents for values if needed. */ if (ymin || ymax) { - FPoint *fpt; - int i; + int i = 0; - for (fpt = fcu->fpt, i = 0; i < fcu->totvert; fpt++, i++) { + for (FPoint *fpt = fcu->fpt; i < fcu->totvert; fpt++, i++) { if (fpt->vec[1] < yminv) { yminv = fpt->vec[1]; } @@ -752,7 +735,7 @@ bool BKE_fcurve_calc_bounds(FCurve *fcu, } bool BKE_fcurve_calc_range( - FCurve *fcu, float *start, float *end, const bool do_sel_only, const bool do_min_length) + const FCurve *fcu, float *start, float *end, const bool do_sel_only, const bool do_min_length) { float min = 999999999.0f, max = -999999999.0f; bool foundvert = false; @@ -855,7 +838,7 @@ void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt) /* Gracefully handle out-of-bounds pointers. Ideally this would do a BLI_assert() as well, but * then the unit tests would break in debug mode. */ - ptrdiff_t offset = active_bezt - fcu->bezt; + const ptrdiff_t offset = active_bezt - fcu->bezt; if (offset < 0 || offset >= fcu->totvert) { fcu->active_keyframe_index = FCURVE_ACTIVE_KEYFRAME_NONE; return; @@ -872,8 +855,7 @@ int BKE_fcurve_active_keyframe_index(const FCurve *fcu) const int active_keyframe_index = fcu->active_keyframe_index; /* Array access boundary checks. */ - if ((fcu->bezt == NULL) || (active_keyframe_index >= fcu->totvert) || - (active_keyframe_index < 0)) { + if (fcu->bezt == NULL || active_keyframe_index >= fcu->totvert || active_keyframe_index < 0) { return FCURVE_ACTIVE_KEYFRAME_NONE; } @@ -900,7 +882,7 @@ void BKE_fcurve_keyframe_move_value_with_handles(struct BezTriple *keyframe, con /** \name Status Checks * \{ */ -bool BKE_fcurve_are_keyframes_usable(FCurve *fcu) +bool BKE_fcurve_are_keyframes_usable(const FCurve *fcu) { /* F-Curve must exist. */ if (fcu == NULL) { @@ -914,11 +896,9 @@ bool BKE_fcurve_are_keyframes_usable(FCurve *fcu) /* If it has modifiers, none of these should "drastically" alter the curve. */ if (fcu->modifiers.first) { - FModifier *fcm; - /* Check modifiers from last to first, as last will be more influential. */ /* TODO: optionally, only check modifier if it is the active one... (Joshua Leung 2010) */ - for (fcm = fcu->modifiers.last; fcm; fcm = fcm->prev) { + LISTBASE_FOREACH_BACKWARD (FModifier *, fcm, &fcu->modifiers) { /* Ignore if muted/disabled. */ if (fcm->flag & (FMODIFIER_FLAG_DISABLED | FMODIFIER_FLAG_MUTED)) { continue; @@ -960,12 +940,24 @@ bool BKE_fcurve_are_keyframes_usable(FCurve *fcu) return true; } -bool BKE_fcurve_is_protected(FCurve *fcu) +bool BKE_fcurve_is_protected(const FCurve *fcu) { - return ((fcu->flag & FCURVE_PROTECTED) || ((fcu->grp) && (fcu->grp->flag & AGRP_PROTECTED))); + return ((fcu->flag & FCURVE_PROTECTED) || (fcu->grp && (fcu->grp->flag & AGRP_PROTECTED))); } -bool BKE_fcurve_is_keyframable(FCurve *fcu) +bool BKE_fcurve_has_selected_control_points(const FCurve *fcu) +{ + int i; + BezTriple *bezt; + for (bezt = fcu->bezt, i = 0; i < fcu->totvert; ++i, ++bezt) { + if ((bezt->f2 & SELECT) != 0) { + return true; + } + } + return false; +} + +bool BKE_fcurve_is_keyframable(const FCurve *fcu) { /* F-Curve's keyframes must be "usable" (i.e. visible + have an effect on final result) */ if (BKE_fcurve_are_keyframes_usable(fcu) == 0) { @@ -1036,9 +1028,6 @@ float fcurve_samplingcb_evalcurve(FCurve *fcu, void *UNUSED(data), float evaltim void fcurve_store_samples(FCurve *fcu, void *data, int start, int end, FcuSampleFunc sample_cb) { - FPoint *fpt, *new_fpt; - int cfra; - /* Sanity checks. */ /* TODO: make these tests report errors using reports not CLOG's (Joshua Leung 2009) */ if (ELEM(NULL, fcu, sample_cb)) { @@ -1051,10 +1040,11 @@ void fcurve_store_samples(FCurve *fcu, void *data, int start, int end, FcuSample } /* Set up sample data. */ - fpt = new_fpt = MEM_callocN(sizeof(FPoint) * (end - start + 1), "FPoint Samples"); + FPoint *new_fpt; + FPoint *fpt = new_fpt = MEM_callocN(sizeof(FPoint) * (end - start + 1), "FPoint Samples"); /* Use the sampling callback at 1-frame intervals from start to end frames. */ - for (cfra = start; cfra <= end; cfra++, fpt++) { + for (int cfra = start; cfra <= end; cfra++, fpt++) { fpt->vec[0] = (float)cfra; fpt->vec[1] = sample_cb(fcu, data, (float)cfra); } @@ -1107,12 +1097,12 @@ void fcurve_samples_to_keyframes(FCurve *fcu, const int start, const int end) MEM_freeN(fcu->bezt); } - BezTriple *bezt; FPoint *fpt = fcu->fpt; int keyframes_to_insert = end - start; int sample_points = fcu->totvert; - bezt = fcu->bezt = MEM_callocN(sizeof(*fcu->bezt) * (size_t)keyframes_to_insert, __func__); + BezTriple *bezt = fcu->bezt = MEM_callocN(sizeof(*fcu->bezt) * (size_t)keyframes_to_insert, + __func__); fcu->totvert = keyframes_to_insert; /* Get first sample point to 'copy' as keyframe. */ @@ -1156,7 +1146,7 @@ void fcurve_samples_to_keyframes(FCurve *fcu, const int start, const int end) * that the handles are correct. */ -eFCU_Cycle_Type BKE_fcurve_get_cycle_type(FCurve *fcu) +eFCU_Cycle_Type BKE_fcurve_get_cycle_type(const FCurve *fcu) { FModifier *fcm = fcu->modifiers.first; @@ -1189,7 +1179,7 @@ eFCU_Cycle_Type BKE_fcurve_get_cycle_type(FCurve *fcu) return FCU_CYCLE_NONE; } -bool BKE_fcurve_is_cyclic(FCurve *fcu) +bool BKE_fcurve_is_cyclic(const FCurve *fcu) { return BKE_fcurve_get_cycle_type(fcu) != FCU_CYCLE_NONE; } @@ -1219,7 +1209,6 @@ static BezTriple *cycle_offset_triple( void BKE_fcurve_handles_recalc_ex(FCurve *fcu, eBezTriple_Flag handle_sel_flag) { - BezTriple *bezt, *prev, *next; int a = fcu->totvert; /* Error checking: @@ -1235,12 +1224,12 @@ void BKE_fcurve_handles_recalc_ex(FCurve *fcu, eBezTriple_Flag handle_sel_flag) BezTriple *first = &fcu->bezt[0], *last = &fcu->bezt[fcu->totvert - 1]; BezTriple tmp; - bool cycle = BKE_fcurve_is_cyclic(fcu) && BEZT_IS_AUTOH(first) && BEZT_IS_AUTOH(last); + const bool cycle = BKE_fcurve_is_cyclic(fcu) && BEZT_IS_AUTOH(first) && BEZT_IS_AUTOH(last); /* Get initial pointers. */ - bezt = fcu->bezt; - prev = cycle_offset_triple(cycle, &tmp, &fcu->bezt[fcu->totvert - 2], last, first); - next = (bezt + 1); + BezTriple *bezt = fcu->bezt; + BezTriple *prev = cycle_offset_triple(cycle, &tmp, &fcu->bezt[fcu->totvert - 2], last, first); + BezTriple *next = (bezt + 1); /* Loop over all beztriples, adjusting handles. */ while (a--) { @@ -1307,15 +1296,14 @@ void BKE_fcurve_handles_recalc(FCurve *fcu) void testhandles_fcurve(FCurve *fcu, eBezTriple_Flag sel_flag, const bool use_handle) { - BezTriple *bezt; - uint a; - /* Only beztriples have handles (bpoints don't though). */ if (ELEM(NULL, fcu, fcu->bezt)) { return; } /* Loop over beztriples. */ + BezTriple *bezt; + uint a; for (a = 0, bezt = fcu->bezt; a < fcu->totvert; a++, bezt++) { BKE_nurb_bezt_handle_test(bezt, sel_flag, use_handle, false); } @@ -1689,12 +1677,12 @@ void BKE_fcurve_delete_key(FCurve *fcu, int index) bool BKE_fcurve_delete_keys_selected(FCurve *fcu) { - bool changed = false; - if (fcu->bezt == NULL) { /* ignore baked curves */ return false; } + bool changed = false; + /* Delete selected BezTriples */ for (int i = 0; i < fcu->totvert; i++) { if (fcu->bezt[i].f2 & SELECT) { @@ -1730,9 +1718,9 @@ void BKE_fcurve_delete_keys_all(FCurve *fcu) static float fcurve_eval_keyframes_extrapolate( FCurve *fcu, BezTriple *bezts, float evaltime, int endpoint_offset, int direction_to_neighbor) { - BezTriple *endpoint_bezt = bezts + endpoint_offset; /* The first/last keyframe. */ - BezTriple *neighbor_bezt = endpoint_bezt + - direction_to_neighbor; /* The second (to last) keyframe. */ + const BezTriple *endpoint_bezt = bezts + endpoint_offset; /* The first/last keyframe. */ + const BezTriple *neighbor_bezt = endpoint_bezt + + direction_to_neighbor; /* The second (to last) keyframe. */ if (endpoint_bezt->ipo == BEZT_IPO_CONST || fcu->extend == FCURVE_EXTRAPOLATE_CONSTANT || (fcu->flag & FCURVE_DISCRETE_VALUES) != 0) { @@ -1747,7 +1735,7 @@ static float fcurve_eval_keyframes_extrapolate( return endpoint_bezt->vec[1][1]; } - float dx = endpoint_bezt->vec[1][0] - evaltime; + const float dx = endpoint_bezt->vec[1][0] - evaltime; float fac = neighbor_bezt->vec[1][0] - endpoint_bezt->vec[1][0]; /* Prevent division by zero. */ @@ -1761,8 +1749,8 @@ static float fcurve_eval_keyframes_extrapolate( /* Use the gradient of the second handle (later) of neighbor to calculate the gradient and thus * the value of the curve at evaluation time. */ - int handle = direction_to_neighbor > 0 ? 0 : 2; - float dx = endpoint_bezt->vec[1][0] - evaltime; + const int handle = direction_to_neighbor > 0 ? 0 : 2; + const float dx = endpoint_bezt->vec[1][0] - evaltime; float fac = endpoint_bezt->vec[1][0] - endpoint_bezt->vec[handle][0]; /* Prevent division by zero. */ @@ -1774,10 +1762,11 @@ static float fcurve_eval_keyframes_extrapolate( return endpoint_bezt->vec[1][1] - (fac * dx); } -static float fcurve_eval_keyframes_interpolate(FCurve *fcu, BezTriple *bezts, float evaltime) +static float fcurve_eval_keyframes_interpolate(const FCurve *fcu, + const BezTriple *bezts, + float evaltime) { const float eps = 1.e-8f; - BezTriple *bezt, *prevbezt; uint a; /* Evaltime occurs somewhere in the middle of the curve. */ @@ -1794,7 +1783,7 @@ static float fcurve_eval_keyframes_interpolate(FCurve *fcu, BezTriple *bezts, fl * This lower bound was established in b888a32eee8147b028464336ad2404d8155c64dd. */ a = BKE_fcurve_bezt_binarysearch_index_ex(bezts, evaltime, fcu->totvert, 0.0001, &exact); - bezt = bezts + a; + const BezTriple *bezt = bezts + a; if (exact) { /* Index returned must be interpreted differently when it sits on top of an existing keyframe @@ -1806,7 +1795,7 @@ static float fcurve_eval_keyframes_interpolate(FCurve *fcu, BezTriple *bezts, fl /* Index returned refers to the keyframe that the eval-time occurs *before* * - hence, that keyframe marks the start of the segment we're dealing with. */ - prevbezt = (a > 0) ? (bezt - 1) : bezt; + const BezTriple *prevbezt = (a > 0) ? (bezt - 1) : bezt; /* Use if the key is directly on the frame, in rare cases this is needed else we get 0.0 instead. * XXX: consult T39207 for examples of files where failure of these checks can cause issues. */ @@ -2042,7 +2031,7 @@ static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime return fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, 0, +1); } - BezTriple *lastbezt = bezts + fcu->totvert - 1; + const BezTriple *lastbezt = bezts + fcu->totvert - 1; if (lastbezt->vec[1][0] <= evaltime) { return fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, fcu->totvert - 1, -1); } @@ -2051,14 +2040,13 @@ static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime } /* Calculate F-Curve value for 'evaltime' using #FPoint samples. */ -static float fcurve_eval_samples(FCurve *fcu, FPoint *fpts, float evaltime) +static float fcurve_eval_samples(const FCurve *fcu, const FPoint *fpts, float evaltime) { - FPoint *prevfpt, *lastfpt, *fpt; float cvalue = 0.0f; /* Get pointers. */ - prevfpt = fpts; - lastfpt = prevfpt + fcu->totvert - 1; + const FPoint *prevfpt = fpts; + const FPoint *lastfpt = prevfpt + fcu->totvert - 1; /* Evaluation time at or past endpoints? */ if (prevfpt->vec[0] >= evaltime) { @@ -2073,10 +2061,10 @@ static float fcurve_eval_samples(FCurve *fcu, FPoint *fpts, float evaltime) float t = fabsf(evaltime - floorf(evaltime)); /* Find the one on the right frame (assume that these are spaced on 1-frame intervals). */ - fpt = prevfpt + ((int)evaltime - (int)prevfpt->vec[0]); + const FPoint *fpt = prevfpt + ((int)evaltime - (int)prevfpt->vec[0]); /* If not exactly on the frame, perform linear interpolation with the next one. */ - if ((t != 0.0f) && (t < 1.0f)) { + if (t != 0.0f && t < 1.0f) { cvalue = interpf(fpt->vec[1], (fpt + 1)->vec[1], 1.0f - t); } else { @@ -2098,15 +2086,14 @@ static float fcurve_eval_samples(FCurve *fcu, FPoint *fpts, float evaltime) */ static float evaluate_fcurve_ex(FCurve *fcu, float evaltime, float cvalue) { - float devaltime; - /* Evaluate modifiers which modify time to evaluate the base curve at. */ FModifiersStackStorage storage; storage.modifier_count = BLI_listbase_count(&fcu->modifiers); storage.size_per_modifier = evaluate_fmodifiers_storage_size_per_modifier(&fcu->modifiers); storage.buffer = alloca(storage.modifier_count * storage.size_per_modifier); - devaltime = evaluate_time_fmodifiers(&storage, &fcu->modifiers, fcu, cvalue, evaltime); + const float devaltime = evaluate_time_fmodifiers( + &storage, &fcu->modifiers, fcu, cvalue, evaltime); /* Evaluate curve-data * - 'devaltime' instead of 'evaltime', as this is the time that the last time-modifying @@ -2165,16 +2152,15 @@ float evaluate_fcurve_driver(PathResolvedRNA *anim_rna, /* Only do a default 1-1 mapping if it's unlikely that anything else will set a value... */ if (fcu->totvert == 0) { - FModifier *fcm; bool do_linear = true; /* Out-of-range F-Modifiers will block, as will those which just plain overwrite the values * XXX: additive is a bit more dicey; it really depends then if things are in range or not... */ - for (fcm = fcu->modifiers.first; fcm; fcm = fcm->next) { + LISTBASE_FOREACH (FModifier *, fcm, &fcu->modifiers) { /* If there are range-restrictions, we must definitely block T36950. */ if ((fcm->flag & FMODIFIER_FLAG_RANGERESTRICT) == 0 || - ((fcm->sfra <= evaltime) && (fcm->efra >= evaltime))) { + (fcm->sfra <= evaltime && fcm->efra >= evaltime)) { /* Within range: here it probably doesn't matter, * though we'd want to check on additive. */ } @@ -2195,9 +2181,9 @@ float evaluate_fcurve_driver(PathResolvedRNA *anim_rna, return evaluate_fcurve_ex(fcu, evaltime, cvalue); } -bool BKE_fcurve_is_empty(FCurve *fcu) +bool BKE_fcurve_is_empty(const FCurve *fcu) { - return (fcu->totvert == 0) && (fcu->driver == NULL) && + return fcu->totvert == 0 && fcu->driver == NULL && !list_has_suitable_fmodifier(&fcu->modifiers, 0, FMI_TYPE_GENERATE_CURVE); } diff --git a/source/blender/blenkernel/intern/fmodifier.c b/source/blender/blenkernel/intern/fmodifier.c index 551bab75d4b..46dc01edbff 100644 --- a/source/blender/blenkernel/intern/fmodifier.c +++ b/source/blender/blenkernel/intern/fmodifier.c @@ -1283,7 +1283,7 @@ void set_active_fmodifier(ListBase *modifiers, FModifier *fcm) } } -bool list_has_suitable_fmodifier(ListBase *modifiers, int mtype, short acttype) +bool list_has_suitable_fmodifier(const ListBase *modifiers, int mtype, short acttype) { FModifier *fcm; diff --git a/source/blender/blenkernel/intern/instances.cc b/source/blender/blenkernel/intern/instances.cc index 4675562e927..a2c344e918b 100644 --- a/source/blender/blenkernel/intern/instances.cc +++ b/source/blender/blenkernel/intern/instances.cc @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "BLI_array_utils.hh" -#include "BLI_cpp_type_make.hh" #include "BLI_rand.hh" #include "BLI_task.hh" @@ -9,8 +8,6 @@ #include "BKE_geometry_set.hh" #include "BKE_instances.hh" -BLI_CPP_TYPE_MAKE(InstanceReference, blender::bke::InstanceReference, CPPTypeFlags::None) - namespace blender::bke { InstanceReference::InstanceReference(GeometrySet geometry_set) diff --git a/source/blender/blenkernel/intern/key.c b/source/blender/blenkernel/intern/key.c index 2ba81c54872..53147c94f43 100644 --- a/source/blender/blenkernel/intern/key.c +++ b/source/blender/blenkernel/intern/key.c @@ -2296,8 +2296,8 @@ void BKE_keyblock_mesh_calc_normals(const KeyBlock *kb, (mesh->flag & ME_AUTOSMOOTH) != 0, mesh->smoothresh, NULL, - clnors, - NULL); + NULL, + clnors); } if (free_vert_normals) { diff --git a/source/blender/blenkernel/intern/lib_id_delete.c b/source/blender/blenkernel/intern/lib_id_delete.c index c7643c56212..92b34b9e1af 100644 --- a/source/blender/blenkernel/intern/lib_id_delete.c +++ b/source/blender/blenkernel/intern/lib_id_delete.c @@ -21,6 +21,7 @@ #include "BKE_anim_data.h" #include "BKE_asset.h" +#include "BKE_asset_library.h" #include "BKE_idprop.h" #include "BKE_idtype.h" #include "BKE_key.h" @@ -137,16 +138,16 @@ void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_i BKE_main_lock(bmain); } + struct IDRemapper *remapper = BKE_id_remapper_create(); + BKE_id_remapper_add(remapper, id, NULL); + if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) { if (free_notifier_reference_cb) { free_notifier_reference_cb(id); } if (remap_editor_id_reference_cb) { - struct IDRemapper *remapper = BKE_id_remapper_create(); - BKE_id_remapper_add(remapper, id, NULL); remap_editor_id_reference_cb(remapper); - BKE_id_remapper_free(remapper); } } @@ -158,6 +159,9 @@ void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_i } } + BKE_asset_library_remap_ids(remapper); + BKE_id_remapper_free(remapper); + BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0); if ((flag & LIB_ID_FREE_NO_MAIN) == 0) { diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc index 0018c217964..b8658139161 100644 --- a/source/blender/blenkernel/intern/mesh.cc +++ b/source/blender/blenkernel/intern/mesh.cc @@ -1608,38 +1608,6 @@ void BKE_mesh_tessface_clear(Mesh *mesh) mesh_tessface_clear_intern(mesh, true); } -void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh) -{ - if (UNLIKELY(mesh->cd_flag)) { - return; - } - - const Span<MVert> verts = mesh->verts(); - const Span<MEdge> edges = mesh->edges(); - - for (const MVert &vert : verts) { - if (vert.bweight_legacy != 0) { - mesh->cd_flag |= ME_CDFLAG_VERT_BWEIGHT; - break; - } - } - - for (const MEdge &edge : edges) { - if (edge.bweight_legacy != 0) { - mesh->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT; - if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) { - break; - } - } - if (edge.crease_legacy != 0) { - mesh->cd_flag |= ME_CDFLAG_EDGE_CREASE; - if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) { - break; - } - } - } -} - /* -------------------------------------------------------------------- */ /* MSelect functions (currently used in weight paint mode) */ @@ -1861,9 +1829,9 @@ void BKE_mesh_calc_normals_split_ex(Mesh *mesh, polys.size(), use_split_normals, split_angle, + nullptr, r_lnors_spacearr, - clnors, - nullptr); + clnors); BKE_mesh_assert_normals_dirty_or_calculated(mesh); } diff --git a/source/blender/blenkernel/intern/mesh_calc_edges.cc b/source/blender/blenkernel/intern/mesh_calc_edges.cc index 5e6babdf7b4..4a4c2ebcbb0 100644 --- a/source/blender/blenkernel/intern/mesh_calc_edges.cc +++ b/source/blender/blenkernel/intern/mesh_calc_edges.cc @@ -146,7 +146,7 @@ static void serialize_and_initialize_deduplicated_edges(MutableSpan<EdgeMap> edg /* Initialize new edge. */ new_edge.v1 = item.key.v_low; new_edge.v2 = item.key.v_high; - new_edge.flag = ME_EDGEDRAW | ME_EDGERENDER; + new_edge.flag = ME_EDGEDRAW; } item.value.index = new_edge_index; new_edge_index++; diff --git a/source/blender/blenkernel/intern/mesh_convert.cc b/source/blender/blenkernel/intern/mesh_convert.cc index 784d35a8d65..2255038a991 100644 --- a/source/blender/blenkernel/intern/mesh_convert.cc +++ b/source/blender/blenkernel/intern/mesh_convert.cc @@ -119,7 +119,7 @@ static void make_edges_mdata_extend(Mesh &mesh) BLI_edgehashIterator_getKey(ehi, &medge->v1, &medge->v2); BLI_edgehashIterator_setValue(ehi, POINTER_FROM_UINT(e_index)); - medge->flag = ME_EDGEDRAW | ME_EDGERENDER; + medge->flag = ME_EDGEDRAW; } BLI_edgehashIterator_free(ehi); @@ -223,7 +223,7 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba for (b = 1; b < dl->nr; b++) { medge->v1 = startvert + ofs + b - 1; medge->v2 = startvert + ofs + b; - medge->flag = ME_LOOSEEDGE | ME_EDGERENDER | ME_EDGEDRAW; + medge->flag = ME_LOOSEEDGE | ME_EDGEDRAW; medge++; } @@ -251,7 +251,7 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba else { medge->v2 = startvert + ofs + b + 1; } - medge->flag = ME_LOOSEEDGE | ME_EDGERENDER | ME_EDGEDRAW; + medge->flag = ME_LOOSEEDGE | ME_EDGEDRAW; medge++; } } @@ -706,7 +706,7 @@ void BKE_mesh_edges_set_draw_render(Mesh *mesh) { MutableSpan<MEdge> edges = mesh->edges_for_write(); for (int i = 0; i < mesh->totedge; i++) { - edges[i].flag |= ME_EDGEDRAW | ME_EDGERENDER; + edges[i].flag |= ME_EDGEDRAW; } } diff --git a/source/blender/blenkernel/intern/mesh_legacy_convert.cc b/source/blender/blenkernel/intern/mesh_legacy_convert.cc index df3057d9592..23426f8c087 100644 --- a/source/blender/blenkernel/intern/mesh_legacy_convert.cc +++ b/source/blender/blenkernel/intern/mesh_legacy_convert.cc @@ -30,6 +30,258 @@ #include "BKE_multires.h" /* -------------------------------------------------------------------- */ +/** \name Legacy Edge Calculation + * \{ */ + +struct EdgeSort { + uint v1, v2; + char is_loose, is_draw; +}; + +/* edges have to be added with lowest index first for sorting */ +static void to_edgesort(struct EdgeSort *ed, uint v1, uint v2, char is_loose, short is_draw) +{ + if (v1 < v2) { + ed->v1 = v1; + ed->v2 = v2; + } + else { + ed->v1 = v2; + ed->v2 = v1; + } + ed->is_loose = is_loose; + ed->is_draw = is_draw; +} + +static int vergedgesort(const void *v1, const void *v2) +{ + const struct EdgeSort *x1 = static_cast<const struct EdgeSort *>(v1); + const struct EdgeSort *x2 = static_cast<const struct EdgeSort *>(v2); + + if (x1->v1 > x2->v1) { + return 1; + } + if (x1->v1 < x2->v1) { + return -1; + } + if (x1->v2 > x2->v2) { + return 1; + } + if (x1->v2 < x2->v2) { + return -1; + } + + return 0; +} + +/* Create edges based on known verts and faces, + * this function is only used when loading very old blend files */ +static void mesh_calc_edges_mdata(const MVert * /*allvert*/, + const MFace *allface, + MLoop *allloop, + const MPoly *allpoly, + int /*totvert*/, + int totface, + int /*totloop*/, + int totpoly, + const bool use_old, + MEdge **r_medge, + int *r_totedge) +{ + const MPoly *mpoly; + const MFace *mface; + MEdge *medge, *med; + EdgeHash *hash; + struct EdgeSort *edsort, *ed; + int a, totedge = 0; + uint totedge_final = 0; + uint edge_index; + + /* we put all edges in array, sort them, and detect doubles that way */ + + for (a = totface, mface = allface; a > 0; a--, mface++) { + if (mface->v4) { + totedge += 4; + } + else if (mface->v3) { + totedge += 3; + } + else { + totedge += 1; + } + } + + if (totedge == 0) { + /* flag that mesh has edges */ + (*r_medge) = (MEdge *)MEM_callocN(0, __func__); + (*r_totedge) = 0; + return; + } + + ed = edsort = (EdgeSort *)MEM_mallocN(totedge * sizeof(struct EdgeSort), "EdgeSort"); + + for (a = totface, mface = allface; a > 0; a--, mface++) { + to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2); + if (mface->v4) { + to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3); + to_edgesort(ed++, mface->v3, mface->v4, 0, mface->edcode & ME_V3V4); + to_edgesort(ed++, mface->v4, mface->v1, 0, mface->edcode & ME_V4V1); + } + else if (mface->v3) { + to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3); + to_edgesort(ed++, mface->v3, mface->v1, 0, mface->edcode & ME_V3V1); + } + } + + qsort(edsort, totedge, sizeof(struct EdgeSort), vergedgesort); + + /* count final amount */ + for (a = totedge, ed = edsort; a > 1; a--, ed++) { + /* edge is unique when it differs from next edge, or is last */ + if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) { + totedge_final++; + } + } + totedge_final++; + + medge = (MEdge *)MEM_callocN(sizeof(MEdge) * totedge_final, __func__); + + for (a = totedge, med = medge, ed = edsort; a > 1; a--, ed++) { + /* edge is unique when it differs from next edge, or is last */ + if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) { + med->v1 = ed->v1; + med->v2 = ed->v2; + if (use_old == false || ed->is_draw) { + med->flag = ME_EDGEDRAW; + } + if (ed->is_loose) { + med->flag |= ME_LOOSEEDGE; + } + + /* order is swapped so extruding this edge as a surface won't flip face normals + * with cyclic curves */ + if (ed->v1 + 1 != ed->v2) { + SWAP(uint, med->v1, med->v2); + } + med++; + } + else { + /* Equal edge, merge the draw-flag. */ + (ed + 1)->is_draw |= ed->is_draw; + } + } + /* last edge */ + med->v1 = ed->v1; + med->v2 = ed->v2; + med->flag = ME_EDGEDRAW; + if (ed->is_loose) { + med->flag |= ME_LOOSEEDGE; + } + + MEM_freeN(edsort); + + /* set edge members of mloops */ + hash = BLI_edgehash_new_ex(__func__, totedge_final); + for (edge_index = 0, med = medge; edge_index < totedge_final; edge_index++, med++) { + BLI_edgehash_insert(hash, med->v1, med->v2, POINTER_FROM_UINT(edge_index)); + } + + mpoly = allpoly; + for (a = 0; a < totpoly; a++, mpoly++) { + MLoop *ml, *ml_next; + int i = mpoly->totloop; + + ml_next = allloop + mpoly->loopstart; /* first loop */ + ml = &ml_next[i - 1]; /* last loop */ + + while (i-- != 0) { + ml->e = POINTER_AS_UINT(BLI_edgehash_lookup(hash, ml->v, ml_next->v)); + ml = ml_next; + ml_next++; + } + } + + BLI_edgehash_free(hash, nullptr); + + *r_medge = medge; + *r_totedge = totedge_final; +} + +void BKE_mesh_calc_edges_legacy(Mesh *me, const bool use_old) +{ + using namespace blender; + MEdge *medge; + int totedge = 0; + const Span<MVert> verts = me->verts(); + const Span<MPoly> polys = me->polys(); + MutableSpan<MLoop> loops = me->loops_for_write(); + + mesh_calc_edges_mdata(verts.data(), + (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), + loops.data(), + polys.data(), + verts.size(), + me->totface, + loops.size(), + polys.size(), + use_old, + &medge, + &totedge); + + if (totedge == 0) { + /* flag that mesh has edges */ + me->totedge = 0; + return; + } + + medge = (MEdge *)CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge); + me->totedge = totedge; + + BKE_mesh_strip_loose_faces(me); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name CD Flag Initialization + * \{ */ + +void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh) +{ + using namespace blender; + if (UNLIKELY(mesh->cd_flag)) { + return; + } + + const Span<MVert> verts = mesh->verts(); + const Span<MEdge> edges = mesh->edges(); + + for (const MVert &vert : verts) { + if (vert.bweight_legacy != 0) { + mesh->cd_flag |= ME_CDFLAG_VERT_BWEIGHT; + break; + } + } + + for (const MEdge &edge : edges) { + if (edge.bweight_legacy != 0) { + mesh->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT; + if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) { + break; + } + } + if (edge.crease_legacy != 0) { + mesh->cd_flag |= ME_CDFLAG_EDGE_CREASE; + if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) { + break; + } + } + } +} + +/** \} */ + +/* -------------------------------------------------------------------- */ /** \name NGon Tessellation (NGon to MFace Conversion) * \{ */ @@ -278,6 +530,115 @@ static void convert_mfaces_to_mpolys(ID *id, #undef ME_FGON } +static void update_active_fdata_layers(CustomData *fdata, CustomData *ldata) +{ + int act; + + if (CustomData_has_layer(ldata, CD_MLOOPUV)) { + act = CustomData_get_active_layer(ldata, CD_MLOOPUV); + CustomData_set_layer_active(fdata, CD_MTFACE, act); + + act = CustomData_get_render_layer(ldata, CD_MLOOPUV); + CustomData_set_layer_render(fdata, CD_MTFACE, act); + + act = CustomData_get_clone_layer(ldata, CD_MLOOPUV); + CustomData_set_layer_clone(fdata, CD_MTFACE, act); + + act = CustomData_get_stencil_layer(ldata, CD_MLOOPUV); + CustomData_set_layer_stencil(fdata, CD_MTFACE, act); + } + + if (CustomData_has_layer(ldata, CD_PROP_BYTE_COLOR)) { + act = CustomData_get_active_layer(ldata, CD_PROP_BYTE_COLOR); + CustomData_set_layer_active(fdata, CD_MCOL, act); + + act = CustomData_get_render_layer(ldata, CD_PROP_BYTE_COLOR); + CustomData_set_layer_render(fdata, CD_MCOL, act); + + act = CustomData_get_clone_layer(ldata, CD_PROP_BYTE_COLOR); + CustomData_set_layer_clone(fdata, CD_MCOL, act); + + act = CustomData_get_stencil_layer(ldata, CD_PROP_BYTE_COLOR); + CustomData_set_layer_stencil(fdata, CD_MCOL, act); + } +} + +#ifndef NDEBUG +/** + * Debug check, used to assert when we expect layers to be in/out of sync. + * + * \param fallback: Use when there are no layers to handle, + * since callers may expect success or failure. + */ +static bool check_matching_legacy_layer_counts(CustomData *fdata, CustomData *ldata, bool fallback) +{ + int a_num = 0, b_num = 0; +# define LAYER_CMP(l_a, t_a, l_b, t_b) \ + ((a_num += CustomData_number_of_layers(l_a, t_a)) == \ + (b_num += CustomData_number_of_layers(l_b, t_b))) + + if (!LAYER_CMP(ldata, CD_MLOOPUV, fdata, CD_MTFACE)) { + return false; + } + if (!LAYER_CMP(ldata, CD_PROP_BYTE_COLOR, fdata, CD_MCOL)) { + return false; + } + if (!LAYER_CMP(ldata, CD_PREVIEW_MLOOPCOL, fdata, CD_PREVIEW_MCOL)) { + return false; + } + if (!LAYER_CMP(ldata, CD_ORIGSPACE_MLOOP, fdata, CD_ORIGSPACE)) { + return false; + } + if (!LAYER_CMP(ldata, CD_NORMAL, fdata, CD_TESSLOOPNORMAL)) { + return false; + } + if (!LAYER_CMP(ldata, CD_TANGENT, fdata, CD_TANGENT)) { + return false; + } + +# undef LAYER_CMP + + /* if no layers are on either CustomData's, + * then there was nothing to do... */ + return a_num ? true : fallback; +} +#endif + +static void add_mface_layers(CustomData *fdata, CustomData *ldata, int total) +{ + /* avoid accumulating extra layers */ + BLI_assert(!check_matching_legacy_layer_counts(fdata, ldata, false)); + + for (int i = 0; i < ldata->totlayer; i++) { + if (ldata->layers[i].type == CD_MLOOPUV) { + CustomData_add_layer_named( + fdata, CD_MTFACE, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); + } + if (ldata->layers[i].type == CD_PROP_BYTE_COLOR) { + CustomData_add_layer_named( + fdata, CD_MCOL, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); + } + else if (ldata->layers[i].type == CD_PREVIEW_MLOOPCOL) { + CustomData_add_layer_named( + fdata, CD_PREVIEW_MCOL, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); + } + else if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) { + CustomData_add_layer_named( + fdata, CD_ORIGSPACE, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); + } + else if (ldata->layers[i].type == CD_NORMAL) { + CustomData_add_layer_named( + fdata, CD_TESSLOOPNORMAL, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); + } + else if (ldata->layers[i].type == CD_TANGENT) { + CustomData_add_layer_named( + fdata, CD_TANGENT, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); + } + } + + update_active_fdata_layers(fdata, ldata); +} + static void mesh_ensure_tessellation_customdata(Mesh *me) { if (UNLIKELY((me->totface != 0) && (me->totpoly == 0))) { @@ -296,7 +657,7 @@ static void mesh_ensure_tessellation_customdata(Mesh *me) if (tottex_tessface != tottex_original || totcol_tessface != totcol_original) { BKE_mesh_tessface_clear(me); - BKE_mesh_add_mface_layers(&me->fdata, &me->ldata, me->totface); + add_mface_layers(&me->fdata, &me->ldata, me->totface); /* TODO: add some `--debug-mesh` option. */ if (G.debug & G_DEBUG) { @@ -784,7 +1145,7 @@ static int mesh_tessface_calc(CustomData *fdata, /* #CD_ORIGINDEX will contain an array of indices from tessellation-faces to the polygons * they are directly tessellated from. */ CustomData_add_layer(fdata, CD_ORIGINDEX, CD_ASSIGN, mface_to_poly_map, totface); - BKE_mesh_add_mface_layers(fdata, ldata, totface); + add_mface_layers(fdata, ldata, totface); /* NOTE: quad detection issue - fourth vertidx vs fourth loopidx: * Polygons take care of their loops ordering, hence not of their vertices ordering. @@ -840,82 +1201,6 @@ void BKE_mesh_tessface_ensure(struct Mesh *mesh) } } -#ifndef NDEBUG -/** - * Debug check, used to assert when we expect layers to be in/out of sync. - * - * \param fallback: Use when there are no layers to handle, - * since callers may expect success or failure. - */ -static bool check_matching_legacy_layer_counts(CustomData *fdata, CustomData *ldata, bool fallback) -{ - int a_num = 0, b_num = 0; -# define LAYER_CMP(l_a, t_a, l_b, t_b) \ - ((a_num += CustomData_number_of_layers(l_a, t_a)) == \ - (b_num += CustomData_number_of_layers(l_b, t_b))) - - if (!LAYER_CMP(ldata, CD_MLOOPUV, fdata, CD_MTFACE)) { - return false; - } - if (!LAYER_CMP(ldata, CD_PROP_BYTE_COLOR, fdata, CD_MCOL)) { - return false; - } - if (!LAYER_CMP(ldata, CD_PREVIEW_MLOOPCOL, fdata, CD_PREVIEW_MCOL)) { - return false; - } - if (!LAYER_CMP(ldata, CD_ORIGSPACE_MLOOP, fdata, CD_ORIGSPACE)) { - return false; - } - if (!LAYER_CMP(ldata, CD_NORMAL, fdata, CD_TESSLOOPNORMAL)) { - return false; - } - if (!LAYER_CMP(ldata, CD_TANGENT, fdata, CD_TANGENT)) { - return false; - } - -# undef LAYER_CMP - - /* if no layers are on either CustomData's, - * then there was nothing to do... */ - return a_num ? true : fallback; -} -#endif - -void BKE_mesh_add_mface_layers(CustomData *fdata, CustomData *ldata, int total) -{ - /* avoid accumulating extra layers */ - BLI_assert(!check_matching_legacy_layer_counts(fdata, ldata, false)); - - for (int i = 0; i < ldata->totlayer; i++) { - if (ldata->layers[i].type == CD_MLOOPUV) { - CustomData_add_layer_named( - fdata, CD_MTFACE, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); - } - if (ldata->layers[i].type == CD_PROP_BYTE_COLOR) { - CustomData_add_layer_named( - fdata, CD_MCOL, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); - } - else if (ldata->layers[i].type == CD_PREVIEW_MLOOPCOL) { - CustomData_add_layer_named( - fdata, CD_PREVIEW_MCOL, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); - } - else if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) { - CustomData_add_layer_named( - fdata, CD_ORIGSPACE, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); - } - else if (ldata->layers[i].type == CD_NORMAL) { - CustomData_add_layer_named( - fdata, CD_TESSLOOPNORMAL, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); - } - else if (ldata->layers[i].type == CD_TANGENT) { - CustomData_add_layer_named( - fdata, CD_TANGENT, CD_SET_DEFAULT, nullptr, total, ldata->layers[i].name); - } - } - - CustomData_bmesh_update_active_layers(fdata, ldata); -} - /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/blenkernel/intern/mesh_mirror.c b/source/blender/blenkernel/intern/mesh_mirror.c index ce3fc5d99c8..9f00d8860b8 100644 --- a/source/blender/blenkernel/intern/mesh_mirror.c +++ b/source/blender/blenkernel/intern/mesh_mirror.c @@ -418,9 +418,9 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd, totpoly, true, mesh->smoothresh, + NULL, &lnors_spacearr, - clnors, - NULL); + clnors); /* mirroring has to account for loops being reversed in polys in second half */ MPoly *result_polys = BKE_mesh_polys_for_write(result); diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc index ebb5a72d137..404357bda8d 100644 --- a/source/blender/blenkernel/intern/mesh_normals.cc +++ b/source/blender/blenkernel/intern/mesh_normals.cc @@ -33,18 +33,20 @@ #include "BKE_editmesh_cache.h" #include "BKE_global.h" #include "BKE_mesh.h" +#include "BKE_mesh_mapping.h" #include "atomic_ops.h" using blender::BitVector; +using blender::float3; using blender::MutableSpan; +using blender::short2; using blender::Span; -// #define DEBUG_TIME +#define DEBUG_TIME #ifdef DEBUG_TIME -# include "PIL_time.h" -# include "PIL_time_utildefines.h" +# include "BLI_timeit.hh" #endif /* -------------------------------------------------------------------- */ @@ -442,7 +444,7 @@ void BKE_mesh_ensure_normals_for_display(Mesh *mesh) BKE_mesh_poly_normals_ensure(mesh); break; case ME_WRAPPER_TYPE_BMESH: { - struct BMEditMesh *em = mesh->edit_mesh; + BMEditMesh *em = mesh->edit_mesh; EditMeshData *emd = mesh->runtime->edit_data; if (emd->vertexCos) { BKE_editmesh_cache_ensure_vert_normals(em, emd); @@ -456,12 +458,9 @@ void BKE_mesh_ensure_normals_for_display(Mesh *mesh) void BKE_mesh_calc_normals(Mesh *mesh) { #ifdef DEBUG_TIME - TIMEIT_START_AVERAGED(BKE_mesh_calc_normals); + SCOPED_TIMER_AVERAGED(__func__); #endif BKE_mesh_vertex_normals_ensure(mesh); -#ifdef DEBUG_TIME - TIMEIT_END_AVERAGED(BKE_mesh_calc_normals); -#endif } void BKE_mesh_calc_normals_looptri(const MVert *mverts, @@ -788,7 +787,7 @@ struct LoopSplitTaskData { /** We have to create those outside of tasks, since #MemArena is not thread-safe. */ MLoopNorSpace *lnor_space; - float (*lnor)[3]; + float3 *lnor; const MLoop *ml_curr; const MLoop *ml_prev; int ml_curr_index; @@ -809,22 +808,18 @@ struct LoopSplitTaskDataCommon { * Note we do not need to protect it, though, since two different tasks will *always* affect * different elements in the arrays. */ MLoopNorSpaceArray *lnors_spacearr; - float (*loopnors)[3]; - short (*clnors_data)[2]; + MutableSpan<float3> loopnors; + MutableSpan<short2> clnors_data; /* Read-only. */ - const MVert *mverts; - const MEdge *medges; - const MLoop *mloops; - const MPoly *mpolys; + Span<MVert> verts; + MutableSpan<MEdge> edges; + Span<MLoop> loops; + Span<MPoly> polys; int (*edge_to_loops)[2]; - int *loop_to_poly; - const float (*polynors)[3]; - const float (*vert_normals)[3]; - - int numEdges; - int numLoops; - int numPolys; + Span<int> loop_to_poly; + Span<float3> polynors; + Span<float3> vert_normals; }; #define INDEX_UNSET INT_MIN @@ -837,47 +832,38 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data, const float split_angle, const bool do_sharp_edges_tag) { - const MEdge *medges = data->medges; - const MLoop *mloops = data->mloops; + MutableSpan<MEdge> edges = data->edges; + const Span<MPoly> polys = data->polys; + const Span<MLoop> loops = data->loops; + const Span<int> loop_to_poly = data->loop_to_poly; - const MPoly *mpolys = data->mpolys; - - const int numEdges = data->numEdges; - const int numPolys = data->numPolys; - - float(*loopnors)[3] = data->loopnors; /* NOTE: loopnors may be nullptr here. */ - const float(*polynors)[3] = data->polynors; + MutableSpan<float3> loopnors = data->loopnors; /* NOTE: loopnors may be empty here. */ + const Span<float3> polynors = data->polynors; int(*edge_to_loops)[2] = data->edge_to_loops; - int *loop_to_poly = data->loop_to_poly; BitVector sharp_edges; if (do_sharp_edges_tag) { - sharp_edges.resize(numEdges, false); + sharp_edges.resize(edges.size(), false); } - const MPoly *mp; - int mp_index; - const float split_angle_cos = check_angle ? cosf(split_angle) : -1.0f; - for (mp = mpolys, mp_index = 0; mp_index < numPolys; mp++, mp_index++) { - const MLoop *ml_curr; + for (const int mp_index : polys.index_range()) { + const MPoly &poly = polys[mp_index]; int *e2l; - int ml_curr_index = mp->loopstart; - const int ml_last_index = (ml_curr_index + mp->totloop) - 1; + int ml_curr_index = poly.loopstart; + const int ml_last_index = (ml_curr_index + poly.totloop) - 1; - ml_curr = &mloops[ml_curr_index]; + const MLoop *ml_curr = &loops[ml_curr_index]; for (; ml_curr_index <= ml_last_index; ml_curr++, ml_curr_index++) { e2l = edge_to_loops[ml_curr->e]; - loop_to_poly[ml_curr_index] = mp_index; - /* Pre-populate all loop normals as if their verts were all-smooth, * this way we don't have to compute those later! */ - if (loopnors) { + if (!loopnors.is_empty()) { copy_v3_v3(loopnors[ml_curr_index], data->vert_normals[ml_curr->v]); } @@ -886,7 +872,7 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data, /* 'Empty' edge until now, set e2l[0] (and e2l[1] to INDEX_UNSET to tag it as unset). */ e2l[0] = ml_curr_index; /* We have to check this here too, else we might miss some flat faces!!! */ - e2l[1] = (mp->flag & ME_SMOOTH) ? INDEX_UNSET : INDEX_INVALID; + e2l[1] = (poly.flag & ME_SMOOTH) ? INDEX_UNSET : INDEX_INVALID; } else if (e2l[1] == INDEX_UNSET) { const bool is_angle_sharp = (check_angle && @@ -898,8 +884,8 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data, * or both poly have opposed (flipped) normals, i.e. both loops on the same edge share the * same vertex, or angle between both its polys' normals is above split_angle value. */ - if (!(mp->flag & ME_SMOOTH) || (medges[ml_curr->e].flag & ME_SHARP) || - ml_curr->v == mloops[e2l[0]].v || is_angle_sharp) { + if (!(poly.flag & ME_SMOOTH) || (edges[ml_curr->e].flag & ME_SHARP) || + ml_curr->v == loops[e2l[0]].v || is_angle_sharp) { /* NOTE: we are sure that loop != 0 here ;). */ e2l[1] = INDEX_INVALID; @@ -929,69 +915,64 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data, /* If requested, do actual tagging of edges as sharp in another loop. */ if (do_sharp_edges_tag) { - MEdge *me; - int me_index; - for (me = (MEdge *)medges, me_index = 0; me_index < numEdges; me++, me_index++) { - if (sharp_edges[me_index]) { - me->flag |= ME_SHARP; + for (const int i : edges.index_range()) { + if (sharp_edges[i]) { + edges[i].flag |= ME_SHARP; } } } } -void BKE_edges_sharp_from_angle_set(const struct MVert *mverts, - const int /*numVerts*/, - struct MEdge *medges, +void BKE_edges_sharp_from_angle_set(const MVert *mverts, + const int numVerts, + MEdge *medges, const int numEdges, - const struct MLoop *mloops, + const MLoop *mloops, const int numLoops, - const struct MPoly *mpolys, + const MPoly *mpolys, const float (*polynors)[3], const int numPolys, const float split_angle) { + using namespace blender; + using namespace blender::bke; if (split_angle >= float(M_PI)) { /* Nothing to do! */ return; } - /* Mapping edge -> loops. See BKE_mesh_normals_loop_split() for details. */ + /* Mapping edge -> loops. See #BKE_mesh_normals_loop_split for details. */ int(*edge_to_loops)[2] = (int(*)[2])MEM_calloc_arrayN( size_t(numEdges), sizeof(*edge_to_loops), __func__); /* Simple mapping from a loop to its polygon index. */ - int *loop_to_poly = (int *)MEM_malloc_arrayN(size_t(numLoops), sizeof(*loop_to_poly), __func__); + const Array<int> loop_to_poly = mesh_topology::build_loop_to_poly_map({mpolys, numPolys}, + numLoops); LoopSplitTaskDataCommon common_data = {}; - common_data.mverts = mverts; - common_data.medges = medges; - common_data.mloops = mloops; - common_data.mpolys = mpolys; + common_data.verts = {mverts, numVerts}; + common_data.edges = {medges, numEdges}; + common_data.polys = {mpolys, numPolys}; + common_data.loops = {mloops, numLoops}; common_data.edge_to_loops = edge_to_loops; common_data.loop_to_poly = loop_to_poly; - common_data.polynors = polynors; - common_data.numEdges = numEdges; - common_data.numPolys = numPolys; + common_data.polynors = {reinterpret_cast<const float3 *>(polynors), numPolys}; mesh_edges_sharp_tag(&common_data, true, split_angle, true); MEM_freeN(edge_to_loops); - MEM_freeN(loop_to_poly); } -void BKE_mesh_loop_manifold_fan_around_vert_next(const MLoop *mloops, - const MPoly *mpolys, - const int *loop_to_poly, - const int *e2lfan_curr, - const uint mv_pivot_index, - const MLoop **r_mlfan_curr, - int *r_mlfan_curr_index, - int *r_mlfan_vert_index, - int *r_mpfan_curr_index) +static void loop_manifold_fan_around_vert_next(const Span<MLoop> loops, + const Span<MPoly> polys, + const Span<int> loop_to_poly, + const int *e2lfan_curr, + const uint mv_pivot_index, + const MLoop **r_mlfan_curr, + int *r_mlfan_curr_index, + int *r_mlfan_vert_index, + int *r_mpfan_curr_index) { - const MLoop *mlfan_next; - const MPoly *mpfan_next; - /* WARNING: This is rather complex! * We have to find our next edge around the vertex (fan mode). * First we find the next loop, which is either previous or next to mlfan_curr_index, depending @@ -1005,38 +986,38 @@ void BKE_mesh_loop_manifold_fan_around_vert_next(const MLoop *mloops, BLI_assert(*r_mlfan_curr_index >= 0); BLI_assert(*r_mpfan_curr_index >= 0); - mlfan_next = &mloops[*r_mlfan_curr_index]; - mpfan_next = &mpolys[*r_mpfan_curr_index]; - if (((*r_mlfan_curr)->v == mlfan_next->v && (*r_mlfan_curr)->v == mv_pivot_index) || - ((*r_mlfan_curr)->v != mlfan_next->v && (*r_mlfan_curr)->v != mv_pivot_index)) { + const MLoop &mlfan_next = loops[*r_mlfan_curr_index]; + const MPoly &mpfan_next = polys[*r_mpfan_curr_index]; + if (((*r_mlfan_curr)->v == mlfan_next.v && (*r_mlfan_curr)->v == mv_pivot_index) || + ((*r_mlfan_curr)->v != mlfan_next.v && (*r_mlfan_curr)->v != mv_pivot_index)) { /* We need the previous loop, but current one is our vertex's loop. */ *r_mlfan_vert_index = *r_mlfan_curr_index; - if (--(*r_mlfan_curr_index) < mpfan_next->loopstart) { - *r_mlfan_curr_index = mpfan_next->loopstart + mpfan_next->totloop - 1; + if (--(*r_mlfan_curr_index) < mpfan_next.loopstart) { + *r_mlfan_curr_index = mpfan_next.loopstart + mpfan_next.totloop - 1; } } else { /* We need the next loop, which is also our vertex's loop. */ - if (++(*r_mlfan_curr_index) >= mpfan_next->loopstart + mpfan_next->totloop) { - *r_mlfan_curr_index = mpfan_next->loopstart; + if (++(*r_mlfan_curr_index) >= mpfan_next.loopstart + mpfan_next.totloop) { + *r_mlfan_curr_index = mpfan_next.loopstart; } *r_mlfan_vert_index = *r_mlfan_curr_index; } - *r_mlfan_curr = &mloops[*r_mlfan_curr_index]; + *r_mlfan_curr = &loops[*r_mlfan_curr_index]; /* And now we are back in sync, mlfan_curr_index is the index of `mlfan_curr`! Pff! */ } static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopSplitTaskData *data) { MLoopNorSpaceArray *lnors_spacearr = common_data->lnors_spacearr; - const short(*clnors_data)[2] = common_data->clnors_data; + const Span<short2> clnors_data = common_data->clnors_data; - const MVert *mverts = common_data->mverts; - const MEdge *medges = common_data->medges; - const float(*polynors)[3] = common_data->polynors; + const Span<MVert> verts = common_data->verts; + const Span<MEdge> edges = common_data->edges; + const Span<float3> polynors = common_data->polynors; MLoopNorSpace *lnor_space = data->lnor_space; - float(*lnor)[3] = data->lnor; + float3 *lnor = data->lnor; const MLoop *ml_curr = data->ml_curr; const MLoop *ml_prev = data->ml_prev; const int ml_curr_index = data->ml_curr_index; @@ -1064,13 +1045,13 @@ static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopS float vec_curr[3], vec_prev[3]; const uint mv_pivot_index = ml_curr->v; /* The vertex we are "fanning" around! */ - const MVert *mv_pivot = &mverts[mv_pivot_index]; - const MEdge *me_curr = &medges[ml_curr->e]; - const MVert *mv_2 = (me_curr->v1 == mv_pivot_index) ? &mverts[me_curr->v2] : - &mverts[me_curr->v1]; - const MEdge *me_prev = &medges[ml_prev->e]; - const MVert *mv_3 = (me_prev->v1 == mv_pivot_index) ? &mverts[me_prev->v2] : - &mverts[me_prev->v1]; + const MVert *mv_pivot = &verts[mv_pivot_index]; + const MEdge *me_curr = &edges[ml_curr->e]; + const MVert *mv_2 = (me_curr->v1 == mv_pivot_index) ? &verts[me_curr->v2] : + &verts[me_curr->v1]; + const MEdge *me_prev = &edges[ml_prev->e]; + const MVert *mv_3 = (me_prev->v1 == mv_pivot_index) ? &verts[me_prev->v2] : + &verts[me_prev->v1]; sub_v3_v3v3(vec_curr, mv_2->co, mv_pivot->co); normalize_v3(vec_curr); @@ -1081,7 +1062,7 @@ static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopS /* We know there is only one loop in this space, no need to create a link-list in this case. */ BKE_lnor_space_add_loop(lnors_spacearr, lnor_space, ml_curr_index, nullptr, true); - if (clnors_data) { + if (!clnors_data.is_empty()) { BKE_lnor_space_custom_data_to_normal(lnor_space, clnors_data[ml_curr_index], *lnor); } } @@ -1090,16 +1071,16 @@ static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopS static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSplitTaskData *data) { MLoopNorSpaceArray *lnors_spacearr = common_data->lnors_spacearr; - float(*loopnors)[3] = common_data->loopnors; - short(*clnors_data)[2] = common_data->clnors_data; + MutableSpan<float3> loopnors = common_data->loopnors; + MutableSpan<short2> clnors_data = common_data->clnors_data; - const MVert *mverts = common_data->mverts; - const MEdge *medges = common_data->medges; - const MLoop *mloops = common_data->mloops; - const MPoly *mpolys = common_data->mpolys; + const Span<MVert> verts = common_data->verts; + const Span<MEdge> edges = common_data->edges; + const Span<MPoly> polys = common_data->polys; + const Span<MLoop> loops = common_data->loops; const int(*edge_to_loops)[2] = common_data->edge_to_loops; - const int *loop_to_poly = common_data->loop_to_poly; - const float(*polynors)[3] = common_data->polynors; + const Span<int> loop_to_poly = common_data->loop_to_poly; + const Span<float3> polynors = common_data->polynors; MLoopNorSpace *lnor_space = data->lnor_space; #if 0 /* Not needed for 'fan' loops. */ @@ -1121,22 +1102,17 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli * number of sharp edges per vertex, I doubt the additional memory usage would be worth it, * especially as it should not be a common case in real-life meshes anyway). */ const uint mv_pivot_index = ml_curr->v; /* The vertex we are "fanning" around! */ - const MVert *mv_pivot = &mverts[mv_pivot_index]; + const MVert *mv_pivot = &verts[mv_pivot_index]; /* `ml_curr` would be mlfan_prev if we needed that one. */ - const MEdge *me_org = &medges[ml_curr->e]; + const MEdge *me_org = &edges[ml_curr->e]; - const int *e2lfan_curr; float vec_curr[3], vec_prev[3], vec_org[3]; - const MLoop *mlfan_curr; float lnor[3] = {0.0f, 0.0f, 0.0f}; - /* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex! - */ - int mlfan_curr_index, mlfan_vert_index, mpfan_curr_index; /* We validate clnors data on the fly - cheapest way to do! */ int clnors_avg[2] = {0, 0}; - short(*clnor_ref)[2] = nullptr; + short2 *clnor_ref = nullptr; int clnors_count = 0; bool clnors_invalid = false; @@ -1145,11 +1121,13 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli /* Temp clnors stack. */ BLI_SMALLSTACK_DECLARE(clnors, short *); - e2lfan_curr = e2l_prev; - mlfan_curr = ml_prev; - mlfan_curr_index = ml_prev_index; - mlfan_vert_index = ml_curr_index; - mpfan_curr_index = mp_index; + const int *e2lfan_curr = e2l_prev; + const MLoop *mlfan_curr = ml_prev; + /* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex! + */ + int mlfan_curr_index = ml_prev_index; + int mlfan_vert_index = ml_curr_index; + int mpfan_curr_index = mp_index; BLI_assert(mlfan_curr_index >= 0); BLI_assert(mlfan_vert_index >= 0); @@ -1157,7 +1135,7 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli /* Only need to compute previous edge's vector once, then we can just reuse old current one! */ { - const MVert *mv_2 = (me_org->v1 == mv_pivot_index) ? &mverts[me_org->v2] : &mverts[me_org->v1]; + const MVert *mv_2 = (me_org->v1 == mv_pivot_index) ? &verts[me_org->v2] : &verts[me_org->v1]; sub_v3_v3v3(vec_org, mv_2->co, mv_pivot->co); normalize_v3(vec_org); @@ -1171,15 +1149,15 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli // printf("FAN: vert %d, start edge %d\n", mv_pivot_index, ml_curr->e); while (true) { - const MEdge *me_curr = &medges[mlfan_curr->e]; + const MEdge *me_curr = &edges[mlfan_curr->e]; /* Compute edge vectors. * NOTE: We could pre-compute those into an array, in the first iteration, instead of computing * them twice (or more) here. However, time gained is not worth memory and time lost, * given the fact that this code should not be called that much in real-life meshes. */ { - const MVert *mv_2 = (me_curr->v1 == mv_pivot_index) ? &mverts[me_curr->v2] : - &mverts[me_curr->v1]; + const MVert *mv_2 = (me_curr->v1 == mv_pivot_index) ? &verts[me_curr->v2] : + &verts[me_curr->v1]; sub_v3_v3v3(vec_curr, mv_2->co, mv_pivot->co); normalize_v3(vec_curr); @@ -1194,9 +1172,9 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli /* Accumulate */ madd_v3_v3fl(lnor, polynors[mpfan_curr_index], fac); - if (clnors_data) { + if (!clnors_data.is_empty()) { /* Accumulate all clnors, if they are not all equal we have to fix that! */ - short(*clnor)[2] = &clnors_data[mlfan_vert_index]; + short2 *clnor = &clnors_data[mlfan_vert_index]; if (clnors_count) { clnors_invalid |= ((*clnor_ref)[0] != (*clnor)[0] || (*clnor_ref)[1] != (*clnor)[1]); } @@ -1233,15 +1211,15 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli copy_v3_v3(vec_prev, vec_curr); /* Find next loop of the smooth fan. */ - BKE_mesh_loop_manifold_fan_around_vert_next(mloops, - mpolys, - loop_to_poly, - e2lfan_curr, - mv_pivot_index, - &mlfan_curr, - &mlfan_curr_index, - &mlfan_vert_index, - &mpfan_curr_index); + loop_manifold_fan_around_vert_next(loops, + polys, + loop_to_poly, + e2lfan_curr, + mv_pivot_index, + &mlfan_curr, + &mlfan_curr_index, + &mlfan_vert_index, + &mpfan_curr_index); e2lfan_curr = edge_to_loops[mlfan_curr->e]; } @@ -1261,7 +1239,7 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli BKE_lnor_space_define(lnor_space, lnor, vec_org, vec_curr, edge_vectors); - if (clnors_data) { + if (!clnors_data.is_empty()) { if (clnors_invalid) { short *clnor; @@ -1325,10 +1303,6 @@ static void loop_split_worker(TaskPool *__restrict pool, void *taskdata) BLI_stack_new(sizeof(float[3]), __func__) : nullptr; -#ifdef DEBUG_TIME - TIMEIT_START_AVERAGED(loop_split_worker); -#endif - for (int i = 0; i < LOOP_SPLIT_TASK_BLOCK_SIZE; i++, data++) { /* A nullptr ml_curr is used to tag ended data! */ if (data->ml_curr == nullptr) { @@ -1341,10 +1315,6 @@ static void loop_split_worker(TaskPool *__restrict pool, void *taskdata) if (edge_vectors) { BLI_stack_free(edge_vectors); } - -#ifdef DEBUG_TIME - TIMEIT_END_AVERAGED(loop_split_worker); -#endif } /** @@ -1352,10 +1322,10 @@ static void loop_split_worker(TaskPool *__restrict pool, void *taskdata) * Needed because cyclic smooth fans have no obvious 'entry point', * and yet we need to walk them once, and only once. */ -static bool loop_split_generator_check_cyclic_smooth_fan(const MLoop *mloops, - const MPoly *mpolys, +static bool loop_split_generator_check_cyclic_smooth_fan(const Span<MLoop> mloops, + const Span<MPoly> mpolys, const int (*edge_to_loops)[2], - const int *loop_to_poly, + const Span<int> loop_to_poly, const int *e2l_prev, BitVector<> &skip_loops, const MLoop *ml_curr, @@ -1365,22 +1335,19 @@ static bool loop_split_generator_check_cyclic_smooth_fan(const MLoop *mloops, const int mp_curr_index) { const uint mv_pivot_index = ml_curr->v; /* The vertex we are "fanning" around! */ - const int *e2lfan_curr; - const MLoop *mlfan_curr; - /* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex! - */ - int mlfan_curr_index, mlfan_vert_index, mpfan_curr_index; - e2lfan_curr = e2l_prev; + const int *e2lfan_curr = e2l_prev; if (IS_EDGE_SHARP(e2lfan_curr)) { /* Sharp loop, so not a cyclic smooth fan. */ return false; } - mlfan_curr = ml_prev; - mlfan_curr_index = ml_prev_index; - mlfan_vert_index = ml_curr_index; - mpfan_curr_index = mp_curr_index; + /* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex! + */ + const MLoop *mlfan_curr = ml_prev; + int mlfan_curr_index = ml_prev_index; + int mlfan_vert_index = ml_curr_index; + int mpfan_curr_index = mp_curr_index; BLI_assert(mlfan_curr_index >= 0); BLI_assert(mlfan_vert_index >= 0); @@ -1391,15 +1358,15 @@ static bool loop_split_generator_check_cyclic_smooth_fan(const MLoop *mloops, while (true) { /* Find next loop of the smooth fan. */ - BKE_mesh_loop_manifold_fan_around_vert_next(mloops, - mpolys, - loop_to_poly, - e2lfan_curr, - mv_pivot_index, - &mlfan_curr, - &mlfan_curr_index, - &mlfan_vert_index, - &mpfan_curr_index); + loop_manifold_fan_around_vert_next(mloops, + mpolys, + loop_to_poly, + e2lfan_curr, + mv_pivot_index, + &mlfan_curr, + &mlfan_curr_index, + &mlfan_vert_index, + &mpfan_curr_index); e2lfan_curr = edge_to_loops[mlfan_curr->e]; @@ -1426,24 +1393,14 @@ static bool loop_split_generator_check_cyclic_smooth_fan(const MLoop *mloops, static void loop_split_generator(TaskPool *pool, LoopSplitTaskDataCommon *common_data) { MLoopNorSpaceArray *lnors_spacearr = common_data->lnors_spacearr; - float(*loopnors)[3] = common_data->loopnors; + MutableSpan<float3> loopnors = common_data->loopnors; - const MLoop *mloops = common_data->mloops; - const MPoly *mpolys = common_data->mpolys; - const int *loop_to_poly = common_data->loop_to_poly; + const Span<MLoop> loops = common_data->loops; + const Span<MPoly> polys = common_data->polys; + const Span<int> loop_to_poly = common_data->loop_to_poly; const int(*edge_to_loops)[2] = common_data->edge_to_loops; - const int numLoops = common_data->numLoops; - const int numPolys = common_data->numPolys; - const MPoly *mp; - int mp_index; - - const MLoop *ml_curr; - const MLoop *ml_prev; - int ml_curr_index; - int ml_prev_index; - - BitVector<> skip_loops(numLoops, false); + BitVector<> skip_loops(loops.size(), false); LoopSplitTaskData *data_buff = nullptr; int data_idx = 0; @@ -1453,7 +1410,7 @@ static void loop_split_generator(TaskPool *pool, LoopSplitTaskDataCommon *common BLI_Stack *edge_vectors = nullptr; #ifdef DEBUG_TIME - TIMEIT_START_AVERAGED(loop_split_generator); + SCOPED_TIMER_AVERAGED(__func__); #endif if (!pool) { @@ -1465,15 +1422,15 @@ static void loop_split_generator(TaskPool *pool, LoopSplitTaskDataCommon *common /* We now know edges that can be smoothed (with their vector, and their two loops), * and edges that will be hard! Now, time to generate the normals. */ - for (mp = mpolys, mp_index = 0; mp_index < numPolys; mp++, mp_index++) { - float(*lnors)[3]; - const int ml_last_index = (mp->loopstart + mp->totloop) - 1; - ml_curr_index = mp->loopstart; - ml_prev_index = ml_last_index; + for (const int mp_index : polys.index_range()) { + const MPoly &poly = polys[mp_index]; + const int ml_last_index = (poly.loopstart + poly.totloop) - 1; + int ml_curr_index = poly.loopstart; + int ml_prev_index = ml_last_index; - ml_curr = &mloops[ml_curr_index]; - ml_prev = &mloops[ml_prev_index]; - lnors = &loopnors[ml_curr_index]; + const MLoop *ml_curr = &loops[ml_curr_index]; + const MLoop *ml_prev = &loops[ml_prev_index]; + float3 *lnors = &loopnors[ml_curr_index]; for (; ml_curr_index <= ml_last_index; ml_curr++, ml_curr_index++, lnors++) { const int *e2l_curr = edge_to_loops[ml_curr->e]; @@ -1499,8 +1456,8 @@ static void loop_split_generator(TaskPool *pool, LoopSplitTaskDataCommon *common * complexity, #loop_manifold_fan_around_vert_next() is quite cheap in term of CPU cycles, * so really think it's not worth it. */ if (!IS_EDGE_SHARP(e2l_curr) && (skip_loops[ml_curr_index] || - !loop_split_generator_check_cyclic_smooth_fan(mloops, - mpolys, + !loop_split_generator_check_cyclic_smooth_fan(loops, + polys, edge_to_loops, loop_to_poly, e2l_prev, @@ -1593,15 +1550,11 @@ static void loop_split_generator(TaskPool *pool, LoopSplitTaskDataCommon *common if (edge_vectors) { BLI_stack_free(edge_vectors); } - -#ifdef DEBUG_TIME - TIMEIT_END_AVERAGED(loop_split_generator); -#endif } void BKE_mesh_normals_loop_split(const MVert *mverts, const float (*vert_normals)[3], - const int /*numVerts*/, + const int numVerts, const MEdge *medges, const int numEdges, const MLoop *mloops, @@ -1612,10 +1565,12 @@ void BKE_mesh_normals_loop_split(const MVert *mverts, const int numPolys, const bool use_split_normals, const float split_angle, + const int *loop_to_poly_map, MLoopNorSpaceArray *r_lnors_spacearr, - short (*clnors_data)[2], - int *r_loop_to_poly) + short (*clnors_data)[2]) { + using namespace blender; + using namespace blender::bke; /* For now this is not supported. * If we do not use split normals, we do not generate anything fancy! */ BLI_assert(use_split_normals || !(r_lnors_spacearr)); @@ -1636,9 +1591,6 @@ void BKE_mesh_normals_loop_split(const MVert *mverts, const bool is_poly_flat = ((mp->flag & ME_SMOOTH) == 0); for (; ml_index < ml_index_end; ml_index++) { - if (r_loop_to_poly) { - r_loop_to_poly[ml_index] = mp_index; - } if (is_poly_flat) { copy_v3_v3(r_loopnors[ml_index], polynors[mp_index]); } @@ -1668,9 +1620,15 @@ void BKE_mesh_normals_loop_split(const MVert *mverts, size_t(numEdges), sizeof(*edge_to_loops), __func__); /* Simple mapping from a loop to its polygon index. */ - int *loop_to_poly = r_loop_to_poly ? r_loop_to_poly : - (int *)MEM_malloc_arrayN( - size_t(numLoops), sizeof(*loop_to_poly), __func__); + Span<int> loop_to_poly; + Array<int> local_loop_to_poly_map; + if (loop_to_poly_map) { + loop_to_poly = {loop_to_poly_map, numLoops}; + } + else { + local_loop_to_poly_map = mesh_topology::build_loop_to_poly_map({mpolys, numPolys}, numLoops); + loop_to_poly = local_loop_to_poly_map; + } /* When using custom loop normals, disable the angle feature! */ const bool check_angle = (split_angle < float(M_PI)) && (clnors_data == nullptr); @@ -1678,7 +1636,7 @@ void BKE_mesh_normals_loop_split(const MVert *mverts, MLoopNorSpaceArray _lnors_spacearr = {nullptr}; #ifdef DEBUG_TIME - TIMEIT_START_AVERAGED(BKE_mesh_normals_loop_split); + SCOPED_TIMER_AVERAGED(__func__); #endif if (!r_lnors_spacearr && clnors_data) { @@ -1692,19 +1650,16 @@ void BKE_mesh_normals_loop_split(const MVert *mverts, /* Init data common to all tasks. */ LoopSplitTaskDataCommon common_data; common_data.lnors_spacearr = r_lnors_spacearr; - common_data.loopnors = r_loopnors; - common_data.clnors_data = clnors_data; - common_data.mverts = mverts; - common_data.medges = medges; - common_data.mloops = mloops; - common_data.mpolys = mpolys; + common_data.loopnors = {reinterpret_cast<float3 *>(r_loopnors), numLoops}; + common_data.clnors_data = {reinterpret_cast<short2 *>(clnors_data), clnors_data ? numLoops : 0}; + common_data.verts = {mverts, numVerts}; + common_data.edges = {const_cast<MEdge *>(medges), numEdges}; + common_data.polys = {mpolys, numPolys}; + common_data.loops = {mloops, numLoops}; common_data.edge_to_loops = edge_to_loops; common_data.loop_to_poly = loop_to_poly; - common_data.polynors = polynors; - common_data.vert_normals = vert_normals; - common_data.numEdges = numEdges; - common_data.numLoops = numLoops; - common_data.numPolys = numPolys; + common_data.polynors = {reinterpret_cast<const float3 *>(polynors), numPolys}; + common_data.vert_normals = {reinterpret_cast<const float3 *>(vert_normals), numVerts}; /* This first loop check which edges are actually smooth, and compute edge vectors. */ mesh_edges_sharp_tag(&common_data, check_angle, split_angle, false); @@ -1724,19 +1679,12 @@ void BKE_mesh_normals_loop_split(const MVert *mverts, } MEM_freeN(edge_to_loops); - if (!r_loop_to_poly) { - MEM_freeN(loop_to_poly); - } if (r_lnors_spacearr) { if (r_lnors_spacearr == &_lnors_spacearr) { BKE_lnor_spacearr_free(r_lnors_spacearr); } } - -#ifdef DEBUG_TIME - TIMEIT_END_AVERAGED(BKE_mesh_normals_loop_split); -#endif } #undef INDEX_UNSET @@ -1766,6 +1714,8 @@ static void mesh_normals_loop_custom_set(const MVert *mverts, short (*r_clnors_data)[2], const bool use_vertices) { + using namespace blender; + using namespace blender::bke; /* We *may* make that poor #BKE_mesh_normals_loop_split() even more complex by making it handling * that feature too, would probably be more efficient in absolute. * However, this function *is not* performance-critical, since it is mostly expected to be called @@ -1775,7 +1725,8 @@ static void mesh_normals_loop_custom_set(const MVert *mverts, MLoopNorSpaceArray lnors_spacearr = {nullptr}; BitVector<> done_loops(numLoops, false); float(*lnors)[3] = (float(*)[3])MEM_calloc_arrayN(size_t(numLoops), sizeof(*lnors), __func__); - int *loop_to_poly = (int *)MEM_malloc_arrayN(size_t(numLoops), sizeof(int), __func__); + const Array<int> loop_to_poly = mesh_topology::build_loop_to_poly_map({mpolys, numPolys}, + numLoops); /* In this case we always consider split nors as ON, * and do not want to use angle to define smooth fans! */ const bool use_split_normals = true; @@ -1797,9 +1748,9 @@ static void mesh_normals_loop_custom_set(const MVert *mverts, numPolys, use_split_normals, split_angle, + loop_to_poly.data(), &lnors_spacearr, - nullptr, - loop_to_poly); + nullptr); /* Set all given zero vectors to their default value. */ if (use_vertices) { @@ -1920,9 +1871,9 @@ static void mesh_normals_loop_custom_set(const MVert *mverts, numPolys, use_split_normals, split_angle, + loop_to_poly.data(), &lnors_spacearr, - nullptr, - loop_to_poly); + nullptr); } else { done_loops.fill(true); @@ -1984,7 +1935,6 @@ static void mesh_normals_loop_custom_set(const MVert *mverts, } MEM_freeN(lnors); - MEM_freeN(loop_to_poly); BKE_lnor_spacearr_free(&lnors_spacearr); } diff --git a/source/blender/blenkernel/intern/mesh_remap.c b/source/blender/blenkernel/intern/mesh_remap.c index 90798ea593d..d96cd54e198 100644 --- a/source/blender/blenkernel/intern/mesh_remap.c +++ b/source/blender/blenkernel/intern/mesh_remap.c @@ -1372,8 +1372,8 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode, use_split_nors_dst, split_angle_dst, NULL, - custom_nors_dst, - NULL); + NULL, + custom_nors_dst); } } if (need_pnors_src || need_lnors_src) { diff --git a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc index f38efd2cf65..0887e26148a 100644 --- a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc +++ b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc @@ -426,23 +426,18 @@ void BKE_remesh_reproject_vertex_paint(Mesh *target, const Mesh *source) else { /* Lazily init vertex -> loop maps. */ if (!source_lmap) { - const MPoly *source_polys = (MPoly *)CustomData_get_layer(&source->pdata, CD_MPOLY); - const MLoop *source_loops = (MLoop *)CustomData_get_layer(&source->ldata, CD_MLOOP); - const MPoly *target_polys = (MPoly *)CustomData_get_layer(&target->pdata, CD_MPOLY); - const MLoop *target_loops = (MLoop *)CustomData_get_layer(&target->ldata, CD_MLOOP); - BKE_mesh_vert_loop_map_create(&source_lmap, &source_lmap_mem, - source_polys, - source_loops, + source->polys().data(), + source->loops().data(), source->totvert, source->totpoly, source->totloop); BKE_mesh_vert_loop_map_create(&target_lmap, &target_lmap_mem, - target_polys, - target_loops, + target->polys().data(), + target->loops().data(), target->totvert, target->totpoly, target->totloop); diff --git a/source/blender/blenkernel/intern/mesh_validate.cc b/source/blender/blenkernel/intern/mesh_validate.cc index 9b140171912..9cd3b6e9e9e 100644 --- a/source/blender/blenkernel/intern/mesh_validate.cc +++ b/source/blender/blenkernel/intern/mesh_validate.cc @@ -1315,216 +1315,6 @@ void BKE_mesh_strip_loose_edges(Mesh *me) /** \name Mesh Edge Calculation * \{ */ -/* make edges in a Mesh, for outside of editmode */ - -struct EdgeSort { - uint v1, v2; - char is_loose, is_draw; -}; - -/* edges have to be added with lowest index first for sorting */ -static void to_edgesort(struct EdgeSort *ed, uint v1, uint v2, char is_loose, short is_draw) -{ - if (v1 < v2) { - ed->v1 = v1; - ed->v2 = v2; - } - else { - ed->v1 = v2; - ed->v2 = v1; - } - ed->is_loose = is_loose; - ed->is_draw = is_draw; -} - -static int vergedgesort(const void *v1, const void *v2) -{ - const struct EdgeSort *x1 = static_cast<const struct EdgeSort *>(v1); - const struct EdgeSort *x2 = static_cast<const struct EdgeSort *>(v2); - - if (x1->v1 > x2->v1) { - return 1; - } - if (x1->v1 < x2->v1) { - return -1; - } - if (x1->v2 > x2->v2) { - return 1; - } - if (x1->v2 < x2->v2) { - return -1; - } - - return 0; -} - -/* Create edges based on known verts and faces, - * this function is only used when loading very old blend files */ - -static void mesh_calc_edges_mdata(const MVert * /*allvert*/, - const MFace *allface, - MLoop *allloop, - const MPoly *allpoly, - int /*totvert*/, - int totface, - int /*totloop*/, - int totpoly, - const bool use_old, - MEdge **r_medge, - int *r_totedge) -{ - const MPoly *mpoly; - const MFace *mface; - MEdge *medge, *med; - EdgeHash *hash; - struct EdgeSort *edsort, *ed; - int a, totedge = 0; - uint totedge_final = 0; - uint edge_index; - - /* we put all edges in array, sort them, and detect doubles that way */ - - for (a = totface, mface = allface; a > 0; a--, mface++) { - if (mface->v4) { - totedge += 4; - } - else if (mface->v3) { - totedge += 3; - } - else { - totedge += 1; - } - } - - if (totedge == 0) { - /* flag that mesh has edges */ - (*r_medge) = (MEdge *)MEM_callocN(0, __func__); - (*r_totedge) = 0; - return; - } - - ed = edsort = (EdgeSort *)MEM_mallocN(totedge * sizeof(struct EdgeSort), "EdgeSort"); - - for (a = totface, mface = allface; a > 0; a--, mface++) { - to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2); - if (mface->v4) { - to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3); - to_edgesort(ed++, mface->v3, mface->v4, 0, mface->edcode & ME_V3V4); - to_edgesort(ed++, mface->v4, mface->v1, 0, mface->edcode & ME_V4V1); - } - else if (mface->v3) { - to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3); - to_edgesort(ed++, mface->v3, mface->v1, 0, mface->edcode & ME_V3V1); - } - } - - qsort(edsort, totedge, sizeof(struct EdgeSort), vergedgesort); - - /* count final amount */ - for (a = totedge, ed = edsort; a > 1; a--, ed++) { - /* edge is unique when it differs from next edge, or is last */ - if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) { - totedge_final++; - } - } - totedge_final++; - - medge = (MEdge *)MEM_callocN(sizeof(MEdge) * totedge_final, __func__); - - for (a = totedge, med = medge, ed = edsort; a > 1; a--, ed++) { - /* edge is unique when it differs from next edge, or is last */ - if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) { - med->v1 = ed->v1; - med->v2 = ed->v2; - if (use_old == false || ed->is_draw) { - med->flag = ME_EDGEDRAW | ME_EDGERENDER; - } - if (ed->is_loose) { - med->flag |= ME_LOOSEEDGE; - } - - /* order is swapped so extruding this edge as a surface won't flip face normals - * with cyclic curves */ - if (ed->v1 + 1 != ed->v2) { - SWAP(uint, med->v1, med->v2); - } - med++; - } - else { - /* Equal edge, merge the draw-flag. */ - (ed + 1)->is_draw |= ed->is_draw; - } - } - /* last edge */ - med->v1 = ed->v1; - med->v2 = ed->v2; - med->flag = ME_EDGEDRAW; - if (ed->is_loose) { - med->flag |= ME_LOOSEEDGE; - } - med->flag |= ME_EDGERENDER; - - MEM_freeN(edsort); - - /* set edge members of mloops */ - hash = BLI_edgehash_new_ex(__func__, totedge_final); - for (edge_index = 0, med = medge; edge_index < totedge_final; edge_index++, med++) { - BLI_edgehash_insert(hash, med->v1, med->v2, POINTER_FROM_UINT(edge_index)); - } - - mpoly = allpoly; - for (a = 0; a < totpoly; a++, mpoly++) { - MLoop *ml, *ml_next; - int i = mpoly->totloop; - - ml_next = allloop + mpoly->loopstart; /* first loop */ - ml = &ml_next[i - 1]; /* last loop */ - - while (i-- != 0) { - ml->e = POINTER_AS_UINT(BLI_edgehash_lookup(hash, ml->v, ml_next->v)); - ml = ml_next; - ml_next++; - } - } - - BLI_edgehash_free(hash, nullptr); - - *r_medge = medge; - *r_totedge = totedge_final; -} - -void BKE_mesh_calc_edges_legacy(Mesh *me, const bool use_old) -{ - MEdge *medge; - int totedge = 0; - const Span<MVert> verts = me->verts(); - const Span<MPoly> polys = me->polys(); - MutableSpan<MLoop> loops = me->loops_for_write(); - - mesh_calc_edges_mdata(verts.data(), - (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), - loops.data(), - polys.data(), - verts.size(), - me->totface, - loops.size(), - polys.size(), - use_old, - &medge, - &totedge); - - if (totedge == 0) { - /* flag that mesh has edges */ - me->totedge = 0; - return; - } - - medge = (MEdge *)CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge); - me->totedge = totedge; - - BKE_mesh_strip_loose_faces(me); -} - void BKE_mesh_calc_edges_loose(Mesh *mesh) { const Span<MLoop> loops = mesh->loops(); @@ -1579,7 +1369,7 @@ void BKE_mesh_calc_edges_tessface(Mesh *mesh) BLI_edgesetIterator_step(ehi), i++, med++, index++) { BLI_edgesetIterator_getKey(ehi, &med->v1, &med->v2); - med->flag = ME_EDGEDRAW | ME_EDGERENDER; + med->flag = ME_EDGEDRAW; *index = ORIGINDEX_NONE; } BLI_edgesetIterator_free(ehi); diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 59c2769869d..5400fd78ddb 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -4352,12 +4352,6 @@ void node_type_socket_templates(struct bNodeType *ntype, } } -void node_type_init(struct bNodeType *ntype, - void (*initfunc)(struct bNodeTree *ntree, struct bNode *node)) -{ - ntype->initfunc = initfunc; -} - void node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth) { ntype->width = width; @@ -4405,33 +4399,6 @@ void node_type_storage(bNodeType *ntype, ntype->freefunc = freefunc; } -void node_type_update(struct bNodeType *ntype, - void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node)) -{ - ntype->updatefunc = updatefunc; -} - -void node_type_group_update(struct bNodeType *ntype, - void (*group_update_func)(struct bNodeTree *ntree, struct bNode *node)) -{ - ntype->group_update_func = group_update_func; -} - -void node_type_exec(struct bNodeType *ntype, - NodeInitExecFunction init_exec_fn, - NodeFreeExecFunction free_exec_fn, - NodeExecFunction exec_fn) -{ - ntype->init_exec_fn = init_exec_fn; - ntype->free_exec_fn = free_exec_fn; - ntype->exec_fn = exec_fn; -} - -void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn) -{ - ntype->gpu_fn = gpu_fn; -} - /* callbacks for undefined types */ static bool node_undefined_poll(bNodeType * /*ntype*/, diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c index 3b0f35263d3..d03f12b98bd 100644 --- a/source/blender/blenkernel/intern/pbvh_bmesh.c +++ b/source/blender/blenkernel/intern/pbvh_bmesh.c @@ -1527,8 +1527,8 @@ bool pbvh_bmesh_node_raycast(PBVHNode *node, float location[3] = {0.0f}; madd_v3_v3v3fl(location, ray_start, ray_normal, *depth); for (int j = 0; j < 3; j++) { - if (len_squared_v3v3(location, cos[j]) < - len_squared_v3v3(location, nearest_vertex_co)) { + if (j == 0 || len_squared_v3v3(location, cos[j]) < + len_squared_v3v3(location, nearest_vertex_co)) { copy_v3_v3(nearest_vertex_co, cos[j]); r_active_vertex->i = (intptr_t)node->bm_orvert[node->bm_ortri[i][j]]; } @@ -1559,8 +1559,8 @@ bool pbvh_bmesh_node_raycast(PBVHNode *node, float location[3] = {0.0f}; madd_v3_v3v3fl(location, ray_start, ray_normal, *depth); for (int j = 0; j < 3; j++) { - if (len_squared_v3v3(location, v_tri[j]->co) < - len_squared_v3v3(location, nearest_vertex_co)) { + if (j == 0 || len_squared_v3v3(location, v_tri[j]->co) < + len_squared_v3v3(location, nearest_vertex_co)) { copy_v3_v3(nearest_vertex_co, v_tri[j]->co); r_active_vertex->i = (intptr_t)v_tri[j]; } diff --git a/source/blender/blenkernel/intern/sound.c b/source/blender/blenkernel/intern/sound.c index 1aef42ef037..51a20cf1e35 100644 --- a/source/blender/blenkernel/intern/sound.c +++ b/source/blender/blenkernel/intern/sound.c @@ -1025,7 +1025,7 @@ void BKE_sound_free_waveform(bSound *sound) sound->tags &= ~SOUND_TAGS_WAVEFORM_NO_RELOAD; } -void BKE_sound_read_waveform(Main *bmain, bSound *sound, short *stop) +void BKE_sound_read_waveform(Main *bmain, bSound *sound, bool *stop) { bool need_close_audio_handles = false; if (sound->playback_handle == NULL) { @@ -1041,8 +1041,11 @@ void BKE_sound_read_waveform(Main *bmain, bSound *sound, short *stop) int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND; waveform->data = MEM_mallocN(sizeof(float[3]) * length, "SoundWaveform.samples"); + /* Ideally this would take a boolean argument. */ + short stop_i16 = *stop; waveform->length = AUD_readSound( - sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, stop); + sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, &stop_i16); + *stop = stop_i16 != 0; } else { /* Create an empty waveform here if the sound couldn't be @@ -1381,7 +1384,7 @@ int BKE_sound_scene_playing(Scene *UNUSED(scene)) void BKE_sound_read_waveform(Main *bmain, bSound *sound, /* NOLINTNEXTLINE: readability-non-const-parameter. */ - short *stop) + bool *stop) { UNUSED_VARS(sound, stop, bmain); } diff --git a/source/blender/blenkernel/intern/subdiv_mesh.cc b/source/blender/blenkernel/intern/subdiv_mesh.cc index 3b97c1f5e68..e1c434f2b2e 100644 --- a/source/blender/blenkernel/intern/subdiv_mesh.cc +++ b/source/blender/blenkernel/intern/subdiv_mesh.cc @@ -794,7 +794,7 @@ static void subdiv_copy_edge_data(SubdivMeshContext *ctx, /* TODO: Ensure crease layer isn't copied to result. */ subdiv_edge->flag = 0; if (!ctx->settings->use_optimal_display) { - subdiv_edge->flag |= ME_EDGERENDER; + subdiv_edge->flag |= ME_EDGEDRAW; } if (ctx->edge_origindex != nullptr) { ctx->edge_origindex[subdiv_edge_index] = ORIGINDEX_NONE; @@ -804,7 +804,7 @@ static void subdiv_copy_edge_data(SubdivMeshContext *ctx, const int coarse_edge_index = coarse_edge - ctx->coarse_edges; CustomData_copy_data( &ctx->coarse_mesh->edata, &ctx->subdiv_mesh->edata, coarse_edge_index, subdiv_edge_index, 1); - subdiv_edge->flag |= ME_EDGERENDER; + subdiv_edge->flag |= ME_EDGEDRAW; } static void subdiv_mesh_edge(const SubdivForeachContext *foreach_context, diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c index c37f4b1ea2f..80fb637b76e 100644 --- a/source/blender/blenkernel/intern/subsurf_ccg.c +++ b/source/blender/blenkernel/intern/subsurf_ccg.c @@ -962,7 +962,7 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge) int edgeSize = ccgSubSurf_getEdgeSize(ss); uint i = 0; short *edgeFlags = ccgdm->edgeFlags; - const short ed_interior_flag = ccgdm->drawInteriorEdges ? (ME_EDGEDRAW | ME_EDGERENDER) : 0; + const short ed_interior_flag = ccgdm->drawInteriorEdges ? ME_EDGEDRAW : 0; totface = ccgSubSurf_getNumFaces(ss); for (index = 0; index < totface; index++) { @@ -1005,11 +1005,11 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge) if (edgeFlags) { if (edgeIdx != -1) { - ed_flag |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER); + ed_flag |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW); } } else { - ed_flag |= ME_EDGEDRAW | ME_EDGERENDER; + ed_flag |= ME_EDGEDRAW; } for (x = 0; x < edgeSize - 1; x++) { diff --git a/source/blender/blenkernel/intern/tracking_solver.c b/source/blender/blenkernel/intern/tracking_solver.c index 11041b4c0fd..e784c39ba08 100644 --- a/source/blender/blenkernel/intern/tracking_solver.c +++ b/source/blender/blenkernel/intern/tracking_solver.c @@ -56,8 +56,8 @@ typedef struct MovieReconstructContext { } MovieReconstructContext; typedef struct ReconstructProgressData { - short *stop; - short *do_update; + bool *stop; + bool *do_update; float *progress; char *stats_message; int message_size; @@ -465,8 +465,8 @@ static void reconstructionOptionsFromContext(libmv_ReconstructionOptions *recons } void BKE_tracking_reconstruction_solve(MovieReconstructContext *context, - short *stop, - short *do_update, + bool *stop, + bool *do_update, float *progress, char *stats_message, int message_size) diff --git a/source/blender/blenkernel/intern/writeffmpeg.c b/source/blender/blenkernel/intern/writeffmpeg.c index 1d4cbc48833..8d6dba440fd 100644 --- a/source/blender/blenkernel/intern/writeffmpeg.c +++ b/source/blender/blenkernel/intern/writeffmpeg.c @@ -141,19 +141,25 @@ static int write_audio_frame(FFMpegContext *context) frame->pts = context->audio_time / av_q2d(c->time_base); frame->nb_samples = context->audio_input_samples; frame->format = c->sample_fmt; +# ifdef FFMPEG_USE_OLD_CHANNEL_VARS frame->channels = c->channels; frame->channel_layout = c->channel_layout; + const int num_channels = c->channels; +# else + av_channel_layout_copy(&frame->ch_layout, &c->ch_layout); + const int num_channels = c->ch_layout.nb_channels; +# endif if (context->audio_deinterleave) { int channel, i; uint8_t *temp; - for (channel = 0; channel < c->channels; channel++) { + for (channel = 0; channel < num_channels; channel++) { for (i = 0; i < frame->nb_samples; i++) { memcpy(context->audio_deinterleave_buffer + (i + channel * frame->nb_samples) * context->audio_sample_size, context->audio_input_buffer + - (c->channels * i + channel) * context->audio_sample_size, + (num_channels * i + channel) * context->audio_sample_size, context->audio_sample_size); } } @@ -164,10 +170,11 @@ static int write_audio_frame(FFMpegContext *context) } avcodec_fill_audio_frame(frame, - c->channels, + num_channels, c->sample_fmt, context->audio_input_buffer, - context->audio_input_samples * c->channels * context->audio_sample_size, + context->audio_input_samples * num_channels * + context->audio_sample_size, 1); int success = 1; @@ -944,25 +951,34 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, c->sample_rate = rd->ffcodecdata.audio_mixrate; c->bit_rate = context->ffmpeg_audio_bitrate * 1000; c->sample_fmt = AV_SAMPLE_FMT_S16; - c->channels = rd->ffcodecdata.audio_channels; + const int num_channels = rd->ffcodecdata.audio_channels; + int channel_layout_mask = 0; switch (rd->ffcodecdata.audio_channels) { case FFM_CHANNELS_MONO: - c->channel_layout = AV_CH_LAYOUT_MONO; + channel_layout_mask = AV_CH_LAYOUT_MONO; break; case FFM_CHANNELS_STEREO: - c->channel_layout = AV_CH_LAYOUT_STEREO; + channel_layout_mask = AV_CH_LAYOUT_STEREO; break; case FFM_CHANNELS_SURROUND4: - c->channel_layout = AV_CH_LAYOUT_QUAD; + channel_layout_mask = AV_CH_LAYOUT_QUAD; break; case FFM_CHANNELS_SURROUND51: - c->channel_layout = AV_CH_LAYOUT_5POINT1_BACK; + channel_layout_mask = AV_CH_LAYOUT_5POINT1_BACK; break; case FFM_CHANNELS_SURROUND71: - c->channel_layout = AV_CH_LAYOUT_7POINT1; + channel_layout_mask = AV_CH_LAYOUT_7POINT1; break; } + BLI_assert(channel_layout_mask != 0); + +# ifdef FFMPEG_USE_OLD_CHANNEL_VARS + c->channels = num_channels; + c->channel_layout = channel_layout_mask; +# else + av_channel_layout_from_mask(&c->ch_layout, channel_layout_mask); +# endif if (request_float_audio_buffer(codec_id)) { /* mainly for AAC codec which is experimental */ @@ -1027,7 +1043,7 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, * not sure if that is needed anymore, so let's try out if there are any * complaints regarding some FFmpeg versions users might have. */ context->audio_input_samples = AV_INPUT_BUFFER_MIN_SIZE * 8 / c->bits_per_coded_sample / - c->channels; + num_channels; } else { context->audio_input_samples = c->frame_size; @@ -1037,11 +1053,11 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, context->audio_sample_size = av_get_bytes_per_sample(c->sample_fmt); - context->audio_input_buffer = (uint8_t *)av_malloc(context->audio_input_samples * c->channels * + context->audio_input_buffer = (uint8_t *)av_malloc(context->audio_input_samples * num_channels * context->audio_sample_size); if (context->audio_deinterleave) { context->audio_deinterleave_buffer = (uint8_t *)av_malloc( - context->audio_input_samples * c->channels * context->audio_sample_size); + context->audio_input_samples * num_channels * context->audio_sample_size); } context->audio_time = 0.0f; @@ -1432,7 +1448,11 @@ int BKE_ffmpeg_start(void *context_v, AVCodecContext *c = context->audio_codec; AUD_DeviceSpecs specs; +# ifdef FFMPEG_USE_OLD_CHANNEL_VARS specs.channels = c->channels; +# else + specs.channels = c->ch_layout.nb_channels; +# endif switch (av_get_packed_sample_fmt(c->sample_fmt)) { case AV_SAMPLE_FMT_U8: |