Skip to content

Commit

Permalink
Refactor: Use StringRef for attribute API and BMesh CustomData functions
Browse files Browse the repository at this point in the history
Replace `const char *` with `StringRef` for the API in `BKE_attribute.h`.
The benefits are slightly simpler code and possibly slightly improved
performance through avoiding the need to measure string length.

Pull Request: https://projects.blender.org/blender/blender/pulls/134183
  • Loading branch information
HansGoudey authored and Hans Goudey committed Feb 6, 2025
1 parent d279a80 commit 54dc692
Show file tree
Hide file tree
Showing 24 changed files with 250 additions and 219 deletions.
3 changes: 2 additions & 1 deletion intern/cycles/blender/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ static void attr_create_generic(Scene *scene,
const blender::bke::AttributeAccessor b_attributes = b_mesh.attributes();
AttributeSet &attributes = (subdivision) ? mesh->subd_attributes : mesh->attributes;
static const ustring u_velocity("velocity");
const ustring default_color_name{BKE_id_attributes_default_color_name(&b_mesh.id)};
const ustring default_color_name{
std::string_view(BKE_id_attributes_default_color_name(&b_mesh.id).value_or(""))};

b_attributes.foreach_attribute([&](const blender::bke::AttributeIter &iter) {
const ustring name{std::string_view(iter.name)};
Expand Down
43 changes: 25 additions & 18 deletions source/blender/blenkernel/BKE_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include <optional>
#include <string>

#include "BLI_string_ref.hh"
Expand Down Expand Up @@ -81,50 +82,52 @@ class AttributeOwner {
* Create a new attribute layer.
*/
struct CustomDataLayer *BKE_attribute_new(AttributeOwner &owner,
const char *name,
blender::StringRef name,
eCustomDataType type,
blender::bke::AttrDomain domain,
struct ReportList *reports);
bool BKE_attribute_remove(AttributeOwner &owner, const char *name, struct ReportList *reports);
bool BKE_attribute_remove(AttributeOwner &owner,
blender::StringRef name,
struct ReportList *reports);

/**
* Creates a duplicate attribute layer.
*/
struct CustomDataLayer *BKE_attribute_duplicate(AttributeOwner &owner,
const char *name,
blender::StringRef name,
struct ReportList *reports);

struct CustomDataLayer *BKE_attribute_find(const AttributeOwner &owner,
const char *name,
blender::StringRef name,
eCustomDataType type,
blender::bke::AttrDomain domain);

const struct CustomDataLayer *BKE_attribute_search(const AttributeOwner &owner,
const char *name,
blender::StringRef name,
eCustomDataMask type,
AttrDomainMask domain_mask);

struct CustomDataLayer *BKE_attribute_search_for_write(AttributeOwner &owner,
const char *name,
blender::StringRef name,
eCustomDataMask type,
AttrDomainMask domain_mask);

blender::bke::AttrDomain BKE_attribute_domain(const AttributeOwner &owner,
const struct CustomDataLayer *layer);
int BKE_attribute_domain_size(const AttributeOwner &owner, int domain);
int BKE_attribute_data_length(AttributeOwner &owner, struct CustomDataLayer *layer);
bool BKE_attribute_required(const AttributeOwner &owner, const char *name);
bool BKE_attribute_required(const AttributeOwner &owner, blender::StringRef name);
bool BKE_attribute_rename(AttributeOwner &owner,
const char *old_name,
const char *new_name,
blender::StringRef old_name,
blender::StringRef new_name,
struct ReportList *reports);

int BKE_attributes_length(const AttributeOwner &owner,
AttrDomainMask domain_mask,
eCustomDataMask mask);

struct CustomDataLayer *BKE_attributes_active_get(AttributeOwner &owner);
void BKE_attributes_active_set(AttributeOwner &owner, const char *name);
void BKE_attributes_active_set(AttributeOwner &owner, blender::StringRef name);
void BKE_attributes_active_clear(AttributeOwner &owner);
int *BKE_attributes_active_index_p(AttributeOwner &owner);

Expand All @@ -141,18 +144,22 @@ int BKE_attribute_to_index(const AttributeOwner &owner,
AttrDomainMask domain_mask,
eCustomDataMask layer_mask);

const char *BKE_id_attributes_active_color_name(const struct ID *id);
const char *BKE_id_attributes_default_color_name(const struct ID *id);
void BKE_id_attributes_active_color_set(struct ID *id, const char *name);
std::optional<blender::StringRef> BKE_id_attributes_active_color_name(const struct ID *id);
std::optional<blender::StringRef> BKE_id_attributes_default_color_name(const struct ID *id);
void BKE_id_attributes_active_color_set(struct ID *id, std::optional<blender::StringRef> name);
void BKE_id_attributes_active_color_clear(struct ID *id);
void BKE_id_attributes_default_color_set(struct ID *id, const char *name);
void BKE_id_attributes_default_color_set(struct ID *id, std::optional<blender::StringRef> name);

const struct CustomDataLayer *BKE_id_attributes_color_find(const struct ID *id, const char *name);
const struct CustomDataLayer *BKE_id_attributes_color_find(const struct ID *id,
blender::StringRef name);
bool BKE_color_attribute_supported(const struct Mesh &mesh, const blender::StringRef name);

std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner,
const blender::StringRef name);

const char *BKE_uv_map_vert_select_name_get(const char *uv_map_name, char *buffer);
const char *BKE_uv_map_edge_select_name_get(const char *uv_map_name, char *buffer);
const char *BKE_uv_map_pin_name_get(const char *uv_map_name, char *buffer);
[[nodiscard]] blender::StringRef BKE_uv_map_vert_select_name_get(blender::StringRef uv_map_name,
char *buffer);
[[nodiscard]] blender::StringRef BKE_uv_map_edge_select_name_get(blender::StringRef uv_map_name,
char *buffer);
[[nodiscard]] blender::StringRef BKE_uv_map_pin_name_get(blender::StringRef uv_map_name,
char *buffer);
4 changes: 3 additions & 1 deletion source/blender/blenkernel/BKE_curves.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "DNA_curves_types.h"

#include "BLI_string_ref.hh"

/** \file
* \ingroup bke
* \brief Low-level operations for curves that cannot be defined in the C++ header yet.
Expand All @@ -23,7 +25,7 @@ struct Scene;

struct Curves *BKE_curves_add(struct Main *bmain, const char *name);

bool BKE_curves_attribute_required(const struct Curves *curves, const char *name);
bool BKE_curves_attribute_required(const struct Curves *curves, blender::StringRef name);

/* Depsgraph */

Expand Down
3 changes: 2 additions & 1 deletion source/blender/blenkernel/BKE_grease_pencil.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,8 @@ inline bool GreasePencil::has_active_group() const
return (this->active_node != nullptr) && (this->active_node->wrap().is_group());
}

bool BKE_grease_pencil_drawing_attribute_required(const GreasePencilDrawing *, const char *name);
bool BKE_grease_pencil_drawing_attribute_required(const GreasePencilDrawing *,
blender::StringRef name);

GreasePencil *BKE_grease_pencil_add(Main *bmain, const char *name);
GreasePencil *BKE_grease_pencil_new_nomain();
Expand Down
3 changes: 2 additions & 1 deletion source/blender/blenkernel/BKE_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstdint>

#include "BLI_array.hh"
#include "BLI_string_ref.hh"

#include "DNA_mesh_types.h"

Expand Down Expand Up @@ -137,7 +138,7 @@ Mesh *BKE_mesh_copy_for_eval(const Mesh &source);
Mesh *BKE_mesh_new_nomain_from_curve(const Object *ob);
Mesh *BKE_mesh_new_nomain_from_curve_displist(const Object *ob, const ListBase *dispbase);

bool BKE_mesh_attribute_required(const char *name);
bool BKE_mesh_attribute_required(blender::StringRef name);

blender::Array<blender::float3> BKE_mesh_orco_verts_get(const Object *ob);
void BKE_mesh_orco_verts_transform(Mesh *mesh,
Expand Down
3 changes: 2 additions & 1 deletion source/blender/blenkernel/BKE_pointcloud.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "BLI_bounds_types.hh"
#include "BLI_math_vector_types.hh"
#include "BLI_shared_cache.hh"
#include "BLI_string_ref.hh"

#include "DNA_pointcloud_types.h"

Expand Down Expand Up @@ -53,7 +54,7 @@ PointCloud *BKE_pointcloud_add_default(Main *bmain, const char *name);
PointCloud *BKE_pointcloud_new_nomain(int totpoint);
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst);

bool BKE_pointcloud_attribute_required(const PointCloud *pointcloud, const char *name);
bool BKE_pointcloud_attribute_required(const PointCloud *pointcloud, blender::StringRef name);

/* Dependency Graph */

Expand Down
Loading

0 comments on commit 54dc692

Please sign in to comment.